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 69F5554673 for ; Sat, 16 Aug 2025 08:16:06 +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=1755332168; cv=none; b=E0joP8UlAjx55+8c7FrJZ/XqirG2tAR3ktNx6/5jrYF8qygNHfAMZMvACD7i8WCYBRcxcVUO9igAzphKHeg+fST0EDiWfPWM3Z7plknKtRc9sNH5B4x0Nhtjd1b3LVL2xYiS92J5ia9jzTMkWs4vZESjUUAtQTAth3qW6ZXydZw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755332168; c=relaxed/simple; bh=/5Yd7LteZK9ygBBMa5bwCKlmbRlKsf1aUubz/9JoKZQ=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=MQBHpm5LTFHc/JJs/pCubAGw88kYf2I9eOp5EMFs6lqGUzgfyENfJRyHOSpgo4llWmMN8+dBBjm0URRZw3uIkrk1+h7nk7sJ0Nke/nYMCvF0XY7pVQnY6wOQ6F6L9Mm65VSPVDQoTrjfDkTIm8XPXR4GnXbc+zGFnp5VRy04V74= 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=bjrC1gPV; 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="bjrC1gPV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755332165; 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=ixPgA/yl6vc1N1EOYUywRtyrJmPMeh0Q/AJGU5hM31I=; b=bjrC1gPVDSwmwQpMCqliTpsx0zzcMa/ElITQhytxjNpTMRPUtqZfbxqTloBQgHKTDJHSig e32n+FeDb0E6RNd62/cj9LkUSsZxsunPj1NCF9vIfFQZQzUfE+I3gdfdfDHnwOzZea2i/o iq9mdjNCLdbyGLSNYNAZ7h3gU/4+tgM= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-460-yl1s_Vw4P_iIRnssCWCyyA-1; Sat, 16 Aug 2025 04:16:03 -0400 X-MC-Unique: yl1s_Vw4P_iIRnssCWCyyA-1 X-Mimecast-MFC-AGG-ID: yl1s_Vw4P_iIRnssCWCyyA_1755332162 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-45a1b0ccb6cso10800805e9.3 for ; Sat, 16 Aug 2025 01:16:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755332162; x=1755936962; 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=ixPgA/yl6vc1N1EOYUywRtyrJmPMeh0Q/AJGU5hM31I=; b=Oh2gZqJaP7DU/c9+scc49TogDF8IM3z+wUdLP0Jlj+QJHNn0uU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MQ= X-Received: by 2002:a05:600c:a43:b0:459:db69:56bd with SMTP id 5b1f17b1804b1-45a21839e02mr35650855e9.20.1755332162236; Sat, 16 Aug 2025 01:16:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFC3tYNHjWj0KMeKalzdh/MjaeikwufS6vOtijOjsl7Nwlxk/mLPaTTfDxnVXlSUFEAcUfeOA== X-Received: by 2002:a05:600c:a43:b0:459:db69:56bd with SMTP id 5b1f17b1804b1-45a21839e02mr35650575e9.20.1755332161730; Sat, 16 Aug 2025 01:16:01 -0700 (PDT) Received: from ?IPV6:2003:d8:2f23:c700:d8ae:41bf:492a:9e4c? (p200300d82f23c700d8ae41bf492a9e4c.dip0.t-ipconnect.de. [2003:d8:2f23:c700:d8ae:41bf:492a:9e4c]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-45a22210ab4sm47375495e9.3.2025.08.16.01.16.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 16 Aug 2025 01:16:01 -0700 (PDT) Message-ID: <92b16e68-0083-48aa-b281-5c235fd91bfe@redhat.com> Date: Sat, 16 Aug 2025 10:15:59 +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] mm/gup: Drain batched mlock folio processing before attempting migration To: Hugh Dickins , Will Deacon Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Keir Fraser , Jason Gunthorpe , John Hubbard , Frederick Mayle , Andrew Morton , Peter Xu , Rik van Riel , Vlastimil Babka References: <20250815101858.24352-1-will@kernel.org> From: David Hildenbrand Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy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AcLBfAQYAQgAJgIbDBYhBBvZyq1zXEw6Rg38yk3e 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 On 16.08.25 06:14, Hugh Dickins wrote: > On Fri, 15 Aug 2025, Will Deacon wrote: > >> 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. > > Thanks for the very full description, Will, that helped me a lot > (I know very little of the GUP pinning end). > > But one thing would help me to understand better: are the areas being > pinned anonymous or shmem or file memory (or COWed shmem or file)? > > From "the faulting logic allocates a new folio" I first assumed > anonymous, but later came to think "mlocks it with mlock_folio()" > implies they are shmem or file folios (which, yes, can also be > allocated by fault). > > IIRC anonymous and COW faults would go the mlock_new_folio() way, > where the folio goes on to the mlock folio batch without having yet > reached LRU: those should be dealt with by the existing > !folio_test_lru() check. Very right. Because I don't remember easily running into that in wp_can_reuse_anon_folio(), where we also drain the lru if we spot a !lru folio. Having to enlighten all such code to check for if (!folio_test_lru(folio) || folio_test_mlocked(folio)) is a bit suboptimal, but we could add a helper for that. (we might end up draining for actually mlocked folios though :( ) We recently changed in 33dfe9204f29 ("mm/gup: clear the LRU flag of a page before adding to LRU batch") that a folio can really only be once in an LRU batch. So if we spot !folio_test_lru(folio) there might be at most one reference from LRU batches. So naturally I wonder if we could somehow make it work that a folio is either on the ordinary LRU batch or on the mlock batch, but not on both. And that the folio LRU flag would be sufficient to check. That would mean that we would clear the LRU flag when adding a folio to the mlock batch. But not sure how feasible that is (e.g., folio already isolated). -- Cheers David / dhildenb