@@ -284,4 +284,6 @@ struct prctl_mm_map {
#define PR_SET_VMA 0x53564d41
# define PR_SET_VMA_ANON_NAME 0
+#define PR_SET_MEMORY_MERGE 65
+#define PR_GET_MEMORY_MERGE 66
#endif /* _LINUX_PRCTL_H */
@@ -15,6 +15,7 @@
#include <linux/highuid.h>
#include <linux/fs.h>
#include <linux/kmod.h>
+#include <linux/ksm.h>
#include <linux/perf_event.h>
#include <linux/resource.h>
#include <linux/kernel.h>
@@ -2626,6 +2627,34 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
case PR_SET_VMA:
error = prctl_set_vma(arg2, arg3, arg4, arg5);
break;
+#ifdef CONFIG_KSM
+ case PR_SET_MEMORY_MERGE:
+ if (!capable(CAP_SYS_RESOURCE))
+ return -EPERM;
+
+ if (arg2) {
+ if (mmap_write_lock_killable(me->mm))
+ return -EINTR;
+
+ if (test_bit(MMF_VM_MERGEABLE, &me->mm->flags))
+ error = -EINVAL;
+ else if (!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags))
+ error = __ksm_enter(me->mm, MMF_VM_MERGE_ANY);
+ mmap_write_unlock(me->mm);
+ } else {
+ __ksm_exit(me->mm, MMF_VM_MERGE_ANY);
+ }
+ break;
+ case PR_GET_MEMORY_MERGE:
+ if (!capable(CAP_SYS_RESOURCE))
+ return -EPERM;
+
+ if (arg2 || arg3 || arg4 || arg5)
+ return -EINVAL;
+
+ error = !!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags);
+ break;
+#endif
default:
error = -EINVAL;
break;
This adds two new options to the prctl system call - enable ksm for all vmas of a process (if the vmas support it). - query if ksm has been enabled for a process. Signed-off-by: Stefan Roesch <shr@devkernel.io> --- include/uapi/linux/prctl.h | 2 ++ kernel/sys.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)