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 }; 21 22 static int iopolicy = NVME_IOPOLICY_NUMA; 23 24 static int nvme_set_iopolicy(const char *val, const struct kernel_param *kp) 25 { 26 if (!val) 27 return -EINVAL; 28 if (!strncmp(val, "numa", 4)) 29 iopolicy = NVME_IOPOLICY_NUMA; 30 else if (!strncmp(val, "round-robin", 11)) 31 iopolicy = NVME_IOPOLICY_RR; 32 else 33 return -EINVAL; 34 35 return 0; 36 } 37 38 static int nvme_get_iopolicy(char *buf, const struct kernel_param *kp) 39 { 40 return sprintf(buf, "%s\n", nvme_iopolicy_names[iopolicy]); 41 } 42 43 module_param_call(iopolicy, nvme_set_iopolicy, nvme_get_iopolicy, 44 &iopolicy, 0644); 45 MODULE_PARM_DESC(iopolicy, 46 "Default multipath I/O policy; 'numa' (default) or 'round-robin'"); 47 48 void nvme_mpath_default_iopolicy(struct nvme_subsystem *subsys) 49 { 50 subsys->iopolicy = iopolicy; 51 } 52 53 void nvme_mpath_unfreeze(struct nvme_subsystem *subsys) 54 { 55 struct nvme_ns_head *h; 56 57 lockdep_assert_held(&subsys->lock); 58 list_for_each_entry(h, &subsys->nsheads, entry) 59 if (h->disk) 60 blk_mq_unfreeze_queue(h->disk->queue); 61 } 62 63 void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys) 64 { 65 struct nvme_ns_head *h; 66 67 lockdep_assert_held(&subsys->lock); 68 list_for_each_entry(h, &subsys->nsheads, entry) 69 if (h->disk) 70 blk_mq_freeze_queue_wait(h->disk->queue); 71 } 72 73 void nvme_mpath_start_freeze(struct nvme_subsystem *subsys) 74 { 75 struct nvme_ns_head *h; 76 77 lockdep_assert_held(&subsys->lock); 78 list_for_each_entry(h, &subsys->nsheads, entry) 79 if (h->disk) 80 blk_freeze_queue_start(h->disk->queue); 81 } 82 83 void nvme_failover_req(struct request *req) 84 { 85 struct nvme_ns *ns = req->q->queuedata; 86 u16 status = nvme_req(req)->status & 0x7ff; 87 unsigned long flags; 88 struct bio *bio; 89 90 nvme_mpath_clear_current_path(ns); 91 92 /* 93 * If we got back an ANA error, we know the controller is alive but not 94 * ready to serve this namespace. Kick of a re-read of the ANA 95 * information page, and just try any other available path for now. 96 */ 97 if (nvme_is_ana_error(status) && ns->ctrl->ana_log_buf) { 98 set_bit(NVME_NS_ANA_PENDING, &ns->flags); 99 queue_work(nvme_wq, &ns->ctrl->ana_work); 100 } 101 102 spin_lock_irqsave(&ns->head->requeue_lock, flags); 103 for (bio = req->bio; bio; bio = bio->bi_next) { 104 bio_set_dev(bio, ns->head->disk->part0); 105 if (bio->bi_opf & REQ_POLLED) { 106 bio->bi_opf &= ~REQ_POLLED; 107 bio->bi_cookie = BLK_QC_T_NONE; 108 } 109 /* 110 * The alternate request queue that we may end up submitting 111 * the bio to may be frozen temporarily, in this case REQ_NOWAIT 112 * will fail the I/O immediately with EAGAIN to the issuer. 113 * We are not in the issuer context which cannot block. Clear 114 * the flag to avoid spurious EAGAIN I/O failures. 115 */ 116 bio->bi_opf &= ~REQ_NOWAIT; 117 } 118 blk_steal_bios(&ns->head->requeue_list, req); 119 spin_unlock_irqrestore(&ns->head->requeue_lock, flags); 120 121 nvme_req(req)->status = 0; 122 nvme_end_req(req); 123 kblockd_schedule_work(&ns->head->requeue_work); 124 } 125 126 void nvme_mpath_start_request(struct request *rq) 127 { 128 struct nvme_ns *ns = rq->q->queuedata; 129 struct gendisk *disk = ns->head->disk; 130 131 if (!blk_queue_io_stat(disk->queue) || blk_rq_is_passthrough(rq)) 132 return; 133 134 nvme_req(rq)->flags |= NVME_MPATH_IO_STATS; 135 nvme_req(rq)->start_time = bdev_start_io_acct(disk->part0, req_op(rq), 136 jiffies); 137 } 138 EXPORT_SYMBOL_GPL(nvme_mpath_start_request); 139 140 void nvme_mpath_end_request(struct request *rq) 141 { 142 struct nvme_ns *ns = rq->q->queuedata; 143 144 if (!(nvme_req(rq)->flags & NVME_MPATH_IO_STATS)) 145 return; 146 bdev_end_io_acct(ns->head->disk->part0, req_op(rq), 147 blk_rq_bytes(rq) >> SECTOR_SHIFT, 148 nvme_req(rq)->start_time); 149 } 150 151 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl) 152 { 153 struct nvme_ns *ns; 154 int srcu_idx; 155 156 srcu_idx = srcu_read_lock(&ctrl->srcu); 157 list_for_each_entry_rcu(ns, &ctrl->namespaces, list) { 158 if (!ns->head->disk) 159 continue; 160 kblockd_schedule_work(&ns->head->requeue_work); 161 if (ctrl->state == NVME_CTRL_LIVE) 162 disk_uevent(ns->head->disk, KOBJ_CHANGE); 163 } 164 srcu_read_unlock(&ctrl->srcu, srcu_idx); 165 } 166 167 static const char *nvme_ana_state_names[] = { 168 [0] = "invalid state", 169 [NVME_ANA_OPTIMIZED] = "optimized", 170 [NVME_ANA_NONOPTIMIZED] = "non-optimized", 171 [NVME_ANA_INACCESSIBLE] = "inaccessible", 172 [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss", 173 [NVME_ANA_CHANGE] = "change", 174 }; 175 176 bool nvme_mpath_clear_current_path(struct nvme_ns *ns) 177 { 178 struct nvme_ns_head *head = ns->head; 179 bool changed = false; 180 int node; 181 182 if (!head) 183 goto out; 184 185 for_each_node(node) { 186 if (ns == rcu_access_pointer(head->current_path[node])) { 187 rcu_assign_pointer(head->current_path[node], NULL); 188 changed = true; 189 } 190 } 191 out: 192 return changed; 193 } 194 195 void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl) 196 { 197 struct nvme_ns *ns; 198 int srcu_idx; 199 200 srcu_idx = srcu_read_lock(&ctrl->srcu); 201 list_for_each_entry_rcu(ns, &ctrl->namespaces, list) { 202 nvme_mpath_clear_current_path(ns); 203 kblockd_schedule_work(&ns->head->requeue_work); 204 } 205 srcu_read_unlock(&ctrl->srcu, srcu_idx); 206 } 207 208 void nvme_mpath_revalidate_paths(struct nvme_ns *ns) 209 { 210 struct nvme_ns_head *head = ns->head; 211 sector_t capacity = get_capacity(head->disk); 212 int node; 213 int srcu_idx; 214 215 srcu_idx = srcu_read_lock(&head->srcu); 216 list_for_each_entry_rcu(ns, &head->list, siblings) { 217 if (capacity != get_capacity(ns->disk)) 218 clear_bit(NVME_NS_READY, &ns->flags); 219 } 220 srcu_read_unlock(&head->srcu, srcu_idx); 221 222 for_each_node(node) 223 rcu_assign_pointer(head->current_path[node], NULL); 224 kblockd_schedule_work(&head->requeue_work); 225 } 226 227 static bool nvme_path_is_disabled(struct nvme_ns *ns) 228 { 229 /* 230 * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should 231 * still be able to complete assuming that the controller is connected. 232 * Otherwise it will fail immediately and return to the requeue list. 233 */ 234 if (ns->ctrl->state != NVME_CTRL_LIVE && 235 ns->ctrl->state != NVME_CTRL_DELETING) 236 return true; 237 if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) || 238 !test_bit(NVME_NS_READY, &ns->flags)) 239 return true; 240 return false; 241 } 242 243 static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node) 244 { 245 int found_distance = INT_MAX, fallback_distance = INT_MAX, distance; 246 struct nvme_ns *found = NULL, *fallback = NULL, *ns; 247 248 list_for_each_entry_rcu(ns, &head->list, siblings) { 249 if (nvme_path_is_disabled(ns)) 250 continue; 251 252 if (ns->ctrl->numa_node != NUMA_NO_NODE && 253 READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA) 254 distance = node_distance(node, ns->ctrl->numa_node); 255 else 256 distance = LOCAL_DISTANCE; 257 258 switch (ns->ana_state) { 259 case NVME_ANA_OPTIMIZED: 260 if (distance < found_distance) { 261 found_distance = distance; 262 found = ns; 263 } 264 break; 265 case NVME_ANA_NONOPTIMIZED: 266 if (distance < fallback_distance) { 267 fallback_distance = distance; 268 fallback = ns; 269 } 270 break; 271 default: 272 break; 273 } 274 } 275 276 if (!found) 277 found = fallback; 278 if (found) 279 rcu_assign_pointer(head->current_path[node], found); 280 return found; 281 } 282 283 static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head, 284 struct nvme_ns *ns) 285 { 286 ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns, 287 siblings); 288 if (ns) 289 return ns; 290 return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings); 291 } 292 293 static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head, 294 int node, struct nvme_ns *old) 295 { 296 struct nvme_ns *ns, *found = NULL; 297 298 if (list_is_singular(&head->list)) { 299 if (nvme_path_is_disabled(old)) 300 return NULL; 301 return old; 302 } 303 304 for (ns = nvme_next_ns(head, old); 305 ns && ns != old; 306 ns = nvme_next_ns(head, ns)) { 307 if (nvme_path_is_disabled(ns)) 308 continue; 309 310 if (ns->ana_state == NVME_ANA_OPTIMIZED) { 311 found = ns; 312 goto out; 313 } 314 if (ns->ana_state == NVME_ANA_NONOPTIMIZED) 315 found = ns; 316 } 317 318 /* 319 * The loop above skips the current path for round-robin semantics. 320 * Fall back to the current path if either: 321 * - no other optimized path found and current is optimized, 322 * - no other usable path found and current is usable. 323 */ 324 if (!nvme_path_is_disabled(old) && 325 (old->ana_state == NVME_ANA_OPTIMIZED || 326 (!found && old->ana_state == NVME_ANA_NONOPTIMIZED))) 327 return old; 328 329 if (!found) 330 return NULL; 331 out: 332 rcu_assign_pointer(head->current_path[node], found); 333 return found; 334 } 335 336 static inline bool nvme_path_is_optimized(struct nvme_ns *ns) 337 { 338 return ns->ctrl->state == NVME_CTRL_LIVE && 339 ns->ana_state == NVME_ANA_OPTIMIZED; 340 } 341 342 inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head) 343 { 344 int node = numa_node_id(); 345 struct nvme_ns *ns; 346 347 ns = srcu_dereference(head->current_path[node], &head->srcu); 348 if (unlikely(!ns)) 349 return __nvme_find_path(head, node); 350 351 if (READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_RR) 352 return nvme_round_robin_path(head, node, ns); 353 if (unlikely(!nvme_path_is_optimized(ns))) 354 return __nvme_find_path(head, node); 355 return ns; 356 } 357 358 static bool nvme_available_path(struct nvme_ns_head *head) 359 { 360 struct nvme_ns *ns; 361 362 list_for_each_entry_rcu(ns, &head->list, siblings) { 363 if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags)) 364 continue; 365 switch (ns->ctrl->state) { 366 case NVME_CTRL_LIVE: 367 case NVME_CTRL_RESETTING: 368 case NVME_CTRL_CONNECTING: 369 /* fallthru */ 370 return true; 371 default: 372 break; 373 } 374 } 375 return false; 376 } 377 378 static void nvme_ns_head_submit_bio(struct bio *bio) 379 { 380 struct nvme_ns_head *head = bio->bi_bdev->bd_disk->private_data; 381 struct device *dev = disk_to_dev(head->disk); 382 struct nvme_ns *ns; 383 int srcu_idx; 384 385 /* 386 * The namespace might be going away and the bio might be moved to a 387 * different queue via blk_steal_bios(), so we need to use the bio_split 388 * pool from the original queue to allocate the bvecs from. 389 */ 390 bio = bio_split_to_limits(bio); 391 if (!bio) 392 return; 393 394 srcu_idx = srcu_read_lock(&head->srcu); 395 ns = nvme_find_path(head); 396 if (likely(ns)) { 397 bio_set_dev(bio, ns->disk->part0); 398 bio->bi_opf |= REQ_NVME_MPATH; 399 trace_block_bio_remap(bio, disk_devt(ns->head->disk), 400 bio->bi_iter.bi_sector); 401 submit_bio_noacct(bio); 402 } else if (nvme_available_path(head)) { 403 dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n"); 404 405 spin_lock_irq(&head->requeue_lock); 406 bio_list_add(&head->requeue_list, bio); 407 spin_unlock_irq(&head->requeue_lock); 408 } else { 409 dev_warn_ratelimited(dev, "no available path - failing I/O\n"); 410 411 bio_io_error(bio); 412 } 413 414 srcu_read_unlock(&head->srcu, srcu_idx); 415 } 416 417 static int nvme_ns_head_open(struct gendisk *disk, blk_mode_t mode) 418 { 419 if (!nvme_tryget_ns_head(disk->private_data)) 420 return -ENXIO; 421 return 0; 422 } 423 424 static void nvme_ns_head_release(struct gendisk *disk) 425 { 426 nvme_put_ns_head(disk->private_data); 427 } 428 429 #ifdef CONFIG_BLK_DEV_ZONED 430 static int nvme_ns_head_report_zones(struct gendisk *disk, sector_t sector, 431 unsigned int nr_zones, report_zones_cb cb, void *data) 432 { 433 struct nvme_ns_head *head = disk->private_data; 434 struct nvme_ns *ns; 435 int srcu_idx, ret = -EWOULDBLOCK; 436 437 srcu_idx = srcu_read_lock(&head->srcu); 438 ns = nvme_find_path(head); 439 if (ns) 440 ret = nvme_ns_report_zones(ns, sector, nr_zones, cb, data); 441 srcu_read_unlock(&head->srcu, srcu_idx); 442 return ret; 443 } 444 #else 445 #define nvme_ns_head_report_zones NULL 446 #endif /* CONFIG_BLK_DEV_ZONED */ 447 448 const struct block_device_operations nvme_ns_head_ops = { 449 .owner = THIS_MODULE, 450 .submit_bio = nvme_ns_head_submit_bio, 451 .open = nvme_ns_head_open, 452 .release = nvme_ns_head_release, 453 .ioctl = nvme_ns_head_ioctl, 454 .compat_ioctl = blkdev_compat_ptr_ioctl, 455 .getgeo = nvme_getgeo, 456 .report_zones = nvme_ns_head_report_zones, 457 .pr_ops = &nvme_pr_ops, 458 }; 459 460 static inline struct nvme_ns_head *cdev_to_ns_head(struct cdev *cdev) 461 { 462 return container_of(cdev, struct nvme_ns_head, cdev); 463 } 464 465 static int nvme_ns_head_chr_open(struct inode *inode, struct file *file) 466 { 467 if (!nvme_tryget_ns_head(cdev_to_ns_head(inode->i_cdev))) 468 return -ENXIO; 469 return 0; 470 } 471 472 static int nvme_ns_head_chr_release(struct inode *inode, struct file *file) 473 { 474 nvme_put_ns_head(cdev_to_ns_head(inode->i_cdev)); 475 return 0; 476 } 477 478 static const struct file_operations nvme_ns_head_chr_fops = { 479 .owner = THIS_MODULE, 480 .open = nvme_ns_head_chr_open, 481 .release = nvme_ns_head_chr_release, 482 .unlocked_ioctl = nvme_ns_head_chr_ioctl, 483 .compat_ioctl = compat_ptr_ioctl, 484 .uring_cmd = nvme_ns_head_chr_uring_cmd, 485 .uring_cmd_iopoll = nvme_ns_chr_uring_cmd_iopoll, 486 }; 487 488 static int nvme_add_ns_head_cdev(struct nvme_ns_head *head) 489 { 490 int ret; 491 492 head->cdev_device.parent = &head->subsys->dev; 493 ret = dev_set_name(&head->cdev_device, "ng%dn%d", 494 head->subsys->instance, head->instance); 495 if (ret) 496 return ret; 497 ret = nvme_cdev_add(&head->cdev, &head->cdev_device, 498 &nvme_ns_head_chr_fops, THIS_MODULE); 499 return ret; 500 } 501 502 static void nvme_partition_scan_work(struct work_struct *work) 503 { 504 struct nvme_ns_head *head = 505 container_of(work, struct nvme_ns_head, partition_scan_work); 506 507 if (WARN_ON_ONCE(!test_and_clear_bit(GD_SUPPRESS_PART_SCAN, 508 &head->disk->state))) 509 return; 510 511 mutex_lock(&head->disk->open_mutex); 512 bdev_disk_changed(head->disk, false); 513 mutex_unlock(&head->disk->open_mutex); 514 } 515 516 static void nvme_requeue_work(struct work_struct *work) 517 { 518 struct nvme_ns_head *head = 519 container_of(work, struct nvme_ns_head, requeue_work); 520 struct bio *bio, *next; 521 522 spin_lock_irq(&head->requeue_lock); 523 next = bio_list_get(&head->requeue_list); 524 spin_unlock_irq(&head->requeue_lock); 525 526 while ((bio = next) != NULL) { 527 next = bio->bi_next; 528 bio->bi_next = NULL; 529 530 submit_bio_noacct(bio); 531 } 532 } 533 534 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head) 535 { 536 bool vwc = false; 537 538 mutex_init(&head->lock); 539 bio_list_init(&head->requeue_list); 540 spin_lock_init(&head->requeue_lock); 541 INIT_WORK(&head->requeue_work, nvme_requeue_work); 542 INIT_WORK(&head->partition_scan_work, nvme_partition_scan_work); 543 544 /* 545 * Add a multipath node if the subsystems supports multiple controllers. 546 * We also do this for private namespaces as the namespace sharing flag 547 * could change after a rescan. 548 */ 549 if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) || 550 !nvme_is_unique_nsid(ctrl, head) || !multipath) 551 return 0; 552 553 head->disk = blk_alloc_disk(ctrl->numa_node); 554 if (!head->disk) 555 return -ENOMEM; 556 head->disk->fops = &nvme_ns_head_ops; 557 head->disk->private_data = head; 558 559 /* 560 * We need to suppress the partition scan from occuring within the 561 * controller's scan_work context. If a path error occurs here, the IO 562 * will wait until a path becomes available or all paths are torn down, 563 * but that action also occurs within scan_work, so it would deadlock. 564 * Defer the partion scan to a different context that does not block 565 * scan_work. 566 */ 567 set_bit(GD_SUPPRESS_PART_SCAN, &head->disk->state); 568 sprintf(head->disk->disk_name, "nvme%dn%d", 569 ctrl->subsys->instance, head->instance); 570 571 blk_queue_flag_set(QUEUE_FLAG_NONROT, head->disk->queue); 572 blk_queue_flag_set(QUEUE_FLAG_NOWAIT, head->disk->queue); 573 blk_queue_flag_set(QUEUE_FLAG_IO_STAT, head->disk->queue); 574 /* 575 * This assumes all controllers that refer to a namespace either 576 * support poll queues or not. That is not a strict guarantee, 577 * but if the assumption is wrong the effect is only suboptimal 578 * performance but not correctness problem. 579 */ 580 if (ctrl->tagset->nr_maps > HCTX_TYPE_POLL && 581 ctrl->tagset->map[HCTX_TYPE_POLL].nr_queues) 582 blk_queue_flag_set(QUEUE_FLAG_POLL, head->disk->queue); 583 584 /* set to a default value of 512 until the disk is validated */ 585 blk_queue_logical_block_size(head->disk->queue, 512); 586 blk_set_stacking_limits(&head->disk->queue->limits); 587 blk_queue_dma_alignment(head->disk->queue, 3); 588 589 /* we need to propagate up the VMC settings */ 590 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT) 591 vwc = true; 592 blk_queue_write_cache(head->disk->queue, vwc, vwc); 593 return 0; 594 } 595 596 static void nvme_mpath_set_live(struct nvme_ns *ns) 597 { 598 struct nvme_ns_head *head = ns->head; 599 int rc; 600 601 if (!head->disk) 602 return; 603 604 /* 605 * test_and_set_bit() is used because it is protecting against two nvme 606 * paths simultaneously calling device_add_disk() on the same namespace 607 * head. 608 */ 609 if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) { 610 rc = device_add_disk(&head->subsys->dev, head->disk, 611 nvme_ns_id_attr_groups); 612 if (rc) { 613 clear_bit(NVME_NSHEAD_DISK_LIVE, &head->flags); 614 return; 615 } 616 nvme_add_ns_head_cdev(head); 617 kblockd_schedule_work(&head->partition_scan_work); 618 } 619 620 mutex_lock(&head->lock); 621 if (nvme_path_is_optimized(ns)) { 622 int node, srcu_idx; 623 624 srcu_idx = srcu_read_lock(&head->srcu); 625 for_each_online_node(node) 626 __nvme_find_path(head, node); 627 srcu_read_unlock(&head->srcu, srcu_idx); 628 } 629 mutex_unlock(&head->lock); 630 631 synchronize_srcu(&head->srcu); 632 kblockd_schedule_work(&head->requeue_work); 633 } 634 635 static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data, 636 int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *, 637 void *)) 638 { 639 void *base = ctrl->ana_log_buf; 640 size_t offset = sizeof(struct nvme_ana_rsp_hdr); 641 int error, i; 642 643 lockdep_assert_held(&ctrl->ana_lock); 644 645 for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) { 646 struct nvme_ana_group_desc *desc = base + offset; 647 u32 nr_nsids; 648 size_t nsid_buf_size; 649 650 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc))) 651 return -EINVAL; 652 653 nr_nsids = le32_to_cpu(desc->nnsids); 654 nsid_buf_size = flex_array_size(desc, nsids, nr_nsids); 655 656 if (WARN_ON_ONCE(desc->grpid == 0)) 657 return -EINVAL; 658 if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax)) 659 return -EINVAL; 660 if (WARN_ON_ONCE(desc->state == 0)) 661 return -EINVAL; 662 if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE)) 663 return -EINVAL; 664 665 offset += sizeof(*desc); 666 if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size)) 667 return -EINVAL; 668 669 error = cb(ctrl, desc, data); 670 if (error) 671 return error; 672 673 offset += nsid_buf_size; 674 } 675 676 return 0; 677 } 678 679 static inline bool nvme_state_is_live(enum nvme_ana_state state) 680 { 681 return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED; 682 } 683 684 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc, 685 struct nvme_ns *ns) 686 { 687 ns->ana_grpid = le32_to_cpu(desc->grpid); 688 ns->ana_state = desc->state; 689 clear_bit(NVME_NS_ANA_PENDING, &ns->flags); 690 /* 691 * nvme_mpath_set_live() will trigger I/O to the multipath path device 692 * and in turn to this path device. However we cannot accept this I/O 693 * if the controller is not live. This may deadlock if called from 694 * nvme_mpath_init_identify() and the ctrl will never complete 695 * initialization, preventing I/O from completing. For this case we 696 * will reprocess the ANA log page in nvme_mpath_update() once the 697 * controller is ready. 698 */ 699 if (nvme_state_is_live(ns->ana_state) && 700 ns->ctrl->state == NVME_CTRL_LIVE) 701 nvme_mpath_set_live(ns); 702 } 703 704 static int nvme_update_ana_state(struct nvme_ctrl *ctrl, 705 struct nvme_ana_group_desc *desc, void *data) 706 { 707 u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0; 708 unsigned *nr_change_groups = data; 709 struct nvme_ns *ns; 710 int srcu_idx; 711 712 dev_dbg(ctrl->device, "ANA group %d: %s.\n", 713 le32_to_cpu(desc->grpid), 714 nvme_ana_state_names[desc->state]); 715 716 if (desc->state == NVME_ANA_CHANGE) 717 (*nr_change_groups)++; 718 719 if (!nr_nsids) 720 return 0; 721 722 srcu_idx = srcu_read_lock(&ctrl->srcu); 723 list_for_each_entry_rcu(ns, &ctrl->namespaces, list) { 724 unsigned nsid; 725 again: 726 nsid = le32_to_cpu(desc->nsids[n]); 727 if (ns->head->ns_id < nsid) 728 continue; 729 if (ns->head->ns_id == nsid) 730 nvme_update_ns_ana_state(desc, ns); 731 if (++n == nr_nsids) 732 break; 733 if (ns->head->ns_id > nsid) 734 goto again; 735 } 736 srcu_read_unlock(&ctrl->srcu, srcu_idx); 737 return 0; 738 } 739 740 static int nvme_read_ana_log(struct nvme_ctrl *ctrl) 741 { 742 u32 nr_change_groups = 0; 743 int error; 744 745 mutex_lock(&ctrl->ana_lock); 746 error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM, 747 ctrl->ana_log_buf, ctrl->ana_log_size, 0); 748 if (error) { 749 dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error); 750 goto out_unlock; 751 } 752 753 error = nvme_parse_ana_log(ctrl, &nr_change_groups, 754 nvme_update_ana_state); 755 if (error) 756 goto out_unlock; 757 758 /* 759 * In theory we should have an ANATT timer per group as they might enter 760 * the change state at different times. But that is a lot of overhead 761 * just to protect against a target that keeps entering new changes 762 * states while never finishing previous ones. But we'll still 763 * eventually time out once all groups are in change state, so this 764 * isn't a big deal. 765 * 766 * We also double the ANATT value to provide some slack for transports 767 * or AEN processing overhead. 768 */ 769 if (nr_change_groups) 770 mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies); 771 else 772 del_timer_sync(&ctrl->anatt_timer); 773 out_unlock: 774 mutex_unlock(&ctrl->ana_lock); 775 return error; 776 } 777 778 static void nvme_ana_work(struct work_struct *work) 779 { 780 struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work); 781 782 if (ctrl->state != NVME_CTRL_LIVE) 783 return; 784 785 nvme_read_ana_log(ctrl); 786 } 787 788 void nvme_mpath_update(struct nvme_ctrl *ctrl) 789 { 790 u32 nr_change_groups = 0; 791 792 if (!ctrl->ana_log_buf) 793 return; 794 795 mutex_lock(&ctrl->ana_lock); 796 nvme_parse_ana_log(ctrl, &nr_change_groups, nvme_update_ana_state); 797 mutex_unlock(&ctrl->ana_lock); 798 } 799 800 static void nvme_anatt_timeout(struct timer_list *t) 801 { 802 struct nvme_ctrl *ctrl = from_timer(ctrl, t, anatt_timer); 803 804 dev_info(ctrl->device, "ANATT timeout, resetting controller.\n"); 805 nvme_reset_ctrl(ctrl); 806 } 807 808 void nvme_mpath_stop(struct nvme_ctrl *ctrl) 809 { 810 if (!nvme_ctrl_use_ana(ctrl)) 811 return; 812 del_timer_sync(&ctrl->anatt_timer); 813 cancel_work_sync(&ctrl->ana_work); 814 } 815 816 #define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \ 817 struct device_attribute subsys_attr_##_name = \ 818 __ATTR(_name, _mode, _show, _store) 819 820 static ssize_t nvme_subsys_iopolicy_show(struct device *dev, 821 struct device_attribute *attr, char *buf) 822 { 823 struct nvme_subsystem *subsys = 824 container_of(dev, struct nvme_subsystem, dev); 825 826 return sysfs_emit(buf, "%s\n", 827 nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]); 828 } 829 830 static ssize_t nvme_subsys_iopolicy_store(struct device *dev, 831 struct device_attribute *attr, const char *buf, size_t count) 832 { 833 struct nvme_subsystem *subsys = 834 container_of(dev, struct nvme_subsystem, dev); 835 int i; 836 837 for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) { 838 if (sysfs_streq(buf, nvme_iopolicy_names[i])) { 839 WRITE_ONCE(subsys->iopolicy, i); 840 return count; 841 } 842 } 843 844 return -EINVAL; 845 } 846 SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR, 847 nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store); 848 849 static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr, 850 char *buf) 851 { 852 return sysfs_emit(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid); 853 } 854 DEVICE_ATTR_RO(ana_grpid); 855 856 static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr, 857 char *buf) 858 { 859 struct nvme_ns *ns = nvme_get_ns_from_dev(dev); 860 861 return sysfs_emit(buf, "%s\n", nvme_ana_state_names[ns->ana_state]); 862 } 863 DEVICE_ATTR_RO(ana_state); 864 865 static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl, 866 struct nvme_ana_group_desc *desc, void *data) 867 { 868 struct nvme_ana_group_desc *dst = data; 869 870 if (desc->grpid != dst->grpid) 871 return 0; 872 873 *dst = *desc; 874 return -ENXIO; /* just break out of the loop */ 875 } 876 877 void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid) 878 { 879 if (nvme_ctrl_use_ana(ns->ctrl)) { 880 struct nvme_ana_group_desc desc = { 881 .grpid = anagrpid, 882 .state = 0, 883 }; 884 885 mutex_lock(&ns->ctrl->ana_lock); 886 ns->ana_grpid = le32_to_cpu(anagrpid); 887 nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc); 888 mutex_unlock(&ns->ctrl->ana_lock); 889 if (desc.state) { 890 /* found the group desc: update */ 891 nvme_update_ns_ana_state(&desc, ns); 892 } else { 893 /* group desc not found: trigger a re-read */ 894 set_bit(NVME_NS_ANA_PENDING, &ns->flags); 895 queue_work(nvme_wq, &ns->ctrl->ana_work); 896 } 897 } else { 898 ns->ana_state = NVME_ANA_OPTIMIZED; 899 nvme_mpath_set_live(ns); 900 } 901 902 if (blk_queue_stable_writes(ns->queue) && ns->head->disk) 903 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, 904 ns->head->disk->queue); 905 #ifdef CONFIG_BLK_DEV_ZONED 906 if (blk_queue_is_zoned(ns->queue) && ns->head->disk) 907 ns->head->disk->nr_zones = ns->disk->nr_zones; 908 #endif 909 } 910 911 void nvme_mpath_shutdown_disk(struct nvme_ns_head *head) 912 { 913 if (!head->disk) 914 return; 915 kblockd_schedule_work(&head->requeue_work); 916 if (test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) { 917 nvme_cdev_del(&head->cdev, &head->cdev_device); 918 /* 919 * requeue I/O after NVME_NSHEAD_DISK_LIVE has been cleared 920 * to allow multipath to fail all I/O. 921 */ 922 synchronize_srcu(&head->srcu); 923 kblockd_schedule_work(&head->requeue_work); 924 del_gendisk(head->disk); 925 } 926 } 927 928 void nvme_mpath_remove_disk(struct nvme_ns_head *head) 929 { 930 if (!head->disk) 931 return; 932 /* make sure all pending bios are cleaned up */ 933 kblockd_schedule_work(&head->requeue_work); 934 flush_work(&head->requeue_work); 935 flush_work(&head->partition_scan_work); 936 put_disk(head->disk); 937 } 938 939 void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl) 940 { 941 mutex_init(&ctrl->ana_lock); 942 timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0); 943 INIT_WORK(&ctrl->ana_work, nvme_ana_work); 944 } 945 946 int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) 947 { 948 size_t max_transfer_size = ctrl->max_hw_sectors << SECTOR_SHIFT; 949 size_t ana_log_size; 950 int error = 0; 951 952 /* check if multipath is enabled and we have the capability */ 953 if (!multipath || !ctrl->subsys || 954 !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA)) 955 return 0; 956 957 if (!ctrl->max_namespaces || 958 ctrl->max_namespaces > le32_to_cpu(id->nn)) { 959 dev_err(ctrl->device, 960 "Invalid MNAN value %u\n", ctrl->max_namespaces); 961 return -EINVAL; 962 } 963 964 ctrl->anacap = id->anacap; 965 ctrl->anatt = id->anatt; 966 ctrl->nanagrpid = le32_to_cpu(id->nanagrpid); 967 ctrl->anagrpmax = le32_to_cpu(id->anagrpmax); 968 969 ana_log_size = sizeof(struct nvme_ana_rsp_hdr) + 970 ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) + 971 ctrl->max_namespaces * sizeof(__le32); 972 if (ana_log_size > max_transfer_size) { 973 dev_err(ctrl->device, 974 "ANA log page size (%zd) larger than MDTS (%zd).\n", 975 ana_log_size, max_transfer_size); 976 dev_err(ctrl->device, "disabling ANA support.\n"); 977 goto out_uninit; 978 } 979 if (ana_log_size > ctrl->ana_log_size) { 980 nvme_mpath_stop(ctrl); 981 nvme_mpath_uninit(ctrl); 982 ctrl->ana_log_buf = kvmalloc(ana_log_size, GFP_KERNEL); 983 if (!ctrl->ana_log_buf) 984 return -ENOMEM; 985 } 986 ctrl->ana_log_size = ana_log_size; 987 error = nvme_read_ana_log(ctrl); 988 if (error) 989 goto out_uninit; 990 return 0; 991 992 out_uninit: 993 nvme_mpath_uninit(ctrl); 994 return error; 995 } 996 997 void nvme_mpath_uninit(struct nvme_ctrl *ctrl) 998 { 999 kvfree(ctrl->ana_log_buf); 1000 ctrl->ana_log_buf = NULL; 1001 ctrl->ana_log_size = 0; 1002 } 1003