mm: memcontrol: add new kernel parameter cgroup.memory=nobpf
authorYafang Shao <laoar.shao@gmail.com>
Fri, 10 Feb 2023 15:47:31 +0000 (15:47 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 11 Feb 2023 02:59:56 +0000 (18:59 -0800)
Add new kernel parameter cgroup.memory=nobpf to allow user disable bpf
memory accounting. This is a preparation for the followup patch.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Roman Gushchin <roman.gushchin@linux.dev>
Link: https://lore.kernel.org/r/20230210154734.4416-2-laoar.shao@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Documentation/admin-guide/kernel-parameters.txt
include/linux/memcontrol.h
mm/memcontrol.c

index 6cfa6e3996cf75ee6bb1e8b399daa4d5701ab92b..29fb41e801ce0c50fa292091442fd6a18d4927ed 100644 (file)
                        Format: <string>
                        nosocket -- Disable socket memory accounting.
                        nokmem -- Disable kernel memory accounting.
+                       nobpf -- Disable BPF memory accounting.
 
        checkreqprot=   [SELINUX] Set initial checkreqprot flag value.
                        Format: { "0" | "1" }
index 85dc9b88ea3799ed5e598548aa99439a1064a8d8..1e38e99998c79bb4eca5191fe6197ae165e79adb 100644 (file)
@@ -1754,6 +1754,12 @@ struct obj_cgroup *get_obj_cgroup_from_page(struct page *page);
 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size);
 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size);
 
+extern struct static_key_false memcg_bpf_enabled_key;
+static inline bool memcg_bpf_enabled(void)
+{
+       return static_branch_likely(&memcg_bpf_enabled_key);
+}
+
 extern struct static_key_false memcg_kmem_enabled_key;
 
 static inline bool memcg_kmem_enabled(void)
@@ -1832,6 +1838,11 @@ static inline struct obj_cgroup *get_obj_cgroup_from_page(struct page *page)
        return NULL;
 }
 
+static inline bool memcg_bpf_enabled(void)
+{
+       return false;
+}
+
 static inline bool memcg_kmem_enabled(void)
 {
        return false;
index 73afff8062f9bf96b17dfae12fbe18091e215cda..49f40730e7117da7747940942016b97c7ff1e101 100644 (file)
@@ -88,6 +88,9 @@ static bool cgroup_memory_nosocket __ro_after_init;
 /* Kernel memory accounting disabled? */
 static bool cgroup_memory_nokmem __ro_after_init;
 
+/* BPF memory accounting disabled? */
+static bool cgroup_memory_nobpf __ro_after_init;
+
 #ifdef CONFIG_CGROUP_WRITEBACK
 static DECLARE_WAIT_QUEUE_HEAD(memcg_cgwb_frn_waitq);
 #endif
@@ -347,6 +350,9 @@ static void memcg_reparent_objcgs(struct mem_cgroup *memcg,
  */
 DEFINE_STATIC_KEY_FALSE(memcg_kmem_enabled_key);
 EXPORT_SYMBOL(memcg_kmem_enabled_key);
+
+DEFINE_STATIC_KEY_FALSE(memcg_bpf_enabled_key);
+EXPORT_SYMBOL(memcg_bpf_enabled_key);
 #endif
 
 /**
@@ -5357,6 +5363,11 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
        if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
                static_branch_inc(&memcg_sockets_enabled_key);
 
+#if defined(CONFIG_MEMCG_KMEM)
+       if (!cgroup_memory_nobpf)
+               static_branch_inc(&memcg_bpf_enabled_key);
+#endif
+
        return &memcg->css;
 }
 
@@ -5441,6 +5452,11 @@ static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
        if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_active)
                static_branch_dec(&memcg_sockets_enabled_key);
 
+#if defined(CONFIG_MEMCG_KMEM)
+       if (!cgroup_memory_nobpf)
+               static_branch_dec(&memcg_bpf_enabled_key);
+#endif
+
        vmpressure_cleanup(&memcg->vmpressure);
        cancel_work_sync(&memcg->high_work);
        mem_cgroup_remove_from_trees(memcg);
@@ -7269,6 +7285,8 @@ static int __init cgroup_memory(char *s)
                        cgroup_memory_nosocket = true;
                if (!strcmp(token, "nokmem"))
                        cgroup_memory_nokmem = true;
+               if (!strcmp(token, "nobpf"))
+                       cgroup_memory_nobpf = true;
        }
        return 1;
 }