pmem.c 14 KB

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