From nobody Sun Jun 14 21:11:14 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC933C433F5 for ; Wed, 11 May 2022 09:43:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243814AbiEKJmE (ORCPT ); Wed, 11 May 2022 05:42:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242183AbiEKJju (ORCPT ); Wed, 11 May 2022 05:39:50 -0400 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4C818CB0D for ; Wed, 11 May 2022 02:39:34 -0700 (PDT) Received: by mail-pg1-x532.google.com with SMTP id s16so1329332pgs.3 for ; Wed, 11 May 2022 02:39:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SFmfnA2AbiUjenaFSQFfeVxUim1UStEwsd2Nq7u7lQc=; b=5pxZakyesz+ihTSJOSgKjwLeA9fDnvpNWUj2CFaPty5NiNw/Zu5C82YzYnzmyx9f+9 dw65j8B5x+itrtuSqqV3EawpZl7bQ5yNRyXFO5KtaMo3IsYVZkBlSmWVGsNqj1TfixzB vfWhJCXcOE/b0bb0CO6gglCkrIhvINnEMPyz9yBhRXwMGFGOPKYwdGw7cEeyE/ufg54q BFn8VLj9SAHoCQXT5PSixvasV1ShMoElSmL7uUM4gne+a4eFvPtLpVcvVf3fbFLcg6PV lZKBOqPg8Esp4G7HprNZs4DM0puzKNfGoRwIsaddHGNDyfa6J8u8n5NPMzurkmrhhw5M gYzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SFmfnA2AbiUjenaFSQFfeVxUim1UStEwsd2Nq7u7lQc=; b=vDDg71lydoU5pfkhJPwjkUk2aGr9WOS/4W87O9DvksWETZCg6zVbsDoW+Hpkl3a0Jr OO8zGzbwP/mDSq7oAmw2P48o8nPOEv+gTeBYB06X9ArFaNRmBEwpHEYHL6qCdO8E/neO C2u/YNtcEvJOEoxtoenF3QhqDLP9HxSjBE+YSOQNo8HuTP6qSufe27f+u8zOLHuSf+NB uZxV+/hkhiPO88JtvgNxoss2y6lp72O8R3v5b53WXMYSWxb5Xv3o4hktw1clLy7KhSpl NdYd/fG99BhqQSM9LrUaJ4Y4UlG+4i1Z1XT2nMxupqiikg+0jZgGABSUESD/0VjBEGa+ v8qw== X-Gm-Message-State: AOAM530s+q6xnfiSxWyfcE3K3CsogBwulZQdqtu6WHIbATFcgWUoRq0s HSNBVOqJWXEZ4WiEjypV5hMMtA== X-Google-Smtp-Source: ABdhPJwW3IGSCcrYJ/1XbEHBYJqxzOoyiRU8vLFG1M+bzstHNRJm7gDlyW4hqc0W6L0ncrLYo5yuPg== X-Received: by 2002:a63:4459:0:b0:39d:3808:7c84 with SMTP id t25-20020a634459000000b0039d38087c84mr20180990pgk.130.1652261973589; Wed, 11 May 2022 02:39:33 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.241]) by smtp.gmail.com with ESMTPSA id k4-20020aa790c4000000b0050dc76281cdsm1159834pfk.167.2022.05.11.02.39.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 May 2022 02:39:33 -0700 (PDT) From: Feng zhou To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, rostedt@goodmis.org, mingo@redhat.com, jolsa@kernel.org, davemarchevsky@fb.com, joannekoong@fb.com, geliang.tang@suse.com Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, songmuchun@bytedance.com, wangdongdong.6@bytedance.com, cong.wang@bytedance.com, zhouchengming@bytedance.com, zhoufeng.zf@bytedance.com, yosryahmed@google.com Subject: [PATCH bpf-next v2 1/2] bpf: add bpf_map_lookup_percpu_elem for percpu map Date: Wed, 11 May 2022 17:38:53 +0800 Message-Id: <20220511093854.411-2-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20220511093854.411-1-zhoufeng.zf@bytedance.com> References: <20220511093854.411-1-zhoufeng.zf@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Feng Zhou Add new ebpf helpers bpf_map_lookup_percpu_elem. The implementation method is relatively simple, refer to the implementation method of map_lookup_elem of percpu map, increase the parameters of cpu, and obtain it according to the specified cpu. Signed-off-by: Feng Zhou --- include/linux/bpf.h | 2 ++ include/uapi/linux/bpf.h | 9 +++++++++ kernel/bpf/arraymap.c | 15 +++++++++++++++ kernel/bpf/core.c | 1 + kernel/bpf/hashtab.c | 32 ++++++++++++++++++++++++++++++++ kernel/bpf/helpers.c | 18 ++++++++++++++++++ kernel/bpf/verifier.c | 17 +++++++++++++++-- kernel/trace/bpf_trace.c | 2 ++ tools/include/uapi/linux/bpf.h | 9 +++++++++ 9 files changed, 103 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index aba7ded56436..be7fa3afa353 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -89,6 +89,7 @@ struct bpf_map_ops { int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags); int (*map_pop_elem)(struct bpf_map *map, void *value); int (*map_peek_elem)(struct bpf_map *map, void *value); + void *(*map_lookup_percpu_elem)(struct bpf_map *map, void *key, u32 cpu); =20 /* funcs called by prog_array and perf_event_array map */ void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file, @@ -2184,6 +2185,7 @@ extern const struct bpf_func_proto bpf_map_delete_ele= m_proto; extern const struct bpf_func_proto bpf_map_push_elem_proto; extern const struct bpf_func_proto bpf_map_pop_elem_proto; extern const struct bpf_func_proto bpf_map_peek_elem_proto; +extern const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto; =20 extern const struct bpf_func_proto bpf_get_prandom_u32_proto; extern const struct bpf_func_proto bpf_get_smp_processor_id_proto; diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index bc7f89948f54..0210f85131b3 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5164,6 +5164,14 @@ union bpf_attr { * if not NULL, is a reference which must be released using its * corresponding release function, or moved into a BPF map before * program exit. + * + * void *bpf_map_lookup_percpu_elem(struct bpf_map *map, const void *key, = u32 cpu) + * Description + * Perform a lookup in *percpu map* for an entry associated to + * *key* on *cpu*. + * Return + * Map value associated to *key* on *cpu*, or **NULL** if no entry + * was found or *cpu* is invalid. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -5361,6 +5369,7 @@ union bpf_attr { FN(skb_set_tstamp), \ FN(ima_file_hash), \ FN(kptr_xchg), \ + FN(map_lookup_percpu_elem), \ /* */ =20 /* integer value in 'imm' field of BPF_CALL instruction selects which help= er diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index 724613da6576..fe40d3b9458f 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c @@ -243,6 +243,20 @@ static void *percpu_array_map_lookup_elem(struct bpf_m= ap *map, void *key) return this_cpu_ptr(array->pptrs[index & array->index_mask]); } =20 +static void *percpu_array_map_lookup_percpu_elem(struct bpf_map *map, void= *key, u32 cpu) +{ + struct bpf_array *array =3D container_of(map, struct bpf_array, map); + u32 index =3D *(u32 *)key; + + if (cpu >=3D nr_cpu_ids) + return NULL; + + if (unlikely(index >=3D array->map.max_entries)) + return NULL; + + return per_cpu_ptr(array->pptrs[index & array->index_mask], cpu); +} + int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value) { struct bpf_array *array =3D container_of(map, struct bpf_array, map); @@ -725,6 +739,7 @@ const struct bpf_map_ops percpu_array_map_ops =3D { .map_lookup_elem =3D percpu_array_map_lookup_elem, .map_update_elem =3D array_map_update_elem, .map_delete_elem =3D array_map_delete_elem, + .map_lookup_percpu_elem =3D percpu_array_map_lookup_percpu_elem, .map_seq_show_elem =3D percpu_array_map_seq_show_elem, .map_check_btf =3D array_map_check_btf, .map_lookup_batch =3D generic_map_lookup_batch, diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 13e9dbeeedf3..76f68d0a7ae8 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2619,6 +2619,7 @@ const struct bpf_func_proto bpf_map_delete_elem_proto= __weak; const struct bpf_func_proto bpf_map_push_elem_proto __weak; const struct bpf_func_proto bpf_map_pop_elem_proto __weak; const struct bpf_func_proto bpf_map_peek_elem_proto __weak; +const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto __weak; const struct bpf_func_proto bpf_spin_lock_proto __weak; const struct bpf_func_proto bpf_spin_unlock_proto __weak; const struct bpf_func_proto bpf_jiffies64_proto __weak; diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 705841279d16..17fb69c0e0dc 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -2199,6 +2199,20 @@ static void *htab_percpu_map_lookup_elem(struct bpf_= map *map, void *key) return NULL; } =20 +static void *htab_percpu_map_lookup_percpu_elem(struct bpf_map *map, void = *key, u32 cpu) +{ + struct htab_elem *l; + + if (cpu >=3D nr_cpu_ids) + return NULL; + + l =3D __htab_map_lookup_elem(map, key); + if (l) + return per_cpu_ptr(htab_elem_get_ptr(l, map->key_size), cpu); + else + return NULL; +} + static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *ke= y) { struct htab_elem *l =3D __htab_map_lookup_elem(map, key); @@ -2211,6 +2225,22 @@ static void *htab_lru_percpu_map_lookup_elem(struct = bpf_map *map, void *key) return NULL; } =20 +static void *htab_lru_percpu_map_lookup_percpu_elem(struct bpf_map *map, v= oid *key, u32 cpu) +{ + struct htab_elem *l; + + if (cpu >=3D nr_cpu_ids) + return NULL; + + l =3D __htab_map_lookup_elem(map, key); + if (l) { + bpf_lru_node_set_ref(&l->lru_node); + return per_cpu_ptr(htab_elem_get_ptr(l, map->key_size), cpu); + } + + return NULL; +} + int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value) { struct htab_elem *l; @@ -2300,6 +2330,7 @@ const struct bpf_map_ops htab_percpu_map_ops =3D { .map_lookup_and_delete_elem =3D htab_percpu_map_lookup_and_delete_elem, .map_update_elem =3D htab_percpu_map_update_elem, .map_delete_elem =3D htab_map_delete_elem, + .map_lookup_percpu_elem =3D htab_percpu_map_lookup_percpu_elem, .map_seq_show_elem =3D htab_percpu_map_seq_show_elem, .map_set_for_each_callback_args =3D map_set_for_each_callback_args, .map_for_each_callback =3D bpf_for_each_hash_elem, @@ -2318,6 +2349,7 @@ const struct bpf_map_ops htab_lru_percpu_map_ops =3D { .map_lookup_and_delete_elem =3D htab_lru_percpu_map_lookup_and_delete_ele= m, .map_update_elem =3D htab_lru_percpu_map_update_elem, .map_delete_elem =3D htab_lru_map_delete_elem, + .map_lookup_percpu_elem =3D htab_lru_percpu_map_lookup_percpu_elem, .map_seq_show_elem =3D htab_percpu_map_seq_show_elem, .map_set_for_each_callback_args =3D map_set_for_each_callback_args, .map_for_each_callback =3D bpf_for_each_hash_elem, diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 3e709fed5306..d5f104a39092 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -119,6 +119,22 @@ const struct bpf_func_proto bpf_map_peek_elem_proto = =3D { .arg2_type =3D ARG_PTR_TO_UNINIT_MAP_VALUE, }; =20 +BPF_CALL_3(bpf_map_lookup_percpu_elem, struct bpf_map *, map, void *, key,= u32, cpu) +{ + WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held()); + return (unsigned long) map->ops->map_lookup_percpu_elem(map, key, cpu); +} + +const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto =3D { + .func =3D bpf_map_lookup_percpu_elem, + .gpl_only =3D false, + .pkt_access =3D true, + .ret_type =3D RET_PTR_TO_MAP_VALUE_OR_NULL, + .arg1_type =3D ARG_CONST_MAP_PTR, + .arg2_type =3D ARG_PTR_TO_MAP_KEY, + .arg3_type =3D ARG_ANYTHING, +}; + const struct bpf_func_proto bpf_get_prandom_u32_proto =3D { .func =3D bpf_user_rnd_u32, .gpl_only =3D false, @@ -1420,6 +1436,8 @@ bpf_base_func_proto(enum bpf_func_id func_id) return &bpf_map_pop_elem_proto; case BPF_FUNC_map_peek_elem: return &bpf_map_peek_elem_proto; + case BPF_FUNC_map_lookup_percpu_elem: + return &bpf_map_lookup_percpu_elem_proto; case BPF_FUNC_get_prandom_u32: return &bpf_get_prandom_u32_proto; case BPF_FUNC_get_smp_processor_id: diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index c27fee73a2cb..05c1b6656824 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -6137,6 +6137,12 @@ static int check_map_func_compatibility(struct bpf_v= erifier_env *env, map->map_type !=3D BPF_MAP_TYPE_BLOOM_FILTER) goto error; break; + case BPF_FUNC_map_lookup_percpu_elem: + if (map->map_type !=3D BPF_MAP_TYPE_PERCPU_ARRAY && + map->map_type !=3D BPF_MAP_TYPE_PERCPU_HASH && + map->map_type !=3D BPF_MAP_TYPE_LRU_PERCPU_HASH) + goto error; + break; case BPF_FUNC_sk_storage_get: case BPF_FUNC_sk_storage_delete: if (map->map_type !=3D BPF_MAP_TYPE_SK_STORAGE) @@ -6750,7 +6756,8 @@ record_func_map(struct bpf_verifier_env *env, struct = bpf_call_arg_meta *meta, func_id !=3D BPF_FUNC_map_pop_elem && func_id !=3D BPF_FUNC_map_peek_elem && func_id !=3D BPF_FUNC_for_each_map_elem && - func_id !=3D BPF_FUNC_redirect_map) + func_id !=3D BPF_FUNC_redirect_map && + func_id !=3D BPF_FUNC_map_lookup_percpu_elem) return 0; =20 if (map =3D=3D NULL) { @@ -13810,7 +13817,8 @@ static int do_misc_fixups(struct bpf_verifier_env *= env) insn->imm =3D=3D BPF_FUNC_map_pop_elem || insn->imm =3D=3D BPF_FUNC_map_peek_elem || insn->imm =3D=3D BPF_FUNC_redirect_map || - insn->imm =3D=3D BPF_FUNC_for_each_map_elem)) { + insn->imm =3D=3D BPF_FUNC_for_each_map_elem || + insn->imm =3D=3D BPF_FUNC_map_lookup_percpu_elem)) { aux =3D &env->insn_aux_data[i + delta]; if (bpf_map_ptr_poisoned(aux)) goto patch_call_imm; @@ -13859,6 +13867,8 @@ static int do_misc_fixups(struct bpf_verifier_env *= env) bpf_callback_t callback_fn, void *callback_ctx, u64 flags))NULL)); + BUILD_BUG_ON(!__same_type(ops->map_lookup_percpu_elem, + (void *(*)(struct bpf_map *map, void *key, u32 cpu))NULL)); =20 patch_map_ops_generic: switch (insn->imm) { @@ -13886,6 +13896,9 @@ static int do_misc_fixups(struct bpf_verifier_env *= env) case BPF_FUNC_for_each_map_elem: insn->imm =3D BPF_CALL_IMM(ops->map_for_each_callback); continue; + case BPF_FUNC_map_lookup_percpu_elem: + insn->imm =3D BPF_CALL_IMM(ops->map_lookup_percpu_elem); + continue; } =20 goto patch_call_imm; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 2eaac094caf8..7141ca8a1c2d 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1197,6 +1197,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, cons= t struct bpf_prog *prog) return &bpf_map_pop_elem_proto; case BPF_FUNC_map_peek_elem: return &bpf_map_peek_elem_proto; + case BPF_FUNC_map_lookup_percpu_elem: + return &bpf_map_lookup_percpu_elem_proto; case BPF_FUNC_ktime_get_ns: return &bpf_ktime_get_ns_proto; case BPF_FUNC_ktime_get_boot_ns: diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index bc7f89948f54..0210f85131b3 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -5164,6 +5164,14 @@ union bpf_attr { * if not NULL, is a reference which must be released using its * corresponding release function, or moved into a BPF map before * program exit. + * + * void *bpf_map_lookup_percpu_elem(struct bpf_map *map, const void *key, = u32 cpu) + * Description + * Perform a lookup in *percpu map* for an entry associated to + * *key* on *cpu*. + * Return + * Map value associated to *key* on *cpu*, or **NULL** if no entry + * was found or *cpu* is invalid. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -5361,6 +5369,7 @@ union bpf_attr { FN(skb_set_tstamp), \ FN(ima_file_hash), \ FN(kptr_xchg), \ + FN(map_lookup_percpu_elem), \ /* */ =20 /* integer value in 'imm' field of BPF_CALL instruction selects which help= er --=20 2.20.1 From nobody Sun Jun 14 21:11:14 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0CD55C4332F for ; Wed, 11 May 2022 09:43:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235048AbiEKJmQ (ORCPT ); Wed, 11 May 2022 05:42:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232963AbiEKJkQ (ORCPT ); Wed, 11 May 2022 05:40:16 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C86B0994EC for ; Wed, 11 May 2022 02:39:45 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id p12so1548312pfn.0 for ; Wed, 11 May 2022 02:39:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/fUBFqLThKL0/oaZ4altcxEM2SUwyrO5ldQGIQjCN9A=; b=eCKuPbS3/VRMucuQKW3fV3J1Ax7JRz48UTyPqJ9d4QLXXrpHG/NK7WY8d+A+AM8hUp Z4WF3/AniPqFxeK7V7vGG0KrAQOCPyoLla6A1/xTMjxb72DJ+iiSG8IAdYNvzyFhb2zz f9UhYYkuakC1b6i/a/7nPdTssHoc63E0sv6onRn2fTnILoEc7Rs1Sutf09ywHPeXbcqL ErX0tg0HrFzZqIpEd44dcclLS5hYBENT77DOYHQ2l9HGeaSAsor+OqzpHZFKISFSHQ1u EwFScclZw++t3uWQxi2MjTgm82PAnmpAlaEAePYCgZYHtgbGezKLQ7hsXlgLaKg7L30Y m2MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/fUBFqLThKL0/oaZ4altcxEM2SUwyrO5ldQGIQjCN9A=; b=xerbrjhqz5bzMKLELAUoqQksw8TXZKk2HYSmz70EqZlA8XnaPjZVQFJoxKp09mglQr 1U9ZwSto8BkR3KYdE4leNVAShWybwEgFYp5Pt6RtUW2nHOcm7XSSgmwsNC+aIvocLdb2 n60KMhpkooGmoMYZ/t4hUFE1CgdKeR6I6hXxUTBe82hevTWYNpbrYYGWIu9U6OTeoMa8 3SdGWGlHquptA5nwu8h9Fvq/LrbODBDc81zbsYhK1pVX/XZbRJO7y81DmqU7r/HNUm4i 6AEaI1kcPxN6hPLleLF32/UJH0Q6MCLPVS/T4iuLbBGiytGssXPFaeBP9cuKW1jVFs48 ZaTQ== X-Gm-Message-State: AOAM5338Ctd2lzQWI8wa27n4dPP2LvpynWBmiYDt5jUv2/KbIi6ez7RF EYVlEGM91A920FoKjai8FJEg6Q== X-Google-Smtp-Source: ABdhPJwDMbRMxkaPKPPiBYE8YkF1nY3WaIESUDRv3AovB82URlFnnq3Oon+lU4rIHVhBZMZRCGVZmw== X-Received: by 2002:a63:6904:0:b0:3c6:5a3c:64bd with SMTP id e4-20020a636904000000b003c65a3c64bdmr3032831pgc.371.1652261985323; Wed, 11 May 2022 02:39:45 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.241]) by smtp.gmail.com with ESMTPSA id k4-20020aa790c4000000b0050dc76281cdsm1159834pfk.167.2022.05.11.02.39.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 May 2022 02:39:45 -0700 (PDT) From: Feng zhou To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, rostedt@goodmis.org, mingo@redhat.com, jolsa@kernel.org, davemarchevsky@fb.com, joannekoong@fb.com, geliang.tang@suse.com Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, songmuchun@bytedance.com, wangdongdong.6@bytedance.com, cong.wang@bytedance.com, zhouchengming@bytedance.com, zhoufeng.zf@bytedance.com, yosryahmed@google.com Subject: [PATCH bpf-next v2 2/2] selftests/bpf: add test case for bpf_map_lookup_percpu_elem Date: Wed, 11 May 2022 17:38:54 +0800 Message-Id: <20220511093854.411-3-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20220511093854.411-1-zhoufeng.zf@bytedance.com> References: <20220511093854.411-1-zhoufeng.zf@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Feng Zhou test_progs: Tests new ebpf helpers bpf_map_lookup_percpu_elem. Signed-off-by: Feng Zhou --- .../bpf/prog_tests/map_lookup_percpu_elem.c | 46 ++++++++++++++++ .../bpf/progs/test_map_lookup_percpu_elem.c | 54 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/map_lookup_percp= u_elem.c create mode 100644 tools/testing/selftests/bpf/progs/test_map_lookup_percp= u_elem.c diff --git a/tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.= c b/tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.c new file mode 100644 index 000000000000..58b24c2112b0 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.c @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2022 Bytedance + +#include + +#include "test_map_lookup_percpu_elem.skel.h" + +#define TEST_VALUE 1 + +void test_map_lookup_percpu_elem(void) +{ + struct test_map_lookup_percpu_elem *skel; + int key =3D 0, ret; + int nr_cpus =3D sysconf(_SC_NPROCESSORS_ONLN); + int *buf; + + buf =3D (int *)malloc(nr_cpus*sizeof(int)); + if (!ASSERT_OK_PTR(buf, "malloc")) + return; + memset(buf, 0, nr_cpus*sizeof(int)); + buf[0] =3D TEST_VALUE; + + skel =3D test_map_lookup_percpu_elem__open_and_load(); + if (!ASSERT_OK_PTR(skel, "test_map_lookup_percpu_elem__open_and_load")) + return; + ret =3D test_map_lookup_percpu_elem__attach(skel); + ASSERT_OK(ret, "test_map_lookup_percpu_elem__attach"); + + ret =3D bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_array_map), &ke= y, buf, 0); + ASSERT_OK(ret, "percpu_array_map update"); + + ret =3D bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_hash_map), &key= , buf, 0); + ASSERT_OK(ret, "percpu_hash_map update"); + + ret =3D bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_lru_hash_map), = &key, buf, 0); + ASSERT_OK(ret, "percpu_lru_hash_map update"); + + syscall(__NR_getuid); + + ret =3D skel->bss->percpu_array_elem_val =3D=3D TEST_VALUE && + skel->bss->percpu_hash_elem_val =3D=3D TEST_VALUE && + skel->bss->percpu_lru_hash_elem_val =3D=3D TEST_VALUE; + ASSERT_OK(!ret, "bpf_map_lookup_percpu_elem success"); + + test_map_lookup_percpu_elem__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.= c b/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c new file mode 100644 index 000000000000..5d4ef86cbf48 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2022 Bytedance + +#include "vmlinux.h" +#include + +int percpu_array_elem_val =3D 0; +int percpu_hash_elem_val =3D 0; +int percpu_lru_hash_elem_val =3D 0; + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u32); +} percpu_array_map SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_HASH); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u32); +} percpu_hash_map SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u32); +} percpu_lru_hash_map SEC(".maps"); + +SEC("tp/syscalls/sys_enter_getuid") +int sysenter_getuid(const void *ctx) +{ + __u32 key =3D 0; + __u32 cpu =3D 0; + __u32 *value; + + value =3D bpf_map_lookup_percpu_elem(&percpu_array_map, &key, cpu); + if (value) + percpu_array_elem_val =3D *value; + + value =3D bpf_map_lookup_percpu_elem(&percpu_hash_map, &key, cpu); + if (value) + percpu_hash_elem_val =3D *value; + + value =3D bpf_map_lookup_percpu_elem(&percpu_lru_hash_map, &key, cpu); + if (value) + percpu_lru_hash_elem_val =3D *value; + + return 0; +} + +char _license[] SEC("license") =3D "GPL"; --=20 2.20.1