Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp1296773pxb; Sat, 23 Apr 2022 02:40:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyvgaKmThs4gTR976ia0yVV0SYKclqxvQSW6kiWmb438pzek7PW0d89emM6KKRCa2kG8E5/ X-Received: by 2002:a17:903:2452:b0:15a:d388:efd8 with SMTP id l18-20020a170903245200b0015ad388efd8mr8884114pls.37.1650706836290; Sat, 23 Apr 2022 02:40:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650706836; cv=none; d=google.com; s=arc-20160816; b=Lce+1v0Rd5/4ZCSd1+6Lq9ICvd0nuvZaHCaNnjY5EN0a2FaEx3Nq4UxnfyCWSoq4Pa KubHp6eLxBnCWf8hH2PrbJOk1h8crRs1KZup4+bMxzk3ycr8/Gc/wNW8cmHdRNIXrGWH 8DBR+09irFPdQRDli3B6Q+P8ppr403G7iNgQj0x+KOhGRoj3dRR+9/n8+YU8QMZOcqPS 323xKIunXsFVjZ6tRKZ1vsSRARmYnw8tPRlGDcSyPeE3YiDDNxrcfLRCTv7sTz8oXAgf 5RGk3QVyNalntnGLU9Nir6m/jRLdTFMzc1odyA1e5C1BjzNsKtqf/609Bt3nY2SlVOFy CySg== 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=mif/hptMttckx/mJ2yjacwOwrfg6csXHsP1C0SeWPl4=; b=UkOGmXIsNtEp4Dvl7hyFAfzNLi9teCV9684WgqiyrOltdOhTom81Vv3d6PstDPX978 M1GzugqtQL8kS6FJIgc20I83lTyG19AzRegY6JhwjTSbku+6ZYdA2sNRKpBBVcTqESaA UKmvJDkpUGG/WzOkQ2iZbt+zfHAgYl61J+G7+lvUZrU727f943CrmAtSRPAthf6xQ/0p wea25KxmxgK2QmKJmeqUVAbsa0DOLHORcvxr9b4wSQuocRPK9EknAVosFtjWAT8Ots3z UZOstDHVMlhW3b+AHzi6Kt4C/19tqtn/68k6Ck9orD8fSVNW7AuVcB4HWfUx4OFJqunC NwWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@openvz-org.20210112.gappssmtp.com header.s=20210112 header.b=Jh3EjOr+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n1-20020a170902d2c100b0015b7b9822e3si5392840plc.444.2022.04.23.02.40.21; Sat, 23 Apr 2022 02:40:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@openvz-org.20210112.gappssmtp.com header.s=20210112 header.b=Jh3EjOr+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=openvz.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234138AbiDWH7g (ORCPT + 99 others); Sat, 23 Apr 2022 03:59:36 -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-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-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,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham 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_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 --- 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) #endif /* CONFIG_MEMCG_KMEM */ +static inline struct mem_cgroup *get_mem_cgroup_from_kmem(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; +} #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 +#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 = get_mem_cgroup_from_kmem(net); + if (!memcg) + memcg = root_mem_cgroup; + old = set_active_memcg(memcg); error = 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); -- 2.31.1