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 D2AD8193419 for ; Thu, 2 Jan 2025 11:22:36 +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=1735816958; cv=none; b=XIP7E28PmVC0pdJWEanDXDxBVrLRM9tW/7tQjMGiu1Lx1jK0ffDcZed5bAEJy8yX10WtIfyyC6FHOMBOYvG24O3A3usDTENQABa+SAdviYrPXFWbeLhO6dbIMy1idnfqMtOhWIjLbyRFDJ/WH9Fsn2XXAVE3GjQyP24CKGTcAcE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735816958; c=relaxed/simple; bh=5qCjMF9gA40hYN27YpX3HTReHVvuvkkIkCGlvrMng0k=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=FCOOnU9lM/NTrJk5N2pgjteg/g2HEi32yND8JlKlq5dhbr+WBsr2ZPTptofN8vrujOH6Y2BH9v7VssJ0GAWVHWDYSFR5ZSrQcb2EoLPK0E1jEuZhuxTSw1LctmFgGK2TAf3gh8pU6nMNECF1i4EsnXcizOKZ31UoUjgt9naSGZw= 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=fNSyMTYc; arc=none smtp.client-ip=170.10.133.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="fNSyMTYc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1735816955; 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=zz8jiD9AE53PAKIoF9KPlvEa23mPZZnpSL5k7UoaRe4=; b=fNSyMTYcMr2jTcocUIp7395yNG6UppdhHeY/sts17KTS35ch67J4aySEDfihLSjsJubplY TGgUBsC2ScWjgxOFlq+si/WY6CjY3OKIqRqewZcardj1Uu4lLkYygPKnOCF55BONhuWMOz NeoUHzaPzoxLhHUp6jewNggXbpN1H6Y= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-654-weuSsBVfMwuNSfkIEOE8kQ-1; Thu, 02 Jan 2025 06:22:34 -0500 X-MC-Unique: weuSsBVfMwuNSfkIEOE8kQ-1 X-Mimecast-MFC-AGG-ID: weuSsBVfMwuNSfkIEOE8kQ Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-38629a685fdso1935097f8f.2 for ; Thu, 02 Jan 2025 03:22:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735816953; x=1736421753; 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=zz8jiD9AE53PAKIoF9KPlvEa23mPZZnpSL5k7UoaRe4=; b=Nmg5fgrLy3K2n0Ys+WnnWxp6+irWaacTcXhPH+kvFBOZsAWGNCIFuZIjd/95NqLoRq 7OZk/41amdIpaoytT30Xx2hYObgqqC2otz5OUfV9K2Vi/HHhDINFwTSB0KyWMB/up/Gy FYnFlQIK+msAwecsaZLdyPQfVEJFAYWG6yRcSXZ5g4N1QMJ+QCn+ZuxDB4PBFgPxJTgJ 1QpE5kFQeQcfG8kiuK5xVXldWa/NBkDP1gU98oYA2dncyc1kAgCXDZPruhxGU/c7OAYm w5maanPU/yJsIW789tkKMqAqFOarepE7kn15CPing7+VsnrjPCTZ0szO9oAmQ6mz/Nwk yD3g== X-Forwarded-Encrypted: i=1; AJvYcCXceP++7sljyg3aQ+bHnRTWv2xDPup1rHEcs2tfSj+wh3QoiwpK0uipHeH45apc12wS3mpCEycrHsrUUII=@vger.kernel.org X-Gm-Message-State: AOJu0YyMQcuVOQjgRWH3ghTe4aV/Oo7eonoKttND/L5xqyHR7Ze/Je1U FnHjUy5ytNr8piOOmg74ne1Noa6Na0ISNen4LhKq5GXFE00mP4Z7wALZCi6mqfb5ZZhxZNyXphp U5dSkLw+IJmHIMNk3UgBrjTa7YeMXWQ4Cr6NED5rnnUx4zdYQPeDaZ0pqy/o3Yw== X-Gm-Gg: ASbGncuNLEup9y8Tts4cPOcwva/FoEsZHAuuqTwhU6cc7FYonA7EX1ep4S1uRNaeRMi KtSQ2HH40S/H4O7800mXVp+L5hLsYVaX6Ucp0o7EL2WuVp5zyTf8VsEbbkwAJRWFFGx7FfOZT3V V+DsSAy8GZ1w/xdBjzJ1syF70ojRvG3UbPT+qChm4ovk8OA4+OCaHVj6Tgfe4Gq8Ju4DQL0znm6 k+bfwC2KTX5XkPObya9yb33+bx51MOPkf4DCp8oMj8r9D5NnYp2ZK9ZiqCgPi5/jsMmQllYXtYf XICaMdyLARqTzr52lAfTjCY/pxXDL1zTXMOSXg7T5yfSJS0dB7wNe0mRW18vYmppDvCVlhc7sbs 5Jcf4CVjk X-Received: by 2002:a05:6000:156f:b0:386:2aba:a7f6 with SMTP id ffacd0b85a97d-38a224071e3mr36512125f8f.49.1735816953237; Thu, 02 Jan 2025 03:22:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IHA2hV9dMb6WkXJbkn5Z4VCd8Ed9IHoPy+VMrmmgAHuoLYLMIJD0KtaAXAPXFnvKGJiXrKQgA== X-Received: by 2002:a05:6000:156f:b0:386:2aba:a7f6 with SMTP id ffacd0b85a97d-38a224071e3mr36512078f8f.49.1735816952705; Thu, 02 Jan 2025 03:22:32 -0800 (PST) Received: from ?IPV6:2003:cb:c735:9400:f91f:1f24:e403:81fe? (p200300cbc7359400f91f1f24e40381fe.dip0.t-ipconnect.de. [2003:cb:c735:9400:f91f:1f24:e403:81fe]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c832e31sm37377155f8f.33.2025.01.02.03.22.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Jan 2025 03:22:31 -0800 (PST) Message-ID: <448c2f2c-ca9f-43cf-8e94-968204228913@redhat.com> Date: Thu, 2 Jan 2025 12:22:28 +0100 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: [RFC PATCH 09/12] khugepaged: Introduce vma_collapse_anon_folio() To: Dev Jain , akpm@linux-foundation.org, willy@infradead.org, kirill.shutemov@linux.intel.com Cc: ryan.roberts@arm.com, anshuman.khandual@arm.com, catalin.marinas@arm.com, cl@gentwo.org, vbabka@suse.cz, mhocko@suse.com, apopple@nvidia.com, dave.hansen@linux.intel.com, will@kernel.org, baohua@kernel.org, jack@suse.cz, srivatsa@csail.mit.edu, haowenchao22@gmail.com, hughd@google.com, aneesh.kumar@kernel.org, yang@os.amperecomputing.com, peterx@redhat.com, ioworker0@gmail.com, wangkefeng.wang@huawei.com, ziy@nvidia.com, jglisse@google.com, surenb@google.com, vishal.moola@gmail.com, zokeefe@google.com, zhengqi.arch@bytedance.com, jhubbard@nvidia.com, 21cnbao@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20241216165105.56185-1-dev.jain@arm.com> <20241216165105.56185-10-dev.jain@arm.com> <2215dd8e-233a-427b-b15c-a2ffbce8f46d@redhat.com> <28013908-65d8-462e-b975-cd0f63d226b1@arm.com> <0368f4f2-cb0f-4633-a86d-5c3f75839b4e@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/g1oFAl8Ox4kFCRKpKXgACg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nhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Still on PTO, but replying to this mail :) >>>> >>>> To Nico, I suggested doing it simple initially, and still clear the >>>> high-level PMD entry + flush under mmap write lock, then re-map the >>>> PTE table after modifying the page table. It's not as efficient, but >>>> "harder to get wrong". >>>> >>>> Maybe that's already happening, but I stumbled over this clearing >>>> logic in __collapse_huge_page_copy_succeeded(), so I'm curious. >>> >>> No, I am not even touching the PMD. I guess the sequence you described >>> should work? I just need to reverse the copying and PTE clearing order >>> to implement this sequence. >> >> That would work, but you really have to hold the PTL for the whole >> period: from when you temporarily clear PTEs +_ flush the TLB, when >> you copy, until you re-insert the updated ones. > > Ignoring the implementation and code churn part :) Is the following > algorithm theoretically correct: (1) Take PTL, scan PTEs, > isolate and lock the folios, set the PTEs to migration entries, check > folio references. This will solve concurrent write > access races. > Now, we can drop the PTL...no one can write to the old> folios because (1) rmap cannot run (2) folio from PTE > cannot be derived. Note that migration_entry_wait_on_locked() path can > be scheduled out, so this is not the same as the > fault handlers spinning on the PTL. We can now safely copy old folios to > new folio, then take the PTL: The PTL is > available because every pagetable walker will see a migration entry and > back off. We batch set the PTEs now, and release > the folio locks, making the fault handlers getting out of > migration_entry_wait_on_locked(). As compared to the old code, > the point of failure we need to handle is when copying fails, or at some > point folio isolation fails...therefore, we need to > maintain a list of old PTEs corresponding to the PTEs set to migration > entries. > > Note that, I had suggested this "setting the PTEs to a global invalid > state" thingy in our previous discussion too, but I guess > simultaneously working on the PMD and PTE was the main problem there, > since the walkers do not take a lock on the PMD > to check if someone is changing it, when what they really are interested > in is to make change at the PTE level. In fact, leaving > all specifics like racing with a specific pagetable walker etc aside, I > do not see why the following claim isn't true: > > Claim: The (anon-private) mTHP khugepaged collapse problem is > mathematically equivalent to the (anon-private) page migration problem. > > The difference being, in khugepaged we need the VMA to be stable, hence > have to take the mmap_read_lock(), and have to "migrate" > to a large folio instead of individual pages. > > If at all my theory is correct, I'll leave it to the community to decide > if it's worth it to go through my brain-rot :) What we have to achieve is a) Make sure GUP-fast cannot grab the folio b) The CPU cannot read/write the folio c) No "ordinary" page table walkers can grab the folio. Handling a) and b) involves either invalidating (incl migration entry) or temporarily clearing (what we do right now for the PMD) the affected entry and flushing the TLB. We can use migration entries while the folio is locked; there might be some devil in the detail, so I would suggest to going with something simpler first, and then try making use of migration entries. > > >> >> When having to back-off (restore original PTEs), or for copying, >> you'll likely need access to the original PTEs, which were already >> cleared. So likely you need a temporary copy of the original PTEs >> somehow. >> >> That's why temporarily clearing the PMD und mmap write lock is easier >> to implement, at the cost of requiring the mmap lock in write mode >> like PMD collapse. >> >> > So, I understand the following: Some CPU spinning on the PTL for a long > time is worse than taking the mmap_write_lock(). The latter blocks this > process > from doing mmap()s, which, in my limited knowledge, is bad for > memory-intensive processes (aligned with the fact that the maple tree was > introduced to optimize VMA operations), and the former literally nukes > one unit of computation from the system for a long time. With per-VMA locks, khugepaged grabbing the mmap long in write mode got "less" bad, because most page fault can still make progress. But it's certainly still suboptimal. Yes, I think having a lot of thread spinning for a long time for a PTL can be worse than using a sleepable lock in some scenarios I think; especially if the PTL spans more than a single page table. -- Cheers, David / dhildenb