mm/vmalloc: rework vmap_area_lock
With the new allocation approach introduced in the 5.2 kernel, it becomes possible to get rid of one global spinlock. By doing that we can further improve the KVA from the performance point of view. Basically we can have two independent locks, one for allocation part and another one for deallocation, because of two different entities: "free data structures" and "busy data structures". As a result, allocation/deallocation operations can still interfere between each other in case of running simultaneously on different CPUs, it means there is still dependency, but with two locks it becomes lower. Summarizing: - it reduces the high lock contention - it allows to perform operations on "free" and "busy" trees in parallel on different CPUs. Please note it does not solve scalability issue. Test results: In order to evaluate this patch, we can run "vmalloc test driver" to see how many CPU cycles it takes to complete all test cases running sequentially. All online CPUs run it so it will cause a high lock contention. HiKey 960, ARM64, 8xCPUs, big.LITTLE: <snip> sudo ./test_vmalloc.sh sequential_test_order=1 <snip> <default> [ 390.950557] All test took CPU0=457126382 cycles [ 391.046690] All test took CPU1=454763452 cycles [ 391.128586] All test took CPU2=454539334 cycles [ 391.222669] All test took CPU3=455649517 cycles [ 391.313946] All test took CPU4=388272196 cycles [ 391.410425] All test took CPU5=384036264 cycles [ 391.492219] All test took CPU6=387432964 cycles [ 391.578433] All test took CPU7=387201996 cycles <default> <patched> [ 304.721224] All test took CPU0=391521310 cycles [ 304.821219] All test took CPU1=393533002 cycles [ 304.917120] All test took CPU2=392243032 cycles [ 305.008986] All test took CPU3=392353853 cycles [ 305.108944] All test took CPU4=297630721 cycles [ 305.196406] All test took CPU5=297548736 cycles [ 305.288602] All test took CPU6=297092392 cycles [ 305.381088] All test took CPU7=297293597 cycles <patched> ~14%-23% patched variant is better. Link: http://lkml.kernel.org/r/20191022155800.20468-1-urezki@gmail.com Signed-off-by: Uladzislau Rezki (Sony) <urezki@gmail.com> Acked-by: Andrew Morton <akpm@linux-foundation.org> Cc: Hillf Danton <hdanton@sina.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: Oleksiy Avramchenko <oleksiy.avramchenko@sonymobile.com> Cc: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
746dd4012d
commit
e36176be1c
86
mm/vmalloc.c
86
mm/vmalloc.c
|
@ -331,6 +331,7 @@ EXPORT_SYMBOL(vmalloc_to_pfn);
|
||||||
|
|
||||||
|
|
||||||
static DEFINE_SPINLOCK(vmap_area_lock);
|
static DEFINE_SPINLOCK(vmap_area_lock);
|
||||||
|
static DEFINE_SPINLOCK(free_vmap_area_lock);
|
||||||
/* Export for kexec only */
|
/* Export for kexec only */
|
||||||
LIST_HEAD(vmap_area_list);
|
LIST_HEAD(vmap_area_list);
|
||||||
static LLIST_HEAD(vmap_purge_list);
|
static LLIST_HEAD(vmap_purge_list);
|
||||||
|
@ -1114,7 +1115,7 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
|
||||||
*/
|
*/
|
||||||
pva = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
|
pva = kmem_cache_alloc_node(vmap_area_cachep, gfp_mask, node);
|
||||||
|
|
||||||
spin_lock(&vmap_area_lock);
|
spin_lock(&free_vmap_area_lock);
|
||||||
|
|
||||||
if (pva && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, pva))
|
if (pva && __this_cpu_cmpxchg(ne_fit_preload_node, NULL, pva))
|
||||||
kmem_cache_free(vmap_area_cachep, pva);
|
kmem_cache_free(vmap_area_cachep, pva);
|
||||||
|
@ -1124,14 +1125,17 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
|
||||||
* returned. Therefore trigger the overflow path.
|
* returned. Therefore trigger the overflow path.
|
||||||
*/
|
*/
|
||||||
addr = __alloc_vmap_area(size, align, vstart, vend);
|
addr = __alloc_vmap_area(size, align, vstart, vend);
|
||||||
|
spin_unlock(&free_vmap_area_lock);
|
||||||
|
|
||||||
if (unlikely(addr == vend))
|
if (unlikely(addr == vend))
|
||||||
goto overflow;
|
goto overflow;
|
||||||
|
|
||||||
va->va_start = addr;
|
va->va_start = addr;
|
||||||
va->va_end = addr + size;
|
va->va_end = addr + size;
|
||||||
va->vm = NULL;
|
va->vm = NULL;
|
||||||
insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
|
|
||||||
|
|
||||||
|
spin_lock(&vmap_area_lock);
|
||||||
|
insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
|
||||||
spin_unlock(&vmap_area_lock);
|
spin_unlock(&vmap_area_lock);
|
||||||
|
|
||||||
BUG_ON(!IS_ALIGNED(va->va_start, align));
|
BUG_ON(!IS_ALIGNED(va->va_start, align));
|
||||||
|
@ -1141,7 +1145,6 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
|
||||||
return va;
|
return va;
|
||||||
|
|
||||||
overflow:
|
overflow:
|
||||||
spin_unlock(&vmap_area_lock);
|
|
||||||
if (!purged) {
|
if (!purged) {
|
||||||
purge_vmap_area_lazy();
|
purge_vmap_area_lazy();
|
||||||
purged = 1;
|
purged = 1;
|
||||||
|
@ -1177,28 +1180,25 @@ int unregister_vmap_purge_notifier(struct notifier_block *nb)
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
|
EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
|
||||||
|
|
||||||
static void __free_vmap_area(struct vmap_area *va)
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* Remove from the busy tree/list.
|
|
||||||
*/
|
|
||||||
unlink_va(va, &vmap_area_root);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Merge VA with its neighbors, otherwise just add it.
|
|
||||||
*/
|
|
||||||
merge_or_add_vmap_area(va,
|
|
||||||
&free_vmap_area_root, &free_vmap_area_list);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Free a region of KVA allocated by alloc_vmap_area
|
* Free a region of KVA allocated by alloc_vmap_area
|
||||||
*/
|
*/
|
||||||
static void free_vmap_area(struct vmap_area *va)
|
static void free_vmap_area(struct vmap_area *va)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
* Remove from the busy tree/list.
|
||||||
|
*/
|
||||||
spin_lock(&vmap_area_lock);
|
spin_lock(&vmap_area_lock);
|
||||||
__free_vmap_area(va);
|
unlink_va(va, &vmap_area_root);
|
||||||
spin_unlock(&vmap_area_lock);
|
spin_unlock(&vmap_area_lock);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Insert/Merge it back to the free tree/list.
|
||||||
|
*/
|
||||||
|
spin_lock(&free_vmap_area_lock);
|
||||||
|
merge_or_add_vmap_area(va,
|
||||||
|
&free_vmap_area_root, &free_vmap_area_list);
|
||||||
|
spin_unlock(&free_vmap_area_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1291,7 +1291,7 @@ static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
|
||||||
flush_tlb_kernel_range(start, end);
|
flush_tlb_kernel_range(start, end);
|
||||||
resched_threshold = lazy_max_pages() << 1;
|
resched_threshold = lazy_max_pages() << 1;
|
||||||
|
|
||||||
spin_lock(&vmap_area_lock);
|
spin_lock(&free_vmap_area_lock);
|
||||||
llist_for_each_entry_safe(va, n_va, valist, purge_list) {
|
llist_for_each_entry_safe(va, n_va, valist, purge_list) {
|
||||||
unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
|
unsigned long nr = (va->va_end - va->va_start) >> PAGE_SHIFT;
|
||||||
|
|
||||||
|
@ -1306,9 +1306,9 @@ static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)
|
||||||
atomic_long_sub(nr, &vmap_lazy_nr);
|
atomic_long_sub(nr, &vmap_lazy_nr);
|
||||||
|
|
||||||
if (atomic_long_read(&vmap_lazy_nr) < resched_threshold)
|
if (atomic_long_read(&vmap_lazy_nr) < resched_threshold)
|
||||||
cond_resched_lock(&vmap_area_lock);
|
cond_resched_lock(&free_vmap_area_lock);
|
||||||
}
|
}
|
||||||
spin_unlock(&vmap_area_lock);
|
spin_unlock(&free_vmap_area_lock);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2030,15 +2030,21 @@ int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page **pages)
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(map_vm_area);
|
EXPORT_SYMBOL_GPL(map_vm_area);
|
||||||
|
|
||||||
static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
|
static inline void setup_vmalloc_vm_locked(struct vm_struct *vm,
|
||||||
unsigned long flags, const void *caller)
|
struct vmap_area *va, unsigned long flags, const void *caller)
|
||||||
{
|
{
|
||||||
spin_lock(&vmap_area_lock);
|
|
||||||
vm->flags = flags;
|
vm->flags = flags;
|
||||||
vm->addr = (void *)va->va_start;
|
vm->addr = (void *)va->va_start;
|
||||||
vm->size = va->va_end - va->va_start;
|
vm->size = va->va_end - va->va_start;
|
||||||
vm->caller = caller;
|
vm->caller = caller;
|
||||||
va->vm = vm;
|
va->vm = vm;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
|
||||||
|
unsigned long flags, const void *caller)
|
||||||
|
{
|
||||||
|
spin_lock(&vmap_area_lock);
|
||||||
|
setup_vmalloc_vm_locked(vm, va, flags, caller);
|
||||||
spin_unlock(&vmap_area_lock);
|
spin_unlock(&vmap_area_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3298,7 +3304,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
|
||||||
goto err_free;
|
goto err_free;
|
||||||
}
|
}
|
||||||
retry:
|
retry:
|
||||||
spin_lock(&vmap_area_lock);
|
spin_lock(&free_vmap_area_lock);
|
||||||
|
|
||||||
/* start scanning - we scan from the top, begin with the last area */
|
/* start scanning - we scan from the top, begin with the last area */
|
||||||
area = term_area = last_area;
|
area = term_area = last_area;
|
||||||
|
@ -3380,29 +3386,38 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
|
||||||
va = vas[area];
|
va = vas[area];
|
||||||
va->va_start = start;
|
va->va_start = start;
|
||||||
va->va_end = start + size;
|
va->va_end = start + size;
|
||||||
|
|
||||||
insert_vmap_area(va, &vmap_area_root, &vmap_area_list);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
spin_unlock(&vmap_area_lock);
|
spin_unlock(&free_vmap_area_lock);
|
||||||
|
|
||||||
/* insert all vm's */
|
/* insert all vm's */
|
||||||
for (area = 0; area < nr_vms; area++)
|
spin_lock(&vmap_area_lock);
|
||||||
setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
|
for (area = 0; area < nr_vms; area++) {
|
||||||
|
insert_vmap_area(vas[area], &vmap_area_root, &vmap_area_list);
|
||||||
|
|
||||||
|
setup_vmalloc_vm_locked(vms[area], vas[area], VM_ALLOC,
|
||||||
pcpu_get_vm_areas);
|
pcpu_get_vm_areas);
|
||||||
|
}
|
||||||
|
spin_unlock(&vmap_area_lock);
|
||||||
|
|
||||||
kfree(vas);
|
kfree(vas);
|
||||||
return vms;
|
return vms;
|
||||||
|
|
||||||
recovery:
|
recovery:
|
||||||
/* Remove previously inserted areas. */
|
/*
|
||||||
|
* Remove previously allocated areas. There is no
|
||||||
|
* need in removing these areas from the busy tree,
|
||||||
|
* because they are inserted only on the final step
|
||||||
|
* and when pcpu_get_vm_areas() is success.
|
||||||
|
*/
|
||||||
while (area--) {
|
while (area--) {
|
||||||
__free_vmap_area(vas[area]);
|
merge_or_add_vmap_area(vas[area],
|
||||||
|
&free_vmap_area_root, &free_vmap_area_list);
|
||||||
vas[area] = NULL;
|
vas[area] = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
overflow:
|
overflow:
|
||||||
spin_unlock(&vmap_area_lock);
|
spin_unlock(&free_vmap_area_lock);
|
||||||
if (!purged) {
|
if (!purged) {
|
||||||
purge_vmap_area_lazy();
|
purge_vmap_area_lazy();
|
||||||
purged = true;
|
purged = true;
|
||||||
|
@ -3453,9 +3468,12 @@ void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
|
||||||
|
|
||||||
#ifdef CONFIG_PROC_FS
|
#ifdef CONFIG_PROC_FS
|
||||||
static void *s_start(struct seq_file *m, loff_t *pos)
|
static void *s_start(struct seq_file *m, loff_t *pos)
|
||||||
|
__acquires(&vmap_purge_lock)
|
||||||
__acquires(&vmap_area_lock)
|
__acquires(&vmap_area_lock)
|
||||||
{
|
{
|
||||||
|
mutex_lock(&vmap_purge_lock);
|
||||||
spin_lock(&vmap_area_lock);
|
spin_lock(&vmap_area_lock);
|
||||||
|
|
||||||
return seq_list_start(&vmap_area_list, *pos);
|
return seq_list_start(&vmap_area_list, *pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3465,8 +3483,10 @@ static void *s_next(struct seq_file *m, void *p, loff_t *pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void s_stop(struct seq_file *m, void *p)
|
static void s_stop(struct seq_file *m, void *p)
|
||||||
|
__releases(&vmap_purge_lock)
|
||||||
__releases(&vmap_area_lock)
|
__releases(&vmap_area_lock)
|
||||||
{
|
{
|
||||||
|
mutex_unlock(&vmap_purge_lock);
|
||||||
spin_unlock(&vmap_area_lock);
|
spin_unlock(&vmap_area_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue