From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-173.mta0.migadu.com (out-173.mta0.migadu.com [91.218.175.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 24C3A219A8A for ; Tue, 9 Sep 2025 14:14:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757427293; cv=none; b=LulZH+4Mx+yTnuuuxKyZDbjjblsLhh7cu5qDVA1xmh3QgLhsUev1YhPhgkwQQOn8eDN+gU9Ys3u/wfsMVrUzXmWHwdIDZU6+t+VmbLzVQe+gy42YZlmcYR7WkD/fXzf6qEqvcth9gW2SRTDGabrq0BzWSahGbBCyEBc7adq5oH4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757427293; c=relaxed/simple; bh=mdH6S6VvtnlqtU+A9ksVquBGOXd5mfxhBTLYG+XK5A0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r9DGJx3JKGLXy73Db4PjpyFAeazlZVUWwRwDpg5VjgHx7B5jVjfCYPEMRv4tat8LxTrnXPmCr5Gb83iOQupd2PSXf74Y35UEmZo7Z/NKKN6tqKHL9bd+kwmwpSzHm91GqgT4S+zNc96LjX5FXX/06PkifhTa4PhDXtOklM/Z9E8= 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=W9zID2Vy; arc=none smtp.client-ip=91.218.175.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="W9zID2Vy" 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=1757427288; 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=8yry49zEfNOEZsUNbOweZ9wayQJrzHevYaTRuzSfhzw=; b=W9zID2Vy2Q7NvKuhaWTpe0feKEvvlN1dgJXazVNemzHiu3Tg9uWgBo3zwQ8V5ELmR++O/H GWqpjW6n4M/DmDelNU//HT+P593ftDOxoGtxaavc1f5j+fYwJF0N3CMyxGfjMQhwAXbwDd 8XUxt/mv4akmhqtkBJ6wbXNLNboO49o= 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, leon.hwang@linux.dev, kernel-patches-bot@fb.com Subject: [PATCH bpf-next v6 3/7] bpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu_array maps Date: Tue, 9 Sep 2025 22:14:17 +0800 Message-ID: <20250909141422.45450-4-leon.hwang@linux.dev> In-Reply-To: <20250909141422.45450-1-leon.hwang@linux.dev> References: <20250909141422.45450-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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 --- include/linux/bpf.h | 2 +- kernel/bpf/arraymap.c | 36 +++++++++++++++++++++++++++++------- kernel/bpf/syscall.c | 9 +++++++-- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 60c235836987d..b5143420ea947 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2697,7 +2697,7 @@ int map_set_for_each_callback_args(struct bpf_verifier_env *env, struct bpf_func_state *callee); 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, diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index 3d080916faf97..aa5c17c707f1c 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c @@ -295,14 +295,18 @@ 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); } -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 = container_of(map, struct bpf_array, map); u32 index = *(u32 *)key; + int err, cpu, off = 0; void __percpu *pptr; - int cpu, off = 0; u32 size; + err = bpf_map_check_cpu_flags(map_flags, false); + if (unlikely(err)) + return err; + if (unlikely(index >= array->map.max_entries)) return -ENOENT; @@ -313,11 +317,18 @@ int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value) size = array->elem_size; rcu_read_lock(); pptr = array->pptrs[index & array->index_mask]; + if (map_flags & BPF_F_CPU) { + cpu = map_flags >> 32; + copy_map_value_long(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 += size; } +unlock: rcu_read_unlock(); return 0; } @@ -386,13 +397,13 @@ int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value, { struct bpf_array *array = container_of(map, struct bpf_array, map); u32 index = *(u32 *)key; + int err, cpu, off = 0; void __percpu *pptr; - int cpu, off = 0; u32 size; - if (unlikely(map_flags > BPF_EXIST)) - /* unknown flags */ - return -EINVAL; + err = bpf_map_check_cpu_flags(map_flags, true); + if (unlikely(err)) + return err; if (unlikely(index >= array->map.max_entries)) /* all elements were pre-allocated, cannot insert a new one */ @@ -411,11 +422,22 @@ int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value, size = array->elem_size; rcu_read_lock(); pptr = array->pptrs[index & array->index_mask]; + if (map_flags & BPF_F_CPU) { + cpu = map_flags >> 32; + copy_map_value_long(map, per_cpu_ptr(pptr, cpu), value); + bpf_obj_free_fields(array->map.record, per_cpu_ptr(pptr, cpu)); + 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 += size; + /* same user-provided value is used if BPF_F_ALL_CPUS is + * specified, otherwise value is an array of per-CPU values. + */ + if (!(map_flags & BPF_F_ALL_CPUS)) + off += size; } +unlock: rcu_read_unlock(); return 0; } diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index db841b38f0c22..6d0a08d63f175 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -133,7 +133,12 @@ bool bpf_map_write_active(const struct bpf_map *map) static 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; + } } static int bpf_map_check_op_flags(struct bpf_map *map, u64 flags, u64 allowed_flags) @@ -338,7 +343,7 @@ static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value, map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { err = bpf_percpu_hash_copy(map, key, value); } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { - err = bpf_percpu_array_copy(map, key, value); + err = bpf_percpu_array_copy(map, key, value, flags); } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { err = bpf_percpu_cgroup_storage_copy(map, key, value); } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { -- 2.50.1