Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/ksm: fix exec/fork inheritance support for prctl

Patch series "ksm: fix exec/fork inheritance", v2.

This series fixes exec/fork inheritance. See the detailed description of
the issue below.


This patch (of 2):

Background
==========

commit d7597f59d1d33 ("mm: add new api to enable ksm per process")
introduced MMF_VM_MERGE_ANY for mm->flags, and allowed user to set it by
prctl() so that the process's VMAs are forcibly scanned by ksmd.

Subsequently, the 3c6f33b7273a ("mm/ksm: support fork/exec for prctl")
supported inheriting the MMF_VM_MERGE_ANY flag when a task calls execve().

Finally, commit 3a9e567ca45fb ("mm/ksm: fix ksm exec support for prctl")
fixed the issue that ksmd doesn't scan the mm_struct with MMF_VM_MERGE_ANY
by adding the mm_slot to ksm_mm_head in __bprm_mm_init().

Problem
=======

In some extreme scenarios, however, this inheritance of MMF_VM_MERGE_ANY
during exec/fork can fail. For example, when the scanning frequency of
ksmd is tuned extremely high, a process carrying MMF_VM_MERGE_ANY may
still fail to pass it to the newly exec'd process. This happens because
ksm_execve() is executed too early in the do_execve flow (prematurely
adding the new mm_struct to the ksm_mm_slot list).

As a result, before do_execve completes, ksmd may have already performed a
scan and found that this new mm_struct has no VM_MERGEABLE VMAs, thus
clearing its MMF_VM_MERGE_ANY flag. Consequently, when the new program
executes, the flag MMF_VM_MERGE_ANY inheritance missed.

Root reason
===========

commit d7597f59d1d33 ("mm: add new api to enable ksm per process") clear
the flag MMF_VM_MERGE_ANY when ksmd found no VM_MERGEABLE VMAs.

Solution
========

Firstly, Don't clear MMF_VM_MERGE_ANY when ksmd found no VM_MERGEABLE
VMAs, because perhaps their mm_struct has just been added to ksm_mm_slot
list, and its process has not yet officially started running or has not
yet performed mmap/brk to allocate anonymous VMAS.

Secondly, recheck MMF_VM_MERGEABLE again if a process takes
MMF_VM_MERGE_ANY, and create a mm_slot and join it into ksm_scan_list
again.

Link: https://lkml.kernel.org/r/20251007182504440BJgK8VXRHh8TD7IGSUIY4@zte.com.cn
Link: https://lkml.kernel.org/r/20251007182821572h_SoFqYZXEP1mvWI4n9VL@zte.com.cn
Fixes: 3c6f33b7273a ("mm/ksm: support fork/exec for prctl")
Fixes: d7597f59d1d3 ("mm: add new api to enable ksm per process")
Signed-off-by: xu xin <xu.xin16@zte.com.cn>
Cc: Stefan Roesch <shr@devkernel.io>
Cc: David Hildenbrand <david@redhat.com>
Cc: Jinjiang Tu <tujinjiang@huawei.com>
Cc: Wang Yaxin <wang.yaxin@zte.com.cn>
Cc: Yang Yang <yang.yang29@zte.com.cn>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

xu xin and committed by
Andrew Morton
590c03ca c6307674

+19 -5
+2 -2
include/linux/ksm.h
··· 17 17 #ifdef CONFIG_KSM 18 18 int ksm_madvise(struct vm_area_struct *vma, unsigned long start, 19 19 unsigned long end, int advice, vm_flags_t *vm_flags); 20 - vm_flags_t ksm_vma_flags(const struct mm_struct *mm, const struct file *file, 20 + vm_flags_t ksm_vma_flags(struct mm_struct *mm, const struct file *file, 21 21 vm_flags_t vm_flags); 22 22 int ksm_enable_merge_any(struct mm_struct *mm); 23 23 int ksm_disable_merge_any(struct mm_struct *mm); ··· 103 103 104 104 #else /* !CONFIG_KSM */ 105 105 106 - static inline vm_flags_t ksm_vma_flags(const struct mm_struct *mm, 106 + static inline vm_flags_t ksm_vma_flags(struct mm_struct *mm, 107 107 const struct file *file, vm_flags_t vm_flags) 108 108 { 109 109 return vm_flags;
+17 -3
mm/ksm.c
··· 2712 2712 spin_unlock(&ksm_mmlist_lock); 2713 2713 2714 2714 mm_slot_free(mm_slot_cache, mm_slot); 2715 + /* 2716 + * Only clear MMF_VM_MERGEABLE. We must not clear 2717 + * MMF_VM_MERGE_ANY, because for those MMF_VM_MERGE_ANY process, 2718 + * perhaps their mm_struct has just been added to ksm_mm_slot 2719 + * list, and its process has not yet officially started running 2720 + * or has not yet performed mmap/brk to allocate anonymous VMAS. 2721 + */ 2715 2722 mm_flags_clear(MMF_VM_MERGEABLE, mm); 2716 - mm_flags_clear(MMF_VM_MERGE_ANY, mm); 2717 2723 mmap_read_unlock(mm); 2718 2724 mmdrop(mm); 2719 2725 } else { ··· 2837 2831 * 2838 2832 * Returns: @vm_flags possibly updated to mark mergeable. 2839 2833 */ 2840 - vm_flags_t ksm_vma_flags(const struct mm_struct *mm, const struct file *file, 2834 + vm_flags_t ksm_vma_flags(struct mm_struct *mm, const struct file *file, 2841 2835 vm_flags_t vm_flags) 2842 2836 { 2843 2837 if (mm_flags_test(MMF_VM_MERGE_ANY, mm) && 2844 - __ksm_should_add_vma(file, vm_flags)) 2838 + __ksm_should_add_vma(file, vm_flags)) { 2845 2839 vm_flags |= VM_MERGEABLE; 2840 + /* 2841 + * Generally, the flags here always include MMF_VM_MERGEABLE. 2842 + * However, in rare cases, this flag may be cleared by ksmd who 2843 + * scans a cycle without finding any mergeable vma. 2844 + */ 2845 + if (unlikely(!mm_flags_test(MMF_VM_MERGEABLE, mm))) 2846 + __ksm_enter(mm); 2847 + } 2846 2848 2847 2849 return vm_flags; 2848 2850 }