diff mbox series

[v2,1/2] usb: xhci: correct debug message page size calculation

Message ID 20250113143037.2131346-2-niklas.neronin@linux.intel.com (mailing list archive)
State New
Headers show
Series usb: xhci: page size improvements | expand

Commit Message

Neronin, Niklas Jan. 13, 2025, 2:30 p.m. UTC
The ffs() function returns the index of the first set bit, starting from 1.
If no bits are set, it returns zero. This behavior causes an off-by-one
page size in the debug message, as the page size calculation [1]
is zero-based, while ffs() is one-based.

Fix this by subtracting one from the result of ffs(). Note that since
variable 'i' is unsigned, subtracting one from zero will result in the
maximum unsigned integer value. Consequently, the condition 'if (i < 16)'
will still function correctly.

[1], Page size: (2^(n+12)), where 'n' is the set page size bit.

Fixes: 81720ec5320c ("usb: host: xhci: use ffs() in xhci_mem_init()")
Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
---
 drivers/usb/host/xhci-mem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Michał Pecio Jan. 13, 2025, 4 p.m. UTC | #1
On Mon, 13 Jan 2025 16:30:36 +0200, Niklas Neronin wrote:
> Fix this by subtracting one from the result of ffs(). Note that since
> variable 'i' is unsigned, subtracting one from zero will result in the
> maximum unsigned integer value. Consequently, the condition 'if (i <
> 16)' will still function correctly.

Is it unsigned? I see a simple 'int' here, so this wouldn't work.

Michal
Neronin, Niklas Jan. 14, 2025, 8:31 a.m. UTC | #2
On 13/01/2025 18.00, Michał Pecio wrote:
> On Mon, 13 Jan 2025 16:30:36 +0200, Niklas Neronin wrote:
>> Fix this by subtracting one from the result of ffs(). Note that since
>> variable 'i' is unsigned, subtracting one from zero will result in the
>> maximum unsigned integer value. Consequently, the condition 'if (i <
>> 16)' will still function correctly.
> 
> Is it unsigned? I see a simple 'int' here, so this wouldn't work.
> 

You are correct, it is not unsigned.
Thank you for catching it, don't know how I missed it.

Best Regards,
Niklas Neronin
diff mbox series

Patch

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 92703efda1f7..66584aafc513 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -2391,7 +2391,7 @@  int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
 	page_size = readl(&xhci->op_regs->page_size);
 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
 			"Supported page size register = 0x%x", page_size);
-	i = ffs(page_size);
+	i = ffs(page_size) - 1;
 	if (i < 16)
 		xhci_dbg_trace(xhci, trace_xhci_dbg_init,
 			"Supported page size of %iK", (1 << (i+12)) / 1024);