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