Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: set proper memcg for net_init hooks allocations

__register_pernet_operations() executes init hook of registered
pernet_operation structure in all existing net namespaces.

Typically, these hooks are called by a process associated with the
specified net namespace, and all __GFP_ACCOUNT marked allocation are
accounted for corresponding container/memcg.

However __register_pernet_operations() calls the hooks in the same
context, and as a result all marked allocations are accounted to one memcg
for all processed net namespaces.

This patch adjusts active memcg for each net namespace and helps to
account memory allocated inside ops_init() into the proper memcg.

Link: https://lkml.kernel.org/r/f9394752-e272-9bf9-645f-a18c56d1c4ec@openvz.org
Signed-off-by: Vasily Averin <vvs@openvz.org>
Acked-by: Roman Gushchin <roman.gushchin@linux.dev>
Acked-by: Shakeel Butt <shakeelb@google.com>
Cc: Michal Koutný <mkoutny@suse.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Florian Westphal <fw@strlen.de>
Cc: David S. Miller <davem@davemloft.net>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
Cc: Linux Kernel Functional Testing <lkft@linaro.org>
Cc: Muchun Song <songmuchun@bytedance.com>
Cc: Naresh Kamboju <naresh.kamboju@linaro.org>
Cc: Qian Cai <quic_qiancai@quicinc.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Vasily Averin and committed by
akpm
1d0403d2 fc4db90f

+53 -1
+46 -1
include/linux/memcontrol.h
··· 1756 1756 rcu_read_unlock(); 1757 1757 } 1758 1758 1759 + /** 1760 + * get_mem_cgroup_from_obj - get a memcg associated with passed kernel object. 1761 + * @p: pointer to object from which memcg should be extracted. It can be NULL. 1762 + * 1763 + * Retrieves the memory group into which the memory of the pointed kernel 1764 + * object is accounted. If memcg is found, its reference is taken. 1765 + * If a passed kernel object is uncharged, or if proper memcg cannot be found, 1766 + * as well as if mem_cgroup is disabled, NULL is returned. 1767 + * 1768 + * Return: valid memcg pointer with taken reference or NULL. 1769 + */ 1770 + static inline struct mem_cgroup *get_mem_cgroup_from_obj(void *p) 1771 + { 1772 + struct mem_cgroup *memcg; 1773 + 1774 + rcu_read_lock(); 1775 + do { 1776 + memcg = mem_cgroup_from_obj(p); 1777 + } while (memcg && !css_tryget(&memcg->css)); 1778 + rcu_read_unlock(); 1779 + return memcg; 1780 + } 1781 + 1782 + /** 1783 + * mem_cgroup_or_root - always returns a pointer to a valid memory cgroup. 1784 + * @memcg: pointer to a valid memory cgroup or NULL. 1785 + * 1786 + * If passed argument is not NULL, returns it without any additional checks 1787 + * and changes. Otherwise, root_mem_cgroup is returned. 1788 + * 1789 + * NOTE: root_mem_cgroup can be NULL during early boot. 1790 + */ 1791 + static inline struct mem_cgroup *mem_cgroup_or_root(struct mem_cgroup *memcg) 1792 + { 1793 + return memcg ? memcg : root_mem_cgroup; 1794 + } 1759 1795 #else 1760 1796 static inline bool mem_cgroup_kmem_disabled(void) 1761 1797 { ··· 1835 1799 1836 1800 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p) 1837 1801 { 1838 - return NULL; 1802 + return NULL; 1839 1803 } 1840 1804 1841 1805 static inline struct mem_cgroup *mem_cgroup_from_slab_obj(void *p) ··· 1848 1812 { 1849 1813 } 1850 1814 1815 + static inline struct mem_cgroup *get_mem_cgroup_from_obj(void *p) 1816 + { 1817 + return NULL; 1818 + } 1819 + 1820 + static inline struct mem_cgroup *mem_cgroup_or_root(struct mem_cgroup *memcg) 1821 + { 1822 + return NULL; 1823 + } 1851 1824 #endif /* CONFIG_MEMCG_KMEM */ 1852 1825 1853 1826 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
+7
net/core/net_namespace.c
··· 18 18 #include <linux/user_namespace.h> 19 19 #include <linux/net_namespace.h> 20 20 #include <linux/sched/task.h> 21 + #include <linux/sched/mm.h> 21 22 #include <linux/uidgid.h> 22 23 #include <linux/cookie.h> 23 24 ··· 1144 1143 * setup_net() and cleanup_net() are not possible. 1145 1144 */ 1146 1145 for_each_net(net) { 1146 + struct mem_cgroup *old, *memcg; 1147 + 1148 + memcg = mem_cgroup_or_root(get_mem_cgroup_from_obj(net)); 1149 + old = set_active_memcg(memcg); 1147 1150 error = ops_init(ops, net); 1151 + set_active_memcg(old); 1152 + mem_cgroup_put(memcg); 1148 1153 if (error) 1149 1154 goto out_undo; 1150 1155 list_add_tail(&net->exit_list, &net_exit_list);