From: Sasha Levin <sashal@kernel.org>
To: patches@lists.linux.dev, stable@vger.kernel.org
Cc: wangshuaiwei <wangshuaiwei1@xiaomi.com>,
Bart Van Assche <bvanassche@acm.org>,
"Martin K. Petersen" <martin.petersen@oracle.com>,
Sasha Levin <sashal@kernel.org>,
James.Bottomley@HansenPartnership.com,
linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH AUTOSEL 6.19-6.6] scsi: ufs: core: Fix shift out of bounds when MAXQ=32
Date: Thu, 5 Mar 2026 10:36:59 -0500 [thread overview]
Message-ID: <20260305153704.106918-16-sashal@kernel.org> (raw)
In-Reply-To: <20260305153704.106918-1-sashal@kernel.org>
From: wangshuaiwei <wangshuaiwei1@xiaomi.com>
[ Upstream commit 2f38fd99c0004676d835ae96ac4f3b54edc02c82 ]
According to JESD223F, the maximum number of queues (MAXQ) is 32. When MCQ
is enabled and ESI is disabled, nr_hw_queues=32 causes a shift overflow
problem.
Fix this by using 64-bit intermediate values to handle the nr_hw_queues=32
case safely.
Signed-off-by: wangshuaiwei <wangshuaiwei1@xiaomi.com>
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Link: https://patch.msgid.link/20260224063228.50112-1-wangshuaiwei1@xiaomi.com
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
LLM Generated explanations, may be completely bogus:
## Analysis
### What the commit fixes
This is a classic **undefined behavior (UB) / shift out of bounds** bug.
The expression `(1U << hba->nr_hw_queues) - 1` uses a 32-bit unsigned
integer literal `1U`. When `hba->nr_hw_queues == 32` (which is the
maximum allowed by the JESD223F UFS specification), the expression `1U
<< 32` is undefined behavior in C — shifting a 32-bit value by 32 or
more positions is explicitly undefined per the C standard.
The fix changes `1U` to `1ULL` (64-bit), making the shift well-defined
for values up to 63.
### Bug mechanism and impact
- **Variable:** `outstanding_cqs` is declared as `unsigned long` (64-bit
on 64-bit platforms)
- **Context:** This is a fallback path in the interrupt handler
`ufshcd_handle_mcq_cq_events()` — executed when
`ufshcd_vops_get_outstanding_cqs()` fails (vendor-specific register
not available)
- **Trigger:** Hardware with MAXQ=32 (the maximum allowed by UFS spec)
- **Consequence:** On such hardware, the undefined behavior could result
in `outstanding_cqs` being set to 0 instead of the intended bitmask of
all 1s (0xFFFFFFFF). This would mean **no completion queues get
serviced**, potentially causing I/O hangs or lost completions — a
severe storage subsystem issue.
### Stable kernel criteria assessment
1. **Obviously correct and tested:** Yes — a single-character change
(`U` → `ULL`), reviewed by Bart Van Assche (UFS maintainer). The fix
is trivially correct.
2. **Fixes a real bug:** Yes — undefined behavior that can cause I/O
failures on hardware with 32 queues.
3. **Important issue:** Yes — storage I/O hangs are critical. UFS is the
standard storage interface for mobile devices.
4. **Small and contained:** Yes — a single line change, single character
modification.
5. **No new features:** Correct — pure bug fix.
### Risk assessment
**Risk: Extremely low.** This is a one-character change from `1U` to
`1ULL`. It cannot introduce regressions — on hardware with fewer than 32
queues, the behavior is identical. On hardware with exactly 32 queues,
it fixes the undefined behavior.
### Affected versions
The buggy code was introduced in commit `f87b2c41822aa` ("scsi: ufs:
mcq: Add completion support of a CQE") which landed in v6.3 (merged
January 2023). All stable trees from 6.3 onward that include MCQ support
are affected.
### Verification
- **git blame** confirmed the buggy line `(1U << hba->nr_hw_queues) - 1`
originates from commit `f87b2c41822aa` (January 2023)
- **Code reading** confirmed `outstanding_cqs` is `unsigned long` and
`nr_hw_queues` is `unsigned int`, verifying the type mismatch concern
- **Read `ufs-mcq.c:174`** confirmed `hba_maxq` is derived from
`FIELD_GET(MAX_QUEUE_SUP, ...)` + 1, and per JESD223F the max is 32,
confirming `nr_hw_queues=32` is a valid hardware configuration
- **Read `ufs-mcq.c:193-219`** confirmed `hba->nr_hw_queues` is set to
the total number of queues which can reach `hba_maxq` (up to 32)
- **Reviewed-by: Bart Van Assche** — UFS subsystem expert confirms the
fix
- The commit applies to a single file with a trivial one-character
change
This is a textbook stable backport candidate: a one-character fix for
undefined behavior in a storage driver interrupt handler, with potential
for I/O hangs on compliant hardware. Minimal risk, clear correctness,
important subsystem.
**YES**
drivers/ufs/core/ufshcd.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 27d53a044dbad..f65b0aeef6dde 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -7094,7 +7094,7 @@ static irqreturn_t ufshcd_handle_mcq_cq_events(struct ufs_hba *hba)
ret = ufshcd_vops_get_outstanding_cqs(hba, &outstanding_cqs);
if (ret)
- outstanding_cqs = (1U << hba->nr_hw_queues) - 1;
+ outstanding_cqs = (1ULL << hba->nr_hw_queues) - 1;
/* Exclude the poll queues */
nr_queues = hba->nr_hw_queues - hba->nr_queues[HCTX_TYPE_POLL];
--
2.51.0
next prev parent reply other threads:[~2026-03-05 15:37 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-05 15:36 [PATCH AUTOSEL 6.19-6.18] scsi: ufs: core: Reset urgent_bkops_lvl to allow runtime PM power mode Sasha Levin
2026-03-05 15:36 ` [PATCH AUTOSEL 6.19-5.10] unshare: fix unshare_fs() handling Sasha Levin
2026-03-05 15:36 ` [PATCH AUTOSEL 6.19-6.12] drm/amdgpu/vcn5: Add SMU dpm interface type Sasha Levin
2026-03-05 15:36 ` [PATCH AUTOSEL 6.19-6.1] wifi: mac80211: set default WMM parameters on all links Sasha Levin
2026-03-05 15:36 ` [PATCH AUTOSEL 6.19-5.15] ALSA: usb-audio: Check max frame size for implicit feedback mode, too Sasha Levin
2026-03-05 15:36 ` [PATCH AUTOSEL 6.19-5.10] scsi: ses: Fix devices attaching to different hosts Sasha Levin
2026-03-05 15:36 ` [PATCH AUTOSEL 6.19-6.6] ASoC: cs42l43: Report insert for exotic peripherals Sasha Levin
2026-03-05 15:36 ` [PATCH AUTOSEL 6.19-5.15] ALSA: usb-audio: Avoid implicit feedback mode on DIYINHK USB Audio 2.0 Sasha Levin
2026-03-05 15:36 ` [PATCH AUTOSEL 6.19-5.10] ACPI: PM: Save NVS memory on Lenovo G70-35 Sasha Levin
2026-03-05 15:36 ` [PATCH AUTOSEL 6.19-5.10] scsi: storvsc: Fix scheduling while atomic on PREEMPT_RT Sasha Levin
2026-03-05 15:36 ` [PATCH AUTOSEL 6.19-6.1] ASoC: amd: yc: Add ASUS EXPERTBOOK BM1503CDA to quirk table Sasha Levin
2026-03-05 15:36 ` [PATCH AUTOSEL 6.19-5.10] ACPI: OSI: Add DMI quirk for Acer Aspire One D255 Sasha Levin
2026-03-05 15:36 ` [PATCH AUTOSEL 6.19-6.18] fs: init flags_valid before calling vfs_fileattr_get Sasha Levin
2026-03-05 15:36 ` [PATCH AUTOSEL 6.19-6.6] scsi: ufs: core: Fix possible NULL pointer dereference in ufshcd_add_command_trace() Sasha Levin
2026-03-05 15:36 ` [PATCH AUTOSEL 6.19-6.18] perf/core: Fix refcount bug and potential UAF in perf_mmap Sasha Levin
2026-03-05 15:36 ` Sasha Levin [this message]
2026-03-05 15:37 ` [PATCH AUTOSEL 6.19-5.15] scsi: mpi3mr: Add NULL checks when resetting request and reply queues Sasha Levin
2026-03-05 15:37 ` [PATCH AUTOSEL 6.19-6.12] ALSA: hda/realtek: Fix speaker pop on Star Labs StarFighter Sasha Levin
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=20260305153704.106918-16-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=James.Bottomley@HansenPartnership.com \
--cc=bvanassche@acm.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=martin.petersen@oracle.com \
--cc=patches@lists.linux.dev \
--cc=stable@vger.kernel.org \
--cc=wangshuaiwei1@xiaomi.com \
/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