fs/proc/task_mmu.c: add user-space support for resetting mm->hiwater_rss (peak RSS)
authorPetr Cermak <petrcermak@chromium.org>
Wed, 18 Feb 2015 10:39:10 +0000 (10:39 +0000)
committerJP Abgrall <jpa@google.com>
Fri, 6 Mar 2015 19:44:26 +0000 (19:44 +0000)
Peak resident size of a process can be reset back to the process's
current rss value by writing "5" to /proc/pid/clear_refs.  The driving
use-case for this would be getting the peak RSS value, which can be
retrieved from the VmHWM field in /proc/pid/status, per benchmark
iteration or test scenario.

Origin:
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=695f055936938c674473ea071ca7359a863551e7

[akpm@linux-foundation.org: clarify behaviour in documentation]
Signed-off-by: Petr Cermak <petrcermak@chromium.org>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Primiano Tucci <primiano@chromium.org>
Cc: Petr Cermak <petrcermak@chromium.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Change-Id: I543a7640639d9916e813af875003fe3ee3a6bfe0

Documentation/filesystems/proc.txt
fs/proc/task_mmu.c
include/linux/mm.h

index e0eb9d287312edde869c2f16d21a436f3afd1ae7..22c3b9c17e5ba3a61fc72c38d9c7358eefaec180 100644 (file)
@@ -490,6 +490,10 @@ To clear the bits for the file mapped pages associated with the process
     > echo 3 > /proc/PID/clear_refs
 Any other value written to /proc/PID/clear_refs will have no effect.
 
+To reset the peak resident set size ("high water mark") to the process's
+current value:
+    > echo 5 > /proc/PID/clear_refs
+
 The /proc/pid/pagemap gives the PFN, which can be used to find the pageflags
 using /proc/kpageflags and number of times a page is mapped using
 /proc/kpagecount. For detailed explanation, see Documentation/vm/pagemap.txt.
index e2c925fa782714943f1c83d937f2427d77443e82..e5bc1c7e266702737c0508d9bfe440412fe06a92 100644 (file)
@@ -784,6 +784,7 @@ static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
 #define CLEAR_REFS_ALL 1
 #define CLEAR_REFS_ANON 2
 #define CLEAR_REFS_MAPPED 3
+#define CLEAR_REFS_MM_HIWATER_RSS 5
 
 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
                                size_t count, loff_t *ppos)
@@ -803,7 +804,8 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
        rv = kstrtoint(strstrip(buffer), 10, &type);
        if (rv < 0)
                return rv;
-       if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED)
+       if ((type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED) &&
+           type != CLEAR_REFS_MM_HIWATER_RSS)
                return -EINVAL;
        task = get_proc_task(file_inode(file));
        if (!task)
@@ -814,6 +816,18 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
                        .pmd_entry = clear_refs_pte_range,
                        .mm = mm,
                };
+
+               if (type == CLEAR_REFS_MM_HIWATER_RSS) {
+                       /*
+                        * Writing 5 to /proc/pid/clear_refs resets the peak
+                        * resident set size to this mm's current rss value.
+                        */
+                       down_write(&mm->mmap_sem);
+                       reset_mm_hiwater_rss(mm);
+                       up_write(&mm->mmap_sem);
+                       goto out_mm;
+               }
+
                down_read(&mm->mmap_sem);
                for (vma = mm->mmap; vma; vma = vma->vm_next) {
                        clear_refs_walk.private = vma;
@@ -837,6 +851,7 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
                }
                flush_tlb_mm(mm);
                up_read(&mm->mmap_sem);
+out_mm:
                mmput(mm);
        }
        put_task_struct(task);
index ff7f6375f33ff26a236a4d5bd6b2c91fc5099156..2d8d22cf15cebc6bda76cf9388bcf10603d8a475 100644 (file)
@@ -1166,6 +1166,11 @@ static inline void update_hiwater_vm(struct mm_struct *mm)
                mm->hiwater_vm = mm->total_vm;
 }
 
+static inline void reset_mm_hiwater_rss(struct mm_struct *mm)
+{
+       mm->hiwater_rss = get_mm_rss(mm);
+}
+
 static inline void setmax_mm_hiwater_rss(unsigned long *maxrss,
                                         struct mm_struct *mm)
 {