From mboxrd@z Thu Jan 1 00:00:00 1970 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 94511C433F5 for ; Thu, 21 Apr 2022 15:56:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1390335AbiDUP7Q (ORCPT ); Thu, 21 Apr 2022 11:59:16 -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-Google-Smtp-Source: ABdhPJzl3m2COqyb0vmSqE7CigU3O3oCUJAkex/AAQkQPfRDhlCItCldOZZiRmn4K+gJ/4u1hsqVXRz0RihG4QjuGGM= 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" 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 >