Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp894840pxb; Fri, 22 Apr 2022 13:40:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyDVOTW6qOx+1zWZPZtMgbak8zYiVDyZKcmYd3JVevz2Iv9sAjTVigsjV/db8PEBq5BHHSv X-Received: by 2002:a17:902:ef45:b0:156:1858:71fc with SMTP id e5-20020a170902ef4500b00156185871fcmr6348308plx.23.1650660000686; Fri, 22 Apr 2022 13:40:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650660000; cv=none; d=google.com; s=arc-20160816; b=BLVIJ1tLIi4bcQCm+yd5bTkoIQAfD9SZe9h4hGBepb2oy7Jtnrus2Yn3Csble3t+IO LydhO2VD7DkSlCCo+dzSMIn98hid/tJR4GKCykTsJZJKvP6FpZgPukPScCv9XzT2omrv zzrSbXProrQ4+eIPrhcyu2/frfvDKgbfHS5C0Xosf9a1i0bEsAcB9uj1CfNK8MsS/+RU U1k5rL+Ol9MNHIQdfk87iovypp+T/G4EawlVs7txywVvSt+iW4vaTdDEkcA2QPD42Svr lpYOlm/mHC7OJsuhqUH5w05Wb4AZhPMORSymKJDVgt5wvsvyNq3NypgF+xloF5HMri7n UVXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Tb6I/vNw4MkH6cXJACmThyQFIFSwo0v3WzOA2XSKmSs=; b=OBc85VWZuBcSM/qVDTcy5wXKXAWbLEVyu2k97RD/4izpkluq2Zqfr8BegFwFIMBbr7 ikI8ytSYT4/oJv1DsVt+HtPQ9sKa99VzqWQW9kl/Iv7HT4eO7/cnkPSeTlRp04ElYJ+6 aGJm8/fDviTBTEdnR2z+yeXbXdJYDZi+jZUdYnWBjs1fsMb1X3U5omsIzm265ZWEXhjD Ei8I9wrAniRoW30EiHbD5u0HxVxC+K2pmqJrAi4vHQR0suADrMVSUjm9e0oOmlpI2kT+ CfkZz3PcYnFQoJS+66nnistmVuOLGh38V+VClCwTYhVUKIdBnbTjuPN9W0rYrXG1+G/n Ekdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=hkeImdbT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id mw10-20020a17090b4d0a00b001cb1157100esi10039673pjb.137.2022.04.22.13.40.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 13:40:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=hkeImdbT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BEF381CCF8E; Fri, 22 Apr 2022 12:24:54 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232453AbiDUP7P (ORCPT + 99 others); Thu, 21 Apr 2022 11:59:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232456AbiDUP7O (ORCPT ); Thu, 21 Apr 2022 11:59:14 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B78DB40E70 for ; Thu, 21 Apr 2022 08:56:23 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id x80so5407178pfc.1 for ; Thu, 21 Apr 2022 08:56:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Tb6I/vNw4MkH6cXJACmThyQFIFSwo0v3WzOA2XSKmSs=; b=hkeImdbTeSMXSMeckvO9JBqgK+ypyTZt/okKK7TMyp+Jj42kGAhZ/7QBcpqgOpnqZj gGCX5b138SAqJNu67mSEPBS6HjBSc3NUwwii2N9q3VIj1j6hfggpfT0NzCG9RSRX5XzX nem4EvlLHjThukcbQCBeuQSpwwkuANGAiKT4jdpMxO+QaID+CZCjyfdsqCFNkXN894SL ibNTaINtlSpFjl1GBaz7n+xDgttL7ngW7o2eXndy6RqXfSveGmsHbAN72F463+yh9jhQ LwpxF/i4bpdWG8LRcg6N36V32wZxeba+ZnbpooMdNjNU398VMLDoMlcqVKTjzjuxKwPh 7KWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Tb6I/vNw4MkH6cXJACmThyQFIFSwo0v3WzOA2XSKmSs=; b=I88Q4OZJyBG/zmQZ7NN63Nu/WO98hG5VYTXWYC7lUn4CXrwRT3X3582y2WmQ2OJr69 +rFD7qsFXbcOw1i2FledxWKB1pXkLVV+TxMztTdRLdnCEQc7QosUi08cXycgxnnfBiYa D/AY25pBk5F/aJuszYjREfnKxOaW9R+JSv5EsgICYTUMne5FwYAC5y3cdBeZONZ0RUFm 0Wgf7a4rJA+NXAC6YX5iipQbGShdWKEHjqNonkXBO03UzxxujmmcmFuUvfujR7GoKEL4 Xe7aC8arfbvJv4vJNiHuRjOdHj4ZCJA+0a6Ip2PYRtOlzSuMj8i1qq1m6GG4ERWeeVz0 YA7w== X-Gm-Message-State: AOAM531gv5QocmvjLkumWcVs/Ik/wgq7iPOXWdgMY7WaVw+1GNVOKHp9 8Xaf2+tLMrHBaafcsyIV2mtLREw3mHHe06wHFM+Giw== X-Received: by 2002:a63:9502:0:b0:386:3916:ca8e with SMTP id p2-20020a639502000000b003863916ca8emr78805pgd.357.1650556582965; Thu, 21 Apr 2022 08:56:22 -0700 (PDT) MIME-Version: 1.0 References: <46c1c59e-1368-620d-e57a-f35c2c82084d@linux.dev> <55605876-d05a-8be3-a6ae-ec26de9ee178@openvz.org> In-Reply-To: <55605876-d05a-8be3-a6ae-ec26de9ee178@openvz.org> From: Shakeel Butt Date: Thu, 21 Apr 2022 08:56:12 -0700 Message-ID: Subject: Re: [PATCH memcg RFC] net: set proper memcg for net_init hooks allocations To: Vasily Averin Cc: Vlastimil Babka , Roman Gushchin , kernel@openvz.org, LKML , netdev , Cgroups , Michal Hocko , Florian Westphal , "David S. Miller" , Jakub Kicinski , Paolo Abeni Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,USER_IN_DEF_DKIM_WL 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 On Sat, Apr 16, 2022 at 11:39 PM Vasily Averin wrote: > > __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 > --- > Dear Vlastimil, Roman, > I'm not sure that memcg is used correctly here, > is it perhaps some additional locking required? > --- > net/core/net_namespace.c | 7 +++++++ > 1 file changed, 7 insertions(+) > > diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c > index a5b5bb99c644..171c6e0b2337 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,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 = NULL; > +#ifdef CONFIG_MEMCG > + memcg = (net == &init_net) ? root_mem_cgroup : mem_cgroup_from_obj(net); memcg from obj is unstable, so you need a reference on memcg. You can introduce get_mem_cgroup_from_kmem() which works for both MEMCG_DATA_OBJCGS and MEMCG_DATA_KMEM. For uncharged objects (like init_net) it should return NULL. > +#endif > + old = set_active_memcg(memcg); > error = ops_init(ops, net); > + set_active_memcg(old); > if (error) > goto out_undo; > list_add_tail(&net->exit_list, &net_exit_list); > -- > 2.31.1 >