From: Aurelien Jarno <aurelien@aurel32.net>
To: gao.rui@zte.com.cn
Cc: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu,
alex@ghiti.fr, jiangfeng@kylinos.cn,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
jmontleo@redhat.com
Subject: Re: [PATCH v2] riscv: fix strnlen() overflow in Zbb implementation
Date: Thu, 24 Sep 2026 06:49:52 +0200 [thread overview]
Message-ID: <arSr8E0CYQRi8wMs@aurel32.net> (raw)
In-Reply-To: <20260924105708488K1vV5J_xi9COWY7TvJawc@zte.com.cn>
Hi,
On 2026-09-24 10:57, gao.rui@zte.com.cn wrote:
> The RISC-V Zbb optimized strnlen() implementation can return incorrect
> results when very large count values are supplied.
>
> The previous implementation calculates an end address based on the
> input pointer and count. When count is close to SIZE_MAX, the address
> calculation may overflow, resulting in incorrect termination checks and
> wrong return values.
>
> This issue was observed while running device-mapper tests:
>
> dmsetup create testname9 --table "0 8 zero"
> cat /sys/block/dm-*/dm/name
> dmsetup remove testname9
>
> Rework the Zbb implementation to use a decrementing word counter.
> This removes the dependency on end-address calculations,
> avoids overflow entirely, and simplifies the word scanning loop.
>
> Performance was evaluated with string_bench_strnlen:
>
> New Implementation Previous Implementation
>
> len=0 : 70 ns/call 70 ns/call
> len=1 : 81 ns/call 81 ns/call
> len=7 : 81 ns/call 81 ns/call
> len=8 : 81 ns/call 81 ns/call
> len=16 : 97 ns/call 90 ns/call
> len=31 : 119 ns/call 113 ns/call
> len=64 : 174 ns/call 162 ns/call
> len=127 : 264 ns/call 258 ns/call
> len=512 : 801 ns/call 824 ns/call
> len=1024 : 1578 ns/call 1550 ns/call
> len=3173 : 4541 ns/call 4703 ns/call
> len=4096 : 5984 ns/call 5982 ns/call
>
> Results show comparable performance to the previous implementation
> while fixing the overflow issue.
>
> Fixes: 5ba15d419fab ("riscv: lib: add strnlen() implementation")
> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
>
> ---
> v2:
> - Rework the Zbb implementation to eliminate end-address overflow
> instead of falling back to the generic path.
> - Use a decrementing word counter for word scanning.
> - Replace numeric labels with descriptive local labels.
> - Add comments describing the loop structure.
> - Run KUnit string tests successfully.
> - Add string_bench_strnlen benchmark results.
> ---
> arch/riscv/lib/strnlen.S | 111 +++++++++++++++++----------------------
> 1 file changed, 48 insertions(+), 63 deletions(-)
Note that the following patch was also posted, to what I believe is the
same issue:
https://lore.kernel.org/linux-riscv/DLLJDLKPZ4S3.1KQ5O4OQBBTEW@linux.dev
Regards
Aurelien
--
Aurelien Jarno GPG: 4096R/1DDD8C9B
aurelien@aurel32.net http://aurel32.net
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2026-09-24 4:50 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-24 2:57 [PATCH v2] riscv: fix strnlen() overflow in Zbb implementation gao.rui
2026-09-24 4:49 ` Aurelien Jarno [this message]
2026-09-25 2:47 ` Jason Montleon
2026-09-25 9:09 ` David Laight
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=arSr8E0CYQRi8wMs@aurel32.net \
--to=aurelien@aurel32.net \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=gao.rui@zte.com.cn \
--cc=jiangfeng@kylinos.cn \
--cc=jmontleo@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.