Skip to content

Commit 78db341

Browse files
surenbaghdasaryantorvalds
authored andcommitted
mm: add anonymous vma name refcounting
While forking a process with high number (64K) of named anonymous vmas the overhead caused by strdup() is noticeable. Experiments with ARM64 Android device show up to 40% performance regression when forking a process with 64k unpopulated anonymous vmas using the max name lengths vs the same process with the same number of anonymous vmas having no name. Introduce anon_vma_name refcounted structure to avoid the overhead of copying vma names during fork() and when splitting named anonymous vmas. When a vma is duplicated, instead of copying the name we increment the refcount of this structure. Multiple vmas can point to the same anon_vma_name as long as they increment the refcount. The name member of anon_vma_name structure is assigned at structure allocation time and is never changed. If vma name changes then the refcount of the original structure is dropped, a new anon_vma_name structure is allocated to hold the new name and the vma pointer is updated to point to the new structure. With this approach the fork() performance regressions is reduced 3-4x times and with usecases using more reasonable number of VMAs (a few thousand) the regressions is not measurable. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Suren Baghdasaryan <[email protected]> Reviewed-by: Kees Cook <[email protected]> Cc: Al Viro <[email protected]> Cc: Colin Cross <[email protected]> Cc: Cyrill Gorcunov <[email protected]> Cc: Dave Hansen <[email protected]> Cc: David Rientjes <[email protected]> Cc: "Eric W. Biederman" <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jan Glauber <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: John Stultz <[email protected]> Cc: Mel Gorman <[email protected]> Cc: Minchan Kim <[email protected]> Cc: Oleg Nesterov <[email protected]> Cc: Pekka Enberg <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Rob Landley <[email protected]> Cc: "Serge E. Hallyn" <[email protected]> Cc: Shaohua Li <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 9a10064 commit 78db341

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

include/linux/mm_types.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/mm_types_task.h>
66

77
#include <linux/auxvec.h>
8+
#include <linux/kref.h>
89
#include <linux/list.h>
910
#include <linux/spinlock.h>
1011
#include <linux/rbtree.h>
@@ -386,6 +387,12 @@ struct vm_userfaultfd_ctx {
386387
struct vm_userfaultfd_ctx {};
387388
#endif /* CONFIG_USERFAULTFD */
388389

390+
struct anon_vma_name {
391+
struct kref kref;
392+
/* The name needs to be at the end because it is dynamically sized. */
393+
char name[];
394+
};
395+
389396
/*
390397
* This struct describes a virtual memory area. There is one of these
391398
* per VM-area/task. A VM area is any part of the process virtual memory
@@ -437,7 +444,7 @@ struct vm_area_struct {
437444
unsigned long rb_subtree_last;
438445
} shared;
439446
/* Serialized by mmap_sem. */
440-
char *anon_name;
447+
struct anon_vma_name *anon_name;
441448
};
442449

443450
/*

mm/madvise.c

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ static int madvise_need_mmap_write(int behavior)
6464
}
6565

6666
#ifdef CONFIG_ANON_VMA_NAME
67+
static struct anon_vma_name *anon_vma_name_alloc(const char *name)
68+
{
69+
struct anon_vma_name *anon_name;
70+
size_t count;
71+
72+
/* Add 1 for NUL terminator at the end of the anon_name->name */
73+
count = strlen(name) + 1;
74+
anon_name = kmalloc(struct_size(anon_name, name, count), GFP_KERNEL);
75+
if (anon_name) {
76+
kref_init(&anon_name->kref);
77+
memcpy(anon_name->name, name, count);
78+
}
79+
80+
return anon_name;
81+
}
82+
83+
static void vma_anon_name_free(struct kref *kref)
84+
{
85+
struct anon_vma_name *anon_name =
86+
container_of(kref, struct anon_vma_name, kref);
87+
kfree(anon_name);
88+
}
89+
6790
static inline bool has_vma_anon_name(struct vm_area_struct *vma)
6891
{
6992
return !vma->vm_file && vma->anon_name;
@@ -76,7 +99,7 @@ const char *vma_anon_name(struct vm_area_struct *vma)
7699

77100
mmap_assert_locked(vma->vm_mm);
78101

79-
return vma->anon_name;
102+
return vma->anon_name->name;
80103
}
81104

82105
void dup_vma_anon_name(struct vm_area_struct *orig_vma,
@@ -85,34 +108,41 @@ void dup_vma_anon_name(struct vm_area_struct *orig_vma,
85108
if (!has_vma_anon_name(orig_vma))
86109
return;
87110

88-
new_vma->anon_name = kstrdup(orig_vma->anon_name, GFP_KERNEL);
111+
kref_get(&orig_vma->anon_name->kref);
112+
new_vma->anon_name = orig_vma->anon_name;
89113
}
90114

91115
void free_vma_anon_name(struct vm_area_struct *vma)
92116
{
117+
struct anon_vma_name *anon_name;
118+
93119
if (!has_vma_anon_name(vma))
94120
return;
95121

96-
kfree(vma->anon_name);
122+
anon_name = vma->anon_name;
97123
vma->anon_name = NULL;
124+
kref_put(&anon_name->kref, vma_anon_name_free);
98125
}
99126

100127
/* mmap_lock should be write-locked */
101128
static int replace_vma_anon_name(struct vm_area_struct *vma, const char *name)
102129
{
130+
const char *anon_name;
131+
103132
if (!name) {
104133
free_vma_anon_name(vma);
105134
return 0;
106135
}
107136

108-
if (vma->anon_name) {
137+
anon_name = vma_anon_name(vma);
138+
if (anon_name) {
109139
/* Same name, nothing to do here */
110-
if (!strcmp(name, vma->anon_name))
140+
if (!strcmp(name, anon_name))
111141
return 0;
112142

113143
free_vma_anon_name(vma);
114144
}
115-
vma->anon_name = kstrdup(name, GFP_KERNEL);
145+
vma->anon_name = anon_vma_name_alloc(name);
116146
if (!vma->anon_name)
117147
return -ENOMEM;
118148

0 commit comments

Comments
 (0)