mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Qingfang Deng <qingfang.deng@linux.dev>
To: shao.mingyin@zte.com.cn, pjw@kernel.org, mikey@neuling.org
Cc: jiangfeng@kylinos.cn, palmer@dabbelt.com, aou@eecs.berkeley.edu,
	alex@ghiti.fr, linux-riscv@lists.infradead.org,
	linux-kernel@vger.kernel.org, zhang.yue5@zte.com.cn,
	david.laight.linux@gmail.com
Subject: Re: [PATCH v3] riscv: lib: Fix ZBB strnlen wrap-around regression on huge counts
Date: Tue, 15 Sep 2026 11:43:58 +0800	[thread overview]
Message-ID: <02a03893-b092-433b-8b28-586dcb5a54bc@linux.dev> (raw)
In-Reply-To: <20260914162123230u1y1M4UHrO8E-cU-opJ_7@zte.com.cn>

Hi,

On 2026/9/14 16:21, shao.mingyin@zte.com.cn wrote:
> diff --git a/arch/riscv/lib/strnlen.S b/arch/riscv/lib/strnlen.S
> index a8911605c248..528ee7995969 100644
> --- a/arch/riscv/lib/strnlen.S
> +++ b/arch/riscv/lib/strnlen.S
> @@ -67,7 +67,7 @@ strnlen_zbb:
>   	 *   a1 - Max length of string
>   	 *
>   	 * Clobbers
> -	 *   t0, t1, t2, t3, t4
> +	 *   t0, t1, t2, t3, t4, t5
>   	 */
>
>   	/* If maxlen is 0, return 0. */
> @@ -87,9 +87,22 @@ strnlen_zbb:
>   	 * Aligned boundary.  Use the address of the last valid byte
>   	 * (s + count - 1) to avoid loading a word past the count
>   	 * boundary in the loop below.  count == 0 is handled above.
> +	 *
> +	 * Saturate the boundary when s + count wraps around (very large
> +	 * counts, e.g. SIZE_MAX passed by FORTIFY strcat/strlcat with a
> +	 * destination whose size is unknown at compile time).  Without
> +	 * this, the wrapped boundary lands before s and the pre-loop
> +	 * guard below always exits, returning a truncated length.
> +	 * Saturating makes the scan equivalent to strlen().
> +	 *
> +	 * Keep the saturation branchless so that the normal path does
> +	 * not carry a taken branch that could be mispredicted.
>   	 */
>   	add	t4, a0, a1
>   	addi	t4, t4, -1
> +	sltu	t5, t4, a0	/* Did s + count wrap around? */
> +	sub	t5, zero, t5	/* -1 if wrapped, 0 otherwise */
> +	or	t4, t4, t5	/* Saturate to the top of the address space */
You can save one instruction by using Zbb minu instruction.

Equation: saturating_add(s, count - 1) == s + min(count - 1, ~s), given 
that count >= 1.

     addi    t4, a1, -1
     not    t1, a0
     minu    t4, t4, t1
     add    t4, a0, t4
>   	andi	t4, t4, -SZREG
>
>   	/* Get the first word.  */
Kind regards,
Qingfang

      reply	other threads:[~2026-09-15  3:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-14  8:21 shao.mingyin
2026-09-15  3:43 ` Qingfang Deng [this message]

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=02a03893-b092-433b-8b28-586dcb5a54bc@linux.dev \
    --to=qingfang.deng@linux.dev \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=david.laight.linux@gmail.com \
    --cc=jiangfeng@kylinos.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=mikey@neuling.org \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=shao.mingyin@zte.com.cn \
    --cc=zhang.yue5@zte.com.cn \
    /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®