[PATCH RFC 4/5] mm/ksm, proc: introduce remote merge
From: Oleksandr Natalenko
Date: Thu May 16 2019 - 05:44:47 EST
Use previously introduced remote madvise knob to mark task's
anonymous memory as mergeable.
To force merging task's VMAs, "merge" hint is used:
# echo merge > /proc/<pid>/madvise
Force unmerging is done similarly:
# echo unmerge > /proc/<pid>/madvise
To achieve this, previously introduced ksm_madvise_*() helpers
are used.
Signed-off-by: Oleksandr Natalenko <oleksandr@xxxxxxxxxx>
---
fs/proc/base.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 51 insertions(+), 1 deletion(-)
diff --git a/fs/proc/base.c b/fs/proc/base.c
index f69532d6b74f..6677580080ed 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -94,6 +94,8 @@
#include <linux/sched/debug.h>
#include <linux/sched/stat.h>
#include <linux/posix-timers.h>
+#include <linux/mman.h>
+#include <linux/ksm.h>
#include <trace/events/oom.h>
#include "internal.h"
#include "fd.h"
@@ -2960,15 +2962,63 @@ static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
static ssize_t madvise_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
+ /* For now, only KSM hints are implemented */
+#ifdef CONFIG_KSM
+ char buffer[PROC_NUMBUF];
+ int behaviour;
struct task_struct *task;
+ struct mm_struct *mm;
+ int err = 0;
+ struct vm_area_struct *vma;
+
+ memset(buffer, 0, sizeof(buffer));
+ if (count > sizeof(buffer) - 1)
+ count = sizeof(buffer) - 1;
+ if (copy_from_user(buffer, buf, count))
+ return -EFAULT;
+
+ if (!memcmp("merge", buffer, min(sizeof("merge")-1, count)))
+ behaviour = MADV_MERGEABLE;
+ else if (!memcmp("unmerge", buffer, min(sizeof("unmerge")-1, count)))
+ behaviour = MADV_UNMERGEABLE;
+ else
+ return -EINVAL;
task = get_proc_task(file_inode(file));
if (!task)
return -ESRCH;
+ mm = get_task_mm(task);
+ if (!mm) {
+ err = -EINVAL;
+ goto out_put_task_struct;
+ }
+
+ down_write(&mm->mmap_sem);
+ switch (behaviour) {
+ case MADV_MERGEABLE:
+ case MADV_UNMERGEABLE:
+ vma = mm->mmap;
+ while (vma) {
+ if (behaviour == MADV_MERGEABLE)
+ ksm_madvise_merge(vma->vm_mm, vma, &vma->vm_flags);
+ else
+ ksm_madvise_unmerge(vma, vma->vm_start, vma->vm_end, &vma->vm_flags);
+ vma = vma->vm_next;
+ }
+ break;
+ }
+ up_write(&mm->mmap_sem);
+
+ mmput(mm);
+
+out_put_task_struct:
put_task_struct(task);
- return count;
+ return err ? err : count;
+#else
+ return -EINVAL;
+#endif /* CONFIG_KSM */
}
static const struct file_operations proc_madvise_operations = {
--
2.21.0