scsi: hisi_sas: Avoid synchronize_irq() under task_state_lock
Subsystems (3)
PATCH 1/1 scsi: hisi_sas: Avoid synchronize_irq() under task_state_lock
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.
---
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(-)
drivers/scsi/hisi_sas/hisi_sas_main.c +8 -11
@@ -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)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
[email protected]
AI reviewThank 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…
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 <[email protected]>
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.
Quoted 32 lines
drivers/scsi/hisi_sas/hisi_sas_main.c +5
-11@@ -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.
drivers/scsi/hisi_sas/hisi_sas_main.c static int hisi_sas_abort_task(struct sas_task *task) · line 1761
Quoted 10 lines
drivers/scsi/hisi_sas/hisi_sas_main.c +3
-0+*/ +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 CPU1hisi_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 slotIf 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
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1