From: Emil Renner Berthing <emil.renner.berthing@canonical.com>
To: Charlie Jenkins <charlie@rivosinc.com>,
linux-riscv@lists.infradead.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Cc: Eric Biederman <ebiederm@xmission.com>,
Kees Cook <keescook@chromium.org>,
Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Andreas Schwab <schwab@linux-m68k.org>,
Emil Renner Berthing <emil.renner.berthing@canonical.com>,
Samuel Holland <samuel.holland@sifive.com>,
Nelson Chu <nelson@rivosinc.com>,
Emil Renner Berthing <kernel@esmil.dk>
Subject: Re: [PATCH v7 1/3] riscv: Avoid unaligned access when relocating modules
Date: Tue, 31 Oct 2023 06:11:47 -0700 [thread overview]
Message-ID: <CAJM55Z-v0EwrZp876DdLSzad2u55nJV_uBs_+MUJDqFW5MTPkA@mail.gmail.com> (raw)
In-Reply-To: <20231031-module_relocations-v7-1-6f4719b64bf7@rivosinc.com>
Charlie Jenkins wrote:
> From: Emil Renner Berthing <kernel@esmil.dk>
>
> With the C-extension regular 32bit instructions are not
> necessarily aligned on 4-byte boundaries. RISC-V instructions
> are in fact an ordered list of 16bit little-endian
> "parcels", so access the instruction as such.
>
> This should also make the code work in case someone builds
> a big-endian RISC-V machine.
>
> Signed-off-by: Emil Renner Berthing <kernel@esmil.dk>
> Signed-off-by: Charlie Jenkins <charlie@rivosinc.com>
> ---
> arch/riscv/kernel/module.c | 153 +++++++++++++++++++++++----------------------
> 1 file changed, 77 insertions(+), 76 deletions(-)
>
> diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c
> index 7c651d55fcbd..a9e94e939cb5 100644
> --- a/arch/riscv/kernel/module.c
> +++ b/arch/riscv/kernel/module.c
> @@ -27,68 +27,86 @@ static bool riscv_insn_valid_32bit_offset(ptrdiff_t val)
> #endif
> }
>
> -static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Addr v)
> +static int riscv_insn_rmw(void *location, u32 keep, u32 set)
> +{
> + u16 *parcel = location;
> + u32 insn = (u32)le16_to_cpu(parcel[0]) | (u32)le16_to_cpu(parcel[1]) << 16;
> +
> + insn &= keep;
> + insn |= set;
> +
> + parcel[0] = cpu_to_le32(insn);
Why cpu_to_le32(insn)? Unless I've misunderstood something downcasting unsigned
to unsigned values in C (eg. from u32 to u16) is defined to always discard the
most signifcant bits, so cpu_to_le16(insn) should be fine.
> + parcel[1] = cpu_to_le16(insn >> 16);
> + return 0;
> +}
> +
> +static int riscv_insn_rvc_rmw(void *location, u16 keep, u16 set)
> +{
> + u16 *parcel = location;
> +
> + *parcel = cpu_to_le16((le16_to_cpu(*parcel) & keep) | set);
In this case, maybe consider writing it out like above just so it's easy to see
that the two functions does the same just for differently sized instructions.
The compiler should generate the same code.
> + return 0;
> +}
> +
> ...
next prev parent reply other threads:[~2023-10-31 13:11 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-31 7:24 [PATCH v7 0/3] riscv: Add remaining module relocations and tests Charlie Jenkins
2023-10-31 7:24 ` [PATCH v7 1/3] riscv: Avoid unaligned access when relocating modules Charlie Jenkins
2023-10-31 13:11 ` Emil Renner Berthing [this message]
2023-10-31 16:35 ` Andreas Schwab
2023-10-31 20:06 ` Charlie Jenkins
2023-10-31 7:24 ` [PATCH v7 2/3] riscv: Add remaining module relocations Charlie Jenkins
2023-10-31 13:43 ` Emil Renner Berthing
2023-10-31 18:18 ` Charlie Jenkins
2023-10-31 7:24 ` [PATCH v7 3/3] riscv: Add tests for riscv module loading 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=CAJM55Z-v0EwrZp876DdLSzad2u55nJV_uBs_+MUJDqFW5MTPkA@mail.gmail.com \
--to=emil.renner.berthing@canonical.com \
--cc=aou@eecs.berkeley.edu \
--cc=charlie@rivosinc.com \
--cc=ebiederm@xmission.com \
--cc=keescook@chromium.org \
--cc=kernel@esmil.dk \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-riscv@lists.infradead.org \
--cc=nelson@rivosinc.com \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=samuel.holland@sifive.com \
--cc=schwab@linux-m68k.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