linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [RFC] mm/huge_memory: prevent potential NULL pointer dereference
@ 2025-07-16 14:58 Antonio Quartulli
  2025-07-16 15:07 ` Lorenzo Stoakes
  2025-07-16 15:10 ` Zi Yan
  0 siblings, 2 replies; 10+ messages in thread
From: Antonio Quartulli @ 2025-07-16 14:58 UTC (permalink / raw)
  To: linux-mm
  Cc: Antonio Quartulli, Andrew Morton, David Hildenbrand,
	Lorenzo Stoakes, Baolin Wang, Liam R. Howlett, Nico Pache,
	Ryan Roberts, Dev Jain, Barry Song, Zi Yan

I just found this issue in the last linux-next Coverity report and it
caught my attention.
I am not familiar with this code, therefore I am sending this patch
as RFC because I am not 100% sure whether this is a false positive or
not.
However, it seems potentially legit to me:

In __folio_split(), when looping over folios we dereference
`mapping` before ensuring it is non-NULL.

Following code in the loop body performs such check, thus
suggesting that `mapping` may be NULL and accessing it
without any check may be dangerous.

Add NULL check before passing it to shmem_mapping().

Cc: Zi Yan <ziy@nvidia.com>
Fixes: 00527733d0dc ("mm/huge_memory: add two new (not yet used) functions for folio_split()")
Addresses-Coverity-ID: 1647614 ("FORWARD_NULL")
Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---
 mm/huge_memory.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 389620c65a5f..d649026db95a 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -3802,7 +3802,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 
 			/* Some pages can be beyond EOF: drop them from cache */
 			if (new_folio->index >= end) {
-				if (shmem_mapping(mapping))
+				if (mapping && shmem_mapping(mapping))
 					nr_shmem_dropped += folio_nr_pages(new_folio);
 				else if (folio_test_clear_dirty(new_folio))
 					folio_account_cleaned(
-- 
2.49.1



^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2025-07-16 19:13 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-16 14:58 [RFC] mm/huge_memory: prevent potential NULL pointer dereference Antonio Quartulli
2025-07-16 15:07 ` Lorenzo Stoakes
2025-07-16 19:05   ` Antonio Quartulli
2025-07-16 19:10     ` Lorenzo Stoakes
2025-07-16 19:13       ` Antonio Quartulli
2025-07-16 15:10 ` Zi Yan
2025-07-16 15:18   ` David Hildenbrand
2025-07-16 15:24     ` Zi Yan
2025-07-16 15:31       ` David Hildenbrand
2025-07-16 16:18   ` Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).