pmem.c 15 KB

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