Skip to content

Commit 6d3c7fb

Browse files
committed
nvme: use ctrl state accessor
The ctrl->state value is updated in another thread using WRITE_ONCE, so ensure all the readers use the appropriate accessor. Reviewed-by: Sagi Grimberg <sagi@grmberg.me> Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com> Signed-off-by: Keith Busch <kbusch@kernel.org>
1 parent 47c5dd6 commit 6d3c7fb

8 files changed

Lines changed: 37 additions & 30 deletions

File tree

drivers/nvme/host/apple.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ static int apple_nvme_init_request(struct blk_mq_tag_set *set,
797797

798798
static void apple_nvme_disable(struct apple_nvme *anv, bool shutdown)
799799
{
800+
enum nvme_ctrl_state state = nvme_ctrl_state(&anv->ctrl);
800801
u32 csts = readl(anv->mmio_nvme + NVME_REG_CSTS);
801802
bool dead = false, freeze = false;
802803
unsigned long flags;
@@ -808,8 +809,8 @@ static void apple_nvme_disable(struct apple_nvme *anv, bool shutdown)
808809
if (csts & NVME_CSTS_CFS)
809810
dead = true;
810811

811-
if (anv->ctrl.state == NVME_CTRL_LIVE ||
812-
anv->ctrl.state == NVME_CTRL_RESETTING) {
812+
if (state == NVME_CTRL_LIVE ||
813+
state == NVME_CTRL_RESETTING) {
813814
freeze = true;
814815
nvme_start_freeze(&anv->ctrl);
815816
}
@@ -881,7 +882,7 @@ static enum blk_eh_timer_return apple_nvme_timeout(struct request *req)
881882
unsigned long flags;
882883
u32 csts = readl(anv->mmio_nvme + NVME_REG_CSTS);
883884

884-
if (anv->ctrl.state != NVME_CTRL_LIVE) {
885+
if (nvme_ctrl_state(&anv->ctrl) != NVME_CTRL_LIVE) {
885886
/*
886887
* From rdma.c:
887888
* If we are resetting, connecting or deleting we should
@@ -985,10 +986,10 @@ static void apple_nvme_reset_work(struct work_struct *work)
985986
u32 boot_status, aqa;
986987
struct apple_nvme *anv =
987988
container_of(work, struct apple_nvme, ctrl.reset_work);
989+
enum nvme_ctrl_state state = nvme_ctrl_state(&anv->ctrl);
988990

989-
if (anv->ctrl.state != NVME_CTRL_RESETTING) {
990-
dev_warn(anv->dev, "ctrl state %d is not RESETTING\n",
991-
anv->ctrl.state);
991+
if (state != NVME_CTRL_RESETTING) {
992+
dev_warn(anv->dev, "ctrl state %d is not RESETTING\n", state);
992993
ret = -ENODEV;
993994
goto out;
994995
}

drivers/nvme/host/auth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ static void nvme_ctrl_auth_work(struct work_struct *work)
897897
* If the ctrl is no connected, bail as reconnect will handle
898898
* authentication.
899899
*/
900-
if (ctrl->state != NVME_CTRL_LIVE)
900+
if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE)
901901
return;
902902

903903
/* Authenticate admin queue first */

drivers/nvme/host/core.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ blk_status_t nvme_fail_nonready_command(struct nvme_ctrl *ctrl,
721721
EXPORT_SYMBOL_GPL(nvme_fail_nonready_command);
722722

723723
bool __nvme_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
724-
bool queue_live)
724+
bool queue_live, enum nvme_ctrl_state state)
725725
{
726726
struct nvme_request *req = nvme_req(rq);
727727

@@ -742,7 +742,7 @@ bool __nvme_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
742742
* command, which is require to set the queue live in the
743743
* appropinquate states.
744744
*/
745-
switch (nvme_ctrl_state(ctrl)) {
745+
switch (state) {
746746
case NVME_CTRL_CONNECTING:
747747
if (blk_rq_is_passthrough(rq) && nvme_is_fabrics(req->cmd) &&
748748
(req->cmd->fabrics.fctype == nvme_fabrics_type_connect ||

drivers/nvme/host/fabrics.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,11 @@ static inline bool
185185
nvmf_ctlr_matches_baseopts(struct nvme_ctrl *ctrl,
186186
struct nvmf_ctrl_options *opts)
187187
{
188-
if (ctrl->state == NVME_CTRL_DELETING ||
189-
ctrl->state == NVME_CTRL_DELETING_NOIO ||
190-
ctrl->state == NVME_CTRL_DEAD ||
188+
enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
189+
190+
if (state == NVME_CTRL_DELETING ||
191+
state == NVME_CTRL_DELETING_NOIO ||
192+
state == NVME_CTRL_DEAD ||
191193
strcmp(opts->subsysnqn, ctrl->opts->subsysnqn) ||
192194
strcmp(opts->host->nqn, ctrl->opts->host->nqn) ||
193195
!uuid_equal(&opts->host->id, &ctrl->opts->host->id))

drivers/nvme/host/multipath.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
156156
if (!ns->head->disk)
157157
continue;
158158
kblockd_schedule_work(&ns->head->requeue_work);
159-
if (ctrl->state == NVME_CTRL_LIVE)
159+
if (nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE)
160160
disk_uevent(ns->head->disk, KOBJ_CHANGE);
161161
}
162162
up_read(&ctrl->namespaces_rwsem);
@@ -223,13 +223,14 @@ void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
223223

224224
static bool nvme_path_is_disabled(struct nvme_ns *ns)
225225
{
226+
enum nvme_ctrl_state state = nvme_ctrl_state(ns->ctrl);
227+
226228
/*
227229
* We don't treat NVME_CTRL_DELETING as a disabled path as I/O should
228230
* still be able to complete assuming that the controller is connected.
229231
* Otherwise it will fail immediately and return to the requeue list.
230232
*/
231-
if (ns->ctrl->state != NVME_CTRL_LIVE &&
232-
ns->ctrl->state != NVME_CTRL_DELETING)
233+
if (state != NVME_CTRL_LIVE && state != NVME_CTRL_DELETING)
233234
return true;
234235
if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
235236
!test_bit(NVME_NS_READY, &ns->flags))
@@ -331,7 +332,7 @@ static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head,
331332

332333
static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
333334
{
334-
return ns->ctrl->state == NVME_CTRL_LIVE &&
335+
return nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE &&
335336
ns->ana_state == NVME_ANA_OPTIMIZED;
336337
}
337338

@@ -358,7 +359,7 @@ static bool nvme_available_path(struct nvme_ns_head *head)
358359
list_for_each_entry_rcu(ns, &head->list, siblings) {
359360
if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags))
360361
continue;
361-
switch (ns->ctrl->state) {
362+
switch (nvme_ctrl_state(ns->ctrl)) {
362363
case NVME_CTRL_LIVE:
363364
case NVME_CTRL_RESETTING:
364365
case NVME_CTRL_CONNECTING:
@@ -667,7 +668,7 @@ static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
667668
* controller is ready.
668669
*/
669670
if (nvme_state_is_live(ns->ana_state) &&
670-
ns->ctrl->state == NVME_CTRL_LIVE)
671+
nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE)
671672
nvme_mpath_set_live(ns);
672673
}
673674

@@ -748,7 +749,7 @@ static void nvme_ana_work(struct work_struct *work)
748749
{
749750
struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
750751

751-
if (ctrl->state != NVME_CTRL_LIVE)
752+
if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE)
752753
return;
753754

754755
nvme_read_ana_log(ctrl);

drivers/nvme/host/nvme.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -805,17 +805,18 @@ blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req);
805805
blk_status_t nvme_fail_nonready_command(struct nvme_ctrl *ctrl,
806806
struct request *req);
807807
bool __nvme_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
808-
bool queue_live);
808+
bool queue_live, enum nvme_ctrl_state state);
809809

810810
static inline bool nvme_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
811811
bool queue_live)
812812
{
813-
if (likely(ctrl->state == NVME_CTRL_LIVE))
813+
enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
814+
815+
if (likely(state == NVME_CTRL_LIVE))
814816
return true;
815-
if (ctrl->ops->flags & NVME_F_FABRICS &&
816-
ctrl->state == NVME_CTRL_DELETING)
817+
if (ctrl->ops->flags & NVME_F_FABRICS && state == NVME_CTRL_DELETING)
817818
return queue_live;
818-
return __nvme_check_ready(ctrl, rq, queue_live);
819+
return __nvme_check_ready(ctrl, rq, queue_live, state);
819820
}
820821

821822
/*

drivers/nvme/host/sysfs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ static ssize_t nvme_sysfs_show_state(struct device *dev,
311311
char *buf)
312312
{
313313
struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
314+
unsigned state = (unsigned)nvme_ctrl_state(ctrl);
314315
static const char *const state_name[] = {
315316
[NVME_CTRL_NEW] = "new",
316317
[NVME_CTRL_LIVE] = "live",
@@ -321,9 +322,8 @@ static ssize_t nvme_sysfs_show_state(struct device *dev,
321322
[NVME_CTRL_DEAD] = "dead",
322323
};
323324

324-
if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
325-
state_name[ctrl->state])
326-
return sysfs_emit(buf, "%s\n", state_name[ctrl->state]);
325+
if (state < ARRAY_SIZE(state_name) && state_name[state])
326+
return sysfs_emit(buf, "%s\n", state_name[state]);
327327

328328
return sysfs_emit(buf, "unknown state\n");
329329
}

drivers/nvme/target/loop.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl)
400400
}
401401

402402
nvme_quiesce_admin_queue(&ctrl->ctrl);
403-
if (ctrl->ctrl.state == NVME_CTRL_LIVE)
403+
if (nvme_ctrl_state(&ctrl->ctrl) == NVME_CTRL_LIVE)
404404
nvme_disable_ctrl(&ctrl->ctrl, true);
405405

406406
nvme_cancel_admin_tagset(&ctrl->ctrl);
@@ -434,8 +434,10 @@ static void nvme_loop_reset_ctrl_work(struct work_struct *work)
434434
nvme_loop_shutdown_ctrl(ctrl);
435435

436436
if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
437-
if (ctrl->ctrl.state != NVME_CTRL_DELETING &&
438-
ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO)
437+
enum nvme_ctrl_state state = nvme_ctrl_state(&ctrl->ctrl);
438+
439+
if (state != NVME_CTRL_DELETING &&
440+
state != NVME_CTRL_DELETING_NOIO)
439441
/* state change failure for non-deleted ctrl? */
440442
WARN_ON_ONCE(1);
441443
return;

0 commit comments

Comments
 (0)