1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2017-2018 Christoph Hellwig. 4 */ 5 6 #include <linux/backing-dev.h> 7 #include <linux/moduleparam.h> 8 #include <linux/vmalloc.h> 9 #include <trace/events/block.h> 10 #include "nvme.h" 11 12 bool multipath = true; 13 module_param(multipath, bool, 0444); 14 MODULE_PARM_DESC(multipath, 15 "turn on native support for multiple controllers per subsystem"); 16 17 static const char *nvme_iopolicy_names[] = { 18 [NVME_IOPOLICY_NUMA] = "numa", 19 [NVME_IOPOLICY_RR] = "round-robin", 20 [NVME_IOPOLICY_QD] = "queue-depth", 21 }; 22 23 static int iopolicy = NVME_IOPOLICY_NUMA; 24 25 static int nvme_set_iopolicy(const char *val, const struct kernel_param *kp) 26 { 27 if (!val) 28 return -EINVAL; 29 if (!strncmp(val, "numa", 4)) 30 iopolicy = NVME_IOPOLICY_NUMA; 31 else if (!strncmp(val, "round-robin", 11)) 32 iopolicy = NVME_IOPOLICY_RR; 33 else if (!strncmp(val, "queue-depth", 11)) 34 iopolicy = NVME_IOPOLICY_QD; 35 else 36 return -EINVAL; 37 38 return 0; 39 } 40 41 static int nvme_get_iopolicy(char *buf, const struct kernel_param *kp) 42 { 43 return sprintf(buf, "%s\n", nvme_iopolicy_names[iopolicy]); 44 } 45 46 module_param_call(iopolicy, nvme_set_iopolicy, nvme_get_iopolicy, 47 &iopolicy, 0644); 48 MODULE_PARM_DESC(iopolicy, 49 "Default multipath I/O policy; 'numa' (default), 'round-robin' or 'queue-depth'"); 50 51 void nvme_mpath_default_iopolicy(struct nvme_subsystem *subsys) 52 { 53 subsys->iopolicy = iopolicy; 54 } 55 56 void nvme_mpath_unfreeze(struct nvme_subsystem *subsys) 57 { 58 struct nvme_ns_head *h; 59 60 lockdep_assert_held(&subsys->lock); 61 list_for_each_entry(h, &subsys->nsheads, entry) 62 if (h->disk) 63 blk_mq_unfreeze_queue_nomemrestore(h->disk->queue); 64 } 65 66 void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys) 67 { 68 struct nvme_ns_head *h; 69 70 lockdep_assert_held(&subsys->lock); 71 list_for_each_entry(h, &subsys->nsheads, entry) 72 if (h->disk) 73 blk_mq_freeze_queue_wait(h->disk->queue); 74 } 75 76 void nvme_mpath_start_freeze(struct nvme_subsystem *subsys) 77 { 78 struct nvme_ns_head *h; 79 80 lockdep_assert_held(&subsys->lock); 81 list_for_each_entry(h, &subsys->nsheads, entry) 82 if (h->disk) 83 blk_freeze_queue_start(h->disk->queue); 84 } 85 86 void nvme_failover_req(struct request *req) 87 { 88 struct nvme_ns *ns = req->q->queuedata; 89 u16 status = nvme_req(req)->status & NVME_SCT_SC_MASK; 90 unsigned long flags; 91 struct bio *bio; 92 93 nvme_mpath_clear_current_path(ns); 94 95 /* 96 * If we got back an ANA error, we know the controller is alive but not 97 * ready to serve this namespace. Kick of a re-read of the ANA 98 * information page, and just try any other available path for now. 99 */ 100 if (nvme_is_ana_error(status) && ns->ctrl->ana_log_buf) { 101 set_bit(NVME_NS_ANA_PENDING, &ns->flags); 102 queue_work(nvme_wq, &ns->ctrl->ana_work); 103 } 104 105 spin_lock_irqsave(&ns->head->requeue_lock, flags); 106 for (bio = req->bio; bio; bio = bio->bi_next) { 107 bio_set_dev(bio, ns->head->disk->part0); 108 if (bio->bi_opf & REQ_POLLED) { 109 bio->bi_opf &= ~REQ_POLLED; 110 bio->bi_cookie = BLK_QC_T_NONE; 111 } 112 /* 113 * The alternate request queue that we may end up submitting 114 * the bio to may be frozen temporarily, in this case REQ_NOWAIT 115 * will fail the I/O immediately with EAGAIN to the issuer. 116 * We are not in the issuer context which cannot block. Clear 117 * the flag to avoid spurious EAGAIN I/O failures. 118 */ 119 bio->bi_opf &= ~REQ_NOWAIT; 120 } 121 blk_steal_bios(&ns->head->requeue_list, req); 122 spin_unlock_irqrestore(&ns->head->requeue_lock, flags); 123 124 nvme_req(req)->status = 0; 125 nvme_end_req(req); 126 kblockd_schedule_work(&ns->head->requeue_work); 127 } 128 129 void nvme_mpath_start_request(struct request *rq) 130 { 131 struct nvme_ns *ns = rq->q->queuedata; 132 struct gendisk *disk = ns->head->disk; 133 134 if (READ_ONCE(ns->head->subsys->iopolicy) == NVME_IOPOLICY_QD) { 135 atomic_inc(&ns->ctrl->nr_active); 136 nvme_req(rq)->flags |= NVME_MPATH_CNT_ACTIVE; 137 } 138 139 if (!blk_queue_io_stat(disk->queue) || blk_rq_is_passthrough(rq)) 140 return; 141 142 nvme_req(rq)->flags |= NVME_MPATH_IO_STATS; 143 nvme_req(rq)->start_time = bdev_start_io_acct(disk->part0, req_op(rq), 144 jiffies); 145 } 146 EXPORT_SYMBOL_GPL(nvme_mpath_start_request); 147 148 void nvme_mpath_end_request(struct request *rq) 149 { 150 struct nvme_ns *ns = rq->q->queuedata; 151 152 if (nvme_req(rq)->flags & NVME_MPATH_CNT_ACTIVE) 153 atomic_dec_if_positive(&ns->ctrl->nr_active); 154 155 if (!(nvme_req(rq)->flags & NVME_MPATH_IO_STATS)) 156 return; 157 bdev_end_io_acct(ns->head->disk->part0, req_op(rq), 158 blk_rq_bytes(rq) >> SECTOR_SHIFT, 159 nvme_req(rq)->start_time); 160 } 161 162 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl) 163 { 164 struct nvme_ns *ns; 165 int srcu_idx; 166 167 srcu_idx = srcu_read_lock(&ctrl->srcu); 168 list_for_each_entry_srcu(ns, &ctrl->namespaces, list, 169 srcu_read_lock_held(&ctrl->srcu)) { 170 if (!ns->head->disk) 171 continue; 172 kblockd_schedule_work(&ns->head->requeue_work); 173 if (nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE) 174 disk_uevent(ns->head->disk, KOBJ_CHANGE); 175 } 176 srcu_read_unlock(&ctrl->srcu, srcu_idx); 177 } 178 179 static const char *nvme_ana_state_names[] = { 180 [0] = "invalid state", 181 [NVME_ANA_OPTIMIZED] = "optimized", 182 [NVME_ANA_NONOPTIMIZED] = "non-optimized", 183 [NVME_ANA_INACCESSIBLE] = "inaccessible", 184 [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss", 185 [NVME_ANA_CHANGE] = "change", 186 }; 187 188 bool nvme_mpath_clear_current_path(struct nvme_ns *ns) 189 { 190 struct nvme_ns_head *head = ns->head; 191 bool changed = false; 192 int node; 193 194 if (!head) 195 goto out; 196 197 for_each_node(node) { 198 if (ns == rcu_access_pointer(head->current_path[node])) { 199 rcu_assign_pointer(head->current_path[node], NULL); 200 changed = true; 201 } 202 } 203 out: 204 return changed; 205 } 206 207 void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl) 208 { 209 struct nvme_ns *ns; 210 int srcu_idx; 211 212 srcu_idx = srcu_read_lock(&ctrl->srcu); 213 list_for_each_entry_srcu(ns, &ctrl->namespaces, list, 214 srcu_read_lock_held(&ctrl->srcu)) { 215 nvme_mpath_clear_current_path(ns); 216 kblockd_schedule_work(&ns->head->requeue_work); 217 } 218 srcu_read_unlock(&ctrl->srcu, srcu_idx); 219 } 220 221 void nvme_mpath_revalidate_paths(struct nvme_ns *ns) 222 { 223 struct nvme_ns_head *head = ns->head; 224 sector_t capacity = get_capacity(head->disk); 225 int node; 226 int srcu_idx; 227 228 srcu_idx = srcu_read_lock(&head->srcu); 229 list_for_each_entry_srcu(ns, &head->list, siblings, 230 srcu_read_lock_held(&head->srcu)) { 231 if (capacity != get_capacity(ns->disk)) 232 clear_bit(NVME_NS_READY, &ns->flags); 233 } 234 srcu_read_unlock(&head->srcu, srcu_idx); 235 236 for_each_node(node) 237 rcu_assign_pointer(head->current_path[node], NULL); 238 kblockd_schedule_work(&head->requeue_work); 239 } 240 241 static bool nvme_path_is_disabled(struct nvme_ns *ns) 242 { 243 enum nvme_ctrl_state state = nvme_ctrl_state(ns->ctrl); 244 245 /* 246 * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should 247 * still be able to complete assuming that the controller is connected. 248 * Otherwise it will fail immediately and return to the requeue list. 249 */ 250 if (state != NVME_CTRL_LIVE && state != NVME_CTRL_DELETING) 251 return true; 252 if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) || 253 !test_bit(NVME_NS_READY, &ns->flags)) 254 return true; 255 return false; 256 } 257 258 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node) 259 { 260 int found_distance = INT_MAX, fallback_distance = INT_MAX, distance; 261 struct nvme_ns *found = NULL, *fallback = NULL, *ns; 262 263 list_for_each_entry_srcu(ns, &head->list, siblings, 264 srcu_read_lock_held(&head->srcu)) { 265 if (nvme_path_is_disabled(ns)) 266 continue; 267 268 if (ns->ctrl->numa_node != NUMA_NO_NODE && 269 READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA) 270 distance = node_distance(node, ns->ctrl->numa_node); 271 else 272 distance = LOCAL_DISTANCE; 273 274 switch (ns->ana_state) { 275 case NVME_ANA_OPTIMIZED: 276 if (distance < found_distance) { 277 found_distance = distance; 278 found = ns; 279 } 280 break; 281 case NVME_ANA_NONOPTIMIZED: 282 if (distance < fallback_distance) { 283 fallback_distance = distance; 284 fallback = ns; 285 } 286 break; 287 default: 288 break; 289 } 290 } 291 292 if (!found) 293 found = fallback; 294 if (found) 295 rcu_assign_pointer(head->current_path[node], found); 296 return found; 297 } 298 299 static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head, 300 struct nvme_ns *ns) 301 { 302 ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns, 303 siblings); 304 if (ns) 305 return ns; 306 return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings); 307 } 308 309 static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head) 310 { 311 struct nvme_ns *ns, *found = NULL; 312 int node = numa_node_id(); 313 struct nvme_ns *old = srcu_dereference(head->current_path[node], 314 &head->srcu); 315 316 if (unlikely(!old)) 317 return __nvme_find_path(head, node); 318 319 if (list_is_singular(&head->list)) { 320 if (nvme_path_is_disabled(old)) 321 return NULL; 322 return old; 323 } 324 325 for (ns = nvme_next_ns(head, old); 326 ns && ns != old; 327 ns = nvme_next_ns(head, ns)) { 328 if (nvme_path_is_disabled(ns)) 329 continue; 330 331 if (ns->ana_state == NVME_ANA_OPTIMIZED) { 332 found = ns; 333 goto out; 334 } 335 if (ns->ana_state == NVME_ANA_NONOPTIMIZED) 336 found = ns; 337 } 338 339 /* 340 * The loop above skips the current path for round-robin semantics. 341 * Fall back to the current path if either: 342 * - no other optimized path found and current is optimized, 343 * - no other usable path found and current is usable. 344 */ 345 if (!nvme_path_is_disabled(old) && 346 (old->ana_state == NVME_ANA_OPTIMIZED || 347 (!found && old->ana_state == NVME_ANA_NONOPTIMIZED))) 348 return old; 349 350 if (!found) 351 return NULL; 352 out: 353 rcu_assign_pointer(head->current_path[node], found); 354 return found; 355 } 356 357 static struct nvme_ns *nvme_queue_depth_path(struct nvme_ns_head *head) 358 { 359 struct nvme_ns *best_opt = NULL, *best_nonopt = NULL, *ns; 360 unsigned int min_depth_opt = UINT_MAX, min_depth_nonopt = UINT_MAX; 361 unsigned int depth; 362 363 list_for_each_entry_srcu(ns, &head->list, siblings, 364 srcu_read_lock_held(&head->srcu)) { 365 if (nvme_path_is_disabled(ns)) 366 continue; 367 368 depth = atomic_read(&ns->ctrl->nr_active); 369 370 switch (ns->ana_state) { 371 case NVME_ANA_OPTIMIZED: 372 if (depth < min_depth_opt) { 373 min_depth_opt = depth; 374 best_opt = ns; 375 } 376 break; 377 case NVME_ANA_NONOPTIMIZED: 378 if (depth < min_depth_nonopt) { 379 min_depth_nonopt = depth; 380 best_nonopt = ns; 381 } 382 break; 383 default: 384 break; 385 } 386 387 if (min_depth_opt == 0) 388 return best_opt; 389 } 390 391 return best_opt ? best_opt : best_nonopt; 392 } 393 394 static inline bool nvme_path_is_optimized(struct nvme_ns *ns) 395 { 396 return nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE && 397 ns->ana_state == NVME_ANA_OPTIMIZED; 398 } 399 400 static struct nvme_ns *nvme_numa_path(struct nvme_ns_head *head) 401 { 402 int node = numa_node_id(); 403 struct nvme_ns *ns; 404 405 ns = srcu_dereference(head->current_path[node], &head->srcu); 406 if (unlikely(!ns)) 407 return __nvme_find_path(head, node); 408 if (unlikely(!nvme_path_is_optimized(ns))) 409 return __nvme_find_path(head, node); 410 return ns; 411 } 412 413 inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head) 414 { 415 switch (READ_ONCE(head->subsys->iopolicy)) { 416 case NVME_IOPOLICY_QD: 417 return nvme_queue_depth_path(head); 418 case NVME_IOPOLICY_RR: 419 return nvme_round_robin_path(head); 420 default: 421 return nvme_numa_path(head); 422 } 423 } 424 425 static bool nvme_available_path(struct nvme_ns_head *head) 426 { 427 struct nvme_ns *ns; 428 429 if (!test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) 430 return false; 431 432 list_for_each_entry_srcu(ns, &head->list, siblings, 433 srcu_read_lock_held(&head->srcu)) { 434 if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags)) 435 continue; 436 switch (nvme_ctrl_state(ns->ctrl)) { 437 case NVME_CTRL_LIVE: 438 case NVME_CTRL_RESETTING: 439 case NVME_CTRL_CONNECTING: 440 return true; 441 default: 442 break; 443 } 444 } 445 return false; 446 } 447 448 static void nvme_ns_head_submit_bio(struct bio *bio) 449 { 450 struct nvme_ns_head *head = bio->bi_bdev->bd_disk->private_data; 451 struct device *dev = disk_to_dev(head->disk); 452 struct nvme_ns *ns; 453 int srcu_idx; 454 455 /* 456 * The namespace might be going away and the bio might be moved to a 457 * different queue via blk_steal_bios(), so we need to use the bio_split 458 * pool from the original queue to allocate the bvecs from. 459 */ 460 bio = bio_split_to_limits(bio); 461 if (!bio) 462 return; 463 464 srcu_idx = srcu_read_lock(&head->srcu); 465 ns = nvme_find_path(head); 466 if (likely(ns)) { 467 bio_set_dev(bio, ns->disk->part0); 468 bio->bi_opf |= REQ_NVME_MPATH; 469 trace_block_bio_remap(bio, disk_devt(ns->head->disk), 470 bio->bi_iter.bi_sector); 471 submit_bio_noacct(bio); 472 } else if (nvme_available_path(head)) { 473 dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n"); 474 475 spin_lock_irq(&head->requeue_lock); 476 bio_list_add(&head->requeue_list, bio); 477 spin_unlock_irq(&head->requeue_lock); 478 } else { 479 dev_warn_ratelimited(dev, "no available path - failing I/O\n"); 480 481 bio_io_error(bio); 482 } 483 484 srcu_read_unlock(&head->srcu, srcu_idx); 485 } 486 487 static int nvme_ns_head_open(struct gendisk *disk, blk_mode_t mode) 488 { 489 if (!nvme_tryget_ns_head(disk->private_data)) 490 return -ENXIO; 491 return 0; 492 } 493 494 static void nvme_ns_head_release(struct gendisk *disk) 495 { 496 nvme_put_ns_head(disk->private_data); 497 } 498 499 static int nvme_ns_head_get_unique_id(struct gendisk *disk, u8 id[16], 500 enum blk_unique_id type) 501 { 502 struct nvme_ns_head *head = disk->private_data; 503 struct nvme_ns *ns; 504 int srcu_idx, ret = -EWOULDBLOCK; 505 506 srcu_idx = srcu_read_lock(&head->srcu); 507 ns = nvme_find_path(head); 508 if (ns) 509 ret = nvme_ns_get_unique_id(ns, id, type); 510 srcu_read_unlock(&head->srcu, srcu_idx); 511 return ret; 512 } 513 514 #ifdef CONFIG_BLK_DEV_ZONED 515 static int nvme_ns_head_report_zones(struct gendisk *disk, sector_t sector, 516 unsigned int nr_zones, report_zones_cb cb, void *data) 517 { 518 struct nvme_ns_head *head = disk->private_data; 519 struct nvme_ns *ns; 520 int srcu_idx, ret = -EWOULDBLOCK; 521 522 srcu_idx = srcu_read_lock(&head->srcu); 523 ns = nvme_find_path(head); 524 if (ns) 525 ret = nvme_ns_report_zones(ns, sector, nr_zones, cb, data); 526 srcu_read_unlock(&head->srcu, srcu_idx); 527 return ret; 528 } 529 #else 530 #define nvme_ns_head_report_zones NULL 531 #endif /* CONFIG_BLK_DEV_ZONED */ 532 533 const struct block_device_operations nvme_ns_head_ops = { 534 .owner = THIS_MODULE, 535 .submit_bio = nvme_ns_head_submit_bio, 536 .open = nvme_ns_head_open, 537 .release = nvme_ns_head_release, 538 .ioctl = nvme_ns_head_ioctl, 539 .compat_ioctl = blkdev_compat_ptr_ioctl, 540 .getgeo = nvme_getgeo, 541 .get_unique_id = nvme_ns_head_get_unique_id, 542 .report_zones = nvme_ns_head_report_zones, 543 .pr_ops = &nvme_pr_ops, 544 }; 545 546 static inline struct nvme_ns_head *cdev_to_ns_head(struct cdev *cdev) 547 { 548 return container_of(cdev, struct nvme_ns_head, cdev); 549 } 550 551 static int nvme_ns_head_chr_open(struct inode *inode, struct file *file) 552 { 553 if (!nvme_tryget_ns_head(cdev_to_ns_head(inode->i_cdev))) 554 return -ENXIO; 555 return 0; 556 } 557 558 static int nvme_ns_head_chr_release(struct inode *inode, struct file *file) 559 { 560 nvme_put_ns_head(cdev_to_ns_head(inode->i_cdev)); 561 return 0; 562 } 563 564 static const struct file_operations nvme_ns_head_chr_fops = { 565 .owner = THIS_MODULE, 566 .open = nvme_ns_head_chr_open, 567 .release = nvme_ns_head_chr_release, 568 .unlocked_ioctl = nvme_ns_head_chr_ioctl, 569 .compat_ioctl = compat_ptr_ioctl, 570 .uring_cmd = nvme_ns_head_chr_uring_cmd, 571 .uring_cmd_iopoll = nvme_ns_chr_uring_cmd_iopoll, 572 }; 573 574 static int nvme_add_ns_head_cdev(struct nvme_ns_head *head) 575 { 576 int ret; 577 578 head->cdev_device.parent = &head->subsys->dev; 579 ret = dev_set_name(&head->cdev_device, "ng%dn%d", 580 head->subsys->instance, head->instance); 581 if (ret) 582 return ret; 583 ret = nvme_cdev_add(&head->cdev, &head->cdev_device, 584 &nvme_ns_head_chr_fops, THIS_MODULE); 585 return ret; 586 } 587 588 static void nvme_partition_scan_work(struct work_struct *work) 589 { 590 struct nvme_ns_head *head = 591 container_of(work, struct nvme_ns_head, partition_scan_work); 592 593 if (WARN_ON_ONCE(!test_and_clear_bit(GD_SUPPRESS_PART_SCAN, 594 &head->disk->state))) 595 return; 596 597 mutex_lock(&head->disk->open_mutex); 598 bdev_disk_changed(head->disk, false); 599 mutex_unlock(&head->disk->open_mutex); 600 } 601 602 static void nvme_requeue_work(struct work_struct *work) 603 { 604 struct nvme_ns_head *head = 605 container_of(work, struct nvme_ns_head, requeue_work); 606 struct bio *bio, *next; 607 608 spin_lock_irq(&head->requeue_lock); 609 next = bio_list_get(&head->requeue_list); 610 spin_unlock_irq(&head->requeue_lock); 611 612 while ((bio = next) != NULL) { 613 next = bio->bi_next; 614 bio->bi_next = NULL; 615 616 submit_bio_noacct(bio); 617 } 618 } 619 620 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head) 621 { 622 struct queue_limits lim; 623 624 mutex_init(&head->lock); 625 bio_list_init(&head->requeue_list); 626 spin_lock_init(&head->requeue_lock); 627 INIT_WORK(&head->requeue_work, nvme_requeue_work); 628 INIT_WORK(&head->partition_scan_work, nvme_partition_scan_work); 629 630 /* 631 * Add a multipath node if the subsystems supports multiple controllers. 632 * We also do this for private namespaces as the namespace sharing flag 633 * could change after a rescan. 634 */ 635 if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) || 636 !nvme_is_unique_nsid(ctrl, head) || !multipath) 637 return 0; 638 639 blk_set_stacking_limits(&lim); 640 lim.dma_alignment = 3; 641 lim.features |= BLK_FEAT_IO_STAT | BLK_FEAT_NOWAIT | 642 BLK_FEAT_POLL | BLK_FEAT_ATOMIC_WRITES; 643 if (head->ids.csi == NVME_CSI_ZNS) 644 lim.features |= BLK_FEAT_ZONED; 645 646 head->disk = blk_alloc_disk(&lim, ctrl->numa_node); 647 if (IS_ERR(head->disk)) 648 return PTR_ERR(head->disk); 649 head->disk->fops = &nvme_ns_head_ops; 650 head->disk->private_data = head; 651 652 /* 653 * We need to suppress the partition scan from occuring within the 654 * controller's scan_work context. If a path error occurs here, the IO 655 * will wait until a path becomes available or all paths are torn down, 656 * but that action also occurs within scan_work, so it would deadlock. 657 * Defer the partion scan to a different context that does not block 658 * scan_work. 659 */ 660 set_bit(GD_SUPPRESS_PART_SCAN, &head->disk->state); 661 sprintf(head->disk->disk_name, "nvme%dn%d", 662 ctrl->subsys->instance, head->instance); 663 return 0; 664 } 665 666 static void nvme_mpath_set_live(struct nvme_ns *ns) 667 { 668 struct nvme_ns_head *head = ns->head; 669 int rc; 670 671 if (!head->disk) 672 return; 673 674 /* 675 * test_and_set_bit() is used because it is protecting against two nvme 676 * paths simultaneously calling device_add_disk() on the same namespace 677 * head. 678 */ 679 if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) { 680 rc = device_add_disk(&head->subsys->dev, head->disk, 681 nvme_ns_attr_groups); 682 if (rc) { 683 clear_bit(NVME_NSHEAD_DISK_LIVE, &head->flags); 684 return; 685 } 686 nvme_add_ns_head_cdev(head); 687 kblockd_schedule_work(&head->partition_scan_work); 688 } 689 690 nvme_mpath_add_sysfs_link(ns->head); 691 692 mutex_lock(&head->lock); 693 if (nvme_path_is_optimized(ns)) { 694 int node, srcu_idx; 695 696 srcu_idx = srcu_read_lock(&head->srcu); 697 for_each_online_node(node) 698 __nvme_find_path(head, node); 699 srcu_read_unlock(&head->srcu, srcu_idx); 700 } 701 mutex_unlock(&head->lock); 702 703 synchronize_srcu(&head->srcu); 704 kblockd_schedule_work(&head->requeue_work); 705 } 706 707 static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data, 708 int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *, 709 void *)) 710 { 711 void *base = ctrl->ana_log_buf; 712 size_t offset = sizeof(struct nvme_ana_rsp_hdr); 713 int error, i; 714 715 lockdep_assert_held(&ctrl->ana_lock); 716 717 for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) { 718 struct nvme_ana_group_desc *desc = base + offset; 719 u32 nr_nsids; 720 size_t nsid_buf_size; 721 722 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc))) 723 return -EINVAL; 724 725 nr_nsids = le32_to_cpu(desc->nnsids); 726 nsid_buf_size = flex_array_size(desc, nsids, nr_nsids); 727 728 if (WARN_ON_ONCE(desc->grpid == 0)) 729 return -EINVAL; 730 if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax)) 731 return -EINVAL; 732 if (WARN_ON_ONCE(desc->state == 0)) 733 return -EINVAL; 734 if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE)) 735 return -EINVAL; 736 737 offset += sizeof(*desc); 738 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size)) 739 return -EINVAL; 740 741 error = cb(ctrl, desc, data); 742 if (error) 743 return error; 744 745 offset += nsid_buf_size; 746 } 747 748 return 0; 749 } 750 751 static inline bool nvme_state_is_live(enum nvme_ana_state state) 752 { 753 return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED; 754 } 755 756 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc, 757 struct nvme_ns *ns) 758 { 759 ns->ana_grpid = le32_to_cpu(desc->grpid); 760 ns->ana_state = desc->state; 761 clear_bit(NVME_NS_ANA_PENDING, &ns->flags); 762 /* 763 * nvme_mpath_set_live() will trigger I/O to the multipath path device 764 * and in turn to this path device. However we cannot accept this I/O 765 * if the controller is not live. This may deadlock if called from 766 * nvme_mpath_init_identify() and the ctrl will never complete 767 * initialization, preventing I/O from completing. For this case we 768 * will reprocess the ANA log page in nvme_mpath_update() once the 769 * controller is ready. 770 */ 771 if (nvme_state_is_live(ns->ana_state) && 772 nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE) 773 nvme_mpath_set_live(ns); 774 else { 775 /* 776 * Add sysfs link from multipath head gendisk node to path 777 * device gendisk node. 778 * If path's ana state is live (i.e. state is either optimized 779 * or non-optimized) while we alloc the ns then sysfs link would 780 * be created from nvme_mpath_set_live(). In that case we would 781 * not fallthrough this code path. However for the path's ana 782 * state other than live, we call nvme_mpath_set_live() only 783 * after ana state transitioned to the live state. But we still 784 * want to create the sysfs link from head node to a path device 785 * irrespctive of the path's ana state. 786 * If we reach through here then it means that path's ana state 787 * is not live but still create the sysfs link to this path from 788 * head node if head node of the path has already come alive. 789 */ 790 if (test_bit(NVME_NSHEAD_DISK_LIVE, &ns->head->flags)) 791 nvme_mpath_add_sysfs_link(ns->head); 792 } 793 } 794 795 static int nvme_update_ana_state(struct nvme_ctrl *ctrl, 796 struct nvme_ana_group_desc *desc, void *data) 797 { 798 u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0; 799 unsigned *nr_change_groups = data; 800 struct nvme_ns *ns; 801 int srcu_idx; 802 803 dev_dbg(ctrl->device, "ANA group %d: %s.\n", 804 le32_to_cpu(desc->grpid), 805 nvme_ana_state_names[desc->state]); 806 807 if (desc->state == NVME_ANA_CHANGE) 808 (*nr_change_groups)++; 809 810 if (!nr_nsids) 811 return 0; 812 813 srcu_idx = srcu_read_lock(&ctrl->srcu); 814 list_for_each_entry_srcu(ns, &ctrl->namespaces, list, 815 srcu_read_lock_held(&ctrl->srcu)) { 816 unsigned nsid; 817 again: 818 nsid = le32_to_cpu(desc->nsids[n]); 819 if (ns->head->ns_id < nsid) 820 continue; 821 if (ns->head->ns_id == nsid) 822 nvme_update_ns_ana_state(desc, ns); 823 if (++n == nr_nsids) 824 break; 825 if (ns->head->ns_id > nsid) 826 goto again; 827 } 828 srcu_read_unlock(&ctrl->srcu, srcu_idx); 829 return 0; 830 } 831 832 static int nvme_read_ana_log(struct nvme_ctrl *ctrl) 833 { 834 u32 nr_change_groups = 0; 835 int error; 836 837 mutex_lock(&ctrl->ana_lock); 838 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM, 839 ctrl->ana_log_buf, ctrl->ana_log_size, 0); 840 if (error) { 841 dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error); 842 goto out_unlock; 843 } 844 845 error = nvme_parse_ana_log(ctrl, &nr_change_groups, 846 nvme_update_ana_state); 847 if (error) 848 goto out_unlock; 849 850 /* 851 * In theory we should have an ANATT timer per group as they might enter 852 * the change state at different times. But that is a lot of overhead 853 * just to protect against a target that keeps entering new changes 854 * states while never finishing previous ones. But we'll still 855 * eventually time out once all groups are in change state, so this 856 * isn't a big deal. 857 * 858 * We also double the ANATT value to provide some slack for transports 859 * or AEN processing overhead. 860 */ 861 if (nr_change_groups) 862 mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies); 863 else 864 timer_delete_sync(&ctrl->anatt_timer); 865 out_unlock: 866 mutex_unlock(&ctrl->ana_lock); 867 return error; 868 } 869 870 static void nvme_ana_work(struct work_struct *work) 871 { 872 struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work); 873 874 if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE) 875 return; 876 877 nvme_read_ana_log(ctrl); 878 } 879 880 void nvme_mpath_update(struct nvme_ctrl *ctrl) 881 { 882 u32 nr_change_groups = 0; 883 884 if (!ctrl->ana_log_buf) 885 return; 886 887 mutex_lock(&ctrl->ana_lock); 888 nvme_parse_ana_log(ctrl, &nr_change_groups, nvme_update_ana_state); 889 mutex_unlock(&ctrl->ana_lock); 890 } 891 892 static void nvme_anatt_timeout(struct timer_list *t) 893 { 894 struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer); 895 896 dev_info(ctrl->device, "ANATT timeout, resetting controller.\n"); 897 nvme_reset_ctrl(ctrl); 898 } 899 900 void nvme_mpath_stop(struct nvme_ctrl *ctrl) 901 { 902 if (!nvme_ctrl_use_ana(ctrl)) 903 return; 904 timer_delete_sync(&ctrl->anatt_timer); 905 cancel_work_sync(&ctrl->ana_work); 906 } 907 908 #define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \ 909 struct device_attribute subsys_attr_##_name = \ 910 __ATTR(_name, _mode, _show, _store) 911 912 static ssize_t nvme_subsys_iopolicy_show(struct device *dev, 913 struct device_attribute *attr, char *buf) 914 { 915 struct nvme_subsystem *subsys = 916 container_of(dev, struct nvme_subsystem, dev); 917 918 return sysfs_emit(buf, "%s\n", 919 nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]); 920 } 921 922 static void nvme_subsys_iopolicy_update(struct nvme_subsystem *subsys, 923 int iopolicy) 924 { 925 struct nvme_ctrl *ctrl; 926 int old_iopolicy = READ_ONCE(subsys->iopolicy); 927 928 if (old_iopolicy == iopolicy) 929 return; 930 931 WRITE_ONCE(subsys->iopolicy, iopolicy); 932 933 /* iopolicy changes clear the mpath by design */ 934 mutex_lock(&nvme_subsystems_lock); 935 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) 936 nvme_mpath_clear_ctrl_paths(ctrl); 937 mutex_unlock(&nvme_subsystems_lock); 938 939 pr_notice("subsysnqn %s iopolicy changed from %s to %s\n", 940 subsys->subnqn, 941 nvme_iopolicy_names[old_iopolicy], 942 nvme_iopolicy_names[iopolicy]); 943 } 944 945 static ssize_t nvme_subsys_iopolicy_store(struct device *dev, 946 struct device_attribute *attr, const char *buf, size_t count) 947 { 948 struct nvme_subsystem *subsys = 949 container_of(dev, struct nvme_subsystem, dev); 950 int i; 951 952 for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) { 953 if (sysfs_streq(buf, nvme_iopolicy_names[i])) { 954 nvme_subsys_iopolicy_update(subsys, i); 955 return count; 956 } 957 } 958 959 return -EINVAL; 960 } 961 SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR, 962 nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store); 963 964 static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr, 965 char *buf) 966 { 967 return sysfs_emit(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid); 968 } 969 DEVICE_ATTR_RO(ana_grpid); 970 971 static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr, 972 char *buf) 973 { 974 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 975 976 return sysfs_emit(buf, "%s\n", nvme_ana_state_names[ns->ana_state]); 977 } 978 DEVICE_ATTR_RO(ana_state); 979 980 static ssize_t queue_depth_show(struct device *dev, 981 struct device_attribute *attr, char *buf) 982 { 983 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 984 985 if (ns->head->subsys->iopolicy != NVME_IOPOLICY_QD) 986 return 0; 987 988 return sysfs_emit(buf, "%d\n", atomic_read(&ns->ctrl->nr_active)); 989 } 990 DEVICE_ATTR_RO(queue_depth); 991 992 static ssize_t numa_nodes_show(struct device *dev, struct device_attribute *attr, 993 char *buf) 994 { 995 int node, srcu_idx; 996 nodemask_t numa_nodes; 997 struct nvme_ns *current_ns; 998 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 999 struct nvme_ns_head *head = ns->head; 1000 1001 if (head->subsys->iopolicy != NVME_IOPOLICY_NUMA) 1002 return 0; 1003 1004 nodes_clear(numa_nodes); 1005 1006 srcu_idx = srcu_read_lock(&head->srcu); 1007 for_each_node(node) { 1008 current_ns = srcu_dereference(head->current_path[node], 1009 &head->srcu); 1010 if (ns == current_ns) 1011 node_set(node, numa_nodes); 1012 } 1013 srcu_read_unlock(&head->srcu, srcu_idx); 1014 1015 return sysfs_emit(buf, "%*pbl\n", nodemask_pr_args(&numa_nodes)); 1016 } 1017 DEVICE_ATTR_RO(numa_nodes); 1018 1019 static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl, 1020 struct nvme_ana_group_desc *desc, void *data) 1021 { 1022 struct nvme_ana_group_desc *dst = data; 1023 1024 if (desc->grpid != dst->grpid) 1025 return 0; 1026 1027 *dst = *desc; 1028 return -ENXIO; /* just break out of the loop */ 1029 } 1030 1031 void nvme_mpath_add_sysfs_link(struct nvme_ns_head *head) 1032 { 1033 struct device *target; 1034 int rc, srcu_idx; 1035 struct nvme_ns *ns; 1036 struct kobject *kobj; 1037 1038 /* 1039 * Ensure head disk node is already added otherwise we may get invalid 1040 * kobj for head disk node 1041 */ 1042 if (!test_bit(GD_ADDED, &head->disk->state)) 1043 return; 1044 1045 kobj = &disk_to_dev(head->disk)->kobj; 1046 1047 /* 1048 * loop through each ns chained through the head->list and create the 1049 * sysfs link from head node to the ns path node 1050 */ 1051 srcu_idx = srcu_read_lock(&head->srcu); 1052 1053 list_for_each_entry_rcu(ns, &head->list, siblings) { 1054 /* 1055 * Ensure that ns path disk node is already added otherwise we 1056 * may get invalid kobj name for target 1057 */ 1058 if (!test_bit(GD_ADDED, &ns->disk->state)) 1059 continue; 1060 1061 /* 1062 * Avoid creating link if it already exists for the given path. 1063 * When path ana state transitions from optimized to non- 1064 * optimized or vice-versa, the nvme_mpath_set_live() is 1065 * invoked which in truns call this function. Now if the sysfs 1066 * link already exists for the given path and we attempt to re- 1067 * create the link then sysfs code would warn about it loudly. 1068 * So we evaluate NVME_NS_SYSFS_ATTR_LINK flag here to ensure 1069 * that we're not creating duplicate link. 1070 * The test_and_set_bit() is used because it is protecting 1071 * against multiple nvme paths being simultaneously added. 1072 */ 1073 if (test_and_set_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags)) 1074 continue; 1075 1076 target = disk_to_dev(ns->disk); 1077 /* 1078 * Create sysfs link from head gendisk kobject @kobj to the 1079 * ns path gendisk kobject @target->kobj. 1080 */ 1081 rc = sysfs_add_link_to_group(kobj, nvme_ns_mpath_attr_group.name, 1082 &target->kobj, dev_name(target)); 1083 if (unlikely(rc)) { 1084 dev_err(disk_to_dev(ns->head->disk), 1085 "failed to create link to %s\n", 1086 dev_name(target)); 1087 clear_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags); 1088 } 1089 } 1090 1091 srcu_read_unlock(&head->srcu, srcu_idx); 1092 } 1093 1094 void nvme_mpath_remove_sysfs_link(struct nvme_ns *ns) 1095 { 1096 struct device *target; 1097 struct kobject *kobj; 1098 1099 if (!test_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags)) 1100 return; 1101 1102 target = disk_to_dev(ns->disk); 1103 kobj = &disk_to_dev(ns->head->disk)->kobj; 1104 sysfs_remove_link_from_group(kobj, nvme_ns_mpath_attr_group.name, 1105 dev_name(target)); 1106 clear_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags); 1107 } 1108 1109 void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid) 1110 { 1111 if (nvme_ctrl_use_ana(ns->ctrl)) { 1112 struct nvme_ana_group_desc desc = { 1113 .grpid = anagrpid, 1114 .state = 0, 1115 }; 1116 1117 mutex_lock(&ns->ctrl->ana_lock); 1118 ns->ana_grpid = le32_to_cpu(anagrpid); 1119 nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc); 1120 mutex_unlock(&ns->ctrl->ana_lock); 1121 if (desc.state) { 1122 /* found the group desc: update */ 1123 nvme_update_ns_ana_state(&desc, ns); 1124 } else { 1125 /* group desc not found: trigger a re-read */ 1126 set_bit(NVME_NS_ANA_PENDING, &ns->flags); 1127 queue_work(nvme_wq, &ns->ctrl->ana_work); 1128 } 1129 } else { 1130 ns->ana_state = NVME_ANA_OPTIMIZED; 1131 nvme_mpath_set_live(ns); 1132 } 1133 1134 #ifdef CONFIG_BLK_DEV_ZONED 1135 if (blk_queue_is_zoned(ns->queue) && ns->head->disk) 1136 ns->head->disk->nr_zones = ns->disk->nr_zones; 1137 #endif 1138 } 1139 1140 void nvme_mpath_shutdown_disk(struct nvme_ns_head *head) 1141 { 1142 if (!head->disk) 1143 return; 1144 if (test_and_clear_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) { 1145 nvme_cdev_del(&head->cdev, &head->cdev_device); 1146 /* 1147 * requeue I/O after NVME_NSHEAD_DISK_LIVE has been cleared 1148 * to allow multipath to fail all I/O. 1149 */ 1150 synchronize_srcu(&head->srcu); 1151 kblockd_schedule_work(&head->requeue_work); 1152 del_gendisk(head->disk); 1153 } 1154 } 1155 1156 void nvme_mpath_remove_disk(struct nvme_ns_head *head) 1157 { 1158 if (!head->disk) 1159 return; 1160 /* make sure all pending bios are cleaned up */ 1161 kblockd_schedule_work(&head->requeue_work); 1162 flush_work(&head->requeue_work); 1163 flush_work(&head->partition_scan_work); 1164 put_disk(head->disk); 1165 } 1166 1167 void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl) 1168 { 1169 mutex_init(&ctrl->ana_lock); 1170 timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0); 1171 INIT_WORK(&ctrl->ana_work, nvme_ana_work); 1172 } 1173 1174 int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) 1175 { 1176 size_t max_transfer_size = ctrl->max_hw_sectors << SECTOR_SHIFT; 1177 size_t ana_log_size; 1178 int error = 0; 1179 1180 /* check if multipath is enabled and we have the capability */ 1181 if (!multipath || !ctrl->subsys || 1182 !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA)) 1183 return 0; 1184 1185 /* initialize this in the identify path to cover controller resets */ 1186 atomic_set(&ctrl->nr_active, 0); 1187 1188 if (!ctrl->max_namespaces || 1189 ctrl->max_namespaces > le32_to_cpu(id->nn)) { 1190 dev_err(ctrl->device, 1191 "Invalid MNAN value %u\n", ctrl->max_namespaces); 1192 return -EINVAL; 1193 } 1194 1195 ctrl->anacap = id->anacap; 1196 ctrl->anatt = id->anatt; 1197 ctrl->nanagrpid = le32_to_cpu(id->nanagrpid); 1198 ctrl->anagrpmax = le32_to_cpu(id->anagrpmax); 1199 1200 ana_log_size = sizeof(struct nvme_ana_rsp_hdr) + 1201 ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) + 1202 ctrl->max_namespaces * sizeof(__le32); 1203 if (ana_log_size > max_transfer_size) { 1204 dev_err(ctrl->device, 1205 "ANA log page size (%zd) larger than MDTS (%zd).\n", 1206 ana_log_size, max_transfer_size); 1207 dev_err(ctrl->device, "disabling ANA support.\n"); 1208 goto out_uninit; 1209 } 1210 if (ana_log_size > ctrl->ana_log_size) { 1211 nvme_mpath_stop(ctrl); 1212 nvme_mpath_uninit(ctrl); 1213 ctrl->ana_log_buf = kvmalloc(ana_log_size, GFP_KERNEL); 1214 if (!ctrl->ana_log_buf) 1215 return -ENOMEM; 1216 } 1217 ctrl->ana_log_size = ana_log_size; 1218 error = nvme_read_ana_log(ctrl); 1219 if (error) 1220 goto out_uninit; 1221 return 0; 1222 1223 out_uninit: 1224 nvme_mpath_uninit(ctrl); 1225 return error; 1226 } 1227 1228 void nvme_mpath_uninit(struct nvme_ctrl *ctrl) 1229 { 1230 kvfree(ctrl->ana_log_buf); 1231 ctrl->ana_log_buf = NULL; 1232 ctrl->ana_log_size = 0; 1233 } 1234