From nobody Sat Feb 7 14:52:44 2026 Received: from out-182.mta1.migadu.com (out-182.mta1.migadu.com [95.215.58.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F79E800; Wed, 7 Jan 2026 02:25:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767752736; cv=none; b=D2p34Oi2fnFKAzZ8KZO42kwAsAZeCByL4ZS/ZgodunySxBf31FeLlI5RyHeeBva9uujAe3To0azOymkUoZToRhiDCz4gccJPdrqn4N0nie4chT0/634pm+u5qYSlKbd0frykaTtzoWJFLA24RIHZn3zq9JCdymXs4DbrNsd4Sto= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767752736; c=relaxed/simple; bh=GAyVhW+cvl1skCLq6NLPcKLf1t2m+eDQlW8BAzeUbN8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=h3mjol7OIhmG+ebjxrbyDsTexJgaZ+R2t0ATil2p+DJtEIvOTpaOJJfzsBPVhjihkmjQdHWp6ZOuRMjQS91ABNETZgjmXBVTMmwZw8FfLZWbqHdRS/nljecFqOIwQzIwtheRNqGseHgBgC96XdPIxb5cjTHgQDzkxck3+P+XnlY= 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=BxfPs68w; arc=none smtp.client-ip=95.215.58.182 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="BxfPs68w" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767752732; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iDVlRE+HtxaRduhaunWaUPDQBHmD4ghTtCNLMBnulhs=; b=BxfPs68wL9ShqIo7QYZutg+tMoexL2CNzyq1O4Uey/b4RYyPRkV2Yaf2tenDlp1957/rkq 3/vXZzPBrX9sOD6fzylIE2YIu11JmKLnu9odyDOCg+naLTH4247fBrGlteu3LcUaV5soW6 vIxuL2sgelC7/nGq48GThBigpgNkToU= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Jiri Olsa , Yonghong Song , Song Liu , Eduard Zingerman , Martin KaFai Lau , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Shuah Khan , Jason Xing , Willem de Bruijn , Mykyta Yatsenko , Tao Chen , Kumar Kartikeya Dwivedi , Anton Protopopov , Amery Hung , Tobias Klauser , Rong Tao , dxu@dxuuu.xyz, deso@posteo.net, Leon Hwang , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [PATCH bpf-next v13 1/7] bpf: Introduce BPF_F_CPU and BPF_F_ALL_CPUS flags Date: Wed, 7 Jan 2026 10:20:16 +0800 Message-ID: <20260107022022.12843-2-leon.hwang@linux.dev> In-Reply-To: <20260107022022.12843-1-leon.hwang@linux.dev> References: <20260107022022.12843-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: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Introduce BPF_F_CPU and BPF_F_ALL_CPUS flags and check them for following APIs: * 'map_lookup_elem()' * 'map_update_elem()' * 'generic_map_lookup_batch()' * 'generic_map_update_batch()' And, get the correct value size for these APIs. Acked-by: Andrii Nakryiko Signed-off-by: Leon Hwang --- include/linux/bpf.h | 23 ++++++++++++++++++++++- include/uapi/linux/bpf.h | 2 ++ kernel/bpf/syscall.c | 31 +++++++++++++++++-------------- tools/include/uapi/linux/bpf.h | 2 ++ 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index a63e47d2109c..108bab1bda9d 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -3915,14 +3915,35 @@ bpf_prog_update_insn_ptrs(struct bpf_prog *prog, u3= 2 *offsets, void *image) } #endif =20 +static inline bool bpf_map_supports_cpu_flags(enum bpf_map_type map_type) +{ + return false; +} + static inline int bpf_map_check_op_flags(struct bpf_map *map, u64 flags, u= 64 allowed_flags) { - if (flags & ~allowed_flags) + u32 cpu; + + if ((u32)flags & ~allowed_flags) return -EINVAL; =20 if ((flags & BPF_F_LOCK) && !btf_record_has_field(map->record, BPF_SPIN_L= OCK)) return -EINVAL; =20 + if (!(flags & BPF_F_CPU) && flags >> 32) + return -EINVAL; + + if (flags & (BPF_F_CPU | BPF_F_ALL_CPUS)) { + if (!bpf_map_supports_cpu_flags(map->map_type)) + return -EINVAL; + if ((flags & BPF_F_CPU) && (flags & BPF_F_ALL_CPUS)) + return -EINVAL; + + cpu =3D flags >> 32; + if ((flags & BPF_F_CPU) && cpu >=3D num_possible_cpus()) + return -ERANGE; + } + return 0; } =20 diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 84ced3ed2d21..2a2ade4be60f 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1384,6 +1384,8 @@ enum { BPF_NOEXIST =3D 1, /* create new element if it didn't exist */ BPF_EXIST =3D 2, /* update existing element */ BPF_F_LOCK =3D 4, /* spin_lock-ed map_lookup/map_update */ + BPF_F_CPU =3D 8, /* cpu flag for percpu maps, upper 32-bit of flags is a = cpu number */ + BPF_F_ALL_CPUS =3D 16, /* update value across all CPUs for percpu maps */ }; =20 /* flags for BPF_MAP_CREATE command */ diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 6dd2ad2f9e81..e8cfe9d67e64 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -133,12 +133,14 @@ bool bpf_map_write_active(const struct bpf_map *map) return atomic64_read(&map->writecnt) !=3D 0; } =20 -static u32 bpf_map_value_size(const struct bpf_map *map) -{ - if (map->map_type =3D=3D BPF_MAP_TYPE_PERCPU_HASH || - map->map_type =3D=3D BPF_MAP_TYPE_LRU_PERCPU_HASH || - map->map_type =3D=3D BPF_MAP_TYPE_PERCPU_ARRAY || - map->map_type =3D=3D BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) +static u32 bpf_map_value_size(const struct bpf_map *map, u64 flags) +{ + if (flags & (BPF_F_CPU | BPF_F_ALL_CPUS)) + return map->value_size; + else if (map->map_type =3D=3D BPF_MAP_TYPE_PERCPU_HASH || + map->map_type =3D=3D BPF_MAP_TYPE_LRU_PERCPU_HASH || + map->map_type =3D=3D BPF_MAP_TYPE_PERCPU_ARRAY || + map->map_type =3D=3D BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) return round_up(map->value_size, 8) * num_possible_cpus(); else if (IS_FD_MAP(map)) return sizeof(u32); @@ -1729,7 +1731,7 @@ static int map_lookup_elem(union bpf_attr *attr) if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) return -EPERM; =20 - err =3D bpf_map_check_op_flags(map, attr->flags, BPF_F_LOCK); + err =3D bpf_map_check_op_flags(map, attr->flags, BPF_F_LOCK | BPF_F_CPU); if (err) return err; =20 @@ -1737,7 +1739,7 @@ static int map_lookup_elem(union bpf_attr *attr) if (IS_ERR(key)) return PTR_ERR(key); =20 - value_size =3D bpf_map_value_size(map); + value_size =3D bpf_map_value_size(map, attr->flags); =20 err =3D -ENOMEM; value =3D kvmalloc(value_size, GFP_USER | __GFP_NOWARN); @@ -1804,7 +1806,7 @@ static int map_update_elem(union bpf_attr *attr, bpfp= tr_t uattr) goto err_put; } =20 - value_size =3D bpf_map_value_size(map); + value_size =3D bpf_map_value_size(map, attr->flags); value =3D kvmemdup_bpfptr(uvalue, value_size); if (IS_ERR(value)) { err =3D PTR_ERR(value); @@ -2000,11 +2002,12 @@ int generic_map_update_batch(struct bpf_map *map, s= truct file *map_file, void *key, *value; int err =3D 0; =20 - err =3D bpf_map_check_op_flags(map, attr->batch.elem_flags, BPF_F_LOCK); + err =3D bpf_map_check_op_flags(map, attr->batch.elem_flags, + BPF_F_LOCK | BPF_F_CPU | BPF_F_ALL_CPUS); if (err) return err; =20 - value_size =3D bpf_map_value_size(map); + value_size =3D bpf_map_value_size(map, attr->batch.elem_flags); =20 max_count =3D attr->batch.count; if (!max_count) @@ -2059,11 +2062,11 @@ int generic_map_lookup_batch(struct bpf_map *map, u32 value_size, cp, max_count; int err; =20 - err =3D bpf_map_check_op_flags(map, attr->batch.elem_flags, BPF_F_LOCK); + err =3D bpf_map_check_op_flags(map, attr->batch.elem_flags, BPF_F_LOCK | = BPF_F_CPU); if (err) return err; =20 - value_size =3D bpf_map_value_size(map); + value_size =3D bpf_map_value_size(map, attr->batch.elem_flags); =20 max_count =3D attr->batch.count; if (!max_count) @@ -2185,7 +2188,7 @@ static int map_lookup_and_delete_elem(union bpf_attr = *attr) goto err_put; } =20 - value_size =3D bpf_map_value_size(map); + value_size =3D bpf_map_value_size(map, 0); =20 err =3D -ENOMEM; value =3D kvmalloc(value_size, GFP_USER | __GFP_NOWARN); diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 6b92b0847ec2..b816bc53d2e1 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1384,6 +1384,8 @@ enum { BPF_NOEXIST =3D 1, /* create new element if it didn't exist */ BPF_EXIST =3D 2, /* update existing element */ BPF_F_LOCK =3D 4, /* spin_lock-ed map_lookup/map_update */ + BPF_F_CPU =3D 8, /* cpu flag for percpu maps, upper 32-bit of flags is a = cpu number */ + BPF_F_ALL_CPUS =3D 16, /* update value across all CPUs for percpu maps */ }; =20 /* flags for BPF_MAP_CREATE command */ --=20 2.52.0