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