From nobody Wed Dec 17 10:03:15 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56ADEC4167B for ; Tue, 31 Oct 2023 07:25:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343682AbjJaHZJ (ORCPT ); Tue, 31 Oct 2023 03:25:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343646AbjJaHZC (ORCPT ); Tue, 31 Oct 2023 03:25:02 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78CC5C2 for ; Tue, 31 Oct 2023 00:24:59 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-6b2018a11efso5444659b3a.0 for ; Tue, 31 Oct 2023 00:24:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1698737099; x=1699341899; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=URk3OdUManeok/shpPwBhUNFZ0p313A2y5YhZgjx0S8=; b=SZAFWquEbDKWEN+6VjLfX7YfMS6LOJR9brFJ1siyB/KgJN8AbKNYgFqMnmtmzeICe7 rLySiWpHTWsY0Oi6uMZY8xYK7gMb8TX8J+aasFWuPw8uteITeyNhHgt5VTdUiwVHLBbE t+X2Alz191o7knb8ZReG0ChqetCOetNFGfiZ7cptVhXmMYsfeBXvuNZhkblAi30mw7lw NiOnTOjsWiolrqNF0JGO7GCRlbSAE6JEh3gb40tIQvC73m9JTcsR42aAgwNdfGGTGn3v CN8T3fg7R9IWN2WoLsOw6MzcHQziYhtC5aJ8w+EGUlwIbDYzaBgV0MBYL9/doJCZkiZG WJ0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698737099; x=1699341899; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=URk3OdUManeok/shpPwBhUNFZ0p313A2y5YhZgjx0S8=; b=mAWcrFOO2BZlKSmXxh+8XA9g12YowBtTWxGHmbjyGeyHG020i9vIyMLFLeZQNAAfqM fMjdeuXj/XqSB6o4Vxf3dIIP9H5iMZ+POuf+7XpAajQUGyDQkBk+Q/lNzGvL810wciyS BQRPTBrvwS3Fv75eTtHvrlIbPqzogfSFrc06lWcju1adnxcIbxvmXL+0JNSPu/0c4Mg6 iOyJnEfWju/+QwD3LPgsXMLJIGB5k4SvWDwO2O6xquhFmDN1Z9iztMcK4E10X626USvC DQGLG/Evr8T0Ma7fq0QkQdOqEj6IVh1iA+0dRajyqPcUdHou7gB8afZ+zyhwZhzobh/6 gbzw== X-Gm-Message-State: AOJu0YztCCJm2tAx5vZPVD4FKoI/s181joNnVK8s4MeXtGp6TFHtrIw1 OFlFZ7yUOKqv/uoYz9Uu6kbm4A== X-Google-Smtp-Source: AGHT+IGMiYHXnhGdC7cRoaxFCEKMq3w/10RpLtJzNqO9zNSb0eOy2g2uER1RInnmVou5GpnGB/rkDw== X-Received: by 2002:a05:6a20:748b:b0:17a:de5d:1d7e with SMTP id p11-20020a056a20748b00b0017ade5d1d7emr15340331pzd.55.1698737098947; Tue, 31 Oct 2023 00:24:58 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id c4-20020a170902d90400b001c74876f018sm652575plz.18.2023.10.31.00.24.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 00:24:58 -0700 (PDT) From: Charlie Jenkins Date: Tue, 31 Oct 2023 00:24:50 -0700 Subject: [PATCH v7 1/3] riscv: Avoid unaligned access when relocating modules MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20231031-module_relocations-v7-1-6f4719b64bf7@rivosinc.com> References: <20231031-module_relocations-v7-0-6f4719b64bf7@rivosinc.com> In-Reply-To: <20231031-module_relocations-v7-0-6f4719b64bf7@rivosinc.com> To: linux-riscv@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Eric Biederman , Kees Cook , Paul Walmsley , Palmer Dabbelt , Albert Ou , Andreas Schwab , Emil Renner Berthing , Samuel Holland , Nelson Chu , Charlie Jenkins , Emil Renner Berthing X-Mailer: b4 0.12.3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Emil Renner Berthing 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 Signed-off-by: Charlie Jenkins --- 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 } =20 -static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Add= r v) +static int riscv_insn_rmw(void *location, u32 keep, u32 set) +{ + u16 *parcel =3D location; + u32 insn =3D (u32)le16_to_cpu(parcel[0]) | (u32)le16_to_cpu(parcel[1]) <<= 16; + + insn &=3D keep; + insn |=3D set; + + parcel[0] =3D cpu_to_le32(insn); + parcel[1] =3D cpu_to_le16(insn >> 16); + return 0; +} + +static int riscv_insn_rvc_rmw(void *location, u16 keep, u16 set) +{ + u16 *parcel =3D location; + + *parcel =3D cpu_to_le16((le16_to_cpu(*parcel) & keep) | set); + return 0; +} + +static int apply_r_riscv_32_rela(struct module *me, void *location, Elf_Ad= dr v) { if (v !=3D (u32)v) { pr_err("%s: value %016llx out of range for 32-bit field\n", me->name, (long long)v); return -EINVAL; } - *location =3D v; + *(u32 *)location =3D v; return 0; } =20 -static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Add= r v) +static int apply_r_riscv_64_rela(struct module *me, void *location, Elf_Ad= dr v) { *(u64 *)location =3D v; return 0; } =20 -static int apply_r_riscv_branch_rela(struct module *me, u32 *location, +static int apply_r_riscv_branch_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; + ptrdiff_t offset =3D (void *)v - location; u32 imm12 =3D (offset & 0x1000) << (31 - 12); u32 imm11 =3D (offset & 0x800) >> (11 - 7); u32 imm10_5 =3D (offset & 0x7e0) << (30 - 10); u32 imm4_1 =3D (offset & 0x1e) << (11 - 4); =20 - *location =3D (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1; - return 0; + return riscv_insn_rmw(location, 0x1fff07f, imm12 | imm11 | imm10_5 | imm4= _1); } =20 -static int apply_r_riscv_jal_rela(struct module *me, u32 *location, +static int apply_r_riscv_jal_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; + ptrdiff_t offset =3D (void *)v - location; u32 imm20 =3D (offset & 0x100000) << (31 - 20); u32 imm19_12 =3D (offset & 0xff000); u32 imm11 =3D (offset & 0x800) << (20 - 11); u32 imm10_1 =3D (offset & 0x7fe) << (30 - 10); =20 - *location =3D (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1; - return 0; + return riscv_insn_rmw(location, 0xfff, imm20 | imm19_12 | imm11 | imm10_1= ); } =20 -static int apply_r_riscv_rvc_branch_rela(struct module *me, u32 *location, +static int apply_r_riscv_rvc_branch_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; + ptrdiff_t offset =3D (void *)v - location; u16 imm8 =3D (offset & 0x100) << (12 - 8); u16 imm7_6 =3D (offset & 0xc0) >> (6 - 5); u16 imm5 =3D (offset & 0x20) >> (5 - 2); u16 imm4_3 =3D (offset & 0x18) << (12 - 5); u16 imm2_1 =3D (offset & 0x6) << (12 - 10); =20 - *(u16 *)location =3D (*(u16 *)location & 0xe383) | - imm8 | imm7_6 | imm5 | imm4_3 | imm2_1; - return 0; + return riscv_insn_rvc_rmw(location, 0xe383, + imm8 | imm7_6 | imm5 | imm4_3 | imm2_1); } =20 -static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location, +static int apply_r_riscv_rvc_jump_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; + ptrdiff_t offset =3D (void *)v - location; u16 imm11 =3D (offset & 0x800) << (12 - 11); u16 imm10 =3D (offset & 0x400) >> (10 - 8); u16 imm9_8 =3D (offset & 0x300) << (12 - 11); @@ -98,16 +116,14 @@ static int apply_r_riscv_rvc_jump_rela(struct module *= me, u32 *location, u16 imm4 =3D (offset & 0x10) << (12 - 5); u16 imm3_1 =3D (offset & 0xe) << (12 - 10); =20 - *(u16 *)location =3D (*(u16 *)location & 0xe003) | - imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1; - return 0; + return riscv_insn_rvc_rmw(location, 0xe003, + imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1); } =20 -static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location, +static int apply_r_riscv_pcrel_hi20_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; - s32 hi20; + ptrdiff_t offset =3D (void *)v - location; =20 if (!riscv_insn_valid_32bit_offset(offset)) { pr_err( @@ -116,23 +132,20 @@ static int apply_r_riscv_pcrel_hi20_rela(struct modul= e *me, u32 *location, return -EINVAL; } =20 - hi20 =3D (offset + 0x800) & 0xfffff000; - *location =3D (*location & 0xfff) | hi20; - return 0; + return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000); } =20 -static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *locatio= n, +static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, void *locati= on, Elf_Addr v) { /* * v is the lo12 value to fill. It is calculated before calling this * handler. */ - *location =3D (*location & 0xfffff) | ((v & 0xfff) << 20); - return 0; + return riscv_insn_rmw(location, 0xfffff, (v & 0xfff) << 20); } =20 -static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *locatio= n, +static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, void *locati= on, Elf_Addr v) { /* @@ -142,15 +155,12 @@ static int apply_r_riscv_pcrel_lo12_s_rela(struct mod= ule *me, u32 *location, u32 imm11_5 =3D (v & 0xfe0) << (31 - 11); u32 imm4_0 =3D (v & 0x1f) << (11 - 4); =20 - *location =3D (*location & 0x1fff07f) | imm11_5 | imm4_0; - return 0; + return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0); } =20 -static int apply_r_riscv_hi20_rela(struct module *me, u32 *location, +static int apply_r_riscv_hi20_rela(struct module *me, void *location, Elf_Addr v) { - s32 hi20; - if (IS_ENABLED(CONFIG_CMODEL_MEDLOW)) { pr_err( "%s: target %016llx can not be addressed by the 32-bit offset from PC = =3D %p\n", @@ -158,22 +168,20 @@ static int apply_r_riscv_hi20_rela(struct module *me,= u32 *location, return -EINVAL; } =20 - hi20 =3D ((s32)v + 0x800) & 0xfffff000; - *location =3D (*location & 0xfff) | hi20; - return 0; + return riscv_insn_rmw(location, 0xfff, ((s32)v + 0x800) & 0xfffff000); } =20 -static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *location, +static int apply_r_riscv_lo12_i_rela(struct module *me, void *location, Elf_Addr v) { /* Skip medlow checking because of filtering by HI20 already */ s32 hi20 =3D ((s32)v + 0x800) & 0xfffff000; s32 lo12 =3D ((s32)v - hi20); - *location =3D (*location & 0xfffff) | ((lo12 & 0xfff) << 20); - return 0; + + return riscv_insn_rmw(location, 0xfffff, (lo12 & 0xfff) << 20); } =20 -static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location, +static int apply_r_riscv_lo12_s_rela(struct module *me, void *location, Elf_Addr v) { /* Skip medlow checking because of filtering by HI20 already */ @@ -181,20 +189,18 @@ static int apply_r_riscv_lo12_s_rela(struct module *m= e, u32 *location, s32 lo12 =3D ((s32)v - hi20); u32 imm11_5 =3D (lo12 & 0xfe0) << (31 - 11); u32 imm4_0 =3D (lo12 & 0x1f) << (11 - 4); - *location =3D (*location & 0x1fff07f) | imm11_5 | imm4_0; - return 0; + + return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0); } =20 -static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location, +static int apply_r_riscv_got_hi20_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; - s32 hi20; + ptrdiff_t offset =3D (void *)v - location; =20 /* Always emit the got entry */ if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { - offset =3D module_emit_got_entry(me, v); - offset =3D (void *)offset - (void *)location; + offset =3D (void *)module_emit_got_entry(me, v) - location; } else { pr_err( "%s: can not generate the GOT entry for symbol =3D %016llx from PC =3D= %p\n", @@ -202,22 +208,19 @@ static int apply_r_riscv_got_hi20_rela(struct module = *me, u32 *location, return -EINVAL; } =20 - hi20 =3D (offset + 0x800) & 0xfffff000; - *location =3D (*location & 0xfff) | hi20; - return 0; + return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000); } =20 -static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location, +static int apply_r_riscv_call_plt_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; + ptrdiff_t offset =3D (void *)v - location; u32 hi20, lo12; =20 if (!riscv_insn_valid_32bit_offset(offset)) { /* Only emit the plt entry if offset over 32-bit range */ if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) { - offset =3D module_emit_plt_entry(me, v); - offset =3D (void *)offset - (void *)location; + offset =3D (void *)module_emit_plt_entry(me, v) - location; } else { pr_err( "%s: target %016llx can not be addressed by the 32-bit offset from PC= =3D %p\n", @@ -228,15 +231,14 @@ static int apply_r_riscv_call_plt_rela(struct module = *me, u32 *location, =20 hi20 =3D (offset + 0x800) & 0xfffff000; lo12 =3D (offset - hi20) & 0xfff; - *location =3D (*location & 0xfff) | hi20; - *(location + 1) =3D (*(location + 1) & 0xfffff) | (lo12 << 20); - return 0; + riscv_insn_rmw(location, 0xfff, hi20); + return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20); } =20 -static int apply_r_riscv_call_rela(struct module *me, u32 *location, +static int apply_r_riscv_call_rela(struct module *me, void *location, Elf_Addr v) { - ptrdiff_t offset =3D (void *)v - (void *)location; + ptrdiff_t offset =3D (void *)v - location; u32 hi20, lo12; =20 if (!riscv_insn_valid_32bit_offset(offset)) { @@ -248,18 +250,17 @@ static int apply_r_riscv_call_rela(struct module *me,= u32 *location, =20 hi20 =3D (offset + 0x800) & 0xfffff000; lo12 =3D (offset - hi20) & 0xfff; - *location =3D (*location & 0xfff) | hi20; - *(location + 1) =3D (*(location + 1) & 0xfffff) | (lo12 << 20); - return 0; + riscv_insn_rmw(location, 0xfff, hi20); + return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20); } =20 -static int apply_r_riscv_relax_rela(struct module *me, u32 *location, +static int apply_r_riscv_relax_rela(struct module *me, void *location, Elf_Addr v) { return 0; } =20 -static int apply_r_riscv_align_rela(struct module *me, u32 *location, +static int apply_r_riscv_align_rela(struct module *me, void *location, Elf_Addr v) { pr_err( @@ -268,49 +269,49 @@ static int apply_r_riscv_align_rela(struct module *me= , u32 *location, return -EINVAL; } =20 -static int apply_r_riscv_add16_rela(struct module *me, u32 *location, +static int apply_r_riscv_add16_rela(struct module *me, void *location, Elf_Addr v) { *(u16 *)location +=3D (u16)v; return 0; } =20 -static int apply_r_riscv_add32_rela(struct module *me, u32 *location, +static int apply_r_riscv_add32_rela(struct module *me, void *location, Elf_Addr v) { *(u32 *)location +=3D (u32)v; return 0; } =20 -static int apply_r_riscv_add64_rela(struct module *me, u32 *location, +static int apply_r_riscv_add64_rela(struct module *me, void *location, Elf_Addr v) { *(u64 *)location +=3D (u64)v; return 0; } =20 -static int apply_r_riscv_sub16_rela(struct module *me, u32 *location, +static int apply_r_riscv_sub16_rela(struct module *me, void *location, Elf_Addr v) { *(u16 *)location -=3D (u16)v; return 0; } =20 -static int apply_r_riscv_sub32_rela(struct module *me, u32 *location, +static int apply_r_riscv_sub32_rela(struct module *me, void *location, Elf_Addr v) { *(u32 *)location -=3D (u32)v; return 0; } =20 -static int apply_r_riscv_sub64_rela(struct module *me, u32 *location, +static int apply_r_riscv_sub64_rela(struct module *me, void *location, Elf_Addr v) { *(u64 *)location -=3D (u64)v; return 0; } =20 -static int (*reloc_handlers_rela[]) (struct module *me, u32 *location, +static int (*reloc_handlers_rela[]) (struct module *me, void *location, Elf_Addr v) =3D { [R_RISCV_32] =3D apply_r_riscv_32_rela, [R_RISCV_64] =3D apply_r_riscv_64_rela, @@ -342,9 +343,9 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *s= trtab, struct module *me) { Elf_Rela *rel =3D (void *) sechdrs[relsec].sh_addr; - int (*handler)(struct module *me, u32 *location, Elf_Addr v); + int (*handler)(struct module *me, void *location, Elf_Addr v); Elf_Sym *sym; - u32 *location; + void *location; unsigned int i, type; Elf_Addr v; int res; --=20 2.34.1