From nobody Sun May 10 21:18:31 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56252C433FE for ; Sat, 23 Apr 2022 07:56:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234118AbiDWH7h (ORCPT ); Sat, 23 Apr 2022 03:59:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234073AbiDWH7e (ORCPT ); Sat, 23 Apr 2022 03:59:34 -0400 Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 413043B2BC for ; Sat, 23 Apr 2022 00:56:38 -0700 (PDT) Received: by mail-lj1-x234.google.com with SMTP id v1so8740987ljv.3 for ; Sat, 23 Apr 2022 00:56:38 -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=mif/hptMttckx/mJ2yjacwOwrfg6csXHsP1C0SeWPl4=; b=Jh3EjOr+fbFSxsWWXzKZ1dLFWdYWIgAT+OvVwnM/YGyDQ7mBB2EepGX50xPrhenMRy 1u2x0E0gGW9XabiMiNEXr83wUcsgtzGlaWKoPNL4DichQ32o6WBMLLJm5WsseIYsGBM6 bpJDaCTTtsHJGcYtnnYYiY5zbPsAe9vWmAk3/b2eHsr7dOgmStQfhoy132T/YObSkjYC rcqowhNPmMq6tcJiN9k+xWNYvmBBNXVUGzvkel+WVoExNr+k2Vgl9IvU7FAzkQyzrQL4 sJq5PQfF02ZAr6M/man+jrRycFRNHpEDaLYAi8IRdb/B4kMJEt1pBYbsZgVDAKSASN1C zIgA== 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=mif/hptMttckx/mJ2yjacwOwrfg6csXHsP1C0SeWPl4=; b=Yv+jMEW5axiuGp9siQh0+ept6ydXbTPGlnTx1gRcOttd/aDIwvYN3IhzPTlUmnxIRz k1NU9OfkjekZ/57N2vS4oQ/JhAO8m9Yfm01pCr/9s4juMDTGVya6vJ27L5jXR4//vRPm qOA6d+cFDyDo8DHXY3qgnyC9qVDkqpvsi7UFmxwcMdaSex501UQhyadHTmPzp3PovH2n ric2+s5MMUKhI1N3N5U3OPPbADCEOtI4iZ8KUHxbcLRq2Br4GeINj1hAvVd/bzyXxOWG mUkMQirdDQ5N93+VUoEvZuGJqqBir30AerM3Ntg2VDFOLzIRfrDyC7epTfN4Iz3kJyfe 0RKg== X-Gm-Message-State: AOAM531zoA3o9xcaO8viJwWJ+5l8o8S4dB9dwjUSsgJYI24ys/GZewN1 mJi1ioE6c/Fh+9QTjBMP74D/AA== X-Google-Smtp-Source: ABdhPJzwOxJDiUT+mxVE2gdeqnTfab1zeDWAizki2WmGRG2zg3Ej+gEKUwuqGX9Bc50MTmsxtY5imA== X-Received: by 2002:a2e:9182:0:b0:24f:66f:7ca9 with SMTP id f2-20020a2e9182000000b0024f066f7ca9mr615846ljg.397.1650700596565; Sat, 23 Apr 2022 00:56:36 -0700 (PDT) Received: from [192.168.1.65] ([46.188.121.177]) by smtp.gmail.com with ESMTPSA id m5-20020a194345000000b0046e951e34b3sm525140lfj.24.2022.04.23.00.56.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 23 Apr 2022 00:56:35 -0700 (PDT) Message-ID: <6f38e02b-9af3-4dcf-9000-1118a04b13c7@openvz.org> Date: Sat, 23 Apr 2022 10:56:34 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 From: Vasily Averin Subject: [PATCH] net: set proper memcg for net_init hooks allocations To: Vlastimil Babka , Shakeel Butt Cc: kernel@openvz.org, Florian Westphal , linux-kernel@vger.kernel.org, Roman Gushchin , Michal Hocko , cgroups@vger.kernel.org, netdev@vger.kernel.org, "David S. Miller" , Jakub Kicinski , Paolo Abeni References: Content-Language: en-US In-Reply-To: Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" __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_ACCOUNTING 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 Reported-by: kernel test robot Reported-by: 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 | 11 +++++++++++ net/core/net_namespace.c | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 0abbd685703b..16b157852a8c 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1768,4 +1768,15 @@ static inline struct mem_cgroup *mem_cgroup_from_obj= (void *p) =20 #endif /* CONFIG_MEMCG_KMEM */ =20 +static inline struct mem_cgroup *get_mem_cgroup_from_kmem(void *p) +{ + struct mem_cgroup *memcg; + + rcu_read_lock(); + do { + memcg =3D mem_cgroup_from_obj(p); + } while (memcg && !css_tryget(&memcg->css)); + rcu_read_unlock(); + return memcg; +} #endif /* _LINUX_MEMCONTROL_H */ diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c index a5b5bb99c644..56608f56bed6 100644 --- a/net/core/net_namespace.c +++ b/net/core/net_namespace.c @@ -26,6 +26,7 @@ #include #include =20 +#include /* * Our network namespace constructor/destructor lists */ @@ -1147,7 +1148,15 @@ 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 =3D get_mem_cgroup_from_kmem(net); + if (!memcg) + memcg =3D root_mem_cgroup; + old =3D set_active_memcg(memcg); error =3D ops_init(ops, net); + set_active_memcg(old); + css_put(&memcg->css); if (error) goto out_undo; list_add_tail(&net->exit_list, &net_exit_list); --=20 2.31.1