Skip to contentSkip to conversation

mm: shmem: allow split THP when truncating THP partially

RFC PATCH Superseded Yang Shi 4 participants 11 messages Nov 23, 2019 – Dec 2, 2019

Subsystems (7)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yang Shi

author
#0 lore raw

RFC PATCH 1/1 mm: shmem: allow split THP when truncating THP partially

Currently when truncating shmem file, if the range is partial of THP (start or end is in the middle of THP), the pages actually will just get cleared rather than being freed unless the range cover the whole THP. Even though all the subpages are truncated (randomly or sequentially), the THP may still be kept in page cache. This might be fine for some usecases which prefer preserving THP.

But, when doing balloon inflation in QEMU, QEMU actually does hole punch or MADV_DONTNEED in base page size granulairty if hugetlbfs is not used. So, when using shmem THP as memory backend QEMU inflation actually doesn't work as expected since it doesn't free memory. But, the inflation usecase really needs get the memory freed. Anonymous THP will not get freed right away too but it will be freed eventually when all subpages are unmapped, but shmem THP would still stay in page cache.

To protect the usecases which may prefer preserving THP, introduce a new fallocate mode: FALLOC_FL_SPLIT_HPAGE, which means spltting THP is preferred behavior if truncating partial THP. This mode just makes sense to tmpfs for the time being.

Cc Hugh Dickins <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Andrea Arcangeli <[email protected]>
---
 drivers/gpu/drm/drm_gem_shmem_helper.c    |  2 +-
 drivers/gpu/drm/i915/gem/i915_gem_shmem.c |  3 +-
 include/linux/shmem_fs.h                  |  3 +-
 include/uapi/linux/falloc.h               |  7 +++
 mm/shmem.c                                | 99 +++++++++++++++++++++++++++----
 5 files changed, 99 insertions(+), 15 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +1 -1
index f591870..d44780e 100644
@@ -408,7 +408,7 @@ void drm_gem_shmem_purge_locked(struct drm_gem_object *obj)
 	 * To do this we must instruct the shmfs to drop all of its
 	 * backing pages, *now*.
 	 */
-	shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
+	shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1, false);
 
 	invalidate_mapping_pages(file_inode(obj->filp)->i_mapping,
 			0, (loff_t)-1);
drivers/gpu/drm/i915/gem/i915_gem_shmem.c +2 -1
index 4c4954e..cdee286 100644
@@ -222,7 +222,8 @@ static int shmem_get_pages(struct drm_i915_gem_object *obj)
 	 * To do this we must instruct the shmfs to drop all of its
 	 * backing pages, *now*.
 	 */
-	shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
+	shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1,
+			     false);
 	obj->mm.madv = __I915_MADV_PURGED;
 	obj->mm.pages = ERR_PTR(-EFAULT);
 }
include/linux/shmem_fs.h +2 -1
index de8e4b7..42c6420 100644
@@ -73,7 +73,8 @@ static inline bool shmem_mapping(struct address_space *mapping)
 extern void shmem_unlock_mapping(struct address_space *mapping);
 extern struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
 					pgoff_t index, gfp_t gfp_mask);
-extern void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end);
+extern void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end,
+				 bool split);
 extern int shmem_unuse(unsigned int type, bool frontswap,
 		       unsigned long *fs_pages_to_unuse);
 
include/uapi/linux/falloc.h +7 -0
index 51398fa..26fd272 100644
@@ -77,4 +77,11 @@
  */
 #define FALLOC_FL_UNSHARE_RANGE		0x40
 
+/*
+ * FALLOC_FL_SPLIT_HPAGE is used with FALLOC_FL_PUNCH_HOLE together to
+ * split huge page if the hole punch range is start or end in the middle
+ * of THP.  So far it only makes sense with tmpfs.
+ */
+#define FALLOC_FL_SPLIT_HPAGE		0x80
+
 #endif /* _UAPI_FALLOC_H_ */
mm/shmem.c +87 -12

226 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#1 lore raw
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please…

Hi Yang,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on mmotm/master]
[cannot apply to v5.4-rc8 next-20191122]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Yang-Shi/mm-shmem-allow-split-THP-when-truncating-THP-partially/20191125-032556
base:   git://git.cmpxchg.org/linux-mmotm.git master
config: arm-allmodconfig (attached as .config)
compiler: arm-linux-gnueabi-gcc (GCC) 7.4.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.4.0 make.cross ARCH=arm

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

drivers/gpu/drm/msm/msm_gem.c: In function 'msm_gem_purge':

>> drivers/gpu/drm/msm/msm_gem.c:671:2: error: too few arguments to function 'shmem_truncate_range'
     shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
     ^~~~~~~~~~~~~~~~~~~~

In file included from drivers/gpu/drm/msm/msm_gem.c:8:0:
include/linux/shmem_fs.h:76:13: note: declared here
extern void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end,
^~~~~~~~~~~~~~~~~~~~

vim +/shmem_truncate_range +671 drivers/gpu/drm/msm/msm_gem.c

4cd33c48ea25ba Rob Clark             2016-05-17  643  
0e08270a1f01bc Sushmita Susheelendra 2017-06-13  644  void msm_gem_purge(struct drm_gem_object *obj, enum msm_gem_lock subclass)
68209390f11603 Rob Clark             2016-05-17  645  {
68209390f11603 Rob Clark             2016-05-17  646  	struct drm_device *dev = obj->dev;
68209390f11603 Rob Clark             2016-05-17  647  	struct msm_gem_object *msm_obj = to_msm_bo(obj);
68209390f11603 Rob Clark             2016-05-17  648  
68209390f11603 Rob Clark             2016-05-17  649  	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
68209390f11603 Rob Clark             2016-05-17  650  	WARN_ON(!is_purgeable(msm_obj));
68209390f11603 Rob Clark             2016-05-17  651  	WARN_ON(obj->import_attach);
68209390f11603 Rob Clark             2016-05-17  652  
0e08270a1f01bc Sushmita Susheelendra 2017-06-13  653  	mutex_lock_nested(&msm_obj->lock, subclass);
0e08270a1f01bc Sushmita Susheelendra 2017-06-13  654  
68209390f11603 Rob Clark             2016-05-17  655  	put_iova(obj);
68209390f11603 Rob Clark             2016-05-17  656  
0e08270a1f01bc Sushmita Susheelendra 2017-06-13  657  	msm_gem_vunmap_locked(obj);
68209390f11603 Rob Clark             2016-05-17  658  
68209390f11603 Rob Clark             2016-05-17  659  	put_pages(obj);
68209390f11603 Rob Clark             2016-05-17  660  
68209390f11603 Rob Clark             2016-05-17  661  	msm_obj->madv = __MSM_MADV_PURGED;
68209390f11603 Rob Clark             2016-05-17  662  
68209390f11603 Rob Clark             2016-05-17  663  	drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
68209390f11603 Rob Clark             2016-05-17  664  	drm_gem_free_mmap_offset(obj);
68209390f11603 Rob Clark             2016-05-17  665  
68209390f11603 Rob Clark             2016-05-17  666  	/* Our goal here is to return as much of the memory as
68209390f11603 Rob Clark             2016-05-17  667  	 * is possible back to the system as we are called from OOM.
68209390f11603 Rob Clark             2016-05-17  668  	 * To do this we must instruct the shmfs to drop all of its
68209390f11603 Rob Clark             2016-05-17  669  	 * backing pages, *now*.
68209390f11603 Rob Clark             2016-05-17  670  	 */
68209390f11603 Rob Clark             2016-05-17 @671  	shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
68209390f11603 Rob Clark             2016-05-17  672  
68209390f11603 Rob Clark             2016-05-17  673  	invalidate_mapping_pages(file_inode(obj->filp)->i_mapping,
68209390f11603 Rob Clark             2016-05-17  674  			0, (loff_t)-1);
0e08270a1f01bc Sushmita Susheelendra 2017-06-13  675  
0e08270a1f01bc Sushmita Susheelendra 2017-06-13  676  	mutex_unlock(&msm_obj->lock);
68209390f11603 Rob Clark             2016-05-17  677  }
68209390f11603 Rob Clark             2016-05-17  678  

:::::: The code at line 671 was first introduced by commit :::::: 68209390f116034449fa6a3ae03f7b100b3d894a drm/msm: shrinker support

:::::: TO: Rob Clark <[email protected]>
:::::: CC: Rob Clark <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation

#2 lore raw
In reply to Yang Shi
Quoted 15 lines · Yang Shi

Currently when truncating shmem file, if the range is partial of THP (start or end is in the middle of THP), the pages actually will just get cleared rather than being freed unless the range cover the whole THP. Even though all the subpages are truncated (randomly or sequentially), the THP may still be kept in page cache. This might be fine for some usecases which prefer preserving THP.

But, when doing balloon inflation in QEMU, QEMU actually does hole punch or MADV_DONTNEED in base page size granulairty if hugetlbfs is not used. So, when using shmem THP as memory backend QEMU inflation actually doesn't work as expected since it doesn't free memory. But, the inflation usecase really needs get the memory freed. Anonymous THP will not get freed right away too but it will be freed eventually when all subpages are unmapped, but shmem THP would still stay in page cache.

Quoted 4 lines

To protect the usecases which may prefer preserving THP, introduce a new fallocate mode: FALLOC_FL_SPLIT_HPAGE, which means spltting THP is preferred behavior if truncating partial THP. This mode just makes sense to tmpfs for the time being.

We need to clarify interaction with khugepaged. This implementation doesn't do anything to prevent khugepaged from collapsing the range back to THP just after the split.

Quoted 21 lines
mm/shmem.c +15 -0
@@ -976,8 +1022,31 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
 			}
 			unlock_page(page);
 		}
+rescan_split:
 		pagevec_remove_exceptionals(&pvec);
 		pagevec_release(&pvec);
+
+		if (split && PageTransCompound(page)) {
+			/* The THP may get freed under us */
+			if (!get_page_unless_zero(compound_head(page)))
+				goto rescan_out;
+
+			lock_page(page);
+
+			/*
+			 * The extra pins from page cache lookup have been
+			 * released by pagevec_release().
+			 */
+			if (!split_huge_page(page)) {
+				unlock_page(page);

mm/shmem.c static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, · line 1051

Quoted 10 lines
mm/shmem.c +8 -0
+				put_page(page);
+				/* Re-look up page cache from current index */
+				goto again;
+			}
+			unlock_page(page);
+			put_page(page);
+		}
+rescan_out:
 		index++;
 	}

Doing get_page_unless_zero() just after you've dropped the pin for the page looks very suboptimal.

Signature · 1 line
 Kirill A. Shutemov
author
#3 lore raw
Quoted 16 lines · Kirill A. Shutemov
Quoted 15 lines · Yang Shi

Currently when truncating shmem file, if the range is partial of THP (start or end is in the middle of THP), the pages actually will just get cleared rather than being freed unless the range cover the whole THP. Even though all the subpages are truncated (randomly or sequentially), the THP may still be kept in page cache. This might be fine for some usecases which prefer preserving THP.

But, when doing balloon inflation in QEMU, QEMU actually does hole punch or MADV_DONTNEED in base page size granulairty if hugetlbfs is not used. So, when using shmem THP as memory backend QEMU inflation actually doesn't work as expected since it doesn't free memory. But, the inflation usecase really needs get the memory freed. Anonymous THP will not get freed right away too but it will be freed eventually when all subpages are unmapped, but shmem THP would still stay in page cache.

Quoted 7 lines
Quoted 4 lines

To protect the usecases which may prefer preserving THP, introduce a new fallocate mode: FALLOC_FL_SPLIT_HPAGE, which means spltting THP is preferred behavior if truncating partial THP. This mode just makes sense to tmpfs for the time being.

We need to clarify interaction with khugepaged. This implementation doesn't do anything to prevent khugepaged from collapsing the range back to THP just after the split.

Yes, it doesn't. Will clarify this in the commit log.

Quoted 26 lines
Quoted 26 lines
mm/shmem.c +19 -0
@@ -976,8 +1022,31 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
  			}
  			unlock_page(page);
  		}
+rescan_split:
  		pagevec_remove_exceptionals(&pvec);
  		pagevec_release(&pvec);
+
+		if (split && PageTransCompound(page)) {
+			/* The THP may get freed under us */
+			if (!get_page_unless_zero(compound_head(page)))
+				goto rescan_out;
+
+			lock_page(page);
+
+			/*
+			 * The extra pins from page cache lookup have been
+			 * released by pagevec_release().
+			 */
+			if (!split_huge_page(page)) {
+				unlock_page(page);
+				put_page(page);
+				/* Re-look up page cache from current index */
+				goto again;
+			}
Quoted 8 lines

mm/shmem.c static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, · line 1051

Quoted 6 lines
mm/shmem.c +4 -0
+			unlock_page(page);
+			put_page(page);
+		}
+rescan_out:
  		index++;
  	}

Doing get_page_unless_zero() just after you've dropped the pin for the page looks very suboptimal.

If I don't drop the pins the THP can't be split. And, there might be more than one pins from find_get_entries() if I read the code correctly. For example, truncate 8K length in the middle of THP, the THP's refcount would get bumpped twice since  two sub pages would be returned.

Quoted 1 line
#4 lore raw
Quoted 23 lines · Yang Shi
Quoted 20 lines · Kirill A. Shutemov
Quoted 19 lines · Yang Shi

Currently when truncating shmem file, if the range is partial of THP (start or end is in the middle of THP), the pages actually will just get cleared rather than being freed unless the range cover the whole THP. Even though all the subpages are truncated (randomly or sequentially), the THP may still be kept in page cache. This might be fine for some usecases which prefer preserving THP.

But, when doing balloon inflation in QEMU, QEMU actually does hole punch or MADV_DONTNEED in base page size granulairty if hugetlbfs is not used. So, when using shmem THP as memory backend QEMU inflation actually doesn't work as expected since it doesn't free memory. But, the inflation usecase really needs get the memory freed. Anonymous THP will not get freed right away too but it will be freed eventually when all subpages are unmapped, but shmem THP would still stay in page cache.

To protect the usecases which may prefer preserving THP, introduce a new fallocate mode: FALLOC_FL_SPLIT_HPAGE, which means spltting THP is preferred behavior if truncating partial THP. This mode just makes sense to tmpfs for the time being.

Quoted 5 lines
Quoted 3 lines

We need to clarify interaction with khugepaged. This implementation doesn't do anything to prevent khugepaged from collapsing the range back to THP just after the split.

Yes, it doesn't. Will clarify this in the commit log.

Okay, but I'm not sure that documention alone will be enough. We need proper design.

Quoted 34 lines
Quoted 25 lines
Quoted 25 lines
mm/shmem.c +19 -0
@@ -976,8 +1022,31 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
  			}
  			unlock_page(page);
  		}
+rescan_split:
  		pagevec_remove_exceptionals(&pvec);
  		pagevec_release(&pvec);
+
+		if (split && PageTransCompound(page)) {
+			/* The THP may get freed under us */
+			if (!get_page_unless_zero(compound_head(page)))
+				goto rescan_out;
+
+			lock_page(page);
+
+			/*
+			 * The extra pins from page cache lookup have been
+			 * released by pagevec_release().
+			 */
+			if (!split_huge_page(page)) {
+				unlock_page(page);
+				put_page(page);
+				/* Re-look up page cache from current index */
+				goto again;
+			}
Quoted 8 lines

mm/shmem.c static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, · line 1051

Quoted 6 lines
mm/shmem.c +4 -0
+			unlock_page(page);
+			put_page(page);
+		}
+rescan_out:
  		index++;
  	}

Doing get_page_unless_zero() just after you've dropped the pin for the page looks very suboptimal.

Quoted 4 lines

If I don't drop the pins the THP can't be split. And, there might be more than one pins from find_get_entries() if I read the code correctly. For example, truncate 8K length in the middle of THP, the THP's refcount would get bumpped twice since  two sub pages would be returned.

Pin the page before pagevec_release() and avoid get_page_unless_zero().

Current code is buggy. You need to check that the page is still belong to the file after speculative lookup.

Signature · 1 line
 Kirill A. Shutemov
author
#5 lore raw
Quoted 23 lines · Kirill A. Shutemov
Quoted 22 lines · Yang Shi
Quoted 20 lines · Kirill A. Shutemov
Quoted 19 lines · Yang Shi

Currently when truncating shmem file, if the range is partial of THP (start or end is in the middle of THP), the pages actually will just get cleared rather than being freed unless the range cover the whole THP. Even though all the subpages are truncated (randomly or sequentially), the THP may still be kept in page cache. This might be fine for some usecases which prefer preserving THP.

But, when doing balloon inflation in QEMU, QEMU actually does hole punch or MADV_DONTNEED in base page size granulairty if hugetlbfs is not used. So, when using shmem THP as memory backend QEMU inflation actually doesn't work as expected since it doesn't free memory. But, the inflation usecase really needs get the memory freed. Anonymous THP will not get freed right away too but it will be freed eventually when all subpages are unmapped, but shmem THP would still stay in page cache.

To protect the usecases which may prefer preserving THP, introduce a new fallocate mode: FALLOC_FL_SPLIT_HPAGE, which means spltting THP is preferred behavior if truncating partial THP. This mode just makes sense to tmpfs for the time being.

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

We need to clarify interaction with khugepaged. This implementation doesn't do anything to prevent khugepaged from collapsing the range back to THP just after the split.

Yes, it doesn't. Will clarify this in the commit log.

Okay, but I'm not sure that documention alone will be enough. We need proper design.

Maybe we could try to hold inode lock with read during collapse_file(). The shmem fallocate does acquire inode lock with write, this should be able to synchronize hole punch and khugepaged. And, shmem just needs hold inode lock for llseek and fallocate, I'm supposed they are should be called not that frequently to have impact on khugepaged. The llseek might be often, but it should be quite fast. However, they might get blocked by khugepaged.

It sounds safe to hold a rwsem during collapsing THP.

Or we could set VM_NOHUGEPAGE in shmem inode's flag with hole punch and clear it after truncate, then check the flag before doing collapse in khugepaged. khugepaged should not need hold the inode lock during collapse since it could be released after the flag is checked.

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 22 lines
mm/shmem.c +15 -0
@@ -976,8 +1022,31 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
   			}
   			unlock_page(page);
   		}
+rescan_split:
   		pagevec_remove_exceptionals(&pvec);
   		pagevec_release(&pvec);
+
+		if (split && PageTransCompound(page)) {
+			/* The THP may get freed under us */
+			if (!get_page_unless_zero(compound_head(page)))
+				goto rescan_out;
+
+			lock_page(page);
+
+			/*
+			 * The extra pins from page cache lookup have been
+			 * released by pagevec_release().
+			 */
+			if (!split_huge_page(page)) {
+				unlock_page(page);

mm/shmem.c static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, · line 1051

Quoted 10 lines
mm/shmem.c +8 -0
+				put_page(page);
+				/* Re-look up page cache from current index */
+				goto again;
+			}
+			unlock_page(page);
+			put_page(page);
+		}
+rescan_out:
   		index++;
   	}

Doing get_page_unless_zero() just after you've dropped the pin for the page looks very suboptimal.

Quoted 8 lines
Quoted 4 lines

If I don't drop the pins the THP can't be split. And, there might be more than one pins from find_get_entries() if I read the code correctly. For example, truncate 8K length in the middle of THP, the THP's refcount would get bumpped twice since  two sub pages would be returned.

Pin the page before pagevec_release() and avoid get_page_unless_zero().

Current code is buggy. You need to check that the page is still belong to the file after speculative lookup.

Yes, I missed this point. Thanks for the suggestion.

Quoted 1 line
author
#6 lore raw
Quoted 30 lines · Yang Shi
Quoted 27 lines · Kirill A. Shutemov
Quoted 26 lines · Yang Shi
Quoted 16 lines · Kirill A. Shutemov
Quoted 15 lines · Yang Shi

Currently when truncating shmem file, if the range is partial of THP (start or end is in the middle of THP), the pages actually will just get cleared rather than being freed unless the range cover the whole THP. Even though all the subpages are truncated (randomly or sequentially), the THP may still be kept in page cache.  This might be fine for some usecases which prefer preserving THP.

But, when doing balloon inflation in QEMU, QEMU actually does hole punch or MADV_DONTNEED in base page size granulairty if hugetlbfs is not used. So, when using shmem THP as memory backend QEMU inflation actually doesn't work as expected since it doesn't free memory.  But, the inflation usecase really needs get the memory freed.  Anonymous THP will not get freed right away too but it will be freed eventually when all subpages are unmapped, but shmem THP would still stay in page cache.

Quoted 7 lines
Quoted 4 lines

To protect the usecases which may prefer preserving THP, introduce a new fallocate mode: FALLOC_FL_SPLIT_HPAGE, which means spltting THP is preferred behavior if truncating partial THP.  This mode just makes sense to tmpfs for the time being.

We need to clarify interaction with khugepaged. This implementation doesn't do anything to prevent khugepaged from collapsing the range back to THP just after the split.

Yes, it doesn't. Will clarify this in the commit log.

Quoted 8 lines
Quoted 2 lines

Okay, but I'm not sure that documention alone will be enough. We need proper design.

Maybe we could try to hold inode lock with read during collapse_file(). The shmem fallocate does acquire inode lock with write, this should be able to synchronize hole punch and khugepaged. And, shmem just needs hold inode lock for llseek and fallocate, I'm supposed they are should be called not that frequently to have impact on khugepaged. The llseek might be often, but it should be quite fast. However, they might get blocked by khugepaged.

It sounds safe to hold a rwsem during collapsing THP.

Or we could set VM_NOHUGEPAGE in shmem inode's flag with hole punch and clear it after truncate, then check the flag before doing collapse in khugepaged. khugepaged should not need hold the inode lock during collapse since it could be released after the flag is checked.

By relooking the code, it looks the latter one (check VM_NOHUGEPAGE) doesn't make sense, it can't prevent khugepaged from collapsing THP in parallel.

Quoted 49 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Quoted 24 lines
mm/shmem.c +15 -0
@@ -976,8 +1022,31 @@ static void shmem_undo_range(struct inode 
 *inode, loff_t lstart, loff_t lend,
                }
                unlock_page(page);
            }
+rescan_split:
            pagevec_remove_exceptionals(&pvec);
            pagevec_release(&pvec);
+
+        if (split && PageTransCompound(page)) {
+            /* The THP may get freed under us */
+            if (!get_page_unless_zero(compound_head(page)))
+                goto rescan_out;
+
+            lock_page(page);
+
+            /*
+             * The extra pins from page cache lookup have been
+             * released by pagevec_release().
+             */
+            if (!split_huge_page(page)) {
+                unlock_page(page);

mm/shmem.c static void shmem_undo_range(struct inode · line 1052

Quoted 10 lines
mm/shmem.c +8 -0
+                put_page(page);
+                /* Re-look up page cache from current index */
+                goto again;
+            }
+            unlock_page(page);
+            put_page(page);
+        }
+rescan_out:
            index++;
        }

Doing get_page_unless_zero() just after you've dropped the pin for the page looks very suboptimal.

Quoted 8 lines
Quoted 4 lines

If I don't drop the pins the THP can't be split. And, there might be more than one pins from find_get_entries() if I read the code correctly. For example, truncate 8K length in the middle of THP, the THP's refcount would get bumpped twice since  two sub pages would be returned.

Pin the page before pagevec_release() and avoid get_page_unless_zero().

Current code is buggy. You need to check that the page is still belong to the file after speculative lookup.

Yes, I missed this point. Thanks for the suggestion.

Quoted 1 line
maintainer
#7 lore raw
Quoted 30 lines · Yang Shi
Quoted 29 lines · Yang Shi
Quoted 28 lines · Kirill A. Shutemov
Quoted 27 lines · Yang Shi
Quoted 26 lines · Kirill A. Shutemov
Quoted 25 lines · Yang Shi

Currently when truncating shmem file, if the range is partial of THP
(start or end is in the middle of THP), the pages actually will just get
cleared rather than being freed unless the range cover the whole THP.
Even though all the subpages are truncated (randomly or sequentially),
the THP may still be kept in page cache.  This might be fine for some
usecases which prefer preserving THP.

But, when doing balloon inflation in QEMU, QEMU actually does hole punch
or MADV_DONTNEED in base page size granulairty if hugetlbfs is not used.
So, when using shmem THP as memory backend QEMU inflation actually doesn't
work as expected since it doesn't free memory.  But, the inflation usecase really needs get the memory freed.  Anonymous THP will not get
freed right away too but it will be freed eventually when all subpages are
unmapped, but shmem THP would still stay in page cache.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

To protect the usecases which may prefer preserving THP, introduce a
new fallocate mode: FALLOC_FL_SPLIT_HPAGE, which means spltting THP is
preferred behavior if truncating partial THP.  This mode just makes sense to tmpfs for the time being.

Sorry, I haven't managed to set aside enough time for this until now.

First off, let me say that I firmly believe this punch-split behavior should be the standard behavior (like in my huge tmpfs implementation), and we should not need a special FALLOC_FL_SPLIT_HPAGE to do it. But I don't know if I'll be able to persuade Kirill of that.

If the caller wants to write zeroes into the file, she can do so with the write syscall: the caller has asked to punch a hole or truncate the file, and in our case, like your QEMU case, hopes that memory and memcg charge will be freed by doing so. I'll be surprised if changing the behavior to yours and mine turns out to introduce a regression, but if it does, I guess we'll then have to put it behind a sysctl or whatever.

IIUC the reason that it's currently implemented by clearing the hole is because split_huge_page() (unlike in older refcounting days) cannot be guaranteed to succeed. Which is unfortunate, and none of us is very keen to build a filesystem on unreliable behavior; but the failure cases appear in practice to be rare enough, that it's on balance better to give the punch-hole-truncate caller what she asked for whenever possible.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

We need to clarify interaction with khugepaged. This implementation doesn't do anything to prevent khugepaged from collapsing the range back
to THP just after the split.

Yes, it doesn't. Will clarify this in the commit log.

Okay, but I'm not sure that documention alone will be enough. We need proper design.

Quoted 8 lines
Quoted 8 lines

Maybe we could try to hold inode lock with read during collapse_file(). The shmem fallocate does acquire inode lock with write, this should be able to synchronize hole punch and khugepaged. And, shmem just needs hold inode lock for llseek and fallocate, I'm supposed they are should be called not that frequently to have impact on khugepaged. The llseek might be often, but it should be quite fast. However, they might get blocked by khugepaged.

It sounds safe to hold a rwsem during collapsing THP.

No, I don't think we want to take any more locks while collapsing THP, but that wasn't really the point. We're not concerned about a *race* between splitting and khugepaged reassembling (I'm assuming that any such race would already exist, and be well-guarded against by all the refcount checks, punchhole not adding anything new here; but perhaps I'm assuming too blithely, and it is worth checking over).

The point, as I see it anyway, is the contradiction in effort: the caller asks for hole to be punched, we do that, then a few seconds or minutes later, khugepaged comes along and fills in the hole (if huge page availability and memcg limit allow it).

I agree that's not very satisfactory, but I think it's a side issue:
we don't have a good mechanism to tell khugepaged to keep off a range. As it is, fallocate and ftruncate ought to do the job expected of them, and khugepaged ought to do the job expected of it. And in many cases, the punched file will not even be mapped (visible to khugepaged), or max_ptes_none set to exclude working on such holes.

Is khugepaged's action an issue for your QEMU case?

Quoted 56 lines
Quoted 5 lines

Or we could set VM_NOHUGEPAGE in shmem inode's flag with hole punch and clear it after truncate, then check the flag before doing collapse in khugepaged. khugepaged should not need hold the inode lock during collapse since it could be released after the flag is checked.

By relooking the code, it looks the latter one (check VM_NOHUGEPAGE) doesn't make sense, it can't prevent khugepaged from collapsing THP in parallel.

Quoted 47 lines
Quoted 47 lines
Quoted 37 lines
Quoted 29 lines
Quoted 29 lines
(quoted diff) +20 -0
@@ -976,8 +1022,31 @@ static void shmem_undo_range(struct inode
 *inode, loff_t lstart, loff_t lend,
                }
                unlock_page(page);
            }
+rescan_split:
            pagevec_remove_exceptionals(&pvec);
            pagevec_release(&pvec);
+
+        if (split && PageTransCompound(page)) {
+            /* The THP may get freed under us */
+            if (!get_page_unless_zero(compound_head(page)))
+                goto rescan_out;
+
+            lock_page(page);
+
+            /*
+             * The extra pins from page cache lookup have been
+             * released by pagevec_release().
+             */
+            if (!split_huge_page(page)) {
+                unlock_page(page);
+                put_page(page);
+                /* Re-look up page cache from current index */
+                goto again;
+            }
+            unlock_page(page);
Quoted 8 lines

Review static void shmem_undo_range(struct inode · line 1052

Quoted 5 lines
(quoted diff) +3 -0
+            put_page(page);
+        }
+rescan_out:
            index++;
        }

Doing get_page_unless_zero() just after you've dropped the pin for the
page looks very suboptimal.

Quoted 6 lines

If I don't drop the pins the THP can't be split. And, there might be more
than one pins from find_get_entries() if I read the code correctly. For example, truncate 8K length in the middle of THP, the THP's refcount would
get bumpped twice since  two sub pages would be returned.

Pin the page before pagevec_release() and avoid get_page_unless_zero().

Current code is buggy. You need to check that the page is still belong to the file after speculative lookup.

Yes indeed (I think you can even keep the page locked, but I may be wrong).

Quoted 2 lines
Quoted 2 lines

Yes, I missed this point. Thanks for the suggestion.

The main problem I see is your "goto retry" and "goto again":
split_huge_page() may fail because a get_page() somewhere is holding a transient reference to the page, or it may fail because there's a GUP that holds a reference for days: you do not want to be stuck here going round and around the loop waiting for that GUP to be released!

It's nice that we already have a trylock_page() loop followed by a lock_page() loop. When split_huge_page() fails, the trylock_page() loop can simply move on to the next page (skip over the compound page, or retry it subpage by subpage? I've forgotten the pros and cons), and leave final resolution to the later lock_page() loop: which has to accept when split_huge_page() failed, and fall back to clearing instead.

I would prefer a smaller patch than your RFC: making split the default behavior cuts out a lot of it, but I think there's still more that can be cut. Here's the patch we've been using internally, which deletes quite a lot of the old code; but you'll quickly notice has a "Revisit later" hack in find_get_entries(), which I've not got around to revisiting yet. Please blend what you can from my patch into yours, or vice versa.

Hugh

---

 mm/filemap.c |    3 +
 mm/shmem.c   |   86 +++++++++++++++++--------------------------------
 2 files changed, 34 insertions(+), 55 deletions(-)
mm/filemap.c +3 -0
@@ -1752,6 +1752,9 @@ unsigned find_get_entries(struct address
 			goto put_page;
 		page = find_subpage(page, xas.xa_index);
 
+		/* Revisit later: make shmem_undo_range() easier for now */
+		if (PageTransCompound(page))
+			nr_entries = ret + 1;
 export:
 		indices[ret] = xas.xa_index;
 		entries[ret] = page;
mm/shmem.c +31 -55

111 lines not on this page · raw message

#8 lore raw
Quoted 52 lines · Hugh Dickins
Quoted 30 lines · Yang Shi
Quoted 29 lines · Yang Shi
Quoted 28 lines · Kirill A. Shutemov
Quoted 27 lines · Yang Shi
Quoted 26 lines · Kirill A. Shutemov
Quoted 25 lines · Yang Shi

Currently when truncating shmem file, if the range is partial of THP
(start or end is in the middle of THP), the pages actually will just get
cleared rather than being freed unless the range cover the whole THP.
Even though all the subpages are truncated (randomly or sequentially),
the THP may still be kept in page cache.  This might be fine for some
usecases which prefer preserving THP.

But, when doing balloon inflation in QEMU, QEMU actually does hole punch
or MADV_DONTNEED in base page size granulairty if hugetlbfs is not used.
So, when using shmem THP as memory backend QEMU inflation actually doesn't
work as expected since it doesn't free memory.  But, the inflation usecase really needs get the memory freed.  Anonymous THP will not get
freed right away too but it will be freed eventually when all subpages are
unmapped, but shmem THP would still stay in page cache.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

To protect the usecases which may prefer preserving THP, introduce a
new fallocate mode: FALLOC_FL_SPLIT_HPAGE, which means spltting THP is
preferred behavior if truncating partial THP.  This mode just makes sense to tmpfs for the time being.

Sorry, I haven't managed to set aside enough time for this until now.

First off, let me say that I firmly believe this punch-split behavior should be the standard behavior (like in my huge tmpfs implementation), and we should not need a special FALLOC_FL_SPLIT_HPAGE to do it. But I don't know if I'll be able to persuade Kirill of that.

If the caller wants to write zeroes into the file, she can do so with the write syscall: the caller has asked to punch a hole or truncate the file, and in our case, like your QEMU case, hopes that memory and memcg charge will be freed by doing so. I'll be surprised if changing the behavior to yours and mine turns out to introduce a regression, but if it does, I guess we'll then have to put it behind a sysctl or whatever.

Quoted 6 lines

IIUC the reason that it's currently implemented by clearing the hole is because split_huge_page() (unlike in older refcounting days) cannot be guaranteed to succeed. Which is unfortunate, and none of us is very keen to build a filesystem on unreliable behavior; but the failure cases appear in practice to be rare enough, that it's on balance better to give the punch-hole-truncate caller what she asked for whenever possible.

I don't have a firm position here. Maybe you are right and we should try to split pages right away.

It might be useful to consider case wider than shmem.

On traditional filesystem with a backing storage semantics of the same punch hole operation is somewhat different. It doesn't have explicit implications on memory footprint. It's about managing persistent storage. With shmem/tmpfs it is lumped together.

It might be nice to write down pages that can be discarded under memory pressure and leave the huge page intact until then...

[ I don't see a problem with your patch as long as we agree that it's desired semantics for the interface. ]

Signature · 1 line
 Kirill A. Shutemov
author
#10 lore raw
In reply to Hugh Dickins
Quoted 44 lines · Hugh Dickins
Quoted 30 lines · Yang Shi
Quoted 29 lines · Yang Shi
Quoted 28 lines · Kirill A. Shutemov
Quoted 27 lines · Yang Shi
Quoted 26 lines · Kirill A. Shutemov
Quoted 25 lines · Yang Shi

Currently when truncating shmem file, if the range is partial of THP
(start or end is in the middle of THP), the pages actually will just get
cleared rather than being freed unless the range cover the whole THP.
Even though all the subpages are truncated (randomly or sequentially),
the THP may still be kept in page cache.  This might be fine for some
usecases which prefer preserving THP.

But, when doing balloon inflation in QEMU, QEMU actually does hole punch
or MADV_DONTNEED in base page size granulairty if hugetlbfs is not used.
So, when using shmem THP as memory backend QEMU inflation actually doesn't
work as expected since it doesn't free memory.  But, the inflation usecase really needs get the memory freed.  Anonymous THP will not get
freed right away too but it will be freed eventually when all subpages are
unmapped, but shmem THP would still stay in page cache.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

To protect the usecases which may prefer preserving THP, introduce a
new fallocate mode: FALLOC_FL_SPLIT_HPAGE, which means spltting THP is
preferred behavior if truncating partial THP.  This mode just makes sense to tmpfs for the time being.

Sorry, I haven't managed to set aside enough time for this until now.

First off, let me say that I firmly believe this punch-split behavior should be the standard behavior (like in my huge tmpfs implementation), and we should not need a special FALLOC_FL_SPLIT_HPAGE to do it. But I don't know if I'll be able to persuade Kirill of that.

Quoted 6 lines

If the caller wants to write zeroes into the file, she can do so with the write syscall: the caller has asked to punch a hole or truncate the file, and in our case, like your QEMU case, hopes that memory and memcg charge will be freed by doing so. I'll be surprised if changing the behavior to yours and mine turns out to introduce a regression, but if it does, I guess we'll then have to put it behind a sysctl or whatever.

I'm not sure if there may be regression or not so I added the flag to have a choice. But, sysctl may seem better. Anyway, I agree we don't have to consider the potential regression until the real regression is found.

Quoted 35 lines

IIUC the reason that it's currently implemented by clearing the hole is because split_huge_page() (unlike in older refcounting days) cannot be guaranteed to succeed. Which is unfortunate, and none of us is very keen to build a filesystem on unreliable behavior; but the failure cases appear in practice to be rare enough, that it's on balance better to give the punch-hole-truncate caller what she asked for whenever possible.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

We need to clarify interaction with khugepaged. This implementation doesn't do anything to prevent khugepaged from collapsing the range back
to THP just after the split.

Yes, it doesn't. Will clarify this in the commit log.

Okay, but I'm not sure that documention alone will be enough. We need proper design.

Quoted 8 lines
Quoted 8 lines

Maybe we could try to hold inode lock with read during collapse_file(). The shmem fallocate does acquire inode lock with write, this should be able to synchronize hole punch and khugepaged. And, shmem just needs hold inode lock for llseek and fallocate, I'm supposed they are should be called not that frequently to have impact on khugepaged. The llseek might be often, but it should be quite fast. However, they might get blocked by khugepaged.

It sounds safe to hold a rwsem during collapsing THP.

No, I don't think we want to take any more locks while collapsing THP, but that wasn't really the point. We're not concerned about a *race* between splitting and khugepaged reassembling (I'm assuming that any such race would already exist, and be well-guarded against by all the refcount checks, punchhole not adding anything new here; but perhaps I'm assuming too blithely, and it is worth checking over).

The point, as I see it anyway, is the contradiction in effort: the caller asks for hole to be punched, we do that, then a few seconds or minutes later, khugepaged comes along and fills in the hole (if huge page availability and memcg limit allow it).

Quoted 8 lines

I agree that's not very satisfactory, but I think it's a side issue:
we don't have a good mechanism to tell khugepaged to keep off a range. As it is, fallocate and ftruncate ought to do the job expected of them, and khugepaged ought to do the job expected of it. And in many cases, the punched file will not even be mapped (visible to khugepaged), or max_ptes_none set to exclude working on such holes.

Is khugepaged's action an issue for your QEMU case?

So far not.

Quoted 53 lines
Quoted 53 lines
Quoted 5 lines

Or we could set VM_NOHUGEPAGE in shmem inode's flag with hole punch and clear it after truncate, then check the flag before doing collapse in khugepaged. khugepaged should not need hold the inode lock during collapse since it could be released after the flag is checked.

By relooking the code, it looks the latter one (check VM_NOHUGEPAGE) doesn't make sense, it can't prevent khugepaged from collapsing THP in parallel.

Quoted 45 lines
Quoted 45 lines
Quoted 35 lines
Quoted 27 lines
Quoted 27 lines
(quoted diff) +20 -0
@@ -976,8 +1022,31 @@ static void shmem_undo_range(struct inode
 *inode, loff_t lstart, loff_t lend,
                }
                unlock_page(page);
            }
+rescan_split:
            pagevec_remove_exceptionals(&pvec);
            pagevec_release(&pvec);
+
+        if (split && PageTransCompound(page)) {
+            /* The THP may get freed under us */
+            if (!get_page_unless_zero(compound_head(page)))
+                goto rescan_out;
+
+            lock_page(page);
+
+            /*
+             * The extra pins from page cache lookup have been
+             * released by pagevec_release().
+             */
+            if (!split_huge_page(page)) {
+                unlock_page(page);
+                put_page(page);
+                /* Re-look up page cache from current index */
+                goto again;
+            }
+            unlock_page(page);
Quoted 8 lines

Review static void shmem_undo_range(struct inode · line 1052

Quoted 5 lines
(quoted diff) +3 -0
+            put_page(page);
+        }
+rescan_out:
            index++;
        }

Doing get_page_unless_zero() just after you've dropped the pin for the
page looks very suboptimal.

Quoted 6 lines

If I don't drop the pins the THP can't be split. And, there might be more
than one pins from find_get_entries() if I read the code correctly. For example, truncate 8K length in the middle of THP, the THP's refcount would
get bumpped twice since  two sub pages would be returned.

Pin the page before pagevec_release() and avoid get_page_unless_zero().

Current code is buggy. You need to check that the page is still belong to the file after speculative lookup.

Quoted 8 lines

Yes indeed (I think you can even keep the page locked, but I may be wrong).

Quoted 1 line
Quoted 1 line

Yes, I missed this point. Thanks for the suggestion.

The main problem I see is your "goto retry" and "goto again":
split_huge_page() may fail because a get_page() somewhere is holding a transient reference to the page, or it may fail because there's a GUP that holds a reference for days: you do not want to be stuck here going round and around the loop waiting for that GUP to be released!

I think my code already handled this case. Once the split is failed, it just falls through to next index. It just does retry as long as split succeeds.

But my patch does clear before split.

Quoted 8 lines

It's nice that we already have a trylock_page() loop followed by a lock_page() loop. When split_huge_page() fails, the trylock_page() loop can simply move on to the next page (skip over the compound page, or retry it subpage by subpage? I've forgotten the pros and cons), and leave final resolution to the later lock_page() loop: which has to accept when split_huge_page() failed, and fall back to clearing instead.

Quoted 7 lines

I would prefer a smaller patch than your RFC: making split the default behavior cuts out a lot of it, but I think there's still more that can be cut. Here's the patch we've been using internally, which deletes quite a lot of the old code; but you'll quickly notice has a "Revisit later" hack in find_get_entries(), which I've not got around to revisiting yet. Please blend what you can from my patch into yours, or vice versa.

Thank you very much. It looks your "Revisit later" hack tries to keep just one extra pin for the THP so that split could succeed.

I will try to blend the two patches.

Quoted 137 lines

Hugh

---

  mm/filemap.c |    3 +
  mm/shmem.c   |   86 +++++++++++++++++--------------------------------
  2 files changed, 34 insertions(+), 55 deletions(-)
mm/filemap.c +3 -0
@@ -1752,6 +1752,9 @@ unsigned find_get_entries(struct address
  			goto put_page;
  		page = find_subpage(page, xas.xa_index);
 
+		/* Revisit later: make shmem_undo_range() easier for now */
+		if (PageTransCompound(page))
+			nr_entries = ret + 1;
  export:
  		indices[ret] = xas.xa_index;
  		entries[ret] = page;
mm/shmem.c +31 -55
@@ -788,6 +788,20 @@ void shmem_unlock_mapping(struct address
  	}
  }
 
+static bool shmem_punch_compound(struct page *page, pgoff_t start, pgoff_t end)
+{
+	if (!PageTransCompound(page))
+		return true;
+
+	/* Just proceed to delete a huge page wholly within the range punched */
+	if (PageHead(page) &&
+	    page->index >= start && page->index + HPAGE_PMD_NR <= end)
+		return true;
+
+	/* Try to split huge page, so we can truly punch the hole or truncate */
+	return split_huge_page(page) >= 0;
+}
+
  /*
   * Remove range of pages and swap entries from page cache, and free them.
   * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
@@ -838,31 +852,11 @@ static void shmem_undo_range(struct inod
  			if (!trylock_page(page))
  				continue;
 
-			if (PageTransTail(page)) {
-				/* Middle of THP: zero out the page */
-				clear_highpage(page);
-				unlock_page(page);
-				continue;
-			} else if (PageTransHuge(page)) {
-				if (index == round_down(end, HPAGE_PMD_NR)) {
-					/*
-					 * Range ends in the middle of THP:
-					 * zero out the page
-					 */
-					clear_highpage(page);
-					unlock_page(page);
-					continue;
-				}
-				index += HPAGE_PMD_NR - 1;
-				i += HPAGE_PMD_NR - 1;
-			}
-
-			if (!unfalloc || !PageUptodate(page)) {
-				VM_BUG_ON_PAGE(PageTail(page), page);
-				if (page_mapping(page) == mapping) {
-					VM_BUG_ON_PAGE(PageWriteback(page), page);
+			if ((!unfalloc || !PageUptodate(page)) &&
+			    page_mapping(page) == mapping) {
+				VM_BUG_ON_PAGE(PageWriteback(page), page);
+				if (shmem_punch_compound(page, start, end))
  					truncate_inode_page(mapping, page);
-				}
  			}
  			unlock_page(page);
  		}
@@ -936,43 +930,25 @@ static void shmem_undo_range(struct inod
 
  			lock_page(page);
 
-			if (PageTransTail(page)) {
-				/* Middle of THP: zero out the page */
-				clear_highpage(page);
-				unlock_page(page);
-				/*
-				 * Partial thp truncate due 'start' in middle
-				 * of THP: don't need to look on these pages
-				 * again on !pvec.nr restart.
-				 */
-				if (index != round_down(end, HPAGE_PMD_NR))
-					start++;
-				continue;
-			} else if (PageTransHuge(page)) {
-				if (index == round_down(end, HPAGE_PMD_NR)) {
-					/*
-					 * Range ends in the middle of THP:
-					 * zero out the page
-					 */
-					clear_highpage(page);
-					unlock_page(page);
-					continue;
-				}
-				index += HPAGE_PMD_NR - 1;
-				i += HPAGE_PMD_NR - 1;
-			}
-
  			if (!unfalloc || !PageUptodate(page)) {
-				VM_BUG_ON_PAGE(PageTail(page), page);
-				if (page_mapping(page) == mapping) {
-					VM_BUG_ON_PAGE(PageWriteback(page), page);
-					truncate_inode_page(mapping, page);
-				} else {
+				if (page_mapping(page) != mapping) {
  					/* Page was replaced by swap: retry */
  					unlock_page(page);
  					index--;
  					break;
  				}
+				VM_BUG_ON_PAGE(PageWriteback(page), page);
+				if (shmem_punch_compound(page, start, end))
+					truncate_inode_page(mapping, page);
+				else {
+					/* Wipe the page and don't get stuck */
+					clear_highpage(page);
+					flush_dcache_page(page);
+					set_page_dirty(page);
+					if (index <
+					    round_up(start, HPAGE_PMD_NR))
+						start = index + 1;
+				}
  			}
  			unlock_page(page);
  		}
author
#9 lore raw
In reply to Kirill A. Shutemov
Quoted 63 lines · Kirill A. Shutemov
Quoted 51 lines · Hugh Dickins
Quoted 30 lines · Yang Shi
Quoted 29 lines · Yang Shi
Quoted 28 lines · Kirill A. Shutemov
Quoted 27 lines · Yang Shi
Quoted 26 lines · Kirill A. Shutemov
Quoted 25 lines · Yang Shi

Currently when truncating shmem file, if the range is partial of THP
(start or end is in the middle of THP), the pages actually will just get
cleared rather than being freed unless the range cover the whole THP.
Even though all the subpages are truncated (randomly or sequentially),
the THP may still be kept in page cache.  This might be fine for some
usecases which prefer preserving THP.

But, when doing balloon inflation in QEMU, QEMU actually does hole punch
or MADV_DONTNEED in base page size granulairty if hugetlbfs is not used.
So, when using shmem THP as memory backend QEMU inflation actually doesn't
work as expected since it doesn't free memory.  But, the inflation usecase really needs get the memory freed.  Anonymous THP will not get
freed right away too but it will be freed eventually when all subpages are
unmapped, but shmem THP would still stay in page cache.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

To protect the usecases which may prefer preserving THP, introduce a
new fallocate mode: FALLOC_FL_SPLIT_HPAGE, which means spltting THP is
preferred behavior if truncating partial THP.  This mode just makes sense to tmpfs for the time being.

Sorry, I haven't managed to set aside enough time for this until now.

First off, let me say that I firmly believe this punch-split behavior should be the standard behavior (like in my huge tmpfs implementation), and we should not need a special FALLOC_FL_SPLIT_HPAGE to do it. But I don't know if I'll be able to persuade Kirill of that.

If the caller wants to write zeroes into the file, she can do so with the write syscall: the caller has asked to punch a hole or truncate the file, and in our case, like your QEMU case, hopes that memory and memcg charge will be freed by doing so. I'll be surprised if changing the behavior to yours and mine turns out to introduce a regression, but if it does, I guess we'll then have to put it behind a sysctl or whatever.

Quoted 6 lines

IIUC the reason that it's currently implemented by clearing the hole is because split_huge_page() (unlike in older refcounting days) cannot be guaranteed to succeed. Which is unfortunate, and none of us is very keen to build a filesystem on unreliable behavior; but the failure cases appear in practice to be rare enough, that it's on balance better to give the punch-hole-truncate caller what she asked for whenever possible.

I don't have a firm position here. Maybe you are right and we should try to split pages right away.

It might be useful to consider case wider than shmem.

Quoted 7 lines

On traditional filesystem with a backing storage semantics of the same punch hole operation is somewhat different. It doesn't have explicit implications on memory footprint. It's about managing persistent storage. With shmem/tmpfs it is lumped together.

It might be nice to write down pages that can be discarded under memory pressure and leave the huge page intact until then...

Sounds like another deferred split queue. It could be an option, but our usecase needs get memory freed right away since the memory might be reused by others very soon.

Quoted 4 lines

[ I don't see a problem with your patch as long as we agree that it's desired semantics for the interface. ]