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 <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> 26 #include <linux/nd.h> 27 #include <linux/mm.h> 28 #include <asm/cacheflush.h> 29 #include "pmem.h" 30 #include "btt.h" 31 #include "pfn.h" 32 #include "nd.h" 33 34 static struct device *to_dev(struct pmem_device *pmem) 35 { 36 /* 37 * nvdimm bus services need a 'dev' parameter, and we record the device 38 * at init in bb.dev. 39 */ 40 return pmem->bb.dev; 41 } 42 43 static struct nd_region *to_region(struct pmem_device *pmem) 44 { 45 return to_nd_region(to_dev(pmem)->parent); 46 } 47 48 static void hwpoison_clear(struct pmem_device *pmem, 49 phys_addr_t phys, unsigned int len) 50 { 51 unsigned long pfn_start, pfn_end, pfn; 52 53 /* only pmem in the linear map supports HWPoison */ 54 if (is_vmalloc_addr(pmem->virt_addr)) 55 return; 56 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); 61 62 /* 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. 66 */ 67 if (test_and_clear_pmem_poison(page)) 68 clear_mce_nospec(pfn); 69 } 70 } 71 72 static blk_status_t pmem_clear_poison(struct pmem_device *pmem, 73 phys_addr_t offset, unsigned int len) 74 { 75 struct device *dev = to_dev(pmem); 76 sector_t sector; 77 long cleared; 78 blk_status_t rc = BLK_STS_OK; 79 80 sector = (offset - pmem->data_offset) / 512; 81 82 cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len); 83 if (cleared < len) 84 rc = BLK_STS_IOERR; 85 if (cleared > 0 && cleared / 512) { 86 hwpoison_clear(pmem, pmem->phys_addr + offset, cleared); 87 cleared /= 512; 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); 92 if (pmem->bb_state) 93 sysfs_notify_dirent(pmem->bb_state); 94 } 95 96 arch_invalidate_pmem(pmem->virt_addr + offset, len); 97 98 return rc; 99 } 100 101 static void write_pmem(void *pmem_addr, struct page *page, 102 unsigned int off, unsigned int len) 103 { 104 unsigned int chunk; 105 void *mem; 106 107 while (len) { 108 mem = kmap_atomic(page); 109 chunk = min_t(unsigned int, len, PAGE_SIZE - off); 110 memcpy_flushcache(pmem_addr, mem + off, chunk); 111 kunmap_atomic(mem); 112 len -= chunk; 113 off = 0; 114 page++; 115 pmem_addr += chunk; 116 } 117 } 118 119 static blk_status_t read_pmem(struct page *page, unsigned int off, 120 void *pmem_addr, unsigned int len) 121 { 122 unsigned int chunk; 123 unsigned long rem; 124 void *mem; 125 126 while (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); 130 kunmap_atomic(mem); 131 if (rem) 132 return BLK_STS_IOERR; 133 len -= chunk; 134 off = 0; 135 page++; 136 pmem_addr += chunk; 137 } 138 return BLK_STS_OK; 139 } 140 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) 144 { 145 blk_status_t rc; 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 return BLK_STS_IOERR; 151 152 rc = read_pmem(page, page_off, pmem_addr, len); 153 flush_dcache_page(page); 154 return rc; 155 } 156 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) 160 { 161 phys_addr_t pmem_off = sector * 512 + pmem->data_offset; 162 void *pmem_addr = pmem->virt_addr + pmem_off; 163 164 if (unlikely(is_bad_pmem(&pmem->bb, sector, len))) { 165 blk_status_t rc = pmem_clear_poison(pmem, pmem_off, len); 166 167 if (rc != BLK_STS_OK) 168 return rc; 169 } 170 171 flush_dcache_page(page); 172 write_pmem(pmem_addr, page, page_off, len); 173 174 return BLK_STS_OK; 175 } 176 177 static void pmem_submit_bio(struct bio *bio) 178 { 179 int ret = 0; 180 blk_status_t rc = 0; 181 bool do_acct; 182 unsigned long start; 183 struct bio_vec bvec; 184 struct bvec_iter iter; 185 struct pmem_device *pmem = bio->bi_bdev->bd_disk->private_data; 186 struct nd_region *nd_region = to_region(pmem); 187 188 if (bio->bi_opf & REQ_PREFLUSH) 189 ret = nvdimm_flush(nd_region, bio); 190 191 do_acct = blk_queue_io_stat(bio->bi_bdev->bd_disk->queue); 192 if (do_acct) 193 start = bio_start_io_acct(bio); 194 bio_for_each_segment(bvec, bio, iter) { 195 if (op_is_write(bio_op(bio))) 196 rc = pmem_do_write(pmem, bvec.bv_page, bvec.bv_offset, 197 iter.bi_sector, bvec.bv_len); 198 else 199 rc = pmem_do_read(pmem, bvec.bv_page, bvec.bv_offset, 200 iter.bi_sector, bvec.bv_len); 201 if (rc) { 202 bio->bi_status = rc; 203 break; 204 } 205 } 206 if (do_acct) 207 bio_end_io_acct(bio, start); 208 209 if (bio->bi_opf & REQ_FUA) 210 ret = nvdimm_flush(nd_region, bio); 211 212 if (ret) 213 bio->bi_status = errno_to_blk_status(ret); 214 215 bio_endio(bio); 216 } 217 218 static int pmem_rw_page(struct block_device *bdev, sector_t sector, 219 struct page *page, unsigned int op) 220 { 221 struct pmem_device *pmem = bdev->bd_disk->private_data; 222 blk_status_t rc; 223 224 if (op_is_write(op)) 225 rc = pmem_do_write(pmem, page, 0, sector, thp_size(page)); 226 else 227 rc = pmem_do_read(pmem, page, 0, sector, thp_size(page)); 228 /* 229 * The ->rw_page interface is subtle and tricky. The core 230 * retries on any error, so we can only invoke page_endio() in 231 * the successful completion case. Otherwise, we'll see crashes 232 * caused by double completion. 233 */ 234 if (rc == 0) 235 page_endio(page, op_is_write(op), 0); 236 237 return blk_status_to_errno(rc); 238 } 239 240 /* see "strong" declaration in tools/testing/nvdimm/pmem-dax.c */ 241 __weak long __pmem_direct_access(struct pmem_device *pmem, pgoff_t pgoff, 242 long nr_pages, enum dax_access_mode mode, void **kaddr, 243 pfn_t *pfn) 244 { 245 resource_size_t offset = PFN_PHYS(pgoff) + pmem->data_offset; 246 247 if (unlikely(is_bad_pmem(&pmem->bb, PFN_PHYS(pgoff) / 512, 248 PFN_PHYS(nr_pages)))) 249 return -EIO; 250 251 if (kaddr) 252 *kaddr = pmem->virt_addr + offset; 253 if (pfn) 254 *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags); 255 256 /* 257 * If badblocks are present, limit known good range to the 258 * requested range. 259 */ 260 if (unlikely(pmem->bb.count)) 261 return nr_pages; 262 return PHYS_PFN(pmem->size - pmem->pfn_pad - offset); 263 } 264 265 static const struct block_device_operations pmem_fops = { 266 .owner = THIS_MODULE, 267 .submit_bio = pmem_submit_bio, 268 .rw_page = pmem_rw_page, 269 }; 270 271 static int pmem_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, 272 size_t nr_pages) 273 { 274 struct pmem_device *pmem = dax_get_private(dax_dev); 275 276 return blk_status_to_errno(pmem_do_write(pmem, ZERO_PAGE(0), 0, 277 PFN_PHYS(pgoff) >> SECTOR_SHIFT, 278 PAGE_SIZE)); 279 } 280 281 static long pmem_dax_direct_access(struct dax_device *dax_dev, 282 pgoff_t pgoff, long nr_pages, enum dax_access_mode mode, 283 void **kaddr, pfn_t *pfn) 284 { 285 struct pmem_device *pmem = dax_get_private(dax_dev); 286 287 return __pmem_direct_access(pmem, pgoff, nr_pages, mode, kaddr, pfn); 288 } 289 290 static const struct dax_operations pmem_dax_ops = { 291 .direct_access = pmem_dax_direct_access, 292 .zero_page_range = pmem_dax_zero_page_range, 293 }; 294 295 static ssize_t write_cache_show(struct device *dev, 296 struct device_attribute *attr, char *buf) 297 { 298 struct pmem_device *pmem = dev_to_disk(dev)->private_data; 299 300 return sprintf(buf, "%d\n", !!dax_write_cache_enabled(pmem->dax_dev)); 301 } 302 303 static ssize_t write_cache_store(struct device *dev, 304 struct device_attribute *attr, const char *buf, size_t len) 305 { 306 struct pmem_device *pmem = dev_to_disk(dev)->private_data; 307 bool write_cache; 308 int rc; 309 310 rc = strtobool(buf, &write_cache); 311 if (rc) 312 return rc; 313 dax_write_cache(pmem->dax_dev, write_cache); 314 return len; 315 } 316 static DEVICE_ATTR_RW(write_cache); 317 318 static umode_t dax_visible(struct kobject *kobj, struct attribute *a, int n) 319 { 320 #ifndef CONFIG_ARCH_HAS_PMEM_API 321 if (a == &dev_attr_write_cache.attr) 322 return 0; 323 #endif 324 return a->mode; 325 } 326 327 static struct attribute *dax_attributes[] = { 328 &dev_attr_write_cache.attr, 329 NULL, 330 }; 331 332 static const struct attribute_group dax_attribute_group = { 333 .name = "dax", 334 .attrs = dax_attributes, 335 .is_visible = dax_visible, 336 }; 337 338 static const struct attribute_group *pmem_attribute_groups[] = { 339 &dax_attribute_group, 340 NULL, 341 }; 342 343 static void pmem_release_disk(void *__pmem) 344 { 345 struct pmem_device *pmem = __pmem; 346 347 dax_remove_host(pmem->disk); 348 kill_dax(pmem->dax_dev); 349 put_dax(pmem->dax_dev); 350 del_gendisk(pmem->disk); 351 352 blk_cleanup_disk(pmem->disk); 353 } 354 355 static int pmem_attach_disk(struct device *dev, 356 struct nd_namespace_common *ndns) 357 { 358 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev); 359 struct nd_region *nd_region = to_nd_region(dev->parent); 360 int nid = dev_to_node(dev), fua; 361 struct resource *res = &nsio->res; 362 struct range bb_range; 363 struct nd_pfn *nd_pfn = NULL; 364 struct dax_device *dax_dev; 365 struct nd_pfn_sb *pfn_sb; 366 struct pmem_device *pmem; 367 struct request_queue *q; 368 struct gendisk *disk; 369 void *addr; 370 int rc; 371 372 pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL); 373 if (!pmem) 374 return -ENOMEM; 375 376 rc = devm_namespace_enable(dev, ndns, nd_info_block_reserve()); 377 if (rc) 378 return rc; 379 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); 384 if (rc) 385 return rc; 386 } 387 388 /* we're attaching a block device, disable raw namespace access */ 389 devm_namespace_disable(dev, ndns); 390 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"); 397 fua = 0; 398 } 399 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); 403 return -EBUSY; 404 } 405 406 disk = blk_alloc_disk(nid); 407 if (!disk) 408 return -ENOMEM; 409 q = disk->queue; 410 411 pmem->disk = disk; 412 pmem->pgmap.owner = pmem; 413 pmem->pfn_flags = PFN_DEV; 414 if (is_nd_pfn(dev)) { 415 pmem->pgmap.type = MEMORY_DEVICE_FS_DAX; 416 addr = devm_memremap_pages(dev, &pmem->pgmap); 417 pfn_sb = nd_pfn->pfn_sb; 418 pmem->data_offset = le64_to_cpu(pfn_sb->dataoff); 419 pmem->pfn_pad = resource_size(res) - 420 range_len(&pmem->pgmap.range); 421 pmem->pfn_flags |= PFN_MAP; 422 bb_range = pmem->pgmap.range; 423 bb_range.start += pmem->data_offset; 424 } else if (pmem_should_map_pages(dev)) { 425 pmem->pgmap.range.start = res->start; 426 pmem->pgmap.range.end = res->end; 427 pmem->pgmap.nr_range = 1; 428 pmem->pgmap.type = MEMORY_DEVICE_FS_DAX; 429 addr = devm_memremap_pages(dev, &pmem->pgmap); 430 pmem->pfn_flags |= PFN_MAP; 431 bb_range = pmem->pgmap.range; 432 } else { 433 addr = devm_memremap(dev, pmem->phys_addr, 434 pmem->size, ARCH_MEMREMAP_PMEM); 435 bb_range.start = res->start; 436 bb_range.end = res->end; 437 } 438 439 if (IS_ERR(addr)) { 440 rc = PTR_ERR(addr); 441 goto out; 442 } 443 pmem->virt_addr = addr; 444 445 blk_queue_write_cache(q, true, fua); 446 blk_queue_physical_block_size(q, PAGE_SIZE); 447 blk_queue_logical_block_size(q, pmem_sector_size(ndns)); 448 blk_queue_max_hw_sectors(q, UINT_MAX); 449 blk_queue_flag_set(QUEUE_FLAG_NONROT, q); 450 if (pmem->pfn_flags & PFN_MAP) 451 blk_queue_flag_set(QUEUE_FLAG_DAX, q); 452 453 disk->fops = &pmem_fops; 454 disk->private_data = pmem; 455 nvdimm_namespace_disk_name(ndns, disk->disk_name); 456 set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset) 457 / 512); 458 if (devm_init_badblocks(dev, &pmem->bb)) 459 return -ENOMEM; 460 nvdimm_badblocks_populate(nd_region, &pmem->bb, &bb_range); 461 disk->bb = &pmem->bb; 462 463 dax_dev = alloc_dax(pmem, &pmem_dax_ops); 464 if (IS_ERR(dax_dev)) { 465 rc = PTR_ERR(dax_dev); 466 goto out; 467 } 468 set_dax_nocache(dax_dev); 469 set_dax_nomc(dax_dev); 470 if (is_nvdimm_sync(nd_region)) 471 set_dax_synchronous(dax_dev); 472 rc = dax_add_host(dax_dev, disk); 473 if (rc) 474 goto out_cleanup_dax; 475 dax_write_cache(dax_dev, nvdimm_has_cache(nd_region)); 476 pmem->dax_dev = dax_dev; 477 478 rc = device_add_disk(dev, disk, pmem_attribute_groups); 479 if (rc) 480 goto out_remove_host; 481 if (devm_add_action_or_reset(dev, pmem_release_disk, pmem)) 482 return -ENOMEM; 483 484 nvdimm_check_and_set_ro(disk); 485 486 pmem->bb_state = sysfs_get_dirent(disk_to_dev(disk)->kobj.sd, 487 "badblocks"); 488 if (!pmem->bb_state) 489 dev_warn(dev, "'badblocks' notification disabled\n"); 490 return 0; 491 492 out_remove_host: 493 dax_remove_host(pmem->disk); 494 out_cleanup_dax: 495 kill_dax(pmem->dax_dev); 496 put_dax(pmem->dax_dev); 497 out: 498 blk_cleanup_disk(pmem->disk); 499 return rc; 500 } 501 502 static int nd_pmem_probe(struct device *dev) 503 { 504 int ret; 505 struct nd_namespace_common *ndns; 506 507 ndns = nvdimm_namespace_common_probe(dev); 508 if (IS_ERR(ndns)) 509 return PTR_ERR(ndns); 510 511 if (is_nd_btt(dev)) 512 return nvdimm_namespace_attach_btt(ndns); 513 514 if (is_nd_pfn(dev)) 515 return pmem_attach_disk(dev, ndns); 516 517 ret = devm_namespace_enable(dev, ndns, nd_info_block_reserve()); 518 if (ret) 519 return ret; 520 521 ret = nd_btt_probe(dev, ndns); 522 if (ret == 0) 523 return -ENXIO; 524 525 /* 526 * We have two failure conditions here, there is no 527 * info reserver block or we found a valid info reserve block 528 * but failed to initialize the pfn superblock. 529 * 530 * For the first case consider namespace as a raw pmem namespace 531 * and attach a disk. 532 * 533 * For the latter, consider this a success and advance the namespace 534 * seed. 535 */ 536 ret = nd_pfn_probe(dev, ndns); 537 if (ret == 0) 538 return -ENXIO; 539 else if (ret == -EOPNOTSUPP) 540 return ret; 541 542 ret = nd_dax_probe(dev, ndns); 543 if (ret == 0) 544 return -ENXIO; 545 else if (ret == -EOPNOTSUPP) 546 return ret; 547 548 /* probe complete, attach handles namespace enabling */ 549 devm_namespace_disable(dev, ndns); 550 551 return pmem_attach_disk(dev, ndns); 552 } 553 554 static void nd_pmem_remove(struct device *dev) 555 { 556 struct pmem_device *pmem = dev_get_drvdata(dev); 557 558 if (is_nd_btt(dev)) 559 nvdimm_namespace_detach_btt(to_nd_btt(dev)); 560 else { 561 /* 562 * Note, this assumes nd_device_lock() context to not 563 * race nd_pmem_notify() 564 */ 565 sysfs_put(pmem->bb_state); 566 pmem->bb_state = NULL; 567 } 568 nvdimm_flush(to_nd_region(dev->parent), NULL); 569 } 570 571 static void nd_pmem_shutdown(struct device *dev) 572 { 573 nvdimm_flush(to_nd_region(dev->parent), NULL); 574 } 575 576 static void pmem_revalidate_poison(struct device *dev) 577 { 578 struct nd_region *nd_region; 579 resource_size_t offset = 0, end_trunc = 0; 580 struct nd_namespace_common *ndns; 581 struct nd_namespace_io *nsio; 582 struct badblocks *bb; 583 struct range range; 584 struct kernfs_node *bb_state; 585 586 if (is_nd_btt(dev)) { 587 struct nd_btt *nd_btt = to_nd_btt(dev); 588 589 ndns = nd_btt->ndns; 590 nd_region = to_nd_region(ndns->dev.parent); 591 nsio = to_nd_namespace_io(&ndns->dev); 592 bb = &nsio->bb; 593 bb_state = NULL; 594 } else { 595 struct pmem_device *pmem = dev_get_drvdata(dev); 596 597 nd_region = to_region(pmem); 598 bb = &pmem->bb; 599 bb_state = pmem->bb_state; 600 601 if (is_nd_pfn(dev)) { 602 struct nd_pfn *nd_pfn = to_nd_pfn(dev); 603 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb; 604 605 ndns = nd_pfn->ndns; 606 offset = pmem->data_offset + 607 __le32_to_cpu(pfn_sb->start_pad); 608 end_trunc = __le32_to_cpu(pfn_sb->end_trunc); 609 } else { 610 ndns = to_ndns(dev); 611 } 612 613 nsio = to_nd_namespace_io(&ndns->dev); 614 } 615 616 range.start = nsio->res.start + offset; 617 range.end = nsio->res.end - end_trunc; 618 nvdimm_badblocks_populate(nd_region, bb, &range); 619 if (bb_state) 620 sysfs_notify_dirent(bb_state); 621 } 622 623 static void pmem_revalidate_region(struct device *dev) 624 { 625 struct pmem_device *pmem; 626 627 if (is_nd_btt(dev)) { 628 struct nd_btt *nd_btt = to_nd_btt(dev); 629 struct btt *btt = nd_btt->btt; 630 631 nvdimm_check_and_set_ro(btt->btt_disk); 632 return; 633 } 634 635 pmem = dev_get_drvdata(dev); 636 nvdimm_check_and_set_ro(pmem->disk); 637 } 638 639 static void nd_pmem_notify(struct device *dev, enum nvdimm_event event) 640 { 641 switch (event) { 642 case NVDIMM_REVALIDATE_POISON: 643 pmem_revalidate_poison(dev); 644 break; 645 case NVDIMM_REVALIDATE_REGION: 646 pmem_revalidate_region(dev); 647 break; 648 default: 649 dev_WARN_ONCE(dev, 1, "notify: unknown event: %d\n", event); 650 break; 651 } 652 } 653 654 MODULE_ALIAS("pmem"); 655 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO); 656 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM); 657 static struct nd_device_driver nd_pmem_driver = { 658 .probe = nd_pmem_probe, 659 .remove = nd_pmem_remove, 660 .notify = nd_pmem_notify, 661 .shutdown = nd_pmem_shutdown, 662 .drv = { 663 .name = "nd_pmem", 664 }, 665 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM, 666 }; 667 668 module_nd_driver(nd_pmem_driver); 669 670 MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>"); 671 MODULE_LICENSE("GPL v2"); 672