[PATCH v4 3/4] scsi: libiscsi: take a transmit reference before a task is published
From: Yehyeong Lee
Date: Tue Nov 17 2026 - 05:15:33 EST
A task becomes visible to the receive path when its state is stored as
ISCSI_TASK_RUNNING, and the receive path completes it under back_lock
while the transmit path works under frwd_lock. The transmit path
publishes that state before it owns a reference of its own:
- iscsi_prep_mgmt_task() and iscsi_prep_scsi_cmd_pdu() store the state
and the reference is only taken later, in iscsi_xmit_task(). A
response that lands in between drops the only reference, so the slot
is back in the pool by the time iscsi_get_task() runs, which then
warns and drops the PDU.
- a transport without an xmit workqueue does not call iscsi_xmit_task()
at all. iscsi_send_mgmt_task() and iscsi_queuecommand() call
session->tt->xmit_task() holding only the allocation reference, so
the slot can be freed and handed out again while the transport is
still sending from it, and the error path then puts a reference it no
longer owns. ihost->workq only exists when iscsi_host_alloc() is
called with xmit_can_sleep set, which iscsi_tcp and cxgbi do and
iser, bnx2i, qedi, be2iscsi and qla4xxx do not.
For a SCSI command there is no point publishing the state at all,
because iscsi_itt_to_ctask() does not read it: a task is reachable
through session->cmds[] as soon as iscsi_alloc_task() fills it in, so a
target that answers an ITT it has not been given yet reaches
iscsi_scsi_cmd_rsp() for a command that is still sitting on
conn->cmdqueue in ISCSI_TASK_PENDING. That drops the only reference,
which returns the pool slot and calls scsi_done() with whatever the
buffer held, and leaves the task on the queue; iscsi_data_xmit() then
builds a PDU from a task whose ->sc has been cleared.
So: take the transmit reference before the state is stored, in
iscsi_xmit_task() and in the two callers that send a task themselves;
make the SCSI lookup skip a task that has not been sent; and finish
with a failed send inside the same back_lock section that tests for it.
That lookup has to refuse ISCSI_TASK_FREE as well as ISCSI_TASK_PENDING.
iscsi_alloc_task() stores the state before ->sc, and under frwd_lock,
while the FREE the slot's last life ended with was stored under the
back_lock the lookup holds. So the one thing the receive path can see
out of order is the new ->sc together with that FREE; both values mean
the same thing, and a slot that really is free has a NULL ->sc, which
the line above already rejects.
Two choices in there are not arbitrary. The SCSI lookup rejects the
two states that mean "not sent yet" rather than everything that is not
ISCSI_TASK_RUNNING, because iscsi_eh_abort() deliberately leaves the
receive side running after a successful abort TMF and says so, and
because iscsi_tcp_r2t_rsp() looks a task up in order to see that it has
already been completed and ignore the R2T, instead of failing the
connection. And iscsi_send_mgmt_task(), whose caller frees the task,
parks a failed send in ISCSI_TASK_COMPLETED rather than
ISCSI_TASK_PENDING, because bnx2i_cleanup_task() and qedi_cleanup_task()
read ISCSI_TASK_PENDING as "never sent to us" and skip their cleanup,
and both of those are no-workqueue transports.
All three were reproduced with test-only delays that hold each window
open, in a QEMU guest with KASAN against a userspace target reached
through a proxy that can inject a PDU of its own, and on iSER over
soft-RoCE against an in-guest LIO target. Every cell is a fresh boot
and each arm was measured five times, with any cell where the window
closed before the response had been processed counted separately.
For the SCSI window the proxy sends a SCSI Response naming a pool index
it has never seen go out, while the command is still on conn->cmdqueue.
Without this patch the slot went back to the pool inside the window in
5 of 5 attempts and the read returned success on a buffer nobody had
written; with it, 0 of 5, because iscsi_itt_to_ctask() refuses the
response and the connection is dropped with ISCSI_ERR_BAD_ITT, which is
what the driver already does with every other unusable ITT. What the
transmit thread does next with the slot it has lost:
[ 14.300338] Oops: general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] SMP KASAN NOPTI
[ 14.302897] KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
[ 14.303981] CPU: 3 UID: 0 PID: 52 Comm: kworker/u16:2 Not tainted 7.4.0-rc2-00473-g7baa29cbc558-dirty #2 PREEMPT(lazy)
[ 14.305403] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
[ 14.306889] Workqueue: iscsi_q_2 iscsi_xmitworker
[ 14.307549] RIP: 0010:iscsi_prep_scsi_cmd_pdu+0x2ef/0x2220
[ 14.308284] Code: b6 04 02 40 38 f0 7f 08 84 c0 0f 85 c5 0f 00 00 4c 89 e1 41 c6 43 01 01 49 8d 53 08 48 b8 00 00 00 00 00 fc ff df 48 c1 e9 03 <80> 3c 01 00 0f 85 09 17 00 00 48 b9 00 00 00 00 00 fc ff df 49 8b
[ 14.310690] RSP: 0018:ffffc90000397b18 EFLAGS: 00010246
[ 14.311388] RAX: dffffc0000000000 RBX: ffff88800cfc1800 RCX: 0000000000000000
[ 14.312326] RDX: ffff88800cfc1900 RSI: 0000000000000001 RDI: ffff88800cfc1808
[ 14.313265] RBP: ffff88800ce40378 R08: 0000000000000001 R09: ffff88800cfc18f8
[ 14.314202] R10: 0000000000000000 R11: ffff88800cfc18f8 R12: 0000000000000000
[ 14.315139] R13: ffff88800ce42440 R14: ffff88800cfc1858 R15: ffff88800ce40388
[ 14.316077] FS: 0000000000000000(0000) GS:ffff8880e5728000(0000) knlGS:0000000000000000
[ 14.317135] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 14.317904] CR2: 00000000005a46e2 CR3: 0000000005672006 CR4: 0000000000770ef0
[ 14.318844] PKRU: 55555554
[ 14.319217] Call Trace:
[ 14.319564] <TASK>
[ 14.319872] iscsi_xmitworker+0x56b/0xf20
[ 14.320417] process_one_work+0x697/0x1060
[ 14.320979] ? __pfx___schedule+0x10/0x10
[ 14.321522] ? __pfx_process_one_work+0x10/0x10
[ 14.322136] ? _raw_spin_lock_irq+0x81/0xe0
[ 14.322699] ? __pfx_iscsi_xmitworker+0x10/0x10
[ 14.323313] ? assign_work+0x11d/0x370
[ 14.323830] worker_thread+0x45b/0xd10
[ 14.324341] ? __pfx_worker_thread+0x10/0x10
[ 14.324923] kthread+0x2c8/0x3b0
[ 14.325369] ? recalc_sigpending+0x15c/0x1e0
[ 14.325953] ? __pfx_kthread+0x10/0x10
[ 14.326461] ret_from_fork+0x36e/0x5a0
[ 14.326979] ? __pfx_ret_from_fork+0x10/0x10
[ 14.327556] ? __switch_to+0x572/0xdc0
[ 14.328073] ? __pfx_kthread+0x10/0x10
[ 14.328582] ret_from_fork_asm+0x1a/0x30
[ 14.329119] </TASK>
[ 14.329429] Modules linked in:
[ 14.329880] ---[ end trace 0000000000000000 ]---
For the state publication, a NOP-Out answered by the target inside the
window, the slot went back to the pool in 5 of 5 attempts without this
patch and in none of the attempts with it, and the xmit worker hit this
on the iscsi_get_task() that then fails:
WARNING: drivers/scsi/libiscsi.c:1598 at iscsi_xmit_task+0x6e0/0x920
For the no-workqueue path the
reference was put twice in 5 of 5 attempts without the patch and 0 of 5
with it, as "refcount_t: underflow; use-after-free." from
iscsi_send_nopout().
Baseline behaviour is unchanged: discovery, login, write, read-back
compare, a 1 MiB O_DIRECT read, NOP pings and logout pass on iscsi_tcp
and on iSER, and so does an abort TMF driven by swallowing a response -
the path that leaves a task in ISCSI_TASK_ABRT_TMF with the receive
side still up. cxgbi, bnx2i, qedi, be2iscsi and qla4xxx need vendor
hardware and were not run; their paths were followed in the source
only.
Fixes: 659743b02c41 ("[SCSI] libiscsi: Reduce locking contention in fast path")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Yehyeong Lee <yhlee@xxxxxxxxxxxxxxxxxx>
---
drivers/scsi/libiscsi.c | 49 ++++++++++++++++++++++++++++++++++-------
1 file changed, 41 insertions(+), 8 deletions(-)
diff --git a/drivers/scsi/libiscsi.c b/drivers/scsi/libiscsi.c
index 220e89d541af68..af454454c08699 100644
--- a/drivers/scsi/libiscsi.c
+++ b/drivers/scsi/libiscsi.c
@@ -429,8 +429,6 @@ static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
if (session->tt->init_task && session->tt->init_task(task))
return -EIO;
- /* Pairs with the acquire in the RX task lookups. */
- smp_store_release(&task->state, ISCSI_TASK_RUNNING);
session->cmdsn++;
conn->scsicmd_pdus_cnt++;
@@ -703,8 +701,6 @@ static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
session->state = ISCSI_STATE_LOGGING_OUT;
- /* Pairs with the acquire in the RX task lookups. */
- smp_store_release(&task->state, ISCSI_TASK_RUNNING);
ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
"datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
hdr->itt, task->data_count);
@@ -829,7 +825,20 @@ static int iscsi_send_mgmt_task(struct iscsi_task *task)
if (rc)
return rc;
+ iscsi_get_task(task);
+ /* Pairs with the acquire in the RX task lookups. */
+ smp_store_release(&task->state, ISCSI_TASK_RUNNING);
rc = session->tt->xmit_task(task);
+ /* regular RX path uses back_lock */
+ spin_lock_bh(&session->back_lock);
+ /* The RX path completing the task is also a successful send. */
+ if (rc && task->state != ISCSI_TASK_RUNNING)
+ rc = 0;
+ else if (rc)
+ /* Keep the RX path off it; the caller still owns the ref. */
+ task->state = ISCSI_TASK_COMPLETED;
+ __iscsi_put_task(session, task);
+ spin_unlock_bh(&session->back_lock);
if (rc)
return rc;
} else {
@@ -1176,7 +1185,7 @@ static int iscsi_handle_reject(struct iscsi_session *session,
if (!iscsi_verify_itt(conn, rejected_pdu.itt))
task = iscsi_itt_to_task(session, conn,
rejected_pdu.itt);
- /* Pairs with the release when the task is prepared. */
+ /* Pairs with the release in the transmit path. */
/* A reserved ITT means the transport owns the task. */
if (!task || smp_load_acquire(&task->state) != ISCSI_TASK_RUNNING ||
task->sc || task->hdr->itt == RESERVED_ITT) {
@@ -1318,7 +1327,7 @@ int __iscsi_complete_pdu(struct iscsi_session *session, struct iscsi_conn *conn,
case ISCSI_OP_SCSI_TMFUNC_RSP:
case ISCSI_OP_NOOP_IN:
task = iscsi_itt_to_task(session, conn, hdr->itt);
- /* Pairs with the release when the task is prepared. */
+ /* Pairs with the release in the transmit path. */
/* A reserved ITT means the transport owns the task. */
if (!task || smp_load_acquire(&task->state) != ISCSI_TASK_RUNNING ||
task->sc || task->hdr->itt == RESERVED_ITT)
@@ -1447,6 +1456,7 @@ struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_session *session,
__must_hold(&session->back_lock)
{
struct iscsi_task *task;
+ int state;
if (iscsi_verify_itt(conn, itt))
return NULL;
@@ -1455,6 +1465,11 @@ struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_session *session,
if (!task || !task->sc)
return NULL;
+ /* Pairs with the transmit path's release; a new slot is FREE too. */
+ state = smp_load_acquire(&task->state);
+ if (state == ISCSI_TASK_PENDING || state == ISCSI_TASK_FREE)
+ return NULL;
+
if (iscsi_cmd(task->sc)->age != session->age) {
iscsi_session_printk(KERN_ERR, session,
"task's session age %d, expected %d\n",
@@ -1556,6 +1571,9 @@ static int iscsi_xmit_task(struct iscsi_session *session, struct iscsi_conn *con
WARN_ON_ONCE(1);
return 0;
}
+ if (!was_requeue)
+ /* Pairs with the acquire in the RX task lookups. */
+ smp_store_release(&task->state, ISCSI_TASK_RUNNING);
} else {
/* Already have a ref from when we failed to send it last call */
conn->task = NULL;
@@ -1892,10 +1910,25 @@ enum scsi_qc_status iscsi_queuecommand(struct Scsi_Host *host,
goto prepd_fault;
}
}
- if (session->tt->xmit_task(task)) {
+ iscsi_get_task(task);
+ /* Pairs with the acquire in the RX task lookups. */
+ smp_store_release(&task->state, ISCSI_TASK_RUNNING);
+ reason = session->tt->xmit_task(task);
+ /* regular RX path uses back_lock */
+ spin_lock_bh(&session->back_lock);
+ /* The RX path completing the task is also a successful send. */
+ if (reason && task->state != ISCSI_TASK_RUNNING)
+ reason = 0;
+ else if (reason)
+ /* Give the command back inside the lock, not after it. */
+ iscsi_complete_task(session, task,
+ ISCSI_TASK_REQUEUE_SCSIQ);
+ __iscsi_put_task(session, task);
+ spin_unlock_bh(&session->back_lock);
+ if (reason) {
session->cmdsn--;
reason = FAILURE_SESSION_NOT_READY;
- goto prepd_reject;
+ goto reject;
}
} else {
list_add_tail(&task->running, &conn->cmdqueue);
--
2.43.0