From: sashiko-bot@kernel.org
To: alice.chao@mediatek.com
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH 6.18.y] scsi: ufs: core: Re-arm the device command completion before submitting
Date: Tue, 15 Sep 2026 05:36:26 +0000 [thread overview]
Message-ID: <20260915053627.311101F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260915052638.459390-1-alice.chao@mediatek.com>
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] ufs: data race on completion.done via reinit_completion()
--
commit bd0e388ed86b6eb0eec312ed2150d925d6dc5c8a
Author: Alice Chao <alice.chao@mediatek.com>
scsi: ufs: core: Re-arm the device command completion before submitting
This patch re-arms the hba->dev_cmd.complete completion immediately before
submitting a device command. This aims to prevent using a stale completion
from a previously timed-out command, particularly in MCQ mode.
> --- a/drivers/ufs/core/ufshcd.c
> +++ b/drivers/ufs/core/ufshcd.c
> @@ -3312,6 +3312,15 @@ static int ufshcd_issue_dev_cmd(struct ufs_hba *hba, struct ufshcd_lrb *lrbp,
> {
> int err;
>
> + /*
> + * A device command that timed out may still be completed by the
> + * controller later on. hba->dev_cmd.complete is shared by all device
> + * commands, so re-arm it here, immediately before submitting, to keep
> + * such a late completion from being mistaken for the completion of
> + * this command.
> + */
> + reinit_completion(&hba->dev_cmd.complete);
[Severity: Medium]
Could this introduce a data race with concurrent interrupt-driven completions?
Looking at ufshcd_compl_one_cqe(), if the completion queue entry for a
previously timed-out command arrives right as we submit a new one, the IRQ
handler will call complete() on this shared completion.
The complete() function takes x->wait.lock and performs a read-modify-write
on x->done. However, reinit_completion() performs an unlocked plain write
setting x->done to 0.
If these run concurrently:
CPU1 (CQE handler)
ufshcd_compl_one_cqe()
complete(&hba->dev_cmd.complete)
spin_lock_irqsave(&x->wait.lock, flags);
x->done++;
spin_unlock_irqrestore(&x->wait.lock, flags);
CPU2 (Submitter)
ufshcd_issue_dev_cmd()
reinit_completion(&hba->dev_cmd.complete)
x->done = 0;
Since ufshcd_issue_dev_cmd() only holds hba->dev_cmd.lock to serialize
submitters but doesn't synchronize with the asynchronous CQE handler, this
concurrent access to x->done violates the memory model and could lead to
lost or spurious completions.
Would it be safer to drain the completion instead, perhaps by looping
with try_wait_for_completion() rather than performing a plain write?
> +
> ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp->ucd_req_ptr);
> ufshcd_send_command(hba, tag, hba->dev_cmd_queue);
> err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260915052638.459390-1-alice.chao@mediatek.com?part=1
next prev parent reply other threads:[~2026-09-15 5:36 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-15 5:26 [PATCH 6.18.y] scsi: ufs: core: Re-arm the device command completion before submitting alice.chao
2026-09-15 5:36 ` sashiko-bot [this message]
2026-09-16 1:52 ` Sasha Levin
2026-10-05 8:34 ` Alice Chao
2026-10-05 11:54 ` Bean Huo
2026-10-06 9:55 ` Alice Chao
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=20260915053627.311101F000FF@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=alice.chao@mediatek.com \
--cc=linux-scsi@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox