ntb: ntb_tool: accept hexadecimal addresses in peer_mw_trans again
Subsystems (3)
PATCH v3 1/1 ntb: ntb_tool: accept hexadecimal addresses in peer_mw_trans again
Commit 21fbdc4d0b1e ("ntb/ntb_tool: correct sscanf format for u64 and size_t in tool_peer_mw_trans_write") changed the format of the peer_mw_trans parser from "%lli:%zi" to "%llu:%zu". Unlike %lli, the kernel's vsscanf() parses %llu in base 10 only, so an address written in hexadecimal is read as 0, the ':' in the format no longer matches, and the write fails with -EINVAL.
This breaks the usage documented at the top of ntb_tool.c ("echo 0xADD0BA5E:16384 > $DBG_DIR/peer0/peer_mw_trans0") and the NTB selftest. tools/testing/selftests/ntb/ntb_test.sh writes back the "DMA Address" that mw_trans<N> prints with %pad, i.e. in hexadecimal. As the address format comes from ntb_tool itself, the first memory window test fails this way independent of the NTB hardware driver:
Running mw0 tests on: 0000:00:04.0 / 0000:00:05.0 ntb_test.sh: line 117: echo: write error: Invalid argument
Split the input at the ':' and parse both fields with kstrtou64() and kstrtoul() in base 0. Both fields again take decimal, 0x-prefixed hexadecimal and 0-prefixed octal, as they did with "%lli:%zi" before that commit and as every other number ntb_tool reads does (kstrto*() with base 0, or %lli). Unlike with "%lli:%zi", a negative value, whitespace around a number and anything after a number (except the newline from echo) are rejected; a malformed size no longer reads as 0 and unmaps the window. Document the rule next to the usage example.
Found by running the NTB selftest against a virtual NTB device in QEMU; the fix was tested the same way: the unmodified selftest passes, hexadecimal addresses and sizes are accepted ("0x1000:0x1000" maps 4096 bytes), and "-1:16384", "0x1000:-1", "0x1000:16384x", "0x1000:", "0x1000" and ":16384" are rejected.
I directed an AI coding assistant (an LLM), which analysed the regression, drafted the fix and this changelog, and wrote and ran the QEMU tests; I reviewed and tested the result.
---
Notes:
v3:
- Parse the size with kstrtoul(base 0) as well, so "0x1000:0x1000" maps 0x1000 bytes instead of reading the size as 0 and unmapping, and anything after the size is rejected (Koichiro Den).
- Both fields now use base 0, as before 21fbdc4d0b1e and as the rest of ntb_tool; document that in the usage comment (Koichiro Den, Dave Jiang).
- Describe the AI assistance in the changelog instead of the tag used in v1 and v2, following Documentation/process/generated-content.rst.
v2:
- Keep rejecting a negative address: split at ':' with strsep() and parse the address with kstrtou64(base 0) instead of going back to %lli (Koichiro Den).
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
drivers/ntb/test/ntb_tool.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/ntb/test/ntb_tool.c +17 -3
@@ -168,6 +168,8 @@* root@peer# head -c 7 $DBG_DIR/peer0/mw0 * * # Map outbound memory window/check it settings (on peer device) +* # (address:size; like the other numbers here, each may be decimal, +* # 0x-prefixed hexadecimal or 0-prefixed octal; a size of 0 unmaps) * root@peer# echo 0xADD0BA5E:16384 > $DBG_DIR/peer0/peer_mw_trans0 * root@peer# cat $DBG_DIR/peer0/peer_mw_trans0 *
@@ -926,8 +928,10 @@ static ssize_t tool_peer_mw_trans_write(struct file *filep,struct tool_mw *outmw = outmw_wrap->mw;size_t buf_size, wsize;char buf[TOOL_BUF_LEN ]; -int ret, n; +char *addr_str, *size_str; +unsigned long val;u64 addr; +int ret; buf_size =min (size, (sizeof (buf) -1 ));if (copy_from_user (buf, ubuf, buf_size))
@@ -935,10 +939,20 @@ static ssize_t tool_peer_mw_trans_write(struct file *filep, buf[buf_size] ='\0' ; - n =sscanf (buf,"%llu:%zu" , &addr, &wsize); -if (n !=2 ) + size_str = buf; + addr_str =strsep (&size_str,":" ); +if (!size_str)return -EINVAL ; + ret =kstrtou64 (addr_str,0 , &addr); +if (ret) +return ret; + + ret =kstrtoul (size_str,0 , &val); +if (ret) +return ret; + wsize = val; +tool_free_peer_mw (outmw->tc, outmw->widx);if (wsize) { ret = tool_setup_peer_mw(outmw->tc, outmw_wrap->pidx,
Signature · 1 line
2.34.1