All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvme: Prevent controller state invalid transition
@ 2016-07-29 19:15 Gabriel Krisman Bertazi
  2016-08-01 11:13 ` Sagi Grimberg
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Gabriel Krisman Bertazi @ 2016-07-29 19:15 UTC (permalink / raw)


Acquiring the nvme_ctrl lock before reading ctrl->state in
nvme_change_ctrl_state() should prevent a theoretical invalid state
transition, in the event of two threads racing inside that function.

I haven't been able to observe this happening with the current code, and
the current state machine seems to be simple enough to not be
affected by these invalid transitions, but future modifications could
make it more likely to happen.

Signed-off-by: Gabriel Krisman Bertazi <krisman at linux.vnet.ibm.com>
---
 drivers/nvme/host/core.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index 7ff2e82..7f75d66 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -81,10 +81,12 @@ EXPORT_SYMBOL_GPL(nvme_cancel_request);
 bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
 		enum nvme_ctrl_state new_state)
 {
-	enum nvme_ctrl_state old_state = ctrl->state;
+	enum nvme_ctrl_state old_state;
 	bool changed = false;
 
 	spin_lock_irq(&ctrl->lock);
+
+	old_state = ctrl->state;
 	switch (new_state) {
 	case NVME_CTRL_LIVE:
 		switch (old_state) {
@@ -140,11 +142,12 @@ bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
 	default:
 		break;
 	}
-	spin_unlock_irq(&ctrl->lock);
 
 	if (changed)
 		ctrl->state = new_state;
 
+	spin_unlock_irq(&ctrl->lock);
+
 	return changed;
 }
 EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] nvme: Prevent controller state invalid transition
  2016-07-29 19:15 [PATCH] nvme: Prevent controller state invalid transition Gabriel Krisman Bertazi
@ 2016-08-01 11:13 ` Sagi Grimberg
  2016-08-15 15:17 ` Steve Wise
  2016-08-15 15:47 ` Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Sagi Grimberg @ 2016-08-01 11:13 UTC (permalink / raw)


Looks fine to me,

Reviewed-by: Sagi Grimberg <sag at grimberg.me>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] nvme: Prevent controller state invalid transition
  2016-07-29 19:15 [PATCH] nvme: Prevent controller state invalid transition Gabriel Krisman Bertazi
  2016-08-01 11:13 ` Sagi Grimberg
@ 2016-08-15 15:17 ` Steve Wise
  2016-08-15 15:47 ` Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Steve Wise @ 2016-08-15 15:17 UTC (permalink / raw)


> 
> Acquiring the nvme_ctrl lock before reading ctrl->state in
> nvme_change_ctrl_state() should prevent a theoretical invalid state
> transition, in the event of two threads racing inside that function.
> 
> I haven't been able to observe this happening with the current code, and
> the current state machine seems to be simple enough to not be
> affected by these invalid transitions, but future modifications could
> make it more likely to happen.
> 
> Signed-off-by: Gabriel Krisman Bertazi <krisman at linux.vnet.ibm.com>

Looks correct.

Reviewed-by: Steve Wise <swise at opengridcomputing.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] nvme: Prevent controller state invalid transition
  2016-07-29 19:15 [PATCH] nvme: Prevent controller state invalid transition Gabriel Krisman Bertazi
  2016-08-01 11:13 ` Sagi Grimberg
  2016-08-15 15:17 ` Steve Wise
@ 2016-08-15 15:47 ` Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Jens Axboe @ 2016-08-15 15:47 UTC (permalink / raw)


On 07/29/2016 01:15 PM, Gabriel Krisman Bertazi wrote:
> Acquiring the nvme_ctrl lock before reading ctrl->state in
> nvme_change_ctrl_state() should prevent a theoretical invalid state
> transition, in the event of two threads racing inside that function.
>
> I haven't been able to observe this happening with the current code, and
> the current state machine seems to be simple enough to not be
> affected by these invalid transitions, but future modifications could
> make it more likely to happen.

Applied, thanks.

-- 
Jens Axboe

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-08-15 15:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-29 19:15 [PATCH] nvme: Prevent controller state invalid transition Gabriel Krisman Bertazi
2016-08-01 11:13 ` Sagi Grimberg
2016-08-15 15:17 ` Steve Wise
2016-08-15 15:47 ` Jens Axboe

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.