Commit c5723c0b authored by Jinjiang Tu's avatar Jinjiang Tu
Browse files

mm/ksm: fix ksm exec support for prctl

mainline inclusion
from mainline
commit 3a9e567ca45fb5280065283d10d9a11f0db61d2b
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I9GT87

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=3a9e567ca45fb5280065283d10d9a11f0db61d2b

--------------------------------

Patch series "mm/ksm: fix ksm exec support for prctl", v4.

commit 3c6f33b7273a ("mm/ksm: support fork/exec for prctl") inherits
MMF_VM_MERGE_ANY flag when a task calls execve().  However, it doesn't
create the mm_slot, so ksmd will not try to scan this task.  The first
patch fixes the issue.

The second patch refactors to prepare for the third patch.  The third
patch extends the selftests of ksm to verfity the deduplication really
happens after fork/exec inherits ths KSM setting.

This patch (of 3):

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.

Link: https://lkml.kernel.org/r/20240328111010.1502191-1-tujinjiang@huawei.com
Link: https://lkml.kernel.org/r/20240328111010.1502191-2-tujinjiang@huawei.com


Fixes: 3c6f33b7273a ("mm/ksm: support fork/exec for prctl")
Signed-off-by: default avatarJinjiang Tu <tujinjiang@huawei.com>
Reviewed-by: default avatarDavid Hildenbrand <david@redhat.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
Cc: Nanyong Sun <sunnanyong@huawei.com>
Cc: Rik van Riel <riel@surriel.com>
Cc: Stefan Roesch <shr@devkernel.io>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>

Conflicts:
	fs/exec.c
[Context conflicts, and use __GENKSYMS__ to avoid kabi breakage warning.]
Signed-off-by: default avatarJinjiang Tu <tujinjiang@huawei.com>
parent a66b953d
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -65,6 +65,9 @@
#include <linux/compat.h>
#include <linux/vmalloc.h>
#include <linux/io_uring.h>
#ifndef __GENKSYMS__
#include <linux/ksm.h>
#endif

#include <linux/uaccess.h>
#include <asm/mmu_context.h>
@@ -252,6 +255,14 @@ static int __bprm_mm_init(struct linux_binprm *bprm)
		goto err_free;
	}

	/*
	 * Need to be called with mmap write lock
	 * held, to avoid race with ksmd.
	 */
	err = ksm_execve(mm);
	if (err)
		goto err_ksm;

	/*
	 * Place the stack at the largest stack address the architecture
	 * supports. Later, we'll move this to an appropriate place. We don't
@@ -273,6 +284,8 @@ static int __bprm_mm_init(struct linux_binprm *bprm)
	bprm->p = vma->vm_end - sizeof(void *);
	return 0;
err:
	ksm_exit(mm);
err_ksm:
	mmap_write_unlock(mm);
err_free:
	bprm->vma = NULL;
+13 −0
Original line number Diff line number Diff line
@@ -45,6 +45,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))
@@ -83,6 +91,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)
{
}