mm/ksm: fix ksm exec support for prctl
Subsystems (4)
PATCH v3 1/1 mm/ksm: fix ksm exec support for prctl
commit 3c6f33b7273a ("mm/ksm: support fork/exec for prctl") inherits MMF_VM_MERGE_ANY flag when a task calls execve(). Howerver, it doesn't create the mm_slot, so ksmd will not try to scan this task.
To fix it, allocate and add the mm_slot to ksm_mm_head in __bprm_mm_init() when the mm has MMF_VM_MERGE_ANY flag.
--- fs/exec.c | 4 ++++ include/linux/ksm.h | 13 +++++++++++++ 2 files changed, 17 insertions(+)
fs/exec.c +4 -0
@@ -67,6 +67,7 @@#include <linux/time_namespace.h> #include <linux/user_events.h> #include <linux/rseq.h> +#include <linux/ksm.h> #include <linux/uaccess.h> #include <asm/mmu_context.h>
@@ -267,6 +268,9 @@ static int __bprm_mm_init(struct linux_binprm *bprm) goto err_free; } +if (ksm_execve (mm)) + goto err; +/* * Place the stack at the largest stack address the architecture * supports. Later, we'll move this to an appropriate place. We don't
include/linux/ksm.h +13 -0
@@ -59,6 +59,14 @@ static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)return 0 ; } +static inline int ksm_execve (struct mm_struct *mm) +{ +if (test_bit (MMF_VM_MERGE_ANY , &mm->flags)) +return __ksm_enter (mm); + +return 0 ; +} +static inline void ksm_exit (struct mm_struct *mm) {if (test_bit (MMF_VM_MERGEABLE , &mm->flags))
@@ -107,6 +115,11 @@ static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)return 0 ; } +static inline int ksm_execve (struct mm_struct *mm) +{ +return 0 ; +} +static inline void ksm_exit (struct mm_struct *mm) { }
Signature · 1 line
2.25.1
Sorry, the v3 tag is erroneous. This patch is the first version.
Quoted 67 lines · Jinjiang Tu
commit
3c6f33b7273a("mm/ksm: support fork/exec for prctl") inheritsMMF_VM_MERGE_ANYflag when a task callsexecve(). Howerver, it doesn't create themm_slot, so ksmd will not try to scan this task.To fix it, allocate and add the
mm_slottoksm_mm_headin__bprm_mm_init()when the mm hasMMF_VM_MERGE_ANYflag.Fixes 3c6f33b7273a ("mm/ksm: support fork/exec for prctl")Signed-off-by Jinjiang Tu <[email protected]>--- fs/exec.c | 4 ++++ include/linux/ksm.h | 13 +++++++++++++ 2 files changed, 17 insertions(+)fs/exec.c +4
-0@@ -67,6 +67,7 @@#include <linux/time_namespace.h> #include <linux/user_events.h> #include <linux/rseq.h> +#include <linux/ksm.h> #include <linux/uaccess.h> #include <asm/mmu_context.h> @@ -267,6 +268,9 @@ static int __bprm_mm_init(struct linux_binprm *bprm) goto err_free; } +if (ksm_execve (mm)) + goto err; +/* * Place the stack at the largest stack address the architecture * supports. Later, we'll move this to an appropriate place. We don't include/linux/ksm.h +13
-0@@ -59,6 +59,14 @@ static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)return 0 ; } +static inline int ksm_execve (struct mm_struct *mm) +{ +if (test_bit (MMF_VM_MERGE_ANY , &mm->flags)) +return __ksm_enter (mm); + +return 0 ; +} +static inline void ksm_exit (struct mm_struct *mm) {if (test_bit (MMF_VM_MERGEABLE , &mm->flags))@@ -107,6 +115,11 @@ static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)return 0 ; } +static inline int ksm_execve (struct mm_struct *mm) +{ +return 0 ; +} +static inline void ksm_exit (struct mm_struct *mm) { }
Quoted 6 lines · Jinjiang Tu
commit
3c6f33b7273a("mm/ksm: support fork/exec for prctl") inheritsMMF_VM_MERGE_ANYflag when a task callsexecve(). Howerver, it doesn't create themm_slot, so ksmd will not try to scan this task.To fix it, allocate and add the
mm_slottoksm_mm_headin__bprm_mm_init()when the mm hasMMF_VM_MERGE_ANYflag.
That would mean that 3c6f33b7273a is effectively ineffective for fork+exec and only works with fork?
Quoted 35 lines
Fixes 3c6f33b7273a ("mm/ksm: support fork/exec for prctl")Signed-off-by Jinjiang Tu <[email protected]>--- fs/exec.c | 4 ++++ include/linux/ksm.h | 13 +++++++++++++ 2 files changed, 17 insertions(+)fs/exec.c +4
-0@@ -67,6 +67,7 @@#include <linux/time_namespace.h> #include <linux/user_events.h> #include <linux/rseq.h> +#include <linux/ksm.h> #include <linux/uaccess.h> #include <asm/mmu_context.h> @@ -267,6 +268,9 @@ static int __bprm_mm_init(struct linux_binprm *bprm) goto err_free; } +if (ksm_execve (mm)) + goto err; +/* * Place the stack at the largest stack address the architecture * supports. Later, we'll move this to an appropriate place. We don't include/linux/ksm.h +0
-0
include/linux/ksm.h static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) · line 65
Quoted 7 lines
include/linux/ksm.h +4
-0@@ -59,6 +59,14 @@ static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)return 0 ; } +static inline int ksm_execve (struct mm_struct *mm) +{ +if (test_bit (MMF_VM_MERGE_ANY , &mm->flags)) +return __ksm_enter (mm);
As soon as we did the __ksm_enter(), we have to set MMF_VM_MERGEABLE. I don't think it would be set right now, because:
mm_alloc()->mm_init() will initialize the flags using
mm->flags = mmf_init_flags(current->mm->flags);
Whereby MMF_INIT_MASK contains only MMF_VM_MERGE_ANY_MASK.
So we likely need a set_bit(MMF_VM_MERGEABLE, &mm->flags) here as well. Otherwise ksm_exit() wouldn't clean up, and we might call __ksm_enter() twice.
And now I wonder, when would be the right point to call __ksm_enter().
__mmput() calls ksm_exit(). But for example, if __bprm_mm_init() fails after __ksm_enter(), we will only call mmdrop(), not cleaning up ... so that looks wrong.
We would have to make sure we call __ksm_enter() only once we know that callers will call mm_put(). that is the case once we return from bprm_mm_init() with success.
Maybe move the ksm_execve() to bprm_mm_init(), adding a comment that cleanup will only happen during mm_put(), so it's harder to mess up in the future?
Signature · 3 lines
Cheers, David / dhildenb
Quoted 10 lines · David Hildenbrand
Quoted 6 lines · Jinjiang Tu
commit
3c6f33b7273a("mm/ksm: support fork/exec for prctl") inheritsMMF_VM_MERGE_ANYflag when a task callsexecve(). Howerver, it doesn't create themm_slot, so ksmd will not try to scan this task.To fix it, allocate and add the
mm_slottoksm_mm_headin__bprm_mm_init()when the mm hasMMF_VM_MERGE_ANYflag.That would mean that
3c6f33b7273ais effectively ineffective for fork+exec and only works with fork?
Yes. In my test case, parent process calls prctl with PR_SET_MEMORY_MERGE, and then fork, execeve a new process. The new process allocates 3 anon pages with same content and loops infinitely. However, the 3 pages are not merged.
Quoted 43 lines
Quoted 33 lines
Fixes 3c6f33b7273a ("mm/ksm: support fork/exec for prctl")Signed-off-by Jinjiang Tu <[email protected]>---
fs/exec.c | 4 ++++
include/linux/ksm.h| 13 +++++++++++++2 files changed, 17 insertions(+)fs/exec.c +3
-0@@ -67,6 +67,7 @@ #include <linux/time_namespace.h> #include <linux/user_events.h> #include <linux/rseq.h> +#include <linux/ksm.h> #include <linux/uaccess.h> #include <asm/mmu_context.h>@@ -267,6 +268,9 @@ static int __bprm_mm_init(struct linux_binprm *bprm) goto err_free; } + if (ksm_execve(mm)) + goto err; + /** Place the stack at the largest stack address the architecture * supports. Later, we'll move this to an appropriate place. We don't include/linux/ksm.h +0
-0include/linux/ksm.h static inline int ksm_fork(struct mm_struct *mm, · line 65
Quoted 7 lines
include/linux/ksm.h +3
-0@@ -59,6 +59,14 @@ static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) return 0; } +static inline int ksm_execve(struct mm_struct *mm) +{ + if (test_bit(MMF_VM_MERGE_ANY, &mm->flags)) + return __ksm_enter(mm);As soon as we did the
__ksm_enter(), we have to setMMF_VM_MERGEABLE. I don't think it would be set right now, because:
Quoted 8 lines
mm_alloc()->mm_init() will initialize the flags using
mm->flags =
mmf_init_flags(current->mm->flags);Whereby
MMF_INIT_MASKcontains onlyMMF_VM_MERGE_ANY_MASK.So we likely need a
set_bit(MMF_VM_MERGEABLE, &mm->flags)here as well. Otherwiseksm_exit()wouldn't clean up, and we might call__ksm_enter()twice.
__ksm_enter() will set MMF_VM_MERGEABLE when it succeeds.
Quoted 5 lines
And now I wonder, when would be the right point to call
__ksm_enter().
__mmput()callsksm_exit(). But for example, if__bprm_mm_init()fails after__ksm_enter(), we will only callmmdrop(), not cleaning up ... so that looks wrong.
Yes, I forgot cleanup in error path. ksm_exit() should be called when __bprm_mm_init() fails after __ksm_enter().
Quoted 5 lines
We would have to make sure we call
__ksm_enter()only once we know that callers will callmm_put(). that is the case once we return frombprm_mm_init()with success.Maybe move the
ksm_execve()tobprm_mm_init(), adding a comment that cleanup will only happen duringmm_put(), so it's harder to mess up in the future?
__ksm_enter() should be called under mmap write lock to avoid race with ksmd. So we can't move ksm_execve() to bprm_mm_init().
Quoted 13 lines
Quoted 13 lines
Quoted 5 lines
+static inline int
ksm_execve(struct mm_struct *mm)
+{
+ if (test_bit(MMF_VM_MERGE_ANY, &mm->flags)) + return__ksm_enter(mm);As soon as we did the
__ksm_enter(), we have to setMMF_VM_MERGEABLE. I don't think it would be set right now, because:mm_alloc()->mm_init() will initialize the flags using
mm->flags =
mmf_init_flags(current->mm->flags);
Quoted 9 lines
Quoted 6 lines
Whereby
MMF_INIT_MASKcontains onlyMMF_VM_MERGE_ANY_MASK.So we likely need a
set_bit(MMF_VM_MERGEABLE, &mm->flags)here as well. Otherwiseksm_exit()wouldn't clean up, and we might call__ksm_enter()twice.
__ksm_enter()will setMMF_VM_MERGEABLEwhen it succeeds.
Ah, indeed!
Quoted 14 lines
Quoted 7 lines
And now I wonder, when would be the right point to call
__ksm_enter().
__mmput()callsksm_exit(). But for example, if__bprm_mm_init()fails after__ksm_enter(), we will only callmmdrop(), not cleaning up ... so that looks wrong.Yes, I forgot cleanup in error path.
ksm_exit()should be called when__bprm_mm_init()fails after__ksm_enter().Quoted 5 lines
We would have to make sure we call
__ksm_enter()only once we know that callers will callmm_put(). that is the case once we return frombprm_mm_init()with success.
Quoted 6 lines
Quoted 4 lines
Maybe move the
ksm_execve()tobprm_mm_init(), adding a comment that cleanup will only happen duringmm_put(), so it's harder to mess up in the future?
__ksm_enter()should be called under mmap write lock to avoid race with ksmd. So we can't moveksm_execve()tobprm_mm_init().
Makes sense. We have to be careful that this won't silently break in the future. Better add some comment as well.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 10 lines · Jinjiang Tu
commit
3c6f33b7273a("mm/ksm: support fork/exec for prctl") inheritsMMF_VM_MERGE_ANYflag when a task callsexecve(). Howerver, it doesn't create themm_slot, so ksmd will not try to scan this task.To fix it, allocate and add the
mm_slottoksm_mm_headin__bprm_mm_init()when the mm hasMMF_VM_MERGE_ANYflag.Fixes 3c6f33b7273a ("mm/ksm: support fork/exec for prctl")Signed-off-by Jinjiang Tu <[email protected]>---
Can we also please extend the KSM selftest? tools/testing/selftests/mm/ksm_functional_tests.c
We do have test_prctl_fork_exec(), but in ksm_fork_exec_child() we only test if the flag is set, not if de-duplication using the ksm daemon actually works.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 8 lines · David Hildenbrand
Quoted 7 lines · Jinjiang Tu
commit
3c6f33b7273a("mm/ksm: support fork/exec for prctl") inheritsMMF_VM_MERGE_ANYflag when a task callsexecve(). Howerver, it doesn't create themm_slot, so ksmd will not try to scan this task.To fix it, allocate and add the
mm_slottoksm_mm_headin__bprm_mm_init()when the mm hasMMF_VM_MERGE_ANYflag.
Quoted 8 lines
Quoted 3 lines
Fixes 3c6f33b7273a ("mm/ksm: support fork/exec for prctl")Signed-off-by Jinjiang Tu <[email protected]>---
Can we also please extend the KSM selftest?
tools/testing/selftests/mm/ksm_functional_tests.cWe do have
test_prctl_fork_exec(), but inksm_fork_exec_child()we only test if the flag is set, not if de-duplication using the ksm daemon actually works.
OK, I will try to extend the selftest.