[PATCH] usb: xhci: fix maximum event ring segments calculation

From: oushixiong1025

Date: Fri Aug 28 2026 - 07:00:11 EST


From: Shixiong Ou <oushixiong@xxxxxxxxxx>

Commit 6d45e9556d4a ("usb: xhci: standardize multi bit-field macros")
converted BIT(HCS_ERST_MAX(hcs_params2)) into
FIELD_GET(HCS_ERST_MAX, hcs_params2) << 2. The ERST Max field is a
power-of-two exponent, so the maximum number of segments is 2^n, not
n * 4.

With an ERST Max of 0 (as reported by e.g. QEMU xHCI) max_segs becomes
0, the event ring is allocated with 0 segments, and the resulting
zero-sized ERST allocation trips the page allocator:

WARNING: mm/page_alloc.c:5340 at __alloc_frozen_pages_noprof+0x11d0/0x1548, CPU#0: swapper/0/1
Call trace:
__alloc_frozen_pages_noprof+0x11d0/0x1548 (P)
alloc_pages_node_noprof+0xa0/0x108
__dma_direct_alloc_pages.isra.0+0x178/0x260
dma_direct_alloc+0x1e4/0x460
dma_alloc_attrs+0xb4/0x2e8
xhci_alloc_interrupter+0xdc/0x1c8
xhci_mem_init+0x250/0xe70
xhci_gen_setup+0x35c/0x4f0
xhci_pci_setup+0x68/0x138
usb_add_hcd+0x31c/0x640
usb_hcd_pci_probe+0x1fc/0x3d8
...
xhci_hcd 0000:02:00.0: Failed to allocate interrupter erst
xhci_hcd 0000:02:00.0: can't setup: -12

Restore the BIT() based calculation.

Fixes: 6d45e9556d4a ("usb: xhci: standardize multi bit-field macros")
Signed-off-by: Shixiong Ou <oushixiong@xxxxxxxxxx>
---
drivers/usb/host/xhci-mem.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 7a21ac81f9c8..af8d4b74c4ba 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -2301,7 +2301,7 @@ xhci_alloc_interrupter(struct xhci_hcd *xhci, unsigned int segs, gfp_t flags)
if (!segs)
segs = ERST_DEFAULT_SEGS;

- max_segs = FIELD_GET(HCS_ERST_MAX, xhci->hcs_params2) << 2;
+ max_segs = BIT(FIELD_GET(HCS_ERST_MAX, xhci->hcs_params2));
segs = min(segs, max_segs);

ir = kzalloc_node(sizeof(*ir), flags, dev_to_node(dev));
--
2.25.1


No virus found
Checked by Hillstone Network AntiVirus