[PATCH v1 18/29] mm: remove __folio_test_movable()

David Hildenbrand posted 29 patches 3 months, 1 week ago
There is a newer version of this series
[PATCH v1 18/29] mm: remove __folio_test_movable()
Posted by David Hildenbrand 3 months, 1 week ago
Convert to page_has_movable_ops(). While at it, cleanup relevant code
a bit.

The data_race() in migrate_folio_unmap() is questionable: we already
hold a page reference, and concurrent modifications can no longer
happen (iow: __ClearPageMovable() no longer exists). Drop it for now,
we'll rework page_has_movable_ops() soon either way to no longer
rely on page->mapping.

Wherever we cast from folio to page now is a clear sign that this
code has to be decoupled.

Reviewed-by: Zi Yan <ziy@nvidia.com>
Signed-off-by: David Hildenbrand <david@redhat.com>
---
 include/linux/page-flags.h |  6 ------
 mm/migrate.c               | 43 ++++++++++++--------------------------
 mm/vmscan.c                |  6 ++++--
 3 files changed, 17 insertions(+), 38 deletions(-)

diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
index c67163b73c5ec..4c27ebb689e3c 100644
--- a/include/linux/page-flags.h
+++ b/include/linux/page-flags.h
@@ -744,12 +744,6 @@ static __always_inline bool PageAnon(const struct page *page)
 	return folio_test_anon(page_folio(page));
 }
 
-static __always_inline bool __folio_test_movable(const struct folio *folio)
-{
-	return ((unsigned long)folio->mapping & PAGE_MAPPING_FLAGS) ==
-			PAGE_MAPPING_MOVABLE;
-}
-
 static __always_inline bool page_has_movable_ops(const struct page *page)
 {
 	return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
diff --git a/mm/migrate.c b/mm/migrate.c
index 587af35b7390d..15d3c1031530c 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -219,12 +219,7 @@ void putback_movable_pages(struct list_head *l)
 			continue;
 		}
 		list_del(&folio->lru);
-		/*
-		 * We isolated non-lru movable folio so here we can use
-		 * __folio_test_movable because LRU folio's mapping cannot
-		 * have PAGE_MAPPING_MOVABLE.
-		 */
-		if (unlikely(__folio_test_movable(folio))) {
+		if (unlikely(page_has_movable_ops(&folio->page))) {
 			putback_movable_ops_page(&folio->page);
 		} else {
 			node_stat_mod_folio(folio, NR_ISOLATED_ANON +
@@ -237,26 +232,20 @@ void putback_movable_pages(struct list_head *l)
 /* Must be called with an elevated refcount on the non-hugetlb folio */
 bool isolate_folio_to_list(struct folio *folio, struct list_head *list)
 {
-	bool isolated, lru;
-
 	if (folio_test_hugetlb(folio))
 		return folio_isolate_hugetlb(folio, list);
 
-	lru = !__folio_test_movable(folio);
-	if (lru)
-		isolated = folio_isolate_lru(folio);
-	else
-		isolated = isolate_movable_ops_page(&folio->page,
-						    ISOLATE_UNEVICTABLE);
-
-	if (!isolated)
-		return false;
-
-	list_add(&folio->lru, list);
-	if (lru)
+	if (page_has_movable_ops(&folio->page)) {
+		if (!isolate_movable_ops_page(&folio->page,
+					      ISOLATE_UNEVICTABLE))
+			return false;
+	} else {
+		if (!folio_isolate_lru(folio))
+			return false;
 		node_stat_add_folio(folio, NR_ISOLATED_ANON +
 				    folio_is_file_lru(folio));
-
+	}
+	list_add(&folio->lru, list);
 	return true;
 }
 
@@ -1140,12 +1129,7 @@ static void migrate_folio_undo_dst(struct folio *dst, bool locked,
 static void migrate_folio_done(struct folio *src,
 			       enum migrate_reason reason)
 {
-	/*
-	 * Compaction can migrate also non-LRU pages which are
-	 * not accounted to NR_ISOLATED_*. They can be recognized
-	 * as __folio_test_movable
-	 */
-	if (likely(!__folio_test_movable(src)) && reason != MR_DEMOTION)
+	if (likely(!page_has_movable_ops(&src->page)) && reason != MR_DEMOTION)
 		mod_node_page_state(folio_pgdat(src), NR_ISOLATED_ANON +
 				    folio_is_file_lru(src), -folio_nr_pages(src));
 
@@ -1164,7 +1148,6 @@ static int migrate_folio_unmap(new_folio_t get_new_folio,
 	int rc = -EAGAIN;
 	int old_page_state = 0;
 	struct anon_vma *anon_vma = NULL;
-	bool is_lru = data_race(!__folio_test_movable(src));
 	bool locked = false;
 	bool dst_locked = false;
 
@@ -1265,7 +1248,7 @@ static int migrate_folio_unmap(new_folio_t get_new_folio,
 		goto out;
 	dst_locked = true;
 
-	if (unlikely(!is_lru)) {
+	if (unlikely(page_has_movable_ops(&src->page))) {
 		__migrate_folio_record(dst, old_page_state, anon_vma);
 		return MIGRATEPAGE_UNMAP;
 	}
@@ -1330,7 +1313,7 @@ static int migrate_folio_move(free_folio_t put_new_folio, unsigned long private,
 	prev = dst->lru.prev;
 	list_del(&dst->lru);
 
-	if (unlikely(__folio_test_movable(src))) {
+	if (unlikely(page_has_movable_ops(&src->page))) {
 		rc = migrate_movable_ops_page(&dst->page, &src->page, mode);
 		if (rc)
 			goto out;
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 098bcc821fc74..103dfc729a823 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -1658,9 +1658,11 @@ unsigned int reclaim_clean_pages_from_list(struct zone *zone,
 	unsigned int noreclaim_flag;
 
 	list_for_each_entry_safe(folio, next, folio_list, lru) {
+		/* TODO: these pages should not even appear in this list. */
+		if (page_has_movable_ops(&folio->page))
+			continue;
 		if (!folio_test_hugetlb(folio) && folio_is_file_lru(folio) &&
-		    !folio_test_dirty(folio) && !__folio_test_movable(folio) &&
-		    !folio_test_unevictable(folio)) {
+		    !folio_test_dirty(folio) && !folio_test_unevictable(folio)) {
 			folio_clear_active(folio);
 			list_move(&folio->lru, &clean_folios);
 		}
-- 
2.49.0
Re: [PATCH v1 18/29] mm: remove __folio_test_movable()
Posted by Harry Yoo 3 months, 1 week ago
On Mon, Jun 30, 2025 at 02:59:59PM +0200, David Hildenbrand wrote:
> Convert to page_has_movable_ops(). While at it, cleanup relevant code
> a bit.
> 
> The data_race() in migrate_folio_unmap() is questionable: we already
> hold a page reference, and concurrent modifications can no longer
> happen (iow: __ClearPageMovable() no longer exists). Drop it for now,
> we'll rework page_has_movable_ops() soon either way to no longer
> rely on page->mapping.
> 
> Wherever we cast from folio to page now is a clear sign that this
> code has to be decoupled.
> 
> Reviewed-by: Zi Yan <ziy@nvidia.com>
> Signed-off-by: David Hildenbrand <david@redhat.com>
> ---

LGTM

Reviewed-by: Harry Yoo <harry.yoo@oracle.com>

> diff --git a/mm/vmscan.c b/mm/vmscan.c
> index 098bcc821fc74..103dfc729a823 100644
> --- a/mm/vmscan.c
> +++ b/mm/vmscan.c
> @@ -1658,9 +1658,11 @@ unsigned int reclaim_clean_pages_from_list(struct zone *zone,
>  	unsigned int noreclaim_flag;
>  
>  	list_for_each_entry_safe(folio, next, folio_list, lru) {
> +		/* TODO: these pages should not even appear in this list. */
> +		if (page_has_movable_ops(&folio->page))
> +			continue;

Looking forward to see how this TODO will be addressed :)

-- 
Cheers,
Harry / Hyeonggon
Re: [PATCH v1 18/29] mm: remove __folio_test_movable()
Posted by Lorenzo Stoakes 3 months, 1 week ago
On Mon, Jun 30, 2025 at 02:59:59PM +0200, David Hildenbrand wrote:
> Convert to page_has_movable_ops(). While at it, cleanup relevant code
> a bit.
>
> The data_race() in migrate_folio_unmap() is questionable: we already
> hold a page reference, and concurrent modifications can no longer
> happen (iow: __ClearPageMovable() no longer exists). Drop it for now,
> we'll rework page_has_movable_ops() soon either way to no longer
> rely on page->mapping.
>
> Wherever we cast from folio to page now is a clear sign that this
> code has to be decoupled.
>
> Reviewed-by: Zi Yan <ziy@nvidia.com>
> Signed-off-by: David Hildenbrand <david@redhat.com>

LGTM, so:

Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

> ---
>  include/linux/page-flags.h |  6 ------
>  mm/migrate.c               | 43 ++++++++++++--------------------------
>  mm/vmscan.c                |  6 ++++--
>  3 files changed, 17 insertions(+), 38 deletions(-)
>
> diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
> index c67163b73c5ec..4c27ebb689e3c 100644
> --- a/include/linux/page-flags.h
> +++ b/include/linux/page-flags.h
> @@ -744,12 +744,6 @@ static __always_inline bool PageAnon(const struct page *page)
>  	return folio_test_anon(page_folio(page));
>  }
>
> -static __always_inline bool __folio_test_movable(const struct folio *folio)
> -{
> -	return ((unsigned long)folio->mapping & PAGE_MAPPING_FLAGS) ==
> -			PAGE_MAPPING_MOVABLE;
> -}
> -

Woah, wait, does this mean we can remove PAGE_MAPPING_MOVABLE??

Nice!

>  static __always_inline bool page_has_movable_ops(const struct page *page)
>  {
>  	return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
> diff --git a/mm/migrate.c b/mm/migrate.c
> index 587af35b7390d..15d3c1031530c 100644
> --- a/mm/migrate.c
> +++ b/mm/migrate.c
> @@ -219,12 +219,7 @@ void putback_movable_pages(struct list_head *l)
>  			continue;
>  		}
>  		list_del(&folio->lru);
> -		/*
> -		 * We isolated non-lru movable folio so here we can use
> -		 * __folio_test_movable because LRU folio's mapping cannot
> -		 * have PAGE_MAPPING_MOVABLE.
> -		 */

So hate these references to 'LRU' as in meaning 'pages that could be on the
LRU'.


> -		if (unlikely(__folio_test_movable(folio))) {
> +		if (unlikely(page_has_movable_ops(&folio->page))) {
>  			putback_movable_ops_page(&folio->page);
>  		} else {
>  			node_stat_mod_folio(folio, NR_ISOLATED_ANON +
> @@ -237,26 +232,20 @@ void putback_movable_pages(struct list_head *l)
>  /* Must be called with an elevated refcount on the non-hugetlb folio */
>  bool isolate_folio_to_list(struct folio *folio, struct list_head *list)
>  {
> -	bool isolated, lru;
> -
>  	if (folio_test_hugetlb(folio))
>  		return folio_isolate_hugetlb(folio, list);
>
> -	lru = !__folio_test_movable(folio);
> -	if (lru)
> -		isolated = folio_isolate_lru(folio);
> -	else
> -		isolated = isolate_movable_ops_page(&folio->page,
> -						    ISOLATE_UNEVICTABLE);
> -
> -	if (!isolated)
> -		return false;
> -
> -	list_add(&folio->lru, list);
> -	if (lru)
> +	if (page_has_movable_ops(&folio->page)) {
> +		if (!isolate_movable_ops_page(&folio->page,
> +					      ISOLATE_UNEVICTABLE))
> +			return false;
> +	} else {
> +		if (!folio_isolate_lru(folio))
> +			return false;
>  		node_stat_add_folio(folio, NR_ISOLATED_ANON +
>  				    folio_is_file_lru(folio));
> -
> +	}
> +	list_add(&folio->lru, list);
>  	return true;
>  }
>
> @@ -1140,12 +1129,7 @@ static void migrate_folio_undo_dst(struct folio *dst, bool locked,
>  static void migrate_folio_done(struct folio *src,
>  			       enum migrate_reason reason)
>  {
> -	/*
> -	 * Compaction can migrate also non-LRU pages which are
> -	 * not accounted to NR_ISOLATED_*. They can be recognized
> -	 * as __folio_test_movable
> -	 */
> -	if (likely(!__folio_test_movable(src)) && reason != MR_DEMOTION)
> +	if (likely(!page_has_movable_ops(&src->page)) && reason != MR_DEMOTION)
>  		mod_node_page_state(folio_pgdat(src), NR_ISOLATED_ANON +
>  				    folio_is_file_lru(src), -folio_nr_pages(src));
>
> @@ -1164,7 +1148,6 @@ static int migrate_folio_unmap(new_folio_t get_new_folio,
>  	int rc = -EAGAIN;
>  	int old_page_state = 0;
>  	struct anon_vma *anon_vma = NULL;
> -	bool is_lru = data_race(!__folio_test_movable(src));
>  	bool locked = false;
>  	bool dst_locked = false;
>
> @@ -1265,7 +1248,7 @@ static int migrate_folio_unmap(new_folio_t get_new_folio,
>  		goto out;
>  	dst_locked = true;
>
> -	if (unlikely(!is_lru)) {
> +	if (unlikely(page_has_movable_ops(&src->page))) {
>  		__migrate_folio_record(dst, old_page_state, anon_vma);
>  		return MIGRATEPAGE_UNMAP;
>  	}
> @@ -1330,7 +1313,7 @@ static int migrate_folio_move(free_folio_t put_new_folio, unsigned long private,
>  	prev = dst->lru.prev;
>  	list_del(&dst->lru);
>
> -	if (unlikely(__folio_test_movable(src))) {
> +	if (unlikely(page_has_movable_ops(&src->page))) {
>  		rc = migrate_movable_ops_page(&dst->page, &src->page, mode);
>  		if (rc)
>  			goto out;
> diff --git a/mm/vmscan.c b/mm/vmscan.c
> index 098bcc821fc74..103dfc729a823 100644
> --- a/mm/vmscan.c
> +++ b/mm/vmscan.c
> @@ -1658,9 +1658,11 @@ unsigned int reclaim_clean_pages_from_list(struct zone *zone,
>  	unsigned int noreclaim_flag;
>
>  	list_for_each_entry_safe(folio, next, folio_list, lru) {
> +		/* TODO: these pages should not even appear in this list. */
> +		if (page_has_movable_ops(&folio->page))

VM_WARN_ON_ONCE()?

> +			continue;
>  		if (!folio_test_hugetlb(folio) && folio_is_file_lru(folio) &&
> -		    !folio_test_dirty(folio) && !__folio_test_movable(folio) &&
> -		    !folio_test_unevictable(folio)) {
> +		    !folio_test_dirty(folio) && !folio_test_unevictable(folio)) {
>  			folio_clear_active(folio);
>  			list_move(&folio->lru, &clean_folios);
>  		}
> --
> 2.49.0
>
Re: [PATCH v1 18/29] mm: remove __folio_test_movable()
Posted by David Hildenbrand 3 months, 1 week ago
>> ---
>>   include/linux/page-flags.h |  6 ------
>>   mm/migrate.c               | 43 ++++++++++++--------------------------
>>   mm/vmscan.c                |  6 ++++--
>>   3 files changed, 17 insertions(+), 38 deletions(-)
>>
>> diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
>> index c67163b73c5ec..4c27ebb689e3c 100644
>> --- a/include/linux/page-flags.h
>> +++ b/include/linux/page-flags.h
>> @@ -744,12 +744,6 @@ static __always_inline bool PageAnon(const struct page *page)
>>   	return folio_test_anon(page_folio(page));
>>   }
>>
>> -static __always_inline bool __folio_test_movable(const struct folio *folio)
>> -{
>> -	return ((unsigned long)folio->mapping & PAGE_MAPPING_FLAGS) ==
>> -			PAGE_MAPPING_MOVABLE;
>> -}
>> -
> 
> Woah, wait, does this mean we can remove PAGE_MAPPING_MOVABLE??

Jup :)

> 
> Nice!
> 
>>   static __always_inline bool page_has_movable_ops(const struct page *page)
>>   {
>>   	return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
>> diff --git a/mm/migrate.c b/mm/migrate.c
>> index 587af35b7390d..15d3c1031530c 100644
>> --- a/mm/migrate.c
>> +++ b/mm/migrate.c
>> @@ -219,12 +219,7 @@ void putback_movable_pages(struct list_head *l)
>>   			continue;
>>   		}
>>   		list_del(&folio->lru);
>> -		/*
>> -		 * We isolated non-lru movable folio so here we can use
>> -		 * __folio_test_movable because LRU folio's mapping cannot
>> -		 * have PAGE_MAPPING_MOVABLE.
>> -		 */
> 
> So hate these references to 'LRU' as in meaning 'pages that could be on the
> LRU'.

Yeah, it's a historical thing.

But for anything we isolated, it had to be an LRU folio (PageLRU) 
because that's how we were even able to isolate it ... from the LRU.

[...]

>> diff --git a/mm/vmscan.c b/mm/vmscan.c
>> index 098bcc821fc74..103dfc729a823 100644
>> --- a/mm/vmscan.c
>> +++ b/mm/vmscan.c
>> @@ -1658,9 +1658,11 @@ unsigned int reclaim_clean_pages_from_list(struct zone *zone,
>>   	unsigned int noreclaim_flag;
>>
>>   	list_for_each_entry_safe(folio, next, folio_list, lru) {
>> +		/* TODO: these pages should not even appear in this list. */
>> +		if (page_has_movable_ops(&folio->page))
> 
> VM_WARN_ON_ONCE()?

Well, no, it can currently still happen. But really, movable_ops pages 
are not folios that could ever be reclaimed that way.

So the TODO highlights that movable_ops pages should never even be put 
in a list (page->lru will go away).

-- 
Cheers,

David / dhildenb