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 0D6A2298CD7 for ; Thu, 28 Aug 2025 20:38:56 +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=1756413538; cv=none; b=sgbG1gHbVOGtoh5SFDOWptwcd853VNTBG4ZKQOrQLVN5X92dgYrxpXV6sUCwgngeG499FZeLHk5AudSyZRqreTixYa9P4OJuds8X+bNG/3xAJ1x8rCGUyuoIEOIpHIKWwI/9kMtMga+n3qlxiWaPssDQYvwM4tTs2HZeXbUiQMM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756413538; c=relaxed/simple; bh=anLVQJl4AjBIEJag0sUwCnvTo8CcHix1MyVpizJDkTo=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=LQ7AKIbxYP1kukopRx+dDiV0mAC89hBZ0VnwUUavqbvjpAxtZ1UK0a7JSv4Pywv7AT/L4i1GOveay3pGF9APfOfc1RaGevB3V7S0YOwdtOkLClV06D6A11tyyJYqHqZHSFbY5gfJrDztb3YfLho2j9HBW9kIq9B38Ff1JOv9Q2o= 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=Uiy/CrkH; 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="Uiy/CrkH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1756413536; 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=xwZlhU01+EV1yk20M/2DYD/S7JKHDAZ7Ux0lm8SsNeM=; b=Uiy/CrkHe7vOncwO95CyfFcnKLmKywnCyJocAMNWGGozlJjjYm6W9WmsVvXQW7H1rzdrNx EtDyZ01rnKyCaEGJxdLoPOAEUfi6KjX50DAJLsjR8x5OQQFRu84Oe7YQzdGf/ChvyQGjXr p+jQ8NhQjsbF094P6zOJYHdjuM/mwV0= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-500-s6JytKNkPYaWzji2ALKWhQ-1; Thu, 28 Aug 2025 16:38:54 -0400 X-MC-Unique: s6JytKNkPYaWzji2ALKWhQ-1 X-Mimecast-MFC-AGG-ID: s6JytKNkPYaWzji2ALKWhQ_1756413533 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-45b74ec7cd0so8910045e9.2 for ; Thu, 28 Aug 2025 13:38:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756413533; x=1757018333; 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=xwZlhU01+EV1yk20M/2DYD/S7JKHDAZ7Ux0lm8SsNeM=; b=Ywha7M+jwR8e7BN8E9QF0Hax2m0N5qXr89ya/UfOICpgikFeqRbv9uq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X-Received: by 2002:a05:600c:1caa:b0:458:bbed:a806 with SMTP id 5b1f17b1804b1-45b6503919emr91828985e9.22.1756413532902; Thu, 28 Aug 2025 13:38:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEIljhHhdTBn7DEps/7ORUXkYgebAkXS/5XDYdHFvBoLjnLL6phiG8TBqHSGcnisU+IkK5BMQ== X-Received: by 2002:a05:600c:1caa:b0:458:bbed:a806 with SMTP id 5b1f17b1804b1-45b6503919emr91828905e9.22.1756413532445; Thu, 28 Aug 2025 13:38:52 -0700 (PDT) Received: from ?IPV6:2003:d8:2f28:c100:2225:10aa:f247:7b85? (p200300d82f28c100222510aaf2477b85.dip0.t-ipconnect.de. [2003:d8:2f28:c100:2225:10aa:f247:7b85]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3cf276c788esm596872f8f.17.2025.08.28.13.38.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 28 Aug 2025 13:38:51 -0700 (PDT) Message-ID: <56819052-d3f5-4209-824d-5cfbf49ff6e9@redhat.com> Date: Thu, 28 Aug 2025 22:38:50 +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 Cc: Will Deacon , 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 , Ge Yang References: <20250815101858.24352-1-will@kernel.org> <9e7d31b9-1eaf-4599-ce42-b80c0c4bb25d@google.com> <8376d8a3-cc36-ae70-0fa8-427e9ca17b9b@google.com> <3194a67b-194c-151d-a961-08c0d0f24d9b@google.com> From: David Hildenbrand Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1x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dZ5eEkj1dCyx+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: <3194a67b-194c-151d-a961-08c0d0f24d9b@google.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 28.08.25 18:12, Hugh Dickins wrote: > On Thu, 28 Aug 2025, David Hildenbrand wrote: >> On 28.08.25 10:47, Hugh Dickins wrote: > ... >>> It took several days in search of the least bad compromise, but >>> in the end I concluded the opposite of what we'd intended above. >>> >>> There is a fundamental incompatibility between my 5.18 2fbb0c10d1e8 >>> ("mm/munlock: mlock_page() munlock_page() batch by pagevec") >>> and Ge Yang's 6.11 33dfe9204f29 >>> ("mm/gup: clear the LRU flag of a page before adding to LRU batch"). >>> >>> It turns out that the mm/swap.c folio batches (apart from lru_add) >>> are all for best-effort, doesn't matter if it's missed, operations; >>> whereas mlock and munlock are more serious. Probably mlock could >>> be (not very satisfactorily) converted, but then munlock? Because >>> of failed folio_test_clear_lru()s, it would be far too likely to >>> err on either side, munlocking too soon or too late. >>> >>> I've concluded that one or the other has to go. If we're having >>> a beauty contest, there's no doubt that 33dfe9204f29 is much nicer >>> than 2fbb0c10d1e8 (which is itself far from perfect). But functionally, >>> I'm afraid that removing the mlock/munlock batching will show up as a >>> perceptible regression in realistic workloadsg; and on consideration, >>> I've found no real justification for the LRU flag clearing change. >> >> Just to understand what you are saying: are you saying that we will go back to >> having a folio being part of multiple LRU caches? > > Yes. Well, if you count the mlock/munlock batches in as "LRU caches", > then that has been so all along. Yes ... > >> :/ If so, I really rally >> hope that we can find another way and not go back to that old handling. > > For what reason? It sounded like a nice "invariant" to keep in mind, > but it's a limitation, and what purpose was it actually serving? I liked the semantics that if !lru, there could be at most one reference from the LRU caches. That is, if there are two references, you don't even have to bother with flushing anything. > > If it's the "spare room in struct page to keep the address of that one > batch entry ... efficiently extract ..." that I was dreaming of: that > has to be a future thing, when perhaps memdescs will allow an extensible > structure to be attached, and extending it for an mlocked folio (to hold > the mlock_count instead of squeezing it into lru.prev) would not need > mlock/munlock batching at all (I guess: far from uppermost in my mind!), > and including a field for "efficiently extract" from LRU batch would be > nice. > > But, memdescs or not, there will always be pressure to keep the common > struct as small as possible, so I don't know if we would actually go > that way. > > But I suspect that was not your reason at all: please illuminate. You are very right :) Regarding the issue at hand: There were discussions at LSF/MM about also putting (some) large folios onto the LRU caches. In that context, GUP could take multiple references on the same folio, and a simple folio_expected_ref_count() + 1 would no longer do the trick. I thought about this today, and likely it could be handled by scanning the page array for same folios etc. A bit nasty when wanting to cover all corner cases (folio pages must not be consecutive in the passed array ) ... Apart from that issue, I liked the idea of a "single entry in the cache" for other reasons: it gives clear semantics. We cannot end up in a scenario where someone performs OPX and later someone OPY on a folio, but the way the lru caches are flushed we might end up processing OPX after OPY -- this should be able to happen in case of local or remote flushes IIRC. Anyhow, I quickly scanned your code. The folio_expected_ref_count() should solve the issue for now. It's quite unfortunate that any raised reference will make us now flush all remote LRU caches. Maybe we just want to limit it to !folio_test_large(), because flushing there really doesn't give us any chance in succeeding right now? Not sure if it makes any difference in practice, though, we'll likely be flushing remote LRU caches now more frequently either way. -- Cheers David / dhildenb