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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF86AC54E94 for ; Tue, 24 Jan 2023 18:13:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5D3ED6B0071; Tue, 24 Jan 2023 13:13:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 583A46B0072; Tue, 24 Jan 2023 13:13:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 472326B0073; Tue, 24 Jan 2023 13:13:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 354C56B0071 for ; Tue, 24 Jan 2023 13:13:32 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E1F5E80C4F for ; Tue, 24 Jan 2023 18:13:31 +0000 (UTC) X-FDA: 80390490222.06.5578366 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf28.hostedemail.com (Postfix) with ESMTP id 215A3C0005 for ; Tue, 24 Jan 2023 18:13:28 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=N2i5zV4A; spf=none (imf28.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674584010; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=lrfa/2xQ4I/EbLHI3S5iwYcWUsgxj1Lltp4aZaputh4=; b=G/fCfJEIAwS2HHztazxe/thZOgA4NReCkwuLaBUaxl3SZ3L2xSW/jozIHKwpFYuIqD0C7C 5RM32fd6QyPISeQgn0tauS4AI1Qszr7A79RGdXAoqPN8VYYkl9TAbZ0ahLqmp5szdfFzd1 OKhjqpN+QDjPyw34EibEjlX3W2WYq+Q= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=N2i5zV4A; spf=none (imf28.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674584010; a=rsa-sha256; cv=none; b=jSaYAoCuFbyvqxOWl+2KYMlP30xiXlDUQ0DayM4RuGtBSGtUeL2VOUu0dXPAIAiSoL6ymi M92JyNO/Vnp4nVHveSTLg7jncKdWv8UdXJC7Bw1EcNKi1Q/lL605mM75YKBPYCU94vexas W2AZuUsbY0kCr3BsnZaCCRFGa+0tPYU= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:Message-ID: Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:In-Reply-To:References; bh=lrfa/2xQ4I/EbLHI3S5iwYcWUsgxj1Lltp4aZaputh4=; b=N2i5zV4Ab0DRPQgaPcJ1Nos7ps QgVi5j1FmW/KgHXvMQKFxtkSYzbN8w5rVsWDCehP97wtN78iElT/2G3DwMxeArU0DD+nkG2iIdGvu V3erqD0d4E/XDddtayd31Ue4fZNFRP28cuh3RBzertiVevfHJE9cjAxggcEzhKWKI46b6Ca7ZkJ+2 +psl346U9CPfxFl4HHw0fMA7xvm/hGJwvK2x3I0yxnuGfFgEeyuPiE7ymdGN/jCKAdWFvmyyKwVdY aOEOPz2W/IBH4odh45pQJLpfgfbUcQWfkwzL/H4Mx7kVGdVbKHpFhOWNRQICJL4yFq8UayRDqZHHt YHaWMcHw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pKNnN-005FxJ-Mw; Tue, 24 Jan 2023 18:13:21 +0000 Date: Tue, 24 Jan 2023 18:13:21 +0000 From: Matthew Wilcox To: linux-mm@kvack.org Cc: Vishal Moola , Hugh Dickins , Rik van Riel , David Hildenbrand , "Yin, Fengwei" Subject: Folio mapcount Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: fdkhk8ghf17n7y8zwt83sys4eyj57mc4 X-Rspamd-Queue-Id: 215A3C0005 X-HE-Tag: 1674584008-596330 X-HE-Meta: U2FsdGVkX192jDHBwzXplyGmpPDIbSEaPYJp6ANhIoryDMA2vG/1s7rd65KNVVGQnqzsA0aF7znRzjWO4tJl1JHtk8y32TjeqXglmSov0GoMCPM7MpSwz8vRonMm4Dxme1hPlzL/g4xpx4C+Te0bmZpCXlQ8E42cxY7qAOp5K/NXGHdu6vdRcQmTHWnm9nDqauQOeERqgTJztcI0O3GGmdR2eZm9Jok0qO51empaxaWVK9zq7wiRIvpuaok3cpf58SKcK++MfCaG3CfRdJNGhQ4KTGvNI2nWibXxye88hsSS7ke3/Swr9eDH8czF29v53RqwWOEDfDftrzQM+r32aYoeoZ+JujuoTegBuUPYYYUXjGKZhJdb56gQGOnUB5zGBpImiyQKQY3KHLIpb/3Phk/yzQ4JGDG6vgBarfrshSsFk4FCk4a51wk7h57kfRF29vf8u/k+yULqJvSlGZzVZUpkqfmlNwojaqOT9af7IgJBrEH+uMKWPijsCRdc6vrnXnLKvbqDZuDQRYzkEacqjriZCjU6EtIncmjAzHJ9CSB5MPGI5wbpq4s6fnJ62mo0xya+UNi+MA/zWSAlDxY8F9CsZ05EOPM0m67WjYaQXc9981zSbMzvDUxkvICP/A01/M71ivumY+NGlPC53E5i15dEROmQkGjGzUfzteVz+w32Lwcjw8oPRlbv0FqdB9sUANgurwBsrfPFPKH2cw7+BsD4CaeN8MJXPDDnqGNe7bgOiSL39tiP9VpiWzToQW3KAWKhwldaBjPJpBGOikTKLGP6TNcUe0y0RZ3dRtPANW20RceudpfPSp4Q91sJ09KqpCimNShTNYCha27ByzpUvcrnIxYE7EaMG0RZ2zRnMDkt4Ggii0NTblcMSfao1s/7mcon0CZ0J3dPZNuBI64mBC6d3bo24xIRnOyugucNHHzIy4wjrdqgPxCIFOnhq8ABmfZzVrIJaJ19ka4U8Un Khx5Qh+y czDoEJ49sDqWyJF828IhaVdZmXS5CNtDm7IJVugcmeyX18Oeq2QXNWVqlc7c27lYncVZzZJG0akyHQX0p7xcJSaD0RL94j9ScudAZ6qBptbOvDAc2DU+tOwoWOMHNgF57cY/ut2VxwGy6cVE0g2F6UkosCUOHwk09O/TPksdX521Z1PTf7C2VL/Dkj6LPfIFF1s9J/USzz4qvlHJQQFJmkqPHokTLBuKu2Ug/ 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: Once we get to the part of the folio journey where we have one-pointer-per-page, we can't afford to maintain per-page state. Currently we maintain a per-page mapcount, and that will have to go. We can maintain extra state for a multi-page folio, but it has to be a constant amount of extra state no matter how many pages are in the folio. My proposal is that we maintain a single mapcount per folio, and its definition is the number of (vma, page table) tuples which have a reference to any pages in this folio. I think there's a good performance win and simplification to be had here, so I think it's worth doing for 6.4. Examples -------- In the simple and common case where every page in a folio is mapped once by a single vma and single page table, mapcount would be 1 [1]. If the folio is mapped across a page table boundary by a single VMA, after we take a page fault on it in one page table, it gets a mapcount of 1. After taking a page fault on it in the other page table, its mapcount increases to 2. For a PMD-sized THP naturally aligned, mapcount is 1. Splitting the PMD into PTEs would not change the mapcount; the folio remains order-9 but it stll has a reference from only one page table (a different page table, but still just one). Implementation sketch --------------------- When we take a page fault, we can/should map every page in the folio that fits in this VMA and this page table. We do this at present in filemap_map_pages() by looping over each page in the folio and calling do_set_pte() on each. We should have a: do_set_pte_range(vmf, folio, addr, first_page, n); and then change the API to page_add_new_anon_rmap() / page_add_file_rmap() to pass in (folio, first, n) instead of page. That gives us one call to page_add_*_rmap() per (vma, page table) tuple. In try_to_unmap_one(), page_vma_mapped_walk() currently calls us for each pfn. We'll want a function like page_vma_mapped_walk_skip_to_end_of_ptable() in order to persuade it to only call us once or twice if the folio is mapped across a page table boundary. Concerns -------- We'll have to be careful to always zap all the PTEs for a given (vma, pt) tuple at the same time, otherwise mapcount will get out of sync (eg map three pages, unmap two; we shouldn't decrement the mapcount, but I don't think we can know that. But does this ever happen? I think we always unmap the entire folio, like in try_to_unmap_one(). I haven't got my head around SetPageAnonExclusive() yet. I think it can be a per-folio bit, but handling a folio split across two page tables may be tricky. Notes ----- [1] Ignoring the bias by -1 to let us detect transitions that we care about more efficiently; I'm talking about the value returned from page_mapcount(), not the value stored in page->_mapcount.