From: Yafang Shao <laoar.shao@gmail.com>
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,
akpm@linux-foundation.org, cl@linux.com, penberg@kernel.org,
rientjes@google.com, iamjoonsoo.kim@lge.com, vbabka@suse.cz,
hannes@cmpxchg.org, mhocko@kernel.org, vdavydov.dev@gmail.com,
guro@fb.com
Cc: linux-mm@kvack.org, netdev@vger.kernel.org, bpf@vger.kernel.org,
Yafang Shao <laoar.shao@gmail.com>
Subject: [PATCH RFC 2/9] bpftool: show memcg info of bpf map
Date: Tue, 8 Mar 2022 13:10:49 +0000 [thread overview]
Message-ID: <20220308131056.6732-3-laoar.shao@gmail.com> (raw)
In-Reply-To: <20220308131056.6732-1-laoar.shao@gmail.com>
bpf map can be charged to a memcg, so we'd better show the memcg info to
do better bpf memory management. This patch adds a new field
"memcg_state" to show whether a bpf map is charged to a memcg and
whether the memcg is offlined. Currently it has three values,
0 : not charged
-1 : the charged memcg is offline
1 : the charged memcg is online
For instance,
$ bpftool map show
2: array name iterator.rodata flags 0x480
key 4B value 98B max_entries 1 memlock 4096B
btf_id 240 frozen
memcg_state 0
3: hash name calico_failsafe flags 0x1
key 4B value 1B max_entries 65535 memlock 524288B
memcg_state 1
6: lru_hash name access_record flags 0x0
key 8B value 24B max_entries 102400 memlock 3276800B
btf_id 256
memcg_state -1
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
include/uapi/linux/bpf.h | 1 +
kernel/bpf/syscall.c | 11 +++++++++++
tools/bpf/bpftool/map.c | 2 ++
tools/include/uapi/linux/bpf.h | 1 +
4 files changed, 15 insertions(+)
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 4eebea8..a448b06 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -5864,6 +5864,7 @@ struct bpf_map_info {
__u32 btf_value_type_id;
__u32 :32; /* alignment pad */
__u64 map_extra;
+ __s8 memcg_state;
} __attribute__((aligned(8)));
struct bpf_btf_info {
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index db402eb..3b50fcb 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3939,6 +3939,17 @@ static int bpf_map_get_info_by_fd(struct file *file,
}
info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
+#ifdef CONFIG_MEMCG_KMEM
+ if (map->memcg) {
+ struct mem_cgroup *memcg = map->memcg;
+
+ if (memcg == root_mem_cgroup)
+ info.memcg_state = 0;
+ else
+ info.memcg_state = memcg->kmemcg_id < 0 ? -1 : 1;
+ }
+#endif
+
if (bpf_map_is_dev_bound(map)) {
err = bpf_map_offload_info_fill(&info, map);
if (err)
diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
index 0bba337..fe8322f 100644
--- a/tools/bpf/bpftool/map.c
+++ b/tools/bpf/bpftool/map.c
@@ -550,6 +550,7 @@ static int show_map_close_json(int fd, struct bpf_map_info *info)
jsonw_end_array(json_wtr);
}
+ jsonw_int_field(json_wtr, "memcg_state", info->memcg_state);
emit_obj_refs_json(refs_table, info->id, json_wtr);
jsonw_end_object(json_wtr);
@@ -635,6 +636,7 @@ static int show_map_close_plain(int fd, struct bpf_map_info *info)
if (frozen)
printf("%sfrozen", info->btf_id ? " " : "");
+ printf("\n\tmemcg_state %d", info->memcg_state);
emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
printf("\n");
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 4eebea8..41e65b3 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -5864,6 +5864,7 @@ struct bpf_map_info {
__u32 btf_value_type_id;
__u32 :32; /* alignment pad */
__u64 map_extra;
+ __s8 memcg_state;
} __attribute__((aligned(8)));
struct bpf_btf_info {
--
1.8.3.1
next prev parent reply other threads:[~2022-03-08 13:11 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-08 13:10 [PATCH RFC 0/9] bpf, mm: recharge bpf memory from offline memcg Yafang Shao
2022-03-08 13:10 ` [PATCH RFC 1/9] bpftool: fix print error when show bpf man Yafang Shao
2022-03-08 13:10 ` Yafang Shao [this message]
2022-03-08 13:10 ` [PATCH RFC 3/9] mm: add methord to charge kmalloc-ed address Yafang Shao
2022-03-08 13:10 ` [PATCH RFC 4/9] mm: add methord to charge vmalloc-ed address Yafang Shao
2022-03-08 13:10 ` [PATCH RFC 5/9] mm: add methord to charge percpu address Yafang Shao
2022-03-08 13:10 ` [PATCH RFC 6/9] bpf: add a helper to find map by id Yafang Shao
2022-03-08 13:10 ` [PATCH RFC 7/9] bpf: add BPF_MAP_RECHARGE syscall Yafang Shao
2022-03-08 13:10 ` [PATCH RFC 8/9] bpf: make bpf_map_{save, release}_memcg public Yafang Shao
2022-03-08 13:10 ` [PATCH RFC 9/9] bpf: support recharge for hash map Yafang Shao
2022-03-09 1:09 ` [PATCH RFC 0/9] bpf, mm: recharge bpf memory from offline memcg Roman Gushchin
2022-03-09 13:28 ` Yafang Shao
2022-03-09 23:35 ` Roman Gushchin
2022-03-10 13:20 ` Yafang Shao
2022-03-10 18:00 ` Roman Gushchin
2022-03-11 12:48 ` Yafang Shao
2022-03-11 17:49 ` Roman Gushchin
2022-03-12 6:45 ` Yafang Shao
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=20220308131056.6732-3-laoar.shao@gmail.com \
--to=laoar.shao@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=cl@linux.com \
--cc=daniel@iogearbox.net \
--cc=guro@fb.com \
--cc=hannes@cmpxchg.org \
--cc=iamjoonsoo.kim@lge.com \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=penberg@kernel.org \
--cc=rientjes@google.com \
--cc=songliubraving@fb.com \
--cc=vbabka@suse.cz \
--cc=vdavydov.dev@gmail.com \
--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