All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yehyeong Lee" <yhlee@isslab.korea.ac.kr>
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH] scsi: libiscsi_tcp: hold back_lock across the copy of a Data-In
Date: Fri, 20 Nov 2026 15:15:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-225445@kernel.org> (raw)
In-Reply-To: <20261120145243.3124991-1-yhlee@isslab.korea.ac.kr>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] scsi: libiscsi_tcp: race on ITT slot reuse in
  iscsi_tcp_process_data_in()
- [High] scsi: libiscsi_tcp: kmap_atomic() held across back_lock unlock
  at skb_done
- [High] scsi: libiscsi_tcp: missing ISCSI_TASK_RUNNING check in
  iscsi_tcp_hdr_dissect()
- [Low] scsi: libiscsi_tcp: Clang context-analysis warning in
  iscsi_tcp_recv_skb()
--

commit 137946721880763b95233f1c2f065be24dc7e099
Author: Yehyeong Lee <yhlee@isslab.korea.ac.kr>

scsi: libiscsi_tcp: hold back_lock across the copy of a Data-In

This commit tracks the active Data-In task in tcp_conn->in.datain_task and
holds session->back_lock across the Data-In scatterlist copy in
iscsi_tcp_recv_skb() so that error handlers cannot free the command's
scatterlist while the copy is in progress.

> diff --git a/drivers/scsi/libiscsi_tcp.c b/drivers/scsi/libiscsi_tcp.c
> index 20ec3ba45067f..50c58e938321e 100644
> --- a/drivers/scsi/libiscsi_tcp.c
> +++ b/drivers/scsi/libiscsi_tcp.c
[ ... ]
> @@ -744,6 +752,7 @@ iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
>  				      tcp_task->data_offset,
>  				      tcp_conn->in.datalen);
>  			task->last_xfer = jiffies;
> +			tcp_conn->in.datain_task = task;
>  			rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
>  						   sdb->table.sgl,
>  						   sdb->table.nents,

[Severity: High]
Should iscsi_tcp_hdr_dissect() verify that task->state ==
ISCSI_TASK_RUNNING before accepting a Data-In header and setting
tcp_conn->in.datain_task?

iscsi_itt_to_ctask() only checks task->sc and session->age, so it also
returns newly queued commands in ISCSI_TASK_PENDING on conn->cmdqueue.  If
a stray Data-In header arrives for a slot occupied by a pending command and
iscsi_prep_scsi_cmd_pdu() transitions that command to ISCSI_TASK_RUNNING
before the payload skb arrives, iscsi_tcp_recv_skb() sees the matching
datain_task in ISCSI_TASK_RUNNING and copies the stale Data-In payload into
the new command's scatterlist.  In addition, a zero-length Data-In with
ISCSI_FLAG_DATA_STATUS calls __iscsi_complete_pdu() right below without
ever reaching iscsi_tcp_recv_skb(), freeing an ISCSI_TASK_PENDING task
while it is still linked on conn->cmdqueue and causing a NULL task->sc
dereference in iscsi_prep_scsi_cmd_pdu().

> @@ -907,8 +916,10 @@ int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
>  {
>  	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
>  	struct iscsi_segment *segment = &tcp_conn->in.segment;
> +	struct iscsi_session *session = conn->session;
>  	struct skb_seq_state seq;
>  	unsigned int consumed = 0;
> +	bool copy_locked = false;
>  	int rc = 0;
>  
>  	ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
> @@ -925,6 +936,26 @@ int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
>  		return 0;
>  	}
>  
> +	/*
> +	 * Hold back_lock across a Data-In copy so the command cannot be
> +	 * given back while it runs.  _bh: this also runs from a work item.
> +	 */
> +	if (segment->sg) {
> +		struct iscsi_task *task;
> +
> +		spin_lock_bh(&session->back_lock);
> +		task = tcp_conn->in.datain_task;
> +		if (!task || task->state != ISCSI_TASK_RUNNING) {
> +			spin_unlock_bh(&session->back_lock);
> +			iscsi_conn_printk(KERN_ERR, conn,
> +					  "Data-In for a command that is gone\n");
> +			*status = ISCSI_TCP_CONN_ERR;
> +			iscsi_conn_failure(conn, ISCSI_ERR_BAD_ITT);
> +			return 0;
> +		}
> +		copy_locked = true;
> +	}
> +
>  	if (offloaded) {
>  		segment->total_copied = segment->total_size;
>  		goto segment_done;
> @@ -957,6 +988,10 @@ int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
>  	}
>  
>  segment_done:
> +	if (copy_locked) {
> +		spin_unlock_bh(&session->back_lock);
> +		copy_locked = false;
> +	}
>  	*status = ISCSI_TCP_SEGMENT_DONE;
>  	ISCSI_DBG_TCP(conn, "segment done\n");
>  	rc = segment->done(tcp_conn, segment);

[Severity: High]
Can dropping session->back_lock before segment->done() let
iscsi_tcp_process_data_in() complete a newly queued command that reuses
the freed ITT slot?

CPU0 (softirq)                    CPU1 (EH / queuecommand)
iscsi_tcp_recv_skb()
  spin_lock_bh(&back_lock)
  iscsi_tcp_segment_recv()
  spin_unlock_bh(&back_lock)
                                  iscsi_eh_device_reset()
                                    fail_scsi_tasks()
                                      spin_lock_bh(&back_lock)
                                      iscsi_free_task()
                                        datain_task = NULL
                                      spin_unlock_bh(&back_lock)
                                  iscsi_queuecommand()
                                    /* reuses the same ITT slot */
  segment->done()
    iscsi_tcp_process_data_in()
      iscsi_complete_pdu()
        spin_lock(&back_lock)
        iscsi_itt_to_ctask()
        iscsi_complete_task()     <-- completes the new command

When ISCSI_FLAG_DATA_STATUS is set on the Data-In header,
iscsi_tcp_process_data_in() calls iscsi_complete_pdu() without checking
tcp_conn->in.datain_task, so __iscsi_complete_pdu() looks up hdr->itt
afresh via iscsi_itt_to_ctask().  If the new command is still in
ISCSI_TASK_PENDING on conn->cmdqueue, completing it frees the task and sets
task->sc = NULL while it stays linked on conn->cmdqueue, causing a NULL
pointer dereference in iscsi_prep_scsi_cmd_pdu().  Also, when software data
digests are enabled, iscsi_tcp_segment_splice_digest() sets
segment->sg = NULL before receiving the 4-byte digest, so a subsequent skb
carrying the digest skips the if (segment->sg) check and reaches
segment->done() after the task was already freed.

> @@ -969,6 +1004,8 @@ int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
>  	/* The done() functions sets up the next segment. */
>  
>  skb_done:
> +	if (copy_locked)
> +		spin_unlock_bh(&session->back_lock);
>  	conn->rxdata_octets += consumed;
>  	return consumed;
>  }

[Severity: High]
Does unlocking session->back_lock at skb_done leave segment->sg_mapped
mapped with kmap_atomic()?

When an skb ends before the Data-In payload is finished,
iscsi_tcp_segment_done() calls iscsi_tcp_segment_map(), which calls
kmap_atomic(sg_page(sg)), and the unmap does not happen until
iscsi_sw_tcp_recv_data() finishes tcp_read_sock():

drivers/scsi/libiscsi_tcp.c:iscsi_tcp_segment_done() {
	...
	segment->copied += copied;
	if (segment->copied < segment->size) {
		iscsi_tcp_segment_map(segment, recv);
		return 0;
	}
	...
}

On a multi-skb Data-In, skb_done releases session->back_lock while
kmap_atomic() is still active, allowing an error handler on another CPU to
free the command and its scatterlist pages while segment->sg_mapped remains
mapped, and the next skb in tcp_read_sock() then calls
spin_lock_bh(&session->back_lock) while inside kmap_atomic() /
pagefault_disable() (which violates sleeping spinlock rules on
CONFIG_PREEMPT_RT).

[Severity: Low]
Does the conditional locking across copy_locked trigger a Clang
-Wthread-safety-analysis warning when CONFIG_WARN_CONTEXT_ANALYSIS=y?
drivers/scsi/Makefile enables CONTEXT_ANALYSIS := y, and Clang's thread
safety analyzer does not track boolean lock flags across basic blocks.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120145243.3124991-1-yhlee@isslab.korea.ac.kr?part=1

  reply	other threads:[~2026-11-20 15:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 14:52 [PATCH] scsi: libiscsi_tcp: hold back_lock across the copy of a Data-In Yehyeong Lee
2026-11-20 15:15 ` sashiko-bot [this message]
2026-11-20 16:39   ` Yehyeong Lee

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-225445@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yhlee@isslab.korea.ac.kr \
    /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.