mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] scsi: libiscsi_tcp: hold back_lock across the copy of a Data-In
@ 2026-11-20 14:52 Yehyeong Lee
       [not found] ` <sashiko-outbox-225445@kernel.org>
  2026-11-21  2:03 ` kernel test robot
  0 siblings, 2 replies; 3+ messages in thread
From: Yehyeong Lee @ 2026-11-20 14:52 UTC (permalink / raw)
  To: Lee Duncan, Chris Leech, Mike Christie, Martin K . Petersen,
	James E . J . Bottomley
  Cc: linux-scsi, linux-kernel

iscsi_tcp_hdr_dissect() aims the receive segment at a command's
scatterlist and drops back_lock, and the copy itself then runs once per
skb in iscsi_tcp_recv_skb() with nothing held:

	rc = iscsi_segment_seek_sg(&tcp_conn->in.segment, sdb->table.sgl,
				   sdb->table.nents, ...);
	spin_unlock(&session->back_lock);

The SCSI error handler finishes a task while such a copy is pending, and
it leaves the receive side running on purpose while it does.
iscsi_eh_abort() says so itself:

	/*
	 * we do not stop the recv side because targets have been
	 * good and have never sent us a successful tmf response
	 * then sent more data for the cmd.
	 */

iscsi_eh_device_reset() and iscsi_eh_target_reset() do the same, calling
iscsi_suspend_tx() and not iscsi_suspend_rx() before they fail the tasks.
fail_scsi_task() and fail_scsi_tasks() complete the task the receive path
is copying into, iscsi_free_task() hands the command back with
scsi_done(), and its scatterlist returns to the sg mempool while
tcp_conn->in.segment still points into it.

Looking the command up again before each round of copying would not help.
The error handler can fail the task between the lookup and the copy, and
a lookup cannot tell the command apart from its successor either:
task->itt is the index of a preallocated slot and is fixed for the life
of the session, so the next command to take that slot carries the same
ITT and the same session age, and iscsi_itt_to_ctask() returns it as
running while segment->sg still points into the freed scatterlist.

So let the lock that releases a command also guard the copy into it.
iscsi_tcp_cleanup_task() already runs from iscsi_free_task() with
back_lock held, immediately before the command is handed back, so
remember which command a Data-In is being copied into and drop that
pointer there.  iscsi_tcp_recv_skb() then holds back_lock for the whole
of the copy and refuses the rest of the PDU with ISCSI_ERR_BAD_ITT if the
pointer is gone, as it does one step earlier.  Cleanup and copy now
exclude each other, and a slot cannot be reused before the cleanup that
invalidated the pointer has run.  Only a Data-In pays for this: it is the
only receive segment aimed at a scatterlist, every other one is a linear
buffer that belongs to the connection.

The lock is dropped again before segment->done(), which for a Data-In is
iscsi_tcp_process_data_in() and takes back_lock itself.  Holding it over
the copy is bounded by one skb and adds no sleeping call; the copy maps
its pages with kmap_atomic().  It is taken with _bh because the receive
path is not only a softirq: iscsi_sw_tcp_recv_data_work() runs it from a
work item with softirqs enabled when iscsi_recv_from_iscsi_q is set, and
every process context that takes back_lock in libiscsi takes it that way.

A target that holds part of a Data-In until the command's LUN has been
reset, and then sends the rest, gets the copy to run through the freed
scatterlist and KASAN to report it:

  BUG: KASAN: slab-use-after-free in iscsi_tcp_segment_done+0xe4b/0x1490
  Read of size 4 at addr ffff88800d44cc8c by task swapper/0/0

  CPU: 0 UID: 0 PID: 0 Comm: swapper/0 Tainted: G    B               7.4.0-rc2-00478-ga189f19ef663 #10 PREEMPT(lazy)
  Tainted: [B]=BAD_PAGE
  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
  Call Trace:
   <IRQ>
   dump_stack_lvl+0x53/0x70
   print_report+0xd0/0x630
   kasan_report+0xce/0x100
   iscsi_tcp_segment_done+0xe4b/0x1490
   iscsi_tcp_recv_skb+0x924/0x15f0
   iscsi_sw_tcp_recv+0x12f/0x390
   __tcp_read_sock+0x1ab/0x810
   iscsi_sw_tcp_data_ready+0x18b/0x510
   tcp_data_queue+0x1f2d/0x4b70
   tcp_rcv_established+0x8a8/0x3990
   tcp_v4_do_rcv+0x45b/0x970
   tcp_v4_rcv+0x223d/0x3b90
   ip_protocol_deliver_rcu+0x61/0x2e0
   ip_local_deliver_finish+0x329/0x4b0
   ip_local_deliver+0x18f/0x2d0
   ip_sublist_rcv_finish+0x138/0x1b0
   ip_sublist_rcv+0x3ae/0x670
   ip_list_rcv+0x266/0x380
   __netif_receive_skb_list_core+0x5c3/0x860
   netif_receive_skb_list_internal+0x553/0xbb0
   gro_complete.constprop.0+0x3ed/0x5c0
   dev_gro_receive+0xa31/0x23c0
   gro_receive_skb+0x279/0x960
   e1000_clean_rx_irq+0x602/0x1160
   e1000_clean+0x7eb/0x25a0
   __napi_poll+0x9c/0x520
   net_rx_action+0x8f1/0xfb0
   handle_softirqs+0x1b3/0x600
   __irq_exit_rcu+0xfc/0x160
   common_interrupt+0x7f/0x90
   </IRQ>

  Allocated by task 75:
   kasan_save_stack+0x33/0x60
   kasan_save_track+0x14/0x30
   __kasan_slab_alloc+0x6e/0x70
   kmem_cache_alloc_noprof+0x10e/0x340
   mempool_alloc_noprof+0xcd/0x180
   __sg_alloc_table+0x23e/0x410
   sg_alloc_table_chained+0x47/0x180
   scsi_alloc_sgtables+0x17f/0xc50
   sd_init_command+0x20e/0x2e70
   scsi_queue_rq+0x1070/0x2f60
   blk_mq_dispatch_rq_list+0x390/0x2100
   __blk_mq_sched_dispatch_requests+0xa7a/0x1550
   blk_mq_sched_dispatch_requests+0xa7/0x140
   blk_mq_run_work_fn+0x105/0x150
   process_one_work+0x697/0x1060
   worker_thread+0x45b/0xd10
   kthread+0x2c8/0x3b0
   ret_from_fork+0x36e/0x5a0
   ret_from_fork_asm+0x1a/0x30

  Freed by task 130:
   kasan_save_stack+0x33/0x60
   kasan_save_track+0x14/0x30
   kasan_save_free_info+0x3b/0x60
   __kasan_slab_free+0x43/0x70
   kmem_cache_free+0xba/0x3a0
   mempool_free+0xbd/0x110
   __sg_free_table+0xa8/0x170
   scsi_end_request+0x329/0x5d0
   scsi_io_completion+0x4b0/0xda0
   blk_complete_reqs+0xa4/0xe0
   handle_softirqs+0x1b3/0x600
   do_softirq+0x3d/0x60
   __local_bh_enable_ip+0x6a/0x70
   iscsi_eh_device_reset+0x7f1/0xa10
   scsi_ioctl_reset+0x3a8/0x5b0
   scsi_ioctl+0x7ac/0x1360
   blkdev_ioctl+0x385/0x520
   __x64_sys_ioctl+0x134/0x1c0
   do_syscall_64+0xdf/0x4c0
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

  The buggy address belongs to the object at ffff88800d44cc80
   which belongs to the cache sgpool-16 of size 512
  The buggy address is located 12 bytes inside of
   freed 512-byte region [ffff88800d44cc80, ffff88800d44ce80)

Fixes: da32dd681f7a ("[SCSI] iscsi_tcp: rewrite recv path")
Cc: stable@vger.kernel.org
Signed-off-by: Yehyeong Lee <yhlee@isslab.korea.ac.kr>
---

Reproduced against a userspace target reached through a proxy that holds
a 64 KiB read's whole Data-In, lets a LUN reset through, and then sends
the header with the first 2048 bytes and the remaining 63488 bytes after
a gap.  A test-only delay parks the receive path for 20 ms once it has
reached the copy, and the reset path waits for that park before it fails
the tasks, so that the command is given back inside the window.  The
receive softirq is kept on one cpu and the reader and the reset on
others.  Every boot is a fresh one, under KASAN.

Measured on top of the series named below.  Without this patch all 3
boots report, 51 reports each, of which 4 name the freed scatterlist and
the rest are the copy writing through it and into the freed data pages.
With it none of 5 do: the reset waits on back_lock until the copy has
finished, the 63488 bytes land in the command's own buffer, and the reset
then fails the command as it asked to.  A 64 KiB read through the same
proxy with the injection turned off is unaffected in both arms.  Ten
32 MiB direct reads, on kernels built without the test delays and without
KASAN, take 1.23 to 1.39 s without the patch and 1.23 to 1.34 s with it,
three boots of three measurements each per arm.

The report above is the first one that names the scatterlist.  The boot's
first report is the same copy writing into a freed data page, which is
also what set the taint.

This is a follow-up to "scsi: libiscsi_tcp: do not accept a Data-In for a
finished command", which refused a Data-In whose command had already gone
but argued that while a copy is pending only the receive path can finish
the task.  The error handler does too, which is what this patch answers.

  https://lore.kernel.org/r/20261117101500.1894608-5-yhlee@isslab.korea.ac.kr

Sashiko raised the same case on that patch:

  https://lore.kernel.org/all/sashiko-outbox-218654@kernel.org/

Applies on its own, and on top of that series, to mkp-scsi/for-next
(08fec21faae4), origin/master (65e4fd942f72) and next/master
(adcad7f4e6332c).

 drivers/scsi/libiscsi_tcp.c | 37 +++++++++++++++++++++++++++++++++++++
 include/scsi/libiscsi_tcp.h |  7 +++++++
 2 files changed, 44 insertions(+)

diff --git a/drivers/scsi/libiscsi_tcp.c b/drivers/scsi/libiscsi_tcp.c
index eeb853c6b66d49..79489793b076d7 100644
--- a/drivers/scsi/libiscsi_tcp.c
+++ b/drivers/scsi/libiscsi_tcp.c
@@ -442,9 +442,17 @@ iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
  */
 void iscsi_tcp_cleanup_task(struct iscsi_task *task)
 {
+	struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
 	struct iscsi_tcp_task *tcp_task = task->dd_data;
 	struct iscsi_r2t_info *r2t;
 
+	/*
+	 * Runs under back_lock just before the command is given back, and
+	 * the receive path copies into its scatterlist under the same lock.
+	 */
+	if (tcp_conn->in.datain_task == task)
+		tcp_conn->in.datain_task = NULL;
+
 	/* nothing to do for mgmt */
 	if (!task->sc)
 		return;
@@ -745,6 +753,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,
@@ -908,8 +917,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);
@@ -926,6 +937,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;
@@ -958,6 +989,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);
@@ -970,6 +1005,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;
 }
diff --git a/include/scsi/libiscsi_tcp.h b/include/scsi/libiscsi_tcp.h
index ef53d4bea28a07..70967bd936e0bd 100644
--- a/include/scsi/libiscsi_tcp.h
+++ b/include/scsi/libiscsi_tcp.h
@@ -45,6 +45,13 @@ struct iscsi_tcp_recv {
 	struct iscsi_hdr	*hdr;
 	struct iscsi_segment	segment;
 
+	/*
+	 * The command a Data-In is being copied into.  Set when the segment
+	 * is aimed at its scatterlist and cleared when it is given back, both
+	 * with the session back_lock held.
+	 */
+	struct iscsi_task	*datain_task;
+
 	/* Allocate buffer for BHS + AHS */
 	uint32_t		hdr_buf[64];
 
-- 
2.43.0


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

* Re: [PATCH] scsi: libiscsi_tcp: hold back_lock across the copy of a Data-In
       [not found] ` <sashiko-outbox-225445@kernel.org>
@ 2026-11-20 16:39   ` Yehyeong Lee
  0 siblings, 0 replies; 3+ messages in thread
From: Yehyeong Lee @ 2026-11-20 16:39 UTC (permalink / raw)
  To: Lee Duncan, Chris Leech, Mike Christie, Martin K . Petersen,
	James E . J . Bottomley
  Cc: linux-scsi, linux-kernel, sashiko-reviews

On Fri, Nov 20, 2026 at 03:15:55PM +0000, sashiko-bot@kernel.org wrote:
> 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.

It already does, in the series this patch follows up on: 3/4 makes
iscsi_itt_to_ctask() return NULL for ISCSI_TASK_PENDING and
ISCSI_TASK_FREE, and 4/4 adds the task->state != ISCSI_TASK_RUNNING test
to the ISCSI_OP_SCSI_DATA_IN arm of iscsi_tcp_hdr_dissect().  Against
plain mainline the PENDING case is real, but it predates this patch.

  https://lore.kernel.org/r/20261117101500.1894608-4-yhlee@isslab.korea.ac.kr
  https://lore.kernel.org/r/20261117101500.1894608-5-yhlee@isslab.korea.ac.kr

> 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?
[...]
> 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.

No.  iscsi_tcp_process_data_in() never touches segment->sg: it verifies
the digest and, for ISCSI_FLAG_DATA_STATUS, calls iscsi_complete_pdu(),
which takes back_lock itself and completes through __iscsi_complete_pdu()
like any other PDU; a task in ISCSI_TASK_PENDING is refused there by 3/4.
That a target can send status for an ITT whose command has been
transmitted is not new.  The digest path is the same answer: after the
splice, segment->data is the four-byte digest buffer, so the copy that
skips the test goes nowhere near the scatterlist.

> Does unlocking session->back_lock at skb_done leave segment->sg_mapped
> mapped with kmap_atomic()?
[...]
> spin_lock_bh(&session->back_lock) while inside kmap_atomic() /
> pagefault_disable() (which violates sleeping spinlock rules on
> CONFIG_PREEMPT_RT).

Not a problem, on either half.  On CONFIG_PREEMPT_RT kmap_atomic() does
migrate_disable() and pagefault_disable() rather than preempt_disable()
(include/linux/highmem-internal.h), so a spinlock_t may be taken under
it.  And a mapping of a page that has since been freed is not
dereferenced: the next skb hits the segment->sg test first, finds
datain_task gone and fails the connection before any copy, and
iscsi_sw_tcp_recv_data() tears the mapping down once tcp_read_sock()
returns.

> Does the conditional locking across copy_locked trigger a Clang
> -Wthread-safety-analysis warning when CONFIG_WARN_CONTEXT_ANALYSIS=y?

Correct, three of them, fixed in v2.  clang 23.1.6 with
CONFIG_WARN_CONTEXT_ANALYSIS=y gives one "spinlock
'conn->session->back_lock' is not held on every path through here" and
two "releasing spinlock 'conn->session->back_lock' that was not held" on
v1.  v2 moves the copy into a helper so the lock is taken and released on
one path, and the file builds clean.

  https://lore.kernel.org/all/20261120163857.3238489-1-yhlee@isslab.korea.ac.kr/

Best regards,
Yehyeong Lee

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

* Re: [PATCH] scsi: libiscsi_tcp: hold back_lock across the copy of a Data-In
  2026-11-20 14:52 [PATCH] scsi: libiscsi_tcp: hold back_lock across the copy of a Data-In Yehyeong Lee
       [not found] ` <sashiko-outbox-225445@kernel.org>
@ 2026-11-21  2:03 ` kernel test robot
  1 sibling, 0 replies; 3+ messages in thread
From: kernel test robot @ 2026-11-21  2:03 UTC (permalink / raw)
  To: Yehyeong Lee, Lee Duncan, Chris Leech, Mike Christie,
	Martin K . Petersen, James E . J . Bottomley
  Cc: llvm, oe-kbuild-all, linux-scsi, linux-kernel

Hi Yehyeong,

kernel test robot noticed the following build warnings:

[auto build test WARNING on linux-review/Yehyeong-Lee/scsi-libiscsi-only-complete-a-task-that-has-been-sent/20261117-191457]

url:    https://github.com/intel-lab-lkp/linux/commits/Yehyeong-Lee/scsi-libiscsi_tcp-hold-back_lock-across-the-copy-of-a-Data-In/20261120-235243
base:   https://github.com/intel-lab-lkp/linux Yehyeong-Lee/scsi-libiscsi-only-complete-a-task-that-has-been-sent/20261117-191457
patch link:    https://lore.kernel.org/r/20261120145243.3124991-1-yhlee%40isslab.korea.ac.kr
patch subject: [PATCH] scsi: libiscsi_tcp: hold back_lock across the copy of a Data-In
config: loongarch-defconfig (https://download.01.org/0day-ci/archive/20261121/202611210912.K4ornj7h-lkp@intel.com/config)
compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project 3b14d7c370558b46e5f0c6494a05c492e59bf32c)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261121/202611210912.K4ornj7h-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202611210912.K4ornj7h-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> drivers/scsi/libiscsi_tcp.c:960:6: warning: spinlock 'conn->session->back_lock' is not held on every path through here [-Wthread-safety-analysis]
     960 |         if (offloaded) {
         |             ^
   drivers/scsi/libiscsi_tcp.c:947:3: note: spinlock acquired here
     947 |                 spin_lock_bh(&session->back_lock);
         |                 ^
>> drivers/scsi/libiscsi_tcp.c:993:3: warning: releasing spinlock 'conn->session->back_lock' that was not held [-Wthread-safety-analysis]
     993 |                 spin_unlock_bh(&session->back_lock);
         |                 ^
   drivers/scsi/libiscsi_tcp.c:1009:3: warning: releasing spinlock 'conn->session->back_lock' that was not held [-Wthread-safety-analysis]
    1009 |                 spin_unlock_bh(&session->back_lock);
         |                 ^
   3 warnings generated.


vim +960 drivers/scsi/libiscsi_tcp.c

a081c13e39b5c1 Mike Christie 2008-12-02   903  
a081c13e39b5c1 Mike Christie 2008-12-02   904  /**
a081c13e39b5c1 Mike Christie 2008-12-02   905   * iscsi_tcp_recv_skb - Process skb
a081c13e39b5c1 Mike Christie 2008-12-02   906   * @conn: iscsi connection
a081c13e39b5c1 Mike Christie 2008-12-02   907   * @skb: network buffer with header and/or data segment
a081c13e39b5c1 Mike Christie 2008-12-02   908   * @offset: offset in skb
ccd4a43035090c Randy Dunlap  2017-12-22   909   * @offloaded: bool indicating if transfer was offloaded
ccd4a43035090c Randy Dunlap  2017-12-22   910   * @status: iscsi TCP status result
a081c13e39b5c1 Mike Christie 2008-12-02   911   *
ccd4a43035090c Randy Dunlap  2017-12-22   912   * Will return status of transfer in @status. And will return
a081c13e39b5c1 Mike Christie 2008-12-02   913   * number of bytes copied.
a081c13e39b5c1 Mike Christie 2008-12-02   914   */
a081c13e39b5c1 Mike Christie 2008-12-02   915  int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
a081c13e39b5c1 Mike Christie 2008-12-02   916  		       unsigned int offset, bool offloaded, int *status)
a081c13e39b5c1 Mike Christie 2008-12-02   917  {
a081c13e39b5c1 Mike Christie 2008-12-02   918  	struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
a081c13e39b5c1 Mike Christie 2008-12-02   919  	struct iscsi_segment *segment = &tcp_conn->in.segment;
0d79b38099098f Yehyeong Lee  2026-11-20   920  	struct iscsi_session *session = conn->session;
a081c13e39b5c1 Mike Christie 2008-12-02   921  	struct skb_seq_state seq;
a081c13e39b5c1 Mike Christie 2008-12-02   922  	unsigned int consumed = 0;
0d79b38099098f Yehyeong Lee  2026-11-20   923  	bool copy_locked = false;
a081c13e39b5c1 Mike Christie 2008-12-02   924  	int rc = 0;
a081c13e39b5c1 Mike Christie 2008-12-02   925  
0ab1c2529e6a70 Mike Christie 2009-03-05   926  	ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
d1acfae514425d Mike Christie 2009-05-13   927  	/*
d1acfae514425d Mike Christie 2009-05-13   928  	 * Update for each skb instead of pdu, because over slow networks a
d1acfae514425d Mike Christie 2009-05-13   929  	 * data_in's data could take a while to read in. We also want to
d1acfae514425d Mike Christie 2009-05-13   930  	 * account for r2ts.
d1acfae514425d Mike Christie 2009-05-13   931  	 */
d1acfae514425d Mike Christie 2009-05-13   932  	conn->last_recv = jiffies;
a081c13e39b5c1 Mike Christie 2008-12-02   933  
5bd856256f8c03 Mike Christie 2022-04-07   934  	if (unlikely(test_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags))) {
0ab1c2529e6a70 Mike Christie 2009-03-05   935  		ISCSI_DBG_TCP(conn, "Rx suspended!\n");
a081c13e39b5c1 Mike Christie 2008-12-02   936  		*status = ISCSI_TCP_SUSPENDED;
a081c13e39b5c1 Mike Christie 2008-12-02   937  		return 0;
a081c13e39b5c1 Mike Christie 2008-12-02   938  	}
a081c13e39b5c1 Mike Christie 2008-12-02   939  
0d79b38099098f Yehyeong Lee  2026-11-20   940  	/*
0d79b38099098f Yehyeong Lee  2026-11-20   941  	 * Hold back_lock across a Data-In copy so the command cannot be
0d79b38099098f Yehyeong Lee  2026-11-20   942  	 * given back while it runs.  _bh: this also runs from a work item.
0d79b38099098f Yehyeong Lee  2026-11-20   943  	 */
0d79b38099098f Yehyeong Lee  2026-11-20   944  	if (segment->sg) {
0d79b38099098f Yehyeong Lee  2026-11-20   945  		struct iscsi_task *task;
0d79b38099098f Yehyeong Lee  2026-11-20   946  
0d79b38099098f Yehyeong Lee  2026-11-20   947  		spin_lock_bh(&session->back_lock);
0d79b38099098f Yehyeong Lee  2026-11-20   948  		task = tcp_conn->in.datain_task;
0d79b38099098f Yehyeong Lee  2026-11-20   949  		if (!task || task->state != ISCSI_TASK_RUNNING) {
0d79b38099098f Yehyeong Lee  2026-11-20   950  			spin_unlock_bh(&session->back_lock);
0d79b38099098f Yehyeong Lee  2026-11-20   951  			iscsi_conn_printk(KERN_ERR, conn,
0d79b38099098f Yehyeong Lee  2026-11-20   952  					  "Data-In for a command that is gone\n");
0d79b38099098f Yehyeong Lee  2026-11-20   953  			*status = ISCSI_TCP_CONN_ERR;
0d79b38099098f Yehyeong Lee  2026-11-20   954  			iscsi_conn_failure(conn, ISCSI_ERR_BAD_ITT);
0d79b38099098f Yehyeong Lee  2026-11-20   955  			return 0;
0d79b38099098f Yehyeong Lee  2026-11-20   956  		}
0d79b38099098f Yehyeong Lee  2026-11-20   957  		copy_locked = true;
0d79b38099098f Yehyeong Lee  2026-11-20   958  	}
0d79b38099098f Yehyeong Lee  2026-11-20   959  
a081c13e39b5c1 Mike Christie 2008-12-02  @960  	if (offloaded) {
a081c13e39b5c1 Mike Christie 2008-12-02   961  		segment->total_copied = segment->total_size;
a081c13e39b5c1 Mike Christie 2008-12-02   962  		goto segment_done;
a081c13e39b5c1 Mike Christie 2008-12-02   963  	}
a081c13e39b5c1 Mike Christie 2008-12-02   964  
a081c13e39b5c1 Mike Christie 2008-12-02   965  	skb_prepare_seq_read(skb, offset, skb->len, &seq);
a081c13e39b5c1 Mike Christie 2008-12-02   966  	while (1) {
a081c13e39b5c1 Mike Christie 2008-12-02   967  		unsigned int avail;
a081c13e39b5c1 Mike Christie 2008-12-02   968  		const u8 *ptr;
a081c13e39b5c1 Mike Christie 2008-12-02   969  
a081c13e39b5c1 Mike Christie 2008-12-02   970  		avail = skb_seq_read(consumed, &ptr, &seq);
a081c13e39b5c1 Mike Christie 2008-12-02   971  		if (avail == 0) {
0ab1c2529e6a70 Mike Christie 2009-03-05   972  			ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
a081c13e39b5c1 Mike Christie 2008-12-02   973  				      consumed);
a081c13e39b5c1 Mike Christie 2008-12-02   974  			*status = ISCSI_TCP_SKB_DONE;
a081c13e39b5c1 Mike Christie 2008-12-02   975  			goto skb_done;
a081c13e39b5c1 Mike Christie 2008-12-02   976  		}
a081c13e39b5c1 Mike Christie 2008-12-02   977  		BUG_ON(segment->copied >= segment->size);
a081c13e39b5c1 Mike Christie 2008-12-02   978  
0ab1c2529e6a70 Mike Christie 2009-03-05   979  		ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
0ab1c2529e6a70 Mike Christie 2009-03-05   980  			      avail);
a081c13e39b5c1 Mike Christie 2008-12-02   981  		rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
a081c13e39b5c1 Mike Christie 2008-12-02   982  		BUG_ON(rc == 0);
a081c13e39b5c1 Mike Christie 2008-12-02   983  		consumed += rc;
a081c13e39b5c1 Mike Christie 2008-12-02   984  
a081c13e39b5c1 Mike Christie 2008-12-02   985  		if (segment->total_copied >= segment->total_size) {
a081c13e39b5c1 Mike Christie 2008-12-02   986  			skb_abort_seq_read(&seq);
a081c13e39b5c1 Mike Christie 2008-12-02   987  			goto segment_done;
a081c13e39b5c1 Mike Christie 2008-12-02   988  		}
a081c13e39b5c1 Mike Christie 2008-12-02   989  	}
a081c13e39b5c1 Mike Christie 2008-12-02   990  
a081c13e39b5c1 Mike Christie 2008-12-02   991  segment_done:
0d79b38099098f Yehyeong Lee  2026-11-20   992  	if (copy_locked) {
0d79b38099098f Yehyeong Lee  2026-11-20  @993  		spin_unlock_bh(&session->back_lock);
0d79b38099098f Yehyeong Lee  2026-11-20   994  		copy_locked = false;
0d79b38099098f Yehyeong Lee  2026-11-20   995  	}
a081c13e39b5c1 Mike Christie 2008-12-02   996  	*status = ISCSI_TCP_SEGMENT_DONE;
0ab1c2529e6a70 Mike Christie 2009-03-05   997  	ISCSI_DBG_TCP(conn, "segment done\n");
a081c13e39b5c1 Mike Christie 2008-12-02   998  	rc = segment->done(tcp_conn, segment);
a081c13e39b5c1 Mike Christie 2008-12-02   999  	if (rc != 0) {
a081c13e39b5c1 Mike Christie 2008-12-02  1000  		*status = ISCSI_TCP_CONN_ERR;
0ab1c2529e6a70 Mike Christie 2009-03-05  1001  		ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc);
a081c13e39b5c1 Mike Christie 2008-12-02  1002  		iscsi_conn_failure(conn, rc);
a081c13e39b5c1 Mike Christie 2008-12-02  1003  		return 0;
a081c13e39b5c1 Mike Christie 2008-12-02  1004  	}
a081c13e39b5c1 Mike Christie 2008-12-02  1005  	/* The done() functions sets up the next segment. */
a081c13e39b5c1 Mike Christie 2008-12-02  1006  
a081c13e39b5c1 Mike Christie 2008-12-02  1007  skb_done:
0d79b38099098f Yehyeong Lee  2026-11-20  1008  	if (copy_locked)
0d79b38099098f Yehyeong Lee  2026-11-20  1009  		spin_unlock_bh(&session->back_lock);
a081c13e39b5c1 Mike Christie 2008-12-02  1010  	conn->rxdata_octets += consumed;
a081c13e39b5c1 Mike Christie 2008-12-02  1011  	return consumed;
a081c13e39b5c1 Mike Christie 2008-12-02  1012  }
a081c13e39b5c1 Mike Christie 2008-12-02  1013  EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
a081c13e39b5c1 Mike Christie 2008-12-02  1014  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

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

end of thread, other threads:[~2026-11-21  2:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 14:52 [PATCH] scsi: libiscsi_tcp: hold back_lock across the copy of a Data-In Yehyeong Lee
     [not found] ` <sashiko-outbox-225445@kernel.org>
2026-11-20 16:39   ` Yehyeong Lee
2026-11-21  2:03 ` kernel test robot

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

all inboxes | Powered by JetHome®