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.133.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 10E932DC77C for ; Thu, 16 Oct 2025 19:45:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760643906; cv=none; b=K/L/uK2CiEEub+oj4jWZJ1PwbqtS87N8oNL1Eb6ay/7mbRNb5HoLDfmIJz4pgR/ealQAzYy+X3ycfv96M+vBmzmJHT8mtGWLY+2lemsyfLFAQU0gjHwHzNqJY5IE0vidfGK+OGmtNGrs7tiqAo0t3pPwsdta8myxVjPf6oz/H6c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760643906; c=relaxed/simple; bh=osBjfGeNjpJ9/RBRJZly9EpO2J5jBussZghjXnhs6MA=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=tXxxmAs+9oSFbDont9xE/ILS4+zdVadcS5BIYcn5zE07l0QJXBSw/j4YDpG1b0CvYECUpxwvX/4W/YJPYpvPgiYnaI88WAlh4R7M4NO+ACyOVbxYQevNtrX6T2aSNqX/79fW0ujDbcV4FH1mcYmwh4YyMHS8F9TrDM8Ea52FlN0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine 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=GlPCNJEy; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine 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="GlPCNJEy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760643903; 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=Ksp8408IsxwecTmUGjzpuXH4WucsZWUXVX7U4eYiGcs=; b=GlPCNJEySTFxeu1oPRxOAGx3GHwb8YJnOczCBiIxvuPNFeaQaiebmB0oWX2qegMSe3O62p mziei8vG2tZ5qoPJtv1oZWipQXPl9KK0PLcRg+NBnJAaYxrf6gfb1QH1t3PiaLO4hxPVTm yUVru8LdxKNIRYJmYBNHP3aBJvrL2kw= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-622-ItpeUEh8PquA8FPxE4Kmew-1; Thu, 16 Oct 2025 15:45:02 -0400 X-MC-Unique: ItpeUEh8PquA8FPxE4Kmew-1 X-Mimecast-MFC-AGG-ID: ItpeUEh8PquA8FPxE4Kmew_1760643901 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-471168953bdso6125545e9.1 for ; Thu, 16 Oct 2025 12:45:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760643901; x=1761248701; h=content-transfer-encoding:in-reply-to: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=Ksp8408IsxwecTmUGjzpuXH4WucsZWUXVX7U4eYiGcs=; b=w429svIKVFIH7BZfHf5IpoEWDcO5btkrCI0AtlNEFyuNqV5oJaeG1CZeYv8a0X3PXV m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by 2002:a05:600c:2a94:b0:45b:92a6:63e3 with SMTP id 5b1f17b1804b1-47109a24fddmr26392965e9.9.1760643901291; Thu, 16 Oct 2025 12:45:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGIKmtm5OtXO7fBnLBPXWoptqR/y0fSk3woBtfDJ5Y9MjcCQX5Zq0k9GsqIgEbeF4forh+F7A== X-Received: by 2002:a05:600c:2a94:b0:45b:92a6:63e3 with SMTP id 5b1f17b1804b1-47109a24fddmr26392895e9.9.1760643900900; Thu, 16 Oct 2025 12:45:00 -0700 (PDT) Received: from ?IPV6:2003:d8:2f0c:c200:fa4a:c4ff:1b32:21ce? (p200300d82f0cc200fa4ac4ff1b3221ce.dip0.t-ipconnect.de. [2003:d8:2f0c:c200:fa4a:c4ff:1b32:21ce]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4711444d919sm43215855e9.14.2025.10.16.12.44.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 16 Oct 2025 12:45:00 -0700 (PDT) Message-ID: <9d9912fe-3b0b-4754-87f6-6efb49d92a7b@redhat.com> Date: Thu, 16 Oct 2025 21:44:58 +0200 Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Bug: Performance regression in 1013af4f585f: mm/hugetlb: fix huge_pmd_unshare() vs GUP-fast race To: Jann Horn Cc: "Uschakow, Stanislav" , "linux-mm@kvack.org" , "trix@redhat.com" , "ndesaulniers@google.com" , "nathan@kernel.org" , "akpm@linux-foundation.org" , "muchun.song@linux.dev" , "mike.kravetz@oracle.com" , "lorenzo.stoakes@oracle.com" , "liam.howlett@oracle.com" , "osalvador@suse.de" , "vbabka@suse.cz" , "stable@vger.kernel.org" References: <4d3878531c76479d9f8ca9789dc6485d@amazon.de> From: David Hildenbrand Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+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cwRuj1m73D sq8DEFBdZ5eEkj1dCyx+t/ga6x2rHyc8Sl86oK1tvAkwBNsfKou3v+jP/l14a7DGBvrmlYjO 59o3t6inu6H7pt7OL6u6BQj7DoMAEQEAAcLBfAQYAQgAJgIbDBYhBBvZyq1zXEw6Rg38yk3e EPcA/4NaBQJonNqrBQkmWAihAAoJEE3eEPcA/4NaKtMQALAJ8PzprBEXbXcEXwDKQu+P/vts IfUb1UNMfMV76BicGa5NCZnJNQASDP/+bFg6O3gx5NbhHHPeaWz/VxlOmYHokHodOvtL0WCC 8A5PEP8tOk6029Z+J+xUcMrJClNVFpzVvOpb1lCbhjwAV465Hy+NUSbbUiRxdzNQtLtgZzOV Zw7jxUCs4UUZLQTCuBpFgb15bBxYZ/BL9MbzxPxvfUQIPbnzQMcqtpUs21CMK2PdfCh5c4gS sDci6D5/ZIBw94UQWmGpM/O1ilGXde2ZzzGYl64glmccD8e87OnEgKnH3FbnJnT4iJchtSvx yJNi1+t0+qDti4m88+/9IuPqCKb6Stl+s2dnLtJNrjXBGJtsQG/sRpqsJz5x1/2nPJSRMsx9 5YfqbdrJSOFXDzZ8/r82HgQEtUvlSXNaXCa95ez0UkOG7+bDm2b3s0XahBQeLVCH0mw3RAQg r7xDAYKIrAwfHHmMTnBQDPJwVqxJjVNr7yBic4yfzVWGCGNE4DnOW0vcIeoyhy9vnIa3w1uZ 3iyY2Nsd7JxfKu1PRhCGwXzRw5TlfEsoRI7V9A8isUCoqE2Dzh3FvYHVeX4Us+bRL/oqareJ CIFqgYMyvHj7Q06kTKmauOe4Nf0l0qEkIuIzfoLJ3qr5UyXc2hLtWyT9Ir+lYlX9efqh7mOY qIws/H2t In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 16.10.25 21:26, Jann Horn wrote: > On Thu, Oct 16, 2025 at 9:10 PM David Hildenbrand wrote: >>>> I'm currently looking at the fix and what sticks out is "Fix it with an >>>> explicit broadcast IPI through tlb_remove_table_sync_one()". >>>> >>>> (I don't understand how the page table can be used for "normal, >>>> non-hugetlb". I could only see how it is used for the remaining user for >>>> hugetlb stuff, but that's different question) >>> >>> If I remember correctly: >>> When a hugetlb shared page table drops to refcount 1, it turns into a >>> normal page table. If you then afterwards split the hugetlb VMA, unmap >>> one half of it, and place a new unrelated VMA in its place, the same >>> page table will be reused for PTEs of this new unrelated VMA. >> >> That makes sense. >> >>> >>> So the scenario would be: >>> >>> 1. Initially, we have a hugetlb shared page table covering 1G of >>> address space which maps hugetlb 2M pages, which is used by two >>> hugetlb VMAs in different processes (processes P1 and P2). >>> 2. A thread in P2 begins a gup_fast() walk in the hugetlb region, and >>> walks down through the PUD entry that points to the shared page table, >>> then when it reaches the loop in gup_fast_pmd_range() gets interrupted >>> for a while by an NMI or preempted by the hypervisor or something. >>> 3. P2 removes its VMA, and the hugetlb shared page table effectively >>> becomes a normal page table in P1. >>> 4. Then P1 splits the hugetlb VMA in the middle (at a 2M boundary), >>> leaving two VMAs VMA1 and VMA2. >>> 5. P1 unmaps VMA1, and creates a new VMA (VMA3) in its place, for >>> example an anonymous private VMA. >>> 6. P1 populates VMA3 with page table entries. >>> 7. The gup_fast() walk in P2 continues, and gup_fast_pmd_range() now >>> uses the new PMD/PTE entries created for VMA3. >> >> Yeah, sounds possible. And nasty. >> >>> >>>> How does the fix work when an architecture does not issue IPIs for TLB >>>> shootdown? To handle gup-fast on these architectures, we use RCU. >>> >>> gup-fast disables interrupts, which synchronizes against both RCU and IPI. >> >> Right, but RCU is only used for prevent walking a page table that has >> been freed+reused in the meantime (prevent us from de-referencing >> garbage entries). >> >> It does not prevent walking the now-unshared page table that has been >> modified by the other process. > > Hm, I'm a bit lost... which page table walk implementation are you > worried about that accesses page tables purely with RCU? I believe all > page table walks should be happening either with interrupts off (in > gup_fast()) or under the protection of higher-level locks; in > particular, hugetlb page walks take an extra hugetlb specific lock > (for hugetlb VMAs that are eligible for page table sharing, that is > the rw_sema in hugetlb_vma_lock). I'm only concerned about gup-fast, but your comment below explains why your fix works as it triggers an IPI in any case, not just during the TLB flush. Sorry for missing that detail. > > Regarding gup_fast(): > > In the case where CONFIG_MMU_GATHER_RCU_TABLE_FREE is defined, the fix > commit 1013af4f585f uses a synchronous IPI with > tlb_remove_table_sync_one() to wait for any concurrent GUP-fast > software page table walks, and some time after the call to > huge_pmd_unshare() we will do a TLB flush that synchronizes against > hardware page table walks. Right, so we definetly issue an IPI. > > In the case where CONFIG_MMU_GATHER_RCU_TABLE_FREE is not defined, I > believe the expectation is that the TLB flush implicitly does an IPI > which synchronizes against both software and hardware page table > walks. Yes, that's what I had in mind, not an explicit sync. So the big question is whether we could avoid this IPI on every unsharing. Assume we would ever reuse a page table that was shared, we'd have to do this IPI only before freeing the page table I guess, or free the page table through RCU. -- Cheers David / dhildenb