All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Qiliang Yuan" <qiliang.yuan@linux.dev>
Cc: linux-scsi@vger.kernel.org, target-devel@vger.kernel.org
Subject: Re: [PATCH RFC 10/18] scsi: target: uring: Time out commands the server does not complete
Date: Mon, 16 Nov 2026 00:41:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-215964@kernel.org> (raw)
In-Reply-To: <20261115-feat-target-core-uring-v1-10-28cdc137eac6@linux.dev>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] scsi: target: uring: schedule_delayed_work() race in
  tcmur_timeout_work()
- [Medium] scsi: target: uring: UAF race scheduling timeout_work in
  tcmur_queue_cmd()
- [Low] scsi: target: uring: 32-bit overflow in cmd_timeout * HZ in
  tcmur_queue_cmd()
- [Low] scsi: target: uring: unlocked deadline calculation in
  tcmur_queue_cmd()
--

commit 2624a7f3c24be9a1330c0e703d8b3102658c07d2
Author: Qiliang Yuan <qiliang.yuan@linux.dev>

scsi: target: uring: Time out commands the server does not complete

This commit adds a configurable command timeout (cmd_time_out, 30 seconds
by default) and a delayed work item to fail pending or in-flight commands
that the userspace server does not complete in time.

> diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
> index 2978dd08c99cf..0f83bdc55e06a 100644
> --- a/drivers/target/target_core_uring.c
> +++ b/drivers/target/target_core_uring.c
[ ... ]
> @@ -190,6 +196,8 @@ static void tcmur_queue_cmd(struct tcmur_dev *udev, struct tcmur_cmd *tcmd)
>  	struct tcmur_io *io;
>  
>  	refcount_set(&tcmd->ref, 1);
> +	if (udev->cmd_timeout)
> +		tcmd->deadline = jiffies + udev->cmd_timeout * HZ;

[Severity: Low]
Can udev->cmd_timeout * HZ overflow 32 bits here and in the
schedule_delayed_work() call below?

tcmur_set_configfs_dev_params() accepts any non-negative int for
cmd_time_out without an upper bound, and both udev->cmd_timeout and HZ are
32-bit integers.  A value above UINT_MAX / HZ wraps in 32-bit unsigned
arithmetic before being widened to unsigned long (and on 32-bit
architectures a value above LONG_MAX / HZ wraps time_after()), causing
commands to time out prematurely or immediately.

>  
>  	spin_lock(&q->lock);
>  	io = list_first_entry_or_null(&q->idle, struct tcmur_io, node);
> @@ -205,6 +213,9 @@ static void tcmur_queue_cmd(struct tcmur_dev *udev, struct tcmur_cmd *tcmd)
>  
>  	if (ucmd)
>  		io_uring_cmd_complete_in_task(ucmd, tcmur_fetch_tw_cb);
> +	if (udev->cmd_timeout)
> +		schedule_delayed_work(&udev->timeout_work,
> +				      udev->cmd_timeout * HZ);
>  }

[Severity: Medium]
Can scheduling udev->timeout_work after dropping q->lock race with
command completion and device teardown, causing a use-after-free on udev?

Once q->lock is released, the server can complete tcmd immediately on
another CPU, dropping the last LUN and device references while this thread
is preempted:

CPU0                              CPU1
tcmur_queue_cmd()
  spin_lock(&q->lock)
  tcmur_io_start(q, io, tcmd)
  spin_unlock(&q->lock)
                                  tcmur_commit()
                                    tcmur_cmd_end(tcmd, ...)
                                      ...
                                        target_complete_cmd()
                                  tcmur_destroy_device()
                                    cancel_delayed_work_sync(
                                      &udev->timeout_work)
                                  tcmur_free_device()
                                    ...
                                      kfree(udev)
  if (udev->cmd_timeout)
    schedule_delayed_work(
      &udev->timeout_work, ...)  <-- use-after-free / re-armed after cancel

Scheduling udev->timeout_work while holding q->lock avoids this race.

[ ... ]

> @@ -244,6 +255,58 @@ static sense_reason_t tcmur_execute_rw(struct se_cmd *se_cmd,
>  			    se_cmd->data_length, sgl, sgl_nents);
>  }
>  
> +static void tcmur_timeout_work(struct work_struct *work)
> +{
> +	struct tcmur_dev *udev = container_of(work, struct tcmur_dev,
> +					      timeout_work.work);
> +	unsigned long now = jiffies, next = 0;
> +	struct tcmur_cmd *tcmd, *tmp;
> +	struct tcmur_queue *q;
> +	struct tcmur_io *io;
> +	bool rearm = false;
> +	LIST_HEAD(expired);
> +	unsigned int i, j;
> +
> +	for (i = 0; i < udev->nr_queues; i++) {
> +		q = &udev->queues[i];
> +
> +		spin_lock(&q->lock);
> +		list_for_each_entry_safe(tcmd, tmp, &q->pending, node) {
> +			if (time_after(tcmd->deadline, now)) {
> +				if (!rearm || time_before(tcmd->deadline, next))
> +					next = tcmd->deadline;
> +				rearm = true;
> +				break;
> +			}
> +			list_move_tail(&tcmd->node, &expired);
> +		}

[Severity: Low]
Does breaking on the first non-expired command in q->pending skip expired
commands queued behind it?

In tcmur_queue_cmd(), tcmd->deadline is computed before acquiring q->lock.
If one CPU computes an earlier deadline and is delayed before taking
q->lock while another CPU computes a later deadline and appends to
q->pending first, q->pending is no longer ordered by deadline and
tcmur_timeout_work() stops at the later deadline until it expires.

> +		for (j = 0; j < udev->queue_depth; j++) {
> +			io = &q->ios[j];
> +			tcmd = io->cmd;
> +			if (!tcmd)
> +				continue;
> +			if (time_after(tcmd->deadline, now)) {
> +				if (!rearm || time_before(tcmd->deadline, next))
> +					next = tcmd->deadline;
> +				rearm = true;
> +				continue;
> +			}
> +			/* Leave the tag to the server until it commits it */
> +			io->cmd = NULL;
> +			list_add_tail(&tcmd->node, &expired);
> +		}
> +		spin_unlock(&q->lock);
> +	}
> +
> +	if (!list_empty(&expired))
> +		pr_warn_ratelimited("tcmur%d: command timed out\n",
> +				    udev->dev_id);
> +	tcmur_end_cmds(&expired, SAM_STAT_CHECK_CONDITION);
> +
> +	if (rearm)
> +		schedule_delayed_work(&udev->timeout_work, next - now);
> +}

[Severity: Medium]
Can a concurrent tcmur_queue_cmd() cause this schedule_delayed_work() call
to be ignored, delaying expiration of the oldest command by an extra
cmd_timeout period?

CPU0 (kworker)                    CPU1
tcmur_timeout_work()
  /* sees command due at next */
  rearm = true
                                  tcmur_queue_cmd()
                                    schedule_delayed_work(
                                      &udev->timeout_work,
                                      udev->cmd_timeout * HZ)
  schedule_delayed_work(
    &udev->timeout_work,
    next - now)  <-- ignored (already pending for cmd_timeout * HZ)

Because WORK_STRUCT_PENDING_BIT is cleared when tcmur_timeout_work()
starts, CPU1's schedule_delayed_work() arms the timer for the full
cmd_timeout * HZ, and CPU0's schedule_delayed_work() returns false without
shortening the timer to next - now.  Using mod_delayed_work() when
rearming would update the timer to the earlier deadline.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261115-feat-target-core-uring-v1-0-28cdc137eac6@linux.dev?part=10

  reply	other threads:[~2026-11-16  0:41 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
2026-11-16  0:17 ` [PATCH RFC 01/18] scsi: target: uring: Introduce " Qiliang Yuan
2026-11-16  0:38   ` sashiko-bot
2026-11-16  0:17 ` [PATCH RFC 02/18] scsi: target: uring: Allocate per-queue command descriptors Qiliang Yuan
2026-11-16  0:17 ` [PATCH RFC 03/18] scsi: target: uring: Create a character device per backstore device Qiliang Yuan
2026-11-16  0:33   ` sashiko-bot
2026-11-16  0:17 ` [PATCH RFC 04/18] scsi: target: uring: Map the command descriptors into the server Qiliang Yuan
2026-11-16  0:17 ` [PATCH RFC 05/18] scsi: target: uring: Queue commands until a server fetches them Qiliang Yuan
2026-11-16  0:46   ` sashiko-bot
2026-11-16  0:17 ` [PATCH RFC 06/18] scsi: target: uring: Hand commands out through TCMUR_IO_FETCH Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 07/18] scsi: target: uring: Complete commands with TCMUR_IO_COMMIT_AND_FETCH Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 08/18] scsi: target: uring: Copy command data with pread() and pwrite() Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 09/18] scsi: target: uring: Fail in-flight commands when the server exits Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 10/18] scsi: target: uring: Time out commands the server does not complete Qiliang Yuan
2026-11-16  0:41   ` sashiko-bot [this message]
2026-11-16  0:18 ` [PATCH RFC 11/18] scsi: target: uring: Drop aborted commands that are still queued Qiliang Yuan
2026-11-16  0:37   ` sashiko-bot
2026-11-16  0:18 ` [PATCH RFC 12/18] scsi: target: uring: Support SYNCHRONIZE CACHE Qiliang Yuan
2026-11-16  0:40   ` sashiko-bot
2026-11-16  0:18 ` [PATCH RFC 13/18] scsi: target: uring: Implement UNMAP Qiliang Yuan
2026-11-16  0:45   ` sashiko-bot
2026-11-16  0:18 ` [PATCH RFC 14/18] scsi: target: uring: Handle WRITE SAME Qiliang Yuan
2026-11-16  0:44   ` sashiko-bot
2026-11-16  0:18 ` [PATCH RFC 15/18] scsi: target: uring: Register command data as io_uring fixed buffers Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 16/18] selftests: target_core_uring: Add a minimal server Qiliang Yuan
2026-11-16  1:03   ` sashiko-bot
2026-11-16  0:18 ` [PATCH RFC 17/18] selftests: target_core_uring: Test a device exported through tcm_loop Qiliang Yuan
2026-11-16  0:49   ` sashiko-bot
2026-11-16  0:18 ` [PATCH RFC 18/18] Documentation: target: Describe the uring backstore Qiliang Yuan
2026-11-16  2:32 ` [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Ming Lei
2026-11-17  2:46   ` Qiliang Yuan
2026-11-18  1:40     ` Ming Lei

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-215964@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=qiliang.yuan@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=target-devel@vger.kernel.org \
    /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.