Skip to content

Commit

Permalink
From patchwork series 421443
Browse files Browse the repository at this point in the history
  • Loading branch information
Fox Snowpatch committed Aug 29, 2024
1 parent ddf9a4c commit 4896269
Show file tree
Hide file tree
Showing 23 changed files with 120 additions and 0 deletions.
2 changes: 2 additions & 0 deletions arch/alpha/kernel/osf_sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,8 @@ arch_get_unmapped_area_1(unsigned long addr, unsigned long len,
info.length = len;
info.low_limit = addr;
info.high_limit = limit;
info.hint = addr;
info.mmap_flags = flags;
return vm_unmapped_area(&info);
}

Expand Down
3 changes: 3 additions & 0 deletions arch/arc/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,
struct vm_area_struct *vma;
struct vm_unmapped_area_info info = {};

info.hint = addr;
info.mmap_flags = flags;

/*
* We enforce the MAP_FIXED case.
*/
Expand Down
7 changes: 7 additions & 0 deletions arch/arm/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,
int aliasing = cache_is_vipt_aliasing();
struct vm_unmapped_area_info info = {};

info.hint = addr;
info.mmap_flags = flags;

/*
* We only need to do colour alignment if either the I or D
* caches alias.
Expand All @@ -56,6 +59,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,
if (len > TASK_SIZE)
return -ENOMEM;


if (addr) {
if (do_align)
addr = COLOUR_ALIGN(addr, pgoff);
Expand Down Expand Up @@ -88,6 +92,9 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
int aliasing = cache_is_vipt_aliasing();
struct vm_unmapped_area_info info = {};

info.hint = addr;
info.mmap_flags = flags;

/*
* We only need to do colour alignment if either the I or D
* caches alias.
Expand Down
3 changes: 3 additions & 0 deletions arch/csky/abiv1/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,
.align_offset = pgoff << PAGE_SHIFT
};

info.hint = addr;
info.mmap_flags = flags;

/*
* We only need to do colour alignment if either the I or D
* caches alias.
Expand Down
3 changes: 3 additions & 0 deletions arch/loongarch/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ static unsigned long arch_get_unmapped_area_common(struct file *filp,
int do_color_align;
struct vm_unmapped_area_info info = {};

info.hint = addr;
info.mmap_flags = flags;

if (unlikely(len > TASK_SIZE))
return -ENOMEM;

Expand Down
3 changes: 3 additions & 0 deletions arch/mips/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ static unsigned long arch_get_unmapped_area_common(struct file *filp,
int do_color_align;
struct vm_unmapped_area_info info = {};

info.hint = addr;
info.mmap_flags = flags;

if (unlikely(len > TASK_SIZE))
return -ENOMEM;

Expand Down
3 changes: 3 additions & 0 deletions arch/parisc/kernel/sys_parisc.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ static unsigned long arch_get_unmapped_area_common(struct file *filp,
.length = len
};

info.hint = addr;
info.mmap_flags = flags;

if (unlikely(len > TASK_SIZE))
return -ENOMEM;

Expand Down
7 changes: 7 additions & 0 deletions arch/powerpc/mm/book3s64/slice.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ static unsigned long slice_find_area_bottomup(struct mm_struct *mm,
.length = len,
.align_mask = PAGE_MASK & ((1ul << pshift) - 1),
};

info.hint = addr;
info.mmap_flags = flags;

Check failure on line 291 in arch/powerpc/mm/book3s64/slice.c

View workflow job for this annotation

GitHub Actions / kernel (g5_defconfig, fedora-40, /linux/arch/powerpc/configs/g5-qemu.config)

'flags' undeclared (first use in this function)

Check failure on line 291 in arch/powerpc/mm/book3s64/slice.c

View workflow job for this annotation

GitHub Actions / kernel (g5_defconfig, korg-5.5.0, /linux/arch/powerpc/configs/g5-qemu.config)

'flags' undeclared (first use in this function)

Check failure on line 291 in arch/powerpc/mm/book3s64/slice.c

View workflow job for this annotation

GitHub Actions / kernel (ppc64le, ppc64le, fedora-40)

use of undeclared identifier 'flags'

Check failure on line 291 in arch/powerpc/mm/book3s64/slice.c

View workflow job for this annotation

GitHub Actions / kernel (ppc64, fedora-40, ppc64)

use of undeclared identifier 'flags'

/*
* Check till the allow max value for this mmap request
*/
Expand Down Expand Up @@ -331,6 +335,9 @@ static unsigned long slice_find_area_topdown(struct mm_struct *mm,
};
unsigned long min_addr = max(PAGE_SIZE, mmap_min_addr);

info.hint = addr;
info.mmap_flags = flags;

Check failure on line 339 in arch/powerpc/mm/book3s64/slice.c

View workflow job for this annotation

GitHub Actions / kernel (g5_defconfig, fedora-40, /linux/arch/powerpc/configs/g5-qemu.config)

'flags' undeclared (first use in this function)

Check failure on line 339 in arch/powerpc/mm/book3s64/slice.c

View workflow job for this annotation

GitHub Actions / kernel (g5_defconfig, korg-5.5.0, /linux/arch/powerpc/configs/g5-qemu.config)

'flags' undeclared (first use in this function)

Check failure on line 339 in arch/powerpc/mm/book3s64/slice.c

View workflow job for this annotation

GitHub Actions / kernel (ppc64le, ppc64le, fedora-40)

use of undeclared identifier 'flags'

Check failure on line 339 in arch/powerpc/mm/book3s64/slice.c

View workflow job for this annotation

GitHub Actions / kernel (ppc64, fedora-40, ppc64)

use of undeclared identifier 'flags'

/*
* If we are trying to allocate above DEFAULT_MAP_WINDOW
* Add the different to the mmap_base.
Expand Down
4 changes: 4 additions & 0 deletions arch/s390/mm/hugetlbpage.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
info.low_limit = current->mm->mmap_base;
info.high_limit = TASK_SIZE;
info.align_mask = PAGE_MASK & ~huge_page_mask(h);
info.hint = addr;
info.mmap_flags = flags;
return vm_unmapped_area(&info);
}

Expand All @@ -270,6 +272,8 @@ static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
info.low_limit = PAGE_SIZE;
info.high_limit = current->mm->mmap_base;
info.align_mask = PAGE_MASK & ~huge_page_mask(h);
info.hint = addr;
info.mmap_flags = flags;
addr = vm_unmapped_area(&info);

/*
Expand Down
6 changes: 6 additions & 0 deletions arch/s390/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
struct vm_area_struct *vma;
struct vm_unmapped_area_info info = {};

info.hint = addr;
info.mmap_flags = flags;

if (len > TASK_SIZE - mmap_min_addr)
return -ENOMEM;

Expand Down Expand Up @@ -123,6 +126,9 @@ unsigned long arch_get_unmapped_area_topdown(struct file *filp, unsigned long ad
struct mm_struct *mm = current->mm;
struct vm_unmapped_area_info info = {};

info.hint = addr;
info.mmap_flags = flags;

/* requested length too big for entire address space */
if (len > TASK_SIZE - mmap_min_addr)
return -ENOMEM;
Expand Down
6 changes: 6 additions & 0 deletions arch/sh/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
int do_colour_align;
struct vm_unmapped_area_info info = {};

info.hint = addr;
info.mmap_flags = flags;

if (flags & MAP_FIXED) {
/* We do not accept a shared mapping if it would violate
* cache aliasing constraints.
Expand Down Expand Up @@ -107,6 +110,9 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
int do_colour_align;
struct vm_unmapped_area_info info = {};

info.hint = addr;
info.mmap_flags = flags;

if (flags & MAP_FIXED) {
/* We do not accept a shared mapping if it would violate
* cache aliasing constraints.
Expand Down
3 changes: 3 additions & 0 deletions arch/sparc/kernel/sys_sparc_32.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsi
{
struct vm_unmapped_area_info info = {};

info.hint = addr;
info.mmap_flags = flags;

if (flags & MAP_FIXED) {
/* We do not accept a shared mapping if it would violate
* cache aliasing constraints.
Expand Down
6 changes: 6 additions & 0 deletions arch/sparc/kernel/sys_sparc_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsi
int do_color_align;
struct vm_unmapped_area_info info = {};

info.hint = addr;
info.mmap_flags = flags;

if (flags & MAP_FIXED) {
/* We do not accept a shared mapping if it would violate
* cache aliasing constraints.
Expand Down Expand Up @@ -155,6 +158,9 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
int do_color_align;
struct vm_unmapped_area_info info = {};

info.hint = addr;
info.mmap_flags = flags;

/* This should only ever run for 32-bit processes. */
BUG_ON(!test_thread_flag(TIF_32BIT));

Expand Down
4 changes: 4 additions & 0 deletions arch/sparc/mm/hugetlbpage.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *filp,
info.low_limit = TASK_UNMAPPED_BASE;
info.high_limit = min(task_size, VA_EXCLUDE_START);
info.align_mask = PAGE_MASK & ~huge_page_mask(h);
info.hint = addr;
info.mmap_flags = flags;
addr = vm_unmapped_area(&info);

if ((addr & ~PAGE_MASK) && task_size > VA_EXCLUDE_END) {
Expand Down Expand Up @@ -71,6 +73,8 @@ hugetlb_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
info.low_limit = PAGE_SIZE;
info.high_limit = mm->mmap_base;
info.align_mask = PAGE_MASK & ~huge_page_mask(h);
info.hint = addr;
info.mmap_flags = flags;
addr = vm_unmapped_area(&info);

/*
Expand Down
6 changes: 6 additions & 0 deletions arch/x86/kernel/sys_x86_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ arch_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, unsigned l
struct vm_unmapped_area_info info = {};
unsigned long begin, end;

info.hint = addr;
info.mmap_flags = flags;

if (flags & MAP_FIXED)
return addr;

Expand Down Expand Up @@ -167,6 +170,9 @@ arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long addr0,
unsigned long addr = addr0;
struct vm_unmapped_area_info info = {};

info.hint = addr;
info.mmap_flags = flags;

/* requested length too big for entire address space */
if (len > TASK_SIZE)
return -ENOMEM;
Expand Down
4 changes: 4 additions & 0 deletions arch/x86/mm/hugetlbpage.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,

info.length = len;
info.low_limit = get_mmap_base(1);
info.hint = addr;
info.mmap_flags = flags;

/*
* If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area
Expand All @@ -52,6 +54,8 @@ static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
info.length = len;
info.low_limit = PAGE_SIZE;
info.high_limit = get_mmap_base(0);
info.hint = addr;
info.mmap_flags = flags;

/*
* If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area
Expand Down
4 changes: 4 additions & 0 deletions fs/hugetlbfs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ hugetlb_get_unmapped_area_bottomup(struct file *file, unsigned long addr,
info.low_limit = current->mm->mmap_base;
info.high_limit = arch_get_mmap_end(addr, len, flags);
info.align_mask = PAGE_MASK & ~huge_page_mask(h);
info.hint = addr;
info.mmap_flags = flags;
return vm_unmapped_area(&info);
}

Expand All @@ -197,6 +199,8 @@ hugetlb_get_unmapped_area_topdown(struct file *file, unsigned long addr,
info.low_limit = PAGE_SIZE;
info.high_limit = arch_get_mmap_base(addr, current->mm->mmap_base);
info.align_mask = PAGE_MASK & ~huge_page_mask(h);
info.hint = addr;
info.mmap_flags = flags;
addr = vm_unmapped_area(&info);

/*
Expand Down
2 changes: 2 additions & 0 deletions include/linux/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3434,6 +3434,8 @@ struct vm_unmapped_area_info {
unsigned long align_mask;
unsigned long align_offset;
unsigned long start_gap;
unsigned long hint;
unsigned long mmap_flags;
};

extern unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info);
Expand Down
1 change: 1 addition & 0 deletions include/uapi/asm-generic/mman-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#define MAP_UNINITIALIZED 0x4000000 /* For anonymous mmap, memory could be
* uninitialized */
#define MAP_BELOW_HINT 0x8000000 /* give out address that is below (inclusive) hint address */

/*
* Flags for mlock
Expand Down
9 changes: 9 additions & 0 deletions mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,9 @@ unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info)
{
unsigned long addr;

if (info->hint != 0 && info->mmap_flags & MAP_BELOW_HINT)
info->high_limit = MIN(info->high_limit, info->hint + info->length);

if (info->flags & VM_UNMAPPED_AREA_TOPDOWN)
addr = unmapped_area_topdown(info);
else
Expand Down Expand Up @@ -1796,6 +1799,9 @@ generic_get_unmapped_area(struct file *filp, unsigned long addr,
struct vm_unmapped_area_info info = {};
const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags);

info.hint = addr;
info.mmap_flags = flags;

if (len > mmap_end - mmap_min_addr)
return -ENOMEM;

Expand Down Expand Up @@ -1841,6 +1847,9 @@ generic_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
struct vm_unmapped_area_info info = {};
const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags);

info.hint = addr;
info.mmap_flags = flags;

/* requested length too big for entire address space */
if (len > mmap_end - mmap_min_addr)
return -ENOMEM;
Expand Down
1 change: 1 addition & 0 deletions tools/include/uapi/asm-generic/mman-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#define MAP_UNINITIALIZED 0x4000000 /* For anonymous mmap, memory could be
* uninitialized */
#define MAP_BELOW_HINT 0x8000000 /* give out address that is below (inclusive) hint address */

/*
* Flags for mlock
Expand Down
1 change: 1 addition & 0 deletions tools/testing/selftests/mm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ TEST_GEN_FILES += hugepage-shm
TEST_GEN_FILES += hugepage-vmemmap
TEST_GEN_FILES += khugepaged
TEST_GEN_FILES += madv_populate
TEST_GEN_FILES += map_below_hint
TEST_GEN_FILES += map_fixed_noreplace
TEST_GEN_FILES += map_hugetlb
TEST_GEN_FILES += map_populate
Expand Down
32 changes: 32 additions & 0 deletions tools/testing/selftests/mm/map_below_hint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Test the MAP_BELOW_HINT mmap flag.
*/
#include <sys/mman.h>
#include <errno.h>
#include "../kselftest.h"

#define ADDR (1 << 20)
#define LENGTH (ADDR / 10000)

#define MAP_BELOW_HINT 0x8000000 /* Not defined in all libc */

/*
* Map memory with MAP_BELOW_HINT until no memory left. Ensure that all returned
* addresses are below the hint.
*/
int main(int argc, char **argv)
{
void *addr;

do {
addr = mmap((void *)ADDR, LENGTH, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_BELOW_HINT, -1, 0);
} while (addr != MAP_FAILED && (unsigned long)addr <= ADDR);

if (errno == ENOMEM)
ksft_test_result_pass("MAP_BELOW_HINT works\n");
else
ksft_test_result_fail("mmap returned address above hint with MAP_BELOW_HINT with error: %s\n",
strerror(errno));
}

0 comments on commit 4896269

Please sign in to comment.