Skip to contentSkip to conversation

usb: early: xhci-dbc: Some fixes and speed-up

PATCH New Christophe JAILLET 1 participant 4 messages Jan 29, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover usb: early: xhci-dbc: Some fixes and speed-up

While looking at places were using memcpy_and_pad() could be useful, I spotted 1 issue (patch 1) and 1 potential optimization (patch 2).
The use of memcpy_and_pad() is in patch 3.

Christophe JAILLET (3):
usb: early: xhci-dbc: Fix a potential access to
usb: early: xhci-dbc: Optimize early_xdbc_write()
usb: early: xhci-dbc: Use memcpy_and_pad()

 drivers/usb/early/xhci-dbc.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 usb: early: xhci-dbc: Fix a potential out-of-bound memory access

If xdbc_bulk_write() fails, the values in 'buf' can be anything. So the string is not guaranteed to be NULL terminated when xdbc_trace() is called.

Reserve an extra byte, which will be zeroed automatically because 'buf' is a static variable, in order to avoid troubles, should it happen.

Fixes aeb9dd1de98c ("usb/early: Add driver for xhci debug capability")
---
 drivers/usb/early/xhci-dbc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/early/xhci-dbc.c +2 -1
index 797047154820..f3e23be227d4 100644
@@ -874,7 +874,8 @@ static int xdbc_bulk_write(const char *bytes, int size)
 
 static void early_xdbc_write(struct console *con, const char *str, u32 n)
 {
-	static char buf[XDBC_MAX_PACKET];
+	/* static variables are zeroed, so buf is always NULL terminated */
+	static char buf[XDBC_MAX_PACKET + 1];
 	int chunk, ret;
 	int use_cr = 0;
 
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/3 usb: early: xhci-dbc: Optimize early_xdbc_write()

In reply to Christophe JAILLET

There is no point in zeroing 'buf'.
It would be cleared only once, and if the 'while' loop is executed several times, all but the first run would have a 'dirty' buffer. Moreover, the size of the chunk is computed in the loop and this size is passed to xdbc_bulk_write().

So remove this useless memset().

---
Untested.

---
 drivers/usb/early/xhci-dbc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/early/xhci-dbc.c +1 -1
index f3e23be227d4..965a24e47c0f 100644
@@ -881,7 +881,7 @@ static void early_xdbc_write(struct console *con, const char *str, u32 n)
 
 	if (!xdbc.xdbc_reg)
 		return;
-	memset(buf, 0, XDBC_MAX_PACKET);
+
 	while (n > 0) {
 		for (chunk = 0; chunk < XDBC_MAX_PACKET && n > 0; str++, chunk++, n--) {
 
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/3 usb: early: xhci-dbc: Use memcpy_and_pad()

In reply to Christophe JAILLET

Instead of zeroing some memory and then copying data in part or all of it, use memcpy_and_pad().
This avoids writing some memory twice and should save a few cycles.

---
 drivers/usb/early/xhci-dbc.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/usb/early/xhci-dbc.c +1 -2
index 965a24e47c0f..341408410ed9 100644
@@ -499,8 +499,7 @@ static int xdbc_bulk_transfer(void *data, int size, bool read)
 		addr = xdbc.in_dma;
 		xdbc.flags |= XDBC_FLAGS_IN_PROCESS;
 	} else {
-		memset(xdbc.out_buf, 0, XDBC_MAX_PACKET);
-		memcpy(xdbc.out_buf, data, size);
+		memcpy_and_pad(xdbc.out_buf, XDBC_MAX_PACKET, data, size, 0);
 		addr = xdbc.out_dma;
 		xdbc.flags |= XDBC_FLAGS_OUT_PROCESS;
 	}
Signature · 1 line
2.34.1