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 DF95C257427 for ; Fri, 2 May 2025 15:42:33 +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=1746200555; cv=none; b=eD0zh7XybjUxQVGtBSYe3YmALKbGnjVpsJ0AfnxRHIhzGtfGbKK0hVs96S5+ZX/LV5s3pmFNIB9Hg+Q8Ku0e4u4J2m9uRXo5wFf839MLpS6ppeilSvOSt2v4MhRZkow6PfhuGjIHb4GzOWSut5mGC7SdNe0sd+qm5TfjK+RGWH4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746200555; c=relaxed/simple; bh=Lgo3oPvJN4TmDwml+hlw26Vb+KFGfqwA06+QuXWnm/c=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=p+3qA2Q6RSXg2B77Nk7jHXv7RKo5FKDga0nSbJROs5MMdrlFxEZjw5NMg2KkPHaOuelSMJLIjrmPEPMH217yQrJSk5asl6hVrlH1e9XWaf4CMlkNqF+oS+RB/E+7bU7pr8T9Dl2Rjv/yJznOFRyqT+dySNUE0VVQ9a02kxO/vd0= 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=XnZuoWfv; 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="XnZuoWfv" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1746200553; 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=T7cqDJLWyNwhnnHN+5VqQ5VJiv3rSNdDAibInwFuRIg=; b=XnZuoWfvmPAig20asaFOmS8GEP0mL7RCXEqMyu3ioe3W2YWXQ6dtpVo6U/ccj4nIN6/E+V m3uv+O3mc95hx/NIPUMaL7m8JNjTmxu3llMRG/1FsumDhWDVAzWN2sZ3PmkCNk0317NaBu YtjHURy8oTbKbNHWnyDEfHer/qRJVdA= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-395-nzjHN2Y6NKG8fq6e0Y2bSQ-1; Fri, 02 May 2025 11:42:29 -0400 X-MC-Unique: nzjHN2Y6NKG8fq6e0Y2bSQ-1 X-Mimecast-MFC-AGG-ID: nzjHN2Y6NKG8fq6e0Y2bSQ_1746200549 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-39143311936so659009f8f.0 for ; Fri, 02 May 2025 08:42:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746200549; x=1746805349; 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=T7cqDJLWyNwhnnHN+5VqQ5VJiv3rSNdDAibInwFuRIg=; b=qKI1rhsQayaH1LFoaJpKYYerjSykGATN6cEIsUS+l9a0WIVTuVUxVj2JLzYFkPL/CC +fn/1LBwcS4DCNWoe+WNn4gOpg3BLp/gDGJXbC4lz1QJ9JBjilQIJAt2QPKa67GdJ7dr YGKFujWV21W14TsPw1S/EClGsat9s5yk5PtkOtGTZWgbC3SFV2FrANZus5ycxLueyT8K GMLpUOeLFhRD/DDA1AWkT5/GXJUvLUIUfFBAsHf0c3jdzydHB9TZXpn4wCjPpdP0t38T 5rvKuIb2KUBdwt/PQ4+1RVBKaP+LmAAcnrkkVFMJSiZumI/agWrZdWW3uneJ3Vaft72t oNiw== X-Forwarded-Encrypted: i=1; AJvYcCVo4XFd8kUzJl0wDnF4C55sZIjArfUqtLZA4A/8SyUOL2D3w7OZDdoxEnqQKnGkGyz/q2f4+e9XJp8=@vger.kernel.org X-Gm-Message-State: AOJu0YyK/Q6UNQkWzrKiliochRRY18ePXTRL9oSE7+h1/V4F0kTQ1Yyf 77ZdeJ82o9a/7WgifWcmRp9Le5EitXthODExiK68Mqj67aA9AEOyyAloLXeQf3gWeHQ0kSAR5pG k/TuM73UEV3g243jmveGfgdtPCHYZusfRNLs2VMwdu6lmD0zYlIDr8KwYYqnLeOqfITNy X-Gm-Gg: ASbGncs+dEGFpUUGslu31/8VooOoE3GNdwnVmG/BqWYwQw4kQY5U5zkHr9/dmecDjzZ iVT+EROMMOKOyEpybjnbyWmzuXcJMAeUbK1lFidCF64515PIgp4SCshOwWmHaKcHGOZheHlT76h QQ2T30pa0QSHiZ2vZf9ORQWC8myqYu8MvXM6NY5JuBhGIfNf1yysFo0UJyIP4eAq5yxAJbTaVYr Vfx4huQDmDl7irltCClCZWl+XSG6r4iLSAUsJNucvoci7tIWZ+rBGI7LwMn/bUjWs3UhNYi4NqL vtwFVax5jvT0moj8gTQo7r1n72C8LALnSPoXysfUfKDivLuBVyd89AIozSCtzI705V/voCaj38S oGcvZWBGTmWNvaL1E3yTB3k+pA/6aj5xe5mKcTMA= X-Received: by 2002:a05:6000:2c8:b0:3a0:7f90:2e2e with SMTP id ffacd0b85a97d-3a099ad6379mr2543634f8f.24.1746200548739; Fri, 02 May 2025 08:42:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHksxJt4q2NvAKXafUh3Noz69LskLRP5kl9AuC6VrOlPInFhsJt/6w4PnefBiSeeay/3Kr9EQ== X-Received: by 2002:a05:6000:2c8:b0:3a0:7f90:2e2e with SMTP id ffacd0b85a97d-3a099ad6379mr2543613f8f.24.1746200548288; Fri, 02 May 2025 08:42:28 -0700 (PDT) Received: from ?IPV6:2003:cb:c713:d600:afc5:4312:176f:3fb0? (p200300cbc713d600afc54312176f3fb0.dip0.t-ipconnect.de. [2003:cb:c713:d600:afc5:4312:176f:3fb0]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a099ae0c48sm2457341f8f.8.2025.05.02.08.42.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 02 May 2025 08:42:27 -0700 (PDT) Message-ID: Date: Fri, 2 May 2025 17:42:25 +0200 Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 07/12] khugepaged: add mTHP support To: Nico Pache , Jann Horn Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, akpm@linux-foundation.org, corbet@lwn.net, rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, ryan.roberts@arm.com, willy@infradead.org, peterx@redhat.com, ziy@nvidia.com, wangkefeng.wang@huawei.com, usamaarif642@gmail.com, sunnanyong@huawei.com, vishal.moola@gmail.com, thomas.hellstrom@linux.intel.com, yang@os.amperecomputing.com, kirill.shutemov@linux.intel.com, aarcange@redhat.com, raquini@redhat.com, dev.jain@arm.com, anshuman.khandual@arm.com, catalin.marinas@arm.com, tiwai@suse.de, will@kernel.org, dave.hansen@linux.intel.com, jack@suse.cz, cl@gentwo.org, jglisse@google.com, surenb@google.com, zokeefe@google.com, hannes@cmpxchg.org, rientjes@google.com, mhocko@suse.com, rdunlap@infradead.org, lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com References: <20250428181218.85925-1-npache@redhat.com> <20250428181218.85925-8-npache@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 WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzSREYXZpZCBIaWxk ZW5icmFuZCA8ZGF2aWRAcmVkaGF0LmNvbT7CwZgEEwEIAEICGwMGCwkIBwMCBhUIAgkKCwQW AgMBAh4BAheAAhkBFiEEG9nKrXNcTDpGDfzKTd4Q9wD/g1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sUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 02.05.25 17:30, Nico Pache wrote: > On Fri, May 2, 2025 at 9:27 AM Jann Horn wrote: >> >> On Fri, May 2, 2025 at 5:19 PM David Hildenbrand wrote: >>> >>> On 02.05.25 14:50, Jann Horn wrote: >>>> On Fri, May 2, 2025 at 8:29 AM David Hildenbrand wrote: >>>>> On 02.05.25 00:29, Nico Pache wrote: >>>>>> On Wed, Apr 30, 2025 at 2:53 PM Jann Horn wrote: >>>>>>> >>>>>>> On Mon, Apr 28, 2025 at 8:12 PM Nico Pache wrote: >>>>>>>> Introduce the ability for khugepaged to collapse to different mTHP sizes. >>>>>>>> While scanning PMD ranges for potential collapse candidates, keep track >>>>>>>> of pages in KHUGEPAGED_MIN_MTHP_ORDER chunks via a bitmap. Each bit >>>>>>>> represents a utilized region of order KHUGEPAGED_MIN_MTHP_ORDER ptes. If >>>>>>>> mTHPs are enabled we remove the restriction of max_ptes_none during the >>>>>>>> scan phase so we dont bailout early and miss potential mTHP candidates. >>>>>>>> >>>>>>>> After the scan is complete we will perform binary recursion on the >>>>>>>> bitmap to determine which mTHP size would be most efficient to collapse >>>>>>>> to. max_ptes_none will be scaled by the attempted collapse order to >>>>>>>> determine how full a THP must be to be eligible. >>>>>>>> >>>>>>>> If a mTHP collapse is attempted, but contains swapped out, or shared >>>>>>>> pages, we dont perform the collapse. >>>>>>> [...] >>>>>>>> @@ -1208,11 +1211,12 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, >>>>>>>> vma_start_write(vma); >>>>>>>> anon_vma_lock_write(vma->anon_vma); >>>>>>>> >>>>>>>> - mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, address, >>>>>>>> - address + HPAGE_PMD_SIZE); >>>>>>>> + mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, _address, >>>>>>>> + _address + (PAGE_SIZE << order)); >>>>>>>> mmu_notifier_invalidate_range_start(&range); >>>>>>>> >>>>>>>> pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */ >>>>>>>> + >>>>>>>> /* >>>>>>>> * This removes any huge TLB entry from the CPU so we won't allow >>>>>>>> * huge and small TLB entries for the same virtual address to >>>>>>> >>>>>>> It's not visible in this diff, but we're about to do a >>>>>>> pmdp_collapse_flush() here. pmdp_collapse_flush() tears down the >>>>>>> entire page table, meaning it tears down 2MiB of address space; and it >>>>>>> assumes that the entire page table exclusively corresponds to the >>>>>>> current VMA. >>>>>>> >>>>>>> I think you'll need to ensure that the pmdp_collapse_flush() only >>>>>>> happens for full-size THP, and that mTHP only tears down individual >>>>>>> PTEs in the relevant range. (That code might get a bit messy, since >>>>>>> the existing THP code tears down PTEs in a detached page table, while >>>>>>> mTHP would have to do it in a still-attached page table.) >>>>>> Hi Jann! >>>>>> >>>>>> I was under the impression that this is needed to prevent GUP-fast >>>>>> races (and potentially others). >>>> >>>> Why would you need to touch the PMD entry to prevent GUP-fast races for mTHP? >>>> >>>>>> As you state here, conceptually the PMD case is, detach the PMD, do >>>>>> the collapse, then reinstall the PMD (similarly to how the system >>>>>> recovers from a failed PMD collapse). I tried to keep the current >>>>>> locking behavior as it seemed the easiest way to get it right (and not >>>>>> break anything). So I keep the PMD detaching and reinstalling for the >>>>>> mTHP case too. As Hugh points out I am releasing the anon lock too >>>>>> early. I will comment further on his response. >>>> >>>> As I see it, you're not "keeping" the current locking behavior; you're >>>> making a big implicit locking change by reusing a codepath designed >>>> for PMD THP for mTHP, where the page table may not be exclusively >>>> owned by one VMA. >>> >>> That is not the intention. The intention in this series (at least as we >>> discussed) was to not do it across VMAs; that is considered the next >>> logical step (which will be especially relevant on arm64 IMHO). >> >> Ah, so for now this is supposed to only work for PTEs which are in a >> PMD which is fully covered by the VMA? So if I make a 16KiB VMA and >> then try to collapse its contents to an order-2 mTHP page, that should >> just not work? > Correct! As I started in reply to Hugh, the locking conditions explode > if we drop that requirement. Right. Adding to that, one could evaluate how much we would gain by trying to lock, say, up to $MAGIC_NUMBER related VMAs. Of course, if no other VMA spans the PMD, and the VMA only covers it partially, it is likely still fine as long as we hold the mmap lock in write mode. But probably, looking into a different locking scheme would be beneficial at this point. -- Cheers, David / dhildenb