Skip to content

Commit 5f490a5

Browse files
gerald-schaeferVasily Gorbik
authored andcommitted
s390/mm: fix dynamic pagetable upgrade for hugetlbfs
Commit ee71d16 ("s390/mm: make TASK_SIZE independent from the number of page table levels") changed the logic of TASK_SIZE and also removed the arch_mmap_check() implementation for s390. This combination has a subtle effect on how get_unmapped_area() for hugetlbfs pages works. It is now possible that a user process establishes a hugetlbfs mapping at an address above 4 TB, without triggering a dynamic pagetable upgrade from 3 to 4 levels. This is because hugetlbfs mappings will not use mm->get_unmapped_area, but rather file->f_op->get_unmapped_area, which currently is the generic implementation of hugetlb_get_unmapped_area() that does not know about s390 dynamic pagetable upgrades, but with the new definition of TASK_SIZE, it will now allow mappings above 4 TB. Subsequent access to such a mapped address above 4 TB will result in a page fault loop, because the CPU cannot translate such a large address with 3 pagetable levels. The fault handler will try to map in a hugepage at the address, but due to the folded pagetable logic it will end up with creating entries in the 3 level pagetable, possibly overwriting existing mappings, and then it all repeats when the access is retried. Apart from the page fault loop, this can have various nasty effects, e.g. kernel panic from one of the BUG_ON() checks in memory management code, or even data loss if an existing mapping gets overwritten. Fix this by implementing HAVE_ARCH_HUGETLB_UNMAPPED_AREA support for s390, providing an s390 version for hugetlb_get_unmapped_area() with pagetable upgrade support similar to arch_get_unmapped_area(), which will then be used instead of the generic version. Fixes: ee71d16 ("s390/mm: make TASK_SIZE independent from the number of page table levels") Cc: <stable@vger.kernel.org> # 4.12+ Signed-off-by: Gerald Schaefer <gerald.schaefer@de.ibm.com> Signed-off-by: Vasily Gorbik <gor@linux.ibm.com>
1 parent 740eaf7 commit 5f490a5

2 files changed

Lines changed: 101 additions & 1 deletion

File tree

‎arch/s390/include/asm/page.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#define ARCH_HAS_PREPARE_HUGEPAGE
3434
#define ARCH_HAS_HUGEPAGE_CLEAR_FLUSH
3535

36+
#define HAVE_ARCH_HUGETLB_UNMAPPED_AREA
37+
3638
#include <asm/setup.h>
3739
#ifndef __ASSEMBLY__
3840

‎arch/s390/mm/hugetlbpage.c‎

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
* IBM System z Huge TLB Page Support for Kernel.
44
*
5-
* Copyright IBM Corp. 2007,2016
5+
* Copyright IBM Corp. 2007,2020
66
* Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
77
*/
88

@@ -11,6 +11,9 @@
1111

1212
#include <linux/mm.h>
1313
#include <linux/hugetlb.h>
14+
#include <linux/mman.h>
15+
#include <linux/sched/mm.h>
16+
#include <linux/security.h>
1417

1518
/*
1619
* If the bit selected by single-bit bitmask "a" is set within "x", move
@@ -267,3 +270,98 @@ static __init int setup_hugepagesz(char *opt)
267270
return 1;
268271
}
269272
__setup("hugepagesz=", setup_hugepagesz);
273+
274+
static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
275+
unsigned long addr, unsigned long len,
276+
unsigned long pgoff, unsigned long flags)
277+
{
278+
struct hstate *h = hstate_file(file);
279+
struct vm_unmapped_area_info info;
280+
281+
info.flags = 0;
282+
info.length = len;
283+
info.low_limit = current->mm->mmap_base;
284+
info.high_limit = TASK_SIZE;
285+
info.align_mask = PAGE_MASK & ~huge_page_mask(h);
286+
info.align_offset = 0;
287+
return vm_unmapped_area(&info);
288+
}
289+
290+
static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
291+
unsigned long addr0, unsigned long len,
292+
unsigned long pgoff, unsigned long flags)
293+
{
294+
struct hstate *h = hstate_file(file);
295+
struct vm_unmapped_area_info info;
296+
unsigned long addr;
297+
298+
info.flags = VM_UNMAPPED_AREA_TOPDOWN;
299+
info.length = len;
300+
info.low_limit = max(PAGE_SIZE, mmap_min_addr);
301+
info.high_limit = current->mm->mmap_base;
302+
info.align_mask = PAGE_MASK & ~huge_page_mask(h);
303+
info.align_offset = 0;
304+
addr = vm_unmapped_area(&info);
305+
306+
/*
307+
* A failed mmap() very likely causes application failure,
308+
* so fall back to the bottom-up function here. This scenario
309+
* can happen with large stack limits and large mmap()
310+
* allocations.
311+
*/
312+
if (addr & ~PAGE_MASK) {
313+
VM_BUG_ON(addr != -ENOMEM);
314+
info.flags = 0;
315+
info.low_limit = TASK_UNMAPPED_BASE;
316+
info.high_limit = TASK_SIZE;
317+
addr = vm_unmapped_area(&info);
318+
}
319+
320+
return addr;
321+
}
322+
323+
unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
324+
unsigned long len, unsigned long pgoff, unsigned long flags)
325+
{
326+
struct hstate *h = hstate_file(file);
327+
struct mm_struct *mm = current->mm;
328+
struct vm_area_struct *vma;
329+
int rc;
330+
331+
if (len & ~huge_page_mask(h))
332+
return -EINVAL;
333+
if (len > TASK_SIZE - mmap_min_addr)
334+
return -ENOMEM;
335+
336+
if (flags & MAP_FIXED) {
337+
if (prepare_hugepage_range(file, addr, len))
338+
return -EINVAL;
339+
goto check_asce_limit;
340+
}
341+
342+
if (addr) {
343+
addr = ALIGN(addr, huge_page_size(h));
344+
vma = find_vma(mm, addr);
345+
if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
346+
(!vma || addr + len <= vm_start_gap(vma)))
347+
goto check_asce_limit;
348+
}
349+
350+
if (mm->get_unmapped_area == arch_get_unmapped_area)
351+
addr = hugetlb_get_unmapped_area_bottomup(file, addr, len,
352+
pgoff, flags);
353+
else
354+
addr = hugetlb_get_unmapped_area_topdown(file, addr, len,
355+
pgoff, flags);
356+
if (addr & ~PAGE_MASK)
357+
return addr;
358+
359+
check_asce_limit:
360+
if (addr + len > current->mm->context.asce_limit &&
361+
addr + len <= TASK_SIZE) {
362+
rc = crst_table_upgrade(mm, addr + len);
363+
if (rc)
364+
return (unsigned long) rc;
365+
}
366+
return addr;
367+
}

0 commit comments

Comments
 (0)