1 // SPDX-License-Identifier: GPL-2.0-only
3 * Persistent Memory Driver
5 * Copyright (c) 2014-2015, Intel Corporation.
6 * Copyright (c) 2015, Christoph Hellwig <hch@lst.de>.
7 * Copyright (c) 2015, Boaz Harrosh <boaz@plexistor.com>.
10 #include <linux/blkdev.h>
11 #include <linux/pagemap.h>
12 #include <linux/hdreg.h>
13 #include <linux/init.h>
14 #include <linux/platform_device.h>
15 #include <linux/set_memory.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/badblocks.h>
19 #include <linux/memremap.h>
20 #include <linux/vmalloc.h>
21 #include <linux/blk-mq.h>
22 #include <linux/pfn_t.h>
23 #include <linux/slab.h>
24 #include <linux/uio.h>
25 #include <linux/dax.h>
28 #include <asm/cacheflush.h>
34 static struct device *to_dev(struct pmem_device *pmem)
37 * nvdimm bus services need a 'dev' parameter, and we record the device
43 static struct nd_region *to_region(struct pmem_device *pmem)
45 return to_nd_region(to_dev(pmem)->parent);
48 static void hwpoison_clear(struct pmem_device *pmem,
49 phys_addr_t phys, unsigned int len)
51 unsigned long pfn_start, pfn_end, pfn;
53 /* only pmem in the linear map supports HWPoison */
54 if (is_vmalloc_addr(pmem->virt_addr))
57 pfn_start = PHYS_PFN(phys);
58 pfn_end = pfn_start + PHYS_PFN(len);
59 for (pfn = pfn_start; pfn < pfn_end; pfn++) {
60 struct page *page = pfn_to_page(pfn);
63 * Note, no need to hold a get_dev_pagemap() reference
64 * here since we're in the driver I/O path and
65 * outstanding I/O requests pin the dev_pagemap.
67 if (test_and_clear_pmem_poison(page))
68 clear_mce_nospec(pfn);
72 static blk_status_t pmem_clear_poison(struct pmem_device *pmem,
73 phys_addr_t offset, unsigned int len)
75 struct device *dev = to_dev(pmem);
78 blk_status_t rc = BLK_STS_OK;
80 sector = (offset - pmem->data_offset) / 512;
82 cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len);
85 if (cleared > 0 && cleared / 512) {
86 hwpoison_clear(pmem, pmem->phys_addr + offset, cleared);
88 dev_dbg(dev, "%#llx clear %ld sector%s\n",
89 (unsigned long long) sector, cleared,
90 cleared > 1 ? "s" : "");
91 badblocks_clear(&pmem->bb, sector, cleared);
93 sysfs_notify_dirent(pmem->bb_state);
96 arch_invalidate_pmem(pmem->virt_addr + offset, len);
101 static void write_pmem(void *pmem_addr, struct page *page,
102 unsigned int off, unsigned int len)
108 mem = kmap_atomic(page);
109 chunk = min_t(unsigned int, len, PAGE_SIZE - off);
110 memcpy_flushcache(pmem_addr, mem + off, chunk);
119 static blk_status_t read_pmem(struct page *page, unsigned int off,
120 void *pmem_addr, unsigned int len)
127 mem = kmap_atomic(page);
128 chunk = min_t(unsigned int, len, PAGE_SIZE - off);
129 rem = copy_mc_to_kernel(mem + off, pmem_addr, chunk);
132 return BLK_STS_IOERR;
141 static blk_status_t pmem_do_read(struct pmem_device *pmem,
142 struct page *page, unsigned int page_off,
143 sector_t sector, unsigned int len)
146 phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
147 void *pmem_addr = pmem->virt_addr + pmem_off;
149 if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
150 return BLK_STS_IOERR;
152 rc = read_pmem(page, page_off, pmem_addr, len);
153 flush_dcache_page(page);
157 static blk_status_t pmem_do_write(struct pmem_device *pmem,
158 struct page *page, unsigned int page_off,
159 sector_t sector, unsigned int len)
161 blk_status_t rc = BLK_STS_OK;
162 bool bad_pmem = false;
163 phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
164 void *pmem_addr = pmem->virt_addr + pmem_off;
166 if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
170 * Note that we write the data both before and after
171 * clearing poison. The write before clear poison
172 * handles situations where the latest written data is
173 * preserved and the clear poison operation simply marks
174 * the address range as valid without changing the data.
175 * In this case application software can assume that an
176 * interrupted write will either return the new good
179 * However, if pmem_clear_poison() leaves the data in an
180 * indeterminate state we need to perform the write
181 * after clear poison.
183 flush_dcache_page(page);
184 write_pmem(pmem_addr, page, page_off, len);
185 if (unlikely(bad_pmem)) {
186 rc = pmem_clear_poison(pmem, pmem_off, len);
187 write_pmem(pmem_addr, page, page_off, len);
193 static void pmem_submit_bio(struct bio *bio)
200 struct bvec_iter iter;
201 struct pmem_device *pmem = bio->bi_bdev->bd_disk->private_data;
202 struct nd_region *nd_region = to_region(pmem);
204 if (bio->bi_opf & REQ_PREFLUSH)
205 ret = nvdimm_flush(nd_region, bio);
207 do_acct = blk_queue_io_stat(bio->bi_bdev->bd_disk->queue);
209 start = bio_start_io_acct(bio);
210 bio_for_each_segment(bvec, bio, iter) {
211 if (op_is_write(bio_op(bio)))
212 rc = pmem_do_write(pmem, bvec.bv_page, bvec.bv_offset,
213 iter.bi_sector, bvec.bv_len);
215 rc = pmem_do_read(pmem, bvec.bv_page, bvec.bv_offset,
216 iter.bi_sector, bvec.bv_len);
223 bio_end_io_acct(bio, start);
225 if (bio->bi_opf & REQ_FUA)
226 ret = nvdimm_flush(nd_region, bio);
229 bio->bi_status = errno_to_blk_status(ret);
234 static int pmem_rw_page(struct block_device *bdev, sector_t sector,
235 struct page *page, unsigned int op)
237 struct pmem_device *pmem = bdev->bd_disk->private_data;
241 rc = pmem_do_write(pmem, page, 0, sector, thp_size(page));
243 rc = pmem_do_read(pmem, page, 0, sector, thp_size(page));
245 * The ->rw_page interface is subtle and tricky. The core
246 * retries on any error, so we can only invoke page_endio() in
247 * the successful completion case. Otherwise, we'll see crashes
248 * caused by double completion.
251 page_endio(page, op_is_write(op), 0);
253 return blk_status_to_errno(rc);
256 /* see "strong" declaration in tools/testing/nvdimm/pmem-dax.c */
257 __weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff,
258 long nr_pages, void **kaddr, pfn_t *pfn)
260 resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset;
262 if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512,
263 PFN_PHYS(nr_pages))))
267 *kaddr = pmem->virt_addr + offset;
269 *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags);
272 * If badblocks are present, limit known good range to the
275 if (unlikely(pmem->bb.count))
277 return PHYS_PFN(pmem->size - pmem->pfn_pad - offset);
280 static const struct block_device_operations pmem_fops = {
281 .owner = THIS_MODULE,
282 .submit_bio = pmem_submit_bio,
283 .rw_page = pmem_rw_page,
286 static int pmem_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
289 struct pmem_device *pmem = dax_get_private(dax_dev);
291 return blk_status_to_errno(pmem_do_write(pmem, ZERO_PAGE(0), 0,
292 PFN_PHYS(pgoff) >> SECTOR_SHIFT,
296 static long pmem_dax_direct_access(struct dax_device *dax_dev,
297 pgoff_t pgoff, long nr_pages, void **kaddr, pfn_t *pfn)
299 struct pmem_device *pmem = dax_get_private(dax_dev);
301 return __pmem_direct_access(pmem, pgoff, nr_pages, kaddr, pfn);
304 static const struct dax_operations pmem_dax_ops = {
305 .direct_access = pmem_dax_direct_access,
306 .zero_page_range = pmem_dax_zero_page_range,
309 static ssize_t write_cache_show(struct device *dev,
310 struct device_attribute *attr, char *buf)
312 struct pmem_device *pmem = dev_to_disk(dev)->private_data;
314 return sprintf(buf, "%d\n", !!dax_write_cache_enabled(pmem->dax_dev));
317 static ssize_t write_cache_store(struct device *dev,
318 struct device_attribute *attr, const char *buf, size_t len)
320 struct pmem_device *pmem = dev_to_disk(dev)->private_data;
324 rc = strtobool(buf, &write_cache);
327 dax_write_cache(pmem->dax_dev, write_cache);
330 static DEVICE_ATTR_RW(write_cache);
332 static umode_t dax_visible(struct kobject *kobj, struct attribute *a, int n)
334 #ifndef CONFIG_ARCH_HAS_PMEM_API
335 if (a == &dev_attr_write_cache.attr)
341 static struct attribute *dax_attributes[] = {
342 &dev_attr_write_cache.attr,
346 static const struct attribute_group dax_attribute_group = {
348 .attrs = dax_attributes,
349 .is_visible = dax_visible,
352 static const struct attribute_group *pmem_attribute_groups[] = {
353 &dax_attribute_group,
357 static void pmem_release_disk(void *__pmem)
359 struct pmem_device *pmem = __pmem;
361 dax_remove_host(pmem->disk);
362 kill_dax(pmem->dax_dev);
363 put_dax(pmem->dax_dev);
364 del_gendisk(pmem->disk);
366 blk_cleanup_disk(pmem->disk);
369 static int pmem_attach_disk(struct device *dev,
370 struct nd_namespace_common *ndns)
372 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
373 struct nd_region *nd_region = to_nd_region(dev->parent);
374 int nid = dev_to_node(dev), fua;
375 struct resource *res = &nsio->res;
376 struct range bb_range;
377 struct nd_pfn *nd_pfn = NULL;
378 struct dax_device *dax_dev;
379 struct nd_pfn_sb *pfn_sb;
380 struct pmem_device *pmem;
381 struct request_queue *q;
382 struct gendisk *disk;
386 pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
390 rc = devm_namespace_enable(dev, ndns, nd_info_block_reserve());
394 /* while nsio_rw_bytes is active, parse a pfn info block if present */
395 if (is_nd_pfn(dev)) {
396 nd_pfn = to_nd_pfn(dev);
397 rc = nvdimm_setup_pfn(nd_pfn, &pmem->pgmap);
402 /* we're attaching a block device, disable raw namespace access */
403 devm_namespace_disable(dev, ndns);
405 dev_set_drvdata(dev, pmem);
406 pmem->phys_addr = res->start;
407 pmem->size = resource_size(res);
408 fua = nvdimm_has_flush(nd_region);
409 if (!IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) || fua < 0) {
410 dev_warn(dev, "unable to guarantee persistence of writes\n");
414 if (!devm_request_mem_region(dev, res->start, resource_size(res),
415 dev_name(&ndns->dev))) {
416 dev_warn(dev, "could not reserve region %pR\n", res);
420 disk = blk_alloc_disk(nid);
426 pmem->pgmap.owner = pmem;
427 pmem->pfn_flags = PFN_DEV;
428 if (is_nd_pfn(dev)) {
429 pmem->pgmap.type = MEMORY_DEVICE_FS_DAX;
430 addr = devm_memremap_pages(dev, &pmem->pgmap);
431 pfn_sb = nd_pfn->pfn_sb;
432 pmem->data_offset = le64_to_cpu(pfn_sb->dataoff);
433 pmem->pfn_pad = resource_size(res) -
434 range_len(&pmem->pgmap.range);
435 pmem->pfn_flags |= PFN_MAP;
436 bb_range = pmem->pgmap.range;
437 bb_range.start += pmem->data_offset;
438 } else if (pmem_should_map_pages(dev)) {
439 pmem->pgmap.range.start = res->start;
440 pmem->pgmap.range.end = res->end;
441 pmem->pgmap.nr_range = 1;
442 pmem->pgmap.type = MEMORY_DEVICE_FS_DAX;
443 addr = devm_memremap_pages(dev, &pmem->pgmap);
444 pmem->pfn_flags |= PFN_MAP;
445 bb_range = pmem->pgmap.range;
447 addr = devm_memremap(dev, pmem->phys_addr,
448 pmem->size, ARCH_MEMREMAP_PMEM);
449 bb_range.start = res->start;
450 bb_range.end = res->end;
457 pmem->virt_addr = addr;
459 blk_queue_write_cache(q, true, fua);
460 blk_queue_physical_block_size(q, PAGE_SIZE);
461 blk_queue_logical_block_size(q, pmem_sector_size(ndns));
462 blk_queue_max_hw_sectors(q, UINT_MAX);
463 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
464 if (pmem->pfn_flags & PFN_MAP)
465 blk_queue_flag_set(QUEUE_FLAG_DAX, q);
467 disk->fops = &pmem_fops;
468 disk->private_data = pmem;
469 nvdimm_namespace_disk_name(ndns, disk->disk_name);
470 set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset)
472 if (devm_init_badblocks(dev, &pmem->bb))
474 nvdimm_badblocks_populate(nd_region, &pmem->bb, &bb_range);
475 disk->bb = &pmem->bb;
477 dax_dev = alloc_dax(pmem, &pmem_dax_ops);
478 if (IS_ERR(dax_dev)) {
479 rc = PTR_ERR(dax_dev);
482 set_dax_nocache(dax_dev);
483 set_dax_nomc(dax_dev);
484 if (is_nvdimm_sync(nd_region))
485 set_dax_synchronous(dax_dev);
486 rc = dax_add_host(dax_dev, disk);
488 goto out_cleanup_dax;
489 dax_write_cache(dax_dev, nvdimm_has_cache(nd_region));
490 pmem->dax_dev = dax_dev;
492 rc = device_add_disk(dev, disk, pmem_attribute_groups);
494 goto out_remove_host;
495 if (devm_add_action_or_reset(dev, pmem_release_disk, pmem))
498 nvdimm_check_and_set_ro(disk);
500 pmem->bb_state = sysfs_get_dirent(disk_to_dev(disk)->kobj.sd,
503 dev_warn(dev, "'badblocks' notification disabled\n");
507 dax_remove_host(pmem->disk);
509 kill_dax(pmem->dax_dev);
510 put_dax(pmem->dax_dev);
512 blk_cleanup_disk(pmem->disk);
516 static int nd_pmem_probe(struct device *dev)
519 struct nd_namespace_common *ndns;
521 ndns = nvdimm_namespace_common_probe(dev);
523 return PTR_ERR(ndns);
526 return nvdimm_namespace_attach_btt(ndns);
529 return pmem_attach_disk(dev, ndns);
531 ret = devm_namespace_enable(dev, ndns, nd_info_block_reserve());
535 ret = nd_btt_probe(dev, ndns);
540 * We have two failure conditions here, there is no
541 * info reserver block or we found a valid info reserve block
542 * but failed to initialize the pfn superblock.
544 * For the first case consider namespace as a raw pmem namespace
547 * For the latter, consider this a success and advance the namespace
550 ret = nd_pfn_probe(dev, ndns);
553 else if (ret == -EOPNOTSUPP)
556 ret = nd_dax_probe(dev, ndns);
559 else if (ret == -EOPNOTSUPP)
562 /* probe complete, attach handles namespace enabling */
563 devm_namespace_disable(dev, ndns);
565 return pmem_attach_disk(dev, ndns);
568 static void nd_pmem_remove(struct device *dev)
570 struct pmem_device *pmem = dev_get_drvdata(dev);
573 nvdimm_namespace_detach_btt(to_nd_btt(dev));
576 * Note, this assumes nd_device_lock() context to not
577 * race nd_pmem_notify()
579 sysfs_put(pmem->bb_state);
580 pmem->bb_state = NULL;
582 nvdimm_flush(to_nd_region(dev->parent), NULL);
585 static void nd_pmem_shutdown(struct device *dev)
587 nvdimm_flush(to_nd_region(dev->parent), NULL);
590 static void pmem_revalidate_poison(struct device *dev)
592 struct nd_region *nd_region;
593 resource_size_t offset = 0, end_trunc = 0;
594 struct nd_namespace_common *ndns;
595 struct nd_namespace_io *nsio;
596 struct badblocks *bb;
598 struct kernfs_node *bb_state;
600 if (is_nd_btt(dev)) {
601 struct nd_btt *nd_btt = to_nd_btt(dev);
604 nd_region = to_nd_region(ndns->dev.parent);
605 nsio = to_nd_namespace_io(&ndns->dev);
609 struct pmem_device *pmem = dev_get_drvdata(dev);
611 nd_region = to_region(pmem);
613 bb_state = pmem->bb_state;
615 if (is_nd_pfn(dev)) {
616 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
617 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
620 offset = pmem->data_offset +
621 __le32_to_cpu(pfn_sb->start_pad);
622 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
627 nsio = to_nd_namespace_io(&ndns->dev);
630 range.start = nsio->res.start + offset;
631 range.end = nsio->res.end - end_trunc;
632 nvdimm_badblocks_populate(nd_region, bb, &range);
634 sysfs_notify_dirent(bb_state);
637 static void pmem_revalidate_region(struct device *dev)
639 struct pmem_device *pmem;
641 if (is_nd_btt(dev)) {
642 struct nd_btt *nd_btt = to_nd_btt(dev);
643 struct btt *btt = nd_btt->btt;
645 nvdimm_check_and_set_ro(btt->btt_disk);
649 pmem = dev_get_drvdata(dev);
650 nvdimm_check_and_set_ro(pmem->disk);
653 static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
656 case NVDIMM_REVALIDATE_POISON:
657 pmem_revalidate_poison(dev);
659 case NVDIMM_REVALIDATE_REGION:
660 pmem_revalidate_region(dev);
663 dev_WARN_ONCE(dev, 1, "notify: unknown event: %d\n", event);
668 MODULE_ALIAS("pmem");
669 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
670 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
671 static struct nd_device_driver nd_pmem_driver = {
672 .probe = nd_pmem_probe,
673 .remove = nd_pmem_remove,
674 .notify = nd_pmem_notify,
675 .shutdown = nd_pmem_shutdown,
679 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
682 module_nd_driver(nd_pmem_driver);
684 MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
685 MODULE_LICENSE("GPL v2");