From: Yosry Ahmed <yosryahmed@google.com>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
Yonghong Song <yhs@fb.com>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>, Hao Luo <haoluo@google.com>,
Tejun Heo <tj@kernel.org>, Zefan Li <lizefan.x@bytedance.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Shuah Khan <shuah@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Michal Hocko <mhocko@kernel.org>
Cc: Stanislav Fomichev <sdf@google.com>,
David Rientjes <rientjes@google.com>,
Greg Thelen <gthelen@google.com>,
Shakeel Butt <shakeelb@google.com>,
linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
bpf@vger.kernel.org, cgroups@vger.kernel.org,
Yosry Ahmed <yosryahmed@google.com>
Subject: [RFC PATCH bpf-next 5/9] bpf: add bpf_map_lookup_percpu_elem() helper
Date: Tue, 10 May 2022 00:18:03 +0000 [thread overview]
Message-ID: <20220510001807.4132027-6-yosryahmed@google.com> (raw)
In-Reply-To: <20220510001807.4132027-1-yosryahmed@google.com>
Add a helper for bpf programs to lookup a percpu element for a cpu other
than the current one. This is useful for rstat flusher programs as they
get called to aggregate stats from different cpus, regardless of the
current cpu.
Signed-off-by: Yosry Ahmed <yosryahmed@google.com>
---
include/linux/bpf.h | 2 ++
include/uapi/linux/bpf.h | 9 +++++++++
kernel/bpf/arraymap.c | 11 ++++++++---
kernel/bpf/hashtab.c | 25 +++++++++++--------------
kernel/bpf/helpers.c | 26 ++++++++++++++++++++++++++
kernel/bpf/verifier.c | 6 ++++++
tools/include/uapi/linux/bpf.h | 9 +++++++++
7 files changed, 71 insertions(+), 17 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index bdb5298735ce..f6fa35ffe311 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1665,6 +1665,8 @@ int map_set_for_each_callback_args(struct bpf_verifier_env *env,
struct bpf_func_state *caller,
struct bpf_func_state *callee);
+void *bpf_percpu_hash_lookup(struct bpf_map *map, void *key, int cpu);
+void *bpf_percpu_array_lookup(struct bpf_map *map, void *key, int cpu);
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_hash_update(struct bpf_map *map, void *key, void *value,
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index fce5535579d6..015ed402c642 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1553,6 +1553,14 @@ union bpf_attr {
* Map value associated to *key*, or **NULL** if no entry was
* found.
*
+ * void *bpf_map_lookup_percpu_elem(struct bpf_map *map, const void *key, int cpu)
+ * Description
+ * Perform a lookup in percpu *map* for an entry associated to
+ * *key* for the given *cpu*.
+ * Return
+ * Map value associated to *key* per *cpu*, or **NULL** if no entry
+ * was found.
+ *
* long bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u64 flags)
* Description
* Add or update the value of the entry associated to *key* in
@@ -5169,6 +5177,7 @@ union bpf_attr {
#define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \
FN(map_lookup_elem), \
+ FN(map_lookup_percpu_elem), \
FN(map_update_elem), \
FN(map_delete_elem), \
FN(probe_read), \
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
index 7f145aefbff8..945dae4c20eb 100644
--- a/kernel/bpf/arraymap.c
+++ b/kernel/bpf/arraymap.c
@@ -230,8 +230,7 @@ static int array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
return insn - insn_buf;
}
-/* Called from eBPF program */
-static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
+void *bpf_percpu_array_lookup(struct bpf_map *map, void *key, int cpu)
{
struct bpf_array *array = container_of(map, struct bpf_array, map);
u32 index = *(u32 *)key;
@@ -239,7 +238,13 @@ static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
if (unlikely(index >= array->map.max_entries))
return NULL;
- return this_cpu_ptr(array->pptrs[index & array->index_mask]);
+ return per_cpu_ptr(array->pptrs[index & array->index_mask], cpu);
+}
+
+/* Called from eBPF program */
+static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
+{
+ return bpf_percpu_array_lookup(map, key, smp_processor_id());
}
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index 65877967f414..c6d4699d65e8 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -2150,27 +2150,24 @@ const struct bpf_map_ops htab_lru_map_ops = {
.iter_seq_info = &iter_seq_info,
};
-/* Called from eBPF program */
-static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
+void *bpf_percpu_hash_lookup(struct bpf_map *map, void *key, int cpu)
{
+ struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
struct htab_elem *l = __htab_map_lookup_elem(map, key);
- if (l)
- return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
+ if (l) {
+ if (htab_is_lru(htab))
+ bpf_lru_node_set_ref(&l->lru_node);
+ 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 *key)
+/* Called from eBPF program */
+static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
{
- struct htab_elem *l = __htab_map_lookup_elem(map, key);
-
- if (l) {
- bpf_lru_node_set_ref(&l->lru_node);
- return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
- }
-
- return NULL;
+ return bpf_percpu_hash_lookup(map, key, smp_processor_id());
}
int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
@@ -2279,7 +2276,7 @@ const struct bpf_map_ops htab_lru_percpu_map_ops = {
.map_alloc = htab_map_alloc,
.map_free = htab_map_free,
.map_get_next_key = htab_map_get_next_key,
- .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
+ .map_lookup_elem = htab_percpu_map_lookup_elem,
.map_lookup_and_delete_elem = htab_lru_percpu_map_lookup_and_delete_elem,
.map_update_elem = htab_lru_percpu_map_update_elem,
.map_delete_elem = htab_lru_map_delete_elem,
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index d124eed97ad7..abed4e1737f6 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -45,6 +45,30 @@ const struct bpf_func_proto bpf_map_lookup_elem_proto = {
.arg2_type = ARG_PTR_TO_MAP_KEY,
};
+BPF_CALL_3(bpf_map_lookup_percpu_elem, struct bpf_map *, map, void *, key,
+ int, cpu)
+{
+ WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
+ switch (map->map_type) {
+ case BPF_MAP_TYPE_PERCPU_ARRAY:
+ return (unsigned long) bpf_percpu_array_lookup(map, key, cpu);
+ case BPF_MAP_TYPE_PERCPU_HASH:
+ case BPF_MAP_TYPE_LRU_PERCPU_HASH:
+ return (unsigned long) bpf_percpu_hash_lookup(map, key, cpu);
+ default:
+ return (unsigned long) NULL;
+ }
+}
+
+const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto = {
+ .func = bpf_map_lookup_percpu_elem,
+ .gpl_only = false,
+ .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
+ .arg1_type = ARG_CONST_MAP_PTR,
+ .arg2_type = ARG_PTR_TO_MAP_KEY,
+ .arg3_type = ARG_ANYTHING,
+};
+
BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
void *, value, u64, flags)
{
@@ -1414,6 +1438,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
switch (func_id) {
case BPF_FUNC_map_lookup_elem:
return &bpf_map_lookup_elem_proto;
+ case BPF_FUNC_map_lookup_percpu_elem:
+ return &bpf_map_lookup_percpu_elem_proto;
case BPF_FUNC_map_update_elem:
return &bpf_map_update_elem_proto;
case BPF_FUNC_map_delete_elem:
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index d175b70067b3..2d7f7c9a970d 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -5879,6 +5879,12 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
goto error;
break;
+ case BPF_FUNC_map_lookup_percpu_elem:
+ if (map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
+ map->map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH &&
+ map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY)
+ goto error;
+ break;
default:
break;
}
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index fce5535579d6..015ed402c642 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1553,6 +1553,14 @@ union bpf_attr {
* Map value associated to *key*, or **NULL** if no entry was
* found.
*
+ * void *bpf_map_lookup_percpu_elem(struct bpf_map *map, const void *key, int cpu)
+ * Description
+ * Perform a lookup in percpu *map* for an entry associated to
+ * *key* for the given *cpu*.
+ * Return
+ * Map value associated to *key* per *cpu*, or **NULL** if no entry
+ * was found.
+ *
* long bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u64 flags)
* Description
* Add or update the value of the entry associated to *key* in
@@ -5169,6 +5177,7 @@ union bpf_attr {
#define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \
FN(map_lookup_elem), \
+ FN(map_lookup_percpu_elem), \
FN(map_update_elem), \
FN(map_delete_elem), \
FN(probe_read), \
--
2.36.0.512.ge40c2bad7a-goog
next prev parent reply other threads:[~2022-05-10 0:19 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-10 0:17 [RFC PATCH bpf-next 0/9] bpf: cgroup hierarchical stats collection Yosry Ahmed
2022-05-10 0:17 ` [RFC PATCH bpf-next 1/9] bpf: introduce CGROUP_SUBSYS_RSTAT program type Yosry Ahmed
2022-05-10 18:07 ` Yosry Ahmed
2022-05-10 19:21 ` Yosry Ahmed
2022-05-10 18:44 ` Tejun Heo
2022-05-10 19:34 ` Yosry Ahmed
2022-05-10 19:59 ` Tejun Heo
2022-05-10 20:43 ` Yosry Ahmed
2022-05-10 21:01 ` Tejun Heo
2022-05-10 21:55 ` Yosry Ahmed
2022-05-10 22:09 ` Tejun Heo
2022-05-10 22:10 ` Yosry Ahmed
2022-05-10 0:18 ` [RFC PATCH bpf-next 2/9] cgroup: bpf: flush bpf stats on rstat flush Yosry Ahmed
2022-05-10 18:45 ` Tejun Heo
2022-05-10 0:18 ` [RFC PATCH bpf-next 3/9] libbpf: Add support for rstat progs and links Yosry Ahmed
2022-05-10 0:18 ` [RFC PATCH bpf-next 4/9] bpf: add bpf rstat helpers Yosry Ahmed
2022-05-10 0:18 ` Yosry Ahmed [this message]
2022-05-10 0:18 ` [RFC PATCH bpf-next 6/9] cgroup: add v1 support to cgroup_get_from_id() Yosry Ahmed
2022-05-10 18:33 ` Tejun Heo
2022-05-10 18:36 ` Yosry Ahmed
2022-05-10 0:18 ` [RFC PATCH bpf-next 7/9] cgroup: Add cgroup_put() in !CONFIG_CGROUPS case Yosry Ahmed
2022-05-10 18:25 ` Hao Luo
2022-05-10 0:18 ` [RFC PATCH bpf-next 8/9] bpf: Introduce cgroup iter Yosry Ahmed
2022-05-10 18:25 ` Hao Luo
2022-05-10 18:54 ` Tejun Heo
2022-05-10 21:12 ` Hao Luo
2022-05-10 22:07 ` Tejun Heo
2022-05-10 22:49 ` Hao Luo
2022-05-10 0:18 ` [RFC PATCH bpf-next 9/9] selftest/bpf: add a selftest for cgroup hierarchical stats Yosry Ahmed
2022-05-13 7:16 ` [RFC PATCH bpf-next 0/9] bpf: cgroup hierarchical stats collection Yosry Ahmed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220510001807.4132027-6-yosryahmed@google.com \
--to=yosryahmed@google.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=gthelen@google.com \
--cc=hannes@cmpxchg.org \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lizefan.x@bytedance.com \
--cc=mhocko@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=sdf@google.com \
--cc=shakeelb@google.com \
--cc=shuah@kernel.org \
--cc=songliubraving@fb.com \
--cc=tj@kernel.org \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).