From patchwork Wed Nov 1 18:32:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 13442986 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8DA39C4332F for ; Wed, 1 Nov 2023 18:33:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB859900016; Wed, 1 Nov 2023 14:33:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E683590000D; Wed, 1 Nov 2023 14:33:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3013900016; Wed, 1 Nov 2023 14:33:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C488890000D for ; Wed, 1 Nov 2023 14:33:07 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 8DA951409A0 for ; Wed, 1 Nov 2023 18:33:07 +0000 (UTC) X-FDA: 81410232414.10.2E017E2 Received: from mail-oo1-f53.google.com (mail-oo1-f53.google.com [209.85.161.53]) by imf14.hostedemail.com (Postfix) with ESMTP id 8971A10000D for ; Wed, 1 Nov 2023 18:33:05 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=3W6Pzigz; spf=pass (imf14.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.161.53 as permitted sender) smtp.mailfrom=charlie@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698863585; a=rsa-sha256; cv=none; b=yefrwVazZpMepYq5wI+9/rGPbJXhOHrP1lN3snK8sxql/lQi6NHvmEAcj9wFohetUK/2EH vqwvOMQCqvT4GTUp6up6/8L6SZ2aBHcqGd8zYazhVuqpb/wAPp5UoxNk86iv0z2TEalPk3 od3fPrYNb3NXvtodEtddj3Cc/F46A7Y= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=3W6Pzigz; spf=pass (imf14.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.161.53 as permitted sender) smtp.mailfrom=charlie@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698863585; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=YlYNLy4VnSwlzJ5T98YglMe9qEBY6q8tyB3afSFZj/k=; b=ZMGbmp24kHu+5GLDz8YuCHndgfigJfoIchvKQUcFZgUxI9BYKnccwZDoGEss2VXHk4urBw s8RiAtAfwYEeJZFWxVDnfhmPph26VRd/56aPrOIWpdINrLYnPScSv65LowgkNx7eIlqAdm GxClpVRKyY52WZFzNqkctvhJmPmZ5jc= Received: by mail-oo1-f53.google.com with SMTP id 006d021491bc7-586ae6edf77so46680eaf.1 for ; Wed, 01 Nov 2023 11:33:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1698863584; x=1699468384; darn=kvack.org; h=cc:to:content-transfer-encoding:mime-version:message-id:date :subject:from:from:to:cc:subject:date:message-id:reply-to; bh=YlYNLy4VnSwlzJ5T98YglMe9qEBY6q8tyB3afSFZj/k=; b=3W6Pzigz3vsiN4oLFGWQIm5m9Cs6wQSPNVSxp1VbFUeRgZhoA+WWoicNnqsxIKKG/t SRFQuk8sqOM39pcusstgCWHOdnBOZUM/VVgn9Le9eythBPeDNIBJVml8gq6wBKze+Otn Ny8QBT+zg2mnqLyZ6xnkRruvrcI3IgTHnvezqwacp7S/EWE2xr1i1YCoIzk4iAPjWKV9 ZEE/Wv2H5CdnmdQAsjIPHItm72m1EKz6nAs1xlhoOMim1HfhkCMzRB/CHg87uYxnSQJl FIpUV8tj4+gRPd4a95Tqld/AQJAx/d8D2LLbKLzi7SXnhhtDKJ2/Ocjj8dMPkSsq88NA GzCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698863584; x=1699468384; h=cc:to:content-transfer-encoding:mime-version:message-id:date :subject:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=YlYNLy4VnSwlzJ5T98YglMe9qEBY6q8tyB3afSFZj/k=; b=erh3V2TR30IKMSB3XGsbdOlT1K2ghQLFfQ9VZUQisUV0JF3HdXrJ1tkRh6nGwgQ+EV U5F4tSypUGStcRXMV56h5/6nCXr4X6qFtOeKFRLZIzjqpg0FXUIWW62CnZoIhssG4wVe eiY1KYwBNfLnuLickbM6NQo4Pem34By6T7anyYjxGA5no2oUrtKJlUh7TdPo0Xh/o3Qd gjUM2OsrZwSYZ8Yi+GjN28N8E5q1AmpRV2OUIU2Hh/GSyOcdTMxKHkycdrmLCByhL3qL O2ja3G+ArrcA8ESpzTOQZSg6c9xFoN4ZtCKA737lNjTFhn/K81lbeNT4DLGSfOwn1ZsQ 2Mjg== X-Gm-Message-State: AOJu0YyyxZuIXsWsSW8nwHp5UfL0cTfjJ1YJwb/oS9Syw9tS6/43eddT Go31eQUqVA2TdwhJGVTYyEdc2Q== X-Google-Smtp-Source: AGHT+IEjD1wkkctBbU+7K5oihsH7H4HSTVQxge9RCOX/Hymo2C9jI2bYeaR39S7Le6kXF5cz9OqAFw== X-Received: by 2002:a4a:c885:0:b0:587:2b9b:985a with SMTP id t5-20020a4ac885000000b005872b9b985amr5701208ooq.9.1698863584599; Wed, 01 Nov 2023 11:33:04 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id m14-20020a4aab8e000000b00581e090fd1fsm686331oon.8.2023.11.01.11.33.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 11:33:04 -0700 (PDT) From: Charlie Jenkins Subject: [PATCH v9 0/3] riscv: Add remaining module relocations and tests Date: Wed, 01 Nov 2023 11:32:58 -0700 Message-Id: <20231101-module_relocations-v9-0-8dfa3483c400@rivosinc.com> MIME-Version: 1.0 X-B4-Tracking: v=1; b=H4sIANqZQmUC/4XQwWrDMAwG4FcpPi/Dtmwp7mnvMcaobXk1tM1Iu rBR8u5zyhhhCHb8hfRJ6KYmHitPar+7qZHnOtXh0kJ42Kl0PFzeuKu5ZWW1BR10352H/HHi15F PQzpcW/fUFYTEGb2JmVQbfB+51M87+vzS8rFO12H8uu+YzVr94QxI3Gw63cUIuXdITAGexjoPU 72kxzSc1SrO9lcxWqOo2KY48ojOQ+GcBAU2ipEVaMoBkUpGTYaDoLitQqLimhKAiucCBosWFL9 VxDfPvim5MIPNLpck/QW3ShAVXG9xZJHRObBGUGijgBEVagoWRyZEdLGQoPT/K31TdIg+ktXJF P6jLMvyDSROhxShAgAA 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 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 8971A10000D X-Stat-Signature: e3yzkbu7h8bzpf5qsqob48rimohw1uiz X-Rspam-User: X-HE-Tag: 1698863585-587325 X-HE-Meta: U2FsdGVkX19Nje5qMVbk1fAXSfF0Lr8e6Waf3NOt/1F0ZEQgCHGfn99oK4z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9yYu8k4Pz4aWKQTShn/fv7zfF2RNeJdWxu8W7oY+0vwrvsVt6saReLbsENDI+gvb2o6NzzT2E7V3i9JHMTymniSS1ti8KltgyRR4GlJqY0xxrTJwiBhqD/d1N9VXKMxiZJ/PjnXSKPARidq0nDt8ug86QIbE4HPEw/K3nJCf4NtUux0EBLAaxPxrvXxJA9oc3Z9MJBdMnbIPbiyYz3359fY6FFfhHGJUWZVhmueMC5BlB5w8XZuGWuF8CDA9OXSHHGkcFrjOQko4iTKoKhPl39oU3rOCUCooT9d97CK8jB6QSufhdRnUMo5HeZsEKWmFdvDZh46yjaHfUGQ5m2QZM1lq3Y+1killtMcS6RJny43m2KmkCSEP9aUWp6FYt X-Bogosity: Ham, tests=bogofilter, spamicity=0.003099, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: A handful of module relocations were missing, this patch includes the remaining ones. I also wrote some test cases to ensure that module loading works properly. Some relocations cannot be supported in the kernel, these include the ones that rely on thread local storage and dynamic linking. This patch also overhauls the implementation of ADD/SUB/SET/ULEB128 relocations to handle overflow. "Overflow" is different for ULEB128 since it is a variable-length encoding that the compiler can be expected to generate enough space for. Instead of overflowing, ULEB128 will expand into the next 8-bit segment of the location. A psABI proposal [1] was merged that mandates that SET_ULEB128 and SUB_ULEB128 are paired, however the discussion following the merging of the pull request revealed that while the pull request was valid, it would be better for linkers to properly handle this overflow. This patch proactively implements this methodology for future compatibility. This can be tested by enabling KUNIT, RUNTIME_KERNEL_TESTING_MENU, and RISCV_MODULE_LINKING_KUNIT. [1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/403 Signed-off-by: Charlie Jenkins --- Changes in v9: - Use uintptr_t where applicable (Emil) - Split up insn_rmw changes into correct patch (Emil) - Make reloc_handlers const and simplify struct (Emil) - Link to v8: https://lore.kernel.org/r/20231031-module_relocations-v8-0-09b5b720c1fe@rivosinc.com Changes in v8: - Fix up riscv_insn_rmw to work with big-endian systems (Andreas/Emil) - Improve readability of bitops throughout module.c (Emil) - Consolidate all relocation handler functions into a single struct so array doesn't need to be duplicated 3 times - Use .space assember op instead of .rept - Fix up name of uleb128 testcase - Define prototypes of some new functions to conform to -Wmissing-prototypes - Remove unused variable "first" - Link to v7: https://lore.kernel.org/r/20231031-module_relocations-v7-0-6f4719b64bf7@rivosinc.com Changes in v7: - Overhaul ADD/SUB/SET/ULEB128 relocations - Fix ULEB128 so it produces correct values when more than 1 byte is needed - Link to v6: https://lore.kernel.org/r/20231019-module_relocations-v6-0-94726e644321@rivosinc.com Changes in v6: - Use (void *) instead of (u32 *) for handler type - Constrain ULEB128 to be consecutive relocations - Link to v5: https://lore.kernel.org/r/20231018-module_relocations-v5-0-dfee32d4dfc3@rivosinc.com Changes in v5: - Brought in patch by Emil and fixed it up to force little endian - Fixed up issues with apply_r_riscv_32_pcrel_rela and apply_r_riscv_plt32_rela (Samuel) - Added u8 cast in apply_r_riscv_sub6_rela (Andreas) - Link to v4: https://lore.kernel.org/r/20231017-module_relocations-v4-0-937f5ef316f0@rivosinc.com Changes in v4: - Complete removal of R_RISCV_RVC_LUI - Fix bug in R_RISCV_SUB6 linking - Only build ULEB128 tests if supported by toolchain - Link to v3: https://lore.kernel.org/r/20231016-module_relocations-v3-0-a667fd6071e9@rivosinc.com Changes in v3: - Add prototypes to test_module_linking_main as recommended by intel zero day bot - Improve efficiency of ULEB128 pair matching - Link to v2: https://lore.kernel.org/r/20231006-module_relocations-v2-0-47566453fedc@rivosinc.com Changes in v2: - Added ULEB128 relocations - Link to v1: https://lore.kernel.org/r/20230913-module_relocations-v1-0-bb3d8467e793@rivosinc.com --- Charlie Jenkins (2): riscv: Add remaining module relocations riscv: Add tests for riscv module loading Emil Renner Berthing (1): riscv: Avoid unaligned access when relocating modules arch/riscv/Kconfig.debug | 1 + arch/riscv/include/uapi/asm/elf.h | 5 +- arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/module.c | 603 +++++++++++++++++---- arch/riscv/kernel/tests/Kconfig.debug | 35 ++ arch/riscv/kernel/tests/Makefile | 1 + arch/riscv/kernel/tests/module_test/Makefile | 15 + .../tests/module_test/test_module_linking_main.c | 88 +++ arch/riscv/kernel/tests/module_test/test_set16.S | 23 + arch/riscv/kernel/tests/module_test/test_set32.S | 20 + arch/riscv/kernel/tests/module_test/test_set6.S | 23 + arch/riscv/kernel/tests/module_test/test_set8.S | 23 + arch/riscv/kernel/tests/module_test/test_sub16.S | 20 + arch/riscv/kernel/tests/module_test/test_sub32.S | 20 + arch/riscv/kernel/tests/module_test/test_sub6.S | 20 + arch/riscv/kernel/tests/module_test/test_sub64.S | 25 + arch/riscv/kernel/tests/module_test/test_sub8.S | 20 + arch/riscv/kernel/tests/module_test/test_uleb128.S | 31 ++ 18 files changed, 869 insertions(+), 105 deletions(-) --- base-commit: 3bcce01fcbcd868b8cf3a5632fde283e122d7213 change-id: 20230908-module_relocations-f63ced651bd7