[PATCH 2/2] PMFS: Convert to using vm_unmapped_area
Vishal Verma
vishal.l.verma at linux.intel.com
Mon Oct 21 18:14:19 EDT 2013
We used to do a manual search for unmapped VMAs.
This method has been depracated.
Signed-off-by: Vishal Verma <vishal.l.verma at linux.intel.com>
---
arch/x86/kernel/sys_x86_64.c | 150 ------------------------------------------
fs/pmfs/file.c | 28 ++++-----
mm/mmap.c | 4 +
3 files changed, 16 insertions(+), 166 deletions(-)
diff --git a/arch/x86/kernel/sys_x86_64.c b/arch/x86/kernel/sys_x86_64.c
index 69e7eaa..30277e2 100644
--- a/arch/x86/kernel/sys_x86_64.c
+++ b/arch/x86/kernel/sys_x86_64.c
@@ -15,7 +15,6 @@
#include <linux/random.h>
#include <linux/uaccess.h>
#include <linux/elf.h>
-#include <linux/export.h>
#include <asm/ia32.h>
#include <asm/syscalls.h>
@@ -191,152 +190,3 @@ bottomup:
*/
return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
}
-
-
-static unsigned long arch_get_unmapped_area_bottomup_sz(struct file *file,
- unsigned long addr, unsigned long len, unsigned long align_size,
- unsigned long pgoff, unsigned long flags)
-{
- struct mm_struct *mm = current->mm;
- struct vm_area_struct *vma;
- unsigned long start_addr;
-
- if (len > mm->cached_hole_size) {
- start_addr = mm->free_area_cache;
- } else {
- start_addr = TASK_UNMAPPED_BASE;
- mm->cached_hole_size = 0;
- }
-
-full_search:
- addr = ALIGN(start_addr, align_size);
-
- for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
- /* At this point: (!vma || addr < vma->vm_end). */
- if (TASK_SIZE - len < addr) {
- /*
- * Start a new search - just in case we missed
- * some holes.
- */
- if (start_addr != TASK_UNMAPPED_BASE) {
- start_addr = TASK_UNMAPPED_BASE;
- mm->cached_hole_size = 0;
- goto full_search;
- }
- return -ENOMEM;
- }
- if (!vma || addr + len <= vma->vm_start) {
- mm->free_area_cache = addr + len;
- return addr;
- }
- if (addr + mm->cached_hole_size < vma->vm_start)
- mm->cached_hole_size = vma->vm_start - addr;
- addr = ALIGN(vma->vm_end, align_size);
- }
-}
-
-static unsigned long arch_get_unmapped_area_topdown_sz(struct file *file,
- unsigned long addr0, unsigned long len, unsigned long align_size,
- unsigned long pgoff, unsigned long flags)
-{
- struct mm_struct *mm = current->mm;
- struct vm_area_struct *vma, *prev_vma;
- unsigned long base = mm->mmap_base, addr = addr0;
- unsigned long largest_hole = mm->cached_hole_size;
- unsigned long align_mask = ~(align_size - 1);
- int first_time = 1;
-
- /* don't allow allocations above current base */
- if (mm->free_area_cache > base)
- mm->free_area_cache = base;
-
- if (len <= largest_hole) {
- largest_hole = 0;
- mm->free_area_cache = base;
- }
-try_again:
- /* make sure it can fit in the remaining address space */
- if (mm->free_area_cache < len)
- goto fail;
-
- /* either no address requested or can't fit in requested address hole */
- addr = (mm->free_area_cache - len) & align_mask;
- do {
- /*
- * Lookup failure means no vma is above this address,
- * i.e. return with success:
- */
- vma = find_vma(mm, addr);
- if (!vma)
- return addr;
-
- /*
- * new region fits between prev_vma->vm_end and
- * vma->vm_start, use it:
- */
- prev_vma = vma->vm_prev;
- if (addr + len <= vma->vm_start &&
- (!prev_vma || (addr >= prev_vma->vm_end))) {
- /* remember the address as a hint for next time */
- mm->cached_hole_size = largest_hole;
- return (mm->free_area_cache = addr);
- } else {
- /* pull free_area_cache down to the first hole */
- if (mm->free_area_cache == vma->vm_end) {
- mm->free_area_cache = vma->vm_start;
- mm->cached_hole_size = largest_hole;
- }
- }
-
- /* remember the largest hole we saw so far */
- if (addr + largest_hole < vma->vm_start)
- largest_hole = vma->vm_start - addr;
-
- /* try just below the current vma->vm_start */
- addr = (vma->vm_start - len) & align_mask;
- } while (len <= vma->vm_start);
-
-fail:
- /*
- * if hint left us with no space for the requested
- * mapping then try again:
- */
- if (first_time) {
- mm->free_area_cache = base;
- largest_hole = 0;
- first_time = 0;
- goto try_again;
- }
- /*
- * A failed mmap() very likely causes application failure,
- * so fall back to the bottom-up function here. This scenario
- * can happen with large stack limits and large mmap()
- * allocations.
- */
- mm->free_area_cache = TASK_UNMAPPED_BASE;
- mm->cached_hole_size = ~0UL;
- addr = arch_get_unmapped_area_bottomup_sz(file, addr0, len, align_size,
- pgoff, flags);
-
- /*
- * Restore the topdown base:
- */
- mm->free_area_cache = base;
- mm->cached_hole_size = ~0UL;
-
- return addr;
-}
-
-unsigned long arch_get_unmapped_area_sz(struct file *file,
- unsigned long addr, unsigned long len, unsigned long align_size,
- unsigned long pgoff, unsigned long flags)
-{
- struct mm_struct *mm = current->mm;
- if (mm->get_unmapped_area == arch_get_unmapped_area)
- return arch_get_unmapped_area_bottomup_sz(file, addr, len, align_size,
- pgoff, flags);
- return arch_get_unmapped_area_topdown_sz(file, addr, len, align_size,
- pgoff, flags);
-}
-EXPORT_SYMBOL(arch_get_unmapped_area_sz);
-
diff --git a/fs/pmfs/file.c b/fs/pmfs/file.c
index 36bdc1d..23eb9cf 100644
--- a/fs/pmfs/file.c
+++ b/fs/pmfs/file.c
@@ -258,10 +258,6 @@ static int pmfs_flush(struct file *file, fl_owner_t id)
return ret;
}
-extern unsigned long arch_get_unmapped_area_sz(struct file *file,
- unsigned long addr0, unsigned long len, unsigned long align_size,
- unsigned long pgoff, unsigned long flags);
-
static unsigned long
pmfs_get_unmapped_area(struct file *file, unsigned long addr,
unsigned long len, unsigned long pgoff,
@@ -272,6 +268,7 @@ pmfs_get_unmapped_area(struct file *file, unsigned long addr,
struct mm_struct *mm = current->mm;
struct inode *inode = file->f_mapping->host;
struct pmfs_inode *pi = pmfs_get_inode(inode->i_sb, inode->i_ino);
+ struct vm_unmapped_area_info info;
if (len > TASK_SIZE)
return -ENOMEM;
@@ -300,18 +297,17 @@ pmfs_get_unmapped_area(struct file *file, unsigned long addr,
return addr;
}
- return arch_get_unmapped_area_sz(file, addr, len, align_size, pgoff,
- flags);
-#if 0
- if (mm->get_unmapped_area == arch_get_unmapped_area)
- return pmfs_get_unmapped_area_bottomup(file, addr, len,
- align_size, pgoff,
- flags);
- else
- return pmfs_get_unmapped_area_topdown(file, addr, len,
- align_size, pgoff,
- flags);
-#endif
+ /*
+ * FIXME: Using the following values for low_limit and high_limit
+ * implicitly disables ASLR. Awaiting a better way to have this fixed.
+ */
+ info.flags = 0;
+ info.length = len;
+ info.low_limit = TASK_UNMAPPED_BASE;
+ info.high_limit = TASK_SIZE;
+ info.align_mask = align_size - 1;
+ info.align_offset = 0;
+ return vm_unmapped_area(&info);
}
const struct file_operations pmfs_xip_file_operations = {
diff --git a/mm/mmap.c b/mm/mmap.c
index a0d8672..2bdcd55 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1737,6 +1737,8 @@ found:
return gap_start;
}
+EXPORT_SYMBOL(unmapped_area);
+
unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
{
struct mm_struct *mm = current->mm;
@@ -1835,6 +1837,8 @@ found_highest:
return gap_end;
}
+EXPORT_SYMBOL(unmapped_area_topdown);
+
/* Get an address range which is currently unmapped.
* For shmat() with addr=0.
*
--
1.7.0.4
More information about the Linux-pmfs
mailing list