Skip to contentSkip to conversation

xhci: Use ffs() to find page size in xhci_mem_init()

PATCH Superseded Andrey Smirnov 4 participants 7 messages Feb 7, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xhci: Use ffs() to find page size in xhci_mem_init()

Get page size order using ffs() instead of open coding it with a loop.

---
 drivers/usb/host/xhci-mem.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/usb/host/xhci-mem.c +1 -5
index 36a3eb8849f1..44b43c3d819f 100644
@@ -2362,11 +2362,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);
-	for (i = 0; i < 16; i++) {
-		if ((0x1 & page_size) != 0)
-			break;
-		page_size = page_size >> 1;
-	}
+	i = ffs(page_size);
 	if (i < 16)
 		xhci_dbg_trace(xhci, trace_xhci_dbg_init,
 			"Supported page size of %iK", (1 << (i+12)) / 1024);
#1 lore raw
Quoted 18 lines · Andrey Smirnov

Get page size order using ffs() instead of open coding it with a loop.

---
  drivers/usb/host/xhci-mem.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
drivers/usb/host/xhci-mem.c +0 -0
index 36a3eb8849f1..44b43c3d819f 100644
@@ -2362,11 +2362,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,

drivers/usb/host/xhci-mem.c int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags) · line 2368

Quoted 10 lines
drivers/usb/host/xhci-mem.c +1 -5
  			"Supported page size register = 0x%x", page_size);
-	for (i = 0; i < 16; i++) {
-		if ((0x1 & page_size) != 0)
-			break;
-		page_size = page_size >> 1;
-	}
+	i = ffs(page_size);
  	if (i < 16)
  		xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  			"Supported page size of %iK", (1 << (i+12)) / 1024);

Hi

using ffs() is a welcome change, but it will give different a result than the loop.

*old loop
valid page_size value if i < 16
*ffs()
valid page_size value if i >= 1 and i < 17

-Mathias

#2 lore raw

Hi,

Quoted 32 lines · Mathias Nyman
Quoted 18 lines · Andrey Smirnov

Get page size order using ffs() instead of open coding it with a loop.

---
  drivers/usb/host/xhci-mem.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
drivers/usb/host/xhci-mem.c +0 -0
index 36a3eb8849f1..44b43c3d819f 100644
@@ -2362,11 +2362,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,

drivers/usb/host/xhci-mem.c int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags) · line 2368

Quoted 10 lines
drivers/usb/host/xhci-mem.c +1 -5
  			"Supported page size register = 0x%x", page_size);
-	for (i = 0; i < 16; i++) {
-		if ((0x1 & page_size) != 0)
-			break;
-		page_size = page_size >> 1;
-	}
+	i = ffs(page_size);
  	if (i < 16)
  		xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  			"Supported page size of %iK", (1 << (i+12)) / 1024);

Hi

Quoted 6 lines

using ffs() is a welcome change, but it will give different a result than the loop.

*old loop
valid page_size value if i < 16
*ffs()
valid page_size value if i >= 1 and i < 17

off-by-one, just use i = ffs() - 1. Or use __ffs().

maintainer
#3 lore raw
Quoted 36 lines · Felipe Balbi

Hi,

Quoted 32 lines · Mathias Nyman
Quoted 18 lines · Andrey Smirnov

Get page size order using ffs() instead of open coding it with a loop.

---
   drivers/usb/host/xhci-mem.c | 6 +-----
   1 file changed, 1 insertion(+), 5 deletions(-)
drivers/usb/host/xhci-mem.c +0 -0
index 36a3eb8849f1..44b43c3d819f 100644
@@ -2362,11 +2362,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,

drivers/usb/host/xhci-mem.c int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags) · line 2368

Quoted 10 lines
drivers/usb/host/xhci-mem.c +1 -5
   			"Supported page size register = 0x%x", page_size);
-	for (i = 0; i < 16; i++) {
-		if ((0x1 & page_size) != 0)
-			break;
-		page_size = page_size >> 1;
-	}
+	i = ffs(page_size);
   	if (i < 16)
   		xhci_dbg_trace(xhci, trace_xhci_dbg_init,
   			"Supported page size of %iK", (1 << (i+12)) / 1024);

Hi

Quoted 9 lines
Quoted 6 lines

using ffs() is a welcome change, but it will give different a result than the loop.

*old loop
valid page_size value if i < 16
*ffs()
valid page_size value if i >= 1 and i < 17

off-by-one, just use i = ffs() - 1. Or use __ffs().

and handle the page_size == 0 case.

-Mathias

#4 lore raw

Hi,

Quoted 33 lines · Mathias Nyman
Quoted 33 lines
Quoted 33 lines
Quoted 18 lines

Get page size order using ffs() instead of open coding it with a loop.

---
   drivers/usb/host/xhci-mem.c | 6 +-----
   1 file changed, 1 insertion(+), 5 deletions(-)
drivers/usb/host/xhci-mem.c +0 -0
index 36a3eb8849f1..44b43c3d819f 100644
@@ -2362,11 +2362,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,

drivers/usb/host/xhci-mem.c int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags) · line 2368

Quoted 10 lines
drivers/usb/host/xhci-mem.c +1 -5
   			"Supported page size register = 0x%x", page_size);
-	for (i = 0; i < 16; i++) {
-		if ((0x1 & page_size) != 0)
-			break;
-		page_size = page_size >> 1;
-	}
+	i = ffs(page_size);
   	if (i < 16)
   		xhci_dbg_trace(xhci, trace_xhci_dbg_init,
   			"Supported page size of %iK", (1 << (i+12)) / 1024);

Hi

using ffs() is a welcome change, but it will give different a result than the loop.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

*old loop
valid page_size value if i < 16
*ffs()
valid page_size value if i >= 1 and i < 17

off-by-one, just use i = ffs() - 1. Or use __ffs().

and handle the page_size == 0 case.

Can it be zero in real life, or are you protecting against academic possibility that's never going to happen in HW?

#5 lore raw
Quoted 42 lines · Felipe Balbi

Hi,

Quoted 38 lines · Mathias Nyman
Quoted 38 lines
Quoted 37 lines
Quoted 18 lines

Get page size order using ffs() instead of open coding it with a loop.

---
    drivers/usb/host/xhci-mem.c | 6 +-----
    1 file changed, 1 insertion(+), 5 deletions(-)
drivers/usb/host/xhci-mem.c +0 -0
index 36a3eb8849f1..44b43c3d819f 100644
@@ -2362,11 +2362,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,

drivers/usb/host/xhci-mem.c int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags) · line 2368

Quoted 10 lines
drivers/usb/host/xhci-mem.c +1 -5
    			"Supported page size register = 0x%x", page_size);
-	for (i = 0; i < 16; i++) {
-		if ((0x1 & page_size) != 0)
-			break;
-		page_size = page_size >> 1;
-	}
+	i = ffs(page_size);
    	if (i < 16)
    		xhci_dbg_trace(xhci, trace_xhci_dbg_init,
    			"Supported page size of %iK", (1 << (i+12)) / 1024);

Hi

using ffs() is a welcome change, but it will give different a result than the loop.

*old loop
valid page_size value if i < 16
*ffs()
valid page_size value if i >= 1 and i < 17

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

off-by-one, just use i = ffs() - 1. Or use __ffs().

and handle the page_size == 0 case.

Can it be zero in real life, or are you protecting against academic possibility that's never going to happen in HW?

whole page_size check is not really doing much, just printing out different debug messages.

-Mathias

#6 lore raw
In reply to Mathias Nyman
Quoted 33 lines · Mathias Nyman
Quoted 18 lines · Andrey Smirnov

Get page size order using ffs() instead of open coding it with a loop.

---
  drivers/usb/host/xhci-mem.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
drivers/usb/host/xhci-mem.c +0 -0
index 36a3eb8849f1..44b43c3d819f 100644
@@ -2362,11 +2362,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,

drivers/usb/host/xhci-mem.c int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags) · line 2368

Quoted 10 lines
drivers/usb/host/xhci-mem.c +1 -5
                      "Supported page size register = 0x%x", page_size);
-     for (i = 0; i < 16; i++) {
-             if ((0x1 & page_size) != 0)
-                     break;
-             page_size = page_size >> 1;
-     }
+     i = ffs(page_size);
      if (i < 16)
              xhci_dbg_trace(xhci, trace_xhci_dbg_init,
                      "Supported page size of %iK", (1 << (i+12)) / 1024);

Hi

Quoted 7 lines

using ffs() is a welcome change, but it will give different a result than the loop.

*old loop
valid page_size value if i < 16
*ffs()
valid page_size value if i >= 1 and i < 17

My bad, sorry about that, was using __fls() in the version I tested, but then switched it to ffs() at the last minute and forgot to double check. Will update in v2.

Thanks,
Andrey Smirnov