pmem.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Persistent Memory Driver
  3. *
  4. * Copyright (c) 2014-2015, Intel Corporation.
  5. * Copyright (c) 2015, Christoph Hellwig <hch@lst.de>.
  6. * Copyright (c) 2015, Boaz Harrosh <boaz@plexistor.com>.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. */
  17. #include <asm/cacheflush.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/hdreg.h>
  20. #include <linux/init.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/badblocks.h>
  25. #include <linux/memremap.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/blk-mq.h>
  28. #include <linux/pfn_t.h>
  29. #include <linux/slab.h>
  30. #include <linux/pmem.h>
  31. #include <linux/uio.h>
  32. #include <linux/dax.h>
  33. #include <linux/nd.h>
  34. #include "pmem.h"
  35. #include "pfn.h"
  36. #include "nd.h"
  37. static struct device *to_dev(struct pmem_device *pmem)
  38. {
  39. /*
  40. * nvdimm bus services need a 'dev' parameter, and we record the device
  41. * at init in bb.dev.
  42. */
  43. return pmem->bb.dev;
  44. }
  45. static struct nd_region *to_region(struct pmem_device *pmem)
  46. {
  47. return to_nd_region(to_dev(pmem)->parent);
  48. }
  49. static int pmem_clear_poison(struct pmem_device *pmem, phys_addr_t offset,
  50. unsigned int len)
  51. {
  52. struct device *dev = to_dev(pmem);
  53. sector_t sector;
  54. long cleared;
  55. int rc = 0;
  56. sector = (offset - pmem->data_offset) / 512;
  57. cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len);
  58. if (cleared < len)
  59. rc = -EIO;
  60. if (cleared > 0 && cleared / 512) {
  61. cleared /= 512;
  62. dev_dbg(dev, "%s: %#llx clear %ld sector%s\n", __func__,
  63. (unsigned long long) sector, cleared,
  64. cleared > 1 ? "s" : "");
  65. badblocks_clear(&pmem->bb, sector, cleared);
  66. }
  67. invalidate_pmem(pmem->virt_addr + offset, len);
  68. return rc;
  69. }
  70. static void write_pmem(void *pmem_addr, struct page *page,
  71. unsigned int off, unsigned int len)
  72. {
  73. void *mem = kmap_atomic(page);
  74. memcpy_flushcache(pmem_addr, mem + off, len);
  75. kunmap_atomic(mem);
  76. }
  77. static int read_pmem(struct page *page, unsigned int off,
  78. void *pmem_addr, unsigned int len)
  79. {
  80. int rc;
  81. void *mem = kmap_atomic(page);
  82. rc = memcpy_mcsafe(mem + off, pmem_addr, len);
  83. kunmap_atomic(mem);
  84. if (rc)
  85. return -EIO;
  86. return 0;
  87. }
  88. static int pmem_do_bvec(struct pmem_device *pmem, struct page *page,
  89. unsigned int len, unsigned int off, bool is_write,
  90. sector_t sector)
  91. {
  92. int rc = 0;
  93. bool bad_pmem = false;
  94. phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
  95. void *pmem_addr = pmem->virt_addr + pmem_off;
  96. if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
  97. bad_pmem = true;
  98. if (!is_write) {
  99. if (unlikely(bad_pmem))
  100. rc = -EIO;
  101. else {
  102. rc = read_pmem(page, off, pmem_addr, len);
  103. flush_dcache_page(page);
  104. }
  105. } else {
  106. /*
  107. * Note that we write the data both before and after
  108. * clearing poison. The write before clear poison
  109. * handles situations where the latest written data is
  110. * preserved and the clear poison operation simply marks
  111. * the address range as valid without changing the data.
  112. * In this case application software can assume that an
  113. * interrupted write will either return the new good
  114. * data or an error.
  115. *
  116. * However, if pmem_clear_poison() leaves the data in an
  117. * indeterminate state we need to perform the write
  118. * after clear poison.
  119. */
  120. flush_dcache_page(page);
  121. write_pmem(pmem_addr, page, off, len);
  122. if (unlikely(bad_pmem)) {
  123. rc = pmem_clear_poison(pmem, pmem_off, len);
  124. write_pmem(pmem_addr, page, off, len);
  125. }
  126. }
  127. return rc;
  128. }
  129. /* account for REQ_FLUSH rename, replace with REQ_PREFLUSH after v4.8-rc1 */
  130. #ifndef REQ_FLUSH
  131. #define REQ_FLUSH REQ_PREFLUSH
  132. #endif
  133. static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
  134. {
  135. int rc = 0;
  136. bool do_acct;
  137. unsigned long start;
  138. struct bio_vec bvec;
  139. struct bvec_iter iter;
  140. struct pmem_device *pmem = q->queuedata;
  141. struct nd_region *nd_region = to_region(pmem);
  142. if (bio->bi_opf & REQ_FLUSH)
  143. nvdimm_flush(nd_region);
  144. do_acct = nd_iostat_start(bio, &start);
  145. bio_for_each_segment(bvec, bio, iter) {
  146. rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len,
  147. bvec.bv_offset, op_is_write(bio_op(bio)),
  148. iter.bi_sector);
  149. if (rc) {
  150. bio->bi_error = rc;
  151. break;
  152. }
  153. }
  154. if (do_acct)
  155. nd_iostat_end(bio, start);
  156. if (bio->bi_opf & REQ_FUA)
  157. nvdimm_flush(nd_region);
  158. bio_endio(bio);
  159. return BLK_QC_T_NONE;
  160. }
  161. static int pmem_rw_page(struct block_device *bdev, sector_t sector,
  162. struct page *page, bool is_write)
  163. {
  164. struct pmem_device *pmem = bdev->bd_queue->queuedata;
  165. int rc;
  166. rc = pmem_do_bvec(pmem, page, PAGE_SIZE, 0, is_write, sector);
  167. /*
  168. * The ->rw_page interface is subtle and tricky. The core
  169. * retries on any error, so we can only invoke page_endio() in
  170. * the successful completion case. Otherwise, we'll see crashes
  171. * caused by double completion.
  172. */
  173. if (rc == 0)
  174. page_endio(page, is_write, 0);
  175. return rc;
  176. }
  177. /* see "strong" declaration in tools/testing/nvdimm/pmem-dax.c */
  178. __weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
  179. long nr_pages, void **kaddr, pfn_t *pfn)
  180. {
  181. resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset;
  182. if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
  183. PFN_PHYS(nr_pages))))
  184. return -EIO;
  185. *kaddr = pmem->virt_addr + offset;
  186. *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags);
  187. /*
  188. * If badblocks are present, limit known good range to the
  189. * requested range.
  190. */
  191. if (unlikely(pmem->bb.count))
  192. return nr_pages;
  193. return PHYS_PFN(pmem->size - pmem->pfn_pad - offset);
  194. }
  195. static const struct block_device_operations pmem_fops = {
  196. .owner = THIS_MODULE,
  197. .rw_page = pmem_rw_page,
  198. .revalidate_disk = nvdimm_revalidate_disk,
  199. };
  200. static long pmem_dax_direct_access(struct dax_device *dax_dev,
  201. pgoff_t pgoff, long nr_pages, void **kaddr, pfn_t *pfn)
  202. {
  203. struct pmem_device *pmem = dax_get_private(dax_dev);
  204. return __pmem_direct_access(pmem, pgoff, nr_pages, kaddr, pfn);
  205. }
  206. static size_t pmem_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
  207. void *addr, size_t bytes, struct iov_iter *i)
  208. {
  209. return copy_from_iter_flushcache(addr, bytes, i);
  210. }
  211. static void pmem_dax_flush(struct dax_device *dax_dev, pgoff_t pgoff,
  212. void *addr, size_t size)
  213. {
  214. arch_wb_cache_pmem(addr, size);
  215. }
  216. static const struct dax_operations pmem_dax_ops = {
  217. .direct_access = pmem_dax_direct_access,
  218. .copy_from_iter = pmem_copy_from_iter,
  219. .flush = pmem_dax_flush,
  220. };
  221. static void pmem_release_queue(void *q)
  222. {
  223. blk_cleanup_queue(q);
  224. }
  225. static void pmem_freeze_queue(void *q)
  226. {
  227. blk_freeze_queue_start(q);
  228. }
  229. static void pmem_release_disk(void *__pmem)
  230. {
  231. struct pmem_device *pmem = __pmem;
  232. kill_dax(pmem->dax_dev);
  233. put_dax(pmem->dax_dev);
  234. del_gendisk(pmem->disk);
  235. put_disk(pmem->disk);
  236. }
  237. static int pmem_attach_disk(struct device *dev,
  238. struct nd_namespace_common *ndns)
  239. {
  240. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  241. struct nd_region *nd_region = to_nd_region(dev->parent);
  242. struct vmem_altmap __altmap, *altmap = NULL;
  243. struct resource *res = &nsio->res;
  244. struct nd_pfn *nd_pfn = NULL;
  245. struct dax_device *dax_dev;
  246. int nid = dev_to_node(dev);
  247. struct nd_pfn_sb *pfn_sb;
  248. struct pmem_device *pmem;
  249. struct resource pfn_res;
  250. struct request_queue *q;
  251. struct gendisk *disk;
  252. void *addr;
  253. /* while nsio_rw_bytes is active, parse a pfn info block if present */
  254. if (is_nd_pfn(dev)) {
  255. nd_pfn = to_nd_pfn(dev);
  256. altmap = nvdimm_setup_pfn(nd_pfn, &pfn_res, &__altmap);
  257. if (IS_ERR(altmap))
  258. return PTR_ERR(altmap);
  259. }
  260. /* we're attaching a block device, disable raw namespace access */
  261. devm_nsio_disable(dev, nsio);
  262. pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
  263. if (!pmem)
  264. return -ENOMEM;
  265. dev_set_drvdata(dev, pmem);
  266. pmem->phys_addr = res->start;
  267. pmem->size = resource_size(res);
  268. if (!IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE)
  269. || nvdimm_has_flush(nd_region) < 0)
  270. dev_warn(dev, "unable to guarantee persistence of writes\n");
  271. if (!devm_request_mem_region(dev, res->start, resource_size(res),
  272. dev_name(&ndns->dev))) {
  273. dev_warn(dev, "could not reserve region %pR\n", res);
  274. return -EBUSY;
  275. }
  276. q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(dev));
  277. if (!q)
  278. return -ENOMEM;
  279. if (devm_add_action_or_reset(dev, pmem_release_queue, q))
  280. return -ENOMEM;
  281. pmem->pfn_flags = PFN_DEV;
  282. if (is_nd_pfn(dev)) {
  283. addr = devm_memremap_pages(dev, &pfn_res, &q->q_usage_counter,
  284. altmap);
  285. pfn_sb = nd_pfn->pfn_sb;
  286. pmem->data_offset = le64_to_cpu(pfn_sb->dataoff);
  287. pmem->pfn_pad = resource_size(res) - resource_size(&pfn_res);
  288. pmem->pfn_flags |= PFN_MAP;
  289. res = &pfn_res; /* for badblocks populate */
  290. res->start += pmem->data_offset;
  291. } else if (pmem_should_map_pages(dev)) {
  292. addr = devm_memremap_pages(dev, &nsio->res,
  293. &q->q_usage_counter, NULL);
  294. pmem->pfn_flags |= PFN_MAP;
  295. } else
  296. addr = devm_memremap(dev, pmem->phys_addr,
  297. pmem->size, ARCH_MEMREMAP_PMEM);
  298. /*
  299. * At release time the queue must be frozen before
  300. * devm_memremap_pages is unwound
  301. */
  302. if (devm_add_action_or_reset(dev, pmem_freeze_queue, q))
  303. return -ENOMEM;
  304. if (IS_ERR(addr))
  305. return PTR_ERR(addr);
  306. pmem->virt_addr = addr;
  307. blk_queue_write_cache(q, true, true);
  308. blk_queue_make_request(q, pmem_make_request);
  309. blk_queue_physical_block_size(q, PAGE_SIZE);
  310. blk_queue_max_hw_sectors(q, UINT_MAX);
  311. blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
  312. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
  313. queue_flag_set_unlocked(QUEUE_FLAG_DAX, q);
  314. q->queuedata = pmem;
  315. disk = alloc_disk_node(0, nid);
  316. if (!disk)
  317. return -ENOMEM;
  318. pmem->disk = disk;
  319. disk->fops = &pmem_fops;
  320. disk->queue = q;
  321. disk->flags = GENHD_FL_EXT_DEVT;
  322. nvdimm_namespace_disk_name(ndns, disk->disk_name);
  323. set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset)
  324. / 512);
  325. if (devm_init_badblocks(dev, &pmem->bb))
  326. return -ENOMEM;
  327. nvdimm_badblocks_populate(nd_region, &pmem->bb, res);
  328. disk->bb = &pmem->bb;
  329. dax_dev = alloc_dax(pmem, disk->disk_name, &pmem_dax_ops);
  330. if (!dax_dev) {
  331. put_disk(disk);
  332. return -ENOMEM;
  333. }
  334. pmem->dax_dev = dax_dev;
  335. device_add_disk(dev, disk);
  336. if (devm_add_action_or_reset(dev, pmem_release_disk, pmem))
  337. return -ENOMEM;
  338. revalidate_disk(disk);
  339. return 0;
  340. }
  341. static int nd_pmem_probe(struct device *dev)
  342. {
  343. struct nd_namespace_common *ndns;
  344. ndns = nvdimm_namespace_common_probe(dev);
  345. if (IS_ERR(ndns))
  346. return PTR_ERR(ndns);
  347. if (devm_nsio_enable(dev, to_nd_namespace_io(&ndns->dev)))
  348. return -ENXIO;
  349. if (is_nd_btt(dev))
  350. return nvdimm_namespace_attach_btt(ndns);
  351. if (is_nd_pfn(dev))
  352. return pmem_attach_disk(dev, ndns);
  353. /* if we find a valid info-block we'll come back as that personality */
  354. if (nd_btt_probe(dev, ndns) == 0 || nd_pfn_probe(dev, ndns) == 0
  355. || nd_dax_probe(dev, ndns) == 0)
  356. return -ENXIO;
  357. /* ...otherwise we're just a raw pmem device */
  358. return pmem_attach_disk(dev, ndns);
  359. }
  360. static int nd_pmem_remove(struct device *dev)
  361. {
  362. if (is_nd_btt(dev))
  363. nvdimm_namespace_detach_btt(to_nd_btt(dev));
  364. nvdimm_flush(to_nd_region(dev->parent));
  365. return 0;
  366. }
  367. static void nd_pmem_shutdown(struct device *dev)
  368. {
  369. nvdimm_flush(to_nd_region(dev->parent));
  370. }
  371. static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
  372. {
  373. struct nd_region *nd_region;
  374. resource_size_t offset = 0, end_trunc = 0;
  375. struct nd_namespace_common *ndns;
  376. struct nd_namespace_io *nsio;
  377. struct resource res;
  378. struct badblocks *bb;
  379. if (event != NVDIMM_REVALIDATE_POISON)
  380. return;
  381. if (is_nd_btt(dev)) {
  382. struct nd_btt *nd_btt = to_nd_btt(dev);
  383. ndns = nd_btt->ndns;
  384. nd_region = to_nd_region(ndns->dev.parent);
  385. nsio = to_nd_namespace_io(&ndns->dev);
  386. bb = &nsio->bb;
  387. } else {
  388. struct pmem_device *pmem = dev_get_drvdata(dev);
  389. nd_region = to_region(pmem);
  390. bb = &pmem->bb;
  391. if (is_nd_pfn(dev)) {
  392. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  393. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  394. ndns = nd_pfn->ndns;
  395. offset = pmem->data_offset +
  396. __le32_to_cpu(pfn_sb->start_pad);
  397. end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
  398. } else {
  399. ndns = to_ndns(dev);
  400. }
  401. nsio = to_nd_namespace_io(&ndns->dev);
  402. }
  403. res.start = nsio->res.start + offset;
  404. res.end = nsio->res.end - end_trunc;
  405. nvdimm_badblocks_populate(nd_region, bb, &res);
  406. }
  407. MODULE_ALIAS("pmem");
  408. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
  409. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
  410. static struct nd_device_driver nd_pmem_driver = {
  411. .probe = nd_pmem_probe,
  412. .remove = nd_pmem_remove,
  413. .notify = nd_pmem_notify,
  414. .shutdown = nd_pmem_shutdown,
  415. .drv = {
  416. .name = "nd_pmem",
  417. },
  418. .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
  419. };
  420. static int __init pmem_init(void)
  421. {
  422. return nd_driver_register(&nd_pmem_driver);
  423. }
  424. module_init(pmem_init);
  425. static void pmem_exit(void)
  426. {
  427. driver_unregister(&nd_pmem_driver.drv);
  428. }
  429. module_exit(pmem_exit);
  430. MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
  431. MODULE_LICENSE("GPL v2");