From patchwork Mon Apr 15 16:20:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13630288 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19F1080C14 for ; Mon, 15 Apr 2024 16:20:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713198054; cv=none; b=pRo10OyIjvC1dZPdUGsx2ww2uMztZ2BYU3L/nvOpkKyOoTnwtgKvWbhbVQJeC34hEMrLWJsTlgtW4LsR6KWGjrcdbZSc7yXjHFa5f01uFrFNdCDXSFbuYS/j0KuczeGxuoM8u1Yyp8L3oHmnS/rbocHfS0Vgj0jsM1nHZD9B1P8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713198054; c=relaxed/simple; bh=J2hRLplVLxqxMA6E8xzPuTVt84azbEdp5qYIhiP3Ytk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Aqud+y1g98AefQCQ9uCLaYRgYFTBodVl6gWIEcG5C6355F1Cmfof7ULd0DE8j7amOX3xEMbMIeK9iXHxbtO+huADTKWolY3bNwmLGmDK2y8tqUafebmA9YjZN48NqxfMb/zw3gS8LfBkF/ci7JUYRkNFDIqvxYX3HZyTZCMOuMA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vA/AWKYQ; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vA/AWKYQ" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-343e46df264so1850278f8f.1 for ; Mon, 15 Apr 2024 09:20:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713198050; x=1713802850; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=olXUbwFvWhAHV+rAKgvFMgGXe2ohRAJCIFGXNNajNx8=; b=vA/AWKYQXEkfbEVTu+k74IYsRmp/E8LEwP9CHw1cqQqbiHALGR+vepem1WF1CF2GMv VGCChGMezqFG+63IvqMvEe55EmQX6KBp86C8Gw52QfhteJRiL5k+FaRje0CnfveWybRy vPz44NhV9DWT1tIaCqGaQiiKdsYJemGsaDRPBK2ox/Q0tgghKmOgKWpZD4K3LWYPDcSN admV1yjIU4kf43QBw+AFxqk3Fqst9qo1s962UwqfhqPYXQ+CP2B8P2znPBCDJbW+A0GB tOzBZTRR03yCqYROQ6sz6H3nlKiPH2QLfP160PpaPOXSJaPX4/kr2oMw0wL5h00LDeJB oG5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713198050; x=1713802850; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=olXUbwFvWhAHV+rAKgvFMgGXe2ohRAJCIFGXNNajNx8=; b=Ov9Sb26JvsCzqHnT4vKoJSwanwUNkpb/E0nVUTofwUGa3cWiyS1xkBA7SMynthEWQD hluor/HWvRsUYfc3e3wZ8vMIO5c+d4N7hg2gtkgydY7fuy4eJdGUUoOEPizxcuTfx1IX CbJfdRZu87j1IwZDMabxLZPPldaSb391SWGkykMzcX6bY2KzveiRBhdKI3/Jn3hRGSL/ /IkZnEptAo5VLfmwuBUAxIBBoLphlk4rld937ZkMhcozZgJhWVK61Iw9/JIr8Hs+Fgo8 lQW2++hQFowUW0CDjsqg5U2eKcJLSD2zUzYQgwVK+preUYH8pjzq77xt8mF3qqLTRVep 1Dvw== X-Forwarded-Encrypted: i=1; AJvYcCUgWhqYiMuzmVHEeLmxxrCWt87x3Jgc+GSjI41JdvVlK/0WUFUsowgMZAhXlLL01CO3yEgfZVagT5HDfjNcdqGDT4lH X-Gm-Message-State: AOJu0Yw+UrAyaJA6+m2UWdBbXbanzm78VtMmQF8hmmCyX0Hb1utvYLiq wRYvW89PBQMtulPKjyU7fvaLtdMa5zggbn8Fsa6fDUeMNZHNusMn/M68ZwUNcyo8TNBn+g== X-Google-Smtp-Source: AGHT+IEMgew5ssfIbv9fifRncojzkGtMiA8CSK6jhzA2J79/AgUKJ2Zlkb123g0xWbDErEkxut602gdK X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:adf:e38a:0:b0:346:35ad:3d0f with SMTP id e10-20020adfe38a000000b0034635ad3d0fmr20173wrm.4.1713198050558; Mon, 15 Apr 2024 09:20:50 -0700 (PDT) Date: Mon, 15 Apr 2024 18:20:43 +0200 In-Reply-To: <20240415162041.2491523-5-ardb+git@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240415162041.2491523-5-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=5390; i=ardb@kernel.org; h=from:subject; bh=V4aOk/fz1+fDF2STMCLU+VVQ7hXwmmgXF9wA63lernQ=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIU02+HbVugcM9mKl8d6vVHKWyArYhkd23LGffr56kkK50 KrJ01Z0lLIwiHEwyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgInU2DAy9PNsWrBtzrbqf115 on+fPH3+LXWlpem1sgfxW09/+ntpqT0jw+wZE16KfngW3NcZJpls23ek6l5NQKri/IiImAsLNI8 oMgAA X-Mailer: git-send-email 2.44.0.683.g7961c838ac-goog Message-ID: <20240415162041.2491523-6-ardb+git@google.com> Subject: [PATCH v4 1/3] kallsyms: Avoid weak references for kallsyms symbols From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Masahiro Yamada , Arnd Bergmann , Martin KaFai Lau , linux-arch@vger.kernel.org, linux-kbuild@vger.kernel.org, bpf@vger.kernel.org, Andrii Nakryiko , Jiri Olsa , Nick Desaulniers , Kees Cook From: Ard Biesheuvel kallsyms is a directory of all the symbols in the vmlinux binary, and so creating it is somewhat of a chicken-and-egg problem, as its non-zero size affects the layout of the binary, and therefore the values of the symbols. For this reason, the kernel is linked more than once, and the first pass does not include any kallsyms data at all. For the linker to accept this, the symbol declarations describing the kallsyms metadata are emitted as having weak linkage, so they can remain unsatisfied. During the subsequent passes, the weak references are satisfied by the kallsyms metadata that was constructed based on information gathered from the preceding passes. Weak references lead to somewhat worse codegen, because taking their address may need to produce NULL (if the reference was unsatisfied), and this is not usually supported by RIP or PC relative symbol references. Given that these references are ultimately always satisfied in the final link, let's drop the weak annotation, and instead, provide fallback definitions in the linker script that are only emitted if an unsatisfied reference exists. While at it, drop the FRV specific annotation that these symbols reside in .rodata - FRV is long gone. Tested-by: Nick Desaulniers # Boot Reviewed-by: Nick Desaulniers Reviewed-by: Kees Cook Acked-by: Arnd Bergmann Link: https://lkml.kernel.org/r/20230504174320.3930345-1-ardb%40kernel.org Signed-off-by: Ard Biesheuvel --- include/asm-generic/vmlinux.lds.h | 19 +++++++++++++ kernel/kallsyms.c | 6 ---- kernel/kallsyms_internal.h | 30 ++++++++------------ 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index f7749d0f2562..e8449be62058 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -448,11 +448,30 @@ #endif #endif +/* + * Some symbol definitions will not exist yet during the first pass of the + * link, but are guaranteed to exist in the final link. Provide preliminary + * definitions that will be superseded in the final link to avoid having to + * rely on weak external linkage, which requires a GOT when used in position + * independent code. + */ +#define PRELIMINARY_SYMBOL_DEFINITIONS \ + PROVIDE(kallsyms_addresses = .); \ + PROVIDE(kallsyms_offsets = .); \ + PROVIDE(kallsyms_names = .); \ + PROVIDE(kallsyms_num_syms = .); \ + PROVIDE(kallsyms_relative_base = .); \ + PROVIDE(kallsyms_token_table = .); \ + PROVIDE(kallsyms_token_index = .); \ + PROVIDE(kallsyms_markers = .); \ + PROVIDE(kallsyms_seqs_of_names = .); + /* * Read only Data */ #define RO_DATA(align) \ . = ALIGN((align)); \ + PRELIMINARY_SYMBOL_DEFINITIONS \ .rodata : AT(ADDR(.rodata) - LOAD_OFFSET) { \ __start_rodata = .; \ *(.rodata) *(.rodata.*) \ diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index 18edd57b5fe8..22ea19a36e6e 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -325,12 +325,6 @@ static unsigned long get_symbol_pos(unsigned long addr, unsigned long symbol_start = 0, symbol_end = 0; unsigned long i, low, high, mid; - /* This kernel should never had been booted. */ - if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE)) - BUG_ON(!kallsyms_addresses); - else - BUG_ON(!kallsyms_offsets); - /* Do a binary search on the sorted kallsyms_addresses array. */ low = 0; high = kallsyms_num_syms; diff --git a/kernel/kallsyms_internal.h b/kernel/kallsyms_internal.h index 27fabdcc40f5..85480274fc8f 100644 --- a/kernel/kallsyms_internal.h +++ b/kernel/kallsyms_internal.h @@ -5,27 +5,21 @@ #include /* - * These will be re-linked against their real values - * during the second link stage. + * These will be re-linked against their real values during the second link + * stage. Preliminary values must be provided in the linker script using the + * PROVIDE() directive so that the first link stage can complete successfully. */ -extern const unsigned long kallsyms_addresses[] __weak; -extern const int kallsyms_offsets[] __weak; -extern const u8 kallsyms_names[] __weak; +extern const unsigned long kallsyms_addresses[]; +extern const int kallsyms_offsets[]; +extern const u8 kallsyms_names[]; -/* - * Tell the compiler that the count isn't in the small data section if the arch - * has one (eg: FRV). - */ -extern const unsigned int kallsyms_num_syms -__section(".rodata") __attribute__((weak)); - -extern const unsigned long kallsyms_relative_base -__section(".rodata") __attribute__((weak)); +extern const unsigned int kallsyms_num_syms; +extern const unsigned long kallsyms_relative_base; -extern const char kallsyms_token_table[] __weak; -extern const u16 kallsyms_token_index[] __weak; +extern const char kallsyms_token_table[]; +extern const u16 kallsyms_token_index[]; -extern const unsigned int kallsyms_markers[] __weak; -extern const u8 kallsyms_seqs_of_names[] __weak; +extern const unsigned int kallsyms_markers[]; +extern const u8 kallsyms_seqs_of_names[]; #endif // LINUX_KALLSYMS_INTERNAL_H_ From patchwork Mon Apr 15 16:20:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13630289 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89D1982491 for ; Mon, 15 Apr 2024 16:20:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713198056; cv=none; b=ZrVE0wcDMQ5VfgeTLEOyx8i/GfLbtz3bBI2lYn6frk6QTd4/0/q2SsfXP7ymw0jqz5TwCTjivIa+TYxI21YRkgkbSlu0r/K4LstVnZKRuEZMmqTdsObm11XXVDQJ+qqocLb9y1vTXjI2zGsDejSlMJa0R+jspiA+R0snuZ/qzI4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713198056; c=relaxed/simple; bh=DkpORLakWw5z143JLN76w0dBh5hI4i2x8TQpiySZiJU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=g6WZyHW5D1Sxg/nJwMlNtptE8huUHuSidEVUojJsO8Jp0NZHDLNl9ET+MZgKZwNDdGN5egl9guqTldfIjfylA5d81SrkpR7DNOTqa5S7yK6brPqmhmIJQbbpa9Y61rGHROjTOoolSP4srO4PLvLKC7S27EsTiQ6gpM/ALalDIIw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2hTyWwGO; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2hTyWwGO" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-343c6a990dbso1143950f8f.1 for ; Mon, 15 Apr 2024 09:20:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713198053; x=1713802853; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=nirUBk2IWA4nfF0nBoVFV5ytdr6rujdVL1Z+GvYP3xQ=; b=2hTyWwGOiijyzdkgJKWjBYRBN0juykEX/pdXT82Hk9M9ehDh2lmbNVxm0S2eLeXUIc 0+qfeZtBQf7SBll9UoDuyRVUUuPc3pFwGr/Ucds+dH2oT51UZ4MFGot+K5Sm1iq1mWon AzW92YaJKuc2Nv6VxEvQlZNRSQEyFc13HslSF0urNT2B/hBPMFLJvx4TD1cGyAMzzGFm HjMcB9hPy5mVyzi5jWc6YSJ+OobsWx3VXjc/Ett7YQAuoKkK6HfOHPDAXvLrhxT8NoWp PDzkJU0cqCvbbWRFckyzJD7J6fh5+soINdykIWTEevOUZMTFxJi73l7okeddtYN488E8 lJYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713198053; x=1713802853; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nirUBk2IWA4nfF0nBoVFV5ytdr6rujdVL1Z+GvYP3xQ=; b=RhjHIQS8X74TCbFzgsLH5/JZLgzdJbgMQjyP46ZsWxS2Mb4ILNp1d0sa4Gx9f0EltN KM2L28Ly6CW5D+EZyLdWu3GK5DZCuO4mPk7AVpcRCoa2ci10C7CbbAtwqKhCTSDJ0Se7 uInDVtFQO0FaVtzy3pEvXX4bRvejLMKRguUoY95rEE3Nebd7Rf6XsKMAISsKQawcaAb1 2DO2kU0nLki0mVZ8IkttpmZwl2TpKR7H9G+pK9wCHud/FfYTdN3wfb4udrRh5fbjgBST tyCbPpVlNka/DFOeSErKRFsrHVjSabCbfw3s3jbFdXgXZ6GZ6zS7s4qMLN5Aib5g9U/Z a7Wg== X-Forwarded-Encrypted: i=1; AJvYcCVCAQEFuqPvTdgqt6e0S5uRKFsCO0Smc2IuzSyM1GN20uqYg2yVq5ZhOPkNvsFdzZUMpFX1uM58sNV0vMfYeFCNcTTt X-Gm-Message-State: AOJu0Yznfb2NClB9CkS7PyEupi+5yv6YdoiLdGlFBFnIyqOVPoZet/Fs XlYd68PY8q32lDFKxG6yo61febZOA0/wulvG+9xHg0P8EOTm9yZ0O9Pnr/4rIC3hCA8lKw== X-Google-Smtp-Source: AGHT+IEG7QWukhC29WPVFqvJ0sHYtNkZBwbl+gzNLcWFTahlxsENo50bU+lqtlSx0pNqfKHmhlU8U2H/ X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a5d:688f:0:b0:343:8a84:475b with SMTP id h15-20020a5d688f000000b003438a84475bmr224wru.6.1713198053081; Mon, 15 Apr 2024 09:20:53 -0700 (PDT) Date: Mon, 15 Apr 2024 18:20:44 +0200 In-Reply-To: <20240415162041.2491523-5-ardb+git@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240415162041.2491523-5-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=1659; i=ardb@kernel.org; h=from:subject; bh=iEnd1kipFuUW1SJUvbH5tb3oJXutwpN4deu2O40wcts=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIU02+PbdQ+w3umb8jUpXzD++6X70lFcO50+yRerp3Fu1w u7gZeuEjlIWBjEOBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCRgwsY/sp9Lo7xVcwX3drs /0us5puVtGicWcs2px2BT80/9DcujWVk6BX4vJ7rUV3f3I1vZhU11U9/F3z0+kGJP+zGzA9Xyng vZgcA X-Mailer: git-send-email 2.44.0.683.g7961c838ac-goog Message-ID: <20240415162041.2491523-7-ardb+git@google.com> Subject: [PATCH v4 2/3] vmlinux: Avoid weak reference to notes section From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Masahiro Yamada , Arnd Bergmann , Martin KaFai Lau , linux-arch@vger.kernel.org, linux-kbuild@vger.kernel.org, bpf@vger.kernel.org, Andrii Nakryiko , Jiri Olsa From: Ard Biesheuvel Weak references are references that are permitted to remain unsatisfied in the final link. This means they cannot be implemented using place relative relocations, resulting in GOT entries when using position independent code generation. The notes section should always exist, so the weak annotations can be omitted. Acked-by: Arnd Bergmann Signed-off-by: Ard Biesheuvel --- kernel/ksysfs.c | 4 ++-- lib/buildid.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/ksysfs.c b/kernel/ksysfs.c index 495b69a71a5d..07fb5987b42b 100644 --- a/kernel/ksysfs.c +++ b/kernel/ksysfs.c @@ -228,8 +228,8 @@ KERNEL_ATTR_RW(rcu_normal); /* * Make /sys/kernel/notes give the raw contents of our kernel .notes section. */ -extern const void __start_notes __weak; -extern const void __stop_notes __weak; +extern const void __start_notes; +extern const void __stop_notes; #define notes_size (&__stop_notes - &__start_notes) static ssize_t notes_read(struct file *filp, struct kobject *kobj, diff --git a/lib/buildid.c b/lib/buildid.c index 898301b49eb6..7954dd92e36c 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -182,8 +182,8 @@ unsigned char vmlinux_build_id[BUILD_ID_SIZE_MAX] __ro_after_init; */ void __init init_vmlinux_build_id(void) { - extern const void __start_notes __weak; - extern const void __stop_notes __weak; + extern const void __start_notes; + extern const void __stop_notes; unsigned int size = &__stop_notes - &__start_notes; build_id_parse_buf(&__start_notes, vmlinux_build_id, size); From patchwork Mon Apr 15 16:20:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13630290 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3AABC824A0 for ; Mon, 15 Apr 2024 16:20:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713198058; cv=none; b=m9dRbPcpXVxOWDmsfRw6o+ElZFnEd0xrCtXMjoXsxvtj1JtvWdDkXGt9sspMG0NZfl50SkGzRmF0CTzDgIg9Wcza4zMTys3vLZ6SJ5j4H/KCOUdEHCt31PYd8696Vlhx5u8TFDKzPU2UelakxKw1AvT4ptQY4VQLoJIQ5yBuaQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713198058; c=relaxed/simple; bh=GbAxPIv/vbclXmPWPZ8Tu9s+wJ2dryRysJiHKFw6Xpw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Pnt2/Ey7e9HrDZU83Q3JaF8DLjEIJSg0tQHkYV7/EjgIaVWJm/LK4qjA/UMDW0xn9iAFVPA7lOyCyeHsUFyMEHa+6eG9SoaqH3DVS/LcQJMUOM4WsB+XzARZTbxqHn56HjhsJR144gTzP4JAKPIoKPvXV8di7X2xfN0VNPdRcDI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=olNOjuVN; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="olNOjuVN" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-617bd0cf61fso63649577b3.3 for ; Mon, 15 Apr 2024 09:20:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713198055; x=1713802855; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=WHqqRenBwHH3dYNpNtPvujl3JPCakWggIzfiR8Bh9XQ=; b=olNOjuVNcxnUhXkcnnpsE/1bvkr4YCU73TVdBmvsBCde33BwBW5oLO/oQjGbGT4MVU iy0+0pmV7z2AzNN0iJtqrYRY26rIwKSKgg3WX735Q3RCQPIel7LLoXL2NTAm84QJdS/4 s/a8b1Ntmoc/s+fpF7CXx8p4IPxxiiVmjzxWRflAAnJ10AIHz1rs8lwNlwDjdqrqDPGb qQk4JhqYAvnrZedkPyAed66gG/oFnrC5mLWKIDF0xare5irK/VjCPSN7RJIE60S4ot78 l7dLM1L1SB6G4XKVfiF62m++pm4IfRb5Yb7Y9uDj5teNNcaPyyzm/0m1aVUDzhf8uW+t WTxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713198055; x=1713802855; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WHqqRenBwHH3dYNpNtPvujl3JPCakWggIzfiR8Bh9XQ=; b=slSq0jicEvREkc2md+oI42iNFUOd0sPrRWWswLJqDeZBm8ZL1WHnFZ0cejOnLyvENU 2nQhCOUrErX1WGzx7Khjj5FbnbweWC/5V9bMGnkpKLuBhhNGw96U1jiHWsomcckzd+PD DDihpk1wqaS7fa3JEC5O2tpWq7hlDKn4rbHnhDJWDP6kjGV5r1AbDDf9FdfppRX5XqOH Gylh+FUf0hsBY4ihMtkYl+Vi72dFQ+GxrF1UWQA8a3h67r2WY1AuAlUShrST2/ob1rba OFrsxF0TsxoLvKMaL45elyYCBlkHJI2CQ1HVYCIZWKLre1viDOarSlDodIN4Y40fl/tC TQug== X-Forwarded-Encrypted: i=1; AJvYcCWa8n8po8Un5Vebegfva+vO/NgKKGGDuXAWdaMAFET8ABMwDC/uprjEVZ41tTvaHauYncutYLqTukyfZoTE35DAB2Ie X-Gm-Message-State: AOJu0YxpFOL4I5AYT0rgL8D/DVJWx2Pi7pwiwxP+u8Fy9ORPSds8TYnX dm04FTUia4YWwi0A75ymryitJH5LVusfe1ZuoxuNYMtmw/Ek3gNBqFU6uYmtTEsLDiMxyQ== X-Google-Smtp-Source: AGHT+IHNIhta4/6qEKdeuxUljM7MuC6zhk7FC1Wn5VGF0YXerg2GpmyYatVfwOIpnK57xSofd22De4no X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:6902:150d:b0:dc6:cafd:dce5 with SMTP id q13-20020a056902150d00b00dc6cafddce5mr3570371ybu.12.1713198055314; Mon, 15 Apr 2024 09:20:55 -0700 (PDT) Date: Mon, 15 Apr 2024 18:20:45 +0200 In-Reply-To: <20240415162041.2491523-5-ardb+git@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240415162041.2491523-5-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2668; i=ardb@kernel.org; h=from:subject; bh=r5HeLLaiZQAh9QWD51AJtOhyniRpUOwKFejoofTylQw=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIU02+M7jo28Fo36cTxHbcXHZE5f2CWJ3C1mm+lxU4Ww4e rA53Ni2o5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAExEJZzhr8CHbGvGxUIxhboC bnceXbJce7Hx7oOShfraG7sD76299I2RoaeEq++zoZeohsI2xcNPzws8dNRcm/ZAlknm1+JvGV3 ejAA= X-Mailer: git-send-email 2.44.0.683.g7961c838ac-goog Message-ID: <20240415162041.2491523-8-ardb+git@google.com> Subject: [PATCH v4 3/3] btf: Avoid weak external references From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: Ard Biesheuvel , Masahiro Yamada , Arnd Bergmann , Martin KaFai Lau , linux-arch@vger.kernel.org, linux-kbuild@vger.kernel.org, bpf@vger.kernel.org, Andrii Nakryiko , Jiri Olsa , Jiri Olsa X-Patchwork-Delegate: bpf@iogearbox.net From: Ard Biesheuvel If the BTF code is enabled in the build configuration, the start/stop BTF markers are guaranteed to exist. Only when CONFIG_DEBUG_INFO_BTF=n, the references in btf_parse_vmlinux() will remain unsatisfied, relying on the weak linkage of the external references to avoid breaking the build. Avoid GOT based relocations to these markers in the final executable by dropping the weak attribute and instead, make btf_parse_vmlinux() return ERR_PTR(-ENOENT) directly if CONFIG_DEBUG_INFO_BTF is not enabled to begin with. The compiler will drop any subsequent references to __start_BTF and __stop_BTF in that case, allowing the link to succeed. Note that Clang will notice that taking the address of __start_BTF can no longer yield NULL, so testing for that condition becomes unnecessary. Acked-by: Andrii Nakryiko Acked-by: Arnd Bergmann Acked-by: Jiri Olsa Signed-off-by: Ard Biesheuvel --- kernel/bpf/btf.c | 7 +++++-- kernel/bpf/sysfs_btf.c | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 90c4a32d89ff..6d46cee47ae3 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -5642,8 +5642,8 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat return ERR_PTR(err); } -extern char __weak __start_BTF[]; -extern char __weak __stop_BTF[]; +extern char __start_BTF[]; +extern char __stop_BTF[]; extern struct btf *btf_vmlinux; #define BPF_MAP_TYPE(_id, _ops) @@ -5971,6 +5971,9 @@ struct btf *btf_parse_vmlinux(void) struct btf *btf = NULL; int err; + if (!IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) + return ERR_PTR(-ENOENT); + env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN); if (!env) return ERR_PTR(-ENOMEM); diff --git a/kernel/bpf/sysfs_btf.c b/kernel/bpf/sysfs_btf.c index ef6911aee3bb..fedb54c94cdb 100644 --- a/kernel/bpf/sysfs_btf.c +++ b/kernel/bpf/sysfs_btf.c @@ -9,8 +9,8 @@ #include /* See scripts/link-vmlinux.sh, gen_btf() func for details */ -extern char __weak __start_BTF[]; -extern char __weak __stop_BTF[]; +extern char __start_BTF[]; +extern char __stop_BTF[]; static ssize_t btf_vmlinux_read(struct file *file, struct kobject *kobj, @@ -32,7 +32,7 @@ static int __init btf_vmlinux_init(void) { bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF; - if (!__start_BTF || bin_attr_btf_vmlinux.size == 0) + if (bin_attr_btf_vmlinux.size == 0) return 0; btf_kobj = kobject_create_and_add("btf", kernel_kobj);