1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2017-2018 Christoph Hellwig.
6 #include <linux/moduleparam.h>
7 #include <trace/events/block.h>
10 static bool multipath
= true;
11 module_param(multipath
, bool, 0444);
12 MODULE_PARM_DESC(multipath
,
13 "turn on native support for multiple controllers per subsystem");
15 void nvme_mpath_unfreeze(struct nvme_subsystem
*subsys
)
17 struct nvme_ns_head
*h
;
19 lockdep_assert_held(&subsys
->lock
);
20 list_for_each_entry(h
, &subsys
->nsheads
, entry
)
22 blk_mq_unfreeze_queue(h
->disk
->queue
);
25 void nvme_mpath_wait_freeze(struct nvme_subsystem
*subsys
)
27 struct nvme_ns_head
*h
;
29 lockdep_assert_held(&subsys
->lock
);
30 list_for_each_entry(h
, &subsys
->nsheads
, entry
)
32 blk_mq_freeze_queue_wait(h
->disk
->queue
);
35 void nvme_mpath_start_freeze(struct nvme_subsystem
*subsys
)
37 struct nvme_ns_head
*h
;
39 lockdep_assert_held(&subsys
->lock
);
40 list_for_each_entry(h
, &subsys
->nsheads
, entry
)
42 blk_freeze_queue_start(h
->disk
->queue
);
46 * If multipathing is enabled we need to always use the subsystem instance
47 * number for numbering our devices to avoid conflicts between subsystems that
48 * have multiple controllers and thus use the multipath-aware subsystem node
49 * and those that have a single controller and use the controller node
52 void nvme_set_disk_name(char *disk_name
, struct nvme_ns
*ns
,
53 struct nvme_ctrl
*ctrl
, int *flags
)
56 sprintf(disk_name
, "nvme%dn%d", ctrl
->instance
, ns
->head
->instance
);
57 } else if (ns
->head
->disk
) {
58 sprintf(disk_name
, "nvme%dc%dn%d", ctrl
->subsys
->instance
,
59 ctrl
->instance
, ns
->head
->instance
);
60 *flags
= GENHD_FL_HIDDEN
;
62 sprintf(disk_name
, "nvme%dn%d", ctrl
->subsys
->instance
,
67 bool nvme_failover_req(struct request
*req
)
69 struct nvme_ns
*ns
= req
->q
->queuedata
;
70 u16 status
= nvme_req(req
)->status
;
73 switch (status
& 0x7ff) {
74 case NVME_SC_ANA_TRANSITION
:
75 case NVME_SC_ANA_INACCESSIBLE
:
76 case NVME_SC_ANA_PERSISTENT_LOSS
:
78 * If we got back an ANA error we know the controller is alive,
79 * but not ready to serve this namespaces. The spec suggests
80 * we should update our general state here, but due to the fact
81 * that the admin and I/O queues are not serialized that is
82 * fundamentally racy. So instead just clear the current path,
83 * mark the the path as pending and kick of a re-read of the ANA
86 nvme_mpath_clear_current_path(ns
);
87 if (ns
->ctrl
->ana_log_buf
) {
88 set_bit(NVME_NS_ANA_PENDING
, &ns
->flags
);
89 queue_work(nvme_wq
, &ns
->ctrl
->ana_work
);
92 case NVME_SC_HOST_PATH_ERROR
:
93 case NVME_SC_HOST_ABORTED_CMD
:
95 * Temporary transport disruption in talking to the controller.
96 * Try to send on a new path.
98 nvme_mpath_clear_current_path(ns
);
101 /* This was a non-ANA error so follow the normal error path. */
105 spin_lock_irqsave(&ns
->head
->requeue_lock
, flags
);
106 blk_steal_bios(&ns
->head
->requeue_list
, req
);
107 spin_unlock_irqrestore(&ns
->head
->requeue_lock
, flags
);
108 blk_mq_end_request(req
, 0);
110 kblockd_schedule_work(&ns
->head
->requeue_work
);
114 void nvme_kick_requeue_lists(struct nvme_ctrl
*ctrl
)
118 down_read(&ctrl
->namespaces_rwsem
);
119 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
121 kblockd_schedule_work(&ns
->head
->requeue_work
);
123 up_read(&ctrl
->namespaces_rwsem
);
126 static const char *nvme_ana_state_names
[] = {
127 [0] = "invalid state",
128 [NVME_ANA_OPTIMIZED
] = "optimized",
129 [NVME_ANA_NONOPTIMIZED
] = "non-optimized",
130 [NVME_ANA_INACCESSIBLE
] = "inaccessible",
131 [NVME_ANA_PERSISTENT_LOSS
] = "persistent-loss",
132 [NVME_ANA_CHANGE
] = "change",
135 bool nvme_mpath_clear_current_path(struct nvme_ns
*ns
)
137 struct nvme_ns_head
*head
= ns
->head
;
138 bool changed
= false;
144 for_each_node(node
) {
145 if (ns
== rcu_access_pointer(head
->current_path
[node
])) {
146 rcu_assign_pointer(head
->current_path
[node
], NULL
);
154 void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl
*ctrl
)
158 mutex_lock(&ctrl
->scan_lock
);
159 down_read(&ctrl
->namespaces_rwsem
);
160 list_for_each_entry(ns
, &ctrl
->namespaces
, list
)
161 if (nvme_mpath_clear_current_path(ns
))
162 kblockd_schedule_work(&ns
->head
->requeue_work
);
163 up_read(&ctrl
->namespaces_rwsem
);
164 mutex_unlock(&ctrl
->scan_lock
);
167 static bool nvme_path_is_disabled(struct nvme_ns
*ns
)
169 return ns
->ctrl
->state
!= NVME_CTRL_LIVE
||
170 test_bit(NVME_NS_ANA_PENDING
, &ns
->flags
) ||
171 test_bit(NVME_NS_REMOVING
, &ns
->flags
);
174 static struct nvme_ns
*__nvme_find_path(struct nvme_ns_head
*head
, int node
)
176 int found_distance
= INT_MAX
, fallback_distance
= INT_MAX
, distance
;
177 struct nvme_ns
*found
= NULL
, *fallback
= NULL
, *ns
;
179 list_for_each_entry_rcu(ns
, &head
->list
, siblings
) {
180 if (nvme_path_is_disabled(ns
))
183 if (READ_ONCE(head
->subsys
->iopolicy
) == NVME_IOPOLICY_NUMA
)
184 distance
= node_distance(node
, ns
->ctrl
->numa_node
);
186 distance
= LOCAL_DISTANCE
;
188 switch (ns
->ana_state
) {
189 case NVME_ANA_OPTIMIZED
:
190 if (distance
< found_distance
) {
191 found_distance
= distance
;
195 case NVME_ANA_NONOPTIMIZED
:
196 if (distance
< fallback_distance
) {
197 fallback_distance
= distance
;
209 rcu_assign_pointer(head
->current_path
[node
], found
);
213 static struct nvme_ns
*nvme_next_ns(struct nvme_ns_head
*head
,
216 ns
= list_next_or_null_rcu(&head
->list
, &ns
->siblings
, struct nvme_ns
,
220 return list_first_or_null_rcu(&head
->list
, struct nvme_ns
, siblings
);
223 static struct nvme_ns
*nvme_round_robin_path(struct nvme_ns_head
*head
,
224 int node
, struct nvme_ns
*old
)
226 struct nvme_ns
*ns
, *found
, *fallback
= NULL
;
228 if (list_is_singular(&head
->list
)) {
229 if (nvme_path_is_disabled(old
))
234 for (ns
= nvme_next_ns(head
, old
);
236 ns
= nvme_next_ns(head
, ns
)) {
237 if (nvme_path_is_disabled(ns
))
240 if (ns
->ana_state
== NVME_ANA_OPTIMIZED
) {
244 if (ns
->ana_state
== NVME_ANA_NONOPTIMIZED
)
252 rcu_assign_pointer(head
->current_path
[node
], found
);
256 static inline bool nvme_path_is_optimized(struct nvme_ns
*ns
)
258 return ns
->ctrl
->state
== NVME_CTRL_LIVE
&&
259 ns
->ana_state
== NVME_ANA_OPTIMIZED
;
262 inline struct nvme_ns
*nvme_find_path(struct nvme_ns_head
*head
)
264 int node
= numa_node_id();
267 ns
= srcu_dereference(head
->current_path
[node
], &head
->srcu
);
268 if (READ_ONCE(head
->subsys
->iopolicy
) == NVME_IOPOLICY_RR
&& ns
)
269 ns
= nvme_round_robin_path(head
, node
, ns
);
270 if (unlikely(!ns
|| !nvme_path_is_optimized(ns
)))
271 ns
= __nvme_find_path(head
, node
);
275 static bool nvme_available_path(struct nvme_ns_head
*head
)
279 list_for_each_entry_rcu(ns
, &head
->list
, siblings
) {
280 switch (ns
->ctrl
->state
) {
282 case NVME_CTRL_RESETTING
:
283 case NVME_CTRL_CONNECTING
:
293 static blk_qc_t
nvme_ns_head_make_request(struct request_queue
*q
,
296 struct nvme_ns_head
*head
= q
->queuedata
;
297 struct device
*dev
= disk_to_dev(head
->disk
);
299 blk_qc_t ret
= BLK_QC_T_NONE
;
303 * The namespace might be going away and the bio might
304 * be moved to a different queue via blk_steal_bios(),
305 * so we need to use the bio_split pool from the original
306 * queue to allocate the bvecs from.
308 blk_queue_split(q
, &bio
);
310 srcu_idx
= srcu_read_lock(&head
->srcu
);
311 ns
= nvme_find_path(head
);
313 bio
->bi_disk
= ns
->disk
;
314 bio
->bi_opf
|= REQ_NVME_MPATH
;
315 trace_block_bio_remap(bio
->bi_disk
->queue
, bio
,
316 disk_devt(ns
->head
->disk
),
317 bio
->bi_iter
.bi_sector
);
318 ret
= direct_make_request(bio
);
319 } else if (nvme_available_path(head
)) {
320 dev_warn_ratelimited(dev
, "no usable path - requeuing I/O\n");
322 spin_lock_irq(&head
->requeue_lock
);
323 bio_list_add(&head
->requeue_list
, bio
);
324 spin_unlock_irq(&head
->requeue_lock
);
326 dev_warn_ratelimited(dev
, "no available path - failing I/O\n");
328 bio
->bi_status
= BLK_STS_IOERR
;
332 srcu_read_unlock(&head
->srcu
, srcu_idx
);
336 static void nvme_requeue_work(struct work_struct
*work
)
338 struct nvme_ns_head
*head
=
339 container_of(work
, struct nvme_ns_head
, requeue_work
);
340 struct bio
*bio
, *next
;
342 spin_lock_irq(&head
->requeue_lock
);
343 next
= bio_list_get(&head
->requeue_list
);
344 spin_unlock_irq(&head
->requeue_lock
);
346 while ((bio
= next
) != NULL
) {
351 * Reset disk to the mpath node and resubmit to select a new
354 bio
->bi_disk
= head
->disk
;
355 generic_make_request(bio
);
359 int nvme_mpath_alloc_disk(struct nvme_ctrl
*ctrl
, struct nvme_ns_head
*head
)
361 struct request_queue
*q
;
364 mutex_init(&head
->lock
);
365 bio_list_init(&head
->requeue_list
);
366 spin_lock_init(&head
->requeue_lock
);
367 INIT_WORK(&head
->requeue_work
, nvme_requeue_work
);
370 * Add a multipath node if the subsystems supports multiple controllers.
371 * We also do this for private namespaces as the namespace sharing data could
372 * change after a rescan.
374 if (!(ctrl
->subsys
->cmic
& (1 << 1)) || !multipath
)
377 q
= blk_alloc_queue(nvme_ns_head_make_request
, ctrl
->numa_node
);
381 blk_queue_flag_set(QUEUE_FLAG_NONROT
, q
);
382 /* set to a default value for 512 until disk is validated */
383 blk_queue_logical_block_size(q
, 512);
384 blk_set_stacking_limits(&q
->limits
);
386 /* we need to propagate up the VMC settings */
387 if (ctrl
->vwc
& NVME_CTRL_VWC_PRESENT
)
389 blk_queue_write_cache(q
, vwc
, vwc
);
391 head
->disk
= alloc_disk(0);
393 goto out_cleanup_queue
;
394 head
->disk
->fops
= &nvme_ns_head_ops
;
395 head
->disk
->private_data
= head
;
396 head
->disk
->queue
= q
;
397 head
->disk
->flags
= GENHD_FL_EXT_DEVT
;
398 sprintf(head
->disk
->disk_name
, "nvme%dn%d",
399 ctrl
->subsys
->instance
, head
->instance
);
403 blk_cleanup_queue(q
);
408 static void nvme_mpath_set_live(struct nvme_ns
*ns
)
410 struct nvme_ns_head
*head
= ns
->head
;
415 mutex_lock(&head
->lock
);
416 if (!(head
->disk
->flags
& GENHD_FL_UP
))
417 device_add_disk(&head
->subsys
->dev
, head
->disk
,
418 nvme_ns_id_attr_groups
);
420 if (nvme_path_is_optimized(ns
)) {
423 srcu_idx
= srcu_read_lock(&head
->srcu
);
425 __nvme_find_path(head
, node
);
426 srcu_read_unlock(&head
->srcu
, srcu_idx
);
428 mutex_unlock(&head
->lock
);
430 synchronize_srcu(&head
->srcu
);
431 kblockd_schedule_work(&head
->requeue_work
);
434 static int nvme_parse_ana_log(struct nvme_ctrl
*ctrl
, void *data
,
435 int (*cb
)(struct nvme_ctrl
*ctrl
, struct nvme_ana_group_desc
*,
438 void *base
= ctrl
->ana_log_buf
;
439 size_t offset
= sizeof(struct nvme_ana_rsp_hdr
);
442 lockdep_assert_held(&ctrl
->ana_lock
);
444 for (i
= 0; i
< le16_to_cpu(ctrl
->ana_log_buf
->ngrps
); i
++) {
445 struct nvme_ana_group_desc
*desc
= base
+ offset
;
447 size_t nsid_buf_size
;
449 if (WARN_ON_ONCE(offset
> ctrl
->ana_log_size
- sizeof(*desc
)))
452 nr_nsids
= le32_to_cpu(desc
->nnsids
);
453 nsid_buf_size
= nr_nsids
* sizeof(__le32
);
455 if (WARN_ON_ONCE(desc
->grpid
== 0))
457 if (WARN_ON_ONCE(le32_to_cpu(desc
->grpid
) > ctrl
->anagrpmax
))
459 if (WARN_ON_ONCE(desc
->state
== 0))
461 if (WARN_ON_ONCE(desc
->state
> NVME_ANA_CHANGE
))
464 offset
+= sizeof(*desc
);
465 if (WARN_ON_ONCE(offset
> ctrl
->ana_log_size
- nsid_buf_size
))
468 error
= cb(ctrl
, desc
, data
);
472 offset
+= nsid_buf_size
;
478 static inline bool nvme_state_is_live(enum nvme_ana_state state
)
480 return state
== NVME_ANA_OPTIMIZED
|| state
== NVME_ANA_NONOPTIMIZED
;
483 static void nvme_update_ns_ana_state(struct nvme_ana_group_desc
*desc
,
486 ns
->ana_grpid
= le32_to_cpu(desc
->grpid
);
487 ns
->ana_state
= desc
->state
;
488 clear_bit(NVME_NS_ANA_PENDING
, &ns
->flags
);
490 if (nvme_state_is_live(ns
->ana_state
))
491 nvme_mpath_set_live(ns
);
494 static int nvme_update_ana_state(struct nvme_ctrl
*ctrl
,
495 struct nvme_ana_group_desc
*desc
, void *data
)
497 u32 nr_nsids
= le32_to_cpu(desc
->nnsids
), n
= 0;
498 unsigned *nr_change_groups
= data
;
501 dev_dbg(ctrl
->device
, "ANA group %d: %s.\n",
502 le32_to_cpu(desc
->grpid
),
503 nvme_ana_state_names
[desc
->state
]);
505 if (desc
->state
== NVME_ANA_CHANGE
)
506 (*nr_change_groups
)++;
511 down_read(&ctrl
->namespaces_rwsem
);
512 list_for_each_entry(ns
, &ctrl
->namespaces
, list
) {
513 unsigned nsid
= le32_to_cpu(desc
->nsids
[n
]);
515 if (ns
->head
->ns_id
< nsid
)
517 if (ns
->head
->ns_id
== nsid
)
518 nvme_update_ns_ana_state(desc
, ns
);
522 up_read(&ctrl
->namespaces_rwsem
);
526 static int nvme_read_ana_log(struct nvme_ctrl
*ctrl
)
528 u32 nr_change_groups
= 0;
531 mutex_lock(&ctrl
->ana_lock
);
532 error
= nvme_get_log(ctrl
, NVME_NSID_ALL
, NVME_LOG_ANA
, 0,
533 ctrl
->ana_log_buf
, ctrl
->ana_log_size
, 0);
535 dev_warn(ctrl
->device
, "Failed to get ANA log: %d\n", error
);
539 error
= nvme_parse_ana_log(ctrl
, &nr_change_groups
,
540 nvme_update_ana_state
);
545 * In theory we should have an ANATT timer per group as they might enter
546 * the change state at different times. But that is a lot of overhead
547 * just to protect against a target that keeps entering new changes
548 * states while never finishing previous ones. But we'll still
549 * eventually time out once all groups are in change state, so this
552 * We also double the ANATT value to provide some slack for transports
553 * or AEN processing overhead.
555 if (nr_change_groups
)
556 mod_timer(&ctrl
->anatt_timer
, ctrl
->anatt
* HZ
* 2 + jiffies
);
558 del_timer_sync(&ctrl
->anatt_timer
);
560 mutex_unlock(&ctrl
->ana_lock
);
564 static void nvme_ana_work(struct work_struct
*work
)
566 struct nvme_ctrl
*ctrl
= container_of(work
, struct nvme_ctrl
, ana_work
);
568 nvme_read_ana_log(ctrl
);
571 static void nvme_anatt_timeout(struct timer_list
*t
)
573 struct nvme_ctrl
*ctrl
= from_timer(ctrl
, t
, anatt_timer
);
575 dev_info(ctrl
->device
, "ANATT timeout, resetting controller.\n");
576 nvme_reset_ctrl(ctrl
);
579 void nvme_mpath_stop(struct nvme_ctrl
*ctrl
)
581 if (!nvme_ctrl_use_ana(ctrl
))
583 del_timer_sync(&ctrl
->anatt_timer
);
584 cancel_work_sync(&ctrl
->ana_work
);
587 #define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \
588 struct device_attribute subsys_attr_##_name = \
589 __ATTR(_name, _mode, _show, _store)
591 static const char *nvme_iopolicy_names
[] = {
592 [NVME_IOPOLICY_NUMA
] = "numa",
593 [NVME_IOPOLICY_RR
] = "round-robin",
596 static ssize_t
nvme_subsys_iopolicy_show(struct device
*dev
,
597 struct device_attribute
*attr
, char *buf
)
599 struct nvme_subsystem
*subsys
=
600 container_of(dev
, struct nvme_subsystem
, dev
);
602 return sprintf(buf
, "%s\n",
603 nvme_iopolicy_names
[READ_ONCE(subsys
->iopolicy
)]);
606 static ssize_t
nvme_subsys_iopolicy_store(struct device
*dev
,
607 struct device_attribute
*attr
, const char *buf
, size_t count
)
609 struct nvme_subsystem
*subsys
=
610 container_of(dev
, struct nvme_subsystem
, dev
);
613 for (i
= 0; i
< ARRAY_SIZE(nvme_iopolicy_names
); i
++) {
614 if (sysfs_streq(buf
, nvme_iopolicy_names
[i
])) {
615 WRITE_ONCE(subsys
->iopolicy
, i
);
622 SUBSYS_ATTR_RW(iopolicy
, S_IRUGO
| S_IWUSR
,
623 nvme_subsys_iopolicy_show
, nvme_subsys_iopolicy_store
);
625 static ssize_t
ana_grpid_show(struct device
*dev
, struct device_attribute
*attr
,
628 return sprintf(buf
, "%d\n", nvme_get_ns_from_dev(dev
)->ana_grpid
);
630 DEVICE_ATTR_RO(ana_grpid
);
632 static ssize_t
ana_state_show(struct device
*dev
, struct device_attribute
*attr
,
635 struct nvme_ns
*ns
= nvme_get_ns_from_dev(dev
);
637 return sprintf(buf
, "%s\n", nvme_ana_state_names
[ns
->ana_state
]);
639 DEVICE_ATTR_RO(ana_state
);
641 static int nvme_set_ns_ana_state(struct nvme_ctrl
*ctrl
,
642 struct nvme_ana_group_desc
*desc
, void *data
)
644 struct nvme_ns
*ns
= data
;
646 if (ns
->ana_grpid
== le32_to_cpu(desc
->grpid
)) {
647 nvme_update_ns_ana_state(desc
, ns
);
648 return -ENXIO
; /* just break out of the loop */
654 void nvme_mpath_add_disk(struct nvme_ns
*ns
, struct nvme_id_ns
*id
)
656 if (nvme_ctrl_use_ana(ns
->ctrl
)) {
657 mutex_lock(&ns
->ctrl
->ana_lock
);
658 ns
->ana_grpid
= le32_to_cpu(id
->anagrpid
);
659 nvme_parse_ana_log(ns
->ctrl
, ns
, nvme_set_ns_ana_state
);
660 mutex_unlock(&ns
->ctrl
->ana_lock
);
662 ns
->ana_state
= NVME_ANA_OPTIMIZED
;
663 nvme_mpath_set_live(ns
);
667 void nvme_mpath_remove_disk(struct nvme_ns_head
*head
)
671 if (head
->disk
->flags
& GENHD_FL_UP
)
672 del_gendisk(head
->disk
);
673 blk_set_queue_dying(head
->disk
->queue
);
674 /* make sure all pending bios are cleaned up */
675 kblockd_schedule_work(&head
->requeue_work
);
676 flush_work(&head
->requeue_work
);
677 blk_cleanup_queue(head
->disk
->queue
);
678 put_disk(head
->disk
);
681 int nvme_mpath_init(struct nvme_ctrl
*ctrl
, struct nvme_id_ctrl
*id
)
685 /* check if multipath is enabled and we have the capability */
686 if (!multipath
|| !ctrl
->subsys
|| !(ctrl
->subsys
->cmic
& (1 << 3)))
689 ctrl
->anacap
= id
->anacap
;
690 ctrl
->anatt
= id
->anatt
;
691 ctrl
->nanagrpid
= le32_to_cpu(id
->nanagrpid
);
692 ctrl
->anagrpmax
= le32_to_cpu(id
->anagrpmax
);
694 mutex_init(&ctrl
->ana_lock
);
695 timer_setup(&ctrl
->anatt_timer
, nvme_anatt_timeout
, 0);
696 ctrl
->ana_log_size
= sizeof(struct nvme_ana_rsp_hdr
) +
697 ctrl
->nanagrpid
* sizeof(struct nvme_ana_group_desc
);
698 ctrl
->ana_log_size
+= ctrl
->max_namespaces
* sizeof(__le32
);
700 if (ctrl
->ana_log_size
> ctrl
->max_hw_sectors
<< SECTOR_SHIFT
) {
701 dev_err(ctrl
->device
,
702 "ANA log page size (%zd) larger than MDTS (%d).\n",
704 ctrl
->max_hw_sectors
<< SECTOR_SHIFT
);
705 dev_err(ctrl
->device
, "disabling ANA support.\n");
709 INIT_WORK(&ctrl
->ana_work
, nvme_ana_work
);
710 kfree(ctrl
->ana_log_buf
);
711 ctrl
->ana_log_buf
= kmalloc(ctrl
->ana_log_size
, GFP_KERNEL
);
712 if (!ctrl
->ana_log_buf
) {
717 error
= nvme_read_ana_log(ctrl
);
719 goto out_free_ana_log_buf
;
721 out_free_ana_log_buf
:
722 kfree(ctrl
->ana_log_buf
);
723 ctrl
->ana_log_buf
= NULL
;
728 void nvme_mpath_uninit(struct nvme_ctrl
*ctrl
)
730 kfree(ctrl
->ana_log_buf
);
731 ctrl
->ana_log_buf
= NULL
;