Skip to contentSkip to conversation

fixup: mm/huge_memory.c: introduce folio_split_unmapped

PATCH In review Balbir Singh 4 participants 5 messages Nov 20, 2025 – Nov 23, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fixup: mm/huge_memory.c: introduce folio_split_unmapped

Code refactoring of __folio_split() via helper __folio_freeze_and_split_unmapped() caused a regression with clang-20 with CONFIG_SHMEM=n, the compiler was not able to optimize away the call to shmem_uncharge() due to changes in nr_shmem_dropped. Fix this by adding a stub function for shmem_uncharge when CONFIG_SHMEM is not defined.

smatch also complained about parameter end being used without initialization, which is a false positive, but keep the tool happy by sending in initialized parameters. end is initialized to 0. smatch still complains about mapping being NULL and nr_shmem_dropped may not be 0, but that is not true prior to or after the changes.

Add detailed documentation comments for folio_split_unmapped()

Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Gregory Price <[email protected]>
Cc Ying Huang <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Baolin Wang <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Ralph Campbell <[email protected]>
Cc Mika Penttilä <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Francois Dugast <[email protected]>

---
This fixup should be squashed into the patch "mm/huge_memory.c:
introduce folio_split_unmapped" in mm/mm-unstable

 include/linux/shmem_fs.h |  6 +++++-
 mm/huge_memory.c         | 30 +++++++++++++++++++++---------
 2 files changed, 26 insertions(+), 10 deletions(-)
include/linux/shmem_fs.h +5 -1
index 5b368f9549d6..7a412dd6eb4f 100644
@@ -136,11 +136,16 @@ static inline bool shmem_hpage_pmd_enabled(void)
 
 #ifdef CONFIG_SHMEM
 extern unsigned long shmem_swap_usage(struct vm_area_struct *vma);
+extern void shmem_uncharge(struct inode *inode, long pages);
 #else
 static inline unsigned long shmem_swap_usage(struct vm_area_struct *vma)
 {
 	return 0;
 }
+
+static void shmem_uncharge(struct inode *inode, long pages)
+{
+}
 #endif
 extern unsigned long shmem_partial_swap_usage(struct address_space *mapping,
 						pgoff_t start, pgoff_t end);
@@ -194,7 +199,6 @@ static inline pgoff_t shmem_fallocend(struct inode *inode, pgoff_t eof)
 }
 
 extern bool shmem_charge(struct inode *inode, long pages);
-extern void shmem_uncharge(struct inode *inode, long pages);
 
 #ifdef CONFIG_USERFAULTFD
 #ifdef CONFIG_SHMEM
mm/huge_memory.c +21 -9
index 78a31a476ad3..18c12876f5e8 100644
@@ -3751,6 +3751,7 @@ static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int n
 	int ret = 0;
 	struct deferred_split *ds_queue;
 
+	VM_WARN_ON_ONCE(!mapping && end);
 	/* Prevent deferred_split_scan() touching ->_refcount */
 	ds_queue = folio_split_queue_lock(folio);
 	if (folio_ref_freeze(folio, 1 + extra_pins)) {
@@ -3919,7 +3920,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 	int nr_shmem_dropped = 0;
 	int remap_flags = 0;
 	int extra_pins, ret;
-	pgoff_t end;
+	pgoff_t end = 0;
 	bool is_hzp;
 
 	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
@@ -4092,16 +4093,27 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 	return ret;
 }
 
-/*
- * This function is a helper for splitting folios that have already been unmapped.
- * The use case is that the device or the CPU can refuse to migrate THP pages in
- * the middle of migration, due to allocation issues on either side
+/**
+ * folio_split_unmapped() - split a large anon folio that is already unmapped
+ * @folio: folio to split
+ * @new_order: the order of folios after split
+ *
+ * This function is a helper for splitting folios that have already been
+ * unmapped. The use case is that the device or the CPU can refuse to migrate
+ * THP pages in the middle of migration, due to allocation issues on either
+ * side.
+ *
+ * anon_vma_lock is not required to be held, mmap_read_lock() or
+ * mmap_write_lock() should be held. @folio is expected to be locked by the
+ * caller. device-private and non device-private folios are supported along
+ * with folios that are in the swapcache. @folio should also be unmapped and
+ * isolated from LRU (if applicable)
  *
- * The high level code is copied from __folio_split, since the pages are anonymous
- * and are already isolated from the LRU, the code has been simplified to not
- * burden __folio_split with unmapped sprinkled into the code.
+ * Upon return, the folio is not remapped, split folios are not added to LRU,
+ * free_folio_and_swap_cache() is not called, and new folios remain locked.
  *
- * None of the split folios are unlocked
+ * Return: 0 on success, -EAGAIN if the folio cannot be split (e.g., due to
+ *         insufficient reference count or extra pins).
  */
 int folio_split_unmapped(struct folio *folio, unsigned int new_order)
 {
Signature · 1 line
2.51.1
#1 lore raw
Quoted 60 lines · Balbir Singh

Code refactoring of __folio_split() via helper __folio_freeze_and_split_unmapped() caused a regression with clang-20 with CONFIG_SHMEM=n, the compiler was not able to optimize away the call to shmem_uncharge() due to changes in nr_shmem_dropped. Fix this by adding a stub function for shmem_uncharge when CONFIG_SHMEM is not defined.

smatch also complained about parameter end being used without initialization, which is a false positive, but keep the tool happy by sending in initialized parameters. end is initialized to 0. smatch still complains about mapping being NULL and nr_shmem_dropped may not be 0, but that is not true prior to or after the changes.

Add detailed documentation comments for folio_split_unmapped()

Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Gregory Price <[email protected]>
Cc Ying Huang <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Baolin Wang <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Ralph Campbell <[email protected]>
Cc Mika Penttilä <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Francois Dugast <[email protected]>

---
This fixup should be squashed into the patch "mm/huge_memory.c:
introduce folio_split_unmapped" in mm/mm-unstable

 include/linux/shmem_fs.h |  6 +++++-
 mm/huge_memory.c         | 30 +++++++++++++++++++++---------
 2 files changed, 26 insertions(+), 10 deletions(-)
include/linux/shmem_fs.h +1 -0
index 5b368f9549d6..7a412dd6eb4f 100644
@@ -136,11 +136,16 @@ static inline bool shmem_hpage_pmd_enabled(void)
 
 #ifdef CONFIG_SHMEM
 extern unsigned long shmem_swap_usage(struct vm_area_struct *vma);
+extern void shmem_uncharge(struct inode *inode, long pages);

include/linux/shmem_fs.h static inline bool shmem_hpage_pmd_enabled(void) · line 149

Quoted 10 lines
include/linux/shmem_fs.h +4 -0
 #else
 static inline unsigned long shmem_swap_usage(struct vm_area_struct *vma)
 {
 	return 0;
 }
+
+static void shmem_uncharge(struct inode *inode, long pages)
+{
+}
 #endif

This is missing an inline attribute, my compilation tests did not catch it, but I'll do a new version tomorrow

Balbir

Balbir

#2 lore raw
In reply to Balbir Singh
kernel test robot noticed the following build warnings:

Hi Balbir,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on next-20251120]
[cannot apply to drm-misc/drm-misc-next drm-tip/drm-tip linus/master v6.18-rc6] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Balbir-Singh/fixup-mm-huge_memory-c-introduce-folio_split_unmapped/20251120-214322
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20251120134232.3588203-1-balbirs%40nvidia.com
patch subject: [PATCH v2] fixup: mm/huge_memory.c: introduce folio_split_unmapped
config: m68k-allnoconfig (https://download.01.org/0day-ci/archive/20251120/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251120/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from mm/filemap.c:39:

>> include/linux/shmem_fs.h:160:13: warning: 'shmem_uncharge' defined but not used [-Wunused-function]

160 | static void shmem_uncharge(struct inode *inode, long pages)
| ^~~~~~~~~~~~~~
--
mm/shmem.c: In function '__shmem_file_setup':
mm/shmem.c:5838:23: warning: unused variable 'flags' [-Wunused-variable]

    5838 |         unsigned long flags = (vm_flags & VM_NORESERVE) ? SHMEM_F_NORESERVE : 0;
         |                       ^~~~~
   In file included from mm/shmem.c:36:
   include/linux/shmem_fs.h: At top level:
>> include/linux/shmem_fs.h:160:13: warning: 'shmem_uncharge' defined but not used [-Wunused-function]

160 | static void shmem_uncharge(struct inode *inode, long pages)
| ^~~~~~~~~~~~~~

vim +/shmem_uncharge +160 include/linux/shmem_fs.h

159

 > 160	static void shmem_uncharge(struct inode *inode, long pages)

161 {
162 }
163 #endif
164 extern unsigned long shmem_partial_swap_usage(struct address_space *mapping,
165 pgoff_t start, pgoff_t end);
166

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw
In reply to Balbir Singh
Quoted 56 lines · Balbir Singh

Code refactoring of __folio_split() via helper __folio_freeze_and_split_unmapped() caused a regression with clang-20 with CONFIG_SHMEM=n, the compiler was not able to optimize away the call to shmem_uncharge() due to changes in nr_shmem_dropped. Fix this by adding a stub function for shmem_uncharge when CONFIG_SHMEM is not defined.

smatch also complained about parameter end being used without initialization, which is a false positive, but keep the tool happy by sending in initialized parameters. end is initialized to 0. smatch still complains about mapping being NULL and nr_shmem_dropped may not be 0, but that is not true prior to or after the changes.

Add detailed documentation comments for folio_split_unmapped()

Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Gregory Price <[email protected]>
Cc Ying Huang <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Baolin Wang <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Ralph Campbell <[email protected]>
Cc Mika Penttilä <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Francois Dugast <[email protected]>

---
This fixup should be squashed into the patch "mm/huge_memory.c:
introduce folio_split_unmapped" in mm/mm-unstable

  include/linux/shmem_fs.h |  6 +++++-
  mm/huge_memory.c         | 30 +++++++++++++++++++++---------
  2 files changed, 26 insertions(+), 10 deletions(-)
include/linux/shmem_fs.h +0 -0
index 5b368f9549d6..7a412dd6eb4f 100644

include/linux/shmem_fs.h static inline bool shmem_hpage_pmd_enabled(void) · line 139

Quoted 4 lines
include/linux/shmem_fs.h +1 -0
@@ -136,11 +136,16 @@ static inline bool shmem_hpage_pmd_enabled(void)
 
  #ifdef CONFIG_SHMEM
  extern unsigned long shmem_swap_usage(struct vm_area_struct *vma);
+extern void shmem_uncharge(struct inode *inode, long pages);

I forgot to add, we drop the "extern" on functions whenever we move them.

include/linux/shmem_fs.h static inline bool shmem_hpage_pmd_enabled(void) · line 146

Quoted 7 lines
include/linux/shmem_fs.h +2 -0
  #else
  static inline unsigned long shmem_swap_usage(struct vm_area_struct *vma)
  {
  	return 0;
  }
+
+static void shmem_uncharge(struct inode *inode, long pages)

"static inline void" should resolve the compilation issue.

Signature · 3 lines
Cheers

David
#4 lore raw
In reply to Balbir Singh
Quoted 111 lines · Balbir Singh

Code refactoring of __folio_split() via helper __folio_freeze_and_split_unmapped() caused a regression with clang-20 with CONFIG_SHMEM=n, the compiler was not able to optimize away the call to shmem_uncharge() due to changes in nr_shmem_dropped. Fix this by adding a stub function for shmem_uncharge when CONFIG_SHMEM is not defined.

smatch also complained about parameter end being used without initialization, which is a false positive, but keep the tool happy by sending in initialized parameters. end is initialized to 0. smatch still complains about mapping being NULL and nr_shmem_dropped may not be 0, but that is not true prior to or after the changes.

Add detailed documentation comments for folio_split_unmapped()

Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Zi Yan <[email protected]>
Cc Joshua Hahn <[email protected]>
Cc Rakie Kim <[email protected]>
Cc Byungchul Park <[email protected]>
Cc Gregory Price <[email protected]>
Cc Ying Huang <[email protected]>
Cc Alistair Popple <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Lorenzo Stoakes <[email protected]>
Cc Baolin Wang <[email protected]>
Cc "Liam R. Howlett" <[email protected]>
Cc Nico Pache <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Dev Jain <[email protected]>
Cc Barry Song <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Ralph Campbell <[email protected]>
Cc Mika Penttilä <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Francois Dugast <[email protected]>

---
This fixup should be squashed into the patch "mm/huge_memory.c:
introduce folio_split_unmapped" in mm/mm-unstable

include/linux/shmem_fs.h | 6 +++++-
mm/huge_memory.c | 30 +++++++++++++++++++++---------

2 files changed, 26 insertions(+), 10 deletions(-)
include/linux/shmem_fs.h +5 -1
index 5b368f9549d6..7a412dd6eb4f 100644
@@ -136,11 +136,16 @@ static inline bool shmem_hpage_pmd_enabled(void)
 
 #ifdef CONFIG_SHMEM
 extern unsigned long shmem_swap_usage(struct vm_area_struct *vma);
+extern void shmem_uncharge(struct inode *inode, long pages);
 #else
 static inline unsigned long shmem_swap_usage(struct vm_area_struct *vma)
 {
 	return 0;
 }
+
+static void shmem_uncharge(struct inode *inode, long pages)
+{
+}
 #endif
 extern unsigned long shmem_partial_swap_usage(struct address_space *mapping,
 						pgoff_t start, pgoff_t end);
@@ -194,7 +199,6 @@ static inline pgoff_t shmem_fallocend(struct inode *inode, pgoff_t eof)
 }
 
 extern bool shmem_charge(struct inode *inode, long pages);
-extern void shmem_uncharge(struct inode *inode, long pages);
 
 #ifdef CONFIG_USERFAULTFD
 #ifdef CONFIG_SHMEM
mm/huge_memory.c +4 -5
index 78a31a476ad3..18c12876f5e8 100644
@@ -3751,6 +3751,7 @@ static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int n
 	int ret = 0;
 	struct deferred_split *ds_queue;
 
+	VM_WARN_ON_ONCE(!mapping && end);
 	/* Prevent deferred_split_scan() touching ->_refcount */
 	ds_queue = folio_split_queue_lock(folio);
 	if (folio_ref_freeze(folio, 1 + extra_pins)) {
@@ -3919,7 +3920,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 	int nr_shmem_dropped = 0;
 	int remap_flags = 0;
 	int extra_pins, ret;
-	pgoff_t end;
+	pgoff_t end = 0;
 	bool is_hzp;
 
 	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
@@ -4092,16 +4093,27 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
 	return ret;
 }
 
-/*
- * This function is a helper for splitting folios that have already been unmapped.
- * The use case is that the device or the CPU can refuse to migrate THP pages in
- * the middle of migration, due to allocation issues on either side
+/**
+ * folio_split_unmapped() - split a large anon folio that is already unmapped

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

Quoted 10 lines
mm/huge_memory.c +10 -0
+ * @folio: folio to split
+ * @new_order: the order of folios after split
+ *
+ * This function is a helper for splitting folios that have already been
+ * unmapped. The use case is that the device or the CPU can refuse to migrate
+ * THP pages in the middle of migration, due to allocation issues on either
+ * side.
+ *
+ * anon_vma_lock is not required to be held, mmap_read_lock() or
+ * mmap_write_lock() should be held. @folio is expected to be locked by the

Took a look into its caller:

__migrate_device_pages()
migrate_vma_split_unmapped_folio()
folio_split_unmapped()

I don't see where get the folio lock.

Would you mind giving me a hint where we toke the lock? Seems I missed that.

Quoted 19 lines
mm/huge_memory.c +7 -4
+ * caller. device-private and non device-private folios are supported along
+ * with folios that are in the swapcache. @folio should also be unmapped and
+ * isolated from LRU (if applicable)
 *
- * The high level code is copied from __folio_split, since the pages are anonymous
- * and are already isolated from the LRU, the code has been simplified to not
- * burden __folio_split with unmapped sprinkled into the code.
+ * Upon return, the folio is not remapped, split folios are not added to LRU,
+ * free_folio_and_swap_cache() is not called, and new folios remain locked.
 *
- * None of the split folios are unlocked
+ * Return: 0 on success, -EAGAIN if the folio cannot be split (e.g., due to
+ *         insufficient reference count or extra pins).
 */
 int folio_split_unmapped(struct folio *folio, unsigned int new_order)
 {
Signature · 1 line
2.51.1
Signature · 2 lines
Wei Yang
Help you, Help me