From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Yazhou Tang" <tangyazhou@zju.edu.cn>, <bpf@vger.kernel.org>,
<ast@kernel.org>, <eddyz87@gmail.com>
Cc: <daniel@iogearbox.net>, <john.fastabend@gmail.com>,
<andrii@kernel.org>, <martin.lau@linux.dev>, <song@kernel.org>,
<yonghong.song@linux.dev>, <kpsingh@kernel.org>,
<sdf@fomichev.me>, <haoluo@google.com>, <jolsa@kernel.org>,
<tangyazhou518@outlook.com>, <shenghaoyuan0928@163.com>,
<ziye@zju.edu.cn>
Subject: Re: [RFC PATCH bpf-next 1/6] bpf: Add UHMUL and SHMUL instructions
Date: Wed, 16 Sep 2026 05:56:05 +0000 [thread overview]
Message-ID: <DLGIC44OPLOE.XHJ1NP8XKPIQ@gmail.com> (raw)
In-Reply-To: <59e754a2-6158-485c-93b9-6f60d9f57528@zju.edu.cn>
On Tue Sep 15, 2026 at 11:48 AM UTC, Yazhou Tang wrote:
>
>
> On 9/14/26 16:36, Yazhou Tang wrote:
> >
> >
> > On 9/12/26 11:40 AM, Alexei Starovoitov wrote:
> >> On Tue Sep 8, 2026 at 1:30 AM PDT, Yazhou Tang wrote:
> >>>
> >>>
> >>> On 9/8/26 3:51 AM, Alexei Starovoitov wrote:
> >>>> On Wed Sep 2, 2026 at 12:05 AM PDT, Yazhou Tang wrote:
> >>>>> From: Tianci Cao <ziye@zju.edu.cn>
> >>>>>
> >>>>> Define UHMUL and SHMUL as BPF_ALU64 BPF_MUL variants selected by insn->off.
> >>>>> UHMUL returns the high 64 bits of an unsigned 64-by-64-bit product, while
> >>>>> SHMUL returns the high 64 bits of a signed product. Keep off == 0 as the
> >>>>> existing low-half multiplication, and assign off == 1 and off == 2 to
> >>>>> UHMUL and SHMUL, respectively.
> >>>>>
> >>>>> These operations are inspired by the UHMUL and SHMUL instructions
> >>>>> introduced as part of the PQR instruction class in Solana sBPF[1].
> >>>>>
> >>>>> The encoding and immediate semantics differ from current sBPF.
> >>>>>
> >>>>> 1. Solana sBPF assigns these operations dedicated opcodes in the BPF_PQR
> >>>>> class, whereas this implementation selects them through the offset
> >>>>> field of BPF_MUL.
> >>>>>
> >>>>> 2. Solana sBPF initially sign-extended the immediate operand of UHMUL,
> >>>>> but later changed unsigned PQR immediates, including UHMUL, to be
> >>>>> zero-extended[2].
> >>>>
> >>>> Why did you switch to zero-extend ?
> >>>> Sounds odd to do zero extend just for these ops.
> >>>
> >>> Sorry, I wasn't clear. This patch does not zero-extend the UHMUL immediate.
> >>>
> >>> Both UHMUL and SHMUL follow the existing BPF_ALU64 convention and
> >>> sign-extend the 32-bit immediate to 64 bits first. The zero-extension
> >>> I mentioned refers only to current Solana sBPF behavior. I'll clarify
> >>> this in the respin.
> >>
> >> My question was "why in Solana you switched to zero-extend?"
> >
> > I wasn't involved in that Solana change.
> >
> > https://github.com/solana-labs/rbpf/pull/642
> >
> > In Solana BPF, PQR is a separate instruction class for product, quotient,
> > and remainder operations. PR #642 mentioned above changed the treatment of
> > 32-bit immediates for all three 64-bit unsigned PQR operations (UHMUL,
> > UDIV, and UREM) from sign-extension to zero-extension. This appears to
> > have been a broader convention change for unsigned PQR operations, rather
> > than something specific to UHMUL.
> >
> > I couldn't find the design reason documented in the PR, though. I'll try
> > to ask the author about it.
>
> I asked Alexander Meißner, the author of the Solana BPF change. He confirmed
> that zero-extension was an intentional ISA design choice rather than a fix
> for a particular compatibility issue.
>
> He gave two main reasons.
>
> 1. First, for explicitly unsigned instructions such as UHMUL/UDIV/UREM,
> treating the 32-bit immediate as an unsigned value was considered more
> intuitive. For example, with an encoded immediate of 0xffffffff, zero-
> extension gives 0x00000000ffffffff (2^32 - 1), while sign-extension gives
> 0xffffffffffffffff (2^64 - 1). Similarly, 0x80000000 becomes 2^31 with
> zero-extension, rather than 2^64 - 2^31 with sign-extension.
>
> 2. Second, zero-extension gives the continuous unsigned range [0, 2^32 - 1].
> With sign-extension, the upper half of the immediate encoding instead
> represents values in [2^64 - 2^31, 2^64 - 1], while values in [2^31, 2^32 - 1]
> cannot be represented directly. The Solana BPF design considered the range
> provided by zero-extension more useful for unsigned operations.
>
> For Linux, given that the existing ALU64 UDIV/UMOD instructions with
> immediate operands sign-extend the 32-bit immediate to 64 bits and then
> use the resulting value as an unsigned operand, I'm inclined to do the
> same for UHMUL for consistency.
Agree. I think it's better to be consistent, since inconsistency in
imm32 encoding will cause quite a bit of headache to compilers to special
case these instructions.
next prev parent reply other threads:[~2026-09-16 5:56 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-02 7:05 [RFC PATCH bpf-next 0/6] bpf: Add UHMUL and SHMUL instructions Yazhou Tang
2026-09-02 7:05 ` [RFC PATCH bpf-next 1/6] " Yazhou Tang
2026-09-07 19:51 ` Alexei Starovoitov
2026-09-08 8:30 ` Yazhou Tang
2026-09-12 3:40 ` Alexei Starovoitov
2026-09-14 8:36 ` Yazhou Tang
2026-09-15 11:48 ` Yazhou Tang
2026-09-16 5:56 ` Alexei Starovoitov [this message]
2026-09-02 7:05 ` [RFC PATCH bpf-next 2/6] bpf, x86: JIT UHMUL and SHMUL on x86-64 Yazhou Tang
2026-09-02 7:05 ` [RFC PATCH bpf-next 3/6] bpf: Reject UHMUL/SHMUL in unsupported JITs Yazhou Tang
2026-09-02 7:21 ` sashiko-bot
2026-09-02 8:09 ` Yazhou Tang
2026-09-07 19:53 ` Alexei Starovoitov
2026-09-08 8:32 ` Yazhou Tang
2026-09-02 7:05 ` [RFC PATCH bpf-next 4/6] bpf: Refactor ALU instruction variant validation Yazhou Tang
2026-09-02 7:05 ` [RFC PATCH bpf-next 5/6] bpf: Add verifier support for UHMUL and SHMUL Yazhou Tang
2026-09-02 7:05 ` [RFC PATCH bpf-next 6/6] selftests/bpf: Add bytecode tests " Yazhou Tang
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=DLGIC44OPLOE.XHJ1NP8XKPIQ@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=martin.lau@linux.dev \
--cc=sdf@fomichev.me \
--cc=shenghaoyuan0928@163.com \
--cc=song@kernel.org \
--cc=tangyazhou518@outlook.com \
--cc=tangyazhou@zju.edu.cn \
--cc=yonghong.song@linux.dev \
--cc=ziye@zju.edu.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 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.