From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 748F317ADF6 for ; Mon, 12 Aug 2024 18:50:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723488623; cv=none; b=EnNmLwseJUUat/B/OFLyxJcQFUbHTyYgdTkRTXqw9yw7KmRC2Utt6DSZlThzkOnBpVWwhHHKZcFhEQnUVUlW9SSwDrO4YK8Z1bhITdGuLZfG2rp+JiBRiot/cE6rGPq00uJlS8PhzQDvaNOKxD9qieR+OLVAOjQ/tRr81StGSm0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723488623; c=relaxed/simple; bh=UYSKnVroMQDSeji32ngcd/HXvmqbOvFvARM9dsfpxzQ=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=qS3Kii/jOqla6FW2QOQa2FIKjsBEURjCILERk/RIE+TCrGsOfgU23+saRdelBySfAFUHAXIOnsQKXneIq2WEnfRj6HkIrz6taviE48ZfgfgHhEBdYIWcgTFLWa0DUzq9YP3vi+ug4qyq5tXDN/sPV5ilya8Cn0uQ/jaDm9Wd3xc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=W4ySE9W1; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="W4ySE9W1" 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-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_256_GCM_SHA384) id us-mta-451-0TJX0H_-P6eGpm7Cxk879Q-1; Mon, 12 Aug 2024 14:50:18 -0400 X-MC-Unique: 0TJX0H_-P6eGpm7Cxk879Q-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4280645e3e0so33428145e9.1 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=PTb/Wshr7w10VE8AXCaSurmAIa3BeL4qJ58EHXXB6rsMQAVPEmXup7yhEYElRUOez0 RvcsgXUQQ6C4mBUqPK+Y4k/fdbg/OU6aNfoJJwB4KiLleBOOGuwd0K7umNH8FpDdMiJZ 8uNM+bN/SYxM8M3+wB1nfOZFT4wlMwpCrOy5eHJw26Ib/+NlMvGaN0VcocrPguIV0rrW D23ehuLusqBeMhZZ+0vehOWmXrqBxMDGbu6JrrsD5X5SzQSGwXdTeh4eANjaGOWqyAWT cRnK6JVyihEZSbF8xsMOtPNWRlUEop6ogMkypkIfZydH830Xg2DSTLp4SmACyogQxsOJ 4gyA== X-Forwarded-Encrypted: i=1; AJvYcCXoWkUXXd9Sw/kCuwEPuC7l6qVH0vb2zdBT+wDqFyiTqlPQui+y6Ygub+CcwBbpOVPKviLZW9HvBLPsJyrHqpmrW2C1yQoAPLSYzIg5 X-Gm-Message-State: AOJu0YyYHJ12oawTaxSFQM5JhE5deTbPxeoSG8yt6go1Wo0Gvk0Bq5rq IrvAtsJTK9sWc7HbTUCgecaHcLllq3P/3eyWA6Z6uloqtIqLC5yeF7dlJQfOueWc+rMZdvt0Xv+ Tpxxjyk/WHfxKg0+VtoaAFpNBXj1X3BaCsRx01MKoNrkdKhkyVAx/wgyLOvV+sg== X-Received: by 2002:a5d:6e90:0:b0:368:48b1:803 with SMTP id ffacd0b85a97d-3716ccd8db3mr783602f8f.12.1723488616707; 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 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 Content-Language: en-US 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 WhVhdl0qEYB8lkB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MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+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: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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