[PATCH] tmp
From: David Hildenbrand (Arm)
Date: Thu Jul 30 2026 - 06:20:14 EST
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
mm/gup.c | 66 ++++++++++++++++++++++++++++++++++++++++++++------------
1 file changed, 52 insertions(+), 14 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c
index 99902c15703b0..ec43071bd035a 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -2259,6 +2259,56 @@ static struct folio *pofs_next_folio(struct folio *folio,
return pofs_get_folio(pofs, i);
}
+enum lru_cache_drained {
+ LRU_CACHE_NOT_DRAINED,
+ LRU_CACHE_DRAINED,
+ LRU_CACHE_DRAINED_ALL,
+};
+
+static inline void folio_likely_lru_cached(const struct folio *folio)
+{
+ if (!folio_may_be_lru_cached(folio))
+ return false;
+ /*
+ * Having the LRU flag clear either indicates LRU cache references
+ * or LRU isolation.
+ */
+ if (!folio_test_lru(folio))
+ return true;
+ /*
+ * For mlocked folios, we don't have a real indication: we can only
+ * take a guess based on the refcount.
+ */
+ if (!folio_test_mlock(folio))
+ return false;
+ return folio_expected_ref_count(folio) == folio_ref_count(folio) = 1;
+}
+
+/**
+ * lru_cache_drain_for_folio() - progressively try draining the lru cache
+ * @folio: The folio.
+ * @drained: Status initialized to LRU_CACHE_NOT_DRAINED by the caller.
+ *
+ * TODO
+ */
+static void lru_cache_drain_for_folio(const struct folio *folio,
+ enum lru_cache_drained *drained)
+{
+ if (!folio_likely_lru_cached(folio))
+ return false;
+
+ /* Try local draining first, if not already done previously. */
+ if (*drained == LRU_CACHE_NOT_DRAINED) {
+ lru_add_drain();
+ *drained = LRU_CACHE_DRAINED;
+ }
+ /* Try draining all CPUs next if still not an LRU folio. */
+ if (folio_likely_lru_cached(folio) && *drained == LRU_CACHE_DRAINED) {
+ lru_add_drain_all();
+ *drained = LRU_CACHE_DRAINED_ALL;
+ }
+}
+
/*
* Returns the number of collected folios. Return value is always >= 0.
*/
@@ -2266,9 +2316,9 @@ static unsigned long collect_longterm_unpinnable_folios(
struct list_head *movable_folio_list,
struct pages_or_folios *pofs)
{
+ enum lru_cache_drained drained = LRU_CACHE_NOT_DRAINED;
unsigned long collected = 0;
struct folio *folio;
- int drained = 0;
long i = 0;
for (folio = pofs_get_folio(pofs, i); folio;
@@ -2287,19 +2337,7 @@ static unsigned long collect_longterm_unpinnable_folios(
continue;
}
- if (drained == 0 && folio_may_be_lru_cached(folio) &&
- folio_ref_count(folio) !=
- folio_expected_ref_count(folio) + 1) {
- lru_add_drain();
- drained = 1;
- }
- if (drained == 1 && folio_may_be_lru_cached(folio) &&
- folio_ref_count(folio) !=
- folio_expected_ref_count(folio) + 1) {
- lru_add_drain_all();
- drained = 2;
- }
-
+ lru_cache_drain_for_folio(folio, &drained);
if (!folio_isolate_lru(folio))
continue;
--
2.43.0
--
Cheers,
David