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.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL 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 E6A2DC433F5 for ; Thu, 23 Sep 2021 21:10:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6F23660F70 for ; Thu, 23 Sep 2021 21:10:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 6F23660F70 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id BEAA0900002; Thu, 23 Sep 2021 17:10:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B98E06B0071; Thu, 23 Sep 2021 17:10:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A880A900002; Thu, 23 Sep 2021 17:10:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0209.hostedemail.com [216.40.44.209]) by kanga.kvack.org (Postfix) with ESMTP id 988F56B006C for ; Thu, 23 Sep 2021 17:10:28 -0400 (EDT) Received: from smtpin40.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 5733C181B0493 for ; Thu, 23 Sep 2021 21:10:28 +0000 (UTC) X-FDA: 78620081736.40.4B479AC Received: from mail-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) by imf11.hostedemail.com (Postfix) with ESMTP id 154D4F0000AA for ; Thu, 23 Sep 2021 21:10:27 +0000 (UTC) Received: by mail-ot1-f41.google.com with SMTP id l7-20020a0568302b0700b0051c0181deebso10329001otv.12 for ; Thu, 23 Sep 2021 14:10:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:in-reply-to:message-id:references :mime-version; bh=U8C7kTM2i0LULyId4CRBDB3WhhpE9MXA+bUV/5nqDCo=; b=sVjEV9NF9eKm1hMfgtvsNUa3rZxxZ/Lezl4IcpqFtOVWR+5zaDUqxg/2hiXVYGCr7I CQtjidNr9hwZ6tjsTvhRtQwqA2WGW79xEb5uZfAaH/b5TWCqeOnNStNWOKO+Vpo/wP6H U9Ea713fUSGaeqS5WU6p2qphlKV57l9LG5ug8VBf9p8g0U62zTHdouMLa114GRaWESjO blOcE5BxLA772si2ZZeJgIfj0tz2uwzgyy9dRxSeT2SsqNtdXwQ1kZom67fSsff1R26n 30uQDOqWJ1QpTpn1LVOshgY+6SJ/JiBChWBGGblWKwbRCCnlQ2II+jotRQ1v1p7zEydL L7Lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:mime-version; bh=U8C7kTM2i0LULyId4CRBDB3WhhpE9MXA+bUV/5nqDCo=; b=VP2Ai7fWxU5hi91UuYxHYeOFLbSU4ahYk7CkO8LpWl/IH42tl6gfxy7CJrUJKQgEsQ Wqwm/7x3I1wVFyHC6KbeDOpqR+Rxxcqum2ROJNZSk092Z+7JDdpbVyxvkvYVIlbNHdTT YdweorXmuqvB423TV/fISi1p3/eBAnPG17OWLtm8JVjmpajkbdlZpMq8LfH5GmcFScAC QWeNtr6ub2Jo1AKk9nAkHSYLXn/ojeKnnaffHhEH+tEMyHtQ6WcPN2ZW/idDaErVz3z0 J+7l9ye8cWT21AhEfY4IPrHfEAH8tzL1LTxQP2O7ceYK37Hd58flmrhpovpAvdqEo7dT jk8Q== X-Gm-Message-State: AOAM532Y9x70aMPdDCM1M1wtbKjErDtvSHT0HMSIQoCrIuhisHazi5iQ 3gP82+keKHRYMTROjXGgNAPw/g== X-Google-Smtp-Source: ABdhPJzwY4l4nfzUx7XxLiPKY1zK2O5YluTcKjvD7YeJim2WLDvwlsxLKjkqIRbKCKLks7jJnRSJCA== X-Received: by 2002:a9d:60c2:: with SMTP id b2mr707393otk.27.1632431427084; Thu, 23 Sep 2021 14:10:27 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id h25sm524386otl.1.2021.09.23.14.10.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Sep 2021 14:10:26 -0700 (PDT) Date: Thu, 23 Sep 2021 14:10:13 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.anvils To: "Kirill A. Shutemov" cc: Matthew Wilcox , Kent Overstreet , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Johannes Weiner , Linus Torvalds , Andrew Morton , "Darrick J. Wong" , Christoph Hellwig , David Howells , Mike Kravetz Subject: Re: Mapcount of subpages In-Reply-To: <20210923124502.nxfdaoiov4sysed4@box.shutemov.name> Message-ID: <72cc2691-5ebe-8b56-1fe8-eeb4eb4a4c74@google.com> References: <20210923124502.nxfdaoiov4sysed4@box.shutemov.name> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Rspamd-Queue-Id: 154D4F0000AA X-Stat-Signature: dfg4dtbteke5adrn995pw165rz4t6amg Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=sVjEV9NF; spf=pass (imf11.hostedemail.com: domain of hughd@google.com designates 209.85.210.41 as permitted sender) smtp.mailfrom=hughd@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam06 X-HE-Tag: 1632431427-735567 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, 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. 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