From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mta1.migadu.com (out-224.mta1.migadu.com [95.215.58.224]) (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 C938C485CE3 for ; Thu, 13 Aug 2026 15:24:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.224 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786634649; cv=none; b=TbjfuMKWQsUkQZAACpsas7Y1ibZCtppFZfNwrsgkPsQjwlU0lPAeMeJtLAIRyhDqNMqKj8PiM7JwQlJQ4cNM828nmz1moKLSYFxLqX6qFRv72j39MEl6RXRSbrezivflApdaMlaLTYvr7bSbSuy4zD26etOkwRPwIsEK7SnHCU4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786634649; c=relaxed/simple; bh=5ZCt5mLnoYxldIysQPGgQKI9kJMnrVUrpohc+4vpe68=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ivzHEjKuBbNPnUajyBbEWHs3Fyxb6dGWLkrAOFVusqVOxWLL4T6jxkmkPbAZSFWVhcrIk8iyBY7gCRZ85Ez5PNebPBWxfRZpn1PBtYd1aUAnMWfUgTvJVLpSxorhp0MkjPA/tSpYHZXusqh2C9xmIcom1EexugB70F6HUWHa8ro= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=fduSR/cK; arc=none smtp.client-ip=95.215.58.224 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="fduSR/cK" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=5ZCt5mLnoYxldIysQPGgQKI9kJMnrVUrpohc+4vpe68=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1786634645; v=1; x=1787239445; b=fduSR/cKP6G5O8DqkFUx0vA2iFdIyR8khH1x88w5S9/1xLXUFryEH+OjLzesnEfbi1XpucjG YTbBaH6AzhrSeRxXuCRCs2yedElLB4SuK1zQ39N8JPM6DgfAjdhNY+7cLUsZlRzQufv+/9J4whl IpNEDjy0qpQ02RR46DyJ+nSM= X-Envelope-To: linux-kernel@vger.kernel.org Received: from .linux.dev (117.20.149.103) by smtp.migadu.com with ESMTPS id 6c86e6d09adda9ec; Thu, 13 Aug 2026 15:24:05 +0000 X-Migadu-Flow: FLOW_OUT From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Kumar Kartikeya Dwivedi , Song Liu , Yonghong Song , Jiri Olsa , John Fastabend , Quentin Monnet , Shuah Khan , Leon Hwang , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com, Emil Tsalapatis Subject: [PATCH bpf-next v12 04/10] libbpf: Probe percpu data feature Date: Thu, 13 Aug 2026 23:23:17 +0800 Message-ID: <20260813152324.97937-5-leon.hwang@linux.dev> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260813152324.97937-1-leon.hwang@linux.dev> References: <20260813152324.97937-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit libbpf needs a reliable way to distinguish kernels that can support global percpu data from those that cannot. Add a dedicated feature probe, so libbpf can make capability decisions early and fail predictably when global percpu data is unavailable. Acked-by: Andrii Nakryiko Reviewed-by: Emil Tsalapatis Signed-off-by: Leon Hwang --- tools/lib/bpf/features.c | 35 +++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf_internal.h | 2 ++ 2 files changed, 37 insertions(+) diff --git a/tools/lib/bpf/features.c b/tools/lib/bpf/features.c index b7e388f99d0b..ef9581c11303 100644 --- a/tools/lib/bpf/features.c +++ b/tools/lib/bpf/features.c @@ -620,6 +620,38 @@ static int probe_bpf_syscall_common_attrs(int token_fd) return probe_sys_bpf_ext(); } +static int probe_kern_percpu_data(int token_fd) +{ + struct bpf_insn insns[] = { + BPF_LD_MAP_VALUE(BPF_REG_1, 0, 0), + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 0), + BPF_EXIT_INSN(), + }; + LIBBPF_OPTS(bpf_map_create_opts, map_opts, + .token_fd = token_fd, + .map_flags = token_fd ? BPF_F_TOKEN_FD : 0, + ); + LIBBPF_OPTS(bpf_prog_load_opts, prog_opts, + .token_fd = token_fd, + .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0, + ); + int ret, map, insn_cnt = ARRAY_SIZE(insns); + + map = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, "libbpf_percpu", sizeof(int), 8, 1, + &map_opts); + if (map < 0) { + pr_warn("Error in %s(): %s. Couldn't create simple percpu_array map.\n", + __func__, errstr(map)); + return map; + } + + insns[0].imm = map; + + ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, &prog_opts); + close(map); + return probe_fd(ret); +} + typedef int (*feature_probe_fn)(int /* token_fd */); static struct kern_feature_cache feature_cache; @@ -707,6 +739,9 @@ static struct kern_feature_desc { [FEAT_BPF_SYSCALL_COMMON_ATTRS] = { "BPF syscall common attributes support", probe_bpf_syscall_common_attrs, }, + [FEAT_PERCPU_DATA] = { + "kernel supports percpu data", probe_kern_percpu_data, + }, }; bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_id) diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index 7a74abb904f8..4c46d34fc055 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -401,6 +401,8 @@ enum kern_feature_id { FEAT_BTF_LAYOUT, /* Kernel supports BPF syscall common attributes */ FEAT_BPF_SYSCALL_COMMON_ATTRS, + /* Kernel supports percpu data */ + FEAT_PERCPU_DATA, __FEAT_CNT, }; -- 2.55.0