From patchwork Tue Oct 31 22:25:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 13442290 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 38863C4332F for ; Tue, 31 Oct 2023 22:25:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BEB6C6B0336; Tue, 31 Oct 2023 18:25:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B9B926B0337; Tue, 31 Oct 2023 18:25:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A151C6B0338; Tue, 31 Oct 2023 18:25:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 910446B0336 for ; Tue, 31 Oct 2023 18:25:23 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 70C59B5522 for ; Tue, 31 Oct 2023 22:25:23 +0000 (UTC) X-FDA: 81407188926.08.BF71403 Received: from mail-oi1-f173.google.com (mail-oi1-f173.google.com [209.85.167.173]) by imf04.hostedemail.com (Postfix) with ESMTP id 9233740010 for ; Tue, 31 Oct 2023 22:25:21 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=TDybqvNG; spf=pass (imf04.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.167.173 as permitted sender) smtp.mailfrom=charlie@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698791121; a=rsa-sha256; cv=none; b=2UXH8uxVtysPFdr5szb241rshGF8NYALccUkY2IVlOTOmVZdKVeoLjT1T1O9sYHTRDoe1X R2a6J0aWykD0tCKb+hTFftKidJxA/IaJUeD8Zt91o59oaJ/Ld0OGf0t8RYq7Zgcje9435L oiqMPM1bw6oeG6QrEX3+1JNllWJotXM= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=TDybqvNG; spf=pass (imf04.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.167.173 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=1698791121; 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=K8Ul6j8QrEIyS00FFqomsmEEIf38jX8XQ6bvZNd2Kps=; b=glTBNKOBS0Ffy8dLjWGUvfA0zycxLEoOAiq9CNsHkFKVMuZzWwnHoEO/aruxUq1K2hhLqi poHomOQKr1RF/xtQKGPHwcFz6uNBPGjK8ck/VCZdJoQpihtJjUM+0998PoeaA5MBlUJtqN DR6kqHuKcuS/znTJo5keDCBuP1NjWcc= Received: by mail-oi1-f173.google.com with SMTP id 5614622812f47-3b2b1af964dso3966259b6e.1 for ; Tue, 31 Oct 2023 15:25:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1698791120; x=1699395920; 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=K8Ul6j8QrEIyS00FFqomsmEEIf38jX8XQ6bvZNd2Kps=; b=TDybqvNGf0qyVxYyLHX+T4Z6xct5tk5MQ/py+fq0z8OpRmQLnN7wQBksOUPgCi78E7 wLjjo6ySe0+hTKwCKklPWaTzPR3ekpkTtPpy48BeFFNdRc4zeyVRGqsWzH0fkzGqePxe aQj5F6u6EyBfktNQznYA7atVV/ouue4Ioy/mTmjcLEB1QKtXnSjB+mkaqGdX1ZVh7DKm WDFZZ7vBTflFgZB1DXPibRZNIegoKqusOS0HMVtuty2qFC3V7g5k2KH/M7mZAmNHhrsN tKVO0RjUEV5jMJshsPvQRSUBeBnPLH9vgenqTRwSzAidu2BdLWeJmYH5LxG+dPtChERv as2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698791120; x=1699395920; 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=K8Ul6j8QrEIyS00FFqomsmEEIf38jX8XQ6bvZNd2Kps=; b=smauLtcMqQc173hmNFH5Jzc/U1OZ48N5wT+aCv2LjutOFcfzchkgWVbMwuDkUlFn6N 4OTxEI3txj4SBam6aN4qrOlMA+JGjXcE+q7JHhjj7QiauTeXu6GvlMj5CsSlpmVqkGoE UN9wJC4CQUlMKaIpHXDTBlB8slkzoAlWB30Rs5urFRXqP6i/c72chiECoQ7s7KKG/MSm qVD1fQm87brpbRCo+3rdbpo3rXz0vMuDJ5i9763nw3YL4ufpE2xhIZeaQQzmiczzBUTu C2RbYI74RRI/YU0MRHXaTmO472d/YdZawxvcvDLDv37xUkK7wZX88ouhBSifEgwPqOG4 dtLQ== X-Gm-Message-State: AOJu0YyqQ4S5c/5Lj879UlxXxI6LX+GJOccRNda+erbYPAXiu10FxbpO 0seXVtuDPv0EMinCUf98ksLqjQTRFEjn7gHiMpI= X-Google-Smtp-Source: AGHT+IHHDAQnxryUkRQd0J/N4G2ONc59iCfJ5q1P5zTNnC6+DMwlrUqoWf7HvR58EemTACIUosIxPA== X-Received: by 2002:a54:4691:0:b0:3a7:2456:6af6 with SMTP id k17-20020a544691000000b003a724566af6mr12828146oic.31.1698791120624; Tue, 31 Oct 2023 15:25:20 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id k16-20020a544410000000b003b2e7231faasm42975oiw.28.2023.10.31.15.25.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 15:25:19 -0700 (PDT) From: Charlie Jenkins Subject: [PATCH v8 0/3] riscv: Add remaining module relocations and tests Date: Tue, 31 Oct 2023 15:25:12 -0700 Message-Id: <20231031-module_relocations-v8-0-09b5b720c1fe@rivosinc.com> MIME-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAMh+QWUC/23QwWrDMAwG4FcpPi/Dthyp3mnvMcZIbHk1tPGwO 7NR8u5zyhhh+PgL6ZPQTRTOkYt4OtxE5hpLTEsLx4eDcKdpeech+paFlhqklcfhkvznmd8yn5O brq27DAHBscdRzZ5EG/zIHOLXHX15bfkUyzXl7/uOqrbqL6egx1U1yGGewR8NEpOF5xxrKnFxj y5dxCZW/acoKbGr6KYYGhHNCIG96yiwU1RfgaZMiBQ8SlJsO4rZK9RVTFMsUBg5gMIgO8q4V7p vrmNTfGAG7Y0PrvcX3Cu2q+B2iyGNjMaAVh2FdgqorkJNwWBI2RnNHOifsq7rD2L0G9dbAgAA 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: 9233740010 X-Stat-Signature: kqfsfu4ondu4qgsh638pxmsir9bi6nxe X-Rspam-User: X-HE-Tag: 1698791121-226916 X-HE-Meta: U2FsdGVkX1+wFTuoi/MtlGFaeXucz+F/seH4TMcvEoF1s4xhlqLBJXv6XuWhaNFaRRRtj3kOBjigTPpI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61s6Y1moq6QoVw+CodMmD9vFvzb1opecdTQT+hD9oMG56ryYM/c0PWGmi9D08rr9Uw7urq6NnAkyEB7CCAAKpPd2HhWIh6Nmku8k0m/R13L37IVb7v7mwS3TLNz9+5DaUs9kBkPAMv5Z6jU5zoapYPCbM9tlXesJIdrZlUiTpWM1PxzlO0IkSxvDxvN+yGsCLjWGCkVdE0eXWKzyAg4+fwLmUDZBQ813RawU1/qOymUq/M2zkKEYABl4Td4i2i0AnOLjBMjDYRozm9B8XKM+xhfdBQnBQHhewQVudTmosNgNf2vmfCwnISvUNYKAzuJJAYqTIG3k9fw8NjUz11AqHfr6IVTIGpDoSGtv9AKscMyLiYHEssep5je/hERUk X-Bogosity: Ham, tests=bogofilter, spamicity=0.000081, 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 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 | 650 +++++++++++++++++---- 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, 916 insertions(+), 105 deletions(-) --- base-commit: 3bcce01fcbcd868b8cf3a5632fde283e122d7213 change-id: 20230908-module_relocations-f63ced651bd7