Skip to content

Commit 0d2ebf9

Browse files
surenbaghdasaryanakpm00
authored andcommitted
mm/mmap: free vm_area_struct without call_rcu in exit_mmap
call_rcu() can take a long time when callback offloading is enabled. Its use in the vm_area_free can cause regressions in the exit path when multiple VMAs are being freed. Because exit_mmap() is called only after the last mm user drops its refcount, the page fault handlers can't be racing with it. Any other possible user like oom-reaper or process_mrelease are already synchronized using mmap_lock. Therefore exit_mmap() can free VMAs directly, without the use of call_rcu(). Expose __vm_area_free() and use it from exit_mmap() to avoid possible call_rcu() floods and performance regressions caused by it. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Suren Baghdasaryan <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 70d4cbc commit 0d2ebf9

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

include/linux/mm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ void setup_initial_init_mm(void *start_code, void *end_code,
257257
struct vm_area_struct *vm_area_alloc(struct mm_struct *);
258258
struct vm_area_struct *vm_area_dup(struct vm_area_struct *);
259259
void vm_area_free(struct vm_area_struct *);
260+
/* Use only if VMA has no other users */
261+
void __vm_area_free(struct vm_area_struct *vma);
260262

261263
#ifndef CONFIG_MMU
262264
extern struct rb_root nommu_region_tree;

kernel/fork.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)
480480
return new;
481481
}
482482

483-
static void __vm_area_free(struct vm_area_struct *vma)
483+
void __vm_area_free(struct vm_area_struct *vma)
484484
{
485485
free_anon_vma_name(vma);
486486
kmem_cache_free(vm_area_cachep, vma);

mm/mmap.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,18 @@ void unlink_file_vma(struct vm_area_struct *vma)
133133
/*
134134
* Close a vm structure and free it.
135135
*/
136-
static void remove_vma(struct vm_area_struct *vma)
136+
static void remove_vma(struct vm_area_struct *vma, bool unreachable)
137137
{
138138
might_sleep();
139139
if (vma->vm_ops && vma->vm_ops->close)
140140
vma->vm_ops->close(vma);
141141
if (vma->vm_file)
142142
fput(vma->vm_file);
143143
mpol_put(vma_policy(vma));
144-
vm_area_free(vma);
144+
if (unreachable)
145+
__vm_area_free(vma);
146+
else
147+
vm_area_free(vma);
145148
}
146149

147150
static inline struct vm_area_struct *vma_prev_limit(struct vma_iterator *vmi,
@@ -2145,7 +2148,7 @@ static inline void remove_mt(struct mm_struct *mm, struct ma_state *mas)
21452148
if (vma->vm_flags & VM_ACCOUNT)
21462149
nr_accounted += nrpages;
21472150
vm_stat_account(mm, vma->vm_flags, -nrpages);
2148-
remove_vma(vma);
2151+
remove_vma(vma, false);
21492152
}
21502153
vm_unacct_memory(nr_accounted);
21512154
validate_mm(mm);
@@ -3078,7 +3081,7 @@ void exit_mmap(struct mm_struct *mm)
30783081
do {
30793082
if (vma->vm_flags & VM_ACCOUNT)
30803083
nr_accounted += vma_pages(vma);
3081-
remove_vma(vma);
3084+
remove_vma(vma, true);
30823085
count++;
30833086
cond_resched();
30843087
} while ((vma = mas_find(&mas, ULONG_MAX)) != NULL);

0 commit comments

Comments
 (0)