From: "Ho-Ren (Jack) Chuang" <horenchuang@bytedance.com>
To: Alexei Starovoitov <ast@kernel.org>,
Alexei Starovoitov <alexei.starovoitov@gmail.com>,
Hao Luo <haoluo@google.com>, Jiri Olsa <jolsa@kernel.org>,
Jiri Olsa <olsajiri@gmail.com>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
John Fastabend <john.fastabend@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>, Yonghong Song <yhs@fb.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@google.com>,
Quentin Monnet <quentin@isovalent.com>,
Mykola Lysenko <mykolal@fb.com>, Shuah Khan <shuah@kernel.org>,
Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Tom Rix <trix@redhat.com>, Joanne Koong <joannelkoong@gmail.com>,
Kui-Feng Lee <kuifeng@fb.com>,
Lorenzo Bianconi <lorenzo@kernel.org>,
Maxim Mikityanskiy <maximmi@nvidia.com>,
Hao Xiang <hao.xiang@bytedance.com>,
Punit Agrawal <punit.agrawal@bytedance.com>,
Yifei Ma <yifeima@bytedance.com>,
Xiaoning Ding <xiaoning.ding@bytedance.com>,
bpf@vger.kernel.org
Cc: Ho-Ren Chuang <horenc@vt.edu>,
Ho-Ren Chuang <horenchuang@bytedance.com>,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
llvm@lists.linux.dev
Subject: [PATCH bpf-next v1 1/4] bpf: Support reporting BPF htab map's used size for monitoring
Date: Sat, 5 Nov 2022 02:51:43 +0000 [thread overview]
Message-ID: <20221105025146.238209-2-horenchuang@bytedance.com> (raw)
In-Reply-To: <20221105025146.238209-1-horenchuang@bytedance.com>
Expose BPF htab map's used size by counting accessed or allocated/freed
elements to userspace.
Leverage the htab->count value for both preallocated and
dynamically allocated maps. Expose the value to a new field
"used_entries" in a userspace struct bpf_map_info to allow monitoring.
Support hash table type (BPF_MAP_TYPE_HASH).
Signed-off-by: Ho-Ren (Jack) Chuang <horenchuang@bytedance.com>
---
include/linux/bpf.h | 1 +
include/uapi/linux/bpf.h | 1 +
kernel/bpf/hashtab.c | 19 +++++++++++++++++++
kernel/bpf/syscall.c | 2 ++
4 files changed, 23 insertions(+)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 9e7d46d16032..82ee14139b69 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -97,6 +97,7 @@ struct bpf_map_ops {
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);
+ u32 (*map_get_used_elem)(struct bpf_map *map);
/* funcs called by prog_array and perf_event_array map */
void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 17f61338f8f8..63659368cf0e 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -6215,6 +6215,7 @@ struct bpf_map_info {
__u32 id;
__u32 key_size;
__u32 value_size;
+ __u32 used_entries;
__u32 max_entries;
__u32 map_flags;
char name[BPF_OBJ_NAME_LEN];
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index ed3f8a53603b..bc9c00b92e57 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -913,6 +913,7 @@ static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
if (htab_is_prealloc(htab)) {
check_and_free_fields(htab, l);
__pcpu_freelist_push(&htab->freelist, &l->fnode);
+ dec_elem_count(htab);
} else {
dec_elem_count(htab);
htab_elem_free(htab, l);
@@ -994,6 +995,7 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
if (!l)
return ERR_PTR(-E2BIG);
l_new = container_of(l, struct htab_elem, fnode);
+ inc_elem_count(htab);
}
} else {
if (is_map_full(htab))
@@ -2186,6 +2188,22 @@ static int bpf_for_each_hash_elem(struct bpf_map *map, bpf_callback_t callback_f
return num_elems;
}
+u32 htab_map_get_used_elem(struct bpf_map *map)
+{
+ struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
+
+ /* The elem count may temporarily go beyond the max after
+ * inc_elem_count() but before dec_elem_count().
+ */
+ if (htab->use_percpu_counter)
+ return min_t(u32, htab->map.max_entries,
+ percpu_counter_sum(&htab->pcount) +
+ atomic_read(&htab->count));
+ else
+ return min_t(u32, htab->map.max_entries,
+ atomic_read(&htab->count));
+}
+
BTF_ID_LIST_SINGLE(htab_map_btf_ids, struct, bpf_htab)
const struct bpf_map_ops htab_map_ops = {
.map_meta_equal = bpf_map_meta_equal,
@@ -2202,6 +2220,7 @@ const struct bpf_map_ops htab_map_ops = {
.map_seq_show_elem = htab_map_seq_show_elem,
.map_set_for_each_callback_args = map_set_for_each_callback_args,
.map_for_each_callback = bpf_for_each_hash_elem,
+ .map_get_used_elem = htab_map_get_used_elem,
BATCH_OPS(htab),
.map_btf_id = &htab_map_btf_ids[0],
.iter_seq_info = &iter_seq_info,
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 7b373a5e861f..ea4828bb22ac 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -4203,6 +4203,8 @@ static int bpf_map_get_info_by_fd(struct file *file,
info.map_flags = map->map_flags;
info.map_extra = map->map_extra;
memcpy(info.name, map->name, sizeof(map->name));
+ if (map->ops->map_get_used_elem)
+ info.used_entries = map->ops->map_get_used_elem(map);
if (map->btf) {
info.btf_id = btf_obj_id(map->btf);
--
Ho-Ren (Jack) Chuang
next prev parent reply other threads:[~2022-11-05 2:52 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-05 2:51 [PATCH bpf-next v1 0/4] Add " Ho-Ren (Jack) Chuang
2022-11-05 2:51 ` Ho-Ren (Jack) Chuang [this message]
2022-11-05 4:14 ` [PATCH bpf-next v1 1/4] bpf: Support reporting " kernel test robot
2022-11-05 2:51 ` [PATCH bpf-next v1 2/4] bpftool: Add tools support to show BPF htab map's used size Ho-Ren (Jack) Chuang
2022-11-05 2:51 ` [PATCH bpf-next v1 3/4] samples/bpf: Add concurrency testing for " Ho-Ren (Jack) Chuang
2022-11-05 2:51 ` [PATCH bpf-next v1 4/4] selftests/bpf: Add unit tests " Ho-Ren (Jack) Chuang
2022-11-05 16:20 ` [PATCH bpf-next v1 0/4] Add BPF htab map's used size for monitoring Alexei Starovoitov
2022-11-08 0:30 ` [External] " Hao Xiang .
2022-11-28 23:03 ` Hao Xiang .
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=20221105025146.238209-2-horenchuang@bytedance.com \
--to=horenchuang@bytedance.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=hao.xiang@bytedance.com \
--cc=haoluo@google.com \
--cc=horenc@vt.edu \
--cc=joannelkoong@gmail.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=kuifeng@fb.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=lorenzo@kernel.org \
--cc=martin.lau@linux.dev \
--cc=maximmi@nvidia.com \
--cc=mykolal@fb.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=olsajiri@gmail.com \
--cc=punit.agrawal@bytedance.com \
--cc=quentin@isovalent.com \
--cc=sdf@google.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=trix@redhat.com \
--cc=xiaoning.ding@bytedance.com \
--cc=yhs@fb.com \
--cc=yifeima@bytedance.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
all inboxes | Powered by JetHome®