pmem.c 14 KB

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