The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2] ntb: ntb_tool: accept hexadecimal addresses in peer_mw_trans again
@ 2026-11-27 15:41 Oguzhan KAYNAK
  2026-11-30 12:32 ` Koichiro Den
  0 siblings, 1 reply; 2+ messages in thread
From: Oguzhan KAYNAK @ 2026-11-27 15:41 UTC (permalink / raw)
  To: Jon Mason, Dave Jiang, Allen Hubbe
  Cc: Koichiro Den, ntb, linux-kernel, regressions, yangqixiao, stable,
	Oguzhan KAYNAK

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 the address with kstrtou64() in
base 0, which accepts decimal and hexadecimal. A negative address stays
rejected, as it is since that commit. The size is parsed with %zu as
before.

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 writes to peer_mw_trans0 succeed, and malformed input
including a negative address is rejected).

Fixes: 21fbdc4d0b1e ("ntb/ntb_tool: correct sscanf format for u64 and size_t in tool_peer_mw_trans_write")
Cc: stable@vger.kernel.org
Cc: regressions@lists.linux.dev
Cc: yangqixiao <yangqixiao@inspur.com>
Suggested-by: Koichiro Den <den@valinux.co.jp>
Assisted-by: LLM
Signed-off-by: Oguzhan KAYNAK <kaynak.oguzhan0@gmail.com>
---
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).
- Koichiro's Reviewed-by on v1 is not carried over, as the code changed.

v1: https://lore.kernel.org/all/20261126031506.165973-1-kaynak.oguzhan0@gmail.com/

 drivers/ntb/test/ntb_tool.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

--- a/drivers/ntb/test/ntb_tool.c
+++ b/drivers/ntb/test/ntb_tool.c
@@ -927,7 +927,8 @@ static ssize_t tool_peer_mw_trans_write(
 	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;
+	int ret;
 	u64 addr;
 
 	buf_size = min(size, (sizeof(buf) - 1));
@@ -936,8 +937,16 @@ static ssize_t tool_peer_mw_trans_write(
 
 	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;
+
+	if (sscanf(size_str, "%zu", &wsize) != 1)
 		return -EINVAL;
 
 	tool_free_peer_mw(outmw->tc, outmw->widx);
-- 
2.34.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] ntb: ntb_tool: accept hexadecimal addresses in peer_mw_trans again
  2026-11-27 15:41 [PATCH v2] ntb: ntb_tool: accept hexadecimal addresses in peer_mw_trans again Oguzhan KAYNAK
@ 2026-11-30 12:32 ` Koichiro Den
  0 siblings, 0 replies; 2+ messages in thread
From: Koichiro Den @ 2026-11-30 12:32 UTC (permalink / raw)
  To: Oguzhan KAYNAK
  Cc: Jon Mason, Dave Jiang, Allen Hubbe, ntb, linux-kernel,
	regressions, yangqixiao, stable

On Fri, Nov 27, 2026 at 06:41:57PM +0300, Oguzhan KAYNAK wrote:
> 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 the address with kstrtou64() in
> base 0, which accepts decimal and hexadecimal. A negative address stays
> rejected, as it is since that commit. The size is parsed with %zu as
> before.
> 
> 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 writes to peer_mw_trans0 succeed, and malformed input
> including a negative address is rejected).
> 
> Fixes: 21fbdc4d0b1e ("ntb/ntb_tool: correct sscanf format for u64 and size_t in tool_peer_mw_trans_write")
> Cc: stable@vger.kernel.org
> Cc: regressions@lists.linux.dev
> Cc: yangqixiao <yangqixiao@inspur.com>
> Suggested-by: Koichiro Den <den@valinux.co.jp>
> Assisted-by: LLM
> Signed-off-by: Oguzhan KAYNAK <kaynak.oguzhan0@gmail.com>
> ---
> 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).
> - Koichiro's Reviewed-by on v1 is not carried over, as the code changed.
> 
> v1: https://lore.kernel.org/all/20261126031506.165973-1-kaynak.oguzhan0@gmail.com/
> 
>  drivers/ntb/test/ntb_tool.c | 15 ++++++++++++---
>  1 file changed, 12 insertions(+), 3 deletions(-)
> 
> --- a/drivers/ntb/test/ntb_tool.c
> +++ b/drivers/ntb/test/ntb_tool.c
> @@ -927,7 +927,8 @@ static ssize_t tool_peer_mw_trans_write(
>  	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;
> +	int ret;
>  	u64 addr;
>  
>  	buf_size = min(size, (sizeof(buf) - 1));
> @@ -936,8 +937,16 @@ static ssize_t tool_peer_mw_trans_write(
>  
>  	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;
> +
> +	if (sscanf(size_str, "%zu", &wsize) != 1)

With v2, "0x1000:0x1000" now just clears the MW and returns success.
Shouldn't that only happen when the whole size field really represents zero
(e.g. "0x1000:0")?

My first thought was to use kstrtou64(.., 0, ..) for both fields. But should
"010" mean 8 or 10? Looking into it, it seems that "010:010" was (8, 8) before
21fbdc4d0b1e, is (10, 10) on mainline, and would be (8, 10) with v2.. :(

I think the choice of base is the maintainer's call, but AFAIC it would be nice
to document whichever rule we settle on.

Best regards,
Koichiro Den

>  		return -EINVAL;
>  
>  	tool_free_peer_mw(outmw->tc, outmw->widx);
> -- 
> 2.34.1
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 12:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 15:41 [PATCH v2] ntb: ntb_tool: accept hexadecimal addresses in peer_mw_trans again Oguzhan KAYNAK
2026-11-30 12:32 ` Koichiro Den

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox