From patchwork Tue Oct 31 07:24:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 13441125 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 48AD7C4167D for ; Tue, 31 Oct 2023 07:25:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 97E038D0008; Tue, 31 Oct 2023 03:25:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 92E446B0166; Tue, 31 Oct 2023 03:25:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 844578D0008; Tue, 31 Oct 2023 03:25:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 75F106B0165 for ; Tue, 31 Oct 2023 03:25:01 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4180880425 for ; Tue, 31 Oct 2023 07:25:01 +0000 (UTC) X-FDA: 81404920002.12.8D17812 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf28.hostedemail.com (Postfix) with ESMTP id 4071CC0005 for ; Tue, 31 Oct 2023 07:24:59 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=071goMPu; spf=pass (imf28.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.214.180 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=1698737099; 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=vhAhMlUaphHl5V7P6XcsEBHb1Aahpw8dSDQur0cX8zw=; b=dBuSR5qEMHdhqMQvy1HOGNup5XtFHe16GBZX5VKO6Ito1gYUIFEip2ACix6tu7MBLEKpWe E8gn9ucLhkSPlD4gUvjvXR5FZFrv3YKwuDIXXV/PcFm3NinYhZaF6SusxY89VF+xFJ1b3h QRf2UOzC+NvqC7gvBzRyYxz7fZr0130= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698737099; a=rsa-sha256; cv=none; b=wkH9yi4QqdgHnposC2x2FdQqY5TN2WOgtfV5PUgOmWO1LnKzcz6B/AUWlUlhdl0OboMlB3 34Ymp2cgE7Tv1R77Xpzzxv7XE+RQ9UEdzdcBpThenx88XkJPiuyl6lBunRBOnTXN+LQq6G 7IKzEv25cQ7loBopgsJzzxQNtdToX1M= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=071goMPu; spf=pass (imf28.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=charlie@rivosinc.com; dmarc=none Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1cc2f17ab26so22199305ad.0 for ; Tue, 31 Oct 2023 00:24:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1698737098; x=1699341898; 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=vhAhMlUaphHl5V7P6XcsEBHb1Aahpw8dSDQur0cX8zw=; b=071goMPudPETpdwZ7eDK7AZAxNnuH7vrQ+PQ8zZNPETgnqkvvCO40pU1DYCiR4oIAU hMIXE5R46+NcQoVG8GlSZrxV7+FG5YCMSkzM0Qv6vzNXDuEAfsh9+SKNJHa8cwT0nJgU n1tEQxxnaZOcKdXJt1X6Pmc6pM5FmSTLwmUINuNnXH9SxLngz/+4cAb2U/N4wOtlGpT7 hFMG6AfvaSWzqv6ccdSD3BemJR22+fIXEmxE8K9PNb5lgtayzu2382j55/c1ks8vMyOh vK6cf8SqRuXNcV8IhplVCxtCJDtxsWNN7duvQDpXQSasfCB4rj3Qla8A+aXbL5r6XVEV uvYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698737098; x=1699341898; 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=vhAhMlUaphHl5V7P6XcsEBHb1Aahpw8dSDQur0cX8zw=; b=EPcTUHSY1LAbldIHy9PSgwRfYGffbauVmRmLDjPhQOEZOavRefdJnBexqapqtokZH7 HpCrTI8e3gU7WP4HNZQzlQmHG4Swarb5DQGS3hlmz61pxFWg5SxpTBPZa3esVcuNy5dU 7C8iijs5MHws0adPW9F1kQBRXlKDEHYu4YYuGamj6VOEnKDxNrL59PNDJ1vaVaTWp7hQ Pxc3mt1RXywjSYMFw5tPzjk31MuO4VUZWwIwKCt2/VvXsdXWXkpvsrRLmoTpbX6bOurR 2QWdUaLn+/S+dqCj8L6vfmrCO6DBl/Gz+tjtNehQ1R2ivoKiaAkMu0bcOP1ug1pGUdU3 zXhw== X-Gm-Message-State: AOJu0Yw66B4x8D+/+3TtT88+6IiHJPV8yHZ81ihS0zsDci/LEPKPedeJ OuxRqvDhihgbfOKwkwVuDLZtPQ== X-Google-Smtp-Source: AGHT+IGD/aZU9vUfXBx6ayFBL19n5/oM54mKqlA9X4PoucNNNDI8a+afCb9T5z81BufqYr+mzRHhMg== X-Received: by 2002:a17:902:ce8e:b0:1cc:636f:f38a with SMTP id f14-20020a170902ce8e00b001cc636ff38amr1759360plg.57.1698737098025; 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.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 00:24:57 -0700 (PDT) From: Charlie Jenkins Subject: [PATCH v7 0/3] riscv: Add remaining module relocations and tests Date: Tue, 31 Oct 2023 00:24:49 -0700 Message-Id: <20231031-module_relocations-v7-0-6f4719b64bf7@rivosinc.com> MIME-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAMGrQGUC/23QwWoEIQwG4FdZPNeiRuPaU9+jlDKjsSvsjkW30 rLMu9dZShmKxz8kX0JurFJJVNnT4cYKtVRTXnqwDwfmT9PyTjyFnpkSCoQTR37J4fNMb4XO2U/ X3l15RPAU0Mg5WNYHPwrF9HVHX157PqV6zeX7vqPJrfrLSRhxTXLB5xnCUaMl6+C5pJZrWvyjz xe2iU39KVIIHCqqK9oaRG0gUvADBXaKHCvQlQnRxoDCSnIDRe8VO1R0VxzYaCiCxCgGitkrwzc 305UQiUAFHaIf/QX3ihsquN2irUJCrUHJf8q6rj/THr9CFQIAAA== 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-Stat-Signature: dhu85wmiu7izihyyihssoz63txmjoqy5 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 4071CC0005 X-Rspam-User: X-HE-Tag: 1698737099-577363 X-HE-Meta: U2FsdGVkX1+pzpTxyquEM5Jr56m+pCRifpbMoMp12kXOlQCQKQaKfz4pGWfuKInamiIJnmtLrBSgEvX8ll9Pxdfn32lHnGpx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6492gi/9OG0LXQV18fa8QDps/UO71T/lbWcuWW7seaRDIHPbp6cQx1FLfBnpn5uijechy3L86R8CaWdQ5yAEGgFFWEc/riLXvv+IO/r6aU2YIl0GcCr43T2FbplJRBEqfT9GEsujv0aM4GyupTtLgU++84t+GibQ1tAXB3ZwJm6Ddg8cmO43Dtt7CQmv2S8drz7qE2aNE/44elDyc/vALcbSAwINrkK938u4Z0lr/1YnWusy4EBXepU2FQGDGvz1BNdd+EI0S501UGlD1xGXHv/6ZuAYZICwK5GXwoLj2grYOJWNl8bbMlpgjy1WvrlDAAVJVGhI6F2b/1DMje5TEZgnY3FHUK3 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000170, 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 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 | 685 ++++++++++++++++++--- 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 | 22 + arch/riscv/kernel/tests/module_test/test_sub32.S | 22 + arch/riscv/kernel/tests/module_test/test_sub6.S | 22 + arch/riscv/kernel/tests/module_test/test_sub64.S | 27 + arch/riscv/kernel/tests/module_test/test_sub8.S | 22 + arch/riscv/kernel/tests/module_test/test_uleb128.S | 31 + 18 files changed, 963 insertions(+), 103 deletions(-) --- base-commit: 3bcce01fcbcd868b8cf3a5632fde283e122d7213 change-id: 20230908-module_relocations-f63ced651bd7