linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [stable 5.10 PATCH] mm: khugepaged: skip huge page collapse for special files
@ 2021-11-03 20:22 Yang Shi
  2021-11-04  8:19 ` Greg KH
  0 siblings, 1 reply; 2+ messages in thread
From: Yang Shi @ 2021-11-03 20:22 UTC (permalink / raw)
  To: gregkh, hughd, sunhao.th, willy, kirill.shutemov, songliubraving,
	andrea.righi, akpm
  Cc: shy828301, stable, linux-mm, linux-kernel

commit a4aeaa06d45e90f9b279f0b09de84bd00006e733 upstream.

The read-only THP for filesystems will collapse THP for files opened
readonly and mapped with VM_EXEC.  The intended usecase is to avoid TLB
misses for large text segments.  But it doesn't restrict the file types
so a THP could be collapsed for a 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 the intended usecase, so just collapse THP for
regular files in order to close the attack surface.

[shy828301@gmail.com: fix vm_file check [3]]

Link: https://lore.kernel.org/lkml/CACkBjsYwLYLRmX8GpsDpMthagWOjWWrNxqY6ZLNQVr6yx+f5vA@mail.gmail.com/ [1]
Link: https://lore.kernel.org/linux-mm/000000000000c6a82505ce284e4c@google.com/ [2]
Link: https://lkml.kernel.org/r/CAHbLzkqTW9U3VvTu1Ki5v_cLRC9gHW+znBukg_ycergE0JWj-A@mail.gmail.com [3]
Link: https://lkml.kernel.org/r/20211027195221.3825-1-shy828301@gmail.com
Fixes: 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS")
Signed-off-by: Hugh Dickins <hughd@google.com>
Signed-off-by: Yang Shi <shy828301@gmail.com>
Reported-by: Hao Sun <sunhao.th@gmail.com>
Reported-by: syzbot+aae069be1de40fb11825@syzkaller.appspotmail.com
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Song Liu <songliubraving@fb.com>
Cc: Andrea Righi <andrea.righi@canonical.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
---
 mm/khugepaged.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index ee8812578563..5c36848022de 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -443,21 +443,24 @@ static bool hugepage_vma_check(struct vm_area_struct *vma,
 	if (!transhuge_vma_enabled(vma, vm_flags))
 		return false;
 
+	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) && shmem_huge_enabled(vma)) {
-		return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
-				HPAGE_PMD_NR);
-	}
+	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 &&
 	    (vm_flags & VM_DENYWRITE)) {
-		return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
-				HPAGE_PMD_NR);
+		struct inode *inode = vma->vm_file->f_inode;
+
+		return S_ISREG(inode->i_mode);
 	}
 
 	if (!vma->anon_vma || vma->vm_ops)
-- 
2.26.2



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [stable 5.10 PATCH] mm: khugepaged: skip huge page collapse for special files
  2021-11-03 20:22 [stable 5.10 PATCH] mm: khugepaged: skip huge page collapse for special files Yang Shi
@ 2021-11-04  8:19 ` Greg KH
  0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2021-11-04  8:19 UTC (permalink / raw)
  To: Yang Shi
  Cc: hughd, sunhao.th, willy, kirill.shutemov, songliubraving,
	andrea.righi, akpm, stable, linux-mm, linux-kernel

On Wed, Nov 03, 2021 at 01:22:58PM -0700, Yang Shi wrote:
> commit a4aeaa06d45e90f9b279f0b09de84bd00006e733 upstream.
> 
> The read-only THP for filesystems will collapse THP for files opened
> readonly and mapped with VM_EXEC.  The intended usecase is to avoid TLB
> misses for large text segments.  But it doesn't restrict the file types
> so a THP could be collapsed for a 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 the intended usecase, so just collapse THP for
> regular files in order to close the attack surface.
> 
> [shy828301@gmail.com: fix vm_file check [3]]

Now queued up, thanks.

greg k-h


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-11-04  8:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-03 20:22 [stable 5.10 PATCH] mm: khugepaged: skip huge page collapse for special files Yang Shi
2021-11-04  8:19 ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox