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 X-Spam-Level: X-Spam-Status: No, score=-13.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 4FDDAC433E0 for ; Wed, 17 Feb 2021 15:30:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9FAED64DA1 for ; Wed, 17 Feb 2021 15:30:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9FAED64DA1 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 21CCE6B0006; Wed, 17 Feb 2021 10:30:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A6936B006C; Wed, 17 Feb 2021 10:30:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0700A6B006E; Wed, 17 Feb 2021 10:30:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0095.hostedemail.com [216.40.44.95]) by kanga.kvack.org (Postfix) with ESMTP id E1D716B0006 for ; Wed, 17 Feb 2021 10:30:05 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id AC2BF8248D51 for ; Wed, 17 Feb 2021 15:30:05 +0000 (UTC) X-FDA: 77828145570.18.4C1480F Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf13.hostedemail.com (Postfix) with ESMTP id A7569E0001AF for ; Wed, 17 Feb 2021 15:29:57 +0000 (UTC) Received: by mail-pl1-f171.google.com with SMTP id r2so7570890plr.10 for ; Wed, 17 Feb 2021 07:29:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=IEvlIwbmXsuqHNUtpKs8VTQydOk5VI0GaImfE0fmN4U=; b=ask1qaCwK4yLcterujoc3O4p6eeRzLfX0AHVU+3Twr04DTtgqRjpmjFHGq2CBjTJGo b7DXnzAUm1D+aG2t9O2EbR9Xxxmo9BScuE6UsdB5rZDgm7sexqLt8qKvLT3glyLd9duu k1svoB2F2tV6grWbIEeSFe2J2WTRaC992C+DybwmjArIQ8vWwLH7rhei1paBY9XOgQCx bY2MNfyv2ccyvFZFMW7KY3ERiAhM0KgXXuVpa6NKehNhnI1QaFyFCKOqNbPU2niQzx2O I1SvEO2oYKWVfMmxTNM3wQtllDP31zJakKBjKYYGJmeC3OuVEl8iNrixWGvt3NyJP36z 38sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=IEvlIwbmXsuqHNUtpKs8VTQydOk5VI0GaImfE0fmN4U=; b=JfpkzULIsLxalqb6odDt6OH2GgFQnmSX1plQZeCyr1JrTVDpmZW0SwDDiQewyLy6Fn MXutpxE4b8XCOpWT0dJfFg6sjf/GeYNVMq/f1Qx/YUzas+aC5KKMfIqM0zNvoGMkNWEH RXEMHj62nkSAsv9XeFjJWr9oXkhiXpTsXs3QDpgTT7dOWqY1h0nsmHUY8Hi2w5eiT3Fr gU9FWBvgRLDP9Mbem3zd+225DlzluTt9ERvEcbWeuNUBPWMIG3Q5g+TeGVz9T1T2wFmW 1fiZMSt8v9DA/9SwR63+oqPWiCOzNW++I+3zpudlQtiprin1sjPi1YnRAFql68PMi1WA t0Cg== X-Gm-Message-State: AOAM5323FK8hs4WQyIYDMdTKF9d2cGmZEY/GTwyuAt+N9sqwKzYe+vrB i6rlmcQYTM89et5QH8pcLrsdCe6gHlMN2VnKIZXQ9w== X-Google-Smtp-Source: ABdhPJxFmrP4b85rMr9HbCrLZ2cfXGI2IgaAP5+1FXvM7C9T2HmvdP3BP8c2mqbGb9N21VXe/tL7GudKYMkuFEX05+I= X-Received: by 2002:a17:90b:1096:: with SMTP id gj22mr9552250pjb.229.1613575797397; Wed, 17 Feb 2021 07:29:57 -0800 (PST) MIME-Version: 1.0 References: <20210217110907.85120-1-songmuchun@bytedance.com> In-Reply-To: From: Muchun Song Date: Wed, 17 Feb 2021 23:29:21 +0800 Message-ID: Subject: Re: [External] Re: [PATCH v2] mm: memcontrol: fix swap undercounting in cgroup2 To: Shakeel Butt Cc: Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Cgroups , Linux MM , LKML , Michal Hocko , linux- stable Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: qi9pj3t64wkqtyjcniyixn1bkkpm47o5 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: A7569E0001AF Received-SPF: none (bytedance.com>: No applicable sender policy available) receiver=imf13; identity=mailfrom; envelope-from=""; helo=mail-pl1-f171.google.com; client-ip=209.85.214.171 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1613575797-475794 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: On Wed, Feb 17, 2021 at 10:39 PM Shakeel Butt wrote: > > On Wed, Feb 17, 2021 at 3:09 AM Muchun Song wrote: > > > > When pages are swapped in, the VM may retain the swap copy to avoid > > repeated writes in the future. It's also retained if shared pages are > > faulted back in some processes, but not in others. During that time we > > have an in-memory copy of the page, as well as an on-swap copy. Cgroup1 > > and cgroup2 handle these overlapping lifetimes slightly differently > > due to the nature of how they account memory and swap: > > > > Cgroup1 has a unified memory+swap counter that tracks a data page > > regardless whether it's in-core or swapped out. On swapin, we transfer > > the charge from the swap entry to the newly allocated swapcache page, > > even though the swap entry might stick around for a while. That's why > > we have a mem_cgroup_uncharge_swap() call inside mem_cgroup_charge(). > > > > Cgroup2 tracks memory and swap as separate, independent resources and > > thus has split memory and swap counters. On swapin, we charge the > > newly allocated swapcache page as memory, while the swap slot in turn > > must remain charged to the swap counter as long as its allocated too. > > > > The cgroup2 logic was broken by commit 2d1c498072de ("mm: memcontrol: > > make swap tracking an integral part of memory control"), because it > > accidentally removed the do_memsw_account() check in the branch inside > > mem_cgroup_uncharge() that was supposed to tell the difference between > > the charge transfer in cgroup1 and the separate counters in cgroup2. > > > > As a result, cgroup2 currently undercounts retained swap to varying > > degrees: swap slots are cached up to 50% of the configured limit or > > total available swap space; partially faulted back shared pages are > > only limited by physical capacity. This in turn allows cgroups to > > significantly overconsume their alloted swap space. > > > > Add the do_memsw_account() check back to fix this problem. > > > > Fixes: 2d1c498072de ("mm: memcontrol: make swap tracking an integral part of memory control") > > Signed-off-by: Muchun Song > > Acked-by: Johannes Weiner > > Reviewed-by: Shakeel Butt > > Acked-by: Michal Hocko > > Cc: stable@vger.kernel.org # 5.8+ > > --- > > v2: > > - update commit log and add a comment to the code. Very thanks to Johannes. > > > > mm/memcontrol.c | 14 +++++++++++++- > > 1 file changed, 13 insertions(+), 1 deletion(-) > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > index ed5cc78a8dbf..2efbb4f71d5f 100644 > > --- a/mm/memcontrol.c > > +++ b/mm/memcontrol.c > > @@ -6771,7 +6771,19 @@ int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask) > > memcg_check_events(memcg, page); > > local_irq_enable(); > > > > - if (PageSwapCache(page)) { > > + /* > > + * Cgroup1's unified memory+swap counter has been charged with the > > + * new swapcache page, finish the transfer by uncharging the swap > > + * slot. The swap slot would also get uncharged when it dies, but > > + * it can stick around indefinitely and we'd count the page twice > > + * the entire time. > > + * > > + * Cgroup2 has separate resource counters for memory and swap, > > + * so this is a non-issue here. Memory and swap charge lifetimes > > + * correspond 1:1 to page and swap slot lifetimes: we charge the > > + * page to memory here, and uncharge swap when the slot is freed. > > + */ > > + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && PageSwapCache(page)) { > > do_memsw_account() instead of !cgroup_subsys_on_dfl(memory_cgrp_subsys). Thanks a lot. Will do. > > > swp_entry_t entry = { .val = page_private(page) }; > > /* > > * The swap entry might not get freed for a long time, > > -- > > 2.11.0 > >