Skip to contentSkip to conversation

mm, proc: Implement /proc/<pid>/totmaps

PATCH Superseded [email protected] 6 participants 23 messages Aug 9, 2016 – Aug 13, 2016

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm, proc: Implement /proc/<pid>/totmaps

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

This new proc file provides information faster than parsing the potentially huge smaps file.

---
 fs/proc/base.c     |   1 +
 fs/proc/internal.h |   4 ++
 fs/proc/task_mmu.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 131 insertions(+)
fs/proc/base.c +1 -0
index a11eb71..de3acdf 100644
@@ -2855,6 +2855,7 @@ static const struct pid_entry tgid_base_stuff[] = {
 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
 	REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
+	REG("totmaps",    S_IRUGO, proc_totmaps_operations),
 #endif
 #ifdef CONFIG_SECURITY
 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
fs/proc/internal.h +4 -0
index aa27810..6f3540f 100644
@@ -58,6 +58,9 @@ union proc_op {
 		struct task_struct *task);
 };
 
+
+extern const struct file_operations proc_totmaps_operations;
+
 struct proc_inode {
 	struct pid *pid;
 	int fd;
@@ -281,6 +284,7 @@ struct proc_maps_private {
 	struct mm_struct *mm;
 #ifdef CONFIG_MMU
 	struct vm_area_struct *tail_vma;
+	struct mem_size_stats *mss;
 #endif
 #ifdef CONFIG_NUMA
 	struct mempolicy *task_mempolicy;
fs/proc/task_mmu.c +126 -0

144 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw
Quoted 10 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

Quoted 2 lines

This new proc file provides information faster than parsing the potentially huge smaps file.

I have no idea about usefulness of this.

The patch is definitely buggy with respect to how it implements actual access to mm.

Quoted 4 lines
fs/proc/task_mmu.c +4 -0
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+	struct proc_maps_private *priv = m->private;
+	struct mm_struct *mm;

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 833

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+	struct vm_area_struct *vma;
+	struct mem_size_stats *mss_sum = priv->mss;
+
+	/* reference to priv->task already taken */
+	/* but need to get the mm here because */
+	/* task could be in the process of exiting */
+	mm = get_task_mm(priv->task);
+	if (!mm || IS_ERR(mm))
+		return -EINVAL;
+

That's not how it's done in smaps.

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 922

Quoted 9 lines
fs/proc/task_mmu.c +9 -0
+static int totmaps_open(struct inode *inode, struct file *file)
+{
+	struct proc_maps_private *priv;
+	int ret = -ENOMEM;
+	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+	if (priv) {
+		priv->mss = kzalloc(sizeof(*priv->mss), GFP_KERNEL);
+		if (!priv->mss)
+			return -ENOMEM;

Cases below explicitly kfree(priv). I can't remember whether the close routine gets called if this one fails. Either way, something is wrong here.

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 929

Quoted 7 lines
fs/proc/task_mmu.c +7 -0
+
+		/* we need to grab references to the task_struct */
+		/* at open time, because there's a potential information */
+		/* leak where the totmaps file is opened and held open */
+		/* while the underlying pid to task mapping changes */
+		/* underneath it */
+		priv->task = get_pid_task(proc_pid(inode), PIDTYPE_PID);

This performs no permission checks that I would see. If you take a look at smaps you will see the user ends up in proc_maps_open which performs proc_mem_open(inode, PTRACE_MODE_READ) and gets a mm from there.

Quoted 16 lines
fs/proc/task_mmu.c +16 -0
+		if (!priv->task) {
+			kfree(priv->mss);
+			kfree(priv);
+			return -ESRCH;
+		}
+
+		ret = single_open(file, totmaps_proc_show, priv);
+		if (ret) {
+			put_task_struct(priv->task);
+			kfree(priv->mss);
+			kfree(priv);
+		}
+	}
+	return ret;
+}
+
Signature · 1 line
Mateusz Guzik
#2 lore raw
Quoted 11 lines · Mateusz Guzik
Quoted 10 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

Quoted 4 lines
Quoted 2 lines

This new proc file provides information faster than parsing the potentially huge smaps file.

I have no idea about usefulness of this.

I can comment about this. The use case is to speed up monitoring of memory consumption in environments where RSS isn't precise.

For example Chrome tends to many processes which have hundreds of VMAs with a substantial amount of shared memory, and the error of using RSS rather than PSS tends to be very large when looking at overall memory consumption. PSS isn't kept as a single number that's exported like RSS, so to calculate PSS means having to parse a very large smaps file.

This process is slow and has to be repeated for many processes, and we found that the just act of doing the parsing was taking up a significant amount of CPU time, so this patch is an attempt to make that process cheaper.

Quoted 67 lines

The patch is definitely buggy with respect to how it implements actual access to mm.

Quoted 4 lines
fs/proc/task_mmu.c +4 -0
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+     struct proc_maps_private *priv = m->private;
+     struct mm_struct *mm;

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 833

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+     struct vm_area_struct *vma;
+     struct mem_size_stats *mss_sum = priv->mss;
+
+     /* reference to priv->task already taken */
+     /* but need to get the mm here because */
+     /* task could be in the process of exiting */
+     mm = get_task_mm(priv->task);
+     if (!mm || IS_ERR(mm))
+             return -EINVAL;
+

That's not how it's done in smaps.

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 922

Quoted 9 lines
fs/proc/task_mmu.c +9 -0
+static int totmaps_open(struct inode *inode, struct file *file)
+{
+     struct proc_maps_private *priv;
+     int ret = -ENOMEM;
+     priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+     if (priv) {
+             priv->mss = kzalloc(sizeof(*priv->mss), GFP_KERNEL);
+             if (!priv->mss)
+                     return -ENOMEM;

Cases below explicitly kfree(priv). I can't remember whether the close routine gets called if this one fails. Either way, something is wrong here.

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 929

Quoted 7 lines
fs/proc/task_mmu.c +7 -0
+
+             /* we need to grab references to the task_struct */
+             /* at open time, because there's a potential information */
+             /* leak where the totmaps file is opened and held open */
+             /* while the underlying pid to task mapping changes */
+             /* underneath it */
+             priv->task = get_pid_task(proc_pid(inode), PIDTYPE_PID);

This performs no permission checks that I would see. If you take a look at smaps you will see the user ends up in proc_maps_open which performs proc_mem_open(inode, PTRACE_MODE_READ) and gets a mm from there.

Quoted 6 lines
fs/proc/task_mmu.c +6 -0
+             if (!priv->task) {
+                     kfree(priv->mss);
+                     kfree(priv);
+                     return -ESRCH;
+             }
+

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 945

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+             ret = single_open(file, totmaps_proc_show, priv);
+             if (ret) {
+                     put_task_struct(priv->task);
+                     kfree(priv->mss);
+                     kfree(priv);
+             }
+     }
+     return ret;
+}
+

--
Mateusz Guzik

#3 lore raw
In reply to Mateusz Guzik
Quoted 19 lines · Mateusz Guzik
Quoted 10 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

Quoted 2 lines

This new proc file provides information faster than parsing the potentially huge smaps file.

I have no idea about usefulness of this.

The patch is definitely buggy with respect to how it implements actual access to mm.

Quoted 16 lines
Quoted 4 lines
fs/proc/task_mmu.c +4 -0
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+	struct proc_maps_private *priv = m->private;
+	struct mm_struct *mm;

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 833

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+	struct vm_area_struct *vma;
+	struct mem_size_stats *mss_sum = priv->mss;
+
+	/* reference to priv->task already taken */
+	/* but need to get the mm here because */
+	/* task could be in the process of exiting */
+	mm = get_task_mm(priv->task);
+	if (!mm || IS_ERR(mm))
+		return -EINVAL;
+

That's not how it's done in smaps.

Alright, I'll have to look into the difference between this approach and the smaps one.

Quoted 14 lines

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 922

Quoted 10 lines
fs/proc/task_mmu.c +9 -0
+static int totmaps_open(struct inode *inode, struct file *file)
+{
+	struct proc_maps_private *priv;
+	int ret = -ENOMEM;
+	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+	if (priv) {
+		priv->mss = kzalloc(sizeof(*priv->mss), GFP_KERNEL);
+		if (!priv->mss)
+			return -ENOMEM;

Cases below explicitly kfree(priv). I can't remember whether the close routine gets called if this one fails. Either way, something is wrong here.

It looks fishy to me too, I'll have it reworked in v2.

Quoted 12 lines

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 929

Quoted 8 lines
fs/proc/task_mmu.c +7 -0
+
+		/* we need to grab references to the task_struct */
+		/* at open time, because there's a potential information */
+		/* leak where the totmaps file is opened and held open */
+		/* while the underlying pid to task mapping changes */
+		/* underneath it */
+		priv->task = get_pid_task(proc_pid(inode), PIDTYPE_PID);

This performs no permission checks that I would see. If you take a look at smaps you will see the user ends up in proc_maps_open which performs proc_mem_open(inode, PTRACE_MODE_READ) and gets a mm from there.

The proc_maps_open() function does seem to be doing everything I need it. I'll have a look at switching to using it.

Thanks for the heads up!

Rob.

Quoted 19 lines
Quoted 18 lines
fs/proc/task_mmu.c +16 -0
+		if (!priv->task) {
+			kfree(priv->mss);
+			kfree(priv);
+			return -ESRCH;
+		}
+
+		ret = single_open(file, totmaps_proc_show, priv);
+		if (ret) {
+			put_task_struct(priv->task);
+			kfree(priv->mss);
+			kfree(priv);
+		}
+	}
+	return ret;
+}
+
#4 lore raw
Quoted 17 lines · Robert Foss
Quoted 8 lines
Quoted 8 lines
fs/proc/task_mmu.c +8 -0
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+    struct proc_maps_private *priv = m->private;
+    struct mm_struct *mm;
+    struct vm_area_struct *vma;
+    struct mem_size_stats *mss_sum = priv->mss;
+
+    /* reference to priv->task already taken */
Quoted 8 lines

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 833

Quoted 6 lines
fs/proc/task_mmu.c +6 -0
+    /* but need to get the mm here because */
+    /* task could be in the process of exiting */
+    mm = get_task_mm(priv->task);
+    if (!mm || IS_ERR(mm))
+        return -EINVAL;
+

That's not how it's done in smaps.

Quoted 2 lines

Alright, I'll have to look into the difference between this approach and the smaps one.

I had a look at show_smaps(), and it's not entirely clear to me what the advantage of doing it show_smaps() way.

mm = get_task_mm(priv->task) is needed to iterate through all of the mappings. Is there a preferable way of doing that?

#5 lore raw
Quoted 24 lines · Robert Foss
Quoted 17 lines · Robert Foss
Quoted 8 lines
Quoted 8 lines
fs/proc/task_mmu.c +8 -0
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+    struct proc_maps_private *priv = m->private;
+    struct mm_struct *mm;
+    struct vm_area_struct *vma;
+    struct mem_size_stats *mss_sum = priv->mss;
+
+    /* reference to priv->task already taken */
Quoted 8 lines

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 833

Quoted 6 lines
fs/proc/task_mmu.c +6 -0
+    /* but need to get the mm here because */
+    /* task could be in the process of exiting */
+    mm = get_task_mm(priv->task);
+    if (!mm || IS_ERR(mm))
+        return -EINVAL;
+

That's not how it's done in smaps.

Quoted 2 lines

Alright, I'll have to look into the difference between this approach and the smaps one.

Quoted 5 lines

I had a look at show_smaps(), and it's not entirely clear to me what the advantage of doing it show_smaps() way.

mm = get_task_mm(priv->task) is needed to iterate through all of the mappings. Is there a preferable way of doing that?

In the other part of the mail I stated smaps goes to proc_maps_open which has:
priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);

This gives you stable access to mm and all needed permission checks.

Then, in the read routine you can just:
if (!atomic_inc_not_zero(&mm->mm_users))

	goto thats_it;

See smaps routines or e.g. environ_read.

Signature · 1 line
Mateusz Guzik
#6 lore raw
Quoted 35 lines · Mateusz Guzik
Quoted 24 lines · Robert Foss
Quoted 17 lines · Robert Foss
Quoted 8 lines
Quoted 8 lines
fs/proc/task_mmu.c +8 -0
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+    struct proc_maps_private *priv = m->private;
+    struct mm_struct *mm;
+    struct vm_area_struct *vma;
+    struct mem_size_stats *mss_sum = priv->mss;
+
+    /* reference to priv->task already taken */
Quoted 8 lines

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 833

Quoted 6 lines
fs/proc/task_mmu.c +6 -0
+    /* but need to get the mm here because */
+    /* task could be in the process of exiting */
+    mm = get_task_mm(priv->task);
+    if (!mm || IS_ERR(mm))
+        return -EINVAL;
+

That's not how it's done in smaps.

Quoted 2 lines

Alright, I'll have to look into the difference between this approach and the smaps one.

Quoted 5 lines

I had a look at show_smaps(), and it's not entirely clear to me what the advantage of doing it show_smaps() way.

mm = get_task_mm(priv->task) is needed to iterate through all of the mappings. Is there a preferable way of doing that?

In the other part of the mail I stated smaps goes to proc_maps_open which has:
priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);

Quoted 8 lines

This gives you stable access to mm and all needed permission checks.

Then, in the read routine you can just:
if (!atomic_inc_not_zero(&mm->mm_users))

	goto thats_it;

See smaps routines or e.g. environ_read.

Ah! I see what you mean now. Thanks for the clarification!

Rob.

#7 lore raw
In reply to [email protected]
Quoted 10 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

Quoted 2 lines

This new proc file provides information faster than parsing the potentially huge smaps file.

You can "parse" /proc/*/pagemap . RSS, swap are there. So which ones do you really need?
Why the separate anon hugepages and anon regular pages?

Quoted 21 lines
fs/proc/task_mmu.c +21 -0
+	seq_printf(m,
+		   "Rss:            %8lu kB\n"
+		   "Pss:            %8lu kB\n"
+		   "Shared_Clean:   %8lu kB\n"
+		   "Shared_Dirty:   %8lu kB\n"
+		   "Private_Clean:  %8lu kB\n"
+		   "Private_Dirty:  %8lu kB\n"
+		   "Referenced:     %8lu kB\n"
+		   "Anonymous:      %8lu kB\n"
+		   "AnonHugePages:  %8lu kB\n"
+		   "Swap:           %8lu kB\n",
+		   mss_sum->resident >> 10,
+		   (unsigned long)(mss_sum->pss >> (10 + PSS_SHIFT)),
+		   mss_sum->shared_clean  >> 10,
+		   mss_sum->shared_dirty  >> 10,
+		   mss_sum->private_clean >> 10,
+		   mss_sum->private_dirty >> 10,
+		   mss_sum->referenced >> 10,
+		   mss_sum->anonymous >> 10,
+		   mss_sum->anonymous_thp >> 10,
+		   mss_sum->swap >> 10);
#8 lore raw
Quoted 12 lines · Alexey Dobriyan
Quoted 10 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

Quoted 4 lines
Quoted 2 lines

This new proc file provides information faster than parsing the potentially huge smaps file.

You can "parse" /proc/*/pagemap . RSS, swap are there.

/proc/*pagemap is generally restricted and I don't believe it would quickly give PSS.

Quoted 2 lines

So which ones do you really need?

PSS and Swap are the most important. RSS isn't precise enough because it counts shared pages fully, and there tends to be a lot of sharing.

Quoted 1 line

Why the separate anon hugepages and anon regular pages?

I'm not sure if it's necessary, but that's how it's broken out in smaps.

Quoted 22 lines
Quoted 22 lines
fs/proc/task_mmu.c +21 -0
+     seq_printf(m,
+                "Rss:            %8lu kB\n"
+                "Pss:            %8lu kB\n"
+                "Shared_Clean:   %8lu kB\n"
+                "Shared_Dirty:   %8lu kB\n"
+                "Private_Clean:  %8lu kB\n"
+                "Private_Dirty:  %8lu kB\n"
+                "Referenced:     %8lu kB\n"
+                "Anonymous:      %8lu kB\n"
+                "AnonHugePages:  %8lu kB\n"
+                "Swap:           %8lu kB\n",
+                mss_sum->resident >> 10,
+                (unsigned long)(mss_sum->pss >> (10 + PSS_SHIFT)),
+                mss_sum->shared_clean  >> 10,
+                mss_sum->shared_dirty  >> 10,
+                mss_sum->private_clean >> 10,
+                mss_sum->private_dirty >> 10,
+                mss_sum->referenced >> 10,
+                mss_sum->anonymous >> 10,
+                mss_sum->anonymous_thp >> 10,
+                mss_sum->swap >> 10);
#9 lore raw
In reply to [email protected]
Quoted 10 lines · On Tue, Aug 9, 2016 at 7:05 PM, <[email protected]> wrote:

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

Quoted 2 lines

This new proc file provides information faster than parsing the potentially huge smaps file.

What statistics do you really need?

I think, performance and flexibility issues could be really solved only by new syscall for querying memory statistics for address range in any process:
process_vm_stat() or some kind of pumped fincore() for /proc/$pid/mem

Quoted 200 lines
---
 fs/proc/base.c     |   1 +
 fs/proc/internal.h |   4 ++
 fs/proc/task_mmu.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 131 insertions(+)
fs/proc/base.c +1 -0
index a11eb71..de3acdf 100644
@@ -2855,6 +2855,7 @@ static const struct pid_entry tgid_base_stuff[] = {
        REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
        REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
        REG("pagemap",    S_IRUSR, proc_pagemap_operations),
+       REG("totmaps",    S_IRUGO, proc_totmaps_operations),
 #endif
 #ifdef CONFIG_SECURITY
        DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
fs/proc/internal.h +4 -0
index aa27810..6f3540f 100644
@@ -58,6 +58,9 @@ union proc_op {
                struct task_struct *task);
 };
 
+
+extern const struct file_operations proc_totmaps_operations;
+
 struct proc_inode {
        struct pid *pid;
        int fd;
@@ -281,6 +284,7 @@ struct proc_maps_private {
        struct mm_struct *mm;
 #ifdef CONFIG_MMU
        struct vm_area_struct *tail_vma;
+       struct mem_size_stats *mss;
 #endif
 #ifdef CONFIG_NUMA
        struct mempolicy *task_mempolicy;
fs/proc/task_mmu.c +126 -1
index 4648c7f..b61873e 100644
@@ -802,6 +802,81 @@ static int show_smap(struct seq_file *m, void *v, int is_pid)
        return 0;
 }
 
+static void add_smaps_sum(struct mem_size_stats *mss,
+               struct mem_size_stats *mss_sum)
+{
+       mss_sum->resident += mss->resident;
+       mss_sum->pss += mss->pss;
+       mss_sum->shared_clean += mss->shared_clean;
+       mss_sum->shared_dirty += mss->shared_dirty;
+       mss_sum->private_clean += mss->private_clean;
+       mss_sum->private_dirty += mss->private_dirty;
+       mss_sum->referenced += mss->referenced;
+       mss_sum->anonymous += mss->anonymous;
+       mss_sum->anonymous_thp += mss->anonymous_thp;
+       mss_sum->swap += mss->swap;
+}
+
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+       struct proc_maps_private *priv = m->private;
+       struct mm_struct *mm;
+       struct vm_area_struct *vma;
+       struct mem_size_stats *mss_sum = priv->mss;
+
+       /* reference to priv->task already taken */
+       /* but need to get the mm here because */
+       /* task could be in the process of exiting */
+       mm = get_task_mm(priv->task);
+       if (!mm || IS_ERR(mm))
+               return -EINVAL;
+
+       down_read(&mm->mmap_sem);
+       hold_task_mempolicy(priv);
+
+       for (vma = mm->mmap; vma != priv->tail_vma; vma = vma->vm_next) {
+               struct mem_size_stats mss;
+               struct mm_walk smaps_walk = {
+                       .pmd_entry = smaps_pte_range,
+                       .mm = vma->vm_mm,
+                       .private = &mss,
+               };
+
+               if (vma->vm_mm && !is_vm_hugetlb_page(vma)) {
+                       memset(&mss, 0, sizeof(mss));
+                       walk_page_vma(vma, &smaps_walk);
+                       add_smaps_sum(&mss, mss_sum);
+               }
+       }
+       seq_printf(m,
+                  "Rss:            %8lu kB\n"
+                  "Pss:            %8lu kB\n"
+                  "Shared_Clean:   %8lu kB\n"
+                  "Shared_Dirty:   %8lu kB\n"
+                  "Private_Clean:  %8lu kB\n"
+                  "Private_Dirty:  %8lu kB\n"
+                  "Referenced:     %8lu kB\n"
+                  "Anonymous:      %8lu kB\n"
+                  "AnonHugePages:  %8lu kB\n"
+                  "Swap:           %8lu kB\n",
+                  mss_sum->resident >> 10,
+                  (unsigned long)(mss_sum->pss >> (10 + PSS_SHIFT)),
+                  mss_sum->shared_clean  >> 10,
+                  mss_sum->shared_dirty  >> 10,
+                  mss_sum->private_clean >> 10,
+                  mss_sum->private_dirty >> 10,
+                  mss_sum->referenced >> 10,
+                  mss_sum->anonymous >> 10,
+                  mss_sum->anonymous_thp >> 10,
+                  mss_sum->swap >> 10);
+
+       release_task_mempolicy(priv);
+       up_read(&mm->mmap_sem);
+       mmput(mm);
+
+       return 0;
+}
+
 static int show_pid_smap(struct seq_file *m, void *v)
 {
        return show_smap(m, v, 1);
@@ -836,6 +911,50 @@ static int tid_smaps_open(struct inode *inode, struct file *file)
        return do_maps_open(inode, file, &proc_tid_smaps_op);
 }
 
+static int totmaps_open(struct inode *inode, struct file *file)
+{
+       struct proc_maps_private *priv;
+       int ret = -ENOMEM;
+       priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+       if (priv) {
+               priv->mss = kzalloc(sizeof(*priv->mss), GFP_KERNEL);
+               if (!priv->mss)
+                       return -ENOMEM;
+
+               /* we need to grab references to the task_struct */
+               /* at open time, because there's a potential information */
+               /* leak where the totmaps file is opened and held open */
+               /* while the underlying pid to task mapping changes */
+               /* underneath it */
+               priv->task = get_pid_task(proc_pid(inode), PIDTYPE_PID);
+               if (!priv->task) {
+                       kfree(priv->mss);
+                       kfree(priv);
+                       return -ESRCH;
+               }
+
+               ret = single_open(file, totmaps_proc_show, priv);
+               if (ret) {
+                       put_task_struct(priv->task);
+                       kfree(priv->mss);
+                       kfree(priv);
+               }
+       }
+       return ret;
+}
+
+static int totmaps_release(struct inode *inode, struct file *file)
+{
+       struct seq_file *m = file->private_data;
+       struct proc_maps_private *priv = m->private;
+
+       put_task_struct(priv->task);
+       kfree(priv->mss);
+       kfree(priv);
+       m->private = NULL;
+       return single_release(inode, file);
+}
+
 const struct file_operations proc_pid_smaps_operations = {
        .open           = pid_smaps_open,
        .read           = seq_read,
@@ -850,6 +969,13 @@ const struct file_operations proc_tid_smaps_operations = {
        .release        = proc_map_release,
 };
 
+const struct file_operations proc_totmaps_operations = {
+       .open           = totmaps_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = totmaps_release,
+};
+
 enum clear_refs_types {
        CLEAR_REFS_ALL = 1,
        CLEAR_REFS_ANON,
--

2.7.4

#10 lore raw
Quoted 12 lines · Konstantin Khlebnikov
Quoted 10 lines · On Tue, Aug 9, 2016 at 7:05 PM, <[email protected]> wrote:

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

Quoted 4 lines
Quoted 2 lines

This new proc file provides information faster than parsing the potentially huge smaps file.

What statistics do you really need?

PSS (Proportional Set Size) and related accounting of shared pages (swap could be shared) is where the existing summaries of memory usage are cumbersome.

Quoted 5 lines

I think, performance and flexibility issues could be really solved only by new syscall for querying memory statistics for address range in any process:
process_vm_stat() or some kind of pumped fincore() for /proc/$pid/mem

That would be a good long term solution if people want similarly complicated statistics without having to iterate through current interfaces.
I mentioned monitoring before but I'll add that Proportional Set size, Unique Set Size, Swap are per process are also useful because they help us make better decisions about what processes need to be throttled or gracefully killed.

Quoted 201 lines
Quoted 201 lines
---
 fs/proc/base.c     |   1 +
 fs/proc/internal.h |   4 ++
 fs/proc/task_mmu.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 131 insertions(+)
fs/proc/base.c +1 -0
index a11eb71..de3acdf 100644
@@ -2855,6 +2855,7 @@ static const struct pid_entry tgid_base_stuff[] = {
        REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
        REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
        REG("pagemap",    S_IRUSR, proc_pagemap_operations),
+       REG("totmaps",    S_IRUGO, proc_totmaps_operations),
 #endif
 #ifdef CONFIG_SECURITY
        DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
fs/proc/internal.h +4 -0
index aa27810..6f3540f 100644
@@ -58,6 +58,9 @@ union proc_op {
                struct task_struct *task);
 };
 
+
+extern const struct file_operations proc_totmaps_operations;
+
 struct proc_inode {
        struct pid *pid;
        int fd;
@@ -281,6 +284,7 @@ struct proc_maps_private {
        struct mm_struct *mm;
 #ifdef CONFIG_MMU
        struct vm_area_struct *tail_vma;
+       struct mem_size_stats *mss;
 #endif
 #ifdef CONFIG_NUMA
        struct mempolicy *task_mempolicy;
fs/proc/task_mmu.c +126 -1
index 4648c7f..b61873e 100644
@@ -802,6 +802,81 @@ static int show_smap(struct seq_file *m, void *v, int is_pid)
        return 0;
 }
 
+static void add_smaps_sum(struct mem_size_stats *mss,
+               struct mem_size_stats *mss_sum)
+{
+       mss_sum->resident += mss->resident;
+       mss_sum->pss += mss->pss;
+       mss_sum->shared_clean += mss->shared_clean;
+       mss_sum->shared_dirty += mss->shared_dirty;
+       mss_sum->private_clean += mss->private_clean;
+       mss_sum->private_dirty += mss->private_dirty;
+       mss_sum->referenced += mss->referenced;
+       mss_sum->anonymous += mss->anonymous;
+       mss_sum->anonymous_thp += mss->anonymous_thp;
+       mss_sum->swap += mss->swap;
+}
+
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+       struct proc_maps_private *priv = m->private;
+       struct mm_struct *mm;
+       struct vm_area_struct *vma;
+       struct mem_size_stats *mss_sum = priv->mss;
+
+       /* reference to priv->task already taken */
+       /* but need to get the mm here because */
+       /* task could be in the process of exiting */
+       mm = get_task_mm(priv->task);
+       if (!mm || IS_ERR(mm))
+               return -EINVAL;
+
+       down_read(&mm->mmap_sem);
+       hold_task_mempolicy(priv);
+
+       for (vma = mm->mmap; vma != priv->tail_vma; vma = vma->vm_next) {
+               struct mem_size_stats mss;
+               struct mm_walk smaps_walk = {
+                       .pmd_entry = smaps_pte_range,
+                       .mm = vma->vm_mm,
+                       .private = &mss,
+               };
+
+               if (vma->vm_mm && !is_vm_hugetlb_page(vma)) {
+                       memset(&mss, 0, sizeof(mss));
+                       walk_page_vma(vma, &smaps_walk);
+                       add_smaps_sum(&mss, mss_sum);
+               }
+       }
+       seq_printf(m,
+                  "Rss:            %8lu kB\n"
+                  "Pss:            %8lu kB\n"
+                  "Shared_Clean:   %8lu kB\n"
+                  "Shared_Dirty:   %8lu kB\n"
+                  "Private_Clean:  %8lu kB\n"
+                  "Private_Dirty:  %8lu kB\n"
+                  "Referenced:     %8lu kB\n"
+                  "Anonymous:      %8lu kB\n"
+                  "AnonHugePages:  %8lu kB\n"
+                  "Swap:           %8lu kB\n",
+                  mss_sum->resident >> 10,
+                  (unsigned long)(mss_sum->pss >> (10 + PSS_SHIFT)),
+                  mss_sum->shared_clean  >> 10,
+                  mss_sum->shared_dirty  >> 10,
+                  mss_sum->private_clean >> 10,
+                  mss_sum->private_dirty >> 10,
+                  mss_sum->referenced >> 10,
+                  mss_sum->anonymous >> 10,
+                  mss_sum->anonymous_thp >> 10,
+                  mss_sum->swap >> 10);
+
+       release_task_mempolicy(priv);
+       up_read(&mm->mmap_sem);
+       mmput(mm);
+
+       return 0;
+}
+
 static int show_pid_smap(struct seq_file *m, void *v)
 {
        return show_smap(m, v, 1);
@@ -836,6 +911,50 @@ static int tid_smaps_open(struct inode *inode, struct file *file)
        return do_maps_open(inode, file, &proc_tid_smaps_op);
 }
 
+static int totmaps_open(struct inode *inode, struct file *file)
+{
+       struct proc_maps_private *priv;
+       int ret = -ENOMEM;
+       priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+       if (priv) {
+               priv->mss = kzalloc(sizeof(*priv->mss), GFP_KERNEL);
+               if (!priv->mss)
+                       return -ENOMEM;
+
+               /* we need to grab references to the task_struct */
+               /* at open time, because there's a potential information */
+               /* leak where the totmaps file is opened and held open */
+               /* while the underlying pid to task mapping changes */
+               /* underneath it */
+               priv->task = get_pid_task(proc_pid(inode), PIDTYPE_PID);
+               if (!priv->task) {
+                       kfree(priv->mss);
+                       kfree(priv);
+                       return -ESRCH;
+               }
+
+               ret = single_open(file, totmaps_proc_show, priv);
+               if (ret) {
+                       put_task_struct(priv->task);
+                       kfree(priv->mss);
+                       kfree(priv);
+               }
+       }
+       return ret;
+}
+
+static int totmaps_release(struct inode *inode, struct file *file)
+{
+       struct seq_file *m = file->private_data;
+       struct proc_maps_private *priv = m->private;
+
+       put_task_struct(priv->task);
+       kfree(priv->mss);
+       kfree(priv);
+       m->private = NULL;
+       return single_release(inode, file);
+}
+
 const struct file_operations proc_pid_smaps_operations = {
        .open           = pid_smaps_open,
        .read           = seq_read,
@@ -850,6 +969,13 @@ const struct file_operations proc_tid_smaps_operations = {
        .release        = proc_map_release,
 };
 
+const struct file_operations proc_totmaps_operations = {
+       .open           = totmaps_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = totmaps_release,
+};
+
 enum clear_refs_types {
        CLEAR_REFS_ALL = 1,
        CLEAR_REFS_ANON,
--

2.7.4

#11 lore raw
In reply to [email protected]
Quoted 19 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

This new proc file provides information faster than parsing the potentially huge smaps file.

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 829

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+	struct proc_maps_private *priv = m->private;
+	struct mm_struct *mm;
+	struct vm_area_struct *vma;
+	struct mem_size_stats *mss_sum = priv->mss;
+
+	/* reference to priv->task already taken */
+	/* but need to get the mm here because */
+	/* task could be in the process of exiting */

Can you please elaborate on this? My understanding here is that you intend for the caller to be able to repeatedly read the same totmaps file with pread() and still see updated information after the target process has called execve() and be able to detect process death (instead of simply seeing stale values). Is that accurate?

I would prefer it if you could grab a reference to the mm_struct directly at open time.

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 832

Quoted 3 lines
fs/proc/task_mmu.c +3 -0
+	mm = get_task_mm(priv->task);
+	if (!mm || IS_ERR(mm))
+		return -EINVAL;

get_task_mm() doesn't return error codes, and all other callers just check whether the return value is NULL.

Quoted 7 lines
fs/proc/task_mmu.c +7 -0
+	down_read(&mm->mmap_sem);
+	hold_task_mempolicy(priv);
+
+	for (vma = mm->mmap; vma != priv->tail_vma; vma = vma->vm_next) {
+		struct mem_size_stats mss;
+		struct mm_walk smaps_walk = {
+			.pmd_entry = smaps_pte_range,

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 850

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+			.mm = vma->vm_mm,
+			.private = &mss,
+		};
+
+		if (vma->vm_mm && !is_vm_hugetlb_page(vma)) {
+			memset(&mss, 0, sizeof(mss));
+			walk_page_vma(vma, &smaps_walk);
+			add_smaps_sum(&mss, mss_sum);
+		}
+	}

Errrr... what? You accumulate values from mem_size_stats items into a struct mss_sum that is associated with the struct file? So when you read the file the second time, you get the old values plus the new ones? And when you read the file in parallel, you get inconsistent values?

For most files in procfs, the behavior is that you can just call pread(fd, buf, sizeof(buf), 0) on the same fd again and again, giving you the current values every time, without mutating state. I strongly recommend that you get rid of priv->mss and just accumulate the state in a local variable (maybe one on the stack).

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 922

Quoted 13 lines
fs/proc/task_mmu.c +9 -0
@@ -836,6 +911,50 @@ static int tid_smaps_open(struct inode *inode, struct file *file)
 	return do_maps_open(inode, file, &proc_tid_smaps_op);
 }
 
+static int totmaps_open(struct inode *inode, struct file *file)
+{
+	struct proc_maps_private *priv;
+	int ret = -ENOMEM;
+	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+	if (priv) {
+		priv->mss = kzalloc(sizeof(*priv->mss), GFP_KERNEL);
+		if (!priv->mss)
+			return -ENOMEM;

Memory leak: If the first allocation works and the second one doesn't, this doesn't free the first allocation.

Please change this to use the typical goto pattern for error handling.

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 928

Quoted 6 lines
fs/proc/task_mmu.c +6 -0
+
+		/* we need to grab references to the task_struct */
+		/* at open time, because there's a potential information */
+		/* leak where the totmaps file is opened and held open */
+		/* while the underlying pid to task mapping changes */
+		/* underneath it */

Nit: That's not how comments are done in the kernel. Maybe change this to a normal block comment instead of one block comment per line?

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 929

Quoted 1 line
fs/proc/task_mmu.c +1 -0
+		priv->task = get_pid_task(proc_pid(inode), PIDTYPE_PID);

get_pid_task(proc_pid(inode), PIDTYPE_PID) is exactly the definition of get_proc_task(inode), maybe use that instead?

Quoted 5 lines
fs/proc/task_mmu.c +5 -0
+		if (!priv->task) {
+			kfree(priv->mss);
+			kfree(priv);
+			return -ESRCH;
+		}

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 944

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+
+		ret = single_open(file, totmaps_proc_show, priv);
+		if (ret) {
+			put_task_struct(priv->task);
+			kfree(priv->mss);
+			kfree(priv);
+		}
+	}
+	return ret;
+}

Please change this method to use the typical goto pattern for error handling. IMO repeating the undo steps in all error cases makes mistakes (like the one above) more likely and increases the amount of redundant code.

Also: The smaps file is only accessible to callers with PTRACE_MODE_READ privileges on the target task. Your thing doesn't do any access checks, neither in the open handler nor in the read handler. Can you give an analysis of why it's okay to expose this data? As far as I can tell, without spending a lot of time thinking about it, this kind of data looks like it might potentially be useful for side-channel information leaks or so.

#12 lore raw
Quoted 37 lines · Jann Horn
Quoted 19 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

This new proc file provides information faster than parsing the potentially huge smaps file.

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 829

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+	struct proc_maps_private *priv = m->private;
+	struct mm_struct *mm;
+	struct vm_area_struct *vma;
+	struct mem_size_stats *mss_sum = priv->mss;
+
+	/* reference to priv->task already taken */
+	/* but need to get the mm here because */
+	/* task could be in the process of exiting */

Can you please elaborate on this? My understanding here is that you intend for the caller to be able to repeatedly read the same totmaps file with pread() and still see updated information after the target process has called execve() and be able to detect process death (instead of simply seeing stale values). Is that accurate?

Quoted 2 lines

I would prefer it if you could grab a reference to the mm_struct directly at open time.

Sonny, do you know more about the above comment?

Quoted 9 lines

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 832

Quoted 5 lines
fs/proc/task_mmu.c +3 -0
+	mm = get_task_mm(priv->task);
+	if (!mm || IS_ERR(mm))
+		return -EINVAL;

get_task_mm() doesn't return error codes, and all other callers just check whether the return value is NULL.

I'll have that fixed in v2, thanks for spotting it!

Quoted 24 lines
Quoted 8 lines
fs/proc/task_mmu.c +7 -0
+	down_read(&mm->mmap_sem);
+	hold_task_mempolicy(priv);
+
+	for (vma = mm->mmap; vma != priv->tail_vma; vma = vma->vm_next) {
+		struct mem_size_stats mss;
+		struct mm_walk smaps_walk = {
+			.pmd_entry = smaps_pte_range,

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 850

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+			.mm = vma->vm_mm,
+			.private = &mss,
+		};
+
+		if (vma->vm_mm && !is_vm_hugetlb_page(vma)) {
+			memset(&mss, 0, sizeof(mss));
+			walk_page_vma(vma, &smaps_walk);
+			add_smaps_sum(&mss, mss_sum);
+		}
+	}

Errrr... what? You accumulate values from mem_size_stats items into a struct mss_sum that is associated with the struct file? So when you read the file the second time, you get the old values plus the new ones? And when you read the file in parallel, you get inconsistent values?

Quoted 5 lines

For most files in procfs, the behavior is that you can just call pread(fd, buf, sizeof(buf), 0) on the same fd again and again, giving you the current values every time, without mutating state. I strongly recommend that you get rid of priv->mss and just accumulate the state in a local variable (maybe one on the stack).

So a simple "static struct mem_size_stats" in totmaps_proc_show() would be a better solution?

Quoted 19 lines
Quoted 5 lines
fs/proc/task_mmu.c +0 -0
@@ -836,6 +911,50 @@ static int tid_smaps_open(struct inode *inode, struct file *file)
 	return do_maps_open(inode, file, &proc_tid_smaps_op);
 }

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 922

Quoted 10 lines
fs/proc/task_mmu.c +9 -0
 
+static int totmaps_open(struct inode *inode, struct file *file)
+{
+	struct proc_maps_private *priv;
+	int ret = -ENOMEM;
+	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+	if (priv) {
+		priv->mss = kzalloc(sizeof(*priv->mss), GFP_KERNEL);
+		if (!priv->mss)
+			return -ENOMEM;

Memory leak: If the first allocation works and the second one doesn't, this doesn't free the first allocation.

Quoted 1 line

Please change this to use the typical goto pattern for error handling.

Fix will be implemented in v2.

Quoted 10 lines

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 928

Quoted 7 lines
fs/proc/task_mmu.c +6 -0
+
+		/* we need to grab references to the task_struct */
+		/* at open time, because there's a potential information */
+		/* leak where the totmaps file is opened and held open */
+		/* while the underlying pid to task mapping changes */
+		/* underneath it */

Nit: That's not how comments are done in the kernel. Maybe change this to a normal block comment instead of one block comment per line?

I'm not sure how that one slipped by, but I'll change it in v2.

Quoted 6 lines

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 929

Quoted 2 lines
fs/proc/task_mmu.c +1 -0
+		priv->task = get_pid_task(proc_pid(inode), PIDTYPE_PID);

get_pid_task(proc_pid(inode), PIDTYPE_PID) is exactly the definition of get_proc_task(inode), maybe use that instead?

Will do. v2 will fix this.

Quoted 12 lines
Quoted 12 lines
fs/proc/task_mmu.c +12 -0
+		if (!priv->task) {
+			kfree(priv->mss);
+			kfree(priv);
+			return -ESRCH;
+		}
+
+		ret = single_open(file, totmaps_proc_show, priv);
+		if (ret) {
+			put_task_struct(priv->task);
+			kfree(priv->mss);
+			kfree(priv);
+		}
Quoted 8 lines

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 944

Quoted 3 lines
fs/proc/task_mmu.c +3 -0
+	}
+	return ret;
+}

Please change this method to use the typical goto pattern for error handling. IMO repeating the undo steps in all error cases makes mistakes (like the one above) more likely and increases the amount of redundant code.

Agreed. Change queued for v2.

Quoted 9 lines

Also: The smaps file is only accessible to callers with PTRACE_MODE_READ privileges on the target task. Your thing doesn't do any access checks, neither in the open handler nor in the read handler. Can you give an analysis of why it's okay to expose this data? As far as I can tell, without spending a lot of time thinking about it, this kind of data looks like it might potentially be useful for side-channel information leaks or so.

I think it should require the same permissions as smaps, so changing the code to require PTRACE_MODE_READ privileges is most likely a good idea. I'll have a look at it for v2.

#13 lore raw
Quoted 25 lines · Robert Foss
Quoted 24 lines · Jann Horn
Quoted 7 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:
fs/proc/task_mmu.c +7 -0
+	down_read(&mm->mmap_sem);
+	hold_task_mempolicy(priv);
+
+	for (vma = mm->mmap; vma != priv->tail_vma; vma = vma->vm_next) {
+		struct mem_size_stats mss;
+		struct mm_walk smaps_walk = {
+			.pmd_entry = smaps_pte_range,

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 850

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+			.mm = vma->vm_mm,
+			.private = &mss,
+		};
+
+		if (vma->vm_mm && !is_vm_hugetlb_page(vma)) {
+			memset(&mss, 0, sizeof(mss));
+			walk_page_vma(vma, &smaps_walk);
+			add_smaps_sum(&mss, mss_sum);
+		}
+	}

Errrr... what? You accumulate values from mem_size_stats items into a struct mss_sum that is associated with the struct file? So when you read the file the second time, you get the old values plus the new ones? And when you read the file in parallel, you get inconsistent values?

Quoted 8 lines
Quoted 5 lines

For most files in procfs, the behavior is that you can just call pread(fd, buf, sizeof(buf), 0) on the same fd again and again, giving you the current values every time, without mutating state. I strongly recommend that you get rid of priv->mss and just accumulate the state in a local variable (maybe one on the stack).

So a simple "static struct mem_size_stats" in totmaps_proc_show() would be a better solution?

Er, why "static"? Are you trying to create shared state between different readers for some reason?

#14 lore raw
Quoted 32 lines · Jann Horn
Quoted 31 lines · Robert Foss
Quoted 29 lines · Jann Horn
Quoted 7 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:
fs/proc/task_mmu.c +7 -0
+	down_read(&mm->mmap_sem);
+	hold_task_mempolicy(priv);
+
+	for (vma = mm->mmap; vma != priv->tail_vma; vma = vma->vm_next) {
+		struct mem_size_stats mss;
+		struct mm_walk smaps_walk = {
+			.pmd_entry = smaps_pte_range,

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 850

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+			.mm = vma->vm_mm,
+			.private = &mss,
+		};
+
+		if (vma->vm_mm && !is_vm_hugetlb_page(vma)) {
+			memset(&mss, 0, sizeof(mss));
+			walk_page_vma(vma, &smaps_walk);
+			add_smaps_sum(&mss, mss_sum);
+		}
+	}

Errrr... what? You accumulate values from mem_size_stats items into a struct mss_sum that is associated with the struct file? So when you read the file the second time, you get the old values plus the new ones? And when you read the file in parallel, you get inconsistent values?

For most files in procfs, the behavior is that you can just call pread(fd, buf, sizeof(buf), 0) on the same fd again and again, giving you the current values every time, without mutating state. I strongly recommend that you get rid of priv->mss and just accumulate the state in a local variable (maybe one on the stack).

Quoted 6 lines
Quoted 2 lines

So a simple "static struct mem_size_stats" in totmaps_proc_show() would be a better solution?

Er, why "static"? Are you trying to create shared state between different readers for some reason?

I think I'm a bit confused now, how are you suggesting that I replace priv->mss?

#15 lore raw
Quoted 38 lines · Robert Foss
Quoted 35 lines · Jann Horn
Quoted 33 lines · Robert Foss
Quoted 24 lines · Jann Horn
Quoted 7 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:
fs/proc/task_mmu.c +7 -0
+	down_read(&mm->mmap_sem);
+	hold_task_mempolicy(priv);
+
+	for (vma = mm->mmap; vma != priv->tail_vma; vma = vma->vm_next) {
+		struct mem_size_stats mss;
+		struct mm_walk smaps_walk = {
+			.pmd_entry = smaps_pte_range,

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 850

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+			.mm = vma->vm_mm,
+			.private = &mss,
+		};
+
+		if (vma->vm_mm && !is_vm_hugetlb_page(vma)) {
+			memset(&mss, 0, sizeof(mss));
+			walk_page_vma(vma, &smaps_walk);
+			add_smaps_sum(&mss, mss_sum);
+		}
+	}

Errrr... what? You accumulate values from mem_size_stats items into a struct mss_sum that is associated with the struct file? So when you read the file the second time, you get the old values plus the new ones? And when you read the file in parallel, you get inconsistent values?

Quoted 5 lines

For most files in procfs, the behavior is that you can just call pread(fd, buf, sizeof(buf), 0) on the same fd again and again, giving you the current values every time, without mutating state. I strongly recommend that you get rid of priv->mss and just accumulate the state in a local variable (maybe one on the stack).

So a simple "static struct mem_size_stats" in totmaps_proc_show() would be a better solution?

Quoted 6 lines
Quoted 3 lines

Er, why "static"? Are you trying to create shared state between different readers for some reason?

I think I'm a bit confused now, how are you suggesting that I replace priv->mss?

Like this:

static int totmaps_proc_show(struct seq_file *m, void *data)

{
        struct proc_maps_private *priv = m->private;
        struct mm_struct *mm;
        struct vm_area_struct *vma;
        struct mem_size_stats mss_sum;

        memset(&mss_sum, 0, sizeof(mss_sum));

        [...]

        for (vma = mm->mmap; vma != priv->tail_vma; vma = vma->vm_next) {
                struct mem_size_stats mss;
                struct mm_walk smaps_walk = {
                        .pmd_entry = smaps_pte_range,
                        .mm = vma->vm_mm,
                        .private = &mss,
                };

                if (vma->vm_mm && !is_vm_hugetlb_page(vma)) {
                        memset(&mss, 0, sizeof(mss));
                        walk_page_vma(vma, &smaps_walk);
                        add_smaps_sum(&mss, &mss_sum);
                }
        }
        seq_printf(m,
                   "Rss:            %8lu kB\n"
                   "Pss:            %8lu kB\n"
                   "Shared_Clean:   %8lu kB\n"
                   [...],
                   mss_sum.resident >> 10,
                   (unsigned long)(mss_sum.pss >> (10 + PSS_SHIFT)),
                   mss_sum.shared_clean  >> 10,
                   [...]);
        [...]
}
#16 lore raw
Quoted 76 lines · Jann Horn
Quoted 38 lines · Robert Foss
Quoted 35 lines · Jann Horn
Quoted 33 lines · Robert Foss
Quoted 24 lines · Jann Horn
Quoted 7 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:
fs/proc/task_mmu.c +7 -0
+	down_read(&mm->mmap_sem);
+	hold_task_mempolicy(priv);
+
+	for (vma = mm->mmap; vma != priv->tail_vma; vma = vma->vm_next) {
+		struct mem_size_stats mss;
+		struct mm_walk smaps_walk = {
+			.pmd_entry = smaps_pte_range,

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 850

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+			.mm = vma->vm_mm,
+			.private = &mss,
+		};
+
+		if (vma->vm_mm && !is_vm_hugetlb_page(vma)) {
+			memset(&mss, 0, sizeof(mss));
+			walk_page_vma(vma, &smaps_walk);
+			add_smaps_sum(&mss, mss_sum);
+		}
+	}

Errrr... what? You accumulate values from mem_size_stats items into a struct mss_sum that is associated with the struct file? So when you read the file the second time, you get the old values plus the new ones? And when you read the file in parallel, you get inconsistent values?

Quoted 5 lines

For most files in procfs, the behavior is that you can just call pread(fd, buf, sizeof(buf), 0) on the same fd again and again, giving you the current values every time, without mutating state. I strongly recommend that you get rid of priv->mss and just accumulate the state in a local variable (maybe one on the stack).

So a simple "static struct mem_size_stats" in totmaps_proc_show() would be a better solution?

Quoted 6 lines
Quoted 3 lines

Er, why "static"? Are you trying to create shared state between different readers for some reason?

I think I'm a bit confused now, how are you suggesting that I replace priv->mss?

Like this:

static int totmaps_proc_show(struct seq_file *m, void *data)

{
        struct proc_maps_private *priv = m->private;
        struct mm_struct *mm;
        struct vm_area_struct *vma;
        struct mem_size_stats mss_sum;

        memset(&mss_sum, 0, sizeof(mss_sum));

        [...]

        for (vma = mm->mmap; vma != priv->tail_vma; vma = vma->vm_next) {
                struct mem_size_stats mss;
                struct mm_walk smaps_walk = {
                        .pmd_entry = smaps_pte_range,
                        .mm = vma->vm_mm,
                        .private = &mss,
                };

                if (vma->vm_mm && !is_vm_hugetlb_page(vma)) {
                        memset(&mss, 0, sizeof(mss));
                        walk_page_vma(vma, &smaps_walk);
                        add_smaps_sum(&mss, &mss_sum);
                }
        }
        seq_printf(m,
                   "Rss:            %8lu kB\n"
                   "Pss:            %8lu kB\n"
Quoted 9 lines
                   "Shared_Clean:   %8lu kB\n"
                   [...],
                   mss_sum.resident >> 10,
                   (unsigned long)(mss_sum.pss >> (10 + PSS_SHIFT)),
                   mss_sum.shared_clean  >> 10,
                   [...]);
        [...]
}

Thanks Jann for being really clear about this stuff. It is much appreciated!

#17 lore raw
In reply to Robert Foss
Quoted 45 lines · Robert Foss
Quoted 42 lines · Jann Horn
Quoted 22 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

This new proc file provides information faster than parsing the potentially
huge smaps file.

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 829

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+       struct proc_maps_private *priv = m->private;
+       struct mm_struct *mm;
+       struct vm_area_struct *vma;
+       struct mem_size_stats *mss_sum = priv->mss;
+
+       /* reference to priv->task already taken */
+       /* but need to get the mm here because */
+       /* task could be in the process of exiting */

Can you please elaborate on this? My understanding here is that you intend for the caller to be able to repeatedly read the same totmaps file with pread() and still see updated information after the target process has called execve() and be able to detect process death (instead of simply seeing stale values). Is that accurate?

Quoted 5 lines
Quoted 2 lines

I would prefer it if you could grab a reference to the mm_struct directly at open time.

Sonny, do you know more about the above comment?

I think right now the file gets re-opened every time, but the mode where the file is opened once and repeatedly read is interesting because it avoids having to open the file again and again.

I guess you could end up with a wierd situation where you don't read the entire contents of the file in open call to read() and you might get inconsistent data across the different statistics?

Quoted 141 lines
Quoted 11 lines

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 832

Quoted 6 lines
fs/proc/task_mmu.c +3 -0
+       mm = get_task_mm(priv->task);
+       if (!mm || IS_ERR(mm))
+               return -EINVAL;

get_task_mm() doesn't return error codes, and all other callers just check whether the return value is NULL.

I'll have that fixed in v2, thanks for spotting it!

Quoted 25 lines
Quoted 8 lines
fs/proc/task_mmu.c +7 -0
+       down_read(&mm->mmap_sem);
+       hold_task_mempolicy(priv);
+
+       for (vma = mm->mmap; vma != priv->tail_vma; vma = vma->vm_next) {
+               struct mem_size_stats mss;
+               struct mm_walk smaps_walk = {
+                       .pmd_entry = smaps_pte_range,

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 850

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+                       .mm = vma->vm_mm,
+                       .private = &mss,
+               };
+
+               if (vma->vm_mm && !is_vm_hugetlb_page(vma)) {
+                       memset(&mss, 0, sizeof(mss));
+                       walk_page_vma(vma, &smaps_walk);
+                       add_smaps_sum(&mss, mss_sum);
+               }
+       }

Errrr... what? You accumulate values from mem_size_stats items into a struct mss_sum that is associated with the struct file? So when you read the file the second time, you get the old values plus the new ones? And when you read the file in parallel, you get inconsistent values?

Quoted 5 lines

For most files in procfs, the behavior is that you can just call pread(fd, buf, sizeof(buf), 0) on the same fd again and again, giving you the current values every time, without mutating state. I strongly recommend that you get rid of priv->mss and just accumulate the state in a local variable (maybe one on the stack).

So a simple "static struct mem_size_stats" in totmaps_proc_show() would be a better solution?

Quoted 22 lines
Quoted 6 lines
fs/proc/task_mmu.c +0 -0
@@ -836,6 +911,50 @@ static int tid_smaps_open(struct inode *inode,
 struct file *file)
        return do_maps_open(inode, file, &proc_tid_smaps_op);
 }

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, · line 923

Quoted 10 lines
fs/proc/task_mmu.c +9 -0
 
+static int totmaps_open(struct inode *inode, struct file *file)
+{
+       struct proc_maps_private *priv;
+       int ret = -ENOMEM;
+       priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+       if (priv) {
+               priv->mss = kzalloc(sizeof(*priv->mss), GFP_KERNEL);
+               if (!priv->mss)
+                       return -ENOMEM;

Memory leak: If the first allocation works and the second one doesn't, this
doesn't free the first allocation.

Quoted 1 line

Please change this to use the typical goto pattern for error handling.

Fix will be implemented in v2.

Quoted 10 lines

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 928

Quoted 8 lines
fs/proc/task_mmu.c +6 -0
+
+               /* we need to grab references to the task_struct */
+               /* at open time, because there's a potential information
 */
+               /* leak where the totmaps file is opened and held open */
+               /* while the underlying pid to task mapping changes */
+               /* underneath it */
Quoted 2 lines

Nit: That's not how comments are done in the kernel. Maybe change this to a normal block comment instead of one block comment per line?

I'm not sure how that one slipped by, but I'll change it in v2.

Quoted 7 lines

fs/proc/task_mmu.c static int tid_smaps_open(struct inode *inode, struct file *file) · line 929

Quoted 2 lines
fs/proc/task_mmu.c +1 -0
+               priv->task = get_pid_task(proc_pid(inode), PIDTYPE_PID);

get_pid_task(proc_pid(inode), PIDTYPE_PID) is exactly the definition of get_proc_task(inode), maybe use that instead?

Will do. v2 will fix this.

Quoted 13 lines
Quoted 13 lines
fs/proc/task_mmu.c +13 -0
+               if (!priv->task) {
+                       kfree(priv->mss);
+                       kfree(priv);
+                       return -ESRCH;
+               }
+
+               ret = single_open(file, totmaps_proc_show, priv);
+               if (ret) {
+                       put_task_struct(priv->task);
+                       kfree(priv->mss);
+                       kfree(priv);
+               }
+       }
Quoted 8 lines

Review static int tid_smaps_open(struct inode *inode, · line 946

Quoted 2 lines
(quoted diff) +2 -0
+       return ret;
+}

Please change this method to use the typical goto pattern for error handling. IMO repeating the undo steps in all error cases makes mistakes (like the one above) more likely and increases the amount of redundant code.

Agreed. Change queued for v2.

Quoted 9 lines

Also: The smaps file is only accessible to callers with PTRACE_MODE_READ privileges on the target task. Your thing doesn't do any access checks, neither in the open handler nor in the read handler. Can you give an analysis of why it's okay to expose this data? As far as I can tell, without spending a lot of time thinking about it, this kind of data looks like it might potentially be useful for side-channel information leaks or so.

I think it should require the same permissions as smaps, so changing the code to require PTRACE_MODE_READ privileges is most likely a good idea. I'll have a look at it for v2.

#18 lore raw
Quoted 52 lines · Sonny Rao
Quoted 45 lines · Robert Foss
Quoted 42 lines · Jann Horn
Quoted 22 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

This new proc file provides information faster than parsing the potentially
huge smaps file.

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 829

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+       struct proc_maps_private *priv = m->private;
+       struct mm_struct *mm;
+       struct vm_area_struct *vma;
+       struct mem_size_stats *mss_sum = priv->mss;
+
+       /* reference to priv->task already taken */
+       /* but need to get the mm here because */
+       /* task could be in the process of exiting */

Can you please elaborate on this? My understanding here is that you intend for the caller to be able to repeatedly read the same totmaps file with pread() and still see updated information after the target process has called execve() and be able to detect process death (instead of simply seeing stale values). Is that accurate?

Quoted 5 lines
Quoted 2 lines

I would prefer it if you could grab a reference to the mm_struct directly at open time.

Sonny, do you know more about the above comment?

Quoted 7 lines

I think right now the file gets re-opened every time, but the mode where the file is opened once and repeatedly read is interesting because it avoids having to open the file again and again.

I guess you could end up with a wierd situation where you don't read the entire contents of the file in open call to read() and you might get inconsistent data across the different statistics?

If the file is read in two chunks, totmaps_proc_show is only called once. The patch specifies seq_read as read handler. Have a look at its definition. As long as you don't read from the same seq file in parallel or seek around in it, simple sequential reads will not re-invoke the show() method for data that has already been formatted. For partially consumed data, the kernel buffers the rest until someone reads it or seeks to another offset.

#19 lore raw
Quoted 61 lines · Jann Horn
Quoted 52 lines · Sonny Rao
Quoted 45 lines · Robert Foss
Quoted 42 lines · Jann Horn
Quoted 22 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

This new proc file provides information faster than parsing the potentially
huge smaps file.

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 829

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+       struct proc_maps_private *priv = m->private;
+       struct mm_struct *mm;
+       struct vm_area_struct *vma;
+       struct mem_size_stats *mss_sum = priv->mss;
+
+       /* reference to priv->task already taken */
+       /* but need to get the mm here because */
+       /* task could be in the process of exiting */

Can you please elaborate on this? My understanding here is that you intend for the caller to be able to repeatedly read the same totmaps file with pread() and still see updated information after the target process has called execve() and be able to detect process death (instead of simply seeing stale values). Is that accurate?

Quoted 5 lines
Quoted 2 lines

I would prefer it if you could grab a reference to the mm_struct directly at open time.

Sonny, do you know more about the above comment?

Quoted 7 lines

I think right now the file gets re-opened every time, but the mode where the file is opened once and repeatedly read is interesting because it avoids having to open the file again and again.

I guess you could end up with a wierd situation where you don't read the entire contents of the file in open call to read() and you might get inconsistent data across the different statistics?

Quoted 7 lines

If the file is read in two chunks, totmaps_proc_show is only called once. The patch specifies seq_read as read handler. Have a look at its definition. As long as you don't read from the same seq file in parallel or seek around in it, simple sequential reads will not re-invoke the show() method for data that has already been formatted. For partially consumed data, the kernel buffers the rest until someone reads it or seeks to another offset.

Ok that's good. If the consumer were using pread() though, would that look like a seek?

#20 lore raw
Quoted 70 lines · Sonny Rao
Quoted 61 lines · Jann Horn
Quoted 52 lines · Sonny Rao
Quoted 45 lines · Robert Foss
Quoted 42 lines · Jann Horn
Quoted 22 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

This new proc file provides information faster than parsing the potentially
huge smaps file.

fs/proc/task_mmu.c static int show_smap(struct seq_file *m, void *v, int is_pid) · line 829

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+       struct proc_maps_private *priv = m->private;
+       struct mm_struct *mm;
+       struct vm_area_struct *vma;
+       struct mem_size_stats *mss_sum = priv->mss;
+
+       /* reference to priv->task already taken */
+       /* but need to get the mm here because */
+       /* task could be in the process of exiting */

Can you please elaborate on this? My understanding here is that you intend for the caller to be able to repeatedly read the same totmaps file with pread() and still see updated information after the target process has called execve() and be able to detect process death (instead of simply seeing stale values). Is that accurate?

Quoted 5 lines
Quoted 2 lines

I would prefer it if you could grab a reference to the mm_struct directly at open time.

Sonny, do you know more about the above comment?

Quoted 7 lines

I think right now the file gets re-opened every time, but the mode where the file is opened once and repeatedly read is interesting because it avoids having to open the file again and again.

I guess you could end up with a wierd situation where you don't read the entire contents of the file in open call to read() and you might get inconsistent data across the different statistics?

Quoted 7 lines

If the file is read in two chunks, totmaps_proc_show is only called once. The patch specifies seq_read as read handler. Have a look at its definition. As long as you don't read from the same seq file in parallel or seek around in it, simple sequential reads will not re-invoke the show() method for data that has already been formatted. For partially consumed data, the kernel buffers the rest until someone reads it or seeks to another offset.

Quoted 2 lines

Ok that's good. If the consumer were using pread() though, would that look like a seek?

Only if the consumer uses pread() with an offset that is not the same as the end offset of the previous read.

So if you tried to use the same file from multiple threads in parallel, you might still have issues, but as long as you don't do that, it should be fine.

I guess it might make sense to document this behavior somewhere - maybe the proc.5 manpage?

#21 lore raw
Quoted 77 lines · Jann Horn
Quoted 70 lines · Sonny Rao
Quoted 61 lines · Jann Horn
Quoted 52 lines · Sonny Rao
Quoted 45 lines · Robert Foss
Quoted 42 lines · Jann Horn
Quoted 22 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400, [email protected] wrote:

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

This new proc file provides information faster than parsing the potentially
huge smaps file.

Review

Quoted 10 lines
(quoted diff) +10 -0
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+       struct proc_maps_private *priv = m->private;
+       struct mm_struct *mm;
+       struct vm_area_struct *vma;
+       struct mem_size_stats *mss_sum = priv->mss;
+
+       /* reference to priv->task already taken */
+       /* but need to get the mm here because */
+       /* task could be in the process of exiting */

Can you please elaborate on this? My understanding here is that you intend for the caller to be able to repeatedly read the same totmaps file with pread() and still see updated information after the target process has called execve() and be able to detect process death (instead of simply seeing stale values). Is that accurate?

Quoted 5 lines
Quoted 2 lines

I would prefer it if you could grab a reference to the mm_struct directly at open time.

Sonny, do you know more about the above comment?

Quoted 7 lines

I think right now the file gets re-opened every time, but the mode where the file is opened once and repeatedly read is interesting because it avoids having to open the file again and again.

I guess you could end up with a wierd situation where you don't read the entire contents of the file in open call to read() and you might get inconsistent data across the different statistics?

Quoted 7 lines

If the file is read in two chunks, totmaps_proc_show is only called once. The patch specifies seq_read as read handler. Have a look at its definition. As long as you don't read from the same seq file in parallel or seek around in it, simple sequential reads will not re-invoke the show() method for data that has already been formatted. For partially consumed data, the kernel buffers the rest until someone reads it or seeks to another offset.

Quoted 2 lines

Ok that's good. If the consumer were using pread() though, would that look like a seek?

Only if the consumer uses pread() with an offset that is not the same as the end offset of the previous read.

Quoted 7 lines

So if you tried to use the same file from multiple threads in parallel, you might still have issues, but as long as you don't do that, it should be fine.

I guess it might make sense to document this behavior somewhere - maybe the proc.5 manpage?

I'll add a note about limitations for parallel read. The overall documentation for this feature should live in the proc.5 manpage as well?

#22 lore raw
Quoted 84 lines · Robert Foss
Quoted 81 lines · Jann Horn
Quoted 70 lines · Sonny Rao
Quoted 61 lines · Jann Horn
Quoted 52 lines · Sonny Rao
Quoted 45 lines · Robert Foss
Quoted 42 lines · Jann Horn
Quoted 22 lines · On Tue, Aug 09, 2016 at 12:05:43PM -0400…

From: Sonny Rao <[email protected]>

This is based on earlier work by Thiago Goncales. It implements a new per process proc file which summarizes the contents of the smaps file but doesn't display any addresses. It gives more detailed information than statm like the PSS (proprotional set size). It differs from the original implementation in that it doesn't use the full blown set of seq operations, uses a different termination condition, and doesn't displayed "Locked" as that was broken on the original implemenation.

This new proc file provides information faster than parsing the potentially
huge smaps file.

Review

Quoted 10 lines
(quoted diff) +10 -0
+static int totmaps_proc_show(struct seq_file *m, void *data)
+{
+       struct proc_maps_private *priv = m->private;
+       struct mm_struct *mm;
+       struct vm_area_struct *vma;
+       struct mem_size_stats *mss_sum = priv->mss;
+
+       /* reference to priv->task already taken */
+       /* but need to get the mm here because */
+       /* task could be in the process of exiting */

Can you please elaborate on this? My understanding here is that you intend for the caller to be able to repeatedly read the same totmaps file with pread() and still see updated information after the target process has called execve() and be able to detect process death (instead of simply seeing stale values). Is that accurate?

Quoted 5 lines
Quoted 2 lines

I would prefer it if you could grab a reference to the mm_struct directly at open time.

Sonny, do you know more about the above comment?

Quoted 7 lines

I think right now the file gets re-opened every time, but the mode where the file is opened once and repeatedly read is interesting because it avoids having to open the file again and again.

I guess you could end up with a wierd situation where you don't read the entire contents of the file in open call to read() and you might get inconsistent data across the different statistics?

Quoted 7 lines

If the file is read in two chunks, totmaps_proc_show is only called once. The patch specifies seq_read as read handler. Have a look at its definition. As long as you don't read from the same seq file in parallel or seek around in it, simple sequential reads will not re-invoke the show() method for data that has already been formatted. For partially consumed data, the kernel buffers the rest until someone reads it or seeks to another offset.

Quoted 2 lines

Ok that's good. If the consumer were using pread() though, would that look like a seek?

Only if the consumer uses pread() with an offset that is not the same as the end offset of the previous read.

So if you tried to use the same file from multiple threads in parallel, you might still have issues, but as long as you don't do that, it should be fine.

Quoted 6 lines
Quoted 3 lines

I guess it might make sense to document this behavior somewhere - maybe the proc.5 manpage?

I'll add a note about limitations for parallel read. The overall documentation for this feature should live in the proc.5 manpage as well?

Yes, I think so.

+Cc [email protected]