mm/huge_memory: fix NULL pointer deference when splitting folio
Subsystems (3)
PATCH v2 1/1 mm/huge_memory: fix NULL pointer deference when splitting folio
Commit c010d47f107f ("mm: thp: split huge page to any lower order pages") introduced an early check on the folio's order via mapping->flags before proceeding with the split work.
This check introduced a bug: for shmem folios in the swap cache and truncated folios, the mapping pointer can be NULL. Accessing mapping->flags in this state leads directly to a NULL pointer dereference.
This commit fixes the issue by moving the check for mapping != NULL before any attempt to access mapping->flags.
---
This patch is based on current mm-new, latest commit:
febb34c02328 dt-bindings: riscv: Add Svrsw60t59b extension description
v2:
* just move folio->mapping ahead
--- mm/huge_memory.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
mm/huge_memory.c +10 -12
@@ -3929,6 +3929,16 @@ static int __folio_split(struct folio *folio, unsigned int new_order,if (folio !=page_folio (split_at) || folio !=page_folio (lock_at))return -EINVAL ; +/* +* Folios that just got truncated cannot get split. Signal to the +* caller that there was a race. +* +* TODO: this will also currently refuse shmem folios that are in the +* swapcache. +*/ +if (!is_anon && !folio->mapping) +return -EBUSY ; +if (new_order >= old_order)return -EINVAL ;
@@ -3965,18 +3975,6 @@ static int __folio_split(struct folio *folio, unsigned int new_order,gfp_t gfp; mapping = folio->mapping; - -/* Truncated ? */ -/* -* TODO: add support for large shmem folio in swap cache. -* When shmem is in swap cache, mapping is NULL and -* folio_test_swapcache() is true. -*/ -if (!mapping) { - ret = -EBUSY ; - goto out; - } - min_order =mapping_min_folio_order (folio->mapping);if (new_order < min_order) { ret = -EINVAL ;
Signature · 1 line
2.34.1
Quoted 37 lines · Wei Yang
Commit
c010d47f107f("mm: thp: split huge page to any lower order pages") introduced an early check on the folio's order viamapping->flagsbefore proceeding with the split work.This check introduced a bug: for shmem folios in the swap cache and truncated folios, the mapping pointer can be NULL. Accessing
mapping->flagsin this state leads directly to a NULL pointer dereference.This commit fixes the issue by moving the check for mapping != NULL before any attempt to access
mapping->flags.Fixes c010d47f107f ("mm: thp: split huge page to any lower order pages")Signed-off-by Wei Yang <[email protected]>Cc Zi Yan <[email protected]>Cc "David Hildenbrand (Red Hat)" <[email protected]>Cc <[email protected]>---
This patch is based on current mm-new, latest commit:
febb34c02328dt-bindings: riscv: Add Svrsw60t59b extension descriptionv2:
* just movefolio->mappingahead
---mm/huge_memory.c| 22 ++++++++++------------1 file changed, 10 insertions(+), 12 deletions(-)mm/huge_memory.c +0
-0@@ -3929,6 +3929,16 @@ static int __folio_split(struct folio *folio, unsigned int new_order,if (folio !=page_folio (split_at) || folio !=page_folio (lock_at))return -EINVAL ;
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3941
Quoted 10 lines
mm/huge_memory.c +10
-0+/* +* Folios that just got truncated cannot get split. Signal to the +* caller that there was a race. +* +* TODO: this will also currently refuse shmem folios that are in the +* swapcache. +*/ +if (!is_anon && !folio->mapping) +return -EBUSY ; +
This one would have a conflict on direct cherry-pick to current master and mm-stable.
But if I move this code before (folio != page_folio(split_at) ...), it could be apply to mm-new and master/mm-stable smoothly.
Not sure whether this could make Andrew's life easier.
Quoted 24 lines
mm/huge_memory.c +0
-12if (new_order >= old_order)return -EINVAL ;@@ -3965,18 +3975,6 @@ static int __folio_split(struct folio *folio, unsigned int new_order,gfp_t gfp; mapping = folio->mapping; - -/* Truncated ? */ -/* -* TODO: add support for large shmem folio in swap cache. -* When shmem is in swap cache, mapping is NULL and -* folio_test_swapcache() is true. -*/ -if (!mapping) { - ret = -EBUSY ; - goto out; - } - min_order =mapping_min_folio_order (folio->mapping);if (new_order < min_order) { ret = -EINVAL ;Signature · 1 line
2.34.1
Signature · 2 lines
Wei Yang Help you, Help me
Quoted 10 lines · On Thu, 20 Nov 2025 00:03:12 +0000 Wei Yang <[email protected]> wrote:
(quoted diff) +1
-0+ * TODO: this will also currently refuse shmem folios that are in theReview
Quoted 5 lines
(quoted diff) +5
-0+ * swapcache. + */ + if (!is_anon && !folio->mapping) + return -EBUSY; +This one would have a conflict on direct cherry-pick to current master and mm-stable.
Quoted 4 lines
But if I move this code before (folio !=
page_folio(split_at)...), it could be apply to mm-new and master/mm-stable smoothly.Not sure whether this could make Andrew's life easier.
I added the below and fixed up fallout in the later patches.
If this doesn't apply to -stable kernels then the -stable maintainers might later ask you to help rework it.
From: Wei Yang <[email protected]>
Subject: mm/huge_memory: fix NULL pointer deference when splitting folio
Date: Wed, 19 Nov 2025 23:53:02 +0000
Commit c010d47f107f ("mm: thp: split huge page to any lower order pages") introduced an early check on the folio's order via mapping->flags before proceeding with the split work.
This check introduced a bug: for shmem folios in the swap cache and truncated folios, the mapping pointer can be NULL. Accessing mapping->flags in this state leads directly to a NULL pointer dereference.
This commit fixes the issue by moving the check for mapping != NULL before any attempt to access mapping->flags.
---
mm/huge_memory.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
mm/huge_memory.c renamed from mm/huge_memory.c~mm-huge_memory-fix-null-pointer-deference-when-splitting-folio +10 -12
@@ -3619,6 +3619,16 @@ static int __folio_split(struct folio *fif (folio !=page_folio (split_at) || folio !=page_folio (lock_at))return -EINVAL ; +/* +* Folios that just got truncated cannot get split. Signal to the +* caller that there was a race. +* +* TODO: this will also currently refuse shmem folios that are in the +* swapcache. +*/ +if (!is_anon && !folio->mapping) +return -EBUSY ; +if (new_order >=folio_order (folio))return -EINVAL ;
@@ -3659,18 +3669,6 @@ static int __folio_split(struct folio *fgfp_t gfp; mapping = folio->mapping; - -/* Truncated ? */ -/* -* TODO: add support for large shmem folio in swap cache. -* When shmem is in swap cache, mapping is NULL and -* folio_test_swapcache() is true. -*/ -if (!mapping) { - ret = -EBUSY ; - goto out; - } - min_order =mapping_min_folio_order (folio->mapping);if (new_order < min_order) { ret = -EINVAL ;
_
Quoted 15 lines · Andrew Morton
Quoted 13 lines · On Thu, 20 Nov 2025 00:03:12 +0000 Wei Yang <[email protected]> wrote:
mm/huge_memory.c renamed from mm/huge_memory.c~mm-huge_memory-fix-null-pointer-deference-when-splitting-folio +1
-0+* TODO: this will also currently refuse shmem folios that are in the mm/huge_memory.c static int __folio_split(struct folio *f · line 3631
Quoted 5 lines
mm/huge_memory.c renamed from mm/huge_memory.c~mm-huge_memory-fix-null-pointer-deference-when-splitting-folio +5
-0+* swapcache. +*/ +if (!is_anon && !folio->mapping) +return -EBUSY ; +This one would have a conflict on direct cherry-pick to current master and mm-stable.
But if I move this code before (folio !=
page_folio(split_at)...), it could be apply to mm-new and master/mm-stable smoothly.
Quoted 7 lines
Quoted 1 line
Not sure whether this could make Andrew's life easier.
I added the below and fixed up fallout in the later patches.
If this doesn't apply to -stable kernels then the -stable maintainers might later ask you to help rework it.
OK, got it.
Quoted 68 lines
From: Wei Yang <[email protected]>
Subject:mm/huge_memory: fix NULL pointer deference when splitting folio
Date: Wed, 19 Nov 2025 23:53:02 +0000Commit
c010d47f107f("mm: thp: split huge page to any lower order pages") introduced an early check on the folio's order viamapping->flagsbefore proceeding with the split work.This check introduced a bug: for shmem folios in the swap cache and truncated folios, the mapping pointer can be NULL. Accessing
mapping->flagsin this state leads directly to a NULL pointer dereference.This commit fixes the issue by moving the check for mapping != NULL before any attempt to access
mapping->flags.Fixes c010d47f107f ("mm: thp: split huge page to any lower order pages")Signed-off-by Wei Yang <[email protected]>Reviewed-by Zi Yan <[email protected]>Cc "David Hildenbrand (Red Hat)" <[email protected]>Cc <[email protected]>Signed-off-by Andrew Morton <[email protected]>---
mm/huge_memory.c| 22 ++++++++++------------1 file changed, 10 insertions(+), 12 deletions(-)mm/huge_memory.c renamed from mm/huge_memory.c~mm-huge_memory-fix-null-pointer-deference-when-splitting-folio +10
-12@@ -3619,6 +3619,16 @@ static int __folio_split(struct folio *fif (folio !=page_folio (split_at) || folio !=page_folio (lock_at))return -EINVAL ; +/* +* Folios that just got truncated cannot get split. Signal to the +* caller that there was a race. +* +* TODO: this will also currently refuse shmem folios that are in the +* swapcache. +*/ +if (!is_anon && !folio->mapping) +return -EBUSY ; +if (new_order >=folio_order (folio))return -EINVAL ;@@ -3659,18 +3669,6 @@ static int __folio_split(struct folio *fgfp_t gfp; mapping = folio->mapping; - -/* Truncated ? */ -/* -* TODO: add support for large shmem folio in swap cache. -* When shmem is in swap cache, mapping is NULL and -* folio_test_swapcache() is true. -*/ -if (!mapping) { - ret = -EBUSY ; - goto out; - } - min_order =mapping_min_folio_order (folio->mapping);if (new_order < min_order) { ret = -EINVAL ;_
Signature · 2 lines
Wei Yang Help you, Help me
Zi Yan
reviewerQuoted 21 lines · Wei Yang
Commit
c010d47f107f("mm: thp: split huge page to any lower order pages") introduced an early check on the folio's order viamapping->flagsbefore proceeding with the split work.This check introduced a bug: for shmem folios in the swap cache and truncated folios, the mapping pointer can be NULL. Accessing
mapping->flagsin this state leads directly to a NULL pointer dereference.This commit fixes the issue by moving the check for mapping != NULL before any attempt to access
mapping->flags.Fixes c010d47f107f ("mm: thp: split huge page to any lower order pages")Signed-off-by Wei Yang <[email protected]>Cc Zi Yan <[email protected]>Cc "David Hildenbrand (Red Hat)" <[email protected]>Cc <[email protected]>---
This patch is based on current mm-new, latest commit:
Quoted 8 lines
febb34c02328dt-bindings: riscv: Add Svrsw60t59b extension descriptionv2:
* just movefolio->mappingahead--- mm/huge_memory.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
Thanks. Reviewed-by: Zi Yan <[email protected]>
Best Regards,
Yan, Zi
Quoted 12 lines · Wei Yang
Commit
c010d47f107f("mm: thp: split huge page to any lower order pages") introduced an early check on the folio's order viamapping->flagsbefore proceeding with the split work.This check introduced a bug: for shmem folios in the swap cache and truncated folios, the mapping pointer can be NULL. Accessing
mapping->flagsin this state leads directly to a NULL pointer dereference.This commit fixes the issue by moving the check for mapping != NULL before any attempt to access
mapping->flags.
Quoted 7 lines
Fixes c010d47f107f ("mm: thp: split huge page to any lower order pages")Signed-off-by Wei Yang <[email protected]>Cc Zi Yan <[email protected]>Cc "David Hildenbrand (Red Hat)" <[email protected]>Cc <[email protected]>---
LGTM.
Quoted 9 lines · Wei Yang
Commit
c010d47f107f("mm: thp: split huge page to any lower order pages") introduced an early check on the folio's order viamapping->flagsbefore proceeding with the split work.This check introduced a bug: for shmem folios in the swap cache and truncated folios, the mapping pointer can be NULL. Accessing
mapping->flagsin this state leads directly to a NULL pointer dereference.
Quoted 8 lines
This commit fixes the issue by moving the check for mapping != NULL before any attempt to access
mapping->flags.Fixes c010d47f107f ("mm: thp: split huge page to any lower order pages")Signed-off-by Wei Yang <[email protected]>Cc Zi Yan <[email protected]>Cc "David Hildenbrand (Red Hat)" <[email protected]>Cc <[email protected]>
Signature · 3 lines
Cheers David