From patchwork Mon Jun 3 12:16:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13683731 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (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 95F8512A142; Mon, 3 Jun 2024 12:16:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717417015; cv=none; b=GBvEget0004PwQuWKThbsus7xpMxOghfy9h39dFR55VPXghrsL+KFOO/7kvU2n0JzLSYlat7xtCLYuR31wFau2kzMT1yJcwemuy8kruR0TSKpeSbfwI3iQq3KUeV1kWO0LelZU6fS9h4OWJMOBj5hjrbMYERKVW3uZeC/WZTjtA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717417015; c=relaxed/simple; bh=wJbBb7m2T+NjpvyKs6LFLzrHy6bKzV3H5a5pbGj4oT0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Le0aDiXrjdSQ8mhxpt9r7ed3RMYE5CE684bfsDAHD6n7ASM0ZNgkUoQ/U2L1QytdK4W/nmi+QQ7ogM6AfSsyepDtOPsXG5E8OOQYdQ6kzfguU19eC1z9/5ejeYNtA3GcYCgmvn5jh8OMxfL+sdqa6n2smlDmFR2NVm0ctg69R6o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=GnTuWlvj; arc=none smtp.client-ip=209.85.216.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GnTuWlvj" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-2c217727902so941638a91.1; Mon, 03 Jun 2024 05:16:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717417012; x=1718021812; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0sd34brJz2LmRufAc75kDVDkXK/x+3FfZcQK4xNLOc4=; b=GnTuWlvjy4mu0rBtLd0xqQsBBfvo1oDUHg1g/1yORnccAeD4l3eESN8jN2o3f0ZqtJ g40mIG4jJs9Sr3Ie14dYboZdUGQROMkVrP2Tk+OYynF71Vf/JRbx17aR8xM4M4G2Sqf+ OM7vOet2/j6aEZHLGxeVk5bfBnGO//vWyuxz9Ovg48e5vv61uga9hFxfkskr/vC24U9J ryVvSbH4PLasiDRJwyVHmJ0T/aZ9b7OXVpvgBETn3b0DNRB3nmDenTG0hOJLTWOEm4qj KO42HOiLi7Tqsi8FoDANvhR1B2MeXBe9TGd6wfMzY0CgK7JNF7reI6dVcmXneum2ke/v 3B2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717417012; x=1718021812; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0sd34brJz2LmRufAc75kDVDkXK/x+3FfZcQK4xNLOc4=; b=TKy66D/IJyKLel4sERRkxypvqh+6h/ZYLhPZHXXIj+i6RC6PdHpWFAZCJlI5d2KAm+ SbBjOD3wauoVXaK+GvfWQ+Hua+NTUsOvKGaADrHbYQT4RZrzjRWuoN5QVHZVBS3GAQnI CcDxkMiV4V1YKim5YgiOmYN4n719FVOl4RzWzIRnw7cZJXwm5N+EE8VHnIk13cAAUzvn LsDZjXI8W5vyNezDK8jepgDaFENw137/cHwB8gA7tE0uWgBZkeNEuRFpiD7XbfHDnQ4C VXra2oCCIZC/xjA50IKjqLwwaclYOjxgUn8XPuyAChjY/xywCkWoFOE/0YBmrFWzoxd7 lppw== X-Forwarded-Encrypted: i=1; AJvYcCWbbdYlSWzVo4pc4DS0CdfQVbD20S9ojqRHH/4KWpKxphU72klmVlNf++IACG0bRkVZpF3nRHVzrwEvyOXoqAButDIrgabi X-Gm-Message-State: AOJu0YxyP1lvfRGmattJFazkhM3Yqfh5x02+7JfDHN4g7JbCXS6GjIs6 7RWq3TdndZw+QwuDGeGEqYpptLw3YHpbHFR3ruGhMoq8ub0ao3GQ7Dcf6xVy X-Google-Smtp-Source: AGHT+IF//EGWHWC6xMZwK91WkBMRsUgBKoKacOYgQicIaXDtWA8tC4Qs4mDWyx31kB7jjkPHRtgeiQ== X-Received: by 2002:a17:90a:d707:b0:2ae:78cd:59fe with SMTP id 98e67ed59e1d1-2c1dc5c867emr7789964a91.31.1717417012437; Mon, 03 Jun 2024 05:16:52 -0700 (PDT) Received: from localhost.localdomain (69-172-146-21.cable.teksavvy.com. [69.172.146.21]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c1a776f526sm8340820a91.13.2024.06.03.05.16.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 05:16:52 -0700 (PDT) From: Tony Ambardar X-Google-Original-From: Tony Ambardar To: bpf@vger.kernel.org Cc: Tony Ambardar , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Miguel Ojeda , stable@vger.kernel.org Subject: [PATCH bpf v1 1/2] Compiler Attributes: Add __retain macro Date: Mon, 3 Jun 2024 05:16:43 -0700 Message-Id: <5416a38a194bb97930f5a2e672165e573b82857b.1717413886.git.Tony.Ambardar@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Some code includes the __used macro to prevent functions and data from being optimized out. This macro implements __attribute__((__used__)), which operates at the compiler and IR-level, and so still allows a linker to remove objects intended to be kept. Compilers supporting __attribute__((__retain__)) can address this gap by setting the flag SHF_GNU_RETAIN on the section of a function/variable, indicating to the linker the object should be retained. This attribute is available since gcc 11, clang 13, and binutils 2.36. Provide a __retain macro implementing __attribute__((__retain__)), whose first user will be the '__bpf_kfunc' tag. Link: https://lore.kernel.org/bpf/ZlmGoT9KiYLZd91S@krava/T/ Cc: stable@vger.kernel.org # v6.6+ Signed-off-by: Tony Ambardar --- include/linux/compiler_attributes.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/linux/compiler_attributes.h b/include/linux/compiler_attributes.h index 32284cd26d52..1c22e1a734dc 100644 --- a/include/linux/compiler_attributes.h +++ b/include/linux/compiler_attributes.h @@ -326,6 +326,20 @@ */ #define __pure __attribute__((__pure__)) +/* + * Optional: only supported since gcc >= 11, clang >= 13 + * + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-retain-function-attribute + * clang: https://clang.llvm.org/docs/AttributeReference.html#retain + */ +#if __has_attribute(__retain__) && \ + (defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || \ + defined(CONFIG_LTO_CLANG)) +# define __retain __attribute__((__retain__)) +#else +# define __retain +#endif + /* * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-section-function-attribute * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-section-variable-attribute From patchwork Mon Jun 3 12:16:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Ambardar X-Patchwork-Id: 13683732 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 9A619129E8E; Mon, 3 Jun 2024 12:16:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717417016; cv=none; b=h7QVtPP9DbPlRmGFsW7UWceqq1qvGc1auDaTMQYB0y6mi+H0SDTlCtAh/VqYNCAl7k8JLKXAfRvT/J/wodZ2O2WkryoPN7Sn4WVzkaiI9AgorH+G3nhB0fm6YbWQqqLWquBXxrubMfPN1Pp+elb8zvhUkLXXOIY+h3aPd5CUIVk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717417016; c=relaxed/simple; bh=gAwp8yYUnceVBn5scRDmLBn983/3nY5FUXQpnAfxA4I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jaDUCjZGsfwatOBpQOmLAPsDFch1e4DWbkZWo1OYHy1fFymV95OGPTarXXPq2SBsxHdk1jwm8LLfc93ZZG9fg1/aqragURlFAxcgvEQqvO4Tqb7qBC02warCr++IU+osK2p6P8DofbZ+mgzWd3fGpPrXIc+FamfN16DwsbUb8kA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Ezy40xEY; arc=none smtp.client-ip=209.85.214.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Ezy40xEY" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-1f480624d0fso31811415ad.1; Mon, 03 Jun 2024 05:16:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717417014; x=1718021814; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dV9DumgWigNz8izFoAXbyFQWDVcRbsgIhRa9w5EE8so=; b=Ezy40xEYPaFhezMeejSvFt2kZ+MgZYhbDIyuKQV+vZsJIYR2Vt+qlVTSjOi92h28gS 6HH2bYiqEAH75hlGgr/VkDnKj59r2iTmbW6h/3rIZ8OczCwuMP/BciHeTDXIwu1YGYUU 3ShiXRoTVB9WmtClWh1ejp+ISa4Cqc4JtsEMT9PmdNkag8u5HUSpVRl1EBH7q9Zzuthd MmFVlw5S55Q/TZAoEFk0WMUCUuALbT0Kx81LIHNtgfAhOlmOdawgW8PokhKz4uVX/K5L b2YW6/Yft/Gw6256jpwEJckiM+iRHbuqtvLNy0JLatvFf7tPSNAI7vRJErXcQ4K1329W u/qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717417014; x=1718021814; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dV9DumgWigNz8izFoAXbyFQWDVcRbsgIhRa9w5EE8so=; b=kh4Rul2Rx+2JK/bHppAL4AjuGJ7V0cpQXGwbvBOf5uZgPZ279nz4/nOd0y42Mi76Zn Ht9bxHm8retdGu4lpebSWiKkdSHxz0moatwcHqc7AjdV3ULiee7zXclBuwPptJp7JjI+ JU5nahtcOCIONrO/o6SeqvcgcclYxen6NXze7EHv6q/1jsd5PkaD2Tj7rClhYh6QfihE sH/ONPQMxp/GrqBR89QlBg72vFz/n23/jGvtxngI/7cd/C2i1qiz+SZmu/LZVvYVE6Zj 45wabiVagrfcgQo6buR5NNchqV7Bbeq0+HyHR6PdR9GYiAUi8Fqyyi4c3UIRft+AY7E4 qauA== X-Forwarded-Encrypted: i=1; AJvYcCWL1PJxPjF8FJbeh9821nplamOHt7ZY7qNhmg4v/G0FNAVSqbY3K/d7ozPmSLO0VqTjp1muD3307hM0NQ6TmLXumjsd2hJ+ X-Gm-Message-State: AOJu0YwUZXcUGIv/4bwUaNZKPViLWa+UR+Qljs1ADwLTz06LLN10tcUQ lNGaq5P5Ihnu3jWFRAvyPH59YywWtneOFFmrjiYFbI2pjm9Mx/1SFDHIRnOj X-Google-Smtp-Source: AGHT+IGdxb+vI/H6XZG0Ofw/pjF/mGH+P4AD2GKh7+hySgsW8eiq5miB/tAJIU7b2PA+6FyFrFVPxw== X-Received: by 2002:a17:90a:e00a:b0:2bd:fa57:b35c with SMTP id 98e67ed59e1d1-2c1dc590a25mr7119648a91.28.1717417013620; Mon, 03 Jun 2024 05:16:53 -0700 (PDT) Received: from localhost.localdomain (69-172-146-21.cable.teksavvy.com. [69.172.146.21]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c1a776f526sm8340820a91.13.2024.06.03.05.16.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 05:16:53 -0700 (PDT) From: Tony Ambardar X-Google-Original-From: Tony Ambardar To: bpf@vger.kernel.org Cc: Tony Ambardar , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Miguel Ojeda , kernel test robot , stable@vger.kernel.org Subject: [PATCH bpf v1 2/2] bpf: Harden __bpf_kfunc tag against linker kfunc removal Date: Mon, 3 Jun 2024 05:16:44 -0700 Message-Id: <5ed38a5ed9e30232682ac9a57a9ca4bc0a1b9bd3.1717413886.git.Tony.Ambardar@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net BPF kfuncs are often not directly referenced and may be inadvertently removed by optimization steps during kernel builds, thus the __bpf_kfunc tag mitigates against this removal by including the __used macro. However, this macro alone does not prevent removal during linking, and may still yield build warnings (e.g. on mips64el): LD vmlinux BTFIDS vmlinux WARN: resolve_btfids: unresolved symbol bpf_verify_pkcs7_signature WARN: resolve_btfids: unresolved symbol bpf_lookup_user_key WARN: resolve_btfids: unresolved symbol bpf_lookup_system_key WARN: resolve_btfids: unresolved symbol bpf_key_put WARN: resolve_btfids: unresolved symbol bpf_iter_task_next WARN: resolve_btfids: unresolved symbol bpf_iter_css_task_new WARN: resolve_btfids: unresolved symbol bpf_get_file_xattr WARN: resolve_btfids: unresolved symbol bpf_ct_insert_entry WARN: resolve_btfids: unresolved symbol bpf_cgroup_release WARN: resolve_btfids: unresolved symbol bpf_cgroup_from_id WARN: resolve_btfids: unresolved symbol bpf_cgroup_acquire WARN: resolve_btfids: unresolved symbol bpf_arena_free_pages NM System.map SORTTAB vmlinux OBJCOPY vmlinux.32 Update the __bpf_kfunc tag to better guard against linker optimization by including the new __retain compiler macro, which fixes the warnings above. Verify the __retain macro with readelf by checking object flags for 'R': $ readelf -Wa kernel/trace/bpf_trace.o Section Headers: [Nr] Name Type Address Off Size ES Flg Lk Inf Al ... [178] .text.bpf_key_put PROGBITS 00000000 6420 0050 00 AXR 0 0 8 ... Key to Flags: ... R (retain), D (mbind), p (processor specific) Link: https://lore.kernel.org/bpf/ZlmGoT9KiYLZd91S@krava/T/ Reported-by: kernel test robot Closes: https://lore.kernel.org/r/202401211357.OCX9yllM-lkp@intel.com/ Fixes: 57e7c169cd6a ("bpf: Add __bpf_kfunc tag for marking kernel functions as kfuncs") Cc: stable@vger.kernel.org # v6.6+ Signed-off-by: Tony Ambardar --- include/linux/btf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index f9e56fd12a9f..7c3e40c3295e 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -82,7 +82,7 @@ * as to avoid issues such as the compiler inlining or eliding either a static * kfunc, or a global kfunc in an LTO build. */ -#define __bpf_kfunc __used noinline +#define __bpf_kfunc __used __retain noinline #define __bpf_kfunc_start_defs() \ __diag_push(); \