2 * Persistent Memory Driver
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>.
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.
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
18 #include <asm/cacheflush.h>
19 #include <linux/blkdev.h>
20 #include <linux/hdreg.h>
21 #include <linux/init.h>
22 #include <linux/platform_device.h>
23 #include <linux/set_memory.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/badblocks.h>
27 #include <linux/memremap.h>
28 #include <linux/vmalloc.h>
29 #include <linux/blk-mq.h>
30 #include <linux/pfn_t.h>
31 #include <linux/slab.h>
32 #include <linux/uio.h>
33 #include <linux/dax.h>
35 #include <linux/backing-dev.h>
41 static struct device
*to_dev(struct pmem_device
*pmem
)
44 * nvdimm bus services need a 'dev' parameter, and we record the device
50 static struct nd_region
*to_region(struct pmem_device
*pmem
)
52 return to_nd_region(to_dev(pmem
)->parent
);
55 static void hwpoison_clear(struct pmem_device
*pmem
,
56 phys_addr_t phys
, unsigned int len
)
58 unsigned long pfn_start
, pfn_end
, pfn
;
60 /* only pmem in the linear map supports HWPoison */
61 if (is_vmalloc_addr(pmem
->virt_addr
))
64 pfn_start
= PHYS_PFN(phys
);
65 pfn_end
= pfn_start
+ PHYS_PFN(len
);
66 for (pfn
= pfn_start
; pfn
< pfn_end
; pfn
++) {
67 struct page
*page
= pfn_to_page(pfn
);
70 * Note, no need to hold a get_dev_pagemap() reference
71 * here since we're in the driver I/O path and
72 * outstanding I/O requests pin the dev_pagemap.
74 if (test_and_clear_pmem_poison(page
))
75 clear_mce_nospec(pfn
);
79 static blk_status_t
pmem_clear_poison(struct pmem_device
*pmem
,
80 phys_addr_t offset
, unsigned int len
)
82 struct device
*dev
= to_dev(pmem
);
85 blk_status_t rc
= BLK_STS_OK
;
87 sector
= (offset
- pmem
->data_offset
) / 512;
89 cleared
= nvdimm_clear_poison(dev
, pmem
->phys_addr
+ offset
, len
);
92 if (cleared
> 0 && cleared
/ 512) {
93 hwpoison_clear(pmem
, pmem
->phys_addr
+ offset
, cleared
);
95 dev_dbg(dev
, "%#llx clear %ld sector%s\n",
96 (unsigned long long) sector
, cleared
,
97 cleared
> 1 ? "s" : "");
98 badblocks_clear(&pmem
->bb
, sector
, cleared
);
100 sysfs_notify_dirent(pmem
->bb_state
);
103 arch_invalidate_pmem(pmem
->virt_addr
+ offset
, len
);
108 static void write_pmem(void *pmem_addr
, struct page
*page
,
109 unsigned int off
, unsigned int len
)
115 mem
= kmap_atomic(page
);
116 chunk
= min_t(unsigned int, len
, PAGE_SIZE
- off
);
117 memcpy_flushcache(pmem_addr
, mem
+ off
, chunk
);
126 static blk_status_t
read_pmem(struct page
*page
, unsigned int off
,
127 void *pmem_addr
, unsigned int len
)
134 mem
= kmap_atomic(page
);
135 chunk
= min_t(unsigned int, len
, PAGE_SIZE
- off
);
136 rem
= memcpy_mcsafe(mem
+ off
, pmem_addr
, chunk
);
139 return BLK_STS_IOERR
;
148 static blk_status_t
pmem_do_bvec(struct pmem_device
*pmem
, struct page
*page
,
149 unsigned int len
, unsigned int off
, unsigned int op
,
152 blk_status_t rc
= BLK_STS_OK
;
153 bool bad_pmem
= false;
154 phys_addr_t pmem_off
= sector
* 512 + pmem
->data_offset
;
155 void *pmem_addr
= pmem
->virt_addr
+ pmem_off
;
157 if (unlikely(is_bad_pmem(&pmem
->bb
, sector
, len
)))
160 if (!op_is_write(op
)) {
161 if (unlikely(bad_pmem
))
164 rc
= read_pmem(page
, off
, pmem_addr
, len
);
165 flush_dcache_page(page
);
169 * Note that we write the data both before and after
170 * clearing poison. The write before clear poison
171 * handles situations where the latest written data is
172 * preserved and the clear poison operation simply marks
173 * the address range as valid without changing the data.
174 * In this case application software can assume that an
175 * interrupted write will either return the new good
178 * However, if pmem_clear_poison() leaves the data in an
179 * indeterminate state we need to perform the write
180 * after clear poison.
182 flush_dcache_page(page
);
183 write_pmem(pmem_addr
, page
, off
, len
);
184 if (unlikely(bad_pmem
)) {
185 rc
= pmem_clear_poison(pmem
, pmem_off
, len
);
186 write_pmem(pmem_addr
, page
, off
, len
);
193 static blk_qc_t
pmem_make_request(struct request_queue
*q
, struct bio
*bio
)
199 struct bvec_iter iter
;
200 struct pmem_device
*pmem
= q
->queuedata
;
201 struct nd_region
*nd_region
= to_region(pmem
);
203 if (bio
->bi_opf
& REQ_PREFLUSH
)
204 nvdimm_flush(nd_region
);
206 do_acct
= nd_iostat_start(bio
, &start
);
207 bio_for_each_segment(bvec
, bio
, iter
) {
208 rc
= pmem_do_bvec(pmem
, bvec
.bv_page
, bvec
.bv_len
,
209 bvec
.bv_offset
, bio_op(bio
), iter
.bi_sector
);
216 nd_iostat_end(bio
, start
);
218 if (bio
->bi_opf
& REQ_FUA
)
219 nvdimm_flush(nd_region
);
222 return BLK_QC_T_NONE
;
225 static int pmem_rw_page(struct block_device
*bdev
, sector_t sector
,
226 struct page
*page
, unsigned int op
)
228 struct pmem_device
*pmem
= bdev
->bd_queue
->queuedata
;
231 rc
= pmem_do_bvec(pmem
, page
, hpage_nr_pages(page
) * PAGE_SIZE
,
235 * The ->rw_page interface is subtle and tricky. The core
236 * retries on any error, so we can only invoke page_endio() in
237 * the successful completion case. Otherwise, we'll see crashes
238 * caused by double completion.
241 page_endio(page
, op_is_write(op
), 0);
243 return blk_status_to_errno(rc
);
246 /* see "strong" declaration in tools/testing/nvdimm/pmem-dax.c */
247 __weak
long __pmem_direct_access(struct pmem_device
*pmem
, pgoff_t pgoff
,
248 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
250 resource_size_t offset
= PFN_PHYS(pgoff
) + pmem
->data_offset
;
252 if (unlikely(is_bad_pmem(&pmem
->bb
, PFN_PHYS(pgoff
) / 512,
253 PFN_PHYS(nr_pages
))))
257 *kaddr
= pmem
->virt_addr
+ offset
;
259 *pfn
= phys_to_pfn_t(pmem
->phys_addr
+ offset
, pmem
->pfn_flags
);
262 * If badblocks are present, limit known good range to the
265 if (unlikely(pmem
->bb
.count
))
267 return PHYS_PFN(pmem
->size
- pmem
->pfn_pad
- offset
);
270 static const struct block_device_operations pmem_fops
= {
271 .owner
= THIS_MODULE
,
272 .rw_page
= pmem_rw_page
,
273 .revalidate_disk
= nvdimm_revalidate_disk
,
276 static long pmem_dax_direct_access(struct dax_device
*dax_dev
,
277 pgoff_t pgoff
, long nr_pages
, void **kaddr
, pfn_t
*pfn
)
279 struct pmem_device
*pmem
= dax_get_private(dax_dev
);
281 return __pmem_direct_access(pmem
, pgoff
, nr_pages
, kaddr
, pfn
);
285 * Use the 'no check' versions of copy_from_iter_flushcache() and
286 * copy_to_iter_mcsafe() to bypass HARDENED_USERCOPY overhead. Bounds
287 * checking, both file offset and device offset, is handled by
290 static size_t pmem_copy_from_iter(struct dax_device
*dax_dev
, pgoff_t pgoff
,
291 void *addr
, size_t bytes
, struct iov_iter
*i
)
293 return _copy_from_iter_flushcache(addr
, bytes
, i
);
296 static size_t pmem_copy_to_iter(struct dax_device
*dax_dev
, pgoff_t pgoff
,
297 void *addr
, size_t bytes
, struct iov_iter
*i
)
299 return _copy_to_iter_mcsafe(addr
, bytes
, i
);
302 static const struct dax_operations pmem_dax_ops
= {
303 .direct_access
= pmem_dax_direct_access
,
304 .dax_supported
= generic_fsdax_supported
,
305 .copy_from_iter
= pmem_copy_from_iter
,
306 .copy_to_iter
= pmem_copy_to_iter
,
309 static const struct attribute_group
*pmem_attribute_groups
[] = {
310 &dax_attribute_group
,
314 static void pmem_release_queue(void *q
)
316 blk_cleanup_queue(q
);
319 static void pmem_freeze_queue(struct percpu_ref
*ref
)
321 struct request_queue
*q
;
323 q
= container_of(ref
, typeof(*q
), q_usage_counter
);
324 blk_freeze_queue_start(q
);
327 static void pmem_release_disk(void *__pmem
)
329 struct pmem_device
*pmem
= __pmem
;
331 kill_dax(pmem
->dax_dev
);
332 put_dax(pmem
->dax_dev
);
333 del_gendisk(pmem
->disk
);
334 put_disk(pmem
->disk
);
337 static void pmem_release_pgmap_ops(void *__pgmap
)
339 dev_pagemap_put_ops();
342 static void fsdax_pagefree(struct page
*page
, void *data
)
344 wake_up_var(&page
->_refcount
);
347 static int setup_pagemap_fsdax(struct device
*dev
, struct dev_pagemap
*pgmap
)
349 dev_pagemap_get_ops();
350 if (devm_add_action_or_reset(dev
, pmem_release_pgmap_ops
, pgmap
))
352 pgmap
->type
= MEMORY_DEVICE_FS_DAX
;
353 pgmap
->page_free
= fsdax_pagefree
;
358 static int pmem_attach_disk(struct device
*dev
,
359 struct nd_namespace_common
*ndns
)
361 struct nd_namespace_io
*nsio
= to_nd_namespace_io(&ndns
->dev
);
362 struct nd_region
*nd_region
= to_nd_region(dev
->parent
);
363 int nid
= dev_to_node(dev
), fua
;
364 struct resource
*res
= &nsio
->res
;
365 struct resource bb_res
;
366 struct nd_pfn
*nd_pfn
= NULL
;
367 struct dax_device
*dax_dev
;
368 struct nd_pfn_sb
*pfn_sb
;
369 struct pmem_device
*pmem
;
370 struct request_queue
*q
;
371 struct device
*gendev
;
372 struct gendisk
*disk
;
376 pmem
= devm_kzalloc(dev
, sizeof(*pmem
), GFP_KERNEL
);
380 /* while nsio_rw_bytes is active, parse a pfn info block if present */
381 if (is_nd_pfn(dev
)) {
382 nd_pfn
= to_nd_pfn(dev
);
383 rc
= nvdimm_setup_pfn(nd_pfn
, &pmem
->pgmap
);
388 /* we're attaching a block device, disable raw namespace access */
389 devm_nsio_disable(dev
, nsio
);
391 dev_set_drvdata(dev
, pmem
);
392 pmem
->phys_addr
= res
->start
;
393 pmem
->size
= resource_size(res
);
394 fua
= nvdimm_has_flush(nd_region
);
395 if (!IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
) || fua
< 0) {
396 dev_warn(dev
, "unable to guarantee persistence of writes\n");
400 if (!devm_request_mem_region(dev
, res
->start
, resource_size(res
),
401 dev_name(&ndns
->dev
))) {
402 dev_warn(dev
, "could not reserve region %pR\n", res
);
406 q
= blk_alloc_queue_node(GFP_KERNEL
, dev_to_node(dev
));
410 if (devm_add_action_or_reset(dev
, pmem_release_queue
, q
))
413 pmem
->pfn_flags
= PFN_DEV
;
414 pmem
->pgmap
.ref
= &q
->q_usage_counter
;
415 pmem
->pgmap
.kill
= pmem_freeze_queue
;
416 if (is_nd_pfn(dev
)) {
417 if (setup_pagemap_fsdax(dev
, &pmem
->pgmap
))
419 addr
= devm_memremap_pages(dev
, &pmem
->pgmap
);
420 pfn_sb
= nd_pfn
->pfn_sb
;
421 pmem
->data_offset
= le64_to_cpu(pfn_sb
->dataoff
);
422 pmem
->pfn_pad
= resource_size(res
) -
423 resource_size(&pmem
->pgmap
.res
);
424 pmem
->pfn_flags
|= PFN_MAP
;
425 memcpy(&bb_res
, &pmem
->pgmap
.res
, sizeof(bb_res
));
426 bb_res
.start
+= pmem
->data_offset
;
427 } else if (pmem_should_map_pages(dev
)) {
428 memcpy(&pmem
->pgmap
.res
, &nsio
->res
, sizeof(pmem
->pgmap
.res
));
429 pmem
->pgmap
.altmap_valid
= false;
430 if (setup_pagemap_fsdax(dev
, &pmem
->pgmap
))
432 addr
= devm_memremap_pages(dev
, &pmem
->pgmap
);
433 pmem
->pfn_flags
|= PFN_MAP
;
434 memcpy(&bb_res
, &pmem
->pgmap
.res
, sizeof(bb_res
));
436 addr
= devm_memremap(dev
, pmem
->phys_addr
,
437 pmem
->size
, ARCH_MEMREMAP_PMEM
);
438 memcpy(&bb_res
, &nsio
->res
, sizeof(bb_res
));
442 return PTR_ERR(addr
);
443 pmem
->virt_addr
= addr
;
445 blk_queue_write_cache(q
, true, fua
);
446 blk_queue_make_request(q
, pmem_make_request
);
447 blk_queue_physical_block_size(q
, PAGE_SIZE
);
448 blk_queue_logical_block_size(q
, pmem_sector_size(ndns
));
449 blk_queue_max_hw_sectors(q
, UINT_MAX
);
450 blk_queue_flag_set(QUEUE_FLAG_NONROT
, q
);
451 if (pmem
->pfn_flags
& PFN_MAP
)
452 blk_queue_flag_set(QUEUE_FLAG_DAX
, q
);
455 disk
= alloc_disk_node(0, nid
);
460 disk
->fops
= &pmem_fops
;
462 disk
->flags
= GENHD_FL_EXT_DEVT
;
463 disk
->queue
->backing_dev_info
->capabilities
|= BDI_CAP_SYNCHRONOUS_IO
;
464 nvdimm_namespace_disk_name(ndns
, disk
->disk_name
);
465 set_capacity(disk
, (pmem
->size
- pmem
->pfn_pad
- pmem
->data_offset
)
467 if (devm_init_badblocks(dev
, &pmem
->bb
))
469 nvdimm_badblocks_populate(nd_region
, &pmem
->bb
, &bb_res
);
470 disk
->bb
= &pmem
->bb
;
472 dax_dev
= alloc_dax(pmem
, disk
->disk_name
, &pmem_dax_ops
);
477 dax_write_cache(dax_dev
, nvdimm_has_cache(nd_region
));
478 pmem
->dax_dev
= dax_dev
;
480 gendev
= disk_to_dev(disk
);
481 gendev
->groups
= pmem_attribute_groups
;
483 device_add_disk(dev
, disk
, NULL
);
484 if (devm_add_action_or_reset(dev
, pmem_release_disk
, pmem
))
487 revalidate_disk(disk
);
489 pmem
->bb_state
= sysfs_get_dirent(disk_to_dev(disk
)->kobj
.sd
,
492 dev_warn(dev
, "'badblocks' notification disabled\n");
497 static int nd_pmem_probe(struct device
*dev
)
499 struct nd_namespace_common
*ndns
;
501 ndns
= nvdimm_namespace_common_probe(dev
);
503 return PTR_ERR(ndns
);
505 if (devm_nsio_enable(dev
, to_nd_namespace_io(&ndns
->dev
)))
509 return nvdimm_namespace_attach_btt(ndns
);
512 return pmem_attach_disk(dev
, ndns
);
514 /* if we find a valid info-block we'll come back as that personality */
515 if (nd_btt_probe(dev
, ndns
) == 0 || nd_pfn_probe(dev
, ndns
) == 0
516 || nd_dax_probe(dev
, ndns
) == 0)
519 /* ...otherwise we're just a raw pmem device */
520 return pmem_attach_disk(dev
, ndns
);
523 static int nd_pmem_remove(struct device
*dev
)
525 struct pmem_device
*pmem
= dev_get_drvdata(dev
);
528 nvdimm_namespace_detach_btt(to_nd_btt(dev
));
531 * Note, this assumes device_lock() context to not race
534 sysfs_put(pmem
->bb_state
);
535 pmem
->bb_state
= NULL
;
537 nvdimm_flush(to_nd_region(dev
->parent
));
542 static void nd_pmem_shutdown(struct device
*dev
)
544 nvdimm_flush(to_nd_region(dev
->parent
));
547 static void nd_pmem_notify(struct device
*dev
, enum nvdimm_event event
)
549 struct nd_region
*nd_region
;
550 resource_size_t offset
= 0, end_trunc
= 0;
551 struct nd_namespace_common
*ndns
;
552 struct nd_namespace_io
*nsio
;
554 struct badblocks
*bb
;
555 struct kernfs_node
*bb_state
;
557 if (event
!= NVDIMM_REVALIDATE_POISON
)
560 if (is_nd_btt(dev
)) {
561 struct nd_btt
*nd_btt
= to_nd_btt(dev
);
564 nd_region
= to_nd_region(ndns
->dev
.parent
);
565 nsio
= to_nd_namespace_io(&ndns
->dev
);
569 struct pmem_device
*pmem
= dev_get_drvdata(dev
);
571 nd_region
= to_region(pmem
);
573 bb_state
= pmem
->bb_state
;
575 if (is_nd_pfn(dev
)) {
576 struct nd_pfn
*nd_pfn
= to_nd_pfn(dev
);
577 struct nd_pfn_sb
*pfn_sb
= nd_pfn
->pfn_sb
;
580 offset
= pmem
->data_offset
+
581 __le32_to_cpu(pfn_sb
->start_pad
);
582 end_trunc
= __le32_to_cpu(pfn_sb
->end_trunc
);
587 nsio
= to_nd_namespace_io(&ndns
->dev
);
590 res
.start
= nsio
->res
.start
+ offset
;
591 res
.end
= nsio
->res
.end
- end_trunc
;
592 nvdimm_badblocks_populate(nd_region
, bb
, &res
);
594 sysfs_notify_dirent(bb_state
);
597 MODULE_ALIAS("pmem");
598 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO
);
599 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM
);
600 static struct nd_device_driver nd_pmem_driver
= {
601 .probe
= nd_pmem_probe
,
602 .remove
= nd_pmem_remove
,
603 .notify
= nd_pmem_notify
,
604 .shutdown
= nd_pmem_shutdown
,
608 .type
= ND_DRIVER_NAMESPACE_IO
| ND_DRIVER_NAMESPACE_PMEM
,
611 module_nd_driver(nd_pmem_driver
);
613 MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
614 MODULE_LICENSE("GPL v2");