From nobody Tue Dec 2 00:03:26 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 E01F6327790 for ; Tue, 25 Nov 2025 14:59:40 +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=1764082783; cv=none; b=FmWQcEZkgTcY/076Q/1cHJ92YPEtQsBhkoRJNTKkd+EqhYCrrIVDI6N+1EGGVqPNQTiKctNCcAYF1TTwgI6aMWG+aKiUMRnAFwrb/V1vrm6ZSn2hn5iS2xf8iCodbcvPvgdSTFOKva24PgXbv0y1sYgHY7Cd7HxJq2XT8wY0/Tw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764082783; c=relaxed/simple; bh=MipVcJ1lWqTRx4wuG4zL8qH8vMWgIc+hWwMBaYOxkh4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Gpjz2lb8Z8yMX3BETzR/di7dX2naN53LVpL49vG/+KDuKH2/xFNLL6n3YDEl1KyatJ9BIB9ZBZX7ksoU624IiMuFqcp9gdFQSMCo3fRIKkz6jmwIlyA/u/q3uh7io4kTah2zdkSiJ2V8aOEV3ET7BaO2iDp8xEkso2AtMJPn6jo= 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=NW2ZuypA; 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="NW2ZuypA" 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=1764082778; 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=LXp+7pDoGS0WWr6S4WrUXUoCMmCmkevtKo8m5xnvZCM=; b=NW2ZuypACKBmarN/1MlqQmzFJAXxSYSRMc+UiRYYzgRtwW3PYSEc9b8ybhrq8qtoIk6xRq EXzwjgkrAR8Rwr0esu2KEkO4FWXXOZ6WZjGP+tOsPu7yTjbFoK1Qe3ArG8OKnWATncJ5Ax av0rloJKgZ5Hhtd67eTIEcefJiAqMkU= 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 v11 1/8] bpf: Introduce internal bpf_map_check_op_flags helper function Date: Tue, 25 Nov 2025 22:58:50 +0800 Message-ID: <20251125145857.98134-2-leon.hwang@linux.dev> In-Reply-To: <20251125145857.98134-1-leon.hwang@linux.dev> References: <20251125145857.98134-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" It is to unify map flags checking for lookup_elem, update_elem, lookup_batch and update_batch APIs. Therefore, it will be convenient to check BPF_F_CPU and BPF_F_ALL_CPUS flags in it for these APIs in next patch. Acked-by: Andrii Nakryiko Signed-off-by: Leon Hwang --- include/linux/bpf.h | 11 +++++++++++ kernel/bpf/syscall.c | 34 +++++++++++----------------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index a9b788c7b4aa..6498be4c44f8 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -3829,4 +3829,15 @@ bpf_prog_update_insn_ptrs(struct bpf_prog *prog, u32= *offsets, void *image) } #endif =20 +static inline int bpf_map_check_op_flags(struct bpf_map *map, u64 flags, u= 64 allowed_flags) +{ + if (flags & ~allowed_flags) + return -EINVAL; + + if ((flags & BPF_F_LOCK) && !btf_record_has_field(map->record, BPF_SPIN_L= OCK)) + return -EINVAL; + + return 0; +} + #endif /* _LINUX_BPF_H */ diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 792623a7c90b..cef8963d69f9 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -1725,9 +1725,6 @@ static int map_lookup_elem(union bpf_attr *attr) if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) return -EINVAL; =20 - if (attr->flags & ~BPF_F_LOCK) - return -EINVAL; - CLASS(fd, f)(attr->map_fd); map =3D __bpf_map_get(f); if (IS_ERR(map)) @@ -1735,9 +1732,9 @@ static int map_lookup_elem(union bpf_attr *attr) if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) return -EPERM; =20 - if ((attr->flags & BPF_F_LOCK) && - !btf_record_has_field(map->record, BPF_SPIN_LOCK)) - return -EINVAL; + err =3D bpf_map_check_op_flags(map, attr->flags, BPF_F_LOCK); + if (err) + return err; =20 key =3D __bpf_copy_key(ukey, map->key_size); if (IS_ERR(key)) @@ -1800,11 +1797,9 @@ static int map_update_elem(union bpf_attr *attr, bpf= ptr_t uattr) goto err_put; } =20 - if ((attr->flags & BPF_F_LOCK) && - !btf_record_has_field(map->record, BPF_SPIN_LOCK)) { - err =3D -EINVAL; + err =3D bpf_map_check_op_flags(map, attr->flags, ~0); + if (err) goto err_put; - } =20 key =3D ___bpf_copy_key(ukey, map->key_size); if (IS_ERR(key)) { @@ -2008,13 +2003,9 @@ int generic_map_update_batch(struct bpf_map *map, st= ruct file *map_file, void *key, *value; int err =3D 0; =20 - if (attr->batch.elem_flags & ~BPF_F_LOCK) - return -EINVAL; - - if ((attr->batch.elem_flags & BPF_F_LOCK) && - !btf_record_has_field(map->record, BPF_SPIN_LOCK)) { - return -EINVAL; - } + err =3D bpf_map_check_op_flags(map, attr->batch.elem_flags, BPF_F_LOCK); + if (err) + return err; =20 value_size =3D bpf_map_value_size(map); =20 @@ -2071,12 +2062,9 @@ int generic_map_lookup_batch(struct bpf_map *map, u32 value_size, cp, max_count; int err; =20 - if (attr->batch.elem_flags & ~BPF_F_LOCK) - return -EINVAL; - - if ((attr->batch.elem_flags & BPF_F_LOCK) && - !btf_record_has_field(map->record, BPF_SPIN_LOCK)) - return -EINVAL; + err =3D bpf_map_check_op_flags(map, attr->batch.elem_flags, BPF_F_LOCK); + if (err) + return err; =20 value_size =3D bpf_map_value_size(map); =20 --=20 2.51.2 From nobody Tue Dec 2 00:03:26 2025 Received: from out-187.mta1.migadu.com (out-187.mta1.migadu.com [95.215.58.187]) (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 AA01A326D45 for ; Tue, 25 Nov 2025 14:59:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764082793; cv=none; b=fRhvsdTTkkP1I5dtChvl8b7jGClwz6BtF5Oxv4KJFUgBqjsJ+yf1UtlRYc2cErUMrYReRWyQ2fLK2Z3eCl+dwAC1qIeeN2xnCSltEZdjna6eKOLFKw4ol2Oeg9HYx9vnfl8AgteJF24H0qLtP95gixehWP0WC/g5PN5MaMDbSU8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764082793; c=relaxed/simple; bh=W/8lsMv5sUSzOJEHJ2dVfk6ti5mM4wE3LlQs/epbwHs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qpkSwAr+zdOYznhT+2c78707v/7Yl/gJt5Cu7f5jNu0zYD10B+oLSNBrsere1rP+GfqbEVVHaXxiPMrRiJtQXGvSH3/AaQ3VdvhS2dDuu8/4AmKft/GxUihRcaOHWWTnDv0aFov7J7jkSxV/7g305GJL9NQsOoSgtR9pZsGGaM0= 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=NL7+NlnC; arc=none smtp.client-ip=95.215.58.187 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="NL7+NlnC" 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=1764082789; 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=2EQcb8DXtqocNDX66I0256aqYUvL4AFhD0aUjOs0Z0w=; b=NL7+NlnC2wL4dMuk3ooBGrGG7OasJgZEciC7NFbh44zGcZNqzySGNrOJ115qxHo9y3z7sq hCLr6ksd716tss6473HpSMmwT+hSkRQxpMc5PjVnA/I1+qcjz0Fd73jK76ymyKiOumgEO3 3l2sk8YjUjp6nsXeG1o/bK16s2xjAYM= 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 v11 2/8] bpf: Introduce BPF_F_CPU and BPF_F_ALL_CPUS flags Date: Tue, 25 Nov 2025 22:58:51 +0800 Message-ID: <20251125145857.98134-3-leon.hwang@linux.dev> In-Reply-To: <20251125145857.98134-1-leon.hwang@linux.dev> References: <20251125145857.98134-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 --- v10 -> v11: - Use '(BPF_F_ALL_CPUS << 1) - 1' as allowed_flags in map_update_elem(). - Add BPF_EXIST to allowed_flags in generic_map_update_batch(). --- include/linux/bpf.h | 23 +++++++++++++++++++++- include/uapi/linux/bpf.h | 2 ++ kernel/bpf/syscall.c | 36 ++++++++++++++++++++-------------- tools/include/uapi/linux/bpf.h | 2 ++ 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 6498be4c44f8..d84af3719b59 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -3829,14 +3829,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 f5713f59ac10..8b6279ca6e66 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 cef8963d69f9..3c3e3b4095b9 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); @@ -1781,6 +1783,7 @@ static int map_update_elem(union bpf_attr *attr, bpfp= tr_t uattr) bpfptr_t uvalue =3D make_bpfptr(attr->value, uattr.is_kernel); struct bpf_map *map; void *key, *value; + u64 allowed_flags; u32 value_size; int err; =20 @@ -1797,7 +1800,8 @@ static int map_update_elem(union bpf_attr *attr, bpfp= tr_t uattr) goto err_put; } =20 - err =3D bpf_map_check_op_flags(map, attr->flags, ~0); + allowed_flags =3D (BPF_F_ALL_CPUS << 1) - 1; + err =3D bpf_map_check_op_flags(map, attr->flags, allowed_flags); if (err) goto err_put; =20 @@ -1807,7 +1811,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); @@ -2001,13 +2005,15 @@ int generic_map_update_batch(struct bpf_map *map, s= truct file *map_file, void __user *keys =3D u64_to_user_ptr(attr->batch.keys); u32 value_size, cp, max_count; void *key, *value; + u64 allowed_flags; int err =3D 0; =20 - err =3D bpf_map_check_op_flags(map, attr->batch.elem_flags, BPF_F_LOCK); + allowed_flags =3D BPF_EXIST | BPF_F_LOCK | BPF_F_CPU | BPF_F_ALL_CPUS; + err =3D bpf_map_check_op_flags(map, attr->batch.elem_flags, allowed_flags= ); 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 +2068,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 +2194,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 f5713f59ac10..8b6279ca6e66 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 From nobody Tue Dec 2 00:03:26 2025 Received: from out-173.mta1.migadu.com (out-173.mta1.migadu.com [95.215.58.173]) (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 B8E68327205 for ; Tue, 25 Nov 2025 15:00:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764082806; cv=none; b=QfUJTSUu85kUI5mOoDSQNRXvUbT26+t50lvtkF9/4VirTs7jOMtgKHzXPFBCtpOTA2ZfVgsCOjOPL0CI0JI77aellQZ7LSA7LoAgWlyEBuCnCEVDyNfzW6mSz9QlWzSeTcsGyCRfhm2dKiceLl9TvzmgIfEtYcoUvmjE9lEZQgc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764082806; c=relaxed/simple; bh=fF418YuTimb8Z3Chx26glSIBcfmS24386R8cPNTAvFc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rKIP39ho6NERPJ4M+7Hi4mwbcruTHrTgPw1wG5VqxhpF3e1Tp34M6urC791WK2NQHiCTJPUoRcqdswBBPOmGSLSIv64vOivQGGWVV0u/S+jD6jfqk3JEAObTNSqM8dTDJBYFPigmiGPAkF1Cfz/F6GPUqu5d2bJHY3FfJk2HoFU= 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=VsAjQySs; arc=none smtp.client-ip=95.215.58.173 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="VsAjQySs" 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=1764082802; 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=Wc2IYbjQGe/4SO1+MUzWBNyXK12I22pfzhFjWaOiii0=; b=VsAjQySsOWg88YQfT/wn6aF4uyZS7ux30sQgBbxKfPrNTqo1mV0mPqS2bM4Pq3iQYNI1Ux J6i/OyBGsfugrfz9Uz4wYiE2RUvRJ9pKQPDOm0iuzGvGmXMWGQD4MFOQ+wW3DGI3OHI5jb zZu0gKKs9sd8Eh/PSkm38gQR9jNAnho= 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 v11 3/8] bpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu_array maps Date: Tue, 25 Nov 2025 22:58:52 +0800 Message-ID: <20251125145857.98134-4-leon.hwang@linux.dev> In-Reply-To: <20251125145857.98134-1-leon.hwang@linux.dev> References: <20251125145857.98134-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 support for the BPF_F_ALL_CPUS flag in percpu_array maps to allow updating values for all CPUs with a single value for both update_elem and update_batch APIs. Introduce support for the BPF_F_CPU flag in percpu_array maps to allow: * update value for specified CPU for both update_elem and update_batch APIs. * lookup value for specified CPU for both lookup_elem and lookup_batch APIs. The BPF_F_CPU flag is passed via: * map_flags of lookup_elem and update_elem APIs along with embedded cpu info. * elem_flags of lookup_batch and update_batch APIs along with embedded cpu info. Signed-off-by: Leon Hwang --- v10 -> v11: - Drop buggy '(u32)map_flags > BPF_F_ALL_CPUS' check in bpf_percpu_array_u= pdate(). - Update 'map_flags =3D=3D BPF_NOEXIST' to 'map_flags & BPF_NOEXIST' in bp= f_percpu_array_update(). --- include/linux/bpf.h | 9 +++++++-- kernel/bpf/arraymap.c | 32 ++++++++++++++++++++++++-------- kernel/bpf/syscall.c | 2 +- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index d84af3719b59..01a99e3a3e51 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2762,7 +2762,7 @@ int map_set_for_each_callback_args(struct bpf_verifie= r_env *env, struct bpf_func_state *callee); =20 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value); -int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value); +int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value, u64= flags); int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value, u64 flags); int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value, @@ -3831,7 +3831,12 @@ bpf_prog_update_insn_ptrs(struct bpf_prog *prog, u32= *offsets, void *image) =20 static inline bool bpf_map_supports_cpu_flags(enum bpf_map_type map_type) { - return false; + switch (map_type) { + case BPF_MAP_TYPE_PERCPU_ARRAY: + return true; + default: + return false; + } } =20 static inline int bpf_map_check_op_flags(struct bpf_map *map, u64 flags, u= 64 allowed_flags) diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index 1eeb31c5b317..241f11d4d62a 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c @@ -307,7 +307,7 @@ static void *percpu_array_map_lookup_percpu_elem(struct= bpf_map *map, void *key, return per_cpu_ptr(array->pptrs[index & array->index_mask], cpu); } =20 -int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value) +int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value, u64= map_flags) { struct bpf_array *array =3D container_of(map, struct bpf_array, map); u32 index =3D *(u32 *)key; @@ -325,11 +325,18 @@ int bpf_percpu_array_copy(struct bpf_map *map, void *= key, void *value) size =3D array->elem_size; rcu_read_lock(); pptr =3D array->pptrs[index & array->index_mask]; + if (map_flags & BPF_F_CPU) { + cpu =3D map_flags >> 32; + copy_map_value(map, value, per_cpu_ptr(pptr, cpu)); + check_and_init_map_value(map, value); + goto unlock; + } for_each_possible_cpu(cpu) { copy_map_value_long(map, value + off, per_cpu_ptr(pptr, cpu)); check_and_init_map_value(map, value + off); off +=3D size; } +unlock: rcu_read_unlock(); return 0; } @@ -398,18 +405,18 @@ int bpf_percpu_array_update(struct bpf_map *map, void= *key, void *value, struct bpf_array *array =3D container_of(map, struct bpf_array, map); u32 index =3D *(u32 *)key; void __percpu *pptr; - int cpu, off =3D 0; + void *ptr, *val; u32 size; + int cpu; =20 - if (unlikely(map_flags > BPF_EXIST)) - /* unknown flags */ + if (unlikely(map_flags & BPF_F_LOCK)) return -EINVAL; =20 if (unlikely(index >=3D array->map.max_entries)) /* all elements were pre-allocated, cannot insert a new one */ return -E2BIG; =20 - if (unlikely(map_flags =3D=3D BPF_NOEXIST)) + if (unlikely(map_flags & BPF_NOEXIST)) /* all elements already exist */ return -EEXIST; =20 @@ -422,11 +429,20 @@ int bpf_percpu_array_update(struct bpf_map *map, void= *key, void *value, size =3D array->elem_size; rcu_read_lock(); pptr =3D array->pptrs[index & array->index_mask]; + if (map_flags & BPF_F_CPU) { + cpu =3D map_flags >> 32; + ptr =3D per_cpu_ptr(pptr, cpu); + copy_map_value(map, ptr, value); + bpf_obj_free_fields(array->map.record, ptr); + goto unlock; + } for_each_possible_cpu(cpu) { - copy_map_value_long(map, per_cpu_ptr(pptr, cpu), value + off); - bpf_obj_free_fields(array->map.record, per_cpu_ptr(pptr, cpu)); - off +=3D size; + ptr =3D per_cpu_ptr(pptr, cpu); + val =3D (map_flags & BPF_F_ALL_CPUS) ? value : value + size * cpu; + copy_map_value(map, ptr, val); + bpf_obj_free_fields(array->map.record, ptr); } +unlock: rcu_read_unlock(); return 0; } diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 3c3e3b4095b9..238238086b08 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -318,7 +318,7 @@ static int bpf_map_copy_value(struct bpf_map *map, void= *key, void *value, map->map_type =3D=3D BPF_MAP_TYPE_LRU_PERCPU_HASH) { err =3D bpf_percpu_hash_copy(map, key, value); } else if (map->map_type =3D=3D BPF_MAP_TYPE_PERCPU_ARRAY) { - err =3D bpf_percpu_array_copy(map, key, value); + err =3D bpf_percpu_array_copy(map, key, value, flags); } else if (map->map_type =3D=3D BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { err =3D bpf_percpu_cgroup_storage_copy(map, key, value); } else if (map->map_type =3D=3D BPF_MAP_TYPE_STACK_TRACE) { --=20 2.51.2 From nobody Tue Dec 2 00:03:26 2025 Received: from out-176.mta1.migadu.com (out-176.mta1.migadu.com [95.215.58.176]) (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 73398326D44 for ; Tue, 25 Nov 2025 15:00:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764082818; cv=none; b=X7Ruu3dI+LFr5HOQRiqWfxa1PhvLNoZM+Th68ffAqxp00kjU/iseiu3iaJagEacRmJycY810odL6LRbe2BPtLzyrfHpjpGrubbj80f45p84ZpDPM5pIFsCH6lROV3YKT6VTZbKCW/Y7fCeqbGQNcYjIPffTdM2ZHhrpnzA5CFow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764082818; c=relaxed/simple; bh=YLYaiilqKClYzhytmoz4mvsubaGXwk6JKfgQRJnZwcI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=twLLlqZXcV/80LNvg5cpSOE1/dwoYna1Vf/mWnEQHrGvvGvsu4s48gQQnOfDAZJaj3y0A2n3+ZYbEN2/pnNDfm/6bL4a30/pkbQ7k28rweRUsKbQJu5XYlT5w2ZhHzOU5q2Td7BQhy/hewLXXQVFZ3DdVuqs33aSNOpOfyq7SMU= 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=ZuWqxPQZ; arc=none smtp.client-ip=95.215.58.176 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="ZuWqxPQZ" 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=1764082814; 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=CtARq3xEj4HK+Nz5xzCIbjLK7YrBAKPzzKvtA15d36E=; b=ZuWqxPQZzjCclZ+o/Lqrb29oFi8JGn9FSNQE6fsR6tZh6beNN0kEhwmd7ROrchcnumcdpo Qk69zs2lCttYwE1nfq4Z+C0VFJXgYxDEeqCFcMWKnIQyYQWDd3YYwfuGIP7WoY+eUihGTZ p8wayxtc8J5wVGHeep/ytad8aoGoJMc= 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 v11 4/8] bpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu_hash and lru_percpu_hash maps Date: Tue, 25 Nov 2025 22:58:53 +0800 Message-ID: <20251125145857.98134-5-leon.hwang@linux.dev> In-Reply-To: <20251125145857.98134-1-leon.hwang@linux.dev> References: <20251125145857.98134-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_ALL_CPUS flag support for percpu_hash and lru_percpu_hash maps to allow updating values for all CPUs with a single value for both update_elem and update_batch APIs. Introduce BPF_F_CPU flag support for percpu_hash and lru_percpu_hash maps to allow: * update value for specified CPU for both update_elem and update_batch APIs. * lookup value for specified CPU for both lookup_elem and lookup_batch APIs. The BPF_F_CPU flag is passed via: * map_flags along with embedded cpu info. * elem_flags along with embedded cpu info. Signed-off-by: Leon Hwang --- v10 -> v11: - Drop buggy '(u32)map_flags > BPF_F_ALL_CPUS' check in htab_map_check_upd= ate_flags(). - Update 'map_flags !=3D BPF_EXIST' to '!(map_flags & BPF_EXIST)' in __htab_lru_percpu_map_update_elem(). --- include/linux/bpf.h | 4 +- kernel/bpf/hashtab.c | 96 ++++++++++++++++++++++++++++++-------------- kernel/bpf/syscall.c | 2 +- 3 files changed, 69 insertions(+), 33 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 01a99e3a3e51..f79d2ae27335 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2761,7 +2761,7 @@ int map_set_for_each_callback_args(struct bpf_verifie= r_env *env, struct bpf_func_state *caller, struct bpf_func_state *callee); =20 -int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value); +int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value, u64 = flags); int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value, u64= flags); int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value, u64 flags); @@ -3833,6 +3833,8 @@ static inline bool bpf_map_supports_cpu_flags(enum bp= f_map_type map_type) { switch (map_type) { case BPF_MAP_TYPE_PERCPU_ARRAY: + case BPF_MAP_TYPE_PERCPU_HASH: + case BPF_MAP_TYPE_LRU_PERCPU_HASH: return true; default: return false; diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index c8a9b27f8663..c768bf71d60f 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -932,7 +932,7 @@ static void free_htab_elem(struct bpf_htab *htab, struc= t htab_elem *l) } =20 static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr, - void *value, bool onallcpus) + void *value, bool onallcpus, u64 map_flags) { void *ptr; =20 @@ -943,19 +943,28 @@ static void pcpu_copy_value(struct bpf_htab *htab, vo= id __percpu *pptr, bpf_obj_free_fields(htab->map.record, ptr); } else { u32 size =3D round_up(htab->map.value_size, 8); - int off =3D 0, cpu; + void *val; + int cpu; + + if (map_flags & BPF_F_CPU) { + cpu =3D map_flags >> 32; + ptr =3D per_cpu_ptr(pptr, cpu); + copy_map_value(&htab->map, ptr, value); + bpf_obj_free_fields(htab->map.record, ptr); + return; + } =20 for_each_possible_cpu(cpu) { ptr =3D per_cpu_ptr(pptr, cpu); - copy_map_value_long(&htab->map, ptr, value + off); + val =3D (map_flags & BPF_F_ALL_CPUS) ? value : value + size * cpu; + copy_map_value(&htab->map, ptr, val); bpf_obj_free_fields(htab->map.record, ptr); - off +=3D size; } } } =20 static void pcpu_init_value(struct bpf_htab *htab, void __percpu *pptr, - void *value, bool onallcpus) + void *value, bool onallcpus, u64 map_flags) { /* When not setting the initial value on all cpus, zero-fill element * values for other cpus. Otherwise, bpf program has no way to ensure @@ -973,7 +982,7 @@ static void pcpu_init_value(struct bpf_htab *htab, void= __percpu *pptr, zero_map_value(&htab->map, per_cpu_ptr(pptr, cpu)); } } else { - pcpu_copy_value(htab, pptr, value, onallcpus); + pcpu_copy_value(htab, pptr, value, onallcpus, map_flags); } } =20 @@ -985,7 +994,7 @@ static bool fd_htab_map_needs_adjust(const struct bpf_h= tab *htab) static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key, void *value, u32 key_size, u32 hash, bool percpu, bool onallcpus, - struct htab_elem *old_elem) + struct htab_elem *old_elem, u64 map_flags) { u32 size =3D htab->map.value_size; bool prealloc =3D htab_is_prealloc(htab); @@ -1043,7 +1052,7 @@ static struct htab_elem *alloc_htab_elem(struct bpf_h= tab *htab, void *key, pptr =3D *(void __percpu **)ptr; } =20 - pcpu_init_value(htab, pptr, value, onallcpus); + pcpu_init_value(htab, pptr, value, onallcpus, map_flags); =20 if (!prealloc) htab_elem_set_ptr(l_new, key_size, pptr); @@ -1147,7 +1156,7 @@ static long htab_map_update_elem(struct bpf_map *map,= void *key, void *value, } =20 l_new =3D alloc_htab_elem(htab, key, value, key_size, hash, false, false, - l_old); + l_old, map_flags); if (IS_ERR(l_new)) { /* all pre-allocated elements are in use or memory exhausted */ ret =3D PTR_ERR(l_new); @@ -1249,6 +1258,15 @@ static long htab_lru_map_update_elem(struct bpf_map = *map, void *key, void *value return ret; } =20 +static int htab_map_check_update_flags(bool onallcpus, u64 map_flags) +{ + if (unlikely(!onallcpus && map_flags > BPF_EXIST)) + return -EINVAL; + if (unlikely(onallcpus && (map_flags & BPF_F_LOCK))) + return -EINVAL; + return 0; +} + static long htab_map_update_elem_in_place(struct bpf_map *map, void *key, void *value, u64 map_flags, bool percpu, bool onallcpus) @@ -1262,9 +1280,9 @@ static long htab_map_update_elem_in_place(struct bpf_= map *map, void *key, u32 key_size, hash; int ret; =20 - if (unlikely(map_flags > BPF_EXIST)) - /* unknown flags */ - return -EINVAL; + ret =3D htab_map_check_update_flags(onallcpus, map_flags); + if (unlikely(ret)) + return ret; =20 WARN_ON_ONCE(!bpf_rcu_lock_held()); =20 @@ -1289,7 +1307,7 @@ static long htab_map_update_elem_in_place(struct bpf_= map *map, void *key, /* Update value in-place */ if (percpu) { pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size), - value, onallcpus); + value, onallcpus, map_flags); } else { void **inner_map_pptr =3D htab_elem_value(l_old, key_size); =20 @@ -1298,7 +1316,7 @@ static long htab_map_update_elem_in_place(struct bpf_= map *map, void *key, } } else { l_new =3D alloc_htab_elem(htab, key, value, key_size, - hash, percpu, onallcpus, NULL); + hash, percpu, onallcpus, NULL, map_flags); if (IS_ERR(l_new)) { ret =3D PTR_ERR(l_new); goto err; @@ -1324,9 +1342,9 @@ static long __htab_lru_percpu_map_update_elem(struct = bpf_map *map, void *key, u32 key_size, hash; int ret; =20 - if (unlikely(map_flags > BPF_EXIST)) - /* unknown flags */ - return -EINVAL; + ret =3D htab_map_check_update_flags(onallcpus, map_flags); + if (unlikely(ret)) + return ret; =20 WARN_ON_ONCE(!bpf_rcu_lock_held()); =20 @@ -1342,7 +1360,7 @@ static long __htab_lru_percpu_map_update_elem(struct = bpf_map *map, void *key, * to remove older elem from htab and this removal * operation will need a bucket lock. */ - if (map_flags !=3D BPF_EXIST) { + if (!(map_flags & BPF_EXIST)) { l_new =3D prealloc_lru_pop(htab, key, hash); if (!l_new) return -ENOMEM; @@ -1363,10 +1381,10 @@ static long __htab_lru_percpu_map_update_elem(struc= t bpf_map *map, void *key, =20 /* per-cpu hash map can update value in-place */ pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size), - value, onallcpus); + value, onallcpus, map_flags); } else { pcpu_init_value(htab, htab_elem_get_ptr(l_new, key_size), - value, onallcpus); + value, onallcpus, map_flags); hlist_nulls_add_head_rcu(&l_new->hash_node, head); l_new =3D NULL; } @@ -1678,9 +1696,9 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *ma= p, void __user *ukeys =3D u64_to_user_ptr(attr->batch.keys); void __user *ubatch =3D u64_to_user_ptr(attr->batch.in_batch); u32 batch, max_count, size, bucket_size, map_id; + u64 elem_map_flags, map_flags, allowed_flags; u32 bucket_cnt, total, key_size, value_size; struct htab_elem *node_to_free =3D NULL; - u64 elem_map_flags, map_flags; struct hlist_nulls_head *head; struct hlist_nulls_node *n; unsigned long flags =3D 0; @@ -1690,9 +1708,12 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *m= ap, int ret =3D 0; =20 elem_map_flags =3D attr->batch.elem_flags; - if ((elem_map_flags & ~BPF_F_LOCK) || - ((elem_map_flags & BPF_F_LOCK) && !btf_record_has_field(map->record, = BPF_SPIN_LOCK))) - return -EINVAL; + allowed_flags =3D BPF_F_LOCK; + if (!do_delete && is_percpu) + allowed_flags |=3D BPF_F_CPU; + ret =3D bpf_map_check_op_flags(map, elem_map_flags, allowed_flags); + if (ret) + return ret; =20 map_flags =3D attr->batch.flags; if (map_flags) @@ -1715,7 +1736,7 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *ma= p, key_size =3D htab->map.key_size; value_size =3D htab->map.value_size; size =3D round_up(value_size, 8); - if (is_percpu) + if (is_percpu && !(elem_map_flags & BPF_F_CPU)) value_size =3D size * num_possible_cpus(); total =3D 0; /* while experimenting with hash tables with sizes ranging from 10 to @@ -1798,10 +1819,17 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *= map, void __percpu *pptr; =20 pptr =3D htab_elem_get_ptr(l, map->key_size); - for_each_possible_cpu(cpu) { - copy_map_value_long(&htab->map, dst_val + off, per_cpu_ptr(pptr, cpu)); - check_and_init_map_value(&htab->map, dst_val + off); - off +=3D size; + if (elem_map_flags & BPF_F_CPU) { + cpu =3D elem_map_flags >> 32; + copy_map_value(&htab->map, dst_val, per_cpu_ptr(pptr, cpu)); + check_and_init_map_value(&htab->map, dst_val); + } else { + for_each_possible_cpu(cpu) { + copy_map_value_long(&htab->map, dst_val + off, + per_cpu_ptr(pptr, cpu)); + check_and_init_map_value(&htab->map, dst_val + off); + off +=3D size; + } } } else { value =3D htab_elem_value(l, key_size); @@ -2357,7 +2385,7 @@ static void *htab_lru_percpu_map_lookup_percpu_elem(s= truct bpf_map *map, void *k return NULL; } =20 -int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value) +int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value, u64 = map_flags) { struct htab_elem *l; void __percpu *pptr; @@ -2374,16 +2402,22 @@ int bpf_percpu_hash_copy(struct bpf_map *map, void = *key, void *value) l =3D __htab_map_lookup_elem(map, key); if (!l) goto out; + ret =3D 0; /* We do not mark LRU map element here in order to not mess up * eviction heuristics when user space does a map walk. */ pptr =3D htab_elem_get_ptr(l, map->key_size); + if (map_flags & BPF_F_CPU) { + cpu =3D map_flags >> 32; + copy_map_value(map, value, per_cpu_ptr(pptr, cpu)); + check_and_init_map_value(map, value); + goto out; + } for_each_possible_cpu(cpu) { copy_map_value_long(map, value + off, per_cpu_ptr(pptr, cpu)); check_and_init_map_value(map, value + off); off +=3D size; } - ret =3D 0; out: rcu_read_unlock(); return ret; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 238238086b08..7e6cca641026 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -316,7 +316,7 @@ static int bpf_map_copy_value(struct bpf_map *map, void= *key, void *value, bpf_disable_instrumentation(); if (map->map_type =3D=3D BPF_MAP_TYPE_PERCPU_HASH || map->map_type =3D=3D BPF_MAP_TYPE_LRU_PERCPU_HASH) { - err =3D bpf_percpu_hash_copy(map, key, value); + err =3D bpf_percpu_hash_copy(map, key, value, flags); } else if (map->map_type =3D=3D BPF_MAP_TYPE_PERCPU_ARRAY) { err =3D bpf_percpu_array_copy(map, key, value, flags); } else if (map->map_type =3D=3D BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { --=20 2.51.2 From nobody Tue Dec 2 00:03:26 2025 Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) (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 059AE326D44 for ; Tue, 25 Nov 2025 15:00:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764082828; cv=none; b=OJc5i+u5JM3r5h7jcoNx27eOy3Rm5kQp2JmJ2KLKiyj8oosci3Lg8RRuLd9DgTeZFYwL5xYO/KNPmOh9cIDTiCRm5dPphmt0fzDZCXLbvWk0sFKQtRVmrVXXTC2SadLYgKbinzKJyx2Yy5DhwYNNukQ5UawGc4pPGsSb4A6WL3E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764082828; c=relaxed/simple; bh=SZ9BjoQFDYKINn07VcspgXWVdM9dxtKA1Gv6JFK8Y5o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZxnTA8LWsI7dKG5rPJepS/T2wfwyBnrsV73Mry7fPVyPyVjs+46z4dEf65IXwER9g1FdzUhkwDrHB/aCJw3oK1l6KXT9sejdQUa+3isHNtVNVDaBWmgQxOO0rjB+gbD6+MZyqmO+wBUxWpfsWORn9PUSZAgDtlgoePyBTeerH+Q= 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=XZMKMvrU; arc=none smtp.client-ip=95.215.58.181 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="XZMKMvrU" 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=1764082825; 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=pTgwqBgBsAEUNrMYXmgH0c/Rvt/LDiv6qDBO69l3Lvo=; b=XZMKMvrUKlbfm8spXdd6Cd0o1tS3waGseo2VrZfQDKMxr722bMCQMjcwtoi2WFNilpQJem gAeiaIXgSsj/nlN60YMqI6TmrOvHwV1r6DR8iUBCZgbNycTCFoMosqVfMe5ec7hWrL2KGV CTn4CH8nkaQ5yUoaKdwxreJEpvrEE6w= 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 v11 5/8] bpf: Copy map value using copy_map_value_long for percpu_cgroup_storage maps Date: Tue, 25 Nov 2025 22:58:54 +0800 Message-ID: <20251125145857.98134-6-leon.hwang@linux.dev> In-Reply-To: <20251125145857.98134-1-leon.hwang@linux.dev> References: <20251125145857.98134-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" Copy map value using 'copy_map_value_long()'. It's to keep consistent style with the way of other percpu maps. No functional change intended. Signed-off-by: Leon Hwang --- kernel/bpf/local_storage.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/local_storage.c b/kernel/bpf/local_storage.c index c93a756e035c..2ab4b60ffe61 100644 --- a/kernel/bpf/local_storage.c +++ b/kernel/bpf/local_storage.c @@ -200,8 +200,7 @@ int bpf_percpu_cgroup_storage_copy(struct bpf_map *_map= , void *key, */ size =3D round_up(_map->value_size, 8); for_each_possible_cpu(cpu) { - bpf_long_memcpy(value + off, - per_cpu_ptr(storage->percpu_buf, cpu), size); + copy_map_value_long(_map, value + off, per_cpu_ptr(storage->percpu_buf, = cpu)); off +=3D size; } rcu_read_unlock(); @@ -234,8 +233,7 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *_m= ap, void *key, */ size =3D round_up(_map->value_size, 8); for_each_possible_cpu(cpu) { - bpf_long_memcpy(per_cpu_ptr(storage->percpu_buf, cpu), - value + off, size); + copy_map_value_long(_map, per_cpu_ptr(storage->percpu_buf, cpu), value += off); off +=3D size; } rcu_read_unlock(); --=20 2.51.2 From nobody Tue Dec 2 00:03:26 2025 Received: from out-187.mta1.migadu.com (out-187.mta1.migadu.com [95.215.58.187]) (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 2629E326D69 for ; Tue, 25 Nov 2025 15:00:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764082842; cv=none; b=qbLobabWTsDrZnuhvoUQoFN9odmyd6ylXObdPrOM+nr6TWYZoWr2WSWlFmd4APoTC4/9Gq+XZlgkRKrfIqqJ4Fz2xtCa35lz2bvOa74gbThrZhBsCIOleN7XZJRrNvxDQ6Bv7Jp3lRriTjgIyTHSkuwZAqJdL/X0I3dUXCupUQg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764082842; c=relaxed/simple; bh=ghgsY45RF5TkR9lKrPWdNbTeA1WJdKJkm9atuq4v5K8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eV3SlRhCA+R2B7d6cqDsbjpvMI2dw7waiTy3KLdjgLUpSBB2EnZ+Hf+EfYqaofix+UxTfR4MfyeRI7Rc9wLnHCDvjaaiLqVGihlUme7HVgUF9cYUBUXaC8z/ocK/fL6E0RvJDshMPgpCWlFVCPyZY19oWKjwuRvYoVDgEEd8Xs4= 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=mLvyypKE; arc=none smtp.client-ip=95.215.58.187 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="mLvyypKE" 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=1764082837; 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=p81YWnqve83/ohG8fCF50rW1gpi+KzxyiVHkrFHkBUY=; b=mLvyypKEVqlix/AHMUJktiXy8/QGxKeN03rJ8o2jktzfTjb6QRaKIhH7Thnh+nPuqc9uk6 ZLe4cVYhqHj6cUeOwFxZOEP5NuZEinxGVxj/P/UQO4XNv9kEDppGkj+4uN2FQbELe+6Pf9 v38bTtHifVRCd3phUX6lmOCMzOlpHts= 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 v11 6/8] bpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu_cgroup_storage maps Date: Tue, 25 Nov 2025 22:58:55 +0800 Message-ID: <20251125145857.98134-7-leon.hwang@linux.dev> In-Reply-To: <20251125145857.98134-1-leon.hwang@linux.dev> References: <20251125145857.98134-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_ALL_CPUS flag support for percpu_cgroup_storage maps to allow updating values for all CPUs with a single value for update_elem API. Introduce BPF_F_CPU flag support for percpu_cgroup_storage maps to allow: * update value for specified CPU for update_elem API. * lookup value for specified CPU for lookup_elem API. The BPF_F_CPU flag is passed via map_flags along with embedded cpu info. Signed-off-by: Leon Hwang --- include/linux/bpf-cgroup.h | 4 ++-- include/linux/bpf.h | 1 + kernel/bpf/local_storage.c | 23 ++++++++++++++++++----- kernel/bpf/syscall.c | 2 +- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h index aedf573bdb42..013f4db9903f 100644 --- a/include/linux/bpf-cgroup.h +++ b/include/linux/bpf-cgroup.h @@ -172,7 +172,7 @@ void bpf_cgroup_storage_link(struct bpf_cgroup_storage = *storage, void bpf_cgroup_storage_unlink(struct bpf_cgroup_storage *storage); int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux, struct bpf_map *ma= p); =20 -int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void *key, void *v= alue); +int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void *key, void *v= alue, u64 flags); int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key, void *value, u64 flags); =20 @@ -467,7 +467,7 @@ static inline struct bpf_cgroup_storage *bpf_cgroup_sto= rage_alloc( static inline void bpf_cgroup_storage_free( struct bpf_cgroup_storage *storage) {} static inline int bpf_percpu_cgroup_storage_copy(struct bpf_map *map, void= *key, - void *value) { + void *value, u64 flags) { return 0; } static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map, diff --git a/include/linux/bpf.h b/include/linux/bpf.h index f79d2ae27335..9e756db5e132 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -3835,6 +3835,7 @@ static inline bool bpf_map_supports_cpu_flags(enum bp= f_map_type map_type) case BPF_MAP_TYPE_PERCPU_ARRAY: case BPF_MAP_TYPE_PERCPU_HASH: case BPF_MAP_TYPE_LRU_PERCPU_HASH: + case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: return true; default: return false; diff --git a/kernel/bpf/local_storage.c b/kernel/bpf/local_storage.c index 2ab4b60ffe61..1ccbf28b2ad9 100644 --- a/kernel/bpf/local_storage.c +++ b/kernel/bpf/local_storage.c @@ -180,7 +180,7 @@ static long cgroup_storage_update_elem(struct bpf_map *= map, void *key, } =20 int bpf_percpu_cgroup_storage_copy(struct bpf_map *_map, void *key, - void *value) + void *value, u64 map_flags) { struct bpf_cgroup_storage_map *map =3D map_to_storage(_map); struct bpf_cgroup_storage *storage; @@ -198,11 +198,17 @@ int bpf_percpu_cgroup_storage_copy(struct bpf_map *_m= ap, void *key, * access 'value_size' of them, so copying rounded areas * will not leak any kernel data */ + if (map_flags & BPF_F_CPU) { + cpu =3D map_flags >> 32; + copy_map_value(_map, value, per_cpu_ptr(storage->percpu_buf, cpu)); + goto unlock; + } size =3D round_up(_map->value_size, 8); for_each_possible_cpu(cpu) { copy_map_value_long(_map, value + off, per_cpu_ptr(storage->percpu_buf, = cpu)); off +=3D size; } +unlock: rcu_read_unlock(); return 0; } @@ -212,10 +218,11 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *= _map, void *key, { struct bpf_cgroup_storage_map *map =3D map_to_storage(_map); struct bpf_cgroup_storage *storage; - int cpu, off =3D 0; + void *val; u32 size; + int cpu; =20 - if (map_flags !=3D BPF_ANY && map_flags !=3D BPF_EXIST) + if ((u32)map_flags & ~(BPF_ANY | BPF_EXIST | BPF_F_CPU | BPF_F_ALL_CPUS)) return -EINVAL; =20 rcu_read_lock(); @@ -231,11 +238,17 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *= _map, void *key, * returned or zeros which were zero-filled by percpu_alloc, * so no kernel data leaks possible */ + if (map_flags & BPF_F_CPU) { + cpu =3D map_flags >> 32; + copy_map_value(_map, per_cpu_ptr(storage->percpu_buf, cpu), value); + goto unlock; + } size =3D round_up(_map->value_size, 8); for_each_possible_cpu(cpu) { - copy_map_value_long(_map, per_cpu_ptr(storage->percpu_buf, cpu), value += off); - off +=3D size; + val =3D (map_flags & BPF_F_ALL_CPUS) ? value : value + size * cpu; + copy_map_value(_map, per_cpu_ptr(storage->percpu_buf, cpu), val); } +unlock: rcu_read_unlock(); return 0; } diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 7e6cca641026..d64567c6ef50 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -320,7 +320,7 @@ static int bpf_map_copy_value(struct bpf_map *map, void= *key, void *value, } else if (map->map_type =3D=3D BPF_MAP_TYPE_PERCPU_ARRAY) { err =3D bpf_percpu_array_copy(map, key, value, flags); } else if (map->map_type =3D=3D BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { - err =3D bpf_percpu_cgroup_storage_copy(map, key, value); + err =3D bpf_percpu_cgroup_storage_copy(map, key, value, flags); } else if (map->map_type =3D=3D BPF_MAP_TYPE_STACK_TRACE) { err =3D bpf_stackmap_extract(map, key, value, false); } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) { --=20 2.51.2 From nobody Tue Dec 2 00:03:26 2025 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 AD76732549B for ; Tue, 25 Nov 2025 15:00:52 +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=1764082854; cv=none; b=k5D5KmQ2SYecOe9r7ijNhOeBQC9K3uRSg/GF7NGVBoUjZoZGMjC5nFl2t8HtoGVkXT3MZD6Xr0/ahx/QPOPZoHwbr3cyvPYjbxaBzcixZOFeFUECqtvUqZsNm/5HVzwio4fTNdWFhHCCAGG1cZSV0VElNwzYE9bZEfH9p3M6ALs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764082854; c=relaxed/simple; bh=RyzU1nvpVDDyI/x41Rp1aJ+FSOyyVTP60YrViacI0nI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GUOB021Cw7Rl4fYm9bhifxb8rK/rC8u3kyw58sHU3qiY76LNs/vP4kTKK2lYdor+MDKXxPjo6mPGszOayenSYL3xi34Br5a6TkDEBKr1hWUAZWM+J2Sbl6t5jakUumbu3J8D/6jX0epBPEEY8H7BF8NsTpke4jUupteLSe3ZcMo= 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=n9pXiJxj; 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="n9pXiJxj" 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=1764082850; 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=t/m1iVVWkADqfuYoTCAJpHjXrN2MZ0Goe5Iqd3/D1z4=; b=n9pXiJxjfw1/EEVa2jTGyf/hga1mIyHYdiETOof9q0eboe7Q2h9oD98frnUU9cYQvck1Dj oxP0Of+oYvOU+730gE0ZP3BAEEytR6fT+ZrH6ipOdCOjjR8VMo+MNwmD6Nzcg7BWkWPJhe rdgiNkGguICQpkV10bAHS6zodX+6AJY= 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 v11 7/8] libbpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu maps Date: Tue, 25 Nov 2025 22:58:56 +0800 Message-ID: <20251125145857.98134-8-leon.hwang@linux.dev> In-Reply-To: <20251125145857.98134-1-leon.hwang@linux.dev> References: <20251125145857.98134-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 5118d0a90e24..7c38b2e54608 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1196,12 +1196,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()`. * @flags extra flags passed to kernel for this operation * @return 0, on success; negative error, otherwise * @@ -1219,13 +1220,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.' * @flags extra flags passed to kernel for this operation * @return 0, on success; negative error, otherwise * --=20 2.51.2 From nobody Tue Dec 2 00:03:26 2025 Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) (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 A0636326D6E for ; Tue, 25 Nov 2025 15:01:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764082865; cv=none; b=sE+JSCONqOQLuqKD7jxUYQrRbvTFm6QFkcYA5es4cVnKGqXCEm5teMpbUHMHxDTjrigDfr+VJgZT713AyhfIGebrvnsfGVLovqIEGnrKKQ2vddjxGMhZUSxtjQIykQkOJ0CpYFaVJK1ZBPNnYM34jpm8aoI/SCgc6a82IGmdeT8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764082865; c=relaxed/simple; bh=cBe2KXtHyR0PT74TZ23ZnTf7fEywItibmG7jTJ82y8A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sC4ywjAZ8GlNNAYBABCep2ZpDyjELTRRB0N9e42qqjCN1J1OdjDJBNAQhvpQ4EUfcz3g0hD4540IGW9I6GNJKAKG7MeCHDNwGMaUf/qDgalu3yWLiD/CCIAxpUtFeCwsHK84EVZ73UY07s2llqrfzi37O3iT3/I6ANGorXpnNM4= 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=Cz/hkvnX; arc=none smtp.client-ip=95.215.58.181 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="Cz/hkvnX" 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=1764082861; 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=mNm/oiHjXtZXQxDXHUMrktvrMjAL0D6u7psU6Ujt1O8=; b=Cz/hkvnXnKGoYewJ0sYcIjrVf1ED34J22GYuw3j/MlAhOdYNq+QfEpWGpB9+ow+H9Q6Cvm euJrASxOYb2tm9B5Xw2/iNe0UCc3aOs0L+voWWJgivQ0EK1iW9oLxt3d0HV6rcakg6SOTO OpqFpcEg/O94LQuhrCHNII+5mrv6TzQ= 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 v11 8/8] selftests/bpf: Add cases to test BPF_F_CPU and BPF_F_ALL_CPUS flags Date: Tue, 25 Nov 2025 22:58:57 +0800 Message-ID: <20251125145857.98134-9-leon.hwang@linux.dev> In-Reply-To: <20251125145857.98134-1-leon.hwang@linux.dev> References: <20251125145857.98134-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 test coverage for the new BPF_F_CPU and BPF_F_ALL_CPUS flags support in percpu maps. The following APIs are exercised: * bpf_map_update_batch() * bpf_map_lookup_batch() * bpf_map_update_elem() * bpf_map__update_elem() * bpf_map_lookup_elem_flags() * bpf_map__lookup_elem() Add tests to verify that array and hash maps do not support BPF_F_CPU and BPF_F_ALL_CPUS flags. Signed-off-by: Leon Hwang --- v10 -> v11: - Use libbpf_num_possible_cpus() as max_entries for lru_percpu_hash map. - Add BPF_EXIST to flags when update_elem() and update_batch(). --- .../selftests/bpf/prog_tests/percpu_alloc.c | 335 ++++++++++++++++++ .../selftests/bpf/progs/percpu_alloc_array.c | 32 ++ 2 files changed, 367 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/percpu_alloc.c b/tools/= testing/selftests/bpf/prog_tests/percpu_alloc.c index 343da65864d6..ff31107434d7 100644 --- a/tools/testing/selftests/bpf/prog_tests/percpu_alloc.c +++ b/tools/testing/selftests/bpf/prog_tests/percpu_alloc.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include "cgroup_helpers.h" #include "percpu_alloc_array.skel.h" #include "percpu_alloc_cgrp_local_storage.skel.h" #include "percpu_alloc_fail.skel.h" @@ -115,6 +116,328 @@ static void test_failure(void) { RUN_TESTS(percpu_alloc_fail); } =20 +static void test_percpu_map_op_cpu_flag(struct bpf_map *map, void *keys, s= ize_t key_sz, + u32 max_entries, bool test_batch) +{ + size_t value_sz =3D sizeof(u32), value_sz_total; + u32 *values =3D NULL, *values_percpu =3D NULL; + int i, j, cpu, map_fd, nr_cpus, err; + const u32 value =3D 0xDEADC0DE; + u64 batch =3D 0, flags; + void *values_row; + u32 count, v; + LIBBPF_OPTS(bpf_map_batch_opts, batch_opts); + + nr_cpus =3D libbpf_num_possible_cpus(); + if (!ASSERT_GT(nr_cpus, 0, "libbpf_num_possible_cpus")) + return; + + values =3D calloc(max_entries, value_sz * nr_cpus); + if (!ASSERT_OK_PTR(values, "calloc values")) + return; + + values_percpu =3D calloc(max_entries, roundup(value_sz, 8) * nr_cpus); + if (!ASSERT_OK_PTR(values_percpu, "calloc values_percpu")) { + free(values); + return; + } + memset(values_percpu, 0, roundup(value_sz, 8) * nr_cpus * max_entries); + + value_sz_total =3D value_sz * nr_cpus * max_entries; + memset(values, 0, value_sz_total); + + map_fd =3D bpf_map__fd(map); + flags =3D BPF_F_CPU | BPF_F_ALL_CPUS; + err =3D bpf_map_lookup_elem_flags(map_fd, keys, values, flags); + if (!ASSERT_ERR(err, "bpf_map_lookup_elem_flags cpu|all_cpus")) + goto out; + + err =3D bpf_map_update_elem(map_fd, keys, values, flags); + if (!ASSERT_ERR(err, "bpf_map_update_elem cpu|all_cpus")) + goto out; + + flags =3D BPF_F_ALL_CPUS; + err =3D bpf_map_lookup_elem_flags(map_fd, keys, values, flags); + if (!ASSERT_ERR(err, "bpf_map_lookup_elem_flags all_cpus")) + goto out; + + flags =3D BPF_F_LOCK | BPF_F_CPU; + err =3D bpf_map_lookup_elem_flags(map_fd, keys, values, flags); + if (!ASSERT_ERR(err, "bpf_map_lookup_elem_flags BPF_F_LOCK")) + goto out; + + flags =3D BPF_F_LOCK | BPF_F_ALL_CPUS; + err =3D bpf_map_update_elem(map_fd, keys, values, flags); + if (!ASSERT_ERR(err, "bpf_map_update_elem BPF_F_LOCK")) + goto out; + + flags =3D (u64)nr_cpus << 32 | BPF_F_CPU; + err =3D bpf_map_update_elem(map_fd, keys, values, flags); + if (!ASSERT_EQ(err, -ERANGE, "bpf_map_update_elem -ERANGE")) + goto out; + + err =3D bpf_map__update_elem(map, keys, key_sz, values, value_sz, flags); + if (!ASSERT_EQ(err, -ERANGE, "bpf_map__update_elem -ERANGE")) + goto out; + + err =3D bpf_map_lookup_elem_flags(map_fd, keys, values, flags); + if (!ASSERT_EQ(err, -ERANGE, "bpf_map_lookup_elem_flags -ERANGE")) + goto out; + + err =3D bpf_map__lookup_elem(map, keys, key_sz, values, value_sz, flags); + if (!ASSERT_EQ(err, -ERANGE, "bpf_map__lookup_elem -ERANGE")) + goto out; + + + flags =3D BPF_ANY; + for (i =3D 0; i < max_entries; i++) { + err =3D bpf_map__update_elem(map, keys + i * key_sz, key_sz, values_perc= pu, + roundup(value_sz, 8) * nr_cpus, flags); + if (!ASSERT_OK(err, "bpf_map__update_elem init")) + goto out; + } + + for (cpu =3D 0; cpu < nr_cpus; cpu++) { + /* clear value on all cpus */ + values[0] =3D 0; + flags =3D BPF_F_ALL_CPUS | BPF_EXIST; + for (i =3D 0; i < max_entries; i++) { + err =3D bpf_map__update_elem(map, keys + i * key_sz, key_sz, values, + value_sz, flags); + if (!ASSERT_OK(err, "bpf_map__update_elem all_cpus")) + goto out; + } + + /* update value on specified cpu */ + for (i =3D 0; i < max_entries; i++) { + values[0] =3D value; + flags =3D (u64)cpu << 32 | BPF_F_CPU | BPF_EXIST; + err =3D bpf_map__update_elem(map, keys + i * key_sz, key_sz, values, + value_sz, flags); + if (!ASSERT_OK(err, "bpf_map__update_elem specified cpu")) + goto out; + + /* lookup then check value on CPUs */ + for (j =3D 0; j < nr_cpus; j++) { + flags =3D (u64)j << 32 | BPF_F_CPU; + err =3D bpf_map__lookup_elem(map, keys + i * key_sz, key_sz, values, + value_sz, flags); + if (!ASSERT_OK(err, "bpf_map__lookup_elem specified cpu")) + goto out; + if (!ASSERT_EQ(values[0], j !=3D cpu ? 0 : value, + "bpf_map__lookup_elem value on specified cpu")) + goto out; + } + } + } + + if (!test_batch) + goto out; + + count =3D max_entries; + batch_opts.elem_flags =3D (u64)nr_cpus << 32 | BPF_F_CPU; + err =3D bpf_map_update_batch(map_fd, keys, values, &count, &batch_opts); + if (!ASSERT_EQ(err, -ERANGE, "bpf_map_update_batch -ERANGE")) + goto out; + + for (cpu =3D 0; cpu < nr_cpus; cpu++) { + memset(values, 0, value_sz_total); + + /* clear values across all CPUs */ + count =3D max_entries; + batch_opts.elem_flags =3D BPF_F_ALL_CPUS | BPF_EXIST; + err =3D bpf_map_update_batch(map_fd, keys, values, &count, &batch_opts); + if (!ASSERT_OK(err, "bpf_map_update_batch all_cpus")) + goto out; + + /* update values on specified CPU */ + for (i =3D 0; i < max_entries; i++) + values[i] =3D value; + + count =3D max_entries; + batch_opts.elem_flags =3D (u64)cpu << 32 | BPF_F_CPU | BPF_EXIST; + err =3D bpf_map_update_batch(map_fd, keys, values, &count, &batch_opts); + if (!ASSERT_OK(err, "bpf_map_update_batch specified cpu")) + goto out; + + /* lookup values on specified CPU */ + count =3D max_entries; + memset(values, 0, max_entries * value_sz); + batch_opts.elem_flags =3D (u64)cpu << 32 | BPF_F_CPU; + err =3D bpf_map_lookup_batch(map_fd, NULL, &batch, keys, values, &count,= &batch_opts); + if (!ASSERT_TRUE(!err || err =3D=3D -ENOENT, "bpf_map_lookup_batch speci= fied cpu")) + goto out; + + for (i =3D 0; i < max_entries; i++) + if (!ASSERT_EQ(values[i], value, + "bpf_map_lookup_batch value on specified cpu")) + goto out; + + /* lookup values from all CPUs */ + batch =3D 0; + count =3D max_entries; + batch_opts.elem_flags =3D 0; + memset(values_percpu, 0, roundup(value_sz, 8) * nr_cpus * max_entries); + err =3D bpf_map_lookup_batch(map_fd, NULL, &batch, keys, values_percpu, = &count, + &batch_opts); + if (!ASSERT_TRUE(!err || err =3D=3D -ENOENT, "bpf_map_lookup_batch all_c= pus")) + goto out; + + for (i =3D 0; i < max_entries; i++) { + values_row =3D (void *) values_percpu + + roundup(value_sz, 8) * i * nr_cpus; + for (j =3D 0; j < nr_cpus; j++) { + v =3D *(u32 *) (values_row + roundup(value_sz, 8) * j); + if (!ASSERT_EQ(v, j !=3D cpu ? 0 : value, + "bpf_map_lookup_batch value all_cpus")) + goto out; + } + } + } + +out: + free(values_percpu); + free(values); +} + +static void test_percpu_map_cpu_flag(enum bpf_map_type map_type, u32 max_e= ntries) +{ + struct percpu_alloc_array *skel; + size_t key_sz =3D sizeof(int); + struct bpf_map *map; + int *keys, i, err; + + keys =3D calloc(max_entries, key_sz); + if (!ASSERT_OK_PTR(keys, "calloc keys")) + return; + + for (i =3D 0; i < max_entries; i++) + keys[i] =3D i; + + skel =3D percpu_alloc_array__open(); + if (!ASSERT_OK_PTR(skel, "percpu_alloc_array__open")) { + free(keys); + return; + } + + map =3D skel->maps.percpu; + bpf_map__set_type(map, map_type); + bpf_map__set_max_entries(map, max_entries); + + err =3D percpu_alloc_array__load(skel); + if (!ASSERT_OK(err, "test_percpu_alloc__load")) + goto out; + + test_percpu_map_op_cpu_flag(map, keys, key_sz, max_entries, true); +out: + percpu_alloc_array__destroy(skel); + free(keys); +} + +static void test_percpu_array_cpu_flag(void) +{ + test_percpu_map_cpu_flag(BPF_MAP_TYPE_PERCPU_ARRAY, 2); +} + +static void test_percpu_hash_cpu_flag(void) +{ + test_percpu_map_cpu_flag(BPF_MAP_TYPE_PERCPU_HASH, 2); +} + +static void test_lru_percpu_hash_cpu_flag(void) +{ + int nr_cpus =3D libbpf_num_possible_cpus(); + + if (!ASSERT_GT(nr_cpus, 0, "libbpf_num_possible_cpus")) + return; + + test_percpu_map_cpu_flag(BPF_MAP_TYPE_LRU_PERCPU_HASH, nr_cpus); +} + +static void test_percpu_cgroup_storage_cpu_flag(void) +{ + struct percpu_alloc_array *skel =3D NULL; + struct bpf_cgroup_storage_key key; + int cgroup, prog_fd, err; + struct bpf_map *map; + + err =3D setup_cgroup_environment(); + if (!ASSERT_OK(err, "setup_cgroup_environment")) + return; + + cgroup =3D create_and_get_cgroup("/cg_percpu"); + if (!ASSERT_GE(cgroup, 0, "create_and_get_cgroup")) { + cleanup_cgroup_environment(); + return; + } + + err =3D join_cgroup("/cg_percpu"); + if (!ASSERT_OK(err, "join_cgroup")) + goto out; + + skel =3D percpu_alloc_array__open_and_load(); + if (!ASSERT_OK_PTR(skel, "percpu_alloc_array__open_and_load")) + goto out; + + prog_fd =3D bpf_program__fd(skel->progs.cgroup_egress); + err =3D bpf_prog_attach(prog_fd, cgroup, BPF_CGROUP_INET_EGRESS, 0); + if (!ASSERT_OK(err, "bpf_prog_attach")) + goto out; + + map =3D skel->maps.percpu_cgroup_storage; + err =3D bpf_map_get_next_key(bpf_map__fd(map), NULL, &key); + if (!ASSERT_OK(err, "bpf_map_get_next_key")) + goto out; + + test_percpu_map_op_cpu_flag(map, &key, sizeof(key), 1, false); +out: + bpf_prog_detach2(-1, cgroup, BPF_CGROUP_INET_EGRESS); + close(cgroup); + cleanup_cgroup_environment(); + percpu_alloc_array__destroy(skel); +} + +static void test_map_op_cpu_flag(enum bpf_map_type map_type) +{ + u32 max_entries =3D 1, count =3D max_entries; + u64 flags, batch =3D 0, val =3D 0; + int err, map_fd, key =3D 0; + LIBBPF_OPTS(bpf_map_batch_opts, batch_opts); + + map_fd =3D bpf_map_create(map_type, "test_cpu_flag", sizeof(int), sizeof(= u64), max_entries, + NULL); + if (!ASSERT_GE(map_fd, 0, "bpf_map_create")) + return; + + flags =3D BPF_F_ALL_CPUS; + err =3D bpf_map_update_elem(map_fd, &key, &val, flags); + ASSERT_ERR(err, "bpf_map_update_elem all_cpus"); + + batch_opts.elem_flags =3D BPF_F_ALL_CPUS; + err =3D bpf_map_update_batch(map_fd, &key, &val, &count, &batch_opts); + ASSERT_ERR(err, "bpf_map_update_batch all_cpus"); + + flags =3D BPF_F_CPU; + err =3D bpf_map_lookup_elem_flags(map_fd, &key, &val, flags); + ASSERT_ERR(err, "bpf_map_lookup_elem_flags cpu"); + + batch_opts.elem_flags =3D BPF_F_CPU; + err =3D bpf_map_lookup_batch(map_fd, NULL, &batch, &key, &val, &count, &b= atch_opts); + ASSERT_ERR(err, "bpf_map_lookup_batch cpu"); + + close(map_fd); +} + +static void test_array_cpu_flag(void) +{ + test_map_op_cpu_flag(BPF_MAP_TYPE_ARRAY); +} + +static void test_hash_cpu_flag(void) +{ + test_map_op_cpu_flag(BPF_MAP_TYPE_HASH); +} + void test_percpu_alloc(void) { if (test__start_subtest("array")) @@ -125,4 +448,16 @@ void test_percpu_alloc(void) test_cgrp_local_storage(); if (test__start_subtest("failure_tests")) test_failure(); + if (test__start_subtest("cpu_flag_percpu_array")) + test_percpu_array_cpu_flag(); + if (test__start_subtest("cpu_flag_percpu_hash")) + test_percpu_hash_cpu_flag(); + if (test__start_subtest("cpu_flag_lru_percpu_hash")) + test_lru_percpu_hash_cpu_flag(); + if (test__start_subtest("cpu_flag_percpu_cgroup_storage")) + test_percpu_cgroup_storage_cpu_flag(); + if (test__start_subtest("cpu_flag_array")) + test_array_cpu_flag(); + if (test__start_subtest("cpu_flag_hash")) + test_hash_cpu_flag(); } diff --git a/tools/testing/selftests/bpf/progs/percpu_alloc_array.c b/tools= /testing/selftests/bpf/progs/percpu_alloc_array.c index 37c2d2608ec0..ed6a2a93d5a5 100644 --- a/tools/testing/selftests/bpf/progs/percpu_alloc_array.c +++ b/tools/testing/selftests/bpf/progs/percpu_alloc_array.c @@ -187,4 +187,36 @@ int BPF_PROG(test_array_map_10) return 0; } =20 +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(max_entries, 2); + __type(key, int); + __type(value, u32); +} percpu SEC(".maps"); + +SEC("?fentry/bpf_fentry_test1") +int BPF_PROG(test_percpu_array, int x) +{ + u64 value =3D 0xDEADC0DE; + int key =3D 0; + + bpf_map_update_elem(&percpu, &key, &value, BPF_ANY); + return 0; +} + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); + __type(key, struct bpf_cgroup_storage_key); + __type(value, u32); +} percpu_cgroup_storage SEC(".maps"); + +SEC("cgroup_skb/egress") +int cgroup_egress(struct __sk_buff *skb) +{ + u32 *val =3D bpf_get_local_storage(&percpu_cgroup_storage, 0); + + *val =3D 1; + return 1; +} + char _license[] SEC("license") =3D "GPL"; --=20 2.51.2