Skip to contentSkip to conversation

mm/huge_memory: refactor after-split (page) cache code.

PATCH In review Zi Yan 5 participants 13 messages Jul 16, 2025 – Jul 17, 2025

Subsystems (2)
lore
Share & tools
lore

Zi Yan

author reviewer
#0 lore raw

PATCH 1/1 mm/huge_memory: refactor after-split (page) cache code.

Smatch/coverity checkers report NULL mapping referencing issues[1][2][3] every time the code is modified, because they do not understand that mapping cannot be NULL when a folio is in page cache in the code. Refactor the code to make it explicit.

No functional change is intended.

[1]https://lore.kernel.org/linux-mm/[email protected]/ [2]https://lore.kernel.org/oe-kbuild/[email protected]/ [3]https://lore.kernel.org/linux-mm/[email protected]/

---
 mm/huge_memory.c | 43 ++++++++++++++++++++++++++++---------------
 1 file changed, 28 insertions(+), 15 deletions(-)
mm/huge_memory.c +28 -15
index 31b5c4e61a57..fe17b0a157cd 100644
@@ -3804,6 +3804,8 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 		 */
 		for (new_folio = folio_next(folio); new_folio != next_folio;
 		     new_folio = next) {
+			unsigned long nr_pages = folio_nr_pages(new_folio);
+
 			next = folio_next(new_folio);
 
 			expected_refs = folio_expected_ref_count(new_folio) + 1;
@@ -3811,25 +3813,36 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 
 			lru_add_split_folio(folio, new_folio, lruvec, list);
 
-			/* Some pages can be beyond EOF: drop them from cache */
-			if (new_folio->index >= end) {
-				if (shmem_mapping(mapping))
-					nr_shmem_dropped += folio_nr_pages(new_folio);
-				else if (folio_test_clear_dirty(new_folio))
-					folio_account_cleaned(
-						new_folio,
-						inode_to_wb(mapping->host));
-				__filemap_remove_folio(new_folio, NULL);
-				folio_put_refs(new_folio,
-					       folio_nr_pages(new_folio));
-			} else if (mapping) {
-				__xa_store(&mapping->i_pages, new_folio->index,
-					   new_folio, 0);
-			} else if (swap_cache) {
+			/*
+			 * Anonymous folio with swap cache.
+			 * NOTE: shmem in swap cache is not supported yet.
+			 */
+			if (swap_cache) {
 				__xa_store(&swap_cache->i_pages,
 					   swap_cache_index(new_folio->swap),
 					   new_folio, 0);
+				continue;
+			}
+
+			/* Anonymouse folio without swap cache */
+			if (!mapping)
+				continue;
+
+			/* Add the new folio to the page cache. */
+			if (new_folio->index < end) {
+				__xa_store(&mapping->i_pages, new_folio->index,
+					   new_folio, 0);
+				continue;
 			}
+
+			/* Drop folio beyond EOF: ->index >= end */
+			if (shmem_mapping(mapping))
+				nr_shmem_dropped += nr_pages;
+			else if (folio_test_clear_dirty(new_folio))
+				folio_account_cleaned(
+					new_folio, inode_to_wb(mapping->host));
+			__filemap_remove_folio(new_folio, NULL);
+			folio_put_refs(new_folio, nr_pages);
 		}
 		/*
 		 * Unfreeze @folio only after all page cache entries, which
Signature · 1 line
2.47.2
#1 lore raw
Quoted 7 lines · Zi Yan

Smatch/coverity checkers report NULL mapping referencing issues[1][2][3] every time the code is modified, because they do not understand that mapping cannot be NULL when a folio is in page cache in the code. Refactor the code to make it explicit.

No functional change is intended.

Quoted 7 lines

[1]https://lore.kernel.org/linux-mm/[email protected]/ [2]https://lore.kernel.org/oe-kbuild/[email protected]/ [3]https://lore.kernel.org/linux-mm/[email protected]/

---

This silences the Smatch warning. :)

regards,
dan carpenter

author reviewer
#2 lore raw
Quoted 12 lines · Dan Carpenter
Quoted 11 lines · Zi Yan

Smatch/coverity checkers report NULL mapping referencing issues[1][2][3] every time the code is modified, because they do not understand that mapping cannot be NULL when a folio is in page cache in the code. Refactor the code to make it explicit.

No functional change is intended.

[1]https://lore.kernel.org/linux-mm/[email protected]/ [2]https://lore.kernel.org/oe-kbuild/[email protected]/ [3]https://lore.kernel.org/linux-mm/[email protected]/

Quoted 5 lines
Quoted 3 lines

---

This silences the Smatch warning. :)

Thank you for the confirmation.

Best Regards,
Yan, Zi

Acked-by on v1 1/1
#3 lore raw
In reply to Zi Yan
Quoted 7 lines · Zi Yan

Smatch/coverity checkers report NULL mapping referencing issues[1][2][3] every time the code is modified, because they do not understand that mapping cannot be NULL when a folio is in page cache in the code. Refactor the code to make it explicit.

No functional change is intended.

Quoted 7 lines

[1]https://lore.kernel.org/linux-mm/[email protected]/ [2]https://lore.kernel.org/oe-kbuild/[email protected]/ [3]https://lore.kernel.org/linux-mm/[email protected]/

---

Thanks!

Signature · 3 lines
Cheers,

David / dhildenb
author reviewer
#4 lore raw
Quoted 12 lines · David Hildenbrand
Quoted 11 lines · Zi Yan

Smatch/coverity checkers report NULL mapping referencing issues[1][2][3] every time the code is modified, because they do not understand that mapping cannot be NULL when a folio is in page cache in the code. Refactor the code to make it explicit.

No functional change is intended.

[1]https://lore.kernel.org/linux-mm/[email protected]/ [2]https://lore.kernel.org/oe-kbuild/[email protected]/ [3]https://lore.kernel.org/linux-mm/[email protected]/

Quoted 8 lines
Quoted 3 lines

---

Thanks!

Thanks.

Best Regards,
Yan, Zi

#5 lore raw
In reply to Zi Yan
Quoted 5 lines · Zi Yan

Smatch/coverity checkers report NULL mapping referencing issues[1][2][3] every time the code is modified, because they do not understand that mapping cannot be NULL when a folio is in page cache in the code. Refactor the code to make it explicit.

Quoted 8 lines

No functional change is intended.

[1]https://lore.kernel.org/linux-mm/[email protected]/ [2]https://lore.kernel.org/oe-kbuild/[email protected]/ [3]https://lore.kernel.org/linux-mm/[email protected]/

Much easier to grasp - Thanks a lot!

I am sure Coverity will be happy too at this point, because the ambiguity has been fully removed.

In a previous email you asked me how to prevent Coverity from complaining about certain code: my thinking is fully aligned with Dan's reply. IMHO refactoring the code was the best choice - thanks again.

Regards,

Signature · 5 lines
Antonio Quartulli

CEO and Co-Founder
Mandelbit Srl
https://www.mandelbit.com
author reviewer
#6 lore raw
Quoted 12 lines · Antonio Quartulli
Quoted 11 lines · Zi Yan

Smatch/coverity checkers report NULL mapping referencing issues[1][2][3] every time the code is modified, because they do not understand that mapping cannot be NULL when a folio is in page cache in the code. Refactor the code to make it explicit.

No functional change is intended.

[1]https://lore.kernel.org/linux-mm/[email protected]/ [2]https://lore.kernel.org/oe-kbuild/[email protected]/ [3]https://lore.kernel.org/linux-mm/[email protected]/

Quoted 8 lines
Quoted 2 lines

Much easier to grasp - Thanks a lot!

I am sure Coverity will be happy too at this point, because the ambiguity has been fully removed.

In a previous email you asked me how to prevent Coverity from complaining about certain code: my thinking is fully aligned with Dan's reply. IMHO refactoring the code was the best choice - thanks again.

Sure. Coverity/smatch makes the code better this time. :)

Best Regards,
Yan, Zi

Reviewed-by on v1 1/1
#7 lore raw
In reply to Zi Yan
Quoted 5 lines · Zi Yan

Smatch/coverity checkers report NULL mapping referencing issues[1][2][3] every time the code is modified, because they do not understand that mapping cannot be NULL when a folio is in page cache in the code. Refactor the code to make it explicit.

Quoted 8 lines

No functional change is intended.

[1]https://lore.kernel.org/linux-mm/[email protected]/ [2]https://lore.kernel.org/oe-kbuild/[email protected]/ [3]https://lore.kernel.org/linux-mm/[email protected]/

This is fantastic, thanks Zi! There's a nit below but I actually almost _don't_ want you to address it :P

Therefore:

Quoted 29 lines
---
 mm/huge_memory.c | 43 ++++++++++++++++++++++++++++---------------
 1 file changed, 28 insertions(+), 15 deletions(-)
mm/huge_memory.c +2 -8
index 31b5c4e61a57..fe17b0a157cd 100644
@@ -3804,6 +3804,8 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 		 */
 		for (new_folio = folio_next(folio); new_folio != next_folio;
 		     new_folio = next) {
+			unsigned long nr_pages = folio_nr_pages(new_folio);
+
 			next = folio_next(new_folio);
 
 			expected_refs = folio_expected_ref_count(new_folio) + 1;
@@ -3811,25 +3813,36 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 
 			lru_add_split_folio(folio, new_folio, lruvec, list);
 
-			/* Some pages can be beyond EOF: drop them from cache */
-			if (new_folio->index >= end) {
-				if (shmem_mapping(mapping))
-					nr_shmem_dropped += folio_nr_pages(new_folio);
-				else if (folio_test_clear_dirty(new_folio))
-					folio_account_cleaned(
-						new_folio,
-						inode_to_wb(mapping->host));

mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3818

Quoted 10 lines
mm/huge_memory.c +3 -7
-				__filemap_remove_folio(new_folio, NULL);
-				folio_put_refs(new_folio,
-					       folio_nr_pages(new_folio));
-			} else if (mapping) {
-				__xa_store(&mapping->i_pages, new_folio->index,
-					   new_folio, 0);
-			} else if (swap_cache) {
+			/*
+			 * Anonymous folio with swap cache.
+			 * NOTE: shmem in swap cache is not supported yet.

Nice added context!

mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3827

Quoted 9 lines
mm/huge_memory.c +6 -0
+			 */
+			if (swap_cache) {
 				__xa_store(&swap_cache->i_pages,
 					   swap_cache_index(new_folio->swap),
 					   new_folio, 0);
+				continue;
+			}
+
+			/* Anonymouse folio without swap cache */

I almost don't want to comment here because 'anony-mouse' is really cute :P but yeah nit I think you have a trailing 'e' here that my cats would be VERY interested in... ;)

Quoted 22 lines
mm/huge_memory.c +17 -1
+			if (!mapping)
+				continue;
+
+			/* Add the new folio to the page cache. */
+			if (new_folio->index < end) {
+				__xa_store(&mapping->i_pages, new_folio->index,
+					   new_folio, 0);
+				continue;
 			}
+
+			/* Drop folio beyond EOF: ->index >= end */
+			if (shmem_mapping(mapping))
+				nr_shmem_dropped += nr_pages;
+			else if (folio_test_clear_dirty(new_folio))
+				folio_account_cleaned(
+					new_folio, inode_to_wb(mapping->host));
+			__filemap_remove_folio(new_folio, NULL);
+			folio_put_refs(new_folio, nr_pages);
 		}
 		/*
 		 * Unfreeze @folio only after all page cache entries, which
--
Quoted 2 lines

2.47.2

Since we no longer need to make new_folio->index >= end work for anon folios, can we drop the end = -1 in the if (is_anon) { ... } branch?

author reviewer
#8 lore raw
Quoted 64 lines · Lorenzo Stoakes
Quoted 5 lines · Zi Yan

Smatch/coverity checkers report NULL mapping referencing issues[1][2][3] every time the code is modified, because they do not understand that mapping cannot be NULL when a folio is in page cache in the code. Refactor the code to make it explicit.

Quoted 8 lines

No functional change is intended.

[1]https://lore.kernel.org/linux-mm/[email protected]/ [2]https://lore.kernel.org/oe-kbuild/[email protected]/ [3]https://lore.kernel.org/linux-mm/[email protected]/

This is fantastic, thanks Zi! There's a nit below but I actually almost _don't_ want you to address it :P

Therefore:

Quoted 29 lines
---
 mm/huge_memory.c | 43 ++++++++++++++++++++++++++++---------------
 1 file changed, 28 insertions(+), 15 deletions(-)
mm/huge_memory.c +2 -8
index 31b5c4e61a57..fe17b0a157cd 100644
@@ -3804,6 +3804,8 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 		 */
 		for (new_folio = folio_next(folio); new_folio != next_folio;
 		     new_folio = next) {
+			unsigned long nr_pages = folio_nr_pages(new_folio);
+
 			next = folio_next(new_folio);
 
 			expected_refs = folio_expected_ref_count(new_folio) + 1;
@@ -3811,25 +3813,36 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 
 			lru_add_split_folio(folio, new_folio, lruvec, list);
 
-			/* Some pages can be beyond EOF: drop them from cache */
-			if (new_folio->index >= end) {
-				if (shmem_mapping(mapping))
-					nr_shmem_dropped += folio_nr_pages(new_folio);
-				else if (folio_test_clear_dirty(new_folio))
-					folio_account_cleaned(
-						new_folio,
-						inode_to_wb(mapping->host));

mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3818

Quoted 10 lines
mm/huge_memory.c +3 -7
-				__filemap_remove_folio(new_folio, NULL);
-				folio_put_refs(new_folio,
-					       folio_nr_pages(new_folio));
-			} else if (mapping) {
-				__xa_store(&mapping->i_pages, new_folio->index,
-					   new_folio, 0);
-			} else if (swap_cache) {
+			/*
+			 * Anonymous folio with swap cache.
+			 * NOTE: shmem in swap cache is not supported yet.

Nice added context!

Quoted 13 lines

mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3827

Quoted 9 lines
mm/huge_memory.c +6 -0
+			 */
+			if (swap_cache) {
 				__xa_store(&swap_cache->i_pages,
 					   swap_cache_index(new_folio->swap),
 					   new_folio, 0);
+				continue;
+			}
+
+			/* Anonymouse folio without swap cache */

I almost don't want to comment here because 'anony-mouse' is really cute :P but yeah nit I think you have a trailing 'e' here that my cats would be VERY interested in... ;)

Will change it. :p

Quoted 20 lines
Quoted 20 lines
mm/huge_memory.c +17 -0
+			if (!mapping)
+				continue;
+
+			/* Add the new folio to the page cache. */
+			if (new_folio->index < end) {
+				__xa_store(&mapping->i_pages, new_folio->index,
+					   new_folio, 0);
+				continue;
 			}
+
+			/* Drop folio beyond EOF: ->index >= end */
+			if (shmem_mapping(mapping))
+				nr_shmem_dropped += nr_pages;
+			else if (folio_test_clear_dirty(new_folio))
+				folio_account_cleaned(
+					new_folio, inode_to_wb(mapping->host));
+			__filemap_remove_folio(new_folio, NULL);
+			folio_put_refs(new_folio, nr_pages);
 		}
Quoted 8 lines
Quoted 5 lines
mm/huge_memory.c +0 -1
 		/*
 		 * Unfreeze @folio only after all page cache entries, which
--

2.47.2

Since we no longer need to make new_folio->index >= end work for anon folios, can we drop the end = -1 in the if (is_anon) { ... } branch?

Sure.

OK, since I also need to address your comments on “mm/huge_memory: move unrelated code out of __split_unmapped_folio()”, I am going to send a new series with both this patch and patches from __split_unmapped_folio().

We are not in a rush, so let’s make it as good as possible. :)

Best Regards,
Yan, Zi

author reviewer
#9 lore raw
Quoted 6 lines
Quoted 4 lines

Since we no longer need to make new_folio->index >= end work for anon folios, can we drop the end = -1 in the if (is_anon) { ... } branch?

Sure.

A second thought on this one. If I remove end = -1, can static analysis tools understand that end is not used when a folio is anonymous? Probably, I can initialize end to -1 and remove end = -1 in is_anon branch.

Best Regards,
Yan, Zi

#10 lore raw
Quoted 11 lines · Zi Yan
Quoted 6 lines
Quoted 4 lines

Since we no longer need to make new_folio->index >= end work for anon folios, can we drop the end = -1 in the if (is_anon) { ... } branch?

Sure.

A second thought on this one. If I remove end = -1, can static analysis tools understand that end is not used when a folio is anonymous? Probably, I can initialize end to -1 and remove end = -1 in is_anon branch.

Smatch says that "if "mapping" is non-NULL then "end" is initialized" and it doesn't trigger a warning. I don't know how the other checkers handle it.

Btw, the only thing you really have to pay attention to is Clang because we treat build warnings as failure. You're always free to ignore other checkers.

regards,
dan carpenter

author reviewer
#11 lore raw
Quoted 8 lines · Dan Carpenter
Quoted 7 lines · Zi Yan
Quoted 6 lines
Quoted 4 lines

Since we no longer need to make new_folio->index >= end work for anon folios, can we drop the end = -1 in the if (is_anon) { ... } branch?

Sure.

Quoted 8 lines
Quoted 4 lines

A second thought on this one. If I remove end = -1, can static analysis tools understand that end is not used when a folio is anonymous? Probably, I can initialize end to -1 and remove end = -1 in is_anon branch.

Smatch says that "if "mapping" is non-NULL then "end" is initialized" and it doesn't trigger a warning. I don't know how the other checkers handle it.

Great. Thank you for running smatch for it.

Quoted 4 lines

Btw, the only thing you really have to pay attention to is Clang because we treat build warnings as failure. You're always free to ignore other checkers.

Good to know. I will compile my code using clang to get a sense on how checkers will react to my changes. Thank you for the information.

Best Regards,
Yan, Zi

#12 lore raw
In reply to Zi Yan
Quoted 11 lines · Zi Yan
Quoted 6 lines
Quoted 4 lines

Since we no longer need to make new_folio->index >= end work for anon folios, can we drop the end = -1 in the if (is_anon) { ... } branch?

Sure.

A second thought on this one. If I remove end = -1, can static analysis tools understand that end is not used when a folio is anonymous? Probably, I can initialize end to -1 and remove end = -1 in is_anon branch.

I don't think we should be concering ourselves with this generally speaking.

But doesn't David's suggested changes preclude this anyway? As you'd only be referencing end if mapping is set? But then that'd rely on !anon...

Perhaps you can just move figuring out what end is to the if (mapping) block...

But as Dan alluded I think (hope :P) humans read this stuff in the end before reporting :)

So if you add a comment very clearly stating that end won't be used if !mapping then that alone would do I think.

But I see Dan's replying here anyway so will leave to his expertise/your discretion.

Quoted 3 lines

Best Regards,
Yan, Zi