From patchwork Tue Mar 2 08:18:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12111135 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1D6EDC433DB for ; Tue, 2 Mar 2021 08:19:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1D19664DE8 for ; Tue, 2 Mar 2021 08:19:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1D19664DE8 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 219238D00E7; Tue, 2 Mar 2021 03:19:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A2668D0063; Tue, 2 Mar 2021 03:19:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0918A8D00E7; Tue, 2 Mar 2021 03:19:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0055.hostedemail.com [216.40.44.55]) by kanga.kvack.org (Postfix) with ESMTP id E79748D0063 for ; Tue, 2 Mar 2021 03:19:56 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id BA256582D for ; Tue, 2 Mar 2021 08:19:56 +0000 (UTC) X-FDA: 77874235992.21.4E01EA2 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) by imf09.hostedemail.com (Postfix) with ESMTP id 0D92D60024A2 for ; Tue, 2 Mar 2021 08:19:54 +0000 (UTC) Received: by mail-pj1-f54.google.com with SMTP id i14so1415763pjz.4 for ; Tue, 02 Mar 2021 00:19:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=SDK+ajP5jBELnVhiWbUw5itoPVWVT0C+pRbdXA2pXzw=; b=M3wbBHDNI7w7ke6LqBh7MD9RPOf01wSC97wdKQu8q/EV8CoQAYCAM5Uz6qMUDtCjlB VKUdBCfqVmvLMBlHAFw/cxYqPbEPz3v5na3AuCoqbgYbRQJ8kQ63GP36LAPlcC8u+eFa Pkp8UeN6hz2syWJWH+RjC/NInThhKhUk3G0G5Kae/UC4hlKG4eRpRNcU5LO8MxEOTuzD 0VcAWzogWN0HbGOSEolJcws8s0ShmixujgKl1EB/eZRBtCJlpf2jDuAEaXYlQGnFYzer Qm5g8vntBUDyn68OfHHlapgG6k8I2bkXdDWzJnAnIN+JKHmiJTYbrqRfJsgwZbpAu8kJ 2tqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=SDK+ajP5jBELnVhiWbUw5itoPVWVT0C+pRbdXA2pXzw=; b=GEwHzZbdwPE2TAylBlvCHb6I6sLthXlMLwBuRpo649nqxJPh+6W0+Iw37ICkA1NrYb Wby1Dlj1GQgorYtPbSnophFImGwHi+H27RSrJ5TRw+9/YnI1XtPA4fKLvA2rZdXMfZ0g w7Xkx1cHlUTyY4Qx6lVhMh4zav4H/IgS5nJRZR0pyy8BDes4esNbn0u7gRv3FPyNVrPX p8UYn7P6DHQtYQnSLgjzzJJ1dGITOVB12swXEyrKIdKenlumJuPmQsflAFk8KMyujVQG idbj2FIcgq/++VHzPkrNHd5grkMg490ndYNHgOmnhGD67Z+6fDuEQB8WsqNbfIAYDM6f sRqg== X-Gm-Message-State: AOAM530Cu5lj5PvVZqjmnwU5UgBzRX4dUZqidVPk5uCLnxC+us8x9Zpf t6Zt3+nL7w5K9pSWQYRN1/RWZw== X-Google-Smtp-Source: ABdhPJyB3ADWISJa4UEPElc5MoA53ychWV9OqL0XQVQ3l/BtTJlO7Ggf2HeajEQSQxgg06tQnj/wXQ== X-Received: by 2002:a17:902:7d83:b029:e4:45ca:5407 with SMTP id a3-20020a1709027d83b02900e445ca5407mr19052690plm.57.1614673194229; Tue, 02 Mar 2021 00:19:54 -0800 (PST) Received: from localhost.localdomain ([139.177.225.227]) by smtp.gmail.com with ESMTPSA id j20sm2157781pjn.27.2021.03.02.00.19.50 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Mar 2021 00:19:53 -0800 (PST) From: Muchun Song To: guro@fb.com, hannes@cmpxchg.org, mhocko@kernel.org, akpm@linux-foundation.org, shakeelb@google.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Muchun Song Subject: [PATCH] mm: memcontrol: fix root_mem_cgroup charging Date: Tue, 2 Mar 2021 16:18:23 +0800 Message-Id: <20210302081823.9849-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 0D92D60024A2 X-Stat-Signature: 6w67sx69w5ciih59rst6t89mz5tuwqp9 Received-SPF: none (bytedance.com>: No applicable sender policy available) receiver=imf09; identity=mailfrom; envelope-from=""; helo=mail-pj1-f54.google.com; client-ip=209.85.216.54 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1614673194-291727 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: CPU0: CPU1: objcg = get_obj_cgroup_from_current(); obj_cgroup_charge(objcg); memcg_reparent_objcgs(); xchg(&objcg->memcg, root_mem_cgroup); // memcg == root_mem_cgroup memcg = obj_cgroup_memcg(objcg); __memcg_kmem_charge(memcg); // Do not charge to the root memcg try_charge(memcg); If the objcg->memcg is reparented to the root_mem_cgroup, obj_cgroup_charge() can pass root_mem_cgroup as the first parameter to here. The root_mem_cgroup is skipped in the try_charge(). So the page counters of it do not update. When we uncharge this, we will decrease the page counters (e.g. memory and memsw) of the root_mem_cgroup. This will cause the page counters of the root_mem_cgroup to be out of balance. Fix it by charging the page to the root_mem_cgroup unconditional. Fixes: bf4f059954dc ("mm: memcg/slab: obj_cgroup API") Signed-off-by: Muchun Song --- mm/memcontrol.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 2db2aeac8a9e..edf604824d63 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3078,6 +3078,19 @@ static int __memcg_kmem_charge(struct mem_cgroup *memcg, gfp_t gfp, if (ret) return ret; + /* + * If the objcg->memcg is reparented to the root_mem_cgroup, + * obj_cgroup_charge() can pass root_mem_cgroup as the first + * parameter to here. We should charge the page to the + * root_mem_cgroup unconditional to keep it's page counters + * balance. + */ + if (unlikely(mem_cgroup_is_root(memcg))) { + page_counter_charge(&memcg->memory, nr_pages); + if (do_memsw_account()) + page_counter_charge(&memcg->memsw, nr_pages); + } + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !page_counter_try_charge(&memcg->kmem, nr_pages, &counter)) {