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 6C964C54EAA for ; Tue, 24 Jan 2023 18:37:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E080F6B0072; Tue, 24 Jan 2023 13:37:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D910D6B0075; Tue, 24 Jan 2023 13:37:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C0B996B0078; Tue, 24 Jan 2023 13:37:19 -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 AEDA06B0072 for ; Tue, 24 Jan 2023 13:37:19 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 88B19120631 for ; Tue, 24 Jan 2023 18:37:19 +0000 (UTC) X-FDA: 80390550198.04.C79DE9B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf21.hostedemail.com (Postfix) with ESMTP id 5A1F71C001A for ; Tue, 24 Jan 2023 18:37:17 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="U7/Yf1PL"; spf=pass (imf21.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674585437; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=D4bJGRkdoUjYKR1u1VpLYijxHMi3I4pYuuOmol3fzAQ=; b=11deecpRdzM9gAnSvIXwgvDuOSG/tP0gi5HK+6B/Qn1lbuGd0xOGGpbNKpmCWTuMQjGWxI nH3gy5j4ehEpq5logihRslmPR9aAjnuC412F61hKrANemaXOvttfIQjPU3/cq82yq3q1mk bmT9yjhC+mhwtTcfC7UMp/uDTzh7470= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="U7/Yf1PL"; spf=pass (imf21.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674585437; a=rsa-sha256; cv=none; b=g/qFWc8DWO0JlIe0Gby+GuwtHDbrh6LlLMW/KU+VnC1nNkhu+OhUJosFbtD/iZFhg6Ex3I pKcLxVCW6PtgDZ7s/hTf5nwJzDIl9TOECNo7uykd9mUjITlWzwzs1jlfucwCNts7m6KO8e 7jCbR5Bcmx6N1GLgBncaqWQUHGdlAsM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674585436; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=D4bJGRkdoUjYKR1u1VpLYijxHMi3I4pYuuOmol3fzAQ=; b=U7/Yf1PLetrbyvK3WKVTGfrStp+SyLYoKbCe79MIlEQNHuYauQcO4AFoqjRHUpu+AAb9Ru m+CL+g2FGVmtFluzvvAoKxwJ6GNUHSqf92fX7GnZbOyWHuVlCHh4cbEYxdGM8Xi8oh9HFc lUGviVUxIMA2Ez55F6rXEm2yHEyTPy4= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-537-LzbRz4y0NEy6x-dFWD5ShA-1; Tue, 24 Jan 2023 13:37:15 -0500 X-MC-Unique: LzbRz4y0NEy6x-dFWD5ShA-1 Received: by mail-wm1-f70.google.com with SMTP id o5-20020a05600c4fc500b003db0b3230efso11628616wmq.9 for ; Tue, 24 Jan 2023 10:37:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=D4bJGRkdoUjYKR1u1VpLYijxHMi3I4pYuuOmol3fzAQ=; b=AU5RH8n/C1zLlUOMZqQ6Kk8lcjPqXV/OogxECMmJ6C46G7PK+NvDOG3+2sX+JslCNE uW1ZDsxOEZwbYMmF9xzWGBFZG+j5Bu67YmYcjUcJeQ5vgOEZi6UDqLLrDMW2ra44k4is 2ldMmOhUrcZpCTJXHy9uE4zKOplQ/UCGgQRTfochpM/HryIUjX39jtMUrQdHxn6xoirK uap1ePMsJsDBDsMV8gHdbU21UGJw3WFWty4wdCKFDlmV0eEmdatGmvShC+fc8g3X2dIl x9crhmXgAj7Xv/xBGHFLDoyEPKHKx2RS6O1JtgwX1KXk/3gzy2kWIZzZRQ5FxwI9Tb4E 6/pw== X-Gm-Message-State: AO0yUKWkmCNMkQF/nKokrsVSIjIZsCKHV1Dzf67i6A4s8VREWIyFbYd3 R+W+E9go1IskW2AybB+vVgiv/RwGlBTgBPSywNQH6SIpl63HulRktREnd0dxx2In+Q5Q00WwoPD oNem+NQKYMJI= X-Received: by 2002:adf:a1dc:0:b0:2bf:b4ca:6bac with SMTP id v28-20020adfa1dc000000b002bfb4ca6bacmr1434446wrv.59.1674585434321; Tue, 24 Jan 2023 10:37:14 -0800 (PST) X-Google-Smtp-Source: AK7set8FRpO/VEAEVf2r6Qj9jAk+QO01ycq7xhW90iO351NB766sjXKGZcKrpHAkUvFh5B+TIqAsnw== X-Received: by 2002:adf:a1dc:0:b0:2bf:b4ca:6bac with SMTP id v28-20020adfa1dc000000b002bfb4ca6bacmr1434432wrv.59.1674585434089; Tue, 24 Jan 2023 10:37:14 -0800 (PST) Received: from ?IPV6:2003:cb:c707:9d00:9303:90ce:6dcb:2bc9? (p200300cbc7079d00930390ce6dcb2bc9.dip0.t-ipconnect.de. [2003:cb:c707:9d00:9303:90ce:6dcb:2bc9]) by smtp.gmail.com with ESMTPSA id a5-20020adfeec5000000b002bfb5ebf8cfsm661343wrp.21.2023.01.24.10.37.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 24 Jan 2023 10:37:13 -0800 (PST) Message-ID: <3cbc93f2-bacb-3603-cd1d-4b411e6cc723@redhat.com> Date: Tue, 24 Jan 2023 19:37:12 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 Subject: Re: Folio mapcount From: David Hildenbrand To: Matthew Wilcox , linux-mm@kvack.org Cc: Vishal Moola , Hugh Dickins , Rik van Riel , "Yin, Fengwei" References: <3cc8f142-a69d-ae84-6a33-50bdc9aade21@redhat.com> Organization: Red Hat In-Reply-To: <3cc8f142-a69d-ae84-6a33-50bdc9aade21@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: smcibzd4e6e8sjp1ojudrnbwaop8ud9a X-Rspamd-Queue-Id: 5A1F71C001A X-HE-Tag: 1674585437-992628 X-HE-Meta: U2FsdGVkX1/B6gsgDXE8/pGWLAdOnZtRJocICq0Z9ulCbYWhhDxRIrp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C4sV3pjANuSbdWauzxRpo1m06Jq2f22YOsnnvkuxUgIfMH+Zi0v+MdtT5byb9vwD/+IvHP14MFr89dSsMWDHw+GMHup/XoPPNaxfYk4WwMniZVHycAKUSEEcvWDsVcxdfZFplD9uvwpd+Qrxh+b3K5+1sOYYn6URhhBh1UTHocBfatRnBECaGLA9ASSyXoEM3TOasRVFwu1NB6Jj+/QjTrhiasKrCvha0iTQY9zFOKaQtPRY+xETzY0lARhnUrHjXaBGl/vc/M38+h+3sPm/vbovsM+V+I+8vgb8EthY7k3Ze7p+m3aBFkDAZUdIePQ== 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 24.01.23 19:35, David Hildenbrand wrote: > On 24.01.23 19:13, Matthew Wilcox wrote: >> 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(). > > Not sure about file THP, but for anon ... it's very common to partially > MADV_DONTNEED anon THP. Or to have a wild mixture of two (or more) anon > THP fragments after fork() when COW'ing on the PTE-mapped THP ... > >> >> 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. > > I tried hard (very hard!) to make that work but reality caught up. And > the history of why that handling is required goes back to the old days > where we had per-subpage refcounts to then have per-subpage mapcounts to > now have only a single bit to get COW handling right. > > There are very (very!) ugly corner cases of partial mremap, partial > MADV_WILLNEED ... some are included in the cow selftest for that reason. s/MADV_WILLNEED/MADV_DONTFORK/ -- Thanks, David / dhildenb