Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp983307pxb; Fri, 22 Apr 2022 15:57:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwIg6nInbgihiZF+aIwQJdGXciqqKpPuTG2LWdzjdzhGEvgwSz1fxk3qEGeM8nD3qU3DBU4 X-Received: by 2002:aa7:8e85:0:b0:50c:f0a6:2312 with SMTP id a5-20020aa78e85000000b0050cf0a62312mr4454947pfr.74.1650668238186; Fri, 22 Apr 2022 15:57:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650668238; cv=none; d=google.com; s=arc-20160816; b=aoh0PgPlsxAyHt21z16t+pI2vhPT6HuCX6wdaJPNXJXhIlbs/RGYYmVTjgqdZ9Lq8Z EHjEYmkB7wzQCO2PI0Q01hitufOfBTZ4F48Icg0j6M/ByZr7Emo2n+2zQlS29S48QYIa 1kFMY0Qg3j4YPZcfSodAeQ+GACpSX0+jyZcdzjLwQcV/0pJRYfse9Cme3xpQa09Zdf+4 Xkf9AO2u5ghIXsQh0bQRIf4b5HguM+kZCuBkxetQa+/6K7WfUIGk7YFW4UGCpfp/vGMM nUVn4OngubU1zD59g0pCm2k6QAjFHb1ssd9dQFWHRgSH8xhDyEZCOKa97oNh4R3yhOFw tLeA== 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:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=uID8iZcNLBme4rZdpwP2elgnitNvjR/PYrEFPkEMrt4=; b=piusj4HIGw/Q+6LX9k7V4uD5X4bmF9YZqqkKRa+VK/4fVJ/1Z5tHwOQALNAkSNRz86 OM61akOCqo7cC6CV1cjP7v82iKFTGegnHXg3RpKZpuwm4XGRX//qTNWPzkNiK66OxlO5 PIQjjCRuoHfXQ5EDamcr/5jgvbkF6yNzeqHZIjIEReHfrR6JGquDmoOuXXQZkZzakSAb 4mVbKlLvnDs7uxuI9GwUzDhL6DfyrAfGgtCA7BsyfC+7leNLvgGIZ+jYSnQVk11AO1yv 3nNcqvgl5FLFp3fTeR66Tl4B15dmr6pLQs6EMl8zOTv0svbUncfruPhh4kfPySqRZb6J Oupw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@openvz-org.20210112.gappssmtp.com header.s=20210112 header.b=K87wFhFC; 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=fail (p=NONE sp=NONE dis=NONE) header.from=openvz.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id j18-20020a056a00131200b004faa473dabcsi9094629pfu.301.2022.04.22.15.57.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 15:57:18 -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=@openvz-org.20210112.gappssmtp.com header.s=20210112 header.b=K87wFhFC; 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=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 70FBC320388; Fri, 22 Apr 2022 14:07:11 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229637AbiDVVKA (ORCPT + 99 others); Fri, 22 Apr 2022 17:10:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229514AbiDVVJ5 (ORCPT ); Fri, 22 Apr 2022 17:09:57 -0400 Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E9512465FB for ; Fri, 22 Apr 2022 13:06:02 -0700 (PDT) Received: by mail-lf1-x129.google.com with SMTP id y32so16110495lfa.6 for ; Fri, 22 Apr 2022 13:06:01 -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:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=uID8iZcNLBme4rZdpwP2elgnitNvjR/PYrEFPkEMrt4=; b=K87wFhFCqh3RUSfcjyHWaJ/5OlWIaCtbKiOBy/+lg00oDsHo00dI9Rz913ownD+DBL 47j/1FQaoHEzvRGxNbrxGsgl7+D50V85YT1FdoRMTvK7qMkmsNqF9izOyRDx+dPlVcrN PuU5VqnxQaMP1XvJIEFREriWuLui4ThOlm6RCIy09TlUR3zrwFtmnqN//0XUrW33aZyh HKhKRwmvqOV03Ak4cj8WgBUbW7mMsZ2Dg0uMMOqoASX+5BJ0q9R/08Zvy/VmFiAMWiAq NlhqszFUOsK5gPC6/k2K5W00Ot+1S6YmXD7AVIwsTs2LI83qY3/jotSduWfGx+K0rKII rUQQ== 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:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=uID8iZcNLBme4rZdpwP2elgnitNvjR/PYrEFPkEMrt4=; b=igwoHmaJ8zrp+2xO1NFGdQKVk5IyskCUB732bXa5HZKo+oevyoYtNojRIjw4/wfR0N GrgWLQjU46mzrjwtYpqLm56i3lBnp4kSp7mvs3PLRHUm1NmFloKPufdRF/Pxy28idDVP iBcIeClNJYD54elYAElEg/5Uf5Z2vW9FlCzPt3Ki9ID1/6MiifZtV8a1twbQ+lfuuJls voCuuS4ailzrRW7ltYBOslyMI6Zg/1Yb7gjKlHeBVAHSHWTbdechThrObCP9QD8Jxiqu WI1bkWWgOwFGq8L0OtL8pYtT7Qt+oowMkn1vwO4eGP+jW3FQieT4BPWKGUDx77n122di xJJg== X-Gm-Message-State: AOAM531zGkk3ZelgXBAw4jMAnDKPrpgkztOMcD4rwO6+63BpLZh/D3nX aqgDMEaJWFT6TyNogvhMd16tCQ== X-Received: by 2002:a05:6512:228d:b0:471:a5b8:d51a with SMTP id f13-20020a056512228d00b00471a5b8d51amr4118108lfu.411.1650657674555; Fri, 22 Apr 2022 13:01:14 -0700 (PDT) Received: from [192.168.1.65] ([46.188.121.177]) by smtp.gmail.com with ESMTPSA id e5-20020ac25ca5000000b004705dbcc329sm332547lfq.222.2022.04.22.13.01.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 22 Apr 2022 13:01:14 -0700 (PDT) Message-ID: <964ae72a-0484-67de-8143-a9a2d492a520@openvz.org> Date: Fri, 22 Apr 2022 23:01:12 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [PATCH memcg RFC] net: set proper memcg for net_init hooks allocations Content-Language: en-US To: Shakeel Butt Cc: Vlastimil Babka , Roman Gushchin , kernel@openvz.org, LKML , netdev , Cgroups , Michal Hocko , Florian Westphal , "David S. Miller" , Jakub Kicinski , Paolo Abeni References: <46c1c59e-1368-620d-e57a-f35c2c82084d@linux.dev> <55605876-d05a-8be3-a6ae-ec26de9ee178@openvz.org> From: Vasily Averin In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,RDNS_NONE,SPF_HELO_NONE autolearn=unavailable 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 4/21/22 18:56, Shakeel Butt wrote: > On Sat, Apr 16, 2022 at 11:39 PM Vasily Averin wrote: >> @@ -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. Could you please elaborate with more details? It seems to me mem_cgroup_from_obj() does everything exactly as you say: - for slab objects it returns memcg taken from according slab->memcg_data - for ex-slab objects (i.e. page->memcg_data & MEMCG_DATA_OBJCGS) page_memcg_check() returns NULL - for kmem objects (i.e. page->memcg_data & MEMCG_DATA_KMEM) page_memcg_check() returns objcg->memcg - in another cases page_memcg_check() returns page->memcg_data, so for uncharged objects like init_net NULL should be returned. I can introduce exported get_mem_cgroup_from_kmem(), however it should only call mem_cgroup_from_obj(), perhaps under read_rcu_lock/unlock. Do you mean something like this? --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1768,4 +1768,14 @@ 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(); + memcg = mem_cgroup_from_obj(p); + 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..4003c47965c9 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,14 @@ 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 == NULL) + memcg = root_mem_cgroup; + 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);