Re: [PATCH bpf-next 13/15] mm, memcg: Add new helper get_obj_cgroup_from_cgroup

From
Yafang Shao <hidden>
Date
2022-08-13 00:07:44
Also in
bpf, linux-mm
Reply to
Shakeel Butt, 2022-08-12
On Sat, Aug 13, 2022 at 12:57 AM Shakeel Butt [off-list ref] wrote:
On Wed, Aug 10, 2022 at 03:18:38PM +0000, Yafang Shao wrote:
quoted
Introduce new helper get_obj_cgroup_from_cgroup() to get obj_cgroup from
a specific cgroup.
Can you please add couple of lines on why you need objcg?
Sure. will update in the next version.
quoted
Signed-off-by: Yafang Shao <redacted>
---
 include/linux/memcontrol.h |  1 +
 mm/memcontrol.c            | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 2f0a611..901a921 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -1713,6 +1713,7 @@ static inline void set_shrinker_bit(struct mem_cgroup *memcg,
 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
 void __memcg_kmem_uncharge_page(struct page *page, int order);

+struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp);
 struct obj_cgroup *get_obj_cgroup_from_current(void);
 struct obj_cgroup *get_obj_cgroup_from_page(struct page *page);
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 618c366..762cffa 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2908,6 +2908,47 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg)
      return objcg;
 }

+static struct obj_cgroup *get_obj_cgroup_from_memcg(struct mem_cgroup *memcg)
+{
+     struct obj_cgroup *objcg;
+
+     if (memcg_kmem_bypass())
+             return NULL;
+
+     rcu_read_lock();
+     objcg = __get_obj_cgroup_from_memcg(memcg);
+     rcu_read_unlock();
+     return objcg;
+}
+
+struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp)
+{
+     struct cgroup_subsys_state *css;
+     struct mem_cgroup *memcg;
+     struct obj_cgroup *objcg;
+
+     rcu_read_lock();
+     css = rcu_dereference(cgrp->subsys[memory_cgrp_id]);
+     if (!css || !css_tryget_online(css)) {
+             rcu_read_unlock();
+             cgroup_put(cgrp);
+             return ERR_PTR(-EINVAL);
+     }
+     rcu_read_unlock();
+     cgroup_put(cgrp);
The above put seems out of place and buggy.
Thanks for pointing it out.
The cgroup_put should be used in bpf_map_save_memcg().
I will update it.

-- 
Regards
Yafang
Thread (33 messages) 33 messages, 5 authors, last reply 2022-08-14
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help