mm: replace mmap_sem for mm->exe_file serialization
Subsystems (5)
PATCH 1/1 mm: replace mmap_sem for mm->exe_file serialization
We currently use the mmap_sem to serialize the mm exe_file. This is atrocious and a clear example of the misuses this lock has all over the place, making any significant changes to the address space locking that much more complex and tedious. This also has to do of how we used to check for the vma's vm_file being VM_EXECUTABLE (much of which was replaced by 2dd8ad81e31).
This patch, therefore, removes the mmap_sem dependency and introduces a specific lock for the exe_file (rwlock_t, as it is read mostly and protects a trivial critical region). As mentioned, the motivation is to cleanup mmap_sem (as opposed to exe_file performance). A nice side effect of this is that we avoid taking the mmap_sem (shared) in fork paths for the exe_file handling (note that readers block when the rwsem is taken exclusively by another thread).
Now that callers have been updated and standardized[1, 2] around the get_mm_set_exe_file() interface, changing the locking scheme is quite straightforward. The exception being the prctl calls (ie PR_SET_MM_EXE_FILE). Because this caller actually _updates_ the mm->exe_file, we need to handle it in the same patch that changes the locking rules. For this we need to reorganize prctl_set_mm_exe_file, such that:
o mmap_sem is taken when actually needed.
o a new set_mm_exe_file_locked() function is introduced to be used by prctl. We now need to explicitly acquire the exe_file_lock as before it was implicit in holding the mmap_sem for write.
o a new __prctl_set_mm_exe_file() helper is created, which actually does the exe_file handling for the mm side -- needing the write lock for updating the mm->flags (*sigh*). In the future we could have a unique mm::exe_file_struct and keep track of MMF_EXE_FILE_CHANGED on our own.
mm: improve handling of mm->exe_file
[1] Part 1: https://lkml.org/lkml/2015/2/18/721
[2] Part 2: https://lkml.org/lkml/2015/2/25/679
Applies on top of linux-next (20150225).
--- fs/exec.c | 6 ++++ include/linux/mm.h | 3 ++ include/linux/mm_types.h | 1 + kernel/fork.c | 25 ++++++++++--- kernel/sys.c | 92 ++++++++++++++++++++++++++++-------------------- 5 files changed, 84 insertions(+), 43 deletions(-)
fs/exec.c +6 -0
@@ -1078,7 +1078,13 @@ int flush_old_exec(struct linux_binprm * bprm)if (retval) goto out; +/* +* Must be called _before_ exec_mmap() as bprm->mm is +* not visibile until then. This also enables the update +* to be lockless. +*/ set_mm_exe_file (bprm->mm, bprm->file); +/* * Release all of the old mmap stuff */
include/linux/mm.h +3 -0
@@ -1907,7 +1907,10 @@ static inline int check_data_rlimit(unsigned long rlim,extern int mm_take_all_locks (struct mm_struct *mm);extern void mm_drop_all_locks (struct mm_struct *mm); +/* mm->exe_file handling */ extern void set_mm_exe_file (struct mm_struct *mm,struct file *new_exe_file); +extern void set_mm_exe_file_locked (struct mm_struct *mm, +struct file *new_exe_file);extern struct file *get_mm_exe_file (struct mm_struct *mm);extern int may_expand_vm (struct mm_struct *mm,unsigned long npages);
include/linux/mm_types.h +1 -0
@@ -427,6 +427,7 @@ struct mm_struct {#endif /* store ref to file /proc/<pid>/exe symlink points to */ +rwlock_t exe_file_lock;struct file *exe_file;#ifdef CONFIG_MMU_NOTIFIER struct mmu_notifier_mm *mmu_notifier_mm;
kernel/fork.c +20 -5
@@ -565,6 +565,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)mm_init_aio (mm);mm_init_owner (mm, p);mmu_notifier_mm_init (mm); +rwlock_init (&mm->exe_file_lock);clear_tlb_flush_pending (mm);#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) && !USE_SPLIT_PMD_PTLOCKS mm->pmd_huge_pte =NULL ;
@@ -674,12 +675,25 @@ void mmput(struct mm_struct *mm) }EXPORT_SYMBOL_GPL (mmput); +void set_mm_exe_file_locked (struct mm_struct *mm,struct file *new_exe_file) +{ +if (new_exe_file) +get_file (new_exe_file); +if (mm->exe_file) +fput (mm->exe_file); + +write_lock (&mm->exe_file_lock); + mm->exe_file = new_exe_file; +write_unlock (&mm->exe_file_lock); +} +void set_mm_exe_file (struct mm_struct *mm,struct file *new_exe_file) {if (new_exe_file)get_file (new_exe_file);if (mm->exe_file)fput (mm->exe_file); + mm->exe_file = new_exe_file; }
@@ -687,19 +701,20 @@ struct file *get_mm_exe_file(struct mm_struct *mm) {struct file *exe_file; -/* We need mmap_sem to protect against races with removal of exe_file */ -down_read (&mm->mmap_sem); +read_lock (&mm->exe_file_lock); exe_file = mm->exe_file;if (exe_file)get_file (exe_file); -up_read (&mm->mmap_sem); +read_unlock (&mm->exe_file_lock);return exe_file; }static void dup_mm_exe_file (struct mm_struct *oldmm,struct mm_struct *newmm) { -/* It's safe to write the exe_file pointer without exe_file_lock because -* this is called during fork when the task is not yet in /proc */ +/* +* It's safe to write the exe_file without the +* exe_file_lock as we are just setting up the new task. +*/ newmm->exe_file =get_mm_exe_file (oldmm); }
kernel/sys.c +54 -38
122 lines not on this page · raw message
Signature · 9 lines
2.1.4 -- 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 36 lines · Davidlohr Bueso
We currently use the
mmap_semto serialize the mmexe_file. This is atrocious and a clear example of the misuses this lock has all over the place, making any significant changes to the address space locking that much more complex and tedious. This also has to do of how we used to check for the vma'svm_filebeingVM_EXECUTABLE(much of which was replaced by2dd8ad81e31).This patch, therefore, removes the
mmap_semdependency and introduces a specific lock for theexe_file(rwlock_t, as it is read mostly and protects a trivial critical region). As mentioned, the motivation is to cleanupmmap_sem(as opposed toexe_fileperformance). A nice side effect of this is that we avoid taking themmap_sem(shared) in fork paths for theexe_filehandling (note that readers block when the rwsem is taken exclusively by another thread).Now that callers have been updated and standardized[1, 2] around the
get_mm_set_exe_file()interface, changing the locking scheme is quite straightforward. The exception being the prctl calls (iePR_SET_MM_EXE_FILE). Because this caller actually _updates_ themm->exe_file, we need to handle it in the same patch that changes the locking rules. For this we need to reorganizeprctl_set_mm_exe_file, such that:o
mmap_semis taken when actually needed.o a new
set_mm_exe_file_locked()function is introduced to be used by prctl. We now need to explicitly acquire theexe_file_lockas before it was implicit in holding themmap_semfor write.o a new
__prctl_set_mm_exe_file()helper is created, which actually does theexe_filehandling for the mm side -- needing the write lock for updating themm->flags(*sigh*). In the future we could have a uniquemm::exe_file_structand keep track ofMMF_EXE_FILE_CHANGEDon our own.
Quoted 7 lines
mm: improve handling of
mm->exe_file
[1] Part 1: https://lkml.org/lkml/2015/2/18/721
[2] Part 2: https://lkml.org/lkml/2015/2/25/679Applies on top of linux-next (20150225).
Signed-off-by Davidlohr Bueso <[email protected]>
Hi Davidlohr, it would be interesting to know if the cleanup bring some performance benefit?
--
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 9 lines · Cyrill Gorcunov
Quoted 7 lines · Davidlohr Bueso
We currently use the
mmap_semto serialize the mmexe_file. This is atrocious and a clear example of the misuses this lock has all over the place, making any significant changes to the address space locking that much more complex and tedious. This also has to do of how we used to check for the vma'svm_filebeingVM_EXECUTABLE(much of which was replaced by2dd8ad81e31).
Quoted 5 lines
Quoted 5 lines
This patch, therefore, removes the
mmap_semdependency and introduces a specific lock for theexe_file(rwlock_t, as it is read mostly and protects a trivial critical region). As mentioned, the motivation is to cleanupmmap_sem(as opposed toexe_fileperformance).
Well, I didn't see the patch, can't really comment.
But I have to admit that this looks as atrocious and a clear example of "lets add yet another random lock which we will regret about later" ;)
rwlock_t in mm_struct just to serialize access to exe_file?
Quoted 4 lines
A nice side effect of this is that we avoid taking
Quoted 3 lines
the
mmap_sem(shared) in fork paths for theexe_filehandling (note that readers block when the rwsem is taken exclusively by another thread).
Yes, this is ugly. Can't we kill this dup_mm_exe_file() and copy change dup_mmap() to also dup ->exe_file ?
Quoted 2 lines
Hi Davidlohr, it would be interesting to know if the cleanup bring some performance benefit?
To me the main question is whether the patch makes this code simpler or uglier ;)
Oleg.
--
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 16 lines · Oleg Nesterov
Quoted 9 lines · Cyrill Gorcunov
Quoted 7 lines · Davidlohr Bueso
We currently use the
mmap_semto serialize the mmexe_file. This is atrocious and a clear example of the misuses this lock has all over the place, making any significant changes to the address space locking that much more complex and tedious. This also has to do of how we used to check for the vma'svm_filebeingVM_EXECUTABLE(much of which was replaced by2dd8ad81e31).Quoted 5 lines
Quoted 5 lines
This patch, therefore, removes the
mmap_semdependency and introduces a specific lock for theexe_file(rwlock_t, as it is read mostly and protects a trivial critical region). As mentioned, the motivation is to cleanupmmap_sem(as opposed toexe_fileperformance).
Quoted 6 lines
Well, I didn't see the patch, can't really comment.
But I have to admit that this looks as atrocious and a clear example of "lets add yet another random lock which we will regret about later" ;)
rwlock_tinmm_structjust to serialize access toexe_file?
I don't see why this is a random lock nor how would we regret this later. I regret having to do these kind of patches because people were lazy and just relied on mmap_sem without thinking beyond their use case. As mentioned I'm also planning on creating an own sort of exe_file_struct, which would be an isolated entity (still in the mm though), with its own locking and prctl bits, that would tidy mm_struct a bit. RCU was something else I considered, but it doesn't suite well in all paths and we would still need a spinlock when updating the file anyway.
If you have a better suggestion please do tell.
Quoted 6 lines
Quoted 5 lines
A nice side effect of this is that we avoid taking
Quoted 3 lines
the
mmap_sem(shared) in fork paths for theexe_filehandling (note that readers block when the rwsem is taken exclusively by another thread).
Quoted 8 lines
Yes, this is ugly. Can't we kill this
dup_mm_exe_file()and copy changedup_mmap()to also dup ->exe_file ?Quoted 2 lines
Hi Davidlohr, it would be interesting to know if the cleanup bring some performance benefit?
To me the main question is whether the patch makes this code simpler or uglier ;)
Its much beyond that. As mentioned, for any significant changes to the mmap_sem locking scheme, this sort of thing needs to be addressed first.
Thanks,
Davidlohr
--
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 19 lines · Davidlohr Bueso
Quoted 18 lines · Oleg Nesterov
Quoted 9 lines · Cyrill Gorcunov
Quoted 7 lines · Davidlohr Bueso
We currently use the
mmap_semto serialize the mmexe_file. This is atrocious and a clear example of the misuses this lock has all over the place, making any significant changes to the address space locking that much more complex and tedious. This also has to do of how we used to check for the vma'svm_filebeingVM_EXECUTABLE(much of which was replaced by2dd8ad81e31).Quoted 5 lines
Quoted 5 lines
This patch, therefore, removes the
mmap_semdependency and introduces a specific lock for theexe_file(rwlock_t, as it is read mostly and protects a trivial critical region). As mentioned, the motivation is to cleanupmmap_sem(as opposed toexe_fileperformance).Well, I didn't see the patch, can't really comment.
Quoted 8 lines
Quoted 4 lines
But I have to admit that this looks as atrocious and a clear example of "lets add yet another random lock which we will regret about later" ;)
rwlock_tinmm_structjust to serialize access toexe_file?I don't see why this is a random lock nor how would we regret this later. I regret having to do these kind of patches because people were lazy and just relied on
mmap_semwithout thinking beyond their use case.
That's history: exe_file had direct relation to mm->mmap_sem, that was file from first executable vma. After my patch it's less related to vmas.
Quoted 6 lines
As mentioned I'm also planning on creating an own sort of
exe_file_struct, which would be an isolated entity (still in the mm though), with its own locking and prctl bits, that would tidymm_structa bit. RCU was something else I considered, but it doesn't suite well in all paths and we would still need a spinlock when updating the file anyway.
Please don't. What's wrong with mmap_sem?
Do you want optimize reading mm->exe_file?
Then you should use rcu for that: struct file is rcu-protected thing. See fget(), you could do something like that.
Quoted 21 lines
If you have a better suggestion please do tell.
Quoted 6 lines
Quoted 5 lines
A nice side effect of this is that we avoid taking
Quoted 3 lines
the
mmap_sem(shared) in fork paths for theexe_filehandling (note that readers block when the rwsem is taken exclusively by another thread).Quoted 8 lines
Yes, this is ugly. Can't we kill this
dup_mm_exe_file()and copy changedup_mmap()to also dup ->exe_file ?Quoted 2 lines
Hi Davidlohr, it would be interesting to know if the cleanup bring some performance benefit?
To me the main question is whether the patch makes this code simpler or uglier ;)
Its much beyond that. As mentioned, for any significant changes to the
mmap_semlocking scheme, this sort of thing needs to be addressed first.
Quoted 8 lines
Thanks,
Davidlohr--
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>
--
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 25 lines · Konstantin Khlebnikov
Quoted 24 lines · Davidlohr Bueso
Quoted 22 lines · Oleg Nesterov
Quoted 9 lines · Cyrill Gorcunov
Quoted 7 lines · Davidlohr Bueso
We currently use the
mmap_semto serialize the mmexe_file. This is atrocious and a clear example of the misuses this lock has all over the place, making any significant changes to the address space locking that much more complex and tedious. This also has to do of how we used to check for the vma'svm_filebeingVM_EXECUTABLE(much of which was replaced by2dd8ad81e31).Quoted 5 lines
Quoted 5 lines
This patch, therefore, removes the
mmap_semdependency and introduces a specific lock for theexe_file(rwlock_t, as it is read mostly and protects a trivial critical region). As mentioned, the motivation is to cleanupmmap_sem(as opposed toexe_fileperformance).Well, I didn't see the patch, can't really comment.
But I have to admit that this looks as atrocious and a clear example of "lets add yet another random lock which we will regret about later" ;)
rwlock_tinmm_structjust to serialize access toexe_file?
Quoted 7 lines
Quoted 3 lines
I don't see why this is a random lock nor how would we regret this later. I regret having to do these kind of patches because people were lazy and just relied on
mmap_semwithout thinking beyond their use case.That's history:
exe_filehad direct relation tomm->mmap_sem, that was file from first executable vma. After my patch it's less related to vmas.
Indeed. Yet I'm not changing the exe_file address space semantics at all.
Quoted 11 lines
Quoted 7 lines
As mentioned I'm also planning on creating an own sort of
exe_file_struct, which would be an isolated entity (still in the mm though), with its own locking and prctl bits, that would tidymm_structa bit. RCU was something else I considered, but it doesn't suite well in all paths and we would still need a spinlock when updating the file anyway.Please don't. What's wrong with
mmap_sem?Do you want optimize reading
mm->exe_file?
No, I want to get rid of certain things being done under mmap_sem, that's all. This is not performance motivated, it's to allow future work on lock breaking. I've just yesterday explained this at lsfmm (and not only related to exe_file). In any case I've clean up this patch and added more on top to create a friendlier interface, I'll send that out a bit later.
Quoted 2 lines
Then you should use rcu for that: struct file is rcu-protected thing. See
fget(), you could do something like that.
As mentioned, not all exe paths are RCU friendly ;) We'd at least need srcu, but that's neither here nor there. A rwlock is suficient to get the job done and we really need not care much about optimizing this particular file further.
Thanks,
Davidlohr
--
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 56 lines · Davidlohr Bueso
Quoted 25 lines · Konstantin Khlebnikov
Quoted 24 lines · Davidlohr Bueso
Quoted 22 lines · Oleg Nesterov
Quoted 9 lines · Cyrill Gorcunov
Quoted 7 lines · Davidlohr Bueso
We currently use the
mmap_semto serialize the mmexe_file. This is atrocious and a clear example of the misuses this lock has all over the place, making any significant changes to the address space locking that much more complex and tedious. This also has to do of how we used to check for the vma'svm_filebeingVM_EXECUTABLE(much of which was replaced by2dd8ad81e31).Quoted 5 lines
Quoted 5 lines
This patch, therefore, removes the
mmap_semdependency and introduces a specific lock for theexe_file(rwlock_t, as it is read mostly and protects a trivial critical region). As mentioned, the motivation is to cleanupmmap_sem(as opposed toexe_fileperformance).Well, I didn't see the patch, can't really comment.
But I have to admit that this looks as atrocious and a clear example of "lets add yet another random lock which we will regret about later" ;)
rwlock_tinmm_structjust to serialize access toexe_file?Quoted 7 lines
Quoted 3 lines
I don't see why this is a random lock nor how would we regret this later. I regret having to do these kind of patches because people were lazy and just relied on
mmap_semwithout thinking beyond their use case.That's history:
exe_filehad direct relation tomm->mmap_sem, that was file from first executable vma. After my patch it's less related to vmas.Indeed. Yet I'm not changing the
exe_fileaddress space semantics at all.Quoted 11 lines
Quoted 7 lines
As mentioned I'm also planning on creating an own sort of
exe_file_struct, which would be an isolated entity (still in the mm though), with its own locking and prctl bits, that would tidymm_structa bit. RCU was something else I considered, but it doesn't suite well in all paths and we would still need a spinlock when updating the file anyway.Please don't. What's wrong with
mmap_sem?Do you want optimize reading
mm->exe_file?No, I want to get rid of certain things being done under
mmap_sem, that's all. This is not performance motivated, it's to allow future work on lock breaking. I've just yesterday explained this at lsfmm (and not only related toexe_file). In any case I've clean up this patch and added more on top to create a friendlier interface, I'll send that out a bit later.
Quoted 7 lines
Quoted 2 lines
Then you should use rcu for that: struct file is rcu-protected thing. See
fget(), you could do something like that.As mentioned, not all exe paths are RCU friendly ;) We'd at least need srcu, but that's neither here nor there. A rwlock is suficient to get the job done and we really need not care much about optimizing this particular file further.
I mean you could make mm->exe_file rcu protected pointer and use everywhere get_mm_exe_file() which grabs file refcount under rcu and returns pointer.
Quoted 4 lines
Thanks,
Davidlohr
--
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>