From patchwork Sat Mar 2 16:50:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 13579567 X-Patchwork-Delegate: bpf@iogearbox.net Received: from 66-220-155-178.mail-mxout.facebook.com (66-220-155-178.mail-mxout.facebook.com [66.220.155.178]) (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 1BB0520DC5 for ; Sat, 2 Mar 2024 16:50:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=66.220.155.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709398241; cv=none; b=i3/TIesE10gbvhTS+JyWUvVUvDRZVfP0yNxD9VQVrLzuEZ5BINaXL13pO5ooDb/HA5DUJ9Q5w3TY5pf9HRjF+Q5XJzXPk7VbwFj6waFPMp/QmkyDNbNefF1c5nfe9fmRZwF6cwbtHAoyk+GZ2cy6LoN0ZVcnQQB08f/+BNA2nkc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709398241; c=relaxed/simple; bh=r8v/NSiCBB8CdbYb+ClQEZtGaq+Dewk9RsQ7YoaddyQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BigXUR6nJm3sX4IvcK2jq+MTUv7AsL1pwLCijLNYjHHyqaWH7+fDba3tEOaYH89Bg2ylV6ig0xkU7RlQzJ4PybhBnk2fj9t7seJFxkVA3gy7s4xyMoeLr2gzsySaO9yY71w39EHtJ/D52LUjh8S0nVncVjmmwbEseJ5W8YmXEQg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.dev; spf=fail smtp.mailfrom=linux.dev; arc=none smtp.client-ip=66.220.155.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=linux.dev Received: by devbig309.ftw3.facebook.com (Postfix, from userid 128203) id 1BBC411F11FD; Sat, 2 Mar 2024 08:50:27 -0800 (PST) From: Yonghong Song To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , kernel-team@fb.com, Martin KaFai Lau Subject: [PATCH bpf-next 2/4] selftests/bpf: Add check_lto_kernel() helper Date: Sat, 2 Mar 2024 08:50:27 -0800 Message-ID: <20240302165027.1628051-1-yonghong.song@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240302165017.1627295-1-yonghong.song@linux.dev> References: <20240302165017.1627295-1-yonghong.song@linux.dev> 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 Add check_lto_kernel() helper to detect whether the underlying kernel enabled CONFIG_LTO or not. The function check_lto_kernel() can be used by selftests to handle some lto-specific situations. The code is heavily borrowed from libbpf function bpf_object__read_kconfig_file(). Signed-off-by: Yonghong Song --- tools/testing/selftests/bpf/testing_helpers.c | 47 +++++++++++++++++++ tools/testing/selftests/bpf/testing_helpers.h | 1 + 2 files changed, 48 insertions(+) diff --git a/tools/testing/selftests/bpf/testing_helpers.c b/tools/testing/selftests/bpf/testing_helpers.c index 28b6646662af..3f74f73843cf 100644 --- a/tools/testing/selftests/bpf/testing_helpers.c +++ b/tools/testing/selftests/bpf/testing_helpers.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include #include "test_progs.h" @@ -475,3 +477,48 @@ bool is_jit_enabled(void) return enabled; } + +int check_lto_kernel(void) +{ + static int check_lto = 2; + char buf[PATH_MAX]; + struct utsname uts; + gzFile file; + int len; + + if (check_lto != 2) + return check_lto; + + uname(&uts); + len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release); + if (len < 0) { + check_lto = -EINVAL; + goto out; + } else if (len >= PATH_MAX) { + check_lto = -ENAMETOOLONG; + goto out; + } + + /* gzopen also accepts uncompressed files. */ + file = gzopen(buf, "re"); + if (!file) + file = gzopen("/proc/config.gz", "re"); + + if (!file) { + check_lto = -ENOENT; + goto out; + } + + check_lto = 0; + while (gzgets(file, buf, sizeof(buf))) { + /* buf also contains '\n', skip it during comparison. */ + if (!strncmp(buf, "CONFIG_LTO=y", 12)) { + check_lto = 1; + break; + } + } + + gzclose(file); +out: + return check_lto; +} diff --git a/tools/testing/selftests/bpf/testing_helpers.h b/tools/testing/selftests/bpf/testing_helpers.h index d55f6ab12433..57683b3a1280 100644 --- a/tools/testing/selftests/bpf/testing_helpers.h +++ b/tools/testing/selftests/bpf/testing_helpers.h @@ -55,5 +55,6 @@ struct bpf_insn; int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt); int testing_prog_flags(void); bool is_jit_enabled(void); +int check_lto_kernel(void); #endif /* __TESTING_HELPERS_H */