Generic Linux architectural discussions
 help / color / mirror / Atom feed
From: K Prateek Nayak <kprateek.nayak@amd.com>
To: Charlie Jenkins <thecharlesjenkins@gmail.com>
Cc: "Arnd Bergmann" <arnd@arndb.de>,
	"Thomas Gleixner" <tglx@kernel.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Sebastian Andrzej Siewior" <bigeasy@linutronix.de>,
	"Paul Walmsley" <pjw@kernel.org>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Albert Ou" <aou@eecs.berkeley.edu>,
	"Darren Hart" <dvhart@infradead.org>,
	"Davidlohr Bueso" <dave@stgolabs.net>,
	"André Almeida" <andrealmeid@igalia.com>,
	linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Samuel Holland" <samuel.holland@sifive.com>,
	linux-arm-kernel@lists.infradead.org,
	linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org,
	"Alexandre Ghiti" <alex@ghiti.fr>,
	"Jisheng Zhang" <jszhang@kernel.org>
Subject: Re: [PATCH v5 5/8] riscv/runtime-const: Introduce runtime_const_mask_32()
Date: Fri, 10 Jul 2026 13:47:10 +0530	[thread overview]
Message-ID: <650c7050-2a77-4415-b597-3bb39ccfb1e8@amd.com> (raw)
In-Reply-To: <178366995930.1208691.2993932866462893112.b4-review@b4>

Hello Charlie,

On 7/10/2026 1:22 PM, Charlie Jenkins wrote:
> [You don't often get email from thecharlesjenkins@gmail.com. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
> 
> On Tue, 30 Jun 2026 04:55:28 +0000, K Prateek Nayak <kprateek.nayak@amd.com> wrote:
>> Futex hash computation requires a mask operation with read-only after
>> init data that will be converted to a runtime constant in the subsequent
>> commit.
>>
>> Introduce runtime_const_mask_32 to further optimize the mask operation
>> in the futex hash computation hot path. Since all the current use-cases
>> are of the form GENMASK(n, 0), with n > 0, following sequence:
> 
> I really appreciate you spending the time to do this, thank you!

My pleasure! And I really appreciate you taking time to review and test
this series. Thanks a ton for that!

> 
>>
>>
>> diff --git a/arch/riscv/include/asm/runtime-const.h b/arch/riscv/include/asm/runtime-const.h
>> index 1ce02605d2e4..dbf96c937dbb 100644
>> --- a/arch/riscv/include/asm/runtime-const.h
>> +++ b/arch/riscv/include/asm/runtime-const.h
>> @@ -262,6 +279,33 @@ static inline void __runtime_fixup_shift(void *where, unsigned long val)
>> [ ... skip 24 lines ... ]
>> +     BUG_ON(!val || width > 31 || (GENMASK(width - 1, 0) != val));
>> +
>> +     __runtime_fixup_shift(where, 32 - width);
>> +     __runtime_fixup_shift(where + 4, 32 - width);
>> +}
>> +
> 
> It would be "optimal" to use an andi when the mask is <=11 bits since
> andi can fit an 11 bit mask. What you have is good enough but I'll leave
> my stab at doing the andi patching here in case you want to apply it.
> 
> From 9e5527aaddd464783af795aacdb6d094e11cc31e Mon Sep 17 00:00:00 2001
> From: Charlie Jenkins <thecharlesjenkins@gmail.com>
> Date: Thu, 9 Jul 2026 23:18:09 -0700
> Subject: [PATCH] riscv: Optimize __runtime_fixup_mask for masks with <= 11
>  bits

Peter seems to have merged the v5 series in his tree but If you could give
your S-o-b, I can throw in a commit log, some testing along with a few
cosmetic modifications, and send it for official review on top of
queue:locking/core ;-)

> 
> ---
>  arch/riscv/include/asm/insn.h          |  2 ++
>  arch/riscv/include/asm/runtime-const.h | 29 ++++++++++++++++++++++++--
>  2 files changed, 29 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/riscv/include/asm/insn.h b/arch/riscv/include/asm/insn.h
> index c3005573e8c9..0a34cd7305d0 100644
> --- a/arch/riscv/include/asm/insn.h
> +++ b/arch/riscv/include/asm/insn.h
> @@ -141,6 +141,7 @@
>  #define RVG_OPCODE_JALR                0x67
>  #define RVG_OPCODE_JAL         0x6f
>  #define RVG_OPCODE_SYSTEM      0x73
> +#define RVG_OPCODE_ANDI                0x13
>  #define RVG_SYSTEM_CSR_OFF     20
>  #define RVG_SYSTEM_CSR_MASK    GENMASK(12, 0)
> 
> @@ -175,6 +176,7 @@
>  #define RVG_FUNCT3_BGE         0x5
>  #define RVG_FUNCT3_BLTU                0x6
>  #define RVG_FUNCT3_BGEU                0x7
> +#define RVG_FUNCT3_ANDI                0x7
> 
>  /* parts of funct3 code for C extension*/
>  #define RVC_FUNCT3_C_BEQZ      0x6
> diff --git a/arch/riscv/include/asm/runtime-const.h b/arch/riscv/include/asm/runtime-const.h
> index dbf96c937dbb..24a9b13081f7 100644
> --- a/arch/riscv/include/asm/runtime-const.h
> +++ b/arch/riscv/include/asm/runtime-const.h
> @@ -9,6 +9,7 @@
>  #include <asm/asm.h>
>  #include <asm/alternative.h>
>  #include <asm/cacheflush.h>
> +#include <asm/insn.h>
>  #include <asm/insn-def.h>
>  #include <linux/memory.h>
>  #include <asm/text-patching.h>
> @@ -302,8 +303,32 @@ static inline void __runtime_fixup_mask(void *where, unsigned long val)
>          */
>         BUG_ON(!val || width > 31 || (GENMASK(width - 1, 0) != val));
> 
> -       __runtime_fixup_shift(where, 32 - width);
> -       __runtime_fixup_shift(where + 4, 32 - width);
> +       /*
> +        * A riscv 'andi' instruction can fit an 11 bit immediate, so the mask
> +        * can be directly applied. Otherwise fall back to SRLI + SLLI.
> +        */
> +       if (width < 11) {
> +               __le16 *parcel = where;
> +               u32 insn;
> +               __le32 res, nop;
> +
> +               insn = (u32)le16_to_cpu(parcel[0]) | (u32)le16_to_cpu(parcel[1]) << 16;
> +
> +               /* Replace the slli/slliw with an andi */
> +               insn &= 0x000fcf80;
> +               insn |= val << 20 | RV_ENCODE_FUNCT3(ANDI) | RVG_OPCODE_ANDI;
> +
> +               res = cpu_to_le32(insn);
> +               /* Replace the srli/srliw with a nop */
> +               nop = cpu_to_le32(RISCV_INSN_NOP4);
> +               mutex_lock(&text_mutex);
> +               patch_text_nosync(where, &res, sizeof(insn));
> +               patch_text_nosync(where + 4, &nop, sizeof(insn));
> +               mutex_unlock(&text_mutex);
> +       } else {
> +               __runtime_fixup_shift(where, 32 - width);
> +               __runtime_fixup_shift(where + 4, 32 - width);
> +       }
>  }
> 
>  static inline void runtime_const_fixup(void (*fn)(void *, unsigned long),
> --
> 2.54.0
> 
> 
> I would prefer including this, but I am happy to approve this
> regardless.

Ack! I'll keep it as an optimization on top to retain your attribution.

> 
> Reviewed-by: Charlie Jenkins <thecharlesjenkins@gmail.com>
> Tested-by: Charlie Jenkins <thecharlesjenkins@gmail.com>

Thank you again!

-- 
Thanks and Regards,
Prateek


  reply	other threads:[~2026-07-10  8:17 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-30  4:55 [PATCH v5 0/8] futex: Use runtime constants for futex_hash computation K Prateek Nayak
2026-06-30  4:55 ` [PATCH v5 1/8] x86/runtime-const: Introduce runtime_const_mask_32() K Prateek Nayak
2026-06-30  4:55 ` [PATCH v5 2/8] arm64/runtime-const: Use aarch64_insn_patch_text_nosync() for patching K Prateek Nayak
2026-07-10  7:52   ` Charlie Jenkins
2026-06-30  4:55 ` [PATCH v5 3/8] arm64/runtime-const: Introduce runtime_const_mask_32() K Prateek Nayak
2026-07-10  7:52   ` Charlie Jenkins
2026-06-30  4:55 ` [PATCH v5 4/8] riscv/runtime-const: Replace open-coded placeholder with RUNTIME_MAGIC K Prateek Nayak
2026-06-30  6:47   ` Guo Ren
2026-06-30  4:55 ` [PATCH v5 5/8] riscv/runtime-const: Introduce runtime_const_mask_32() K Prateek Nayak
2026-07-10  7:52   ` Charlie Jenkins
2026-07-10  8:17     ` K Prateek Nayak [this message]
2026-06-30  4:55 ` [PATCH v5 6/8] s390/runtime-const: " K Prateek Nayak
2026-06-30  4:55 ` [PATCH v5 7/8] asm-generic/runtime-const: Add dummy runtime_const_mask_32() K Prateek Nayak
2026-07-10  7:52   ` Charlie Jenkins
2026-06-30  4:55 ` [PATCH v5 8/8] futex: Use runtime constants for __futex_hash() hot path K Prateek Nayak
2026-07-01  7:57   ` Peter Zijlstra
2026-07-01  8:41     ` Sebastian Andrzej Siewior
2026-07-01  9:07       ` K Prateek Nayak
2026-07-01 16:17         ` [PATCH] futex: Optimise the size check get_futex_key() Sebastian Andrzej Siewior
2026-07-02  8:59           ` Peter Zijlstra
2026-07-02 10:56             ` Sebastian Andrzej Siewior
2026-07-02 11:18               ` Peter Zijlstra
2026-07-01 11:01       ` [PATCH v5 8/8] futex: Use runtime constants for __futex_hash() hot path Sebastian Andrzej Siewior
2026-07-01 19:58   ` Sebastian Andrzej Siewior
2026-07-10  7:52   ` Charlie Jenkins

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=650c7050-2a77-4415-b597-3bb39ccfb1e8@amd.com \
    --to=kprateek.nayak@amd.com \
    --cc=alex@ghiti.fr \
    --cc=andrealmeid@igalia.com \
    --cc=aou@eecs.berkeley.edu \
    --cc=arnd@arndb.de \
    --cc=bigeasy@linutronix.de \
    --cc=dave@stgolabs.net \
    --cc=dvhart@infradead.org \
    --cc=jszhang@kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=palmer@dabbelt.com \
    --cc=peterz@infradead.org \
    --cc=pjw@kernel.org \
    --cc=samuel.holland@sifive.com \
    --cc=tglx@kernel.org \
    --cc=thecharlesjenkins@gmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox