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=-2.8 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 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 0F335C433FE for ; Thu, 23 Sep 2021 21:55:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8FC446124B for ; Thu, 23 Sep 2021 21:55:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8FC446124B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id D1841900002; Thu, 23 Sep 2021 17:55:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CC7546B0071; Thu, 23 Sep 2021 17:55:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB5CA900002; Thu, 23 Sep 2021 17:55:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0078.hostedemail.com [216.40.44.78]) by kanga.kvack.org (Postfix) with ESMTP id A93E66B006C for ; Thu, 23 Sep 2021 17:55:05 -0400 (EDT) Received: from smtpin35.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 628582FE10 for ; Thu, 23 Sep 2021 21:55:05 +0000 (UTC) X-FDA: 78620194170.35.DA1F971 Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) by imf11.hostedemail.com (Postfix) with ESMTP id 1CDACF0000AE for ; Thu, 23 Sep 2021 21:55:04 +0000 (UTC) Received: by mail-ed1-f53.google.com with SMTP id dj4so28416923edb.5 for ; Thu, 23 Sep 2021 14:55:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=yyaukEdoY1z1aKx24A8HjbLfYl3/26h3pGEVti21r2o=; b=bKLgDd3gQ2vsGxxodi83AcpnShI19aXFzITFGpUC2/B67N5nRW6MaS08TQdLmLVo83 dBB+XTXDeuHIPIanceHrTOUj5vutRzEaQj6omoDdSuzoeGwovop/2B/HZv4KRuqefaVx Im1U8ox47krXE91N/YcU8kq8tMQ7jR2nmyMzlEDeZfz/StqAQ+zIGR1rN+Oq5bT4JOAD vMm5vJbHE+eH0J0TCeMGFJrWnOSQj7kz17jq11hPCWN/14eHVWmcwMxr25E8JTHnZOrj fspqS8rznTsBkITGFIt53bpkM/AWUcbZVHXi0e7yHvpPr5krdJwzNklaP/1oanvW27Hu k2xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=yyaukEdoY1z1aKx24A8HjbLfYl3/26h3pGEVti21r2o=; b=e0pB5qk4f2BmNP7VczdnUCC8rusNoUQS7em4vD0oEnAEqfpTWAKNwcunRLcdUMJujz EKopceSHj/qTAcVQavvsIz+wj1GwRyvzo72C6xHw+EJUGUT2HeUqdDnK2dGpdp9GsPc5 FvRXm30eWPT/2QJX/beNu96ehWsn1FhDYL+Wit8Mlr+PCeCehRZyjmCDRq5eZW1hXZKK pdtkFk2sUdI6RIT6ugQBqKPclqSkmNrPhCuIW8IXsrZ/XfnBEuHB1JNe+WxuXr8B5Duf d1NLPjdJhqbeqP9+LrEi30FwUV0bR5QeQ/tIPqkYGnZsL4zTe4x0Kx7TdN4Pz4pYq/6e am9w== X-Gm-Message-State: AOAM533UqGCgmORQYBv0+6uKBi9NeCRFLAKfoBlxNWR5F4aKQ4jK7g+S dL38jJ8uejaYlJ8X8HtjNrUrtRWUHXlaJtqg4rs= X-Google-Smtp-Source: ABdhPJzvRBl4386O9mXbGkypIExS2NjtQkuuCYFediqBv/zuOYi/yQPY0oXVlOJmvbveYcJbHdVj85A5jW+HBRJMugw= X-Received: by 2002:a05:6402:14c3:: with SMTP id f3mr1389088edx.312.1632434103740; Thu, 23 Sep 2021 14:55:03 -0700 (PDT) MIME-Version: 1.0 References: <20210923124502.nxfdaoiov4sysed4@box.shutemov.name> <72cc2691-5ebe-8b56-1fe8-eeb4eb4a4c74@google.com> In-Reply-To: <72cc2691-5ebe-8b56-1fe8-eeb4eb4a4c74@google.com> From: Yang Shi Date: Thu, 23 Sep 2021 14:54:51 -0700 Message-ID: Subject: Re: Mapcount of subpages To: Hugh Dickins Cc: "Kirill A. Shutemov" , Matthew Wilcox , Kent Overstreet , Linux FS-devel Mailing List , Linux Kernel Mailing List , Linux MM , Johannes Weiner , Linus Torvalds , Andrew Morton , "Darrick J. Wong" , Christoph Hellwig , David Howells , Mike Kravetz Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 1CDACF0000AE X-Stat-Signature: gahw8cxd7shrzr6imkt7eaxyyrwqubq8 Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=bKLgDd3g; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of shy828301@gmail.com designates 209.85.208.53 as permitted sender) smtp.mailfrom=shy828301@gmail.com X-HE-Tag: 1632434104-741391 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 Thu, Sep 23, 2021 at 2:10 PM Hugh Dickins wrote: > > On Thu, 23 Sep 2021, Kirill A. Shutemov wrote: > > On Thu, Sep 23, 2021 at 12:40:14PM +0100, Matthew Wilcox wrote: > > > On Thu, Sep 23, 2021 at 01:15:16AM -0400, Kent Overstreet wrote: > > > > On Thu, Sep 23, 2021 at 04:23:12AM +0100, Matthew Wilcox wrote: > > > > > (compiling that list reminds me that we'll need to sort out mapcount > > > > > on subpages when it comes time to do this. ask me if you don't know > > > > > what i'm talking about here.) > > > > > > > > I am curious why we would ever need a mapcount for just part of a page, tell me > > > > more. > > > > > > I would say Kirill is the expert here. My understanding: > > > > > > We have three different approaches to allocating 2MB pages today; > > > anon THP, shmem THP and hugetlbfs. Hugetlbfs can only be mapped on a > > > 2MB boundary, so it has no special handling of mapcount [1]. Anon THP > > > always starts out as being mapped exclusively on a 2MB boundary, but > > > then it can be split by, eg, munmap(). If it is, then the mapcount in > > > the head page is distributed to the subpages. > > > > One more complication for anon THP is that it can be shared across fork() > > and one process may split it while other have it mapped with PMD. > > > > > Shmem THP is the tricky one. You might have a 2MB page in the page cache, > > > but then have processes which only ever map part of it. Or you might > > > have some processes mapping it with a 2MB entry and others mapping part > > > or all of it with 4kB entries. And then someone truncates the file to > > > midway through this page; we split it, and now we need to figure out what > > > the mapcount should be on each of the subpages. We handle this by using > > > ->mapcount on each subpage to record how many non-2MB mappings there are > > > of that specific page and using ->compound_mapcount to record how many 2MB > > > mappings there are of the entire 2MB page. Then, when we split, we just > > > need to distribute the compound_mapcount to each page to make it correct. > > > We also have the PageDoubleMap flag to tell us whether anybody has this > > > 2MB page mapped with 4kB entries, so we can skip all the summing of 4kB > > > mapcounts if nobody has done that. > > > > Possible future complication comes from 1G THP effort. With 1G THP we > > would have whole hierarchy of mapcounts: 1 PUD mapcount, 512 PMD > > mapcounts and 262144 PTE mapcounts. (That's one of the reasons I don't > > think 1G THP is viable.) > > > > Note that there are places where exact mapcount accounting is critical: > > try_to_unmap() may finish prematurely if we underestimate mapcount and > > overestimating mapcount may lead to superfluous CoW that breaks GUP. > > It is critical to know for sure when a page has been completely unmapped: > but that does not need ptes of subpages to be accounted in the _mapcount > field of subpages - they just need to be counted in the compound page's > total_mapcount. > > I may be wrong, I never had time to prove it one way or the other: but > I have a growing suspicion that the *only* reason for maintaining tail > _mapcounts separately, is to maintain the NR_FILE_MAPPED count exactly > (in the face of pmd mappings overlapping pte mappings). > > NR_FILE_MAPPED being used for /proc/meminfo's "Mapped:" and a couple > of other such stats files, and for a reclaim heuristic in mm/vmscan.c. > > Allow ourselves more slack in NR_FILE_MAPPED accounting (either count > each pte as if it mapped the whole THP, or don't count a THP's ptes > at all - you opted for the latter in the "Mlocked:" accounting), > and I suspect subpage _mapcount could be abandoned. AFAIK, partial THP unmap may need the _mapcount information of every subpage otherwise the deferred split can't know what subpages could be freed. > > But you have a different point in mind when you refer to superfluous > CoW and GUP: I don't know the score there (and I think we are still in > that halfway zone, since pte CoW was changed to depend on page_count, > but THP CoW still depending on mapcount). > > Hugh >