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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4473CC3DA7F for ; Mon, 12 Aug 2024 18:51:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=6EIf98lzYG18+ctG/Z74nuFH8A6VL3NsK2bWXMoSA50=; b=FlfdJ5X7JiKtvt1sYVebzFofzu a/kY9XX/7PSyfUF6HKJzeDeZNq11PGsW+JWH4eN09ywwBCjkK8+W0150KJJDRjYCmU8CPv/PaxoOW 478yFak0gXcR6HjzvSOFrZbjCQoaqcTSDIpyMQHpLxPI4iRiVwPTtPTNQyGsgQbLpOp1rjWWY84Qd MpjI2SSd9Sz5HgNA7NBuHPWKar2kZn3thcISozf38IpHWnCsjoHf+Lbk/INgZyWu/J5To0e5jPeBQ JteSRpFLObCZDfJZ5iyoqzKpqjXOZGfAD68iAuj0Y5Pbr1Vw2PjUHLjwf5gVfy6IGtG3zvNd75zyz HPiy2ibg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sda8A-00000001FWG-1cCN; Mon, 12 Aug 2024 18:50:58 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sda7Y-00000001FPG-2wKY for linux-arm-kernel@lists.infradead.org; Mon, 12 Aug 2024 18:50:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723488619; 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:autocrypt:autocrypt; bh=6EIf98lzYG18+ctG/Z74nuFH8A6VL3NsK2bWXMoSA50=; b=W4ySE9W1pbip8UYdOibU0LRdhXyK4J3LHaxb3W3EqhxbXrpNOZsG0irmCic2e04jk0BMFS nmn11F5FVCbjzwdCXCUmvS41uE5TikYnSg2knIN4Hedl9JXa2qUYpkfobv4VY50d/IuSlq 8jk1JBz5XUY46hAaPQTIr4fKrkbC2JU= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-247-ua8fJtG1NYO8hnXSR2-JBg-1; Mon, 12 Aug 2024 14:50:17 -0400 X-MC-Unique: ua8fJtG1NYO8hnXSR2-JBg-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-36875698d0dso2363293f8f.3 for ; Mon, 12 Aug 2024 11:50:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723488617; x=1724093417; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=6EIf98lzYG18+ctG/Z74nuFH8A6VL3NsK2bWXMoSA50=; b=T85jnf+xW2DCUFnAZzRPf1OCeIIF5aVQda1lwphCQO4wfYJ4FSClEneODDCR7JBLoq gigYMuTUhpmGDDEpg94LMV2b48GwhhrlPSTWGeg0ataa6f6nXBhxTFl+xDkWCUdYvfWE ZGxhZ1MfEcUB8MZm5n3K+facaUoEQ42qEVaUlvmWYiXFFnfmMui7MQD7JqcMqZcb0pIZ Fpw/kfN2RGOSs+8RUwVASwRRi306HbhpYkxjXjfSEK0GNMFdOnWmu4wvQrWQMl2Hf9Q2 25V1Ms7hmtjOndOPkxixvkSj+0XAkF5sTpf9N3BYL1kl+GRttsQBXwLwugeNUXkKC3Ou 150Q== X-Forwarded-Encrypted: i=1; AJvYcCUgNUltX+K2TKccPIlFnvQ3VxAQIYXg9ByIux5K4X0+OW0b8k1q2vgf3QdQ3VdHF6yochSGdqCCccIr/8sHacm5SwMEhmOEhbYKajxfxXGmq57LnXw= X-Gm-Message-State: AOJu0Yxir/f97C1w80xmoxan2fWjJz+QBL3obV1vYrEArlI9hf0CebnY t3+xeL0FCp0vaSVMRbB2FB9tdLTrHa59LW9wAxSuvlk3mGrGbeUywW3+QLIT+gsE3DPnQCzQMfR RWh/UTStJ9Yf7l/Y4MeidOwxo8UHcrcsxw85huWeTmwLgplB6hC3cvcjjn+78QOKKO92nvp34 X-Received: by 2002:a5d:6e90:0:b0:368:48b1:803 with SMTP id ffacd0b85a97d-3716ccd8db3mr783595f8f.12.1723488616705; Mon, 12 Aug 2024 11:50:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF/hZJ+B62cFikFh5hEkl96yNDFQNXVuYBcCbRMsgs1RGeMrxoiw/fNrbEx2z0Cj6z0X13zqg== X-Received: by 2002:a5d:6e90:0:b0:368:48b1:803 with SMTP id ffacd0b85a97d-3716ccd8db3mr783577f8f.12.1723488616097; Mon, 12 Aug 2024 11:50:16 -0700 (PDT) Received: from ?IPV6:2003:d8:2f09:3f00:d228:bd67:7baa:d604? (p200300d82f093f00d228bd677baad604.dip0.t-ipconnect.de. [2003:d8:2f09:3f00:d228:bd67:7baa:d604]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36e4e51ec46sm8246641f8f.81.2024.08.12.11.50.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 12 Aug 2024 11:50:15 -0700 (PDT) Message-ID: <9155deaa-b6c5-4e6c-95a7-9a5311b7085a@redhat.com> Date: Mon, 12 Aug 2024 20:50:12 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 07/19] mm/fork: Accept huge pfnmap entries To: Peter Xu Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sean Christopherson , Oscar Salvador , Jason Gunthorpe , Axel Rasmussen , linux-arm-kernel@lists.infradead.org, x86@kernel.org, Will Deacon , Gavin Shan , Paolo Bonzini , Zi Yan , Andrew Morton , Catalin Marinas , Ingo Molnar , Alistair Popple , Borislav Petkov , Thomas Gleixner , kvm@vger.kernel.org, Dave Hansen , Alex Williamson , Yan Zhao References: <20240809160909.1023470-1-peterx@redhat.com> <20240809160909.1023470-8-peterx@redhat.com> <8ef394e6-a964-41c4-b33c-0e940b6b9bd8@redhat.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiH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TPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240812_115020_863417_91140726 X-CRM114-Status: GOOD ( 37.58 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 12.08.24 20:29, Peter Xu wrote: > On Fri, Aug 09, 2024 at 07:59:58PM +0200, David Hildenbrand wrote: >> On 09.08.24 19:15, Peter Xu wrote: >>> On Fri, Aug 09, 2024 at 06:32:44PM +0200, David Hildenbrand wrote: >>>> On 09.08.24 18:08, Peter Xu wrote: >>>>> Teach the fork code to properly copy pfnmaps for pmd/pud levels. Pud is >>>>> much easier, the write bit needs to be persisted though for writable and >>>>> shared pud mappings like PFNMAP ones, otherwise a follow up write in either >>>>> parent or child process will trigger a write fault. >>>>> >>>>> Do the same for pmd level. >>>>> >>>>> Signed-off-by: Peter Xu >>>>> --- >>>>> mm/huge_memory.c | 27 ++++++++++++++++++++++++--- >>>>> 1 file changed, 24 insertions(+), 3 deletions(-) >>>>> >>>>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c >>>>> index 6568586b21ab..015c9468eed5 100644 >>>>> --- a/mm/huge_memory.c >>>>> +++ b/mm/huge_memory.c >>>>> @@ -1375,6 +1375,22 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, >>>>> pgtable_t pgtable = NULL; >>>>> int ret = -ENOMEM; >>>>> + pmd = pmdp_get_lockless(src_pmd); >>>>> + if (unlikely(pmd_special(pmd))) { >>>>> + dst_ptl = pmd_lock(dst_mm, dst_pmd); >>>>> + src_ptl = pmd_lockptr(src_mm, src_pmd); >>>>> + spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); >>>>> + /* >>>>> + * No need to recheck the pmd, it can't change with write >>>>> + * mmap lock held here. >>>>> + */ >>>>> + if (is_cow_mapping(src_vma->vm_flags) && pmd_write(pmd)) { >>>>> + pmdp_set_wrprotect(src_mm, addr, src_pmd); >>>>> + pmd = pmd_wrprotect(pmd); >>>>> + } >>>>> + goto set_pmd; >>>>> + } >>>>> + >>>> >>>> I strongly assume we should be using using vm_normal_page_pmd() instead of >>>> pmd_page() further below. pmd_special() should be mostly limited to GUP-fast >>>> and vm_normal_page_pmd(). >>> >>> One thing to mention that it has this: >>> >>> if (!vma_is_anonymous(dst_vma)) >>> return 0; >> >> Another obscure thing in this function. It's not the job of copy_huge_pmd() >> to make the decision whether to copy, it's the job of vma_needs_copy() in >> copy_page_range(). >> >> And now I have to suspect that uffd-wp is broken with this function, because >> as vma_needs_copy() clearly states, we must copy, and we don't do that for >> PMDs. Ugh. >> >> What a mess, we should just do what we do for PTEs and we will be fine ;) > > IIUC it's not a problem: file uffd-wp is different from anonymous, in that > it pushes everything down to ptes. > > It means if we skipped one huge pmd here for file, then it's destined to > have nothing to do with uffd-wp, otherwise it should have already been > split at the first attempt to wr-protect. Is that also true for UFFD_FEATURE_WP_ASYNC, when we call pagemap_scan_thp_entry()->make_uffd_wp_pmd() ? I'm not immediately finding the code that does the "pushes everything down to ptes", so I might miss that part. > >> >> Also, we call copy_huge_pmd() only if "is_swap_pmd(*src_pmd) || >> pmd_trans_huge(*src_pmd) || pmd_devmap(*src_pmd)" >> >> Would that even be the case with PFNMAP? I suspect that pmd_trans_huge() >> would return "true" for special pfnmap, which is rather "surprising", but >> fortunate for us. > > It's definitely not surprising to me as that's the plan.. and I thought it > shoulidn't be surprising to you - if you remember before I sent this one, I > tried to decouple that here with the "thp agnostic" series: > > https://lore.kernel.org/r/20240717220219.3743374-1-peterx@redhat.com > > in which you reviewed it (which I appreciated). > > So yes, pfnmap on pmd so far will report pmd_trans_huge==true. I review way to much stuff to remember everything :) That certainly screams for a cleanup ... > >> >> Likely we should be calling copy_huge_pmd() if pmd_leaf() ... cleanup for >> another day. > > Yes, ultimately it should really be a pmd_leaf(), but since I didn't get > much feedback there, and that can further postpone this series from being > posted I'm afraid, then I decided to just move on with "taking pfnmap as > THPs". The corresponding change on this path is here in that series: > > https://lore.kernel.org/all/20240717220219.3743374-7-peterx@redhat.com/ > > @@ -1235,8 +1235,7 @@ copy_pmd_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, > src_pmd = pmd_offset(src_pud, addr); > do { > next = pmd_addr_end(addr, end); > - if (is_swap_pmd(*src_pmd) || pmd_trans_huge(*src_pmd) > - || pmd_devmap(*src_pmd)) { > + if (is_swap_pmd(*src_pmd) || pmd_is_leaf(*src_pmd)) { > int err; > VM_BUG_ON_VMA(next-addr != HPAGE_PMD_SIZE, src_vma); > err = copy_huge_pmd(dst_mm, src_mm, dst_pmd, src_pmd, > Ah, good. [...] >> Yes, as stated above, likely broken with UFFD-WP ... >> >> I really think we should make this code just behave like it would with PTEs, >> instead of throwing in more "different" handling. > > So it could simply because file / anon uffd-wp work very differently. Or because nobody wants to clean up that code ;) -- Cheers, David / dhildenb