Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH] scsi: hisi_sas: Avoid synchronize_irq() under task_state_lock
@ 2026-11-25 22:20 Elkin Cruz
  2026-11-25 22:54 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Elkin Cruz @ 2026-11-25 22:20 UTC (permalink / raw)
  To: Xingui Yang, James E.J. Bottomley, Martin K. Petersen
  Cc: linux-scsi, linux-kernel, llvm, irogers, Elkin Cruz, stable

In hisi_sas_abort_task(), when SAS_TASK_STATE_DONE is set, the driver
invoked hisi_sas_sync_cq() while holding task->task_state_lock with
interrupts disabled. For interrupt-driven queues, hisi_sas_sync_cq()
calls synchronize_irq(), which may sleep (while for poll queues it
spins on cq->poll_lock in hisi_sas_sync_poll_cq()).

In addition, once SAS_TASK_STATE_DONE is set and task_state_lock is
released by the completion handler, hisi_sas_slot_task_free() clears
task->lldd_task, zeroes slot->dlvry_queue, and frees the slot back to
the pool outside task_state_lock.

Release task->task_state_lock before calling hisi_sas_sync_cqs() on the
SAS_TASK_STATE_DONE path, and read task->lldd_task under
task->task_state_lock when setting SAS_TASK_STATE_ABORTED.

Fixes: 584f53fe5f52 ("scsi: hisi_sas: Fix the race between IO completion and timeout for SMP/internal IO")
Cc: stable@vger.kernel.org
Signed-off-by: Elkin Cruz <elkin@google.com>
---

I am trying out new Clang static analysis checks for kernel bug
classes, and they flagged this code as possibly buggy. The findings
were triaged/fixed and commit descriptions were drafted with help of AI,
then checked against linux-next (not runtime-tested on hardware).

Thanks,
Elkin

 drivers/scsi/hisi_sas/hisi_sas_main.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/drivers/scsi/hisi_sas/hisi_sas_main.c b/drivers/scsi/hisi_sas/hisi_sas_main.c
index 06455c7a9e2a..2a5a34eb1b28 100644
--- a/drivers/scsi/hisi_sas/hisi_sas_main.c
+++ b/drivers/scsi/hisi_sas/hisi_sas_main.c
@@ -1730,7 +1730,7 @@ static int hisi_sas_abort_task(struct sas_task *task)
 	struct hisi_sas_internal_abort_data internal_abort_data = { false };
 	struct domain_device *device = task->dev;
 	struct hisi_sas_device *sas_dev = device->lldd_dev;
-	struct hisi_sas_slot *slot = task->lldd_task;
+	struct hisi_sas_slot *slot;
 	struct hisi_hba *hisi_hba;
 	struct device *dev;
 	int rc = TMF_RESP_FUNC_FAILED;
@@ -1744,21 +1744,18 @@ static int hisi_sas_abort_task(struct sas_task *task)
 
 	spin_lock_irqsave(&task->task_state_lock, flags);
 	if (task->task_state_flags & SAS_TASK_STATE_DONE) {
-		struct hisi_sas_cq *cq;
-
-		if (slot) {
-			/*
-			 * sync irq or poll queue to avoid free'ing task
-			 * before using task in IO completion
-			 */
-			cq = &hisi_hba->cq[slot->dlvry_queue];
-			hisi_sas_sync_cq(cq);
-		}
 		spin_unlock_irqrestore(&task->task_state_lock, flags);
+		/*
+		 * Sync all CQs to avoid freeing task before IO completion
+		 * finishes using it; slot and slot->dlvry_queue may already
+		 * be cleared or reallocated once SAS_TASK_STATE_DONE is set.
+		 */
+		hisi_sas_sync_cqs(hisi_hba);
 		rc = TMF_RESP_FUNC_COMPLETE;
 		goto out;
 	}
 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
+	slot = task->lldd_task;
 	spin_unlock_irqrestore(&task->task_state_lock, flags);
 
 	if (!slot)
-- 
2.56.1.839.g22a8290db9-goog


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

* Re: [PATCH] scsi: hisi_sas: Avoid synchronize_irq() under task_state_lock
  2026-11-25 22:20 [PATCH] scsi: hisi_sas: Avoid synchronize_irq() under task_state_lock Elkin Cruz
@ 2026-11-25 22:54 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-25 22:54 UTC (permalink / raw)
  To: Elkin Cruz; +Cc: linux-scsi

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] scsi: hisi_sas: TOCTOU race in hisi_sas_slot_task_free()
  [1]
--

commit f4ff37b15758e0c223690d6105b63787d918393e
Author: Elkin Cruz <elkin@google.com>

scsi: hisi_sas: Avoid synchronize_irq() under task_state_lock

This commit releases task->task_state_lock in hisi_sas_abort_task() before
synchronizing the completion queues when SAS_TASK_STATE_DONE is set, and
reads task->lldd_task under task->task_state_lock on the abort path.

> diff --git a/drivers/scsi/hisi_sas/hisi_sas_main.c b/drivers/scsi/hisi_sas/hisi_sas_main.c
> index 06455c7a9e2ad..2a5a34eb1b28b 100644
> --- a/drivers/scsi/hisi_sas/hisi_sas_main.c
> +++ b/drivers/scsi/hisi_sas/hisi_sas_main.c
> @@ -1730,7 +1730,7 @@ static int hisi_sas_abort_task(struct sas_task *task)
>  	struct hisi_sas_internal_abort_data internal_abort_data = { false };
>  	struct domain_device *device = task->dev;
>  	struct hisi_sas_device *sas_dev = device->lldd_dev;
> -	struct hisi_sas_slot *slot = task->lldd_task;
> +	struct hisi_sas_slot *slot;
>  	struct hisi_hba *hisi_hba;
>  	struct device *dev;
>  	int rc = TMF_RESP_FUNC_FAILED;
> @@ -1744,21 +1744,18 @@ static int hisi_sas_abort_task(struct sas_task *task)
>  
>  	spin_lock_irqsave(&task->task_state_lock, flags);
>  	if (task->task_state_flags & SAS_TASK_STATE_DONE) {
> -		struct hisi_sas_cq *cq;
> -
> -		if (slot) {
> -			/*
> -			 * sync irq or poll queue to avoid free'ing task
> -			 * before using task in IO completion
> -			 */
> -			cq = &hisi_hba->cq[slot->dlvry_queue];
> -			hisi_sas_sync_cq(cq);
> -		}
>  		spin_unlock_irqrestore(&task->task_state_lock, flags);
> +		/*
> +		 * Sync all CQs to avoid freeing task before IO completion
> +		 * finishes using it; slot and slot->dlvry_queue may already
> +		 * be cleared or reallocated once SAS_TASK_STATE_DONE is set.
> +		 */
> +		hisi_sas_sync_cqs(hisi_hba);
>  		rc = TMF_RESP_FUNC_COMPLETE;
>  		goto out;
>  	}
>  	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
> +	slot = task->lldd_task;
>  	spin_unlock_irqrestore(&task->task_state_lock, flags);
>  
>  	if (!slot)

[Severity: Critical]
This problem wasn't introduced by this patch, but can a concurrent
hisi_sas_release_task() and hardware completion race in
hisi_sas_slot_task_free() [1] and double free the slot?

Although hisi_sas_abort_task() now reads task->lldd_task under
task->task_state_lock, hisi_sas_slot_task_free() checks and clears
task->lldd_task without holding any lock:

drivers/scsi/hisi_sas/hisi_sas_main.c:hisi_sas_slot_task_free() {
	...
	if (task) {
		...
		if (!task->lldd_task)
			return;

		task->lldd_task = NULL;
	...
}

When hisi_sas_release_task() runs under sas_dev->lock (passing
need_lock=false) at the same time as a completion handler (passing
need_lock=true), both threads can pass the !task->lldd_task check:

CPU0                                CPU1
hisi_sas_release_task()
  spin_lock(&sas_dev->lock)
  hisi_sas_slot_task_free(..., false)
                                    hisi_sas_slot_task_free(..., true)
    if (!task->lldd_task)             if (!task->lldd_task)
    task->lldd_task = NULL;           task->lldd_task = NULL;
    // unmap DMA                      // double unmap DMA
    list_del_init(&slot->entry);
                                      spin_lock(&sas_dev->lock)
    memset(slot, 0, ...);
    hisi_sas_slot_index_free(...);
  spin_unlock(&sas_dev->lock)
                                      list_del_init(&slot->entry);
                                      spin_unlock(&sas_dev->lock)
                                      memset(slot, 0, ...);
                                      hisi_sas_slot_index_free(...);
                                        <-- corrupts and double-frees slot

If the slot index is reallocated while CPU1 is waiting on sas_dev->lock,
CPU1 zeroes out the live slot with memset() and frees the slot index a
second time.

[1] https://sashiko.dev/#/bug/linux-5c918a18-1806-4334-a75b-c6fd3a29066d

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125222029.1429071-1-elkin@google.com?part=1

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

end of thread, other threads:[~2026-11-25 22:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 22:20 [PATCH] scsi: hisi_sas: Avoid synchronize_irq() under task_state_lock Elkin Cruz
2026-11-25 22:54 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox