mm: use augmented rbtrees for finding unmapped areas
Subsystems (5)
cover mm: use augmented rbtrees for finding unmapped areas
Earlier this year, Rik proposed using augmented rbtrees to optimize our search for a suitable unmapped area during mmap(). This prompted my work on improving the augmented rbtree code. Rik doesn't seem to have time to follow up on his idea at this time, so I'm sending this series to revive the idea.
These changes are against v3.7-rc3. I have only converted the generic and x86_64 search implementations so far, as I'm really looking for comments on the general approach; however it shouldn't be too difficult to convert other architectures in the same way (and eventually the drivers that define their own f_op->get_unmapped_area method as well).
Patch 1 augments the VMA rbtree with a new rb_subtree_gap field, indicating the length of the largest gap immediately preceding any VMAs in a subtree.
Patch 2 adds new checks to CONFIG_DEBUG_VM_RB to verify the above information is correctly maintained.
Patch 3 rearranges the vm_area_struct layout so that rbtree searches only need data that is contained in the first cacheline (this one is from Rik's original patch series)
Patch 4 adds a generic vm_unmapped_area() search function, which allows for searching for an address space of any desired length, within [low; high[ address constraints, with any desired alignment. The generic arch_get_unmapped_area[_topdown] functions are also converted to use this.
Patch 5 converts the x86_64 arch_get_unmapped_area[_topdown] functions to use vm_unmapped_area() as well.
Patch 6 fixes cache coloring on x86_64, as suggested by Rik in his previous series.
My own feel for this series is that I'm fairly confident in the robustness of my vm_unmapped_area() implementation; however I would like to confirm that people are happy with this new interface. Also the code that figures out what constraints to pass to vm_unmapped_area() is a bit odd; I have tried to make the constraints match the behavior of the current code but it's not clear to me if that behavior makes sense in the first place.
There is also the question of performance. I remember from IRC conversations that someone (I think it was Mel ?) had found some regressions with Rik's prior proposal. My current proposal is significantly faster at updating the rbtrees; however there is still the fact that vm_unmapped_area() does not use a free area cache as the old brute-force implementation did. I don't expect that to be a problem, but I have not confirmed yet if the prior regressions are gone (and if they are still present, one would want to find out if they are introduced by rbtree maintainance in Patch 1, or by removing the free area cache as part of patches 4/5).
Michel Lespinasse (5):
mm: augment vma rbtree with rb_subtree_gap
mm: check rb_subtree_gap correctness
mm: vm_unmapped_area() lookup function
mm: use vm_unmapped_area() on x86_64 architecture
mm: fix cache coloring on x86_64
Rik van Riel (1):
mm: rearrange vm_area_struct for fewer cache misses
arch/x86/include/asm/elf.h | 6 +- arch/x86/kernel/sys_x86_64.c | 151 +++----------- arch/x86/vdso/vma.c | 2 +- include/linux/mm.h | 31 +++ include/linux/mm_types.h | 19 ++- mm/mmap.c | 450 ++++++++++++++++++++++++++++++++---------- 6 files changed, 423 insertions(+), 236 deletions(-)
Signature · 7 lines
1.7.7.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH 1/6 mm: augment vma rbtree with rb_subtree_gap
Define vma->rb_subtree_gap as the largest gap between any vma in the subtree rooted at that vma, and their predecessor. Or, for a recursive definition, vma->rb_subtree_gap is the max of:
- vma->vm_start - vma->vm_prev->vm_end
- rb_subtree_gap fields of the vmas pointed by vma->rb.rb_left and vma->rb.rb_right
This will allow get_unmapped_area_* to find a free area of the right size in O(log(N)) time, instead of potentially having to do a linear walk across all the VMAs.
Also define mm->highest_vm_end as the vm_end field of the highest vma, so that we can easily check if the following gap is suitable.
This does have the potential to make unmapping VMAs more expensive, especially for processes with very large numbers of VMAs, where the VMA rbtree can grow quite deep.
--- include/linux/mm_types.h | 9 ++++ mm/mmap.c | 114 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 114 insertions(+), 9 deletions(-)
include/linux/mm_types.h +9 -0
@@ -238,6 +238,14 @@ struct vm_area_struct {struct rb_node vm_rb;/* +* Largest free memory gap in bytes to the left of this VMA. +* Either between this VMA and vma->vm_prev, or between one of the +* VMAs below us in the VMA rbtree and its ->vm_prev. This helps +* get_unmapped_area find a free area of the right size. +*/ +unsigned long rb_subtree_gap; + +/* * For areas with an address space and backing store, * linkage into the address_space->i_mmap interval tree, or * linkage of vma in the address_space->i_mmap_nonlinear list.
@@ -322,6 +330,7 @@ struct mm_struct {unsigned long task_size;/* size of task vm space */ unsigned long cached_hole_size;/* if non-zero, the largest hole below free_area_cache */ unsigned long free_area_cache;/* first hole of size cached_hole_size or larger */ +unsigned long highest_vm_end;/* highest vma end address */ pgd_t * pgd;atomic_t mm_users;/* How many users with user space? */ atomic_t mm_count;/* How many references to "struct mm_struct" (users count as 1) */
mm/mmap.c +105 -9
187 lines not on this page · raw message
Signature · 7 lines
1.7.7.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH 2/6 mm: check rb_subtree_gap correctness
In reply to Michel LespinasseWhen CONFIG_DEBUG_VM_RB is enabled, check that rb_subtree_gap is correctly set for every vma and that mm->highest_vm_end is also correct.
Also add an explicit 'bug' variable to track if browse_rb() detected any invalid condition.
--- mm/mmap.c | 24 ++++++++++++++++-------- 1 files changed, 16 insertions(+), 8 deletions(-)
mm/mmap.c +16 -8
@@ -365,7 +365,7 @@ static void vma_rb_erase(struct vm_area_struct *vma, struct rb_root *root)#ifdef CONFIG_DEBUG_VM_RB static int browse_rb (struct rb_root *root) { -int i =0 , j; +int i =0 , j, bug =0 ;struct rb_node *nd, *pn =NULL ;unsigned long prev =0 , pend =0 ;
@@ -373,39 +373,47 @@ static int browse_rb(struct rb_root *root)struct vm_area_struct *vma; vma = rb_entry(nd, struct vm_area_struct, vm_rb);if (vma->vm_start < prev) -printk ("vm_start %lx prev %lx\n" , vma->vm_start, prev), i =-1 ; +printk ("vm_start %lx prev %lx\n" , vma->vm_start, prev), bug =1 ;if (vma->vm_start < pend) -printk ("vm_start %lx pend %lx\n" , vma->vm_start, pend); +printk ("vm_start %lx pend %lx\n" , vma->vm_start, pend), bug =1 ;if (vma->vm_start > vma->vm_end) -printk ("vm_end %lx < vm_start %lx\n" , vma->vm_end, vma->vm_start); +printk ("vm_end %lx < vm_start %lx\n" , vma->vm_end, vma->vm_start), bug =1 ; +if (vma->rb_subtree_gap !=vma_compute_subtree_gap (vma)) +printk ("free gap %lx, correct %lx\n" , + vma->rb_subtree_gap, +vma_compute_subtree_gap (vma)), bug =1 ; i++; pn = nd; prev = vma->vm_start; pend = vma->vm_end; } j =0 ; -for (nd = pn; nd; nd =rb_prev (nd)) { +for (nd = pn; nd; nd =rb_prev (nd)) j++; - }if (i != j) -printk ("backwards %d, forwards %d\n" , j, i), i =0 ; -return i; +printk ("backwards %d, forwards %d\n" , j, i), bug =1 ; +return bug ?-1 : i; }void validate_mm (struct mm_struct *mm) {int bug =0 ;int i =0 ; +unsigned long highest_address =0 ;struct vm_area_struct *vma = mm->mmap;while (vma) {struct anon_vma_chain *avc;list_for_each_entry (avc, &vma->anon_vma_chain, same_vma)anon_vma_interval_tree_verify (avc); + highest_address = vma->vm_end; vma = vma->vm_next; i++; }if (i != mm->map_count)printk ("map_count %d vm_next %d\n" , mm->map_count, i), bug =1 ; +if (highest_address != mm->highest_vm_end) +printk ("mm->highest_vm_end %lx, found %lx\n" , + mm->highest_vm_end, highest_address), bug =1 ; i =browse_rb (&mm->mm_rb);if (i != mm->map_count)printk ("map_count %d rb %d\n" , mm->map_count, i), bug =1 ;
Signature · 7 lines
1.7.7.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH 3/6 mm: rearrange vm_area_struct for fewer cache misses
In reply to Michel LespinasseFrom: Rik van Riel <[email protected]>
The kernel walks the VMA rbtree in various places, including the page fault path. However, the vm_rb node spanned two cache lines, on 64 bit systems with 64 byte cache lines (most x86 systems).
Rearrange vm_area_struct a little, so all the information we need to do a VMA tree walk is in the first cache line.
--- include/linux/mm_types.h | 12 ++++++++---- 1 files changed, 8 insertions(+), 4 deletions(-)
include/linux/mm_types.h +8 -4
@@ -224,7 +224,8 @@ struct vm_region {* library, the executable area etc). */ struct vm_area_struct { -struct mm_struct * vm_mm;/* The address space we belong to. */ +/* The first cache line has the info for VMA tree walking. */ +unsigned long vm_start;/* Our start address within vm_mm. */ unsigned long vm_end;/* The first byte after our end address within vm_mm. */
@@ -232,9 +233,6 @@ struct vm_area_struct {/* linked list of VM areas per task, sorted by address */ struct vm_area_struct *vm_next, *vm_prev; -pgprot_t vm_page_prot;/* Access permissions of this VMA. */ -unsigned long vm_flags;/* Flags, see mm.h. */ -struct rb_node vm_rb;/*
@@ -245,6 +243,12 @@ struct vm_area_struct {*/ unsigned long rb_subtree_gap; +/* Second cache line starts here. */ + +struct mm_struct * vm_mm;/* The address space we belong to. */ +pgprot_t vm_page_prot;/* Access permissions of this VMA. */ +unsigned long vm_flags;/* Flags, see mm.h. */ +/* * For areas with an address space and backing store, * linkage into the address_space->i_mmap interval tree, or
Signature · 7 lines
1.7.7.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH 4/6 mm: vm_unmapped_area() lookup function
In reply to Michel LespinasseImplement vm_unmapped_area() using the rb_subtree_gap and highest_vm_end information to look up for suitable virtual address space gaps.
struct vm_unmapped_area_info is used to define the desired allocation request:
- lowest or highest possible address matching the remaining constraints
- desired gap length
- low/high address limits that the gap must fit into
- alignment mask and offset
Also update the generic arch_get_unmapped_area[_topdown] functions to make use of vm_unmapped_area() instead of implementing a brute force search.
--- include/linux/mm.h | 31 +++++ mm/mmap.c | 312 +++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 253 insertions(+), 90 deletions(-)
include/linux/mm.h +31 -0
@@ -1456,6 +1456,37 @@ extern unsigned long vm_mmap(struct file *, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long); +struct vm_unmapped_area_info { +#define VM_UNMAPPED_AREA_TOPDOWN 1 +unsigned long flags; +unsigned long length; +unsigned long low_limit; +unsigned long high_limit; +unsigned long align_mask; +unsigned long align_offset; +}; + +extern unsigned long unmapped_area (struct vm_unmapped_area_info *info); +extern unsigned long unmapped_area_topdown (struct vm_unmapped_area_info *info); + +/* +* Search for an unmapped address range. +* +* We are looking for a range that: +* - does not intersect with any VMA; +* - is contained within the [low_limit, high_limit[ interval; +* - is at least the desired size. +* - satisfies (begin_addr & align_mask) == (align_offset & align_mask) +*/ +static inline unsigned long +vm_unmapped_area (struct vm_unmapped_area_info *info) +{ +if (!(info->flags &VM_UNMAPPED_AREA_TOPDOWN )) +return unmapped_area (info); +else +return unmapped_area_topdown (info); +} +/* truncate.c */ extern void truncate_inode_pages (struct address_space *,loff_t ); extern void truncate_inode_pages_range(struct address_space *,
mm/mmap.c +222 -90
349 lines not on this page · raw message
Signature · 7 lines
1.7.7.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH 5/6 mm: use vm_unmapped_area() on x86_64 architecture
In reply to Michel Lespinasse--- arch/x86/include/asm/elf.h | 6 +- arch/x86/kernel/sys_x86_64.c | 151 ++++++++--------------------------------- arch/x86/vdso/vma.c | 2 +- 3 files changed, 33 insertions(+), 126 deletions(-)
arch/x86/include/asm/elf.h +2 -4
@@ -354,12 +354,10 @@ static inline int mmap_is_ia32(void)return 0 ; } -/* The first two values are special, do not change. See align_addr() */ +/* Do not change the values. See get_align_mask() */ enum align_flags {ALIGN_VA_32 =BIT (0 ),ALIGN_VA_64 =BIT (1 ), -ALIGN_VDSO =BIT (2 ), -ALIGN_TOPDOWN =BIT (3 ), };struct va_alignment {
@@ -368,5 +366,5 @@ struct va_alignment { } ____cacheline_aligned;extern struct va_alignment va_align; -extern unsigned long align_addr (unsigned long ,struct file *,enum align_flags ); +extern unsigned long align_vdso_addr (unsigned long );#endif /* _ASM_X86_ELF_H */
arch/x86/kernel/sys_x86_64.c +30 -121
196 lines not on this page · raw message
arch/x86/vdso/vma.c +1 -1
@@ -141,7 +141,7 @@ static unsigned long vdso_addr(unsigned long start, unsigned len)* unaligned here as a result of stack start randomization. */ addr =PAGE_ALIGN (addr); - addr =align_addr (addr,NULL ,ALIGN_VDSO ); + addr =align_vdso_addr (addr);return addr; }
Signature · 7 lines
1.7.7.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH 6/6 mm: fix cache coloring on x86_64
In reply to Michel LespinasseFrom: Rik van Riel <[email protected]>
Fix the x86-64 cache alignment code to take pgoff into account. Use the x86 and MIPS cache alignment code as the basis for a generic cache alignment function.
The old x86 code will always align the mmap to aliasing boundaries, even if the program mmaps the file with a non-zero pgoff.
If program A mmaps the file with pgoff 0, and program B mmaps the file with pgoff 1. The old code would align the mmaps, resulting in misaligned pages:
A: 0123 B: 123
After this patch, they are aligned so the pages line up:
A: 0123
B: 123
--- arch/x86/kernel/sys_x86_64.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/sys_x86_64.c +2 -2
@@ -136,7 +136,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr, info.low_limit = begin; info.high_limit = end; info.align_mask = filp ?get_align_mask () :0 ; - info.align_offset =0 ; + info.align_offset = pgoff <<PAGE_SHIFT ;return vm_unmapped_area (&info); }
@@ -175,7 +175,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, info.low_limit =0 ;// XXX could be PAGE_SIZE ??? info.high_limit = mm->mmap_base; info.align_mask = filp ?get_align_mask () :0 ; - info.align_offset =0 ; + info.align_offset = pgoff <<PAGE_SHIFT ; addr =vm_unmapped_area (&info);if (!(addr & ~PAGE_MASK ))return addr;
Signature · 7 lines
1.7.7.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 7 lines · Michel Lespinasse
My own feel for this series is that I'm fairly confident in the robustness of my
vm_unmapped_area()implementation; however I would like to confirm that people are happy with this new interface. Also the code that figures out what constraints to pass tovm_unmapped_area()is a bit odd; I have tried to make the constraints match the behavior of the current code but it's not clear to me if that behavior makes sense in the first place.
I wanted to expand a bit on that by listing some of these behaviors I have made sure to preserve without really understanding why they are as they are:
- arch_get_unmapped_area() doesn't make use of mm->mmap_base, this value is used only when doing downwards allocations. However, many architectures including x86_64 carefully initialize this (in arch_pick_mmap_layout() ) to different values based on the up/down allocation direction. It seems that the legacy (upwards allocation) mmap_base value is irrelevant as I don't see any place using it ???
- For downwards allocations, it is not clear if the lowest valid address should be 0 or PAGE_SIZE. Existing brute-force search code will treat address 0 as valid on entering the loop, but invalid when reaching the end of the loop.
- When user passes a suggested address without the MAP_FIXED flag, the address range we validate the address against varies depending on the upwards/downwards allocation direction. This doesn't make much sense since there is no address space search taking place in this case.
- The stragegy of allocating upwards if the downwards allocation failed is a bit strange. I'm not sure what we really want; maybe we only need to extend the valid address range for the initial search ? (IIRC Rik's initial patch series got rid of this redundant search, but didn't explain why this was considered safe).
That's all I noticed, but this is really most of the remaining code left in arch_get_unmapped_area[_topdown]... and I didn't even go into architectures other than x86, where I could find some additional questionable stuff (but I don't even want to go there before we at least agree on the general principle of this patch series).
I hope with a proper understanding of the allocation strategies / constraints it might be possible to unify the remaining arch_get_unmapped_area[_topdown] code between architectures, but I'm keeping this for a later step as I'm obviously not informed enough to tackle that just yet...
Signature · 8 lines
Michel "Walken" Lespinasse A program is never fully debugged until the last user dies. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH 1/6] mm: augment vma rbtree with rb_subtree_gap
In reply to Michel LespinasseQuoted 11 lines · Michel Lespinasse
Define
vma->rb_subtree_gapas the largest gap between any vma in the subtree rooted at that vma, and their predecessor. Or, for a recursive definition,vma->rb_subtree_gapis the max of:
-vma->vm_start-vma->vm_prev->vm_end
-rb_subtree_gapfields of the vmas pointed byvma->rb.rb_leftandvma->rb.rb_rightThis will allow get_unmapped_area_* to find a free area of the right size in O(log(N)) time, instead of potentially having to do a linear walk across all the VMAs.
Quoted 8 lines
Also define
mm->highest_vm_endas thevm_endfield of the highest vma, so that we can easily check if the following gap is suitable.This does have the potential to make unmapping VMAs more expensive, especially for processes with very large numbers of VMAs, where the VMA rbtree can grow quite deep.
Signed-off-by Michel Lespinasse <[email protected]>
Signature · 7 lines
All rights reversed -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH 2/6] mm: check rb_subtree_gap correctness
In reply to Michel LespinasseQuoted 5 lines · Michel Lespinasse
When
CONFIG_DEBUG_VM_RBis enabled, check thatrb_subtree_gapis correctly set for every vma and thatmm->highest_vm_endis also correct.Also add an explicit 'bug' variable to track if
browse_rb()detected any invalid condition.
Looks somewhat familiar :)
Quoted 1 line
Signed-off-by Michel Lespinasse <[email protected]>
Signature · 7 lines
All rights reversed -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH 4/6] mm: vm_unmapped_area() lookup function
In reply to Michel LespinasseQuoted 6 lines · Michel Lespinasse
+/*
+ * Search for an unmapped address range.
+ *
+ * We are looking for a range that:
+ * - does not intersect with any VMA;
+ * - is contained within the [low_limit, high_limit[ interval;
^
bracket is the wrong way around :)
include/linux/mm.h extern unsigned long vm_mmap(struct file *, unsigned long, · line 1488
Quoted 11 lines
+ * - is at least the desired size.
+ * - satisfies (begin_addr&align_mask) == (align_offset&align_mask)
+ */include/linux/mm.h +8
-0+static inline unsigned long +vm_unmapped_area (struct vm_unmapped_area_info *info) +{ +if (!(info->flags &VM_UNMAPPED_AREA_TOPDOWN )) +return unmapped_area (info); +else +return unmapped_area_topdown (info); +}
I like how you pass all the info in the struct. That solves some of the problems I was trying to deal with when I implemented something similar.
Quoted 14 lines
mm/mmap.c +8
-0@@ -1494,6 +1494,206 @@ unacct_error:return error; } +unsigned long unmapped_area (struct vm_unmapped_area_info *info) +{ +/* +* We implement the search by looking for an rbtree node that +* immediately follows a suitable gap. That is, +* - gap_start = vma->vm_prev->vm_end <= info->high_limit - length; +* - gap_end = vma->vm_start >= info->low_limit + length; +* - gap_end - gap_start >= length
mm/mmap.c unacct_error: · line 1514
Quoted 10 lines
mm/mmap.c +10
-0+*/ + +struct mm_struct *mm = current->mm; +struct vm_area_struct *vma; +unsigned long length, low_limit, high_limit, gap_start, gap_end; + +/* Adjust search length to account for worst case alignment overhead */ + length = info->length + info->align_mask; +if (length < info->length) +return -ENOMEM ;
If we unmap a VMA that is size N and correctly aligned, then we may leave a gap of size N.
A subsequent call to mmap should be able to find that gap, and use it.
It may make sense to not add the align_mask the first time around, and see if the gap that we find already has the correct alignment.
If it does not, we can always add the align_mask, and do a second search for a hole of N + align_mask:
if (hole not aligned) {
length += info->align_mask;
goto again;
}Quoted 4 lines
mm/mmap.c +0
-0@@ -1603,53 +1777,12 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,return addr; }
...
mm/mmap.c arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, · line 1785
Quoted 6 lines
mm/mmap.c +6
-0+ info.flags =VM_UNMAPPED_AREA_TOPDOWN ; + info.length = len; + info.low_limit =0 ; + info.high_limit = mm->mmap_base; + info.align_mask =0 ; + addr =vm_unmapped_area (&info);
I believe it would be better to set low_limit to PAGE_SIZE.
Otherwise we can return 0, which is indistinguishable from a null pointer. Keeping address 0 unmapped provides the security benefit that code that can be made to address null pointers causes a crash, instead of a potential privilege escalation.
The rest of the patch looks good.
Signature · 7 lines
All rights reversed -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH 5/6] mm: use vm_unmapped_area() on x86_64 architecture
In reply to Michel LespinasseQuoted 1 line · Michel Lespinasse
Signed-off-by Michel Lespinasse <[email protected]>
The patch could use a changelog.
arch/x86/kernel/sys_x86_64.c arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, · line 175
Quoted 3 lines
arch/x86/kernel/sys_x86_64.c +3
-0+ info.flags =VM_UNMAPPED_AREA_TOPDOWN ; + info.length = len; + info.low_limit =0 ;// XXX could be PAGE_SIZE ???
Indeed.
Everything else in the patch looks good to me.
Quoted 7 lines
arch/x86/kernel/sys_x86_64.c +7
-0+ info.high_limit = mm->mmap_base; + info.align_mask = filp ?get_align_mask () :0 ; + info.align_offset =0 ; + addr =vm_unmapped_area (&info); +if (!(addr & ~PAGE_MASK )) +return addr; +VM_BUG_ON (addr != -ENOMEM );
Signature · 7 lines
All rights reversed -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH 4/6] mm: vm_unmapped_area() lookup function
In reply to Rik van RielQuoted 10 lines · Rik van Riel
Quoted 6 lines · Michel Lespinasse
+/*
+ * Search for an unmapped address range.
+ *
+ * We are looking for a range that:
+ * - does not intersect with any VMA;
+ * - is contained within the [low_limit, high_limit[ interval;bracket is the wrong way around :)
Ah, I meant to indicate the byte at high_limit is not included in the interval. I think the convention people are used to here would be [low_limit, high_limit)
Quoted 39 lines
Quoted 24 lines
mm/mmap.c +15
-0@@ -1494,6 +1494,206 @@ unacct_error:return error; } +unsigned long unmapped_area (struct vm_unmapped_area_info *info) +{ +/* +* We implement the search by looking for an rbtree node that +* immediately follows a suitable gap. That is, +* - gap_start = vma->vm_prev->vm_end <= info->high_limit - length; +* - gap_end = vma->vm_start >= info->low_limit + length; +* - gap_end - gap_start >= length +*/ + +struct mm_struct *mm = current->mm; +struct vm_area_struct *vma; +unsigned long length, low_limit, high_limit, gap_start, gap_end; + +/* Adjust search length to account for worst case alignment overhead */
mm/mmap.c unacct_error: · line 1514
Quoted 3 lines
mm/mmap.c +3
-0+ length = info->length + info->align_mask; +if (length < info->length) +return -ENOMEM ;If we unmap a VMA that is size N and correctly aligned, then we may leave a gap of size N.
A subsequent call to mmap should be able to find that gap, and use it.
It may make sense to not add the
align_maskthe first time around, and see if the gap that we find already has the correct alignment.
Quoted 8 lines
If it does not, we can always add the
align_mask, and do a second search for a hole of N +align_mask:if (hole not aligned) { length += info->align_mask; goto again;}
I guess the suggestion is OK in the sense that I can't see a case where it'd hurt. However, it still won't find all cases where we just unmapped a region of size N with the correct alignment - it could be that the first region we find has insufficient alignment, and then the search with an increased length could fail, even though there exists an aligned gap (just not the first) of the desired size. So, this is only a partial solution.
Another suggestion I thought about (but thought it may be overkill) would be to use an extra field (maybe stored in the lowest bits of rb_subtree_gap) to indicate the largest *aligned* gap preceding vmas within a subtree: if the value would be N, there would have to be an gap of size PAGE_SIZE<<N and aligned to PAGE_SIZE<<N. This still doesn't solve the general problem, but does help in the case of wanting to allocate an aligned power-of-2 sized vma.
Unfortunately, I don't think there is an efficient solution to the general problem, and the partial solutions discussed above (both yours and mine) don't seem to cover enough cases to warrant the complexity IMO...
Quoted 8 lines
Quoted 5 lines
mm/mmap.c +0
-0@@ -1603,53 +1777,12 @@ arch_get_unmapped_area_topdown(struct file *filp,const unsigned long addr0, return addr; }...
Quoted 8 lines
mm/mmap.c arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, · line 1785
Quoted 6 lines
mm/mmap.c +6
-0+ info.flags =VM_UNMAPPED_AREA_TOPDOWN ; + info.length = len; + info.low_limit =0 ; + info.high_limit = mm->mmap_base; + info.align_mask =0 ; + addr =vm_unmapped_area (&info);I believe it would be better to set
low_limittoPAGE_SIZE.
Noted. I should probably have tried that first as this is the safer option. (It's one of the things I had noted in my 0/6 email reply: the original code seems to be inconsistent here as it'd allow allocation at address 0 on the first loop iteration but fail it on further iterations)
Signature · 8 lines
Michel "Walken" Lespinasse A program is never fully debugged until the last user dies. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH 4/6] mm: vm_unmapped_area() lookup function
Quoted 11 lines · Michel Lespinasse
Quoted 1 line · Rik van Riel
On 10/31/2012 06:33 AM, Michel Lespinasse wrote:
I guess the suggestion is OK in the sense that I can't see a case where it'd hurt. However, it still won't find all cases where we just unmapped a region of size N with the correct alignment - it could be that the first region we find has insufficient alignment, and then the search with an increased length could fail, even though there exists an aligned gap (just not the first) of the desired size. So, this is only a partial solution.
Quoted 4 lines
Unfortunately, I don't think there is an efficient solution to the general problem, and the partial solutions discussed above (both yours and mine) don't seem to cover enough cases to warrant the complexity IMO...
The common case (anonymous memory) is that no alignment is required, so your solution and mine would be equivalent :)
You are right that your solution and mine are pretty much the same for the (rarer) cases where alignment is needed. Lets stick with your simpler version.
ACK to your version
Signature · 7 lines
All rights reversed -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>