Skip to contentSkip to conversation

mm/rmap: small cleanup for __folio_remove_rmap()

PATCH Superseded Wei Yang 4 participants 18 messages Aug 15, 2025 – Aug 16, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

cover mm/rmap: small cleanup for __folio_remove_rmap()

During code reading, I found some small places where we can improve based on the knowledge we already have.

For example, since base folio is handled in another branch, we case use folio_large_nr_pages() to get the number directly.

Hope I don't mess it up.

Wei Yang (3):
mm/rmap: not necessary to mask off FOLIO_PAGES_MAPPED mm/rmap: could be partially_mapped only after no entire map mm/rmap: use folio_large_nr_pages() when we are sure it is a large
folio

 mm/rmap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/3 mm/rmap: not necessary to mask off FOLIO_PAGES_MAPPED

At this point, we are sure (nr < ENTIRELY_MAPPED). This means the upper bits are already cleared.

Not necessary to mask off it.

Cc David Hildenbrand <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Harry Yoo <[email protected]>
---
 mm/rmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/rmap.c +1 -1
index 1c5988dbd1e7..a927437a56c2 100644
@@ -1749,7 +1749,7 @@ static __always_inline void __folio_remove_rmap(struct folio *folio,
 				nr_pages = folio_large_nr_pages(folio);
 				if (level == PGTABLE_LEVEL_PMD)
 					nr_pmdmapped = nr_pages;
-				nr = nr_pages - (nr & FOLIO_PAGES_MAPPED);
+				nr = nr_pages - nr;
 				/* Raced ahead of another remove and an add? */
 				if (unlikely(nr < 0))
 					nr = 0;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] mm/rmap: not necessary to mask off FOLIO_PAGES_MAPPED

Quoted 4 lines · Wei Yang

At this point, we are sure (nr < ENTIRELY_MAPPED). This means the upper bits are already cleared.

Not necessary to mask off it.

Worth saying it's because we are in an if branch conditional on nr < ENTIRELY_MAPPED, and FOLIO_PAGES_MAPPED is equal to ENTIRELY_MAPPED - 1.

Quoted 2 lines

I suspect that this is copy/pasta from __folio_add_rmap(), but it may also be some way of asserting we don't underflow nr.

Anyway I think this is fine because the branch implies this is ok so:

Quoted 27 lines
Cc David Hildenbrand <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Harry Yoo <[email protected]>
---
 mm/rmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/rmap.c +1 -2
index 1c5988dbd1e7..a927437a56c2 100644
@@ -1749,7 +1749,7 @@ static __always_inline void __folio_remove_rmap(struct folio *folio,
 				nr_pages = folio_large_nr_pages(folio);
 				if (level == PGTABLE_LEVEL_PMD)
 					nr_pmdmapped = nr_pages;
-				nr = nr_pages - (nr & FOLIO_PAGES_MAPPED);
+				nr = nr_pages - nr;
 				/* Raced ahead of another remove and an add? */
 				if (unlikely(nr < 0))
 					nr = 0;
--

2.34.1

author
#3 lore raw

Re: [PATCH 1/3] mm/rmap: not necessary to mask off FOLIO_PAGES_MAPPED

Quoted 9 lines · Lorenzo Stoakes
Quoted 4 lines · Wei Yang

At this point, we are sure (nr < ENTIRELY_MAPPED). This means the upper bits are already cleared.

Not necessary to mask off it.

Worth saying it's because we are in an if branch conditional on nr < ENTIRELY_MAPPED, and FOLIO_PAGES_MAPPED is equal to ENTIRELY_MAPPED - 1.

Will adjust it.

Quoted 10 lines
Quoted 2 lines

I suspect that this is copy/pasta from __folio_add_rmap(), but it may also be some way of asserting we don't underflow nr.

Anyway I think this is fine because the branch implies this is ok so:

Thanks

Signature · 2 lines
Wei Yang
Help you, Help me
Acked-by on v1 1/3
#4 lore raw

Re: [PATCH 1/3] mm/rmap: not necessary to mask off FOLIO_PAGES_MAPPED

In reply to Wei Yang
Quoted 21 lines · Wei Yang

At this point, we are sure (nr < ENTIRELY_MAPPED). This means the upper bits are already cleared.

Not necessary to mask off it.

Cc David Hildenbrand <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Harry Yoo <[email protected]>
---
  mm/rmap.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/rmap.c +0 -0
index 1c5988dbd1e7..a927437a56c2 100644

mm/rmap.c static __always_inline void __folio_remove_rmap(struct folio *folio, · line 1755

Quoted 8 lines
mm/rmap.c +1 -1
@@ -1749,7 +1749,7 @@ static __always_inline void __folio_remove_rmap(struct folio *folio,
  				nr_pages = folio_large_nr_pages(folio);
  				if (level == PGTABLE_LEVEL_PMD)
  					nr_pmdmapped = nr_pages;
-				nr = nr_pages - (nr & FOLIO_PAGES_MAPPED);
+				nr = nr_pages - nr;
  				/* Raced ahead of another remove and an add? */
  				if (unlikely(nr < 0))
  					nr = 0;
Signature · 3 lines
Cheers

David / dhildenb
author
#5 lore raw

PATCH 2/3 mm/rmap: could be partially_mapped only after no entire map

In reply to Wei Yang

If it is not the last entire map, we are sure the folio is not partially mapped.

Move the check when there is no entire map.

Cc David Hildenbrand <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Harry Yoo <[email protected]>
---
 mm/rmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/rmap.c +2 -2
index a927437a56c2..645d924bfc7d 100644
@@ -1757,9 +1757,9 @@ static __always_inline void __folio_remove_rmap(struct folio *folio,
 				/* An add of ENTIRELY_MAPPED raced ahead */
 				nr = 0;
 			}
-		}
 
-		partially_mapped = nr && nr < nr_pmdmapped;
+			partially_mapped = nr && nr < nr_pmdmapped;
+		}
 		break;
 	default:
 		BUILD_BUG();
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH 2/3] mm/rmap: could be partially_mapped only after no entire map

Quoted 5 lines · Wei Yang

If it is not the last entire map, we are sure the folio is not partially mapped.

Move the check when there is no entire map.

This one I don't like, you're having to sit and think about why it is that this would be the case, vs. just unconditionally doing it.

Again, as mentioned on previous series, just because we could do something doesn't mean we should, unless there's statistically reliable perf data on something real-world indicating we _must_, code clarity absolutely beats everything else on importance.

So yeah, sorry but no to this patch, please resend with just the two reviewed (unless David radically disagrees with me :)

Quoted 31 lines
Cc David Hildenbrand <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Harry Yoo <[email protected]>
---
 mm/rmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/rmap.c +2 -3
index a927437a56c2..645d924bfc7d 100644
@@ -1757,9 +1757,9 @@ static __always_inline void __folio_remove_rmap(struct folio *folio,
 				/* An add of ENTIRELY_MAPPED raced ahead */
 				nr = 0;
 			}
-		}
 
-		partially_mapped = nr && nr < nr_pmdmapped;
+			partially_mapped = nr && nr < nr_pmdmapped;
+		}
 		break;
 	default:
 		BUILD_BUG();
--

2.34.1

#7 lore raw

Re: [PATCH 2/3] mm/rmap: could be partially_mapped only after no entire map

Quoted 10 lines · Lorenzo Stoakes
Quoted 5 lines · Wei Yang

If it is not the last entire map, we are sure the folio is not partially mapped.

Move the check when there is no entire map.

This one I don't like, you're having to sit and think about why it is that this would be the case, vs. just unconditionally doing it.

Quoted 7 lines

Again, as mentioned on previous series, just because we could do something doesn't mean we should, unless there's statistically reliable perf data on something real-world indicating we _must_, code clarity absolutely beats everything else on importance.

So yeah, sorry but no to this patch, please resend with just the two reviewed (unless David radically disagrees with me :)

The compiler can figure out that "nr == 0" if the "if (last)" branch is not taken.

So I tend to prefer keeping it as is.

Signature · 3 lines
Cheers

David / dhildenb
author
#8 lore raw

Re: [PATCH 2/3] mm/rmap: could be partially_mapped only after no entire map

Quoted 16 lines · David Hildenbrand
Quoted 15 lines · Lorenzo Stoakes
Quoted 5 lines · Wei Yang

If it is not the last entire map, we are sure the folio is not partially mapped.

Move the check when there is no entire map.

This one I don't like, you're having to sit and think about why it is that this would be the case, vs. just unconditionally doing it.

Again, as mentioned on previous series, just because we could do something doesn't mean we should, unless there's statistically reliable perf data on something real-world indicating we _must_, code clarity absolutely beats everything else on importance.

Quoted 6 lines
Quoted 2 lines

So yeah, sorry but no to this patch, please resend with just the two reviewed (unless David radically disagrees with me :)

The compiler can figure out that "nr == 0" if the "if (last)" branch is not taken.

Per my understanding, last is a run time value. I don't figure out how compiler could help here.

I may miss something. Would you mind giving more hint?

Quoted 5 lines

So I tend to prefer keeping it as is.

Signature · 3 lines
Cheers

David / dhildenb
Signature · 2 lines
Wei Yang
Help you, Help me
#9 lore raw

Re: [PATCH 2/3] mm/rmap: could be partially_mapped only after no entire map

Quoted 20 lines · Wei Yang
Quoted 19 lines · David Hildenbrand
Quoted 17 lines · Lorenzo Stoakes
Quoted 5 lines · Wei Yang

If it is not the last entire map, we are sure the folio is not partially mapped.

Move the check when there is no entire map.

This one I don't like, you're having to sit and think about why it is that this would be the case, vs. just unconditionally doing it.

Again, as mentioned on previous series, just because we could do something doesn't mean we should, unless there's statistically reliable perf data on something real-world indicating we _must_, code clarity absolutely beats everything else on importance.

So yeah, sorry but no to this patch, please resend with just the two reviewed (unless David radically disagrees with me :)

Quoted 8 lines
Quoted 3 lines

The compiler can figure out that "nr == 0" if the "if (last)" branch is not taken.

Per my understanding, last is a run time value. I don't figure out how compiler could help here.

I may miss something. Would you mind giving more hint?

The compiler can figure out that it can move the whole statement to the "if (last)" branch because it knows that nr == 0 otherwise and partially_mapped == 0 already.

Best to see if there is any actual change in generated code, though.

Note that if we're already moving that around, couldn't we move it further in?

mm/rmap.c +1 -1
index 0e9c4041f8687..fb83db88cd1fd 100644
@@ -1753,13 +1753,13 @@ static __always_inline void __folio_remove_rmap(struct folio *folio,
                                /* Raced ahead of another remove and an add? */
                                if (unlikely(nr < 0))
                                        nr = 0;
+                               partially_mapped = nr && nr < nr_pmdmapped;
                        } else {
                                /* An add of ENTIRELY_MAPPED raced ahead */
                                nr = 0;
                        }
                }
 
-               partially_mapped = nr && nr < nr_pmdmapped;
                break;
        default:
                BUILD_BUG();
Signature · 3 lines
Cheers

David / dhildenb
#10 lore raw

Re: [PATCH 2/3] mm/rmap: could be partially_mapped only after no entire map

Since the compiler can prove the expression will always be false in one branch, there's no need to execute that computation at all in that branch, right?

Quoted 1 line · David Hildenbrand

The compiler can figure out that it can move the whole statement to the "if (last)" branch because it knows that nr == 0 otherwise and partially_mapped == 0 already.

#11 lore raw

Re: [PATCH 2/3] mm/rmap: could be partially_mapped only after no entire map

Hi,

please don't top-post.

Quoted 3 lines · Giorgi Tchankvetadze

Since the compiler can prove the expression will always be false in one branch, there's no need to execute that computation at all in that branch, right?

It's mostly a matter of making the code easier to read. The way we have it right now, we calculate partially_mapped in all cases just before the end of the case.

For example, partially_mapped is irrelevant for !folio_test_anon() and !folio_test_large(), but we rely on the compiler to figure that out and optimize the code accordingly.

Having something like

if (folio_test_large(folio) && folio_test_anon(folio) &&

    !folio_test_partially_mapped(folio)) {
	switch (level) {
	case PGTABLE_LEVEL_PTE:
		if (IS_ENABLED(CONFIG_NO_PAGE_MAPCOUNT)) {
			...
			break;
		}
		partially_mapped = ...
	case PGTABLE_LEVEL_PMD:
		...
	}
	if (partially_mapped)
		deferred_split_folio(folio, true);
}

Could be done, but it's arguably harder to read.

Quoted 6 lines · David Hildenbrand
Quoted 3 lines

The compiler can figure out that it can move the whole statement to the "if (last)" branch because it knows that nr == 0 otherwise and partially_mapped == 0 already.

Signature · 3 lines
Cheers

David / dhildenb
author
#12 lore raw

Re: [PATCH 2/3] mm/rmap: could be partially_mapped only after no entire map

In reply to David Hildenbrand
Quoted 25 lines · David Hildenbrand
Quoted 24 lines · Wei Yang
Quoted 22 lines · David Hildenbrand
Quoted 10 lines · Lorenzo Stoakes
Quoted 5 lines · Wei Yang

If it is not the last entire map, we are sure the folio is not partially mapped.

Move the check when there is no entire map.

This one I don't like, you're having to sit and think about why it is that this would be the case, vs. just unconditionally doing it.

Quoted 7 lines

Again, as mentioned on previous series, just because we could do something doesn't mean we should, unless there's statistically reliable perf data on something real-world indicating we _must_, code clarity absolutely beats everything else on importance.

So yeah, sorry but no to this patch, please resend with just the two reviewed (unless David radically disagrees with me :)

The compiler can figure out that "nr == 0" if the "if (last)" branch is not taken.

Quoted 6 lines
Quoted 4 lines

Per my understanding, last is a run time value. I don't figure out how compiler could help here.

I may miss something. Would you mind giving more hint?

The compiler can figure out that it can move the whole statement to the "if (last)" branch because it knows that nr == 0 otherwise and partially_mapped == 0 already.

Wow, this is interesting. Compiler is smarter than I thought.

Thanks

Quoted 3 lines

Best to see if there is any actual change in generated code, though.

Take a look into the generated code, there is no difference.

Quoted 2 lines

Note that if we're already moving that around, couldn't we move it further in?

Yes, I have thought about this. But I was afraid it was too aggressive.

While after a second look, this may make the logic similar with CONFIG_NO_PAGE_MAPCOUNT. I personally like this one, but it seems not a big deal.

Quoted 24 lines
mm/rmap.c +1 -1
index 0e9c4041f8687..fb83db88cd1fd 100644
@@ -1753,13 +1753,13 @@ static __always_inline void __folio_remove_rmap(struct folio *folio,
                               /* Raced ahead of another remove and an add? */
                               if (unlikely(nr < 0))
                                       nr = 0;
+                               partially_mapped = nr && nr < nr_pmdmapped;
                       } else {
                               /* An add of ENTIRELY_MAPPED raced ahead */
                               nr = 0;
                       }
               }
-               partially_mapped = nr && nr < nr_pmdmapped;
               break;
       default:
               BUILD_BUG();
 
Signature · 4 lines
-- 
Cheers

David / dhildenb
Signature · 2 lines
Wei Yang
Help you, Help me
author
#13 lore raw

PATCH 3/3 mm/rmap: use folio_large_nr_pages() when we are sure it is a large folio

In reply to Wei Yang

Non-large folio is handled at the beginning, so it is a large folio for sure.

Use folio_large_nr_pages() here like elsewhere.

Cc David Hildenbrand <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Harry Yoo <[email protected]>
---
 mm/rmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/rmap.c +1 -1
index 645d924bfc7d..1b7011afc663 100644
@@ -1703,7 +1703,7 @@ static __always_inline void __folio_remove_rmap(struct folio *folio,
 			nr = folio_sub_return_large_mapcount(folio, nr_pages, vma);
 			if (!nr) {
 				/* Now completely unmapped. */
-				nr = folio_nr_pages(folio);
+				nr = folio_large_nr_pages(folio);
 			} else {
 				partially_mapped = nr < folio_large_nr_pages(folio) &&
 						   !folio_entire_mapcount(folio);
Signature · 1 line
2.34.1
Reviewed-by on v1 3/3
#14 lore raw

Re: [PATCH 3/3] mm/rmap: use folio_large_nr_pages() when we are sure it is a large folio

Quoted 6 lines · Wei Yang

Non-large folio is handled at the beginning, so it is a large folio for sure.

Use folio_large_nr_pages() here like elsewhere.

This looks fine to me, so:

Quoted 27 lines
Cc David Hildenbrand <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Harry Yoo <[email protected]>
---
 mm/rmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/rmap.c +1 -2
index 645d924bfc7d..1b7011afc663 100644
@@ -1703,7 +1703,7 @@ static __always_inline void __folio_remove_rmap(struct folio *folio,
 			nr = folio_sub_return_large_mapcount(folio, nr_pages, vma);
 			if (!nr) {
 				/* Now completely unmapped. */
-				nr = folio_nr_pages(folio);
+				nr = folio_large_nr_pages(folio);
 			} else {
 				partially_mapped = nr < folio_large_nr_pages(folio) &&
 						   !folio_entire_mapcount(folio);
--

2.34.1

Acked-by on v1 3/3
#15 lore raw

Re: [PATCH 3/3] mm/rmap: use folio_large_nr_pages() when we are sure it is a large folio

In reply to Wei Yang
Quoted 5 lines · Wei Yang

Non-large folio is handled at the beginning, so it is a large folio for sure.

Use folio_large_nr_pages() here like elsewhere.

Quoted 8 lines
Cc David Hildenbrand <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Harry Yoo <[email protected]>

---

Signature · 3 lines
Cheers

David / dhildenb
#16 lore raw
In reply to Wei Yang
Quoted 5 lines · Wei Yang

During code reading, I found some small places where we can improve based on the knowledge we already have.

For example, since base folio is handled in another branch, we case use folio_large_nr_pages() to get the number directly.

I think this cover letter is pretty weak, please describe what you are doing clearly and why.

Quoted 2 lines

Hope I don't mess it up.

Please don't put this kind of thing in the cover letter, it gets reproduced into the actual commit messages and this isn't useful for people reading through commit messages.

Quoted 6 lines

Wei Yang (3):
mm/rmap: not necessary to mask off FOLIO_PAGES_MAPPED mm/rmap: could be partially_mapped only after no entire map mm/rmap: use folio_large_nr_pages() when we are sure it is a large
folio

I find 1/3 and 3/3 ok, 2/3 I don't like, so please resend series as v2 with only 1 and 2, propagating tags + improved cover letter please.

FWIW, I build-tested each commit and ran mm self tests + VMA tests against series and all passed.

Quoted 8 lines
 mm/rmap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

--
2.34.1

Cheers, Lorenzo

author
#17 lore raw
Quoted 10 lines · Lorenzo Stoakes
Quoted 5 lines · Wei Yang

During code reading, I found some small places where we can improve based on the knowledge we already have.

For example, since base folio is handled in another branch, we case use folio_large_nr_pages() to get the number directly.

I think this cover letter is pretty weak, please describe what you are doing clearly and why.

Will adjust it.

Quoted 7 lines
Quoted 2 lines

Hope I don't mess it up.

Please don't put this kind of thing in the cover letter, it gets reproduced into the actual commit messages and this isn't useful for people reading through commit messages.

Sure.

Signature · 2 lines
Wei Yang
Help you, Help me