pmem.c 13 KB

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