Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp231668rdb; Tue, 31 Oct 2023 06:12:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFDbWpg9lPJkFyy5CVxjDzOy0qYyWwRyb6XYSArEC1YOWowu+O1p6YaPuiK8/CFEZRf0BX1 X-Received: by 2002:a05:6358:9195:b0:168:cea0:c837 with SMTP id j21-20020a056358919500b00168cea0c837mr14089405rwa.6.1698757934484; Tue, 31 Oct 2023 06:12:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698757934; cv=none; d=google.com; s=arc-20160816; b=UrQfaWWQCrVDsBz9hMWcgfP1c404w5u7zpC4UxQwq3B33Yu/T0mVebN8eAtNTbuByW TjDzkpgWUYdaNIvW+ndAtXUosYwcw3fNlqN9GFkfUcLCKhUByTGhE08XdEXfTm/qEEI5 1ZY9+aRPcykYV9xS044g9QXgC+CDrhnO/JMBLpQhple1JBUljeQMimPDwvC3updZY6QY aZ/3cIqsM/7p5BN7yN9cRNm/Z6Kk7D28u+i0by6tga6E/LIIHMksTfPaZxtwPCULezl9 88cYtjPOIDQBwP0Ft2xITF3FlEYJZxvS11H4OYNxLGw7H3g2M5r/xuPV5IORMiNSbGYp nEEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:mime-version :references:in-reply-to:from:dkim-signature; bh=b2T7hXExx+WhS9Dv9tSkQ0JEJTevpbCwf8KukhTzG9g=; fh=Qcv3UZLqZ12Iq/FfCXCtP7itO/P3AHbNbArrnJfAb0E=; b=Q2v5u8IuXu3RqGWTZSjlecZCBGP7Ut+wTIyp9CwFQu5ZdW2jaRbQXdNJoltYXER1F9 dZtwo1UVLTWblCY/IexqLqwKnN2Th+czAS9k88XSZBY11KeZ85Db1OZULLOZVMYZ3JUX e1Wgi1hv0qYjNTa4r2mDiZKfAPMFzpOs5PP1QH0lqBbhaIPLthXGDHxgZDzdjb91DlP9 sSmiYX9TxEssxHWEfB16LVwRQUwZV+9Lo6j5MfEW2sUIef2sUJE4v9fz6cbyPG6CBKiY LMOXutgC31lJ9SDGCvOwcSZrqrqqayodJNPrgwvrn42xDVyL5ZNxQPkk7sURR7tboga1 LQtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b="R/j0hF+y"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id w8-20020a63f508000000b005b9022ddeaasi1005781pgh.516.2023.10.31.06.12.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 06:12:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b="R/j0hF+y"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 4A7B280A97C2; Tue, 31 Oct 2023 06:12:08 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344447AbjJaNL5 (ORCPT + 99 others); Tue, 31 Oct 2023 09:11:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344427AbjJaNL4 (ORCPT ); Tue, 31 Oct 2023 09:11:56 -0400 Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 165FCDA for ; Tue, 31 Oct 2023 06:11:51 -0700 (PDT) Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 955723F1D9 for ; Tue, 31 Oct 2023 13:11:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1698757909; bh=b2T7hXExx+WhS9Dv9tSkQ0JEJTevpbCwf8KukhTzG9g=; h=From:In-Reply-To:References:Mime-Version:Date:Message-ID:Subject: To:Cc:Content-Type; b=R/j0hF+yzHC4mSPpdt5KQbhhTTlhtghcZ5ijNHTKXOSaFXWFjLzCMmsiIW/IeGQjE 2si+app0Ee7OvW4k5LpELpJQCT/5/+MAFvpAFKs1y9B57HlVquq2A+2JXsPRAElNRM smABtyAMnUmkU/MkZFdahAvbWWPxYG5LFkMwwusdzwjJO3BpK/EhMpvQ69SGL0Vj7V ubxco5g3Tb0sqE8ngZ8+zutJm06c6wFCLkGaVzDT/TNg9puvOlzskhAG6hF9Zi97Gv GAxl/D8+UFlFec5Twb1hc3JZQQM2KbLol5I/qTzcyvzpPbAcgxZZxvQdX8hV5P/xZk pSeL2ca2Y/nLw== Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-41ce372d248so69744111cf.2 for ; Tue, 31 Oct 2023 06:11:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698757908; x=1699362708; h=cc:to:subject:message-id:date:mime-version:references:in-reply-to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=b2T7hXExx+WhS9Dv9tSkQ0JEJTevpbCwf8KukhTzG9g=; b=hdRKyx8GA5S51YIh6dZpn0iVDvqFEdw6mfIsnK2XwuQN7/0alHzYJFmY1WPhlKRbyM 9kSMeHCxM/knzJyaQfIEfjIcAjdd5VTua6+um7YNCmjhboufJfxyFLRoYyG/WpxxJNgj GKy5SQkNGaeM/2SW39JZOYagJPx3OO+wjgg/hmP5pPritUSB5kduby1kV1fslALooB3W SACOl2H5HD7PJLy+t9ooNNsWl34e77lj8nlMRfQDSGP/u+ZEKtmVUcLCdKmDoWay7PVI J5TQgjlpDcxrsKMW9J9LdKOo2aT97KmYA50cKj7zzCkmcIBp7Xi70NDYq5MUeZsWvfoa vJ2w== X-Gm-Message-State: AOJu0YycslN1Z1dssh4yTLWcDMk+vcwj7HxI47bY7CdcDOCpF0ny94nM CMOh3fJ6pBu52LbArh1Gs6kHOto/1vAs6zs2cK0zGg4U4P9PACpJIrmTsYLNwTR2V/Hm/RY2j2a 4Fiot4DNQ9wzM38ZxhbiCCsDg/1bBdalTqEz5B7rNfoUJkfUIZSnLh4ibyg== X-Received: by 2002:a05:622a:351:b0:418:d3d:30e1 with SMTP id r17-20020a05622a035100b004180d3d30e1mr15984876qtw.4.1698757908409; Tue, 31 Oct 2023 06:11:48 -0700 (PDT) X-Received: by 2002:a05:622a:351:b0:418:d3d:30e1 with SMTP id r17-20020a05622a035100b004180d3d30e1mr15984845qtw.4.1698757908147; Tue, 31 Oct 2023 06:11:48 -0700 (PDT) Received: from 348282803490 named unknown by gmailapi.google.com with HTTPREST; Tue, 31 Oct 2023 06:11:47 -0700 From: Emil Renner Berthing In-Reply-To: <20231031-module_relocations-v7-1-6f4719b64bf7@rivosinc.com> References: <20231031-module_relocations-v7-0-6f4719b64bf7@rivosinc.com> <20231031-module_relocations-v7-1-6f4719b64bf7@rivosinc.com> Mime-Version: 1.0 Date: Tue, 31 Oct 2023 06:11:47 -0700 Message-ID: Subject: Re: [PATCH v7 1/3] riscv: Avoid unaligned access when relocating modules To: Charlie Jenkins , 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 , Emil Renner Berthing Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.3 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 31 Oct 2023 06:12:08 -0700 (PDT) Charlie Jenkins wrote: > 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 > } > > -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; > +} > + > ...