mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Oguzhan KAYNAK <kaynak.oguzhan0@gmail.com>
To: Jon Mason <jdmason@kudzu.us>, Dave Jiang <dave.jiang@intel.com>,
	Allen Hubbe <allenbh@gmail.com>
Cc: Koichiro Den <den@valinux.co.jp>,
	ntb@lists.linux.dev, linux-kernel@vger.kernel.org,
	regressions@lists.linux.dev,
	Oguzhan KAYNAK <kaynak.oguzhan0@gmail.com>,
	stable@vger.kernel.org, yangqixiao <yangqixiao@inspur.com>
Subject: [PATCH v3] ntb: ntb_tool: accept hexadecimal addresses in peer_mw_trans again
Date: Tue,  1 Dec 2026 19:16:58 +0300	[thread overview]
Message-ID: <20261201161658.6286-1-kaynak.oguzhan0@gmail.com> (raw)

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.

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: yangqixiao <yangqixiao@inspur.com>
Suggested-by: Koichiro Den <den@valinux.co.jp>
Signed-off-by: Oguzhan KAYNAK <kaynak.oguzhan0@gmail.com>
---

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/20261127154157.3541-1-kaynak.oguzhan0@gmail.com/
    v1: https://lore.kernel.org/all/20261126031506.165973-1-kaynak.oguzhan0@gmail.com/

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

diff --git a/drivers/ntb/test/ntb_tool.c b/drivers/ntb/test/ntb_tool.c
index 7a88014..7a0f497 100644
--- a/drivers/ntb/test/ntb_tool.c
+++ b/drivers/ntb/test/ntb_tool.c
@@ -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,
-- 
2.34.1


                 reply	other threads:[~2026-12-01 16:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201161658.6286-1-kaynak.oguzhan0@gmail.com \
    --to=kaynak.oguzhan0@gmail.com \
    --cc=allenbh@gmail.com \
    --cc=dave.jiang@intel.com \
    --cc=den@valinux.co.jp \
    --cc=jdmason@kudzu.us \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ntb@lists.linux.dev \
    --cc=regressions@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    --cc=yangqixiao@inspur.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®