From nobody Mon Dec 1 23:11:24 2025 Received: from out-170.mta0.migadu.com (out-170.mta0.migadu.com [91.218.175.170]) (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 12C88305068 for ; Wed, 26 Nov 2025 14:52:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764168753; cv=none; b=pi9OMJEFG2p6Fd2jkFRPRyTl3JR3rrnBb4prLAs7xZu55sAZCiEkJqynOjYpDGkQUmL4LmIk/8nultP+VP/h+PULu+7/VrXEWcxoC5QCoEp8+zBW/pK7T+qRWGNGiBYPhCpyY+HYSLsnCH3serIcM+GcBSYlsR2T9poZtbiO1Zo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764168753; c=relaxed/simple; bh=tv6u8+8HiGWGT3wL1c8GXPJ+6n3YuwfoeZWo5+YnnG0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Aih8EupN4KUyULK1dPcuLSIFGjffn5uUIy1T7thYrW6QMx1Y1qOr1r+Uya+hoidifqPx07f6SYkRa1blrKZfQOZHLKWyOdCESDM6BCMMWgx99n5bxSyF9gvANWwCvxrEf14HCV7fgMSDXEGDjygdH0sJIb3bz0UdyJFXEqfE02Y= 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=I37A+rCL; arc=none smtp.client-ip=91.218.175.170 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="I37A+rCL" 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=1764168750; 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=eyVpsABIiydOnG1kb3o6PrVSMcQesGuFwWpVoehPpLc=; b=I37A+rCLQZBOVUKmlVEmGv71sQCyXJLc0fY2bjGzA0PKa312CVorS0uOTFjDtdWeMVtJi8 AhzszCYoAdm8X1/qh7cr5xpzTJYFk3xdvCm3n0NRQ1jTGmXUjOZeeQIsDUSLXIYVAxy8rC GQNTzoPp18j9HO9Uv7fLglVwtEIKlO0= 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 v12 6/7] libbpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu maps Date: Wed, 26 Nov 2025 22:50:38 +0800 Message-ID: <20251126145039.15715-7-leon.hwang@linux.dev> In-Reply-To: <20251126145039.15715-1-leon.hwang@linux.dev> References: <20251126145039.15715-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" Add libbpf support for the BPF_F_CPU flag for percpu maps by embedding the cpu info into the high 32 bits of: 1. **flags**: bpf_map_lookup_elem_flags(), bpf_map__lookup_elem(), bpf_map_update_elem() and bpf_map__update_elem() 2. **opts->elem_flags**: bpf_map_lookup_batch() and bpf_map_update_batch() And the flag can be BPF_F_ALL_CPUS, but cannot be 'BPF_F_CPU | BPF_F_ALL_CPUS'. Behavior: * If the flag is BPF_F_ALL_CPUS, the update is applied across all CPUs. * If the flag is BPF_F_CPU, it updates value only to the specified CPU. * If the flag is BPF_F_CPU, lookup value only from the specified CPU. * lookup does not support BPF_F_ALL_CPUS. Acked-by: Andrii Nakryiko Signed-off-by: Leon Hwang --- tools/lib/bpf/bpf.h | 8 ++++++++ tools/lib/bpf/libbpf.c | 26 ++++++++++++++++++++------ tools/lib/bpf/libbpf.h | 21 ++++++++------------- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index e983a3e40d61..ffd93feffd71 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -289,6 +289,14 @@ LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd,= void *in_batch, * Update spin_lock-ed map elements. This must be * specified if the map value contains a spinlock. * + * **BPF_F_CPU** + * As for percpu maps, update value on the specified CPU. And the cpu + * info is embedded into the high 32 bits of **opts->elem_flags**. + * + * **BPF_F_ALL_CPUS** + * As for percpu maps, update value across all CPUs. This flag cannot + * be used with BPF_F_CPU at the same time. + * * @param fd BPF map file descriptor * @param keys pointer to an array of *count* keys * @param values pointer to an array of *count* values diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 706e7481bdf6..65b9b5e95544 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10913,7 +10913,7 @@ bpf_object__find_map_fd_by_name(const struct bpf_ob= ject *obj, const char *name) } =20 static int validate_map_op(const struct bpf_map *map, size_t key_sz, - size_t value_sz, bool check_value_sz) + size_t value_sz, bool check_value_sz, __u64 flags) { if (!map_is_created(map)) /* map is not yet created */ return -ENOENT; @@ -10940,6 +10940,20 @@ static int validate_map_op(const struct bpf_map *m= ap, size_t key_sz, int num_cpu =3D libbpf_num_possible_cpus(); size_t elem_sz =3D roundup(map->def.value_size, 8); =20 + if (flags & (BPF_F_CPU | BPF_F_ALL_CPUS)) { + if ((flags & BPF_F_CPU) && (flags & BPF_F_ALL_CPUS)) { + pr_warn("map '%s': BPF_F_CPU and BPF_F_ALL_CPUS are mutually exclusive= \n", + map->name); + return -EINVAL; + } + if (map->def.value_size !=3D value_sz) { + pr_warn("map '%s': unexpected value size %zu provided for either BPF_F= _CPU or BPF_F_ALL_CPUS, expected %u\n", + map->name, value_sz, map->def.value_size); + return -EINVAL; + } + break; + } + if (value_sz !=3D num_cpu * elem_sz) { pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, = expected %d * %zu =3D %zd\n", map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz); @@ -10964,7 +10978,7 @@ int bpf_map__lookup_elem(const struct bpf_map *map, { int err; =20 - err =3D validate_map_op(map, key_sz, value_sz, true); + err =3D validate_map_op(map, key_sz, value_sz, true, flags); if (err) return libbpf_err(err); =20 @@ -10977,7 +10991,7 @@ int bpf_map__update_elem(const struct bpf_map *map, { int err; =20 - err =3D validate_map_op(map, key_sz, value_sz, true); + err =3D validate_map_op(map, key_sz, value_sz, true, flags); if (err) return libbpf_err(err); =20 @@ -10989,7 +11003,7 @@ int bpf_map__delete_elem(const struct bpf_map *map, { int err; =20 - err =3D validate_map_op(map, key_sz, 0, false /* check_value_sz */); + err =3D validate_map_op(map, key_sz, 0, false /* check_value_sz */, flags= ); if (err) return libbpf_err(err); =20 @@ -11002,7 +11016,7 @@ int bpf_map__lookup_and_delete_elem(const struct bp= f_map *map, { int err; =20 - err =3D validate_map_op(map, key_sz, value_sz, true); + err =3D validate_map_op(map, key_sz, value_sz, true, flags); if (err) return libbpf_err(err); =20 @@ -11014,7 +11028,7 @@ int bpf_map__get_next_key(const struct bpf_map *map, { int err; =20 - err =3D validate_map_op(map, key_sz, 0, false /* check_value_sz */); + err =3D validate_map_op(map, key_sz, 0, false /* check_value_sz */, 0); if (err) return libbpf_err(err); =20 diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 65e68e964b89..414e8a873380 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1200,12 +1200,13 @@ LIBBPF_API struct bpf_map *bpf_map__inner_map(struc= t bpf_map *map); * @param key_sz size in bytes of key data, needs to match BPF map definit= ion's **key_size** * @param value pointer to memory in which looked up value will be stored * @param value_sz size in byte of value data memory; it has to match BPF = map - * definition's **value_size**. For per-CPU BPF maps value size has to be - * a product of BPF map value size and number of possible CPUs in the syst= em - * (could be fetched with **libbpf_num_possible_cpus()**). Note also that = for - * per-CPU values value size has to be aligned up to closest 8 bytes for - * alignment reasons, so expected size is: `round_up(value_size, 8) - * * libbpf_num_possible_cpus()`. + * definition's **value_size**. For per-CPU BPF maps, value size can be + * `value_size` if either **BPF_F_CPU** or **BPF_F_ALL_CPUS** is specified + * in **flags**, otherwise a product of BPF map value size and number of + * possible CPUs in the system (could be fetched with + * **libbpf_num_possible_cpus()**). Note also that for per-CPU values value + * size has to be aligned up to closest 8 bytes, so expected size is: + * `round_up(value_size, 8) * libbpf_num_possible_cpus()`. * @param flags extra flags passed to kernel for this operation * @return 0, on success; negative error, otherwise * @@ -1223,13 +1224,7 @@ LIBBPF_API int bpf_map__lookup_elem(const struct bpf= _map *map, * @param key pointer to memory containing bytes of the key * @param key_sz size in bytes of key data, needs to match BPF map definit= ion's **key_size** * @param value pointer to memory containing bytes of the value - * @param value_sz size in byte of value data memory; it has to match BPF = map - * definition's **value_size**. For per-CPU BPF maps value size has to be - * a product of BPF map value size and number of possible CPUs in the syst= em - * (could be fetched with **libbpf_num_possible_cpus()**). Note also that = for - * per-CPU values value size has to be aligned up to closest 8 bytes for - * alignment reasons, so expected size is: `round_up(value_size, 8) - * * libbpf_num_possible_cpus()`. + * @param value_sz refer to **bpf_map__lookup_elem**'s description.' * @param flags extra flags passed to kernel for this operation * @return 0, on success; negative error, otherwise * --=20 2.51.2