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=-23.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_IN_DEF_DKIM_WL 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 D4EA9C433E0 for ; Tue, 16 Feb 2021 17:18:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8633C64E09 for ; Tue, 16 Feb 2021 17:18:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8633C64E09 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 22FBE6B0005; Tue, 16 Feb 2021 12:18:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1DFA56B006C; Tue, 16 Feb 2021 12:18:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F65F6B006E; Tue, 16 Feb 2021 12:18:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0119.hostedemail.com [216.40.44.119]) by kanga.kvack.org (Postfix) with ESMTP id EE5446B0005 for ; Tue, 16 Feb 2021 12:18:01 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id B95628248047 for ; Tue, 16 Feb 2021 17:18:01 +0000 (UTC) X-FDA: 77824788762.04.ABBC70C Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) by imf20.hostedemail.com (Postfix) with ESMTP id 26D40DE for ; Tue, 16 Feb 2021 17:17:58 +0000 (UTC) Received: by mail-lf1-f49.google.com with SMTP id v5so17002775lft.13 for ; Tue, 16 Feb 2021 09:18:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=TAIlfyj1+vl6wEY03t8gQIxJzlIrwjx8g7cV2iEl46I=; b=ajc+Ly1Kl3BQHBgTjkn+/wNh48GTRtZTdxzUnLJIhxY4ZFdqnb9AYLaI2XXT/ZVLrv jyngOR5w7dsP5aWWRcFYUODEcZN65i1edh1DfsByO6EIB6AZ32Cuj5vOz3wZdy9r1zNg Y1RHti10VjznC2eoihl0UL/cr+IAYRk25Oczds3MyxDy/ygc6AF2oSQvlGSgLRUMpRT6 uFlUP8t+nHMnChWyq4NleRHpwctatl95VSvcqZDV/sKZrHxa/2AT/Si5DDZkL/fAhjxt c/Fyll05dd/xV/YbRAi7tz/eBZ8bA2MyA50QOh9upkC1Ic6UaOz9uHizMUsD+L4RszQ6 Mfpw== 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=TAIlfyj1+vl6wEY03t8gQIxJzlIrwjx8g7cV2iEl46I=; b=dzEITntpfUi1qQbOC6wBT5KXlaJk+p9iKRdXc5Z0j5qUE0inz4oQoNM+TNdVaVTuxT 9SoJh3Gatvc+l545S/1KiXCgKpkhBOyORJJN8+fWPxswE1ZjE3NYPaVZOypUA9Yd8ns4 iFOdaUGHC9pm3MosheDlUz1/UhpQO+YRASMEP/JI6NbYZXN3yglH18KYT/BNxH0F+z4O QnADBFPt6LxTqXwf/V7emf1H3wEDkgQZ6AEDyN3DUBOdOYj1487uj+dhelxUrRw3OjCQ ISk28fyEBpV148XT35soPBkGhgks43MqOXBKLR6sfHv4DVec/T+qerOwB1Ck7dFRtDct j2mA== X-Gm-Message-State: AOAM5335mTOjG7sDB1kdM3n+FBN8Wcc/Yzh7g+r5hpyWIrwnuT7Q5E3L aPTz3ONvyXqv4tqOuN5KBjulZNtM9ZuDnvMdD8hXkw== X-Google-Smtp-Source: ABdhPJwONFMMXGr4fcY9J8n+r2ABk3IrHz+j4bqcUiSnw+UBoHcr+mheWKHCl5jqA0yctFSkbQDCLgqmr3CiqcS7MUw= X-Received: by 2002:a05:6512:6c6:: with SMTP id u6mr12389695lff.347.1613495879192; Tue, 16 Feb 2021 09:17:59 -0800 (PST) MIME-Version: 1.0 References: <20210212170159.32153-1-songmuchun@bytedance.com> <20210212170159.32153-4-songmuchun@bytedance.com> In-Reply-To: From: Shakeel Butt Date: Tue, 16 Feb 2021 09:17:45 -0800 Message-ID: Subject: Re: [PATCH 4/4] mm: memcontrol: fix swap uncharge on cgroup v2 To: Johannes Weiner Cc: Muchun Song , Michal Hocko , Vladimir Davydov , Andrew Morton , Cgroups , Linux MM , LKML Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 26D40DE X-Stat-Signature: 6zy37n471kr14qw4c1idebyo8bm7t3q3 Received-SPF: none (google.com>: No applicable sender policy available) receiver=imf20; identity=mailfrom; envelope-from=""; helo=mail-lf1-f49.google.com; client-ip=209.85.167.49 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1613495878-10867 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 Tue, Feb 16, 2021 at 9:05 AM Johannes Weiner wrote: > > Hello Muchun, > > On Sat, Feb 13, 2021 at 01:01:59AM +0800, Muchun Song wrote: > > The swap charges the actual number of swap entries on cgroup v2. > > If a swap cache page is charged successful, and then we uncharge > > the swap counter. It is wrong on cgroup v2. Because the swap > > entry is not freed. > > The patch makes sense to me. But this code is a bit tricky, we should > add more documentation to how it works and what the problem is. > > How about this for the changelog? > > --- > mm: memcontrol: fix swap undercounting for shared pages in cgroup2 > > When shared pages are swapped in partially, we can have some page > tables referencing the in-memory page and some referencing the swap > slot. 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 consumed swap when shared > pages are partially swapped back in. This in turn allows a cgroup to > consume more swap than its configured limit intends. > > 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 With Johannes's suggestions: Reviewed-by: Shakeel Butt > > > --- > > mm/memcontrol.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > index c737c8f05992..be6bc5044150 100644 > > --- a/mm/memcontrol.c > > +++ b/mm/memcontrol.c > > @@ -6753,7 +6753,7 @@ 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)) { > > + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && PageSwapCache(page)) { > > It's more descriptive to use do_memsw_account() here, IMO. > > We should also add a comment. How about this above the branch? > > /* > * 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 > * for shared pages 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. > */