From nobody Mon May 11 02:56:30 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 8042BC433EF for ; Sun, 17 Apr 2022 06:39:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233502AbiDQGmR (ORCPT ); Sun, 17 Apr 2022 02:42:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231481AbiDQGmP (ORCPT ); Sun, 17 Apr 2022 02:42:15 -0400 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 062AA2B24D for ; Sat, 16 Apr 2022 23:39:41 -0700 (PDT) Received: by mail-lf1-x12a.google.com with SMTP id bq30so20002193lfb.3 for ; Sat, 16 Apr 2022 23:39:40 -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=bT5KP076LNXAkBuT6bxsWqWs6Rnf/a89eLMuVK8//BE=; b=ncW0cuAMb8hBdKsyH8A3kwzpFlf5AjHuKNEWWaWOGkn5vtR0OwYflZ/UOyblXqRhoX kbsctJzKaEA9bWHOVkTV9KuCRQcRcOgvcUf41KIzu3vHK2W9CC4bp4s9AHaibfLLOQjS bT1e05vF0QYw5pL8iOoteYlboX9TqJwxMFp/G4XDGLWUN1+Kmy6fQdP8Kl/4NuFsySkn HzOuRVfYzz0kv3CUSF1d0XcVL91nMfwKTE/4x0Inr2clsZHHMcHM8uhguc3zAzvEA9cl WWfK9ozD2CGno/6VT0O3Ng+LCRT/WPIh/FlwpOJ46w4aohgfKTn2GFxLEpSzV5i7vrat rK0w== 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=bT5KP076LNXAkBuT6bxsWqWs6Rnf/a89eLMuVK8//BE=; b=pdzvKYqdazTDKtuh92FA0ixRFd0BO8lAgJFYJYWhGn9hbxzsQXDhV5xZzyqCiNrWcF RVx/vkSqr/Za/ydadK0RftNyd3VH9YlkXDLHA2j+N7KPAeEs0zjgH+BfMWeABJ6cnBBd WnZbTQ37NvnMEO2rXswMX0115CrIB9PbSC2G6gk41Ak0SKdUSQvL8nW8lq655gd3JpHS fLbQqGtqkGhs9LtzTVzm6+PUzzMEt2r7CDWLPRiGn2UnMOhbP9KiPgz4F2WstJBcKxk8 B94dUMPJaGHIU6f7u1inrqGJDSoo/6J0L1Cor90FLDeVX9Wpa9qxJrMT3n941t+t8+6J Vvtw== X-Gm-Message-State: AOAM5322AdH8bicX80enP7tGqDfEmhJN9X8dozh5faLyf9MMo+cVKARN FIuzqV3UjAj3wcAkMccA9XzgQg== X-Google-Smtp-Source: ABdhPJyDayD1yzsslV7LgR6+8/ExlagCq2BG2TF0dic2T/PuIDSyOXUYXztnIZD0s2fB6srWdeqxNQ== X-Received: by 2002:a05:6512:1326:b0:45a:3a4:f25a with SMTP id x38-20020a056512132600b0045a03a4f25amr4319176lfu.575.1650177579195; Sat, 16 Apr 2022 23:39:39 -0700 (PDT) Received: from [192.168.1.65] ([46.188.121.177]) by smtp.gmail.com with ESMTPSA id x40-20020a056512132800b004489691436esm872040lfu.146.2022.04.16.23.39.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 16 Apr 2022 23:39:38 -0700 (PDT) Message-ID: <55605876-d05a-8be3-a6ae-ec26de9ee178@openvz.org> Date: Sun, 17 Apr 2022 09:39:37 +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 memcg RFC] net: set proper memcg for net_init hooks allocations To: Vlastimil Babka , Roman Gushchin Cc: kernel@openvz.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, cgroups@vger.kernel.org, Shakeel Butt , Michal Hocko , Florian Westphal , "David S. Miller" , Jakub Kicinski , Paolo Abeni References: <46c1c59e-1368-620d-e57a-f35c2c82084d@linux.dev> Content-Language: en-US In-Reply-To: <46c1c59e-1368-620d-e57a-f35c2c82084d@linux.dev> 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 --- Dear Vlastimil, Roman, I'm not sure that memcg is used correctly here,=20 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 =20 +#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 =3D NULL; +#ifdef CONFIG_MEMCG + memcg =3D (net =3D=3D &init_net) ? root_mem_cgroup : mem_cgroup_from_ob= j(net); +#endif + old =3D set_active_memcg(memcg); error =3D ops_init(ops, net); + set_active_memcg(old); if (error) goto out_undo; list_add_tail(&net->exit_list, &net_exit_list); --=20 2.31.1