[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <cb65c596-7950-4402-b5e7-fddea12bd7e1@kylinos.cn>
Date: Fri, 23 Jan 2026 09:28:54 +0800
From: Feng Jiang <jiangfeng@...inos.cn>
To: Qingfang Deng <dqfext@...il.com>
Cc: pjw@...nel.org, palmer@...belt.com, aou@...s.berkeley.edu, alex@...ti.fr,
akpm@...ux-foundation.org, kees@...nel.org, andy@...nel.org,
ebiggers@...nel.org, martin.petersen@...cle.com, ardb@...nel.org,
charlie@...osinc.com, conor.dooley@...rochip.com, ajones@...tanamicro.com,
linus.walleij@...aro.org, nathan@...nel.org,
linux-riscv@...ts.infradead.org, linux-kernel@...r.kernel.org,
linux-hardening@...r.kernel.org, Joel Stanley <joel@....id.au>
Subject: Re: [PATCH v3 6/8] riscv: lib: add strnlen implementation
On 2026/1/21 15:24, Qingfang Deng wrote:
> On Tue, 20 Jan 2026 14:58:50 +0800, Feng Jiang wrote:
>> diff --git a/arch/riscv/lib/strnlen.S b/arch/riscv/lib/strnlen.S
>
> Branches that test maxlen can be replaced with Zbb minu instruction.
> (see below)
>
...
>> + /*
>> + * The first chunk is special: compare against the number
>> + * of valid bytes in this chunk.
>> + */
>> + srli a0, t1, 3
>> +
>> + /* Limit the result by maxlen. */
>> + bleu a1, a0, 3f
>
> minu a0, a0, a1
>
>> +
>> + bgtu t3, a0, 2f
>> +
>> + /* Prepare for the word comparison loop. */
>> + addi t2, t0, SZREG
>> + li t3, -1
>> +
>> + /*
>> + * Our critical loop is 4 instructions and processes data in
>> + * 4 byte or 8 byte chunks.
>> + */
>> + .p2align 3
>> +1:
>> + REG_L t1, SZREG(t0)
>> + addi t0, t0, SZREG
>> + orc.b t1, t1
>> + bgeu t0, t4, 4f
>> + beq t1, t3, 1b
>> +4:
>> + not t1, t1
>> + CZ t1, t1
>> + srli t1, t1, 3
>> +
>> + /* Get number of processed bytes. */
>> + sub t2, t0, t2
>> +
>> + /* Add number of characters in the first word. */
>> + add a0, a0, t2
>> +
>> + /* Add number of characters in the last word. */
>> + add a0, a0, t1
>> +
>> + /* Ensure the final result does not exceed maxlen. */
>> + bgeu a0, a1, 3f
>
> minu a0, a0, a1
>
Thanks for the great suggestion! I see your point now—using minu is indeed a much
more elegant and efficient way to handle the maxlen constraint. It nicely eliminates
unnecessary branches and simplifies the code while still allowing for early returns.
I'll incorporate this into a v4 patch and add a Suggested-by tag for you. Thanks
again for your insightful review!
--
With Best Regards,
Feng Jiang
Powered by blists - more mailing lists