Skip to content

Commit cff47b9

Browse files
RichardWeiYangakpm00
authored andcommitted
mm/huge_memory: fix NULL pointer deference when splitting folio
Commit c010d47 ("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. Link: https://lkml.kernel.org/r/20251119235302.24773-1-richard.weiyang@gmail.com Fixes: c010d47 ("mm: thp: split huge page to any lower order pages") Signed-off-by: Wei Yang <richard.weiyang@gmail.com> Reviewed-by: Zi Yan <ziy@nvidia.com> Acked-by: David Hildenbrand (Red Hat) <david@kernel.org> Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 6c96c6b commit cff47b9

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

mm/huge_memory.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3619,6 +3619,16 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
36193619
if (folio != page_folio(split_at) || folio != page_folio(lock_at))
36203620
return -EINVAL;
36213621

3622+
/*
3623+
* Folios that just got truncated cannot get split. Signal to the
3624+
* caller that there was a race.
3625+
*
3626+
* TODO: this will also currently refuse shmem folios that are in the
3627+
* swapcache.
3628+
*/
3629+
if (!is_anon && !folio->mapping)
3630+
return -EBUSY;
3631+
36223632
if (new_order >= folio_order(folio))
36233633
return -EINVAL;
36243634

@@ -3659,18 +3669,6 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
36593669
gfp_t gfp;
36603670

36613671
mapping = folio->mapping;
3662-
3663-
/* Truncated ? */
3664-
/*
3665-
* TODO: add support for large shmem folio in swap cache.
3666-
* When shmem is in swap cache, mapping is NULL and
3667-
* folio_test_swapcache() is true.
3668-
*/
3669-
if (!mapping) {
3670-
ret = -EBUSY;
3671-
goto out;
3672-
}
3673-
36743672
min_order = mapping_min_folio_order(folio->mapping);
36753673
if (new_order < min_order) {
36763674
ret = -EINVAL;

0 commit comments

Comments
 (0)