mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Feng Jiang <jiangfeng@kylinos.cn>
To: Michael Neuling <mikey@neuling.org>,
	pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu,
	alex@ghiti.fr
Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] riscv: lib: Fix ZBB strnlen reading past count boundary
Date: Mon, 13 Apr 2026 11:20:29 +0800	[thread overview]
Message-ID: <5cc377eb-3e6b-44c7-a935-359c5b9d64a2@kylinos.cn> (raw)
In-Reply-To: <20260413010738.1622423-1-mikey@neuling.org>

On 2026/4/13 09:07, Michael Neuling wrote:
> The ZBB-optimized strnlen loop loads one word ahead before checking the
> aligned boundary:
> 
>     REG_L   t1, SZREG(t0)       // load next word
>     addi    t0, t0, SZREG       // advance
>     orc.b   t1, t1
>     bgeu    t0, t4, 4f          // boundary check AFTER load
> 
> where t4 = (s + count) & -SZREG.  When s is aligned and count is a
> multiple of SZREG, t4 equals s + count and the loop loads a full word
> starting at exactly s + count.  If s + count falls on a page boundary
> with the next page unmapped, this faults.
> 
> Fix by computing the aligned boundary from the last valid byte
> (s + count - 1) instead of s + count.  This makes the loop stop at the
> word containing the last valid byte rather than potentially loading the
> word after it.  The count == 0 case is already handled by the beqz
> early exit.
> 
> Also add a pre-loop guard (bgeu t0, t4) for the case where all valid
> bytes fit within the first word.  With the adjusted boundary, t4 can
> equal t0, and entering the loop with stale register state from the
> first-word processing would produce incorrect results.
> 
> The final minu clamp ensures the result is still correct when the last
> loaded word extends past s + count - 1 within the same aligned word.
> 
> Fixes: 5ba15d419fab ("riscv: lib: add strnlen() implementation")
> Signed-off-by: Michael Neuling <mikey@neuling.org>
> Assisted-by: Claude Opus4.6 High Thinking
> ---
> ...

Hi Michael,

Thanks for catching and fixing this! Your analysis is spot on—that
"load-before-check" logic was indeed an oversight on my part, especially
regarding the page boundary edge case.

The test case you provided is extremely helpful. Since you've already
built this reproducer, would you be interested in helping to improve
the KUnit test string_test_strnlen() in lib/tests/string_kunit.c as well?
Currently, it mainly tests strings with NUL terminators and lacks coverage
for these kinds of non-terminated boundary scenarios.

Thanks again!

-- 
With Best Regards,
Feng Jiang


  reply	other threads:[~2026-04-13  3:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-13  1:07 Michael Neuling
2026-04-13  3:20 ` Feng Jiang [this message]
2026-04-13  5:02   ` Michael Neuling
2026-04-13  6:51     ` Feng Jiang
2026-08-08 18:00 ` patchwork-bot+linux-riscv

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=5cc377eb-3e6b-44c7-a935-359c5b9d64a2@kylinos.cn \
    --to=jiangfeng@kylinos.cn \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=mikey@neuling.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 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®