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=-3.6 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=no 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 53195C388F7 for ; Fri, 6 Nov 2020 02:09:56 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8CA8520735 for ; Fri, 6 Nov 2020 02:09:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OpuvIjxx" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8CA8520735 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9F78E6B0036; Thu, 5 Nov 2020 21:09:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A7456B005C; Thu, 5 Nov 2020 21:09:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8950B6B005D; Thu, 5 Nov 2020 21:09:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0219.hostedemail.com [216.40.44.219]) by kanga.kvack.org (Postfix) with ESMTP id 520136B0036 for ; Thu, 5 Nov 2020 21:09:54 -0500 (EST) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id E6891181AEF07 for ; Fri, 6 Nov 2020 02:09:53 +0000 (UTC) X-FDA: 77452362666.11.lake36_410b6b0272ce Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin11.hostedemail.com (Postfix) with ESMTP id C4809180F8B80 for ; Fri, 6 Nov 2020 02:09:53 +0000 (UTC) X-HE-Tag: lake36_410b6b0272ce X-Filterd-Recvd-Size: 5727 Received: from mail-io1-f66.google.com (mail-io1-f66.google.com [209.85.166.66]) by imf01.hostedemail.com (Postfix) with ESMTP for ; Fri, 6 Nov 2020 02:09:53 +0000 (UTC) Received: by mail-io1-f66.google.com with SMTP id s10so3928289ioe.1 for ; Thu, 05 Nov 2020 18:09:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=qwny6o4PGTE4NfHXqynmbmdsQhAv/AA5jG1feD10tiE=; b=OpuvIjxxaanZfN5EhKoAWkw/eTyk/4DuMry7bUU0xBiNF3QljN75KclPLSKkAKlPf3 oiYW6Pdw7GkwhIyPMR00u9zRNe7hwmoBwxfVnK0OA6bGaThw9zHNmgizQfni6Wv6r/50 8MiH5Eq9xxtbOIiZuExtBQZVewi9LpwNbJbBNLUh+swy6/lJY8C8opwC8iF1gqizBTyK aooHSPhwdGB0/m1sjo4g4yX89DuPfnTvPsIGKZlNHpZ2OuR8xnfDbERwKk6JYub8h6qJ IuEzWWyDBTrWyz3Ih0IKwGr0r8X8wdoV2vybAvgo22gFSh7spbhEPaIxKjvadRVYmLUv etmw== 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=qwny6o4PGTE4NfHXqynmbmdsQhAv/AA5jG1feD10tiE=; b=Fq2lcWLVt8aMRVqL/gfTgyoBl4fGCTYPE2PDp8daS8RJhwbLczQAGOhw8irdonQGlK ps3GirOfmPE/PXfkugZ/n4GfRoSLyM6rzNra6LyfBmhXCp62Yvm1uTYagnCZKWcdAvGc 3fhU0cDSYN/36GGJ5DIg4mbkDuI46KyKco/G8mxgCbB62YLpeIpnqZeI+l1pnvOHEvWU QZQLegd+THZgH2n3K7I4OwjkaxTQqRHHJPrb/DnVpN9rGxal2jc5GlRuFrnoADQpxovy 97RNGxQ6ZkgT/5Ld+cV+Q0agwBx5QH72E//Bvi+1nGGaZ4sLC7uJYKaXIWf/1xU4Wc+B S/Ew== X-Gm-Message-State: AOAM531Vkq4PBb8ttJte3iRq3rr7zHvsXzLreRsglzoUb9WOZ3lZzWyC 0IXp2TkAhs4L4hRN4tAvm4KbIKH8B1Tr2YJUem8= X-Google-Smtp-Source: ABdhPJwVj0RYFC15D98Y0HvhZVx5q5/0awYkgfV/MPkmxPLejAV362dNq5yDnQDhpDEEuaLm37fLB7NSJY5NOrYpaLo= X-Received: by 2002:a02:c7c1:: with SMTP id s1mr4324087jao.94.1604628592714; Thu, 05 Nov 2020 18:09:52 -0800 (PST) MIME-Version: 1.0 References: <20201105131012.82457-1-laoar.shao@gmail.com> <20201105162219.GG744831@cmpxchg.org> In-Reply-To: <20201105162219.GG744831@cmpxchg.org> From: Yafang Shao Date: Fri, 6 Nov 2020 10:09:16 +0800 Message-ID: Subject: Re: [PATCH] mm: account lazily freed anon pages in NR_FILE_PAGES To: Johannes Weiner Cc: Andrew Morton , Michal Hocko , minchan@kernel.org, Linux MM Content-Type: text/plain; charset="UTF-8" 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 Fri, Nov 6, 2020 at 12:24 AM Johannes Weiner wrote: > > On Thu, Nov 05, 2020 at 09:10:12PM +0800, Yafang Shao wrote: > > The memory utilization (Used / Total) is used to monitor the memory > > pressure by us. If it is too high, it means the system may be OOM sooner > > or later when swap is off, then we will make adjustment on this system. > > > > However, this method is broken since MADV_FREE is introduced, because > > these lazily free anonymous can be reclaimed under memory pressure while > > they are still accounted in NR_ANON_MAPPED. > > > > Furthermore, since commit f7ad2a6cb9f7 ("mm: move MADV_FREE pages into > > LRU_INACTIVE_FILE list"), these lazily free anonymous pages are moved > > from anon lru list into file lru list. That means > > (Inactive(file) + Active(file)) may be much larger than Cached in > > /proc/meminfo. That makes our users confused. > > > > So we'd better account the lazily freed anonoymous pages in > > NR_FILE_PAGES as well. > > What about the share of pages that have been reused? After all, the > idea behind deferred reclaim is cheap reuse of already allocated and > faulted in pages. > I missed the reuse case. Thanks for the explanation. > Anywhere between 0% and 100% of MADV_FREEd pages may be dirty and need > swap-out to reclaim. That means even after this patch, your formula > would still have an error margin of 100%. > > The tradeoff with saving the reuse fault and relying on the MMU is > that the kernel simply *cannot do* lazy free accounting. Userspace > needs to do it. E.g. if a malloc implementation or similar uses > MADV_FREE, it has to keep track of what is and isn't used and make > those stats available. > > If that's not practical, That is not practical. The process which uses MADV_FREE can keep track of it, but other processes like monitor tools have no easier way to keep track of it. We can't give the userspace trouble. > I don't see an alternative to trapping minor > faults upon page reuse, eating the additional TLB flush, and doing the > accounting properly inside the kernel. > I will try to analyze the details and find whether there is some way to track it in the kernel. > > @@ -1312,8 +1312,13 @@ static void page_remove_anon_compound_rmap(struct page *page) > > if (unlikely(PageMlocked(page))) > > clear_page_mlock(page); > > > > - if (nr) > > - __mod_lruvec_page_state(page, NR_ANON_MAPPED, -nr); > > + if (nr) { > > + if (PageLRU(page) && PageAnon(page) && !PageSwapBacked(page) && > > + !PageSwapCache(page) && !PageUnevictable(page)) > > + __mod_lruvec_page_state(page, NR_FILE_PAGES, -nr); > > + else > > + __mod_lruvec_page_state(page, NR_ANON_MAPPED, -nr); > > I don't think this would work. The page can be temporarily off-LRU for > compaction, migration, reclaim etc. and then you'd misaccount it here. Right, thanks for the clarification. -- Thanks Yafang