Skip to contentSkip to conversation

scsi: ufs: core: Fix a race condition related to device commands

PATCH Superseded Bart Van Assche 3 participants 8 messages Mar 11, 2025 – Mar 13, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Bart Van Assche

author reviewer
#0 lore raw

PATCH 1/1 scsi: ufs: core: Fix a race condition related to device commands

There is a TOCTOU race in ufshcd_compl_one_cqe(): hba->dev_cmd.complete may be cleared from another thread after it has been checked and before it is used. Fix this race by moving the device command completion from the stack of the device command submitter into struct ufs_hba. This patch fixes the following kernel crash:

Unable to handle kernel NULL pointer dereference at virtual address 0000000000000008
Call trace:
 _raw_spin_lock_irqsave+0x34/0x80
 complete+0x24/0xb8
 ufshcd_compl_one_cqe+0x13c/0x4f0
 ufshcd_mcq_poll_cqe_lock+0xb4/0x108
 ufshcd_intr+0x2f4/0x444
 __handle_irq_event_percpu+0xbc/0x250
 handle_irq_event+0x48/0xb0
Fixes 5a0b0cb9bee7 ("[SCSI] ufs: Add support for sending NOP OUT UPIU")
---
 drivers/ufs/core/ufshcd.c | 24 +++++-------------------
 include/ufs/ufshcd.h      |  2 +-
 2 files changed, 6 insertions(+), 20 deletions(-)
drivers/ufs/core/ufshcd.c +5 -19
index 4e1e214fc5a2..23ba3f540f27 100644
@@ -3176,16 +3176,10 @@ static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
 	int err;
 
 retry:
-	time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
+	time_left = wait_for_completion_timeout(&hba->dev_cmd.complete,
 						time_left);
 
 	if (likely(time_left)) {
-		/*
-		 * The completion handler called complete() and the caller of
-		 * this function still owns the @lrbp tag so the code below does
-		 * not trigger any race conditions.
-		 */
-		hba->dev_cmd.complete = NULL;
 		err = ufshcd_get_tr_ocs(lrbp, NULL);
 		if (!err)
 			err = ufshcd_dev_cmd_completion(hba, lrbp);
@@ -3199,7 +3193,6 @@ static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
 			/* successfully cleared the command, retry if needed */
 			if (ufshcd_clear_cmd(hba, lrbp->task_tag) == 0)
 				err = -EAGAIN;
-			hba->dev_cmd.complete = NULL;
 			return err;
 		}
 
@@ -3215,11 +3208,9 @@ static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
 			spin_lock_irqsave(&hba->outstanding_lock, flags);
 			pending = test_bit(lrbp->task_tag,
 					   &hba->outstanding_reqs);
-			if (pending) {
-				hba->dev_cmd.complete = NULL;
+			if (pending)
 				__clear_bit(lrbp->task_tag,
 					    &hba->outstanding_reqs);
-			}
 			spin_unlock_irqrestore(&hba->outstanding_lock, flags);
 
 			if (!pending) {
@@ -3237,8 +3228,6 @@ static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
 			spin_lock_irqsave(&hba->outstanding_lock, flags);
 			pending = test_bit(lrbp->task_tag,
 					   &hba->outstanding_reqs);
-			if (pending)
-				hba->dev_cmd.complete = NULL;
 			spin_unlock_irqrestore(&hba->outstanding_lock, flags);
 
 			if (!pending) {
@@ -3272,13 +3261,10 @@ static void ufshcd_dev_man_unlock(struct ufs_hba *hba)
 static int ufshcd_issue_dev_cmd(struct ufs_hba *hba, struct ufshcd_lrb *lrbp,
 			  const u32 tag, int timeout)
 {
-	DECLARE_COMPLETION_ONSTACK(wait);
 	int err;
 
-	hba->dev_cmd.complete = &wait;
-
 	ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp->ucd_req_ptr);
-
+	init_completion(&hba->dev_cmd.complete);
 	ufshcd_send_command(hba, tag, hba->dev_cmd_queue);
 	err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
 
@@ -5585,12 +5571,12 @@ void ufshcd_compl_one_cqe(struct ufs_hba *hba, int task_tag,
 		ufshcd_release_scsi_cmd(hba, lrbp);
 		/* Do not touch lrbp after scsi done */
 		scsi_done(cmd);
-	} else if (hba->dev_cmd.complete) {
+	} else {
 		if (cqe) {
 			ocs = le32_to_cpu(cqe->status) & MASK_OCS;
 			lrbp->utr_descriptor_ptr->header.ocs = ocs;
 		}
-		complete(hba->dev_cmd.complete);
+		complete(&hba->dev_cmd.complete);
 	}
 }
 
include/ufs/ufshcd.h +1 -1
index e3909cc691b2..f56050ce9445 100644
@@ -246,7 +246,7 @@ struct ufs_query {
 struct ufs_dev_cmd {
 	enum dev_cmd_type type;
 	struct mutex lock;
-	struct completion *complete;
+	struct completion complete;
 	struct ufs_query query;
 };
 
#1 lore raw

Hi,

Quoted 5 lines

There is a TOCTOU race in ufshcd_compl_one_cqe(): hba->dev_cmd.complete may be cleared from another thread after it has been checked and before it is used. Fix this race by moving the device command completion from the stack of the device command submitter into struct ufs_hba. This patch fixes the following kernel crash:

Can you elaborate how this is possible if there is a single tag for device management commands, And it is obtained under lock?
And why making the completion structure persistent beyond the function's scope solves the problem?

Thanks,
Avri

Quoted 119 lines
Unable to handle kernel NULL pointer dereference at virtual address
0000000000000008 Call trace:
 _raw_spin_lock_irqsave+0x34/0x80
 complete+0x24/0xb8
 ufshcd_compl_one_cqe+0x13c/0x4f0
 ufshcd_mcq_poll_cqe_lock+0xb4/0x108
 ufshcd_intr+0x2f4/0x444
 __handle_irq_event_percpu+0xbc/0x250
 handle_irq_event+0x48/0xb0
Fixes 5a0b0cb9bee7 ("[SCSI] ufs: Add support for sending NOP OUT UPIU")
---
 drivers/ufs/core/ufshcd.c | 24 +++++-------------------
 include/ufs/ufshcd.h      |  2 +-
 2 files changed, 6 insertions(+), 20 deletions(-)
drivers/ufs/core/ufshcd.c index renamed from drivers/ufs/core/ufshcd.c +0 -0

4e1e214fc5a2..23ba3f540f27 100644

drivers/ufs/core/ufshcd.c +1 -7
@@ -3176,16 +3176,10 @@ static int ufshcd_wait_for_dev_cmd(struct ufs_hba
 *hba,
 	int err;
 
 retry:
-	time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
+	time_left = wait_for_completion_timeout(&hba->dev_cmd.complete,
 						time_left);
 
 	if (likely(time_left)) {
-		/*
-		 * The completion handler called complete() and the caller of
-		 * this function still owns the @lrbp tag so the code below does
-		 * not trigger any race conditions.
-		 */
-		hba->dev_cmd.complete = NULL;
 		err = ufshcd_get_tr_ocs(lrbp, NULL);
 		if (!err)

err = ufshcd_dev_cmd_completion(hba, lrbp); @@ -
3199,7 +3193,6 @@ static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,

drivers/ufs/core/ufshcd.c +1 -4
 			/* successfully cleared the command, retry if needed */
 			if (ufshcd_clear_cmd(hba, lrbp->task_tag) == 0)
 				err = -EAGAIN;
-			hba->dev_cmd.complete = NULL;
 			return err;
 		}
@@ -3215,11 +3208,9 @@ static int ufshcd_wait_for_dev_cmd(struct ufs_hba
 *hba,
 			spin_lock_irqsave(&hba->outstanding_lock, flags);
 			pending = test_bit(lrbp->task_tag,
 					   &hba->outstanding_reqs);
-			if (pending) {
-				hba->dev_cmd.complete = NULL;
+			if (pending)
 				__clear_bit(lrbp->task_tag,
 					    &hba->outstanding_reqs);
-			}
 			spin_unlock_irqrestore(&hba->outstanding_lock, flags);
 
drivers/ufs/core/ufshcd.c +0 -2
 			if (!pending) {
@@ -3237,8 +3228,6 @@ static int ufshcd_wait_for_dev_cmd(struct ufs_hba
 *hba,
 			spin_lock_irqsave(&hba->outstanding_lock, flags);
 			pending = test_bit(lrbp->task_tag,
 					   &hba->outstanding_reqs);
-			if (pending)
-				hba->dev_cmd.complete = NULL;
 			spin_unlock_irqrestore(&hba->outstanding_lock, flags);
 
drivers/ufs/core/ufshcd.c +1 -4
 			if (!pending) {
@@ -3272,13 +3261,10 @@ static void ufshcd_dev_man_unlock(struct ufs_hba
 *hba)  static int ufshcd_issue_dev_cmd(struct ufs_hba *hba, struct ufshcd_lrb
 *lrbp,
 			  const u32 tag, int timeout)
 {
-	DECLARE_COMPLETION_ONSTACK(wait);
 	int err;
 
-	hba->dev_cmd.complete = &wait;
-
 	ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp-
 >ucd_req_ptr);
-
+	init_completion(&hba->dev_cmd.complete);
 	ufshcd_send_command(hba, tag, hba->dev_cmd_queue);
drivers/ufs/core/ufshcd.c +2 -2
 	err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
@@ -5585,12 +5571,12 @@ void ufshcd_compl_one_cqe(struct ufs_hba *hba,
 int task_tag,
 		ufshcd_release_scsi_cmd(hba, lrbp);
 		/* Do not touch lrbp after scsi done */
 		scsi_done(cmd);
-	} else if (hba->dev_cmd.complete) {
+	} else {
 		if (cqe) {
 			ocs = le32_to_cpu(cqe->status) & MASK_OCS;
 			lrbp->utr_descriptor_ptr->header.ocs = ocs;
 		}
-		complete(hba->dev_cmd.complete);
+		complete(&hba->dev_cmd.complete);
 	}
 }
include/ufs/ufshcd.h index renamed from include/ufs/ufshcd.h +0 -0

e3909cc691b2..f56050ce9445 100644

include/ufs/ufshcd.h +1 -1
@@ -246,7 +246,7 @@ struct ufs_query {
 struct ufs_dev_cmd {
 	enum dev_cmd_type type;
 	struct mutex lock;
-	struct completion *complete;
+	struct completion complete;
 	struct ufs_query query;
 };
 
author reviewer
#2 lore raw
Quoted 9 lines · Avri Altman

Hi,

Quoted 5 lines

There is a TOCTOU race in ufshcd_compl_one_cqe(): hba->dev_cmd.complete may be cleared from another thread after it has been checked and before it is used. Fix this race by moving the device command completion from the stack of the device command submitter into struct ufs_hba. This patch fixes the following kernel crash:

Can you elaborate how this is possible if there is a single tag for device management commands, And it is obtained under lock?

Which lock? The code that submits device management commands is serialized by the hba->dev_cmd.lock mutex while ufshcd_compl_one_cqe() calls are serialized by the hwq->cq_lock spinlock in case of MCQ. I'm not aware of any single synchronization object that serializes all hba->dev_cmd.complete accesses.

Quoted 1 line

And why making the completion structure persistent beyond the function's scope solves the problem?

Without my patch, hba->dev_cmd.complete is sometimes set and sometimes NULL. My patch ensures that the pointer passed to the complete() call is never NULL. Does this answer your question?

Thanks,

Bart.

#3 lore raw
Quoted 21 lines · Avri Altman
Quoted 4 lines

Hi,

Quoted 3 lines

There is a TOCTOU race in ufshcd_compl_one_cqe(): hba->dev_cmd.complete may be cleared from another thread after it has been checked and before it is used. Fix this race by moving the device command completion from the stack

of

Quoted 1 line
Quoted 1 line

the device command submitter into struct ufs_hba. This patch fixes the

following

Quoted 3 lines
Quoted 1 line

kernel crash:

Can you elaborate how this is possible if there is a single tag for device

management commands,

Quoted 1 line

And it is obtained under lock?

Which lock? The code that submits device management commands is serialized by the hba->dev_cmd.lock mutex while ufshcd_compl_one_cqe() calls are serialized by the hwq->cq_lock spinlock in case of MCQ. I'm not aware of any single synchronization object that serializes all hba->dev_cmd.complete accesses.

Quoted 6 lines
Quoted 1 line

And why making the completion structure persistent beyond the function's

scope solves the problem?

Without my patch, hba->dev_cmd.complete is sometimes set and sometimes NULL. My patch ensures that the pointer passed to the complete() call is never NULL. Does this answer your question?

I was hoping for a root-cause analysis explaining why hba->dev_cmd.complete is sometimes set and sometimes NULL.

Thanks,
Avri

Quoted 4 lines

Thanks,

Bart.

author reviewer
#4 lore raw
Quoted 2 lines · Avri Altman

I was hoping for a root-cause analysis explaining why hba-

Quoted 1 line

dev_cmd.complete is sometimes set and sometimes NULL.

Hi Avri,

Hasn't that already been explained in the patch description? It's not clear what additional information you need other than what has already been mentioned in the patch description.

Thanks,

Bart.

#5 lore raw
In reply to Bart Van Assche
Quoted 10 lines · Bart Van Assche
(quoted diff) +0 -0
@@ -3272,13 +3261,10 @@ static void ufshcd_dev_man_unlock(struct
 ufs_hba *hba)

 static int ufshcd_issue_dev_cmd(struct ufs_hba *hba, struct ufshcd_lrb *lrbp,
                          const u32 tag, int timeout) {
-       DECLARE_COMPLETION_ONSTACK(wait);
        int err;

Quoted 9 lines

-       hba->dev_cmd.complete = &wait;
-
        ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp-

Quoted 1 line

ucd_req_ptr);

-
+       init_completion(&hba->dev_cmd.complete); ufshcd_send_command(hba, tag, hba->dev_cmd_queue); err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);

Hi Bart,

This could calling init_completion on the same completion twice?

Thanks.
Peter

author reviewer
#6 lore raw
Quoted 21 lines · Peter Wang (王信友)
Quoted 16 lines · Bart Van Assche
drivers/ufs/core/ufshcd.c +1 -4
@@ -3272,13 +3261,10 @@ static void ufshcd_dev_man_unlock(struct
 ufs_hba *hba)
  static int ufshcd_issue_dev_cmd(struct ufs_hba *hba, struct
 ufshcd_lrb *lrbp,
                           const u32 tag, int timeout)
  {
-       DECLARE_COMPLETION_ONSTACK(wait);
         int err;
 
-       hba->dev_cmd.complete = &wait;
-
         ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp-
 > ucd_req_ptr);
-
+       init_completion(&hba->dev_cmd.complete);
Quoted 3 lines

        ufshcd_send_command(hba, tag, hba->dev_cmd_queue); err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);

Quoted 3 lines

Hi Bart,

This could calling init_completion on the same completion twice?

Hi Peter,

My patch will cause init_completion() to be called as many times as device management commands are submitted. As far as I know the following sequence is allowed and does not trigger any race conditions:

Thread 1 Thread 2
-------- --------
init_completion() wait_for_completion_timeout() is called
complete()
wait_for_completion_timeout() returns

init_completion() wait_for_completion_timeout() is called
complete()
wait_for_completion_timeout() returns

[ ... ]

Thanks,

Bart.

#7 lore raw
Quoted 24 lines · Bart Van Assche
Quoted 4 lines

Hi Bart,

This could calling init_completion on the same completion twice?

Hi Peter,

My patch will cause init_completion() to be called as many times as device management commands are submitted. As far as I know the following
sequence is allowed and does not trigger any race conditions:

Thread 1                                  Thread 2 --------                                  -------- init_completion() wait_for_completion_timeout() is called complete() wait_for_completion_timeout() returns

init_completion() wait_for_completion_timeout() is called complete() wait_for_completion_timeout() returns

Quoted 5 lines

[ ... ]

Thanks,

Bart.

Hi Bart,

https://www.kernel.org/doc/Documentation/scheduler/completion.txt
Calling init_completion() on the same completion object twice is most likely a bug as it re-initializes the queue to an empty queue and enqueued tasks could get "lost" - use reinit_completion() in that case, but be aware of other races.

I understand that it should be protected from racing issues by the dev_cmd.lock, but it's probably best not to use it in this way. Using reinit_completion might be a better approach.

Thanks.
Peter