From patchwork Mon Apr 15 07:58:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13629579 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 AB9972D058 for ; Mon, 15 Apr 2024 07:58:47 +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=1713167929; cv=none; b=ThOYvRbPzYXRZ5ZXYEodAaokrcjYx8AtIrqQI6L/jLByxvQzJOzqX4GS8ZTNB3Nsg+42vD4H77Z1Yw3UAMrYxnMFrlaFQh/q7zFwc/IDFKkgkIzvs6yUIMLuLsiaO+5tLHBJhkRED8pEdSB6X50zVPDZ1Vm6ot0ETrGu5/TzMW8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713167929; c=relaxed/simple; bh=J2hRLplVLxqxMA6E8xzPuTVt84azbEdp5qYIhiP3Ytk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZR6rFfkV3r0SGCqqn6hw0kRCSdn0PlUpxYfTYpLUHPT2ZDT2LVZBkiZmcEw5LQvBh5mePiQE+onzvCeDgj8Hk6TDb9YQMdsvoiRazjmUhfhO0KmXJxSuSxx4A0e9y/Zu8BPDM7ikdI3GcuqVMUuYRWk5vIaNtmCCUbuwWH6MPJE= 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=4o/R5ay3; 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="4o/R5ay3" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-34619c30076so2232623f8f.1 for ; Mon, 15 Apr 2024 00:58:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713167926; x=1713772726; 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=4o/R5ay39sb9LNbrjYvQuZIw3KGQanF0UaQQ0TwM9FTmkyJQwEILAduA5WiEoh7F14 etT8hQX7ye9CIpurM/ex3KML7e6EhVLamI6GHF5lfSEdD4xDXKJ6cR1q6s+u/reYNXTs pJ+pIfrTPp4SPNtY4G/WKiArnxFAO/BjLSaVtNUj0zzWA1V/Scpb+Ze1dwHuwtvJx94y QrtdLbbO4Hc8MipCf65diB//q+P2PUHXQ7CBI0z6AJwczJpw4mXGDHK29/Y2Itxi8nXD RCCEwJ+JlMEzpLahWFSH8TsfZZgH/V8kumQNyY2KW/lZzU6/JtWrsDa/t/f5POhHvv3a lxnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713167926; x=1713772726; 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=C42hIIbOfG4JfLVU2mAUhx2PM79BYqnYRAmYE5eJl76tzUCV8VusiXDUuG+B9PMmtE E5Jb1rlxiuyhmn/eaSyQDDUPnCoF0sIMNytEy3sCOZmAlnNGdPzwhIwH68Ar1TToeJHM G6v98zQJeh68NwPZjxRNxrVaYfW7rIAoO0Vf+p90LAypX6MrZTqHOu6rhLEixbTGKmrp RhYV546qjXYqmLuowlqie3VB6sTIwlusIf/FDLkHzZ+3l+KQgcvPpl84ymOUIPGJTniQ pBc4DYedG1IElB4eN9A27FCuCVx5aEFla3XOqgzvYUMYZIf8I/ynvD8GEYtdaPgBkZRl allA== X-Forwarded-Encrypted: i=1; AJvYcCXDCCC2wjf//xLCxtpamjk6iAet6OXN/FGDF8OYFbUJ0YmP6Kr1fskzQBeQjMjxtuV+jKlLVvfIgtLK5D/RpsKdbA1N X-Gm-Message-State: AOJu0YzsBtfDyhnj2o62R3Bbw/P67BZ4GZq22AllfnVZdfUez7Hv9oi+ R+XO8kkiCy/fNOWWIy4gIVyFAFGYbUqueQZjxu1u6GTtRhRq27yqFJmIdLssFJxAYkSxAQ== X-Google-Smtp-Source: AGHT+IFwDxTcJaKceCQkM1kYIKiVHmoTc2lMM/DBuKq1nDl6RA24l/dE3t+Vmaqnmzc+9t8IXzRS1sNi X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a5d:6043:0:b0:33d:5804:7f6e with SMTP id j3-20020a5d6043000000b0033d58047f6emr18071wrt.4.1713167926026; Mon, 15 Apr 2024 00:58:46 -0700 (PDT) Date: Mon, 15 Apr 2024 09:58:39 +0200 In-Reply-To: <20240415075837.2349766-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: <20240415075837.2349766-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=owGbwMvMwCFmkMcZplerG8N4Wi2JIU3mnn7VugcM9mKl8d6vVHKWyArYhkd23LGffr56kkK50 KrJ01Z0lLIwiHEwyIopsgjM/vtu5+mJUrXOs2Rh5rAygQxh4OIUgIkcE2BkOBbydmeS7RWFDW// CitHCJ+MNrAteHPxKqece02DZq+9F8NfyRdpwiwpizTy9DS8a2d6uio1u+x22l+3rsCRiWO9zWl WAA== X-Mailer: git-send-email 2.44.0.683.g7961c838ac-goog Message-ID: <20240415075837.2349766-6-ardb+git@google.com> Subject: [PATCH v3 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 07:58:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13629580 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 D2A58364CD for ; Mon, 15 Apr 2024 07:58:49 +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=1713167932; cv=none; b=AOA7S4xSQ5rKEuBB2TomKL5/Jyw+wZN+iIbzVufCVXwOgxmzMXrW+CvKIDFv7hb6rFcy47fe+ftV2ketbZBy0qLe5hsckiIhyYwLbOqvQ8CfDmMYNJsDLfpNntRw5NcsdbK7ECwILP3jRleC05SClt8d2YrEyMsobHI5Sli9dRQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713167932; c=relaxed/simple; bh=DkpORLakWw5z143JLN76w0dBh5hI4i2x8TQpiySZiJU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jRYcgyDMNGDKejIFVfumzup0PoRo2IOtL6+2DXYqrZeM1jS60xyiAZWizJ+0u+rt0z9VqczwwNppUHgwdeKDP3TafZpc5c/O+NaBIkzp5D7SY4+hewsqFXfrsD+O2Oh2+WcWZJOBnhJA28PFM0whOenpQQXO80y/Bmsnvq2tb1k= 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=k4qEGlA6; 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="k4qEGlA6" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-343bb240f70so2030569f8f.1 for ; Mon, 15 Apr 2024 00:58:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713167928; x=1713772728; 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=k4qEGlA6yPmLSRUWj3VhIDRGReCoCkra8/37M7U5wzOdaaDOGK7KtyjVh1URO70ByQ YK5CCqJ5/e2QQ87Hwdb9R4Lu3Fi+DKS3t7OFZRlS3b68GRtEHgTRYc03Vj+i8qwR1JgS 8oIfVBALC8OCOnvQGoSmsyJ5v1/1GCsLk7rbG02aSjb6oO6TODqx6WI6FJ+nNSDotOhQ +cIieU2ZTrsYZpkz6ffTJLAS5YtDe7U+VgjO45A4h5ZtrBiBQOs5PjvBTr1+FSrAAz7D 2Nj1ITgfvJZU+L8VwrE8Md5S6BXbDnlkDOWQZpXPuOQPyLfFJwY8S7OLxKqR3jDL9Zw2 9zfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713167928; x=1713772728; 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=O6eDShoazZ5tljHQyjxGG4kDofgP2SyZA5LreBUOAQQHhu+8oUmFxpXQAVX0pgeRfP HD9vDDap/Qzipwrjo9JYDik6rFTLCSv3kNBXdub0kcy5BM7ky/cZILnoh6GR7n7D9WNo 5zmCVThIzdOcgr+mrF4aB3UfYGWQbI1jJfyIgyX2IbsKOotzGvgQRD47ci9qrUiGPk0U SceXMSD4w/OmWTF8Efp7ezGIP121YnFa0ULDAbO1iFc9MBPbA/2xcepROVRD36H0FejD ifshpwCPp6eljxGfIsMzRyKX+FPZZ0BzQC0MX5va6oouoWNtVKvYt1KtSUP8Cpw/keTL Eh3Q== X-Forwarded-Encrypted: i=1; AJvYcCUmK+yZn+QVMtLlKhkAq9JdN0VaB6OXTJdDJIXOF4cx3KNhtHtakQMSDyoNFCjBD7Ug/K03W9K2bO2cCqocZ16oEVwj X-Gm-Message-State: AOJu0YwjqUzvKMcceiNudq9ItwflJTMEe5KLoisLr1c8alNNxSRYspJc SiZfLpXoUuUFQICOLFbft7LBv2fZW4KgSI6nYx20M3280Doz33hLTEGHPXXsNgUYI+idiQ== X-Google-Smtp-Source: AGHT+IHzVTEcbzhRB0NY+AesRBH9T0OrQX3XqSzTwn93e3fEP/KlQT+PWFn3pPDq3hFeDtqmO+tmVi7S X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:adf:f2ca:0:b0:343:668b:be05 with SMTP id d10-20020adff2ca000000b00343668bbe05mr21274wrp.3.1713167928341; Mon, 15 Apr 2024 00:58:48 -0700 (PDT) Date: Mon, 15 Apr 2024 09:58:40 +0200 In-Reply-To: <20240415075837.2349766-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: <20240415075837.2349766-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=owGbwMvMwCFmkMcZplerG8N4Wi2JIU3mnv7dQ+w3umb8jUpXzD++6X70lFcO50+yRerp3Fu1w u7gZeuEjlIWBjEOBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCRJ64Mf8XPzF71ff/fFSHP jscwXJcx+urf8D5r276S3g3vGGWPPznH8D+FlWu3ew9/xPHLtTaJmu9bXyRru679z7f1Z9muwK/ uMqwA X-Mailer: git-send-email 2.44.0.683.g7961c838ac-goog Message-ID: <20240415075837.2349766-7-ardb+git@google.com> Subject: [PATCH v3 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 07:58:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13629581 X-Patchwork-Delegate: bpf@iogearbox.net 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 19E4B376E0 for ; Mon, 15 Apr 2024 07:58: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=1713167933; cv=none; b=UH1NpqpRm/9cxRh2ggwM8gv4rkrpJyR5K9iRxWbQrf5G1dS6Mlg7AiaVFEjIQKckK5GH5sEr1Akrn4zBCNzDAsgIzpVf0Sv5NnKF6NUoTNb+eyUPQOOluI8uhxL4n2a6/MOnEl5gSu5dwGzfMU6/Uw3SvHQzBMlaMM1rpwlIQQY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713167933; c=relaxed/simple; bh=pSFwalqd3s5K1XjaH3Dk31u90WGesAg2Ef0b/1JvrU8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kdIsbhB2+hjeBKxg2p/gZkq09p8MR48MxmSQYtx2sv7e4SM0duO3VCjmi/S0XeBwd8y/iQSsiXM+2Jop5PZ2GkctfN7U9U/yY6kC7KXGiYUOIc8c/VwpSoqruUl0p+nYe7Y0Ed2KbBPcqeIIOoVONGjLSO6A2esLdbOQ/FkuKqI= 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=1HAvSx/M; 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="1HAvSx/M" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-343ee356227so1871564f8f.2 for ; Mon, 15 Apr 2024 00:58:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713167930; x=1713772730; 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=xg18GyqyaeWty+ItqB3Iy6JPUVdZJOOF1IDiLwlHKNc=; b=1HAvSx/MirLi8qZCPzjzHBXa65Ssoi1NxSr1WbXE1tcQ/ICSMSEvcygfVcfd1Nrepn AG8rvW3Y2OuQ/bs/JreqLGS5lySPq6TSbz+k8k8tZlQnjS3ZdsinDqMBuvyIIOgjVDTg Jgq4+6BGPyW9roK9uwf/YHo9pJZUnWF16e7dK3AIvAmZIkJUeseFYMWNaOx5nidl+zOU A4QCbzitJSEGzEyDULWUkbwuY2Ayy9rZKiyNvoXHNQEqi8NjCoE6NVaTDXifoOtVZROd 1qa4UxvQfpgqw77SpQISVBsThS5PSArm4TrBY6K+q8As7oygPyoGY+yBRzHuTEjq4ZJZ 6PSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713167930; x=1713772730; 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=xg18GyqyaeWty+ItqB3Iy6JPUVdZJOOF1IDiLwlHKNc=; b=b6y567XXIfKeelyhjRNCc5LtmjqD8NP7pTZ92f3VrJVau4qMYD3VOIJHBiHQTJOI2s XjypKmcJzM+IdOQWAWr8JTDjbuwQj4XAJevofZSqSl0Oj8clieMp5TzkVSaLKSoPjBwY jxTm+IUanFUbDBiIeumVvubkg/Ao/p5tYI9Ae4ZircTktHjtnxlm2wbSvKYgyQYMfCle niUAj9f2pRSuGnRoSlzo9xI2JGBh7GbJubHoousWjjUJwviV+MBTLjihTFW462RmWmF1 kYfpeESULgcASbN/nLcfHp5fQ9zXsZeECeOBKHn904sYlLNPmWRAl3QzqtLmLLlJfO/b /etA== X-Forwarded-Encrypted: i=1; AJvYcCXj1kif3WQJ+ZQ8eUywQvPN9bl8QbnweJzq3vcG6tH7tvUQCdb70qGA/iNJ3jA+fDyzMxKPx7/krFA2QTOaGR/mEQ48 X-Gm-Message-State: AOJu0YxFYDe4oHC4sdZd71nxYF122jgpEUctIXG8xIq6ChbE48vusYS4 xB0qBSOKWKImizgQ4q2fc/bZ9kWDAkM6fCUjayz0632mQec1JsHKU7SQf9Ism2iV+GnvGA== X-Google-Smtp-Source: AGHT+IE97GkpOEK9YN53r8TW33m7d47GDaBE4OxjbIYnBf83acHkn+Q93xPJ4LiaGWK3NiKjNb4cPYZs X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:adf:e351:0:b0:346:5932:a05d with SMTP id n17-20020adfe351000000b003465932a05dmr19116wrj.8.1713167930559; Mon, 15 Apr 2024 00:58:50 -0700 (PDT) Date: Mon, 15 Apr 2024 09:58:41 +0200 In-Reply-To: <20240415075837.2349766-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: <20240415075837.2349766-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=3445; i=ardb@kernel.org; h=from:subject; bh=Kj7lwSufbF5VMIDGpWufnrtYiMGkl56Qhtk1J6ZR1Ew=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIU3mnsG81wt++v+NZFuZ8fnStSL1Se6xebsflCjflPkhX 3RywmGJjlIWBjEOBlkxRRaB2X/f7Tw9UarWeZYszBxWJpAhDFycAjCRBwWMDAslmD5NmVr72WvH h01cW2oeF76f5MB8fzHHv63X3Fnb9bQZ/sdxMK/3LyxKd/4XrHicbfaGY/l5q7bNjjr9OOdygW/ ZKh4A X-Mailer: git-send-email 2.44.0.683.g7961c838ac-goog Message-ID: <20240415075837.2349766-8-ardb+git@google.com> Subject: [PATCH v3 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 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 in the final link but not during the first linker pass. Avoid GOT based relocations to these markers in the final executable by providing preliminary definitions that will be used by the first linker pass, and superseded by the actual definitions in the subsequent ones. Make the preliminary definitions dependent on CONFIG_DEBUG_INFO_BTF so that inadvertent references to this section will trigger a link failure if they occur in code that does not honour CONFIG_DEBUG_INFO_BTF. Note that Clang will notice that taking the address of__start_BTF cannot yield NULL any longer, so testing for that condition is no longer needed. Acked-by: Andrii Nakryiko Acked-by: Arnd Bergmann Signed-off-by: Ard Biesheuvel Acked-by: Jiri Olsa --- include/asm-generic/vmlinux.lds.h | 9 +++++++++ kernel/bpf/btf.c | 7 +++++-- kernel/bpf/sysfs_btf.c | 6 +++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index e8449be62058..4cb3d88449e5 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -456,6 +456,7 @@ * independent code. */ #define PRELIMINARY_SYMBOL_DEFINITIONS \ + PRELIMINARY_BTF_DEFINITIONS \ PROVIDE(kallsyms_addresses = .); \ PROVIDE(kallsyms_offsets = .); \ PROVIDE(kallsyms_names = .); \ @@ -466,6 +467,14 @@ PROVIDE(kallsyms_markers = .); \ PROVIDE(kallsyms_seqs_of_names = .); +#ifdef CONFIG_DEBUG_INFO_BTF +#define PRELIMINARY_BTF_DEFINITIONS \ + PROVIDE(__start_BTF = .); \ + PROVIDE(__stop_BTF = .); +#else +#define PRELIMINARY_BTF_DEFINITIONS +#endif + /* * Read only Data */ 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);