From: Paul Walmsley <pjw@kernel.org>
To: gao.rui@zte.com.cn
Cc: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu,
alex@ghiti.fr, jiangfeng@kylinos.cn,
david.laight.linux@gmail.com, linux-riscv@lists.infradead.org,
linux-kernel@vger.kernel.org, jmontleo@redhat.com
Subject: Re: [PATCH v3] riscv: fix strnlen() overflow in Zbb implementation
Date: Mon, 5 Oct 2026 11:17:46 -0600 (MDT) [thread overview]
Message-ID: <501e0def-c7ab-0f0c-cc19-7de5d253ca0d@kernel.org> (raw)
In-Reply-To: <202609300948576198tT3nfO5VwsTwUw23gA9v@zte.com.cn>
Hi Gao Rui,
On Wed, 30 Sep 2026, gao.rui@zte.com.cn wrote:
> riscv: fix strnlen() overflow in Zbb implementation
>
> 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 word counter instead of an end
> address. This removes the dependency on end-address calculations,
> avoids overflow entirely, and simplifies the termination condition of
> the word scanning loop. The final length is calculated using the saved
> original pointer.
>
> Performance was evaluated with string_bench_strnlen:
>
> New Implementation Previous Implementation
>
> len=0 : 71 ns/call 70 ns/call
> len=1 : 82 ns/call 81 ns/call
> len=7 : 83 ns/call 81 ns/call
> len=8 : 83 ns/call 81 ns/call
> len=16 : 94 ns/call 90 ns/call
> len=31 : 117 ns/call 113 ns/call
> len=64 : 175 ns/call 167 ns/call
> len=127 : 259 ns/call 258 ns/call
> len=512 : 802 ns/call 833 ns/call
> len=1024 : 1512 ns/call 1536 ns/call
> len=3173 : 4581 ns/call 4640 ns/call
> len=4096 : 6256 ns/call 6068 ns/call
>
> Results show comparable performance to the previous implementation
> while fixing the overflow issue.
>
> Fixes: 5ba15d419fab ("riscv: lib: add strnlen() implementation")
> Suggested-by: DavidLaight <david.laight.linux@gmail.com>
> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
Thanks for your patch. I took Shao Mingyin's patch for this instead,
although I would have preferred that he work with you to clean up your
patch instead, for the reasons described in
https://lore.kernel.org/linux-riscv/970c63ed-9fa8-9d1f-4521-0a7119b9d6e5@kernel.org/
If you think that Shao Mingyin's approach can be improved upon, please
send a followup patch.
- Paul
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2026-10-05 17:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-30 1:48 [PATCH v3] riscv: fix strnlen() overflow in Zbb implementation gao.rui
2026-09-30 8:45 ` David Laight
2026-10-05 17:17 ` Paul Walmsley [this message]
-- strict thread matches above, loose matches on Subject: below --
2026-09-30 12:44 gao.rui
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=501e0def-c7ab-0f0c-cc19-7de5d253ca0d@kernel.org \
--to=pjw@kernel.org \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=david.laight.linux@gmail.com \
--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 \
/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.