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 9/9] bpf: support recharge for hash map
Date: Tue, 8 Mar 2022 13:10:56 +0000 [thread overview]
Message-ID: <20220308131056.6732-10-laoar.shao@gmail.com> (raw)
In-Reply-To: <20220308131056.6732-1-laoar.shao@gmail.com>
This patch supports recharge for hash map. We have already known how the
hash map is allocated and freed, we can also know how to charge and
uncharge the hash map. Firstly, we need to uncharge it from the old
memcg, then charge it to the current memcg. The old memcg must be an
offline memcg.
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
kernel/bpf/hashtab.c | 35 +++++++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index 6587796..4d103f1 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -10,6 +10,7 @@
#include <linux/random.h>
#include <uapi/linux/btf.h>
#include <linux/rcupdate_trace.h>
+#include <linux/memcontrol.h>
#include "percpu_freelist.h"
#include "bpf_lru_list.h"
#include "map_in_map.h"
@@ -1466,6 +1467,36 @@ static void htab_map_free(struct bpf_map *map)
kfree(htab);
}
+static bool htab_map_recharge_memcg(struct bpf_map *map)
+{
+ struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
+ struct mem_cgroup *old = map->memcg;
+ int i;
+
+ if (!old)
+ return false;
+
+ /* Only process offline memcg */
+ if (old == root_mem_cgroup || old->kmemcg_id >= 0)
+ return false;
+
+ bpf_map_release_memcg(map);
+ kcharge(htab, false);
+ kvcharge(htab->buckets, false);
+ charge_percpu(htab->extra_elems, false);
+ for (i = 0; i < HASHTAB_MAP_LOCK_COUNT; i++)
+ charge_percpu(htab->map_locked[i], false);
+
+ kcharge(htab, true);
+ kvcharge(htab->buckets, true);
+ charge_percpu(htab->extra_elems, true);
+ for (i = 0; i < HASHTAB_MAP_LOCK_COUNT; i++)
+ charge_percpu(htab->map_locked[i], true);
+ bpf_map_save_memcg(map);
+
+ return true;
+}
+
static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
struct seq_file *m)
{
@@ -2111,6 +2142,7 @@ static int bpf_for_each_hash_elem(struct bpf_map *map, bpf_callback_t callback_f
.map_alloc_check = htab_map_alloc_check,
.map_alloc = htab_map_alloc,
.map_free = htab_map_free,
+ .map_recharge_memcg = htab_map_recharge_memcg,
.map_get_next_key = htab_map_get_next_key,
.map_release_uref = htab_map_free_timers,
.map_lookup_elem = htab_map_lookup_elem,
@@ -2133,6 +2165,7 @@ static int bpf_for_each_hash_elem(struct bpf_map *map, bpf_callback_t callback_f
.map_alloc_check = htab_map_alloc_check,
.map_alloc = htab_map_alloc,
.map_free = htab_map_free,
+ .map_recharge_memcg = htab_map_recharge_memcg,
.map_get_next_key = htab_map_get_next_key,
.map_release_uref = htab_map_free_timers,
.map_lookup_elem = htab_lru_map_lookup_elem,
@@ -2258,6 +2291,7 @@ static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
.map_alloc_check = htab_map_alloc_check,
.map_alloc = htab_map_alloc,
.map_free = htab_map_free,
+ .map_recharge_memcg = htab_map_recharge_memcg,
.map_get_next_key = htab_map_get_next_key,
.map_lookup_elem = htab_percpu_map_lookup_elem,
.map_lookup_and_delete_elem = htab_percpu_map_lookup_and_delete_elem,
@@ -2278,6 +2312,7 @@ static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
.map_alloc_check = htab_map_alloc_check,
.map_alloc = htab_map_alloc,
.map_free = htab_map_free,
+ .map_recharge_memcg = htab_map_recharge_memcg,
.map_get_next_key = htab_map_get_next_key,
.map_lookup_elem = htab_lru_percpu_map_lookup_elem,
.map_lookup_and_delete_elem = htab_lru_percpu_map_lookup_and_delete_elem,
--
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 ` [PATCH RFC 2/9] bpftool: show memcg info of bpf map Yafang Shao
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 ` Yafang Shao [this message]
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-10-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