Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp81322iof; Sun, 5 Jun 2022 21:47:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTGUCHqwcthEvWZ+UCaWhxYSfRT8eA2i/iYsMOcWx15zjClFybkbal2sw0PHOtz24lFQKw X-Received: by 2002:a17:90b:1a86:b0:1e8:2b80:5e07 with SMTP id ng6-20020a17090b1a8600b001e82b805e07mr18041139pjb.31.1654490834057; Sun, 05 Jun 2022 21:47:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654490834; cv=none; d=google.com; s=arc-20160816; b=vDeiz7rFV27FzBPtJoDWeatzlBGNHduCbzYCEktf1bCqji24401Q82cD3ba4gx53Tw KeX6ZEQ6ytEtD+OHyB3b/2pRyPMVnjjbg0JAzbebEti/Ozs0CDp5FIiWIb6IPp/weD1i Lm1SXrOg4NFewr1S5n8ZmRWhfA/0Xuea4bkBJ/KViqKIj9PIk1X1DZBVWlazmciAjv8L tCzyWQTmgebGyXpLL8qgEP8pmSpq3L0l8xTnmLfVVwecsJl13DriyBIEQnHSoVZxqO0P ReQ2BbrdhRZK1OrcfLc8RqBbesGsSYPSEqqc7/9reXGEyZnb9EQpir4h5SHwwTMv8W/t UUdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :content-language:references:cc:to:subject:from:user-agent :mime-version:date:message-id:dkim-signature; bh=pSYwMXTiWYFCbMo+OGvlkKXlgdvtI08BWksfEz9zHwI=; b=kHqa8qK+LrRppF6s71lCtdM418mHwradn3MC8DwhihpkMKUrpQN9Bl3+5ZR7BcAgvh 26qM5Nii4WUyEO3X3YH7FqItHut8KiNP27wqYllRL1hiPcanUBgiNa0oXAInSp8IgWNY +SRUcW9MVRCqawEQ1w6+q1E+LD651IwUNJhJjE8hgV8tki12wb8FtZU7kiyyQ1GEuJzU GRoyeBlMGnHPKt2v2XdVQ3P9qxMgxVJmYPjohTtJ2vYPkwPd5JTyqwT2ca7Z+LzYEOOJ OrQkxHsuHIYMhtmYPMQKENJqAdV0y3ZDVDshMpXUy5hqwhoD7hdljhcS4VtTAXvhJt1G myjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@openvz-org.20210112.gappssmtp.com header.s=20210112 header.b=1CODD47V; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=openvz.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id mi10-20020a17090b4b4a00b001e67f6b069csi18366143pjb.131.2022.06.05.21.47.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 21:47:14 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@openvz-org.20210112.gappssmtp.com header.s=20210112 header.b=1CODD47V; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=openvz.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 49944473B5; Sun, 5 Jun 2022 21:04:53 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230315AbiFCETz (ORCPT + 99 others); Fri, 3 Jun 2022 00:19:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233753AbiFCETw (ORCPT ); Fri, 3 Jun 2022 00:19:52 -0400 Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E3F260EB for ; Thu, 2 Jun 2022 21:19:47 -0700 (PDT) Received: by mail-lf1-x12f.google.com with SMTP id i10so10852064lfj.0 for ; Thu, 02 Jun 2022 21:19:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz-org.20210112.gappssmtp.com; s=20210112; h=message-id:date:mime-version:user-agent:from:subject:to:cc :references:content-language:in-reply-to:content-transfer-encoding; bh=pSYwMXTiWYFCbMo+OGvlkKXlgdvtI08BWksfEz9zHwI=; b=1CODD47VWyN69Es67Z6riONBZqKuEEgP78VqiQGl2vKLYNexE0WZpG6O9FiSYe5iqv ijkoUB7PQI50ZeSmOFMjjVRjN1uCRNQXm7HLQA+/dGdta3BuKcLeph449pqL+xmCSxUv ibISQ35Y02uC1uLE9hupFlNAZMJ9BQwJK0pZTp4HF75l7144MaPFHMt5uG35vmDjpQNW kbLx6otbwcZRihC9aa5yGdkhFBxOFsqZsj4TksediRwyE5kp6Qys6pnfLGbOAfLN/KMD gHtYVUcq0tFXMMbbM2SCeXnfJQKJ/DE0whVWFW5GaBPAHWVqtc4NOjG1/TuG585ZbJzB 2w0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:from :subject:to:cc:references:content-language:in-reply-to :content-transfer-encoding; bh=pSYwMXTiWYFCbMo+OGvlkKXlgdvtI08BWksfEz9zHwI=; b=VrhraFca0+eLGRJ99+JF3vbOZnUrQCQTEl8nuGbP52J1Gk983/b9Mb9CSpdAcjbo9w h/C29l07Wg6Ko2HY4axEWX4VmepUr9p1mspLAee++q1XHwc2CRd5cTp2lY8CLHWhXN9o vPysuKimifX8or4Io3ErUZtVyiu8qMYkmU64me19UKFbY3fAnNyrIOoY/W3gOpnm4Ft8 84mLkSrKlKCipd4pWVcskU05CIpRP1JVqkEzDW8v+8zUyG5ZXhYrAhvaGULQsbZJE7Qg qkGubEPumsW9f3A75IwSVOMqlCj11reaNiAA0dMY1gFRxJd8OC1FG/wjzLxVA3jhAiDK 5f2Q== X-Gm-Message-State: AOAM531+rNoD677NIM3PSpvLeksWdjqcI/+TCMq31VjQCTfLtY73U3wA en0Lk28oyqVdpPNKDM0HLMeGPQ== X-Received: by 2002:a05:6512:118a:b0:478:a8b3:e1ee with SMTP id g10-20020a056512118a00b00478a8b3e1eemr32562601lfr.391.1654229985403; Thu, 02 Jun 2022 21:19:45 -0700 (PDT) Received: from [192.168.1.65] ([46.188.121.129]) by smtp.gmail.com with ESMTPSA id s17-20020a197711000000b0047255d211fasm617383lfc.297.2022.06.02.21.19.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Jun 2022 21:19:44 -0700 (PDT) Message-ID: Date: Fri, 3 Jun 2022 07:19:43 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1 From: Vasily Averin Subject: [PATCH memcg v6] net: set proper memcg for net_init hooks allocations To: Andrew Morton Cc: kernel@openvz.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Shakeel Butt , Roman Gushchin , =?UTF-8?Q?Michal_Koutn=c3=bd?= , Vlastimil Babka , Michal Hocko , Florian Westphal , "David S. Miller" , Jakub Kicinski , Paolo Abeni , Eric Dumazet , cgroups@vger.kernel.org References: <6b362c6e-9c80-4344-9430-b831f9871a3c@openvz.org> Content-Language: en-US In-Reply-To: <6b362c6e-9c80-4344-9430-b831f9871a3c@openvz.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org __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. Signed-off-by: Vasily Averin Acked-by: Roman Gushchin Acked-by: Shakeel Butt --- v6: re-based to current upstream (v5.18-11267-gb00ed48bb0a7) v5: documented get_mem_cgroup_from_obj() and for mem_cgroup_or_root() functions, asked by Shakeel. v4: get_mem_cgroup_from_kmem() renamed to get_mem_cgroup_from_obj(), get_net_memcg() renamed to mem_cgroup_or_root(), suggested by Roman. v3: put_net_memcg() replaced by an alreay existing mem_cgroup_put() It checks memcg before accessing it, this is required for __register_pernet_operations() called before memcg initialization. Additionally fixed leading whitespaces in non-memcg_kmem version of mem_cgroup_from_obj(). v2: introduced get/put_net_memcg(), new functions are moved under CONFIG_MEMCG_KMEM to fix compilation issues reported by Intel's kernel test robot v1: introduced get_mem_cgroup_from_kmem(), which takes the refcount for the found memcg, suggested by Shakeel --- include/linux/memcontrol.h | 47 +++++++++++++++++++++++++++++++++++++- net/core/net_namespace.c | 7 ++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 9ecead1042b9..dad16b484cd5 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1755,6 +1755,42 @@ static inline void count_objcg_event(struct obj_cgroup *objcg, rcu_read_unlock(); } +/** + * get_mem_cgroup_from_obj - get a memcg associated with passed kernel object. + * @p: pointer to object from which memcg should be extracted. It can be NULL. + * + * Retrieves the memory group into which the memory of the pointed kernel + * object is accounted. If memcg is found, its reference is taken. + * If a passed kernel object is uncharged, or if proper memcg cannot be found, + * as well as if mem_cgroup is disabled, NULL is returned. + * + * Return: valid memcg pointer with taken reference or NULL. + */ +static inline struct mem_cgroup *get_mem_cgroup_from_obj(void *p) +{ + struct mem_cgroup *memcg; + + rcu_read_lock(); + do { + memcg = mem_cgroup_from_obj(p); + } while (memcg && !css_tryget(&memcg->css)); + rcu_read_unlock(); + return memcg; +} + +/** + * mem_cgroup_or_root - always returns a pointer to a valid memory cgroup. + * @memcg: pointer to a valid memory cgroup or NULL. + * + * If passed argument is not NULL, returns it without any additional checks + * and changes. Otherwise, root_mem_cgroup is returned. + * + * NOTE: root_mem_cgroup can be NULL during early boot. + */ +static inline struct mem_cgroup *mem_cgroup_or_root(struct mem_cgroup *memcg) +{ + return memcg ? memcg : root_mem_cgroup; +} #else static inline bool mem_cgroup_kmem_disabled(void) { @@ -1798,7 +1834,7 @@ static inline int memcg_kmem_id(struct mem_cgroup *memcg) static inline struct mem_cgroup *mem_cgroup_from_obj(void *p) { - return NULL; + return NULL; } static inline void count_objcg_event(struct obj_cgroup *objcg, @@ -1806,6 +1842,15 @@ static inline void count_objcg_event(struct obj_cgroup *objcg, { } +static inline struct mem_cgroup *get_mem_cgroup_from_obj(void *p) +{ + return NULL; +} + +static inline struct mem_cgroup *mem_cgroup_or_root(struct mem_cgroup *memcg) +{ + return NULL; +} #endif /* CONFIG_MEMCG_KMEM */ #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c index 0ec2f5906a27..6b9f19122ec1 100644 --- a/net/core/net_namespace.c +++ b/net/core/net_namespace.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -1143,7 +1144,13 @@ static int __register_pernet_operations(struct list_head *list, * setup_net() and cleanup_net() are not possible. */ for_each_net(net) { + struct mem_cgroup *old, *memcg; + + memcg = mem_cgroup_or_root(get_mem_cgroup_from_obj(net)); + old = set_active_memcg(memcg); error = ops_init(ops, net); + set_active_memcg(old); + mem_cgroup_put(memcg); if (error) goto out_undo; list_add_tail(&net->exit_list, &net_exit_list); -- 2.36.1