From nobody Tue Dec 2 02:58:58 2025 Received: from out-189.mta1.migadu.com (out-189.mta1.migadu.com [95.215.58.189]) (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 9A1FD29D29D for ; Mon, 17 Nov 2025 16:21:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763396480; cv=none; b=ZB8YX0B5+Cn9qJQJCctkcneXFneg3dZeF1Yr2dF38j9hdhXhNp4S7KXmUW0ePbiIqwXzanW2bDOSzFOp/qMD+9T5wn5bNwjUZ3BnmLtoCTPJP+S6dbXndbQ1YS0Fo0aedgUBVjvGlcfyXcWZh77GkaxCmPrFY3YdPmY6TrBji/k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763396480; c=relaxed/simple; bh=jgdcJUwfJQR4nC/n5Z9NX59W/QjTvizZH3HW71G0Wj4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dYop8XM2ie0tFuvB+92PEiTx9YyJImX5dsmcv5VzuVckimrzEXZZ+bnXt3X4Q+jJIkyQ8HgNWdRLjU4b8u6UHtEjDQPAKYsbd4r1xz4r7IQzHQu95QVYqsYc8vfiac9pNMDeCkwzY7/IVd/2Zi49WkFm5Ud9W3HJS2T7jwVt/aY= 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=V11TG+jR; arc=none smtp.client-ip=95.215.58.189 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="V11TG+jR" 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=1763396475; 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=Emq6bcW/MJSXF1L3/1EP96y3M+k12AFx24671ihRuA0=; b=V11TG+jRwCSA8CuHYsfUpLScD/Lsz8PX8XlgWysLNKgMp6UwA1q3sXI52I9JrePAGogv7p BycobZDfGleFGTGDMw/tLH0aSteUOZxXk+uYE8LQDux0nicsGspSsAQGtsbog2wEQwuiVi ohlGiJM/D5w70sbqq3GHYdmizaiouYU= From: Leon Hwang To: bpf@vger.kernel.org Cc: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net, jolsa@kernel.org, yonghong.song@linux.dev, song@kernel.org, eddyz87@gmail.com, dxu@dxuuu.xyz, deso@posteo.net, martin.lau@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, shuah@kernel.org, kerneljasonxing@gmail.com, chen.dylane@linux.dev, willemb@google.com, paul.chaignon@gmail.com, a.s.protopopov@gmail.com, memxor@gmail.com, yatsenko@meta.com, tklauser@distanz.ch, leon.hwang@linux.dev, kernel-patches-bot@fb.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v10 2/8] bpf: Introduce BPF_F_CPU and BPF_F_ALL_CPUS flags Date: Tue, 18 Nov 2025 00:20:27 +0800 Message-ID: <20251117162033.6296-3-leon.hwang@linux.dev> In-Reply-To: <20251117162033.6296-1-leon.hwang@linux.dev> References: <20251117162033.6296-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 a900bc022b1cc..65737870f2ea6 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -3813,14 +3813,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 f5713f59ac10a..8b6279ca6e662 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1373,6 +1373,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 f3dc6e2c82411..6edaab858b326 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); @@ -1732,7 +1734,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 @@ -1740,7 +1742,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); @@ -1807,7 +1809,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); @@ -2003,11 +2005,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) @@ -2062,11 +2065,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) @@ -2188,7 +2191,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 f5713f59ac10a..8b6279ca6e662 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1373,6 +1373,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.51.2