Skip to contentSkip to conversation

mm: khugepaged: skip huge page collapse for special files

PATCH In review Yang Shi 4 participants 8 messages Oct 27, 2021

Subsystems (3)
lore
Share & tools
lore

Yang Shi

author
#0 lore raw

PATCH 1/1 mm: khugepaged: skip huge page collapse for special files

The read-only THP for filesystems would collapse THP for file opened readonly and mapped with VM_EXEC, the intended usecase is to avoid TLB miss for large text segment. But it doesn't restrict the file types so THP could be collapsed for non-regular file, for example, block device, if it is opened readonly and mapped with EXEC permission. This may cause bugs, like [1] and [2].

This is definitely not intended usecase, so just collapsing THP for regular file in order to close the attack surface.

[1] https://lore.kernel.org/lkml/CACkBjsYwLYLRmX8GpsDpMthagWOjWWrNxqY6ZLNQVr6yx+f5vA@mail.gmail.com/
[2] https://lore.kernel.org/linux-mm/[email protected]/

Fixes 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS")
Reported-by [email protected]
Cc Hao Sun <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Song Liu <[email protected]>
Cc Andrea Righi <[email protected]>

---
The patch is basically based off the proposal from Hugh
(https://lore.kernel.org/linux-mm/[email protected]/).
It seems Hugh is too busy to prepare the patch for formal submission (I didn't hear from him by pinging him a couple of times on mailing list), so I prepared the patch and added his SOB.

 mm/khugepaged.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
mm/khugepaged.c +10 -7
index 045cc579f724..e91b7271275e 100644
@@ -445,22 +445,25 @@ static bool hugepage_vma_check(struct vm_area_struct *vma,
 	if (!transhuge_vma_enabled(vma, vm_flags))
 		return false;
 
-	/* Enabled via shmem mount options or sysfs settings. */
-	if (shmem_file(vma->vm_file) && shmem_huge_enabled(vma)) {
+	if (vma->vm_file)
 		return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
 				HPAGE_PMD_NR);
-	}
+
+	/* Enabled via shmem mount options or sysfs settings. */
+	if (shmem_file(vma->vm_file))
+		return shmem_huge_enabled(vma);
 
 	/* THP settings require madvise. */
 	if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always())
 		return false;
 
-	/* Read-only file mappings need to be aligned for THP to work. */
+	/* Only regular file is valid */
 	if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && vma->vm_file &&
-	    !inode_is_open_for_write(vma->vm_file->f_inode) &&
 	    (vm_flags & VM_EXEC)) {
-		return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
-				HPAGE_PMD_NR);
+		struct inode *inode = vma->vm_file->f_inode;
+
+		return !inode_is_open_for_write(inode) &&
+			S_ISREG(inode->i_mode);
 	}
 
 	if (!vma->anon_vma || vma->vm_ops)
Signature · 1 line
2.26.2
#1 lore raw
Quoted 43 lines · Yang Shi

The read-only THP for filesystems would collapse THP for file opened readonly and mapped with VM_EXEC, the intended usecase is to avoid TLB miss for large text segment. But it doesn't restrict the file types so THP could be collapsed for non-regular file, for example, block device, if it is opened readonly and mapped with EXEC permission. This may cause bugs, like [1] and [2].

This is definitely not intended usecase, so just collapsing THP for regular file in order to close the attack surface.

[1] https://lore.kernel.org/lkml/CACkBjsYwLYLRmX8GpsDpMthagWOjWWrNxqY6ZLNQVr6yx+f5vA@mail.gmail.com/
[2] https://lore.kernel.org/linux-mm/[email protected]/

Fixes 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS")
Reported-by [email protected]
Cc Hao Sun <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Song Liu <[email protected]>
Cc Andrea Righi <[email protected]>

---
The patch is basically based off the proposal from Hugh
(https://lore.kernel.org/linux-mm/[email protected]/).
It seems Hugh is too busy to prepare the patch for formal submission (I didn't hear from him by pinging him a couple of times on mailing list), so I prepared the patch and added his SOB.

mm/khugepaged.c | 17 ++++++++++-------

1 file changed, 10 insertions(+), 7 deletions(-)
mm/khugepaged.c +0 -0
index 045cc579f724..e91b7271275e 100644

mm/khugepaged.c static bool hugepage_vma_check(struct vm_area_struct *vma, · line 450

Quoted 8 lines
mm/khugepaged.c +1 -2
@@ -445,22 +445,25 @@ static bool hugepage_vma_check(struct vm_area_struct *vma,
 	if (!transhuge_vma_enabled(vma, vm_flags))
 		return false;
 
-	/* Enabled via shmem mount options or sysfs settings. */
-	if (shmem_file(vma->vm_file) && shmem_huge_enabled(vma)) {
+	if (vma->vm_file)
 		return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
 				HPAGE_PMD_NR);

Am I misreading this? If we return here for vma->vm_file, the following logic (shmem_file(), etc.) would be skipped, no?

Thanks,
Song

Quoted 27 lines
mm/khugepaged.c +9 -5
-	}
+
+	/* Enabled via shmem mount options or sysfs settings. */
+	if (shmem_file(vma->vm_file))
+		return shmem_huge_enabled(vma);
 
 	/* THP settings require madvise. */
 	if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always())
 		return false;
 
-	/* Read-only file mappings need to be aligned for THP to work. */
+	/* Only regular file is valid */
 	if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && vma->vm_file &&
-	    !inode_is_open_for_write(vma->vm_file->f_inode) &&
 	    (vm_flags & VM_EXEC)) {
-		return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
-				HPAGE_PMD_NR);
+		struct inode *inode = vma->vm_file->f_inode;
+
+		return !inode_is_open_for_write(inode) &&
+			S_ISREG(inode->i_mode);
 	}
 
 	if (!vma->anon_vma || vma->vm_ops)
Signature · 1 line
2.26.2
author
#2 lore raw
Quoted 47 lines · Song Liu
Quoted 47 lines · Yang Shi

The read-only THP for filesystems would collapse THP for file opened readonly and mapped with VM_EXEC, the intended usecase is to avoid TLB miss for large text segment. But it doesn't restrict the file types so THP could be collapsed for non-regular file, for example, block device, if it is opened readonly and mapped with EXEC permission. This may cause bugs, like [1] and [2].

This is definitely not intended usecase, so just collapsing THP for regular file in order to close the attack surface.

[1] https://lore.kernel.org/lkml/CACkBjsYwLYLRmX8GpsDpMthagWOjWWrNxqY6ZLNQVr6yx+f5vA@mail.gmail.com/
[2] https://lore.kernel.org/linux-mm/[email protected]/

Fixes 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS")
Reported-by [email protected]
Cc Hao Sun <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Song Liu <[email protected]>
Cc Andrea Righi <[email protected]>

---
The patch is basically based off the proposal from Hugh
(https://lore.kernel.org/linux-mm/[email protected]/).
It seems Hugh is too busy to prepare the patch for formal submission (I didn't hear from him by pinging him a couple of times on mailing list), so I prepared the patch and added his SOB.

mm/khugepaged.c | 17 ++++++++++-------

1 file changed, 10 insertions(+), 7 deletions(-)
mm/khugepaged.c +0 -0
index 045cc579f724..e91b7271275e 100644
@@ -445,22 +445,25 @@ static bool hugepage_vma_check(struct vm_area_struct *vma,
      if (!transhuge_vma_enabled(vma, vm_flags))
              return false;
 
Quoted 8 lines

mm/khugepaged.c static bool hugepage_vma_check(struct vm_area_struct *vma, · line 450

Quoted 5 lines
mm/khugepaged.c +1 -2
-     /* Enabled via shmem mount options or sysfs settings. */
-     if (shmem_file(vma->vm_file) && shmem_huge_enabled(vma)) {
+     if (vma->vm_file)
              return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
                              HPAGE_PMD_NR);

Am I misreading this? If we return here for vma->vm_file, the following logic (shmem_file(), etc.) would be skipped, no?

Oh, yes, you are right. My mistake.

Andrew,

Could you please apply the below fix?

mm/khugepaged.c +3 -3
index e91b7271275e..26f1798c88d2 100644
@@ -445,9 +445,9 @@ static bool hugepage_vma_check(struct vm_area_struct *vma,
        if (!transhuge_vma_enabled(vma, vm_flags))
                return false;
 
-       if (vma->vm_file)
-               return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
-                               HPAGE_PMD_NR);
+       if (vma->vm_file && !IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) -
+                               vma->vm_pgoff, HPAGE_PMD_NR))
+               return false;
 
        /* Enabled via shmem mount options or sysfs settings. */
        if (shmem_file(vma->vm_file))
Quoted 32 lines

Thanks,
Song

Quoted 27 lines
mm/khugepaged.c +9 -6
-     }
+
+     /* Enabled via shmem mount options or sysfs settings. */
+     if (shmem_file(vma->vm_file))
+             return shmem_huge_enabled(vma);
 
      /* THP settings require madvise. */
      if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always())
              return false;
 
-     /* Read-only file mappings need to be aligned for THP to work. */
+     /* Only regular file is valid */
      if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && vma->vm_file &&
-         !inode_is_open_for_write(vma->vm_file->f_inode) &&
          (vm_flags & VM_EXEC)) {
-             return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
-                             HPAGE_PMD_NR);
+             struct inode *inode = vma->vm_file->f_inode;
+
+             return !inode_is_open_for_write(inode) &&
+                     S_ISREG(inode->i_mode);
      }
 
      if (!vma->anon_vma || vma->vm_ops)
--

2.26.2

#5 lore raw
In reply to Yang Shi
Quoted 5 lines · Yang Shi

+++ b/mm/khugepaged.c

mm/khugepaged.c +0 -0
@@ -445,22 +445,25 @@ static bool hugepage_vma_check(struct vm_area_struct *vma,
 	if (!transhuge_vma_enabled(vma, vm_flags))
 		return false;
 

mm/khugepaged.c static bool hugepage_vma_check(struct vm_area_struct *vma, · line 454

Quoted 10 lines
mm/khugepaged.c +5 -3
-	/* Enabled via shmem mount options or sysfs settings. */
-	if (shmem_file(vma->vm_file) && shmem_huge_enabled(vma)) {
+	if (vma->vm_file)
 		return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
 				HPAGE_PMD_NR);
-	}
+
+	/* Enabled via shmem mount options or sysfs settings. */
+	if (shmem_file(vma->vm_file))
+		return shmem_huge_enabled(vma);

This doesn't make sense to me. if vma->vm_file, we already returned, so this is dead code.

Quoted 21 lines
mm/khugepaged.c +5 -4
 	/* THP settings require madvise. */
 	if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always())
 		return false;
 
-	/* Read-only file mappings need to be aligned for THP to work. */
+	/* Only regular file is valid */
 	if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && vma->vm_file &&
-	    !inode_is_open_for_write(vma->vm_file->f_inode) &&
 	    (vm_flags & VM_EXEC)) {
-		return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
-				HPAGE_PMD_NR);
+		struct inode *inode = vma->vm_file->f_inode;
+
+		return !inode_is_open_for_write(inode) &&
+			S_ISREG(inode->i_mode);
 	}
 
 	if (!vma->anon_vma || vma->vm_ops)
Signature · 1 line
2.26.2
maintainer
#3 lore raw
In reply to Yang Shi
Quoted 15 lines · On Wed, 27 Oct 2021 13:44:37 -0700 Yang Shi <[email protected]> wrote:
Quoted 6 lines
Quoted 6 lines
mm/khugepaged.c +0 -0
@@ -445,22 +445,25 @@ static bool hugepage_vma_check(struct vm_area_struct *vma,
      if (!transhuge_vma_enabled(vma, vm_flags))
              return false;
 
Quoted 8 lines

mm/khugepaged.c static bool hugepage_vma_check(struct vm_area_struct *vma, · line 450

Quoted 5 lines
mm/khugepaged.c +1 -2
-     /* Enabled via shmem mount options or sysfs settings. */
-     if (shmem_file(vma->vm_file) && shmem_huge_enabled(vma)) {
+     if (vma->vm_file)
              return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
                              HPAGE_PMD_NR);

Am I misreading this? If we return here for vma->vm_file, the following logic (shmem_file(), etc.) would be skipped, no?

Quoted 5 lines

Oh, yes, you are right. My mistake.

Andrew,

Could you please apply the below fix?

um, how well tested are these changes?

author
#6 lore raw
In reply to Matthew Wilcox
Quoted 7 lines · Matthew Wilcox
Quoted 5 lines · Yang Shi

+++ b/mm/khugepaged.c

mm/khugepaged.c +0 -0
@@ -445,22 +445,25 @@ static bool hugepage_vma_check(struct vm_area_struct *vma,
      if (!transhuge_vma_enabled(vma, vm_flags))
              return false;
 
Quoted 13 lines

mm/khugepaged.c static bool hugepage_vma_check(struct vm_area_struct *vma, · line 454

Quoted 10 lines
mm/khugepaged.c +5 -3
-     /* Enabled via shmem mount options or sysfs settings. */
-     if (shmem_file(vma->vm_file) && shmem_huge_enabled(vma)) {
+     if (vma->vm_file)
              return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
                              HPAGE_PMD_NR);
-     }
+
+     /* Enabled via shmem mount options or sysfs settings. */
+     if (shmem_file(vma->vm_file))
+             return shmem_huge_enabled(vma);

This doesn't make sense to me. if vma->vm_file, we already returned, so this is dead code.

Yes, Song mentioned the same thing. Fixed by an incremental patch.

Quoted 22 lines
Quoted 22 lines
mm/khugepaged.c +5 -5
      /* THP settings require madvise. */
      if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always())
              return false;
 
-     /* Read-only file mappings need to be aligned for THP to work. */
+     /* Only regular file is valid */
      if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && vma->vm_file &&
-         !inode_is_open_for_write(vma->vm_file->f_inode) &&
          (vm_flags & VM_EXEC)) {
-             return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
-                             HPAGE_PMD_NR);
+             struct inode *inode = vma->vm_file->f_inode;
+
+             return !inode_is_open_for_write(inode) &&
+                     S_ISREG(inode->i_mode);
      }
 
      if (!vma->anon_vma || vma->vm_ops)
--

2.26.2

#7 lore raw
Quoted 4 lines · Yang Shi
Quoted 2 lines

This doesn't make sense to me. if vma->vm_file, we already returned, so this is dead code.

Yes, Song mentioned the same thing. Fixed by an incremental patch.

I saw that after I sent this. My email sometimes arrives slowly.

author
#4 lore raw
In reply to Andrew Morton
Quoted 18 lines · Andrew Morton
Quoted 15 lines · On Wed, 27 Oct 2021 13:44:37 -0700 Yang Shi <[email protected]> wrote:
Quoted 14 lines

mm/khugepaged.c static bool hugepage_vma_check(struct vm_area_struct *vma, · line 450

Quoted 11 lines
mm/khugepaged.c +1 -2
@@ -445,22 +445,25 @@ static bool hugepage_vma_check(struct vm_area_struct *vma,
      if (!transhuge_vma_enabled(vma, vm_flags))
              return false;
 
-     /* Enabled via shmem mount options or sysfs settings. */
-     if (shmem_file(vma->vm_file) && shmem_huge_enabled(vma)) {
+     if (vma->vm_file)
              return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
                              HPAGE_PMD_NR);

Am I misreading this? If we return here for vma->vm_file, the following logic (shmem_file(), etc.) would be skipped, no?

Quoted 7 lines
Quoted 5 lines

Oh, yes, you are right. My mistake.

Andrew,

Could you please apply the below fix?

um, how well tested are these changes?

I has this fix on my test machine, but somehow forgot to fold it into the original patch. The whole fix was tested by opening /dev/nullb0 readonly and mapping with PROT_EXEC, the THP was not collapsed anymore.