Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp5863667pxb; Tue, 16 Feb 2021 09:20:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJwvDIS6FDKu6RG9i8NB8cvUSgsbF+u4tPNyTEjv0/v4bkhdohKX16NcZomIg85gzZAcKh0g X-Received: by 2002:a17:907:3e06:: with SMTP id hp6mr21227689ejc.254.1613496019297; Tue, 16 Feb 2021 09:20:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613496019; cv=none; d=google.com; s=arc-20160816; b=AArHC8P9IvCwDZX2L/pD9kGY9ZXko0EyovN9Lm2Rj9KraBiCOdsuShKIrfZgiRIkcp KGAzS8vTII0hVVeqT7GF2FdQzgEQRTdYEp903cvflXzVwqwLEiMzTSPp6FjE2THkFo8S VJbUPuRQQFVTks+cCdlr5bNmhaCuBRVXmRV8RMpWtLbGWbmONLK7bCVTSRQTuSSo9iHT eNCga/57YQBjyre1UXYrTIX3yOrnBexBrqyhcOFFEyqvyf9IQIRKBZFQLe5KBfSogs9f L+DdnG9uMPmuwaEF7Sjsch3LMWEE1/1UtKQnyE9ea2WOKaXupjmq//JAIPKcQzOtOKG6 z3pw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=TAIlfyj1+vl6wEY03t8gQIxJzlIrwjx8g7cV2iEl46I=; b=Lu3bPWResRApsLT65XAAzzKC7LCsSR/rpQAcuIVckiO7jxj5yDX5c7gehrQXKW2sm6 ylVWssn02TEXownbhIeRKHsBUK56gjRes3/Coa02FilP9Rrd1F9Bb14T6/I1P21WcblQ LCqYv/170gjw2CQtmlHC1wDGFW3kpUTe0/PMz5hOqcd2QYi6RxneBhyLUVQDaVsxwC8+ HddD3DoG6KYtXkCjEmqNa+gnCLtaj+x4+VWa3xI8fvUg0rzaiHrS64XpRkQ4mQGK1Ma6 pjeEu/UhYX/9ysEX2LSgyEiMNVunWv17o/Dz4lOF5+r3bxWg7ZAw6g5z8cUNNkXLrTC/ cBxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ajc+Ly1K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dc19si13826570edb.348.2021.02.16.09.19.55; Tue, 16 Feb 2021 09:20:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ajc+Ly1K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230398AbhBPRSr (ORCPT + 99 others); Tue, 16 Feb 2021 12:18:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230362AbhBPRSn (ORCPT ); Tue, 16 Feb 2021 12:18:43 -0500 Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89A4FC0613D6 for ; Tue, 16 Feb 2021 09:18:02 -0800 (PST) Received: by mail-lf1-x12c.google.com with SMTP id u4so10059837lfs.0 for ; Tue, 16 Feb 2021 09:18:02 -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=WblKG1V7rAVY2P8wvoMZnTX7IKZSYGEWmSbQloGy5h7G+aM5dFZCRyg+p3cEX6sssW i1yD89mRMfACjTmHYrN5duVhiC0CrRfYx2ZhWzJCyQynV4K5KZc6dTN5zUpFbxp9KfXU 5rAQeJkoKFlheTQtKXwTgHdwWQIIFeeJgskObmcwuAbfGu/pVhlxBFay3Q24+YtdxTeP HhlxHCx2AnKv+RHXOwgCvjmYbPmgGDWmUSr/UuY+PXT6Mffs1fJcY6yMAQN8PVw0Ewhw 4rlhCgeB9CK4SH0mDj+5wA7Z1Xlcbc8uM1UCftmFyC8ZJiYHmG7tYyabIgVdiUxnsA4C /lLQ== X-Gm-Message-State: AOAM533j445pEjhGuAAceoR6JZFNgoah1MQK8CE32U+2wcT74RjCMwtM NFwYWN+xZYN1W2gpsPkwL2k1KWYiGL/RjM6VKUZCnw== 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" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. > */