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 707D02550CA for ; Thu, 16 Oct 2025 19:10:13 +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=1760641815; cv=none; b=qhPQkuq0j6kvt5fD/in/Xl5joB4Ci5cKpCOjVXWiW8Yit+JuKjlN3fRNCb8BD9C1eeJM1fqbthxf9psuOEXMN/6JxsHxSGfYIeP0ghJHLcAosotak2cYqSF5nIhO+GfuMkHzUi25eXrDN1BiRxf96UH3ChNEBOkPAA/BqOL+szE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760641815; c=relaxed/simple; bh=1YIxE/5Vi3hruKRl+eohYotPfaWKW71gmzDh9nJEsTs=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=D/nycvOaPTAI5OQzSr2dQsEMIUUYpnqWvt770M1hws/xMNYpzpb2ef2vU5A2ntBFXIQjr6fm44aQWTuER/pRY5JD+zBg8Fnlune8M68uH6RuMEtlBc4CFEB60bcMc8dp00iwkW/nD6gBn8TBiZqxMvt6DIq50NR9OBG49V8N++c= 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=Jp5G6clF; arc=none smtp.client-ip=170.10.129.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="Jp5G6clF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760641812; 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=+3AAg05Yf1Z3P5L48FsHnTWstB0ReAZ8G/euj8J0YSU=; b=Jp5G6clFe2pld9iyHQXrXZgqGO7u3tY0/7N+wsCFM34QOqkPauHPvFNRmWVlHJ7Wo4P6Ha zExmULYiDW15TWiwvCHJi0xGjoleA388fJuo7kE2nzLGO6zoYjz2IvRH57gHFIWtpLIY5Y cskDBVvgZOo62wye0NVd16K2xWnbDR0= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-447-K0MBdO4APgiAbKXG6WR8_g-1; Thu, 16 Oct 2025 15:10:10 -0400 X-MC-Unique: K0MBdO4APgiAbKXG6WR8_g-1 X-Mimecast-MFC-AGG-ID: K0MBdO4APgiAbKXG6WR8_g_1760641806 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-46b303f6c9cso11693385e9.2 for ; Thu, 16 Oct 2025 12:10:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760641806; x=1761246606; 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=+3AAg05Yf1Z3P5L48FsHnTWstB0ReAZ8G/euj8J0YSU=; b=JvNOQkAbaGTe/STZpV7bRE1EDPBKMtyflKjFMbIxin8nJbukyfe/jFAwYQzeHLk6Zx I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MuNw= X-Received: by 2002:a05:600c:3b83:b0:46e:3d41:6001 with SMTP id 5b1f17b1804b1-47117925171mr12364025e9.34.1760641805977; Thu, 16 Oct 2025 12:10:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGc5h2vbpz69OVWKGuaFjCUjxbNEASV+GvR8Na4j9NprkAExd41lL0K2813EC7L3Hmt8vs2XQ== X-Received: by 2002:a05:600c:3b83:b0:46e:3d41:6001 with SMTP id 5b1f17b1804b1-47117925171mr12363635e9.34.1760641805466; Thu, 16 Oct 2025 12:10:05 -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-4711441f776sm46310535e9.3.2025.10.16.12.10.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 16 Oct 2025 12:10:04 -0700 (PDT) Message-ID: Date: Thu, 16 Oct 2025 21:10:03 +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+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQO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2rHyc8Sl86oK1tvAkwBNsfKou3v+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: 7bit >> 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. For that, we need the back-off described below. IIRC we implemented that in the PMD case for khugepaged. Or is there somewhere a guaranteed RCU sync before the shared page table gets reused? > >> So I'm wondering whether we use RCU somehow. >> >> But note that in gup_fast_pte_range(), we are validating whether the PMD >> changed: >> >> if (unlikely(pmd_val(pmd) != pmd_val(*pmdp)) || >> unlikely(pte_val(pte) != pte_val(ptep_get(ptep)))) { >> gup_put_folio(folio, 1, flags); >> goto pte_unmap; >> } >> >> >> So in case the page table got reused in the meantime, we should just >> back off and be fine, right? > > The shared page table is mapped with a PUD entry, and we don't check > whether the PUD entry changed here. Yes, see my follow-up mail, that's what we'd have to add. On an arch without IPI, page tables will be freed with RCU and it just works. We walk the wrong page table, realize that the PUD changed and back off. On an arch with IPI it's tricky: if we don't issue the IPI you added, we might still back off once we check the PUD entry didn't changee, but I'm afraid nothing would stop us from walking the previous page table that was freed in the meantime, containing garbage. Easy fix would be never reusing a page table once shared once? -- Cheers David / dhildenb