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 E9BF1376F1 for ; Mon, 1 Sep 2025 08:00:35 +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=1756713637; cv=none; b=EgWcZisKjm13j8WS57OH2lA5yBUyHi9BRRPuaX+39sTJuA4x1eB87A/tgf1rTLcTAjz4jUc2kIF1ggbCx7tiBkaO7KWvgNEYhS+mQTr+ksmlI7ZkYLq1oRrfMDZYbp1dz/p7vVnjM9sy3LNg8lY+qBQMpWx/5eu6jXElZOLwUmI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756713637; c=relaxed/simple; bh=/8yD6G2R6GdJMDu/MrhaSmC1K5Y2isSkZ3HH8jWMv+c=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=KAHtXb2mjTaD44aZnkMvhTwSfDh4q/gmY9SWwEKiI09t7SBHZElkPsrpuMLJNC2/zD6o4F7QaKJMmibzEbS7k1xWRYy8qgsJ0d+XFTq1c6VEvwLJpAKyPAcv60AJ/lWj7Lp40JxdpIEt8qkIXi5/wD6j+IgdLukqjiTJcEYeJ40= 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=Jm6EwUhs; 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="Jm6EwUhs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1756713635; 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=nMoScysp6W8agnmS99/Jo+BknK/QCKLcEVvBRO/BrfU=; b=Jm6EwUhstuMDrUSV0ayGkoR3nDxhVfsLsTU1/y5LFMHALbK+umvAGQg/sMYpW3kgkbf3Eu SJjAQWevTl4Fi2WTN7RjfrhSmz8Fi5DEnlJR8u4sKhgWFAR/jtrsgH9IibP3qPGtjfDgr6 fARolCmvHBi9Btz6Uvg4NAH9QaurEpg= 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-531-Hlu-k8bINGW9_czoe_CkpQ-1; Mon, 01 Sep 2025 04:00:33 -0400 X-MC-Unique: Hlu-k8bINGW9_czoe_CkpQ-1 X-Mimecast-MFC-AGG-ID: Hlu-k8bINGW9_czoe_CkpQ_1756713632 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-45b869d35e6so8766825e9.1 for ; Mon, 01 Sep 2025 01:00:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756713632; x=1757318432; 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=nMoScysp6W8agnmS99/Jo+BknK/QCKLcEVvBRO/BrfU=; b=gUgxZpfmWkBVY/6UFdk+xAGAXg/1ha0BR+E3+00f3L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received: by 2002:a05:600c:524d:b0:45b:8a10:e5a7 with SMTP id 5b1f17b1804b1-45b8a10e8c5mr38768435e9.37.1756713630591; Mon, 01 Sep 2025 01:00:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFLQBpKydXLPgNuaYH1v/Kwcnc9w+AqlJhCRAo3rSYLANWpzHzUcARkSJmgNUWV3Yjyip67yQ== X-Received: by 2002:a05:600c:524d:b0:45b:8a10:e5a7 with SMTP id 5b1f17b1804b1-45b8a10e8c5mr38768115e9.37.1756713630019; Mon, 01 Sep 2025 01:00:30 -0700 (PDT) Received: from ?IPV6:2003:d8:2f37:2b00:948c:dd9f:29c8:73f4? (p200300d82f372b00948cdd9f29c873f4.dip0.t-ipconnect.de. [2003:d8:2f37:2b00:948c:dd9f:29c8:73f4]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3cf276d5e5fsm14200992f8f.27.2025.09.01.01.00.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 01 Sep 2025 01:00:29 -0700 (PDT) Message-ID: Date: Mon, 1 Sep 2025 10:00:28 +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 2/7] mm/gup: check ref_count instead of lru before migration To: Hugh Dickins , Andrew Morton Cc: Will Deacon , Shivank Garg , Matthew Wilcox , Christoph Hellwig , Keir Fraser , Jason Gunthorpe , John Hubbard , Frederick Mayle , Peter Xu , "Aneesh Kumar K.V" , Johannes Weiner , Vlastimil Babka , Alexander Krabler , Ge Yang , Li Zhe , Chris Li , Yu Zhao , Axel Rasmussen , Yuanchu Xie , Wei Xu , Konstantin Khlebnikov , linux-kernel@vger.kernel.org, linux-mm@kvack.org References: <47c51c9a-140f-1ea1-b692-c4bae5d1fa58@google.com> From: David Hildenbrand Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zY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Ruj1m73D 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: <47c51c9a-140f-1ea1-b692-c4bae5d1fa58@google.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 31.08.25 11:05, Hugh Dickins wrote: > Will Deacon reports:- > > When taking a longterm GUP pin via pin_user_pages(), > __gup_longterm_locked() tries to migrate target folios that should not > be longterm pinned, for example because they reside in a CMA region or > movable zone. This is done by first pinning all of the target folios > anyway, collecting all of the longterm-unpinnable target folios into a > list, dropping the pins that were just taken and finally handing the > list off to migrate_pages() for the actual migration. > > It is critically important that no unexpected references are held on the > folios being migrated, otherwise the migration will fail and > pin_user_pages() will return -ENOMEM to its caller. Unfortunately, it is > relatively easy to observe migration failures when running pKVM (which > uses pin_user_pages() on crosvm's virtual address space to resolve > stage-2 page faults from the guest) on a 6.15-based Pixel 6 device and > this results in the VM terminating prematurely. > > In the failure case, 'crosvm' has called mlock(MLOCK_ONFAULT) on its > mapping of guest memory prior to the pinning. Subsequently, when > pin_user_pages() walks the page-table, the relevant 'pte' is not > present and so the faulting logic allocates a new folio, mlocks it > with mlock_folio() and maps it in the page-table. > > Since commit 2fbb0c10d1e8 ("mm/munlock: mlock_page() munlock_page() > batch by pagevec"), mlock/munlock operations on a folio (formerly page), > are deferred. For example, mlock_folio() takes an additional reference > on the target folio before placing it into a per-cpu 'folio_batch' for > later processing by mlock_folio_batch(), which drops the refcount once > the operation is complete. Processing of the batches is coupled with > the LRU batch logic and can be forcefully drained with > lru_add_drain_all() but as long as a folio remains unprocessed on the > batch, its refcount will be elevated. > > This deferred batching therefore interacts poorly with the pKVM pinning > scenario as we can find ourselves in a situation where the migration > code fails to migrate a folio due to the elevated refcount from the > pending mlock operation. > > Hugh Dickins adds:- > > !folio_test_lru() has never been a very reliable way to tell if an > lru_add_drain_all() is worth calling, to remove LRU cache references > to make the folio migratable: the LRU flag may be set even while the > folio is held with an extra reference in a per-CPU LRU cache. > > 5.18 commit 2fbb0c10d1e8 may have made it more unreliable. Then 6.11 > commit 33dfe9204f29 ("mm/gup: clear the LRU flag of a page before adding > to LRU batch") tried to make it reliable, by moving LRU flag clearing; > but missed the mlock/munlock batches, so still unreliable as reported. > > And it turns out to be difficult to extend 33dfe9204f29's LRU flag > clearing to the mlock/munlock batches: if they do benefit from batching, > mlock/munlock cannot be so effective when easily suppressed while !LRU. > > Instead, switch to an expected ref_count check, which was more reliable > all along: some more false positives (unhelpful drains) than before, and > never a guarantee that the folio will prove migratable, but better. > > Note for stable backports: requires 6.16 commit 86ebd50224c0 ("mm: > add folio_expected_ref_count() for reference count calculation") and > 6.17 commit ("mm: fix folio_expected_ref_count() when PG_private_2"). > > Reported-by: Will Deacon > Link: https://lore.kernel.org/linux-mm/20250815101858.24352-1-will@kernel.org/ > Fixes: 9a4e9f3b2d73 ("mm: update get_user_pages_longterm to migrate pages allocated from CMA region") > Signed-off-by: Hugh Dickins > Cc: > --- > mm/gup.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/mm/gup.c b/mm/gup.c > index adffe663594d..82aec6443c0a 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -2307,7 +2307,8 @@ static unsigned long collect_longterm_unpinnable_folios( > continue; > } > > - if (!folio_test_lru(folio) && drain_allow) { > + if (drain_allow && folio_ref_count(folio) != > + folio_expected_ref_count(folio) + 1) { > lru_add_drain_all(); > drain_allow = false; > } In general, to the fix idea Acked-by: David Hildenbrand But as raised in reply to patch #1, we have to be a bit careful about including private_2 in folio_expected_ref_count() at this point. If we cannot include it in folio_expected_ref_count(), it's all going to be a mess until PG_private_2 is removed for good. So that part still needs to be figured out. -- Cheers David / dhildenb