All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Elkin Cruz" <elkin@google.com>
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH] scsi: hisi_sas: Avoid synchronize_irq() under task_state_lock
Date: Wed, 25 Nov 2026 22:54:26 +0000	[thread overview]
Message-ID: <sashiko-outbox-234314@kernel.org> (raw)
In-Reply-To: <20261125222029.1429071-1-elkin@google.com>

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

      reply	other threads:[~2026-11-25 22:54 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-234314@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=elkin@google.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.