Skip to contentSkip to conversation

Implement /proc/<pid>/totmaps

PATCH Superseded [email protected] 2 participants 9 messages Aug 16, 2016

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Implement /proc/<pid>/totmaps

From: Robert Foss <[email protected]>

This series provides the /proc/PID/totmaps feature, which summarizes the information provided by /proc/PID/smaps for improved performance and usability reasons.

A 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.

/proc/PID/totmaps provides roughly a 2x speedup compared to parsing /proc/PID/smaps with awk.

$ /usr/bin/time -v -p zsh -c "(repeat 25 {cat /proc/5025/totmaps})"
[...]
        Command being timed: "zsh -c (repeat 25 {cat /proc/5025/totmaps})"
        User time (seconds): 0.00
        System time (seconds): 0.40
        Percent of CPU this job got: 90%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.45
$ /usr/bin/time -v -p zsh -c "repeat 25 { awk '/^Rss/{rss+=\$2} /^Pss/{pss+=\$2} END {printf \"rss:%d pss:%d\n\", rss, pss}\' /proc/5025/smaps}"
[...]
        Command being timed: "zsh -c repeat 25 { awk '/^Rss/{rss+=$2}
/^Pss/{pss+=$2} END {printf "rss:%d pss:%d\n", rss, pss}\' /proc/5025/smaps }"
        User time (seconds): 0.37
        System time (seconds): 0.45
        Percent of CPU this job got: 92%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.89

Robert Foss (3):
mm, proc: Implement /proc/<pid>/totmaps Documentation/filesystems: Fixed typo Documentation/filesystems: Added /proc/PID/totmaps documentation

 Documentation/filesystems/proc.txt |  23 ++++++-
 fs/proc/base.c                     |   1 +
 fs/proc/internal.h                 |   2 +
 fs/proc/task_mmu.c                 | 129 +++++++++++++++++++++++++++++++++++++
 4 files changed, 154 insertions(+), 1 deletion(-)
Signature · 1 line
2.7.4
#1 lore raw

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

From: Robert Foss <[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 |   2 +
 fs/proc/task_mmu.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 132 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 +2 -0
index aa27810..99f97d7 100644
@@ -297,6 +297,8 @@ extern const struct file_operations proc_pid_smaps_operations;
 extern const struct file_operations proc_tid_smaps_operations;
 extern const struct file_operations proc_clear_refs_operations;
 extern const struct file_operations proc_pagemap_operations;
+extern const struct file_operations proc_totmaps_operations;
+
 
 extern unsigned long task_vsize(struct mm_struct *);
 extern unsigned long task_statm(struct mm_struct *,
fs/proc/task_mmu.c +129 -0

153 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

Re: [PACTH v3 1/3] mm, proc: Implement /proc/<pid>/totmaps

Quoted 18 lines · On Tue, Aug 16, 2016 at 01:34:14PM -0400, [email protected] wrote:

From: Robert Foss <[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.

---

[...]

Quoted 13 lines
fs/proc/task_mmu.c +13 -0
+static int totmaps_open(struct inode *inode, struct file *file)
+{
+	struct proc_maps_private *priv = NULL;
+	struct seq_file *seq;
+	int ret;
+
+	ret = do_maps_open(inode, file, &proc_totmaps_op);
+	if (ret)
+		goto error;
+
+	/*
+	 * We need to grab references to the task_struct
+	 * at open time, because there's a potential information

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

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+	 * leak where the totmaps file is opened and held open
+	 * while the underlying pid to task mapping changes
+	 * underneath it
+	 */
+	seq = file->private_data;
+	priv = seq->private;
+	priv->task = get_proc_task(inode);
+	if (!priv->task) {
+		ret = -ESRCH;
+		goto error;

I see that you removed the proc_map_release() call for the upper error case as I recommended. However, for the second error case, you do have to call it because do_maps_open() succeeded.

You could fix this by turning the first "goto error;" into "return;" and adding the proc_map_release() call back in after the "error:" label. This would be fine - if an error branch just needs to return an error code, it's okay to do so directly without jumping to an error label.

Alternatively, you could add a second label
in front of the existing "error:" label, jump to the new label for the second error case, and call proc_map_release() between the new label and the old one.

Quoted 8 lines
fs/proc/task_mmu.c +8 -0
+	}
+
+	return 0;
+
+error:
+	return ret;
+}
+

[...]

fs/proc/task_mmu.c const struct file_operations proc_tid_smaps_operations = { · line 980

Quoted 6 lines
fs/proc/task_mmu.c +6 -0
+const struct file_operations proc_totmaps_operations = {
+	.open		= totmaps_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= proc_map_release,
+};

As I said regarding v2 already:
This won't release priv->task, causing a memory leak (exploitable through a reference counter overflow of the task_struct usage counter).

#3 lore raw

Re: [PACTH v3 1/3] mm, proc: Implement /proc/<pid>/totmaps

Quoted 54 lines · Jann Horn
Quoted 18 lines · On Tue, Aug 16, 2016 at 01:34:14PM -0400…

From: Robert Foss <robert.foss-ZGY8ohtN/[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.

---

[...]

Quoted 13 lines
fs/proc/task_mmu.c +13 -0
+static int totmaps_open(struct inode *inode, struct file *file)
+{
+	struct proc_maps_private *priv = NULL;
+	struct seq_file *seq;
+	int ret;
+
+	ret = do_maps_open(inode, file, &proc_totmaps_op);
+	if (ret)
+		goto error;
+
+	/*
+	 * We need to grab references to the task_struct
+	 * at open time, because there's a potential information

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

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+	 * leak where the totmaps file is opened and held open
+	 * while the underlying pid to task mapping changes
+	 * underneath it
+	 */
+	seq = file->private_data;
+	priv = seq->private;
+	priv->task = get_proc_task(inode);
+	if (!priv->task) {
+		ret = -ESRCH;
+		goto error;

I see that you removed the proc_map_release() call for the upper error case as I recommended. However, for the second error case, you do have to call it because do_maps_open() succeeded.

You could fix this by turning the first "goto error;" into "return;" and adding the proc_map_release() call back in after the "error:" label. This would be fine - if an error branch just needs to return an error code, it's okay to do so directly without jumping to an error label.

Quoted 4 lines

Alternatively, you could add a second label
in front of the existing "error:" label, jump to the new label for the second error case, and call proc_map_release() between the new label and the old one.

Ah, naturally. Thanks for the patience and advice!

Quoted 11 lines
Quoted 10 lines
fs/proc/task_mmu.c +8 -0
+	}
+
+	return 0;
+
+error:
+	return ret;
+}
+

[...]

Quoted 12 lines

fs/proc/task_mmu.c const struct file_operations proc_tid_smaps_operations = { · line 980

Quoted 6 lines
fs/proc/task_mmu.c +6 -0
+const struct file_operations proc_totmaps_operations = {
+	.open		= totmaps_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= proc_map_release,
+};

As I said regarding v2 already:
This won't release priv->task, causing a memory leak (exploitable through a reference counter overflow of the task_struct usage counter).

Sorry about dropping the ball on that one, what's correct way to release priv->task?

#4 lore raw

Re: [PACTH v3 1/3] mm, proc: Implement /proc/<pid>/totmaps

Quoted 23 lines · Robert Foss
Quoted 10 lines · Jann Horn
Quoted 8 lines · On Tue, Aug 16, 2016 at 01:34:14PM -0400, [email protected] wrote:
fs/proc/task_mmu.c +8 -0
+	}
+
+	return 0;
+
+error:
+	return ret;
+}
+

[...]

Quoted 11 lines

fs/proc/task_mmu.c const struct file_operations proc_tid_smaps_operations = { · line 980

Quoted 6 lines
fs/proc/task_mmu.c +6 -0
+const struct file_operations proc_totmaps_operations = {
+	.open		= totmaps_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= proc_map_release,
+};

As I said regarding v2 already:
This won't release priv->task, causing a memory leak (exploitable through a reference counter overflow of the task_struct usage counter).

Quoted 2 lines

Sorry about dropping the ball on that one, what's correct way to release priv->task?

get_proc_task() does get_pid_task(), which does get_task_struct(), which increments the ->usage field of the task. You want the inverse operation - something that decrements ->usage and checks for zero. This is done via put_task_struct(), which is defined a few lines below get_task_struct().

#5 lore raw

PATCH v3 2/3 Documentation/filesystems: Fixed typo

In reply to [email protected]

From: Robert Foss <[email protected]>

Fixed a -> an typo.

---
 Documentation/filesystems/proc.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/filesystems/proc.txt +1 -1
index e8d0075..7d001be 100644
@@ -145,7 +145,7 @@ Table 1-1: Process specific entries in /proc
 		symbol the task is blocked in - or "0" if not blocked.
  pagemap	Page table
  stack		Report full stack trace, enable via CONFIG_STACKTRACE
- smaps		a extension based on maps, showing the memory consumption of
+ smaps		an extension based on maps, showing the memory consumption of
 		each mapping and flags associated with it
  numa_maps	an extension based on maps, showing the memory locality and
 		binding policy as well as mem usage (in pages) of each mapping.
Signature · 1 line
2.7.4
#6 lore raw

PATCH v3 3/3 Documentation/filesystems: Added /proc/PID/totmaps documentation

In reply to [email protected]

From: Robert Foss <[email protected]>

Added documentation covering /proc/PID/totmaps.

---
 Documentation/filesystems/proc.txt | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
Documentation/filesystems/proc.txt +21 -0
index 7d001be..c06ff33 100644
@@ -11,6 +11,7 @@ Version 1.3                                              Kernel version 2.2.12
 					      Kernel version 2.4.0-test11-pre4
 ------------------------------------------------------------------------------
 fixes/update part 1.1  Stefani Seibold <[email protected]>       June 9 2009
+add totmaps            Robert Foss <[email protected]>  August 12 2016
 
 Table of Contents
 -----------------
@@ -147,6 +148,8 @@ Table 1-1: Process specific entries in /proc
  stack		Report full stack trace, enable via CONFIG_STACKTRACE
  smaps		an extension based on maps, showing the memory consumption of
 		each mapping and flags associated with it
+ totmaps        an extenssion based on maps, showing the total memory
+                consumption of all mappings
  numa_maps	an extension based on maps, showing the memory locality and
 		binding policy as well as mem usage (in pages) of each mapping.
 ..............................................................................
@@ -512,6 +515,24 @@ be vanished or the reverse -- new added.
 This file is only present if the CONFIG_MMU kernel configuration option is
 enabled.
 
+The /proc/PID/totmaps is an extension based on maps, showing the memory
+consumption totals for all of the process's mappings. It lists the sums of the
+same statistics as /proc/PID/smaps.
+
+The process' mappings will be summarized as a series of lines like the
+following:
+
+Rss:                4256 kB
+Pss:                1170 kB
+Shared_Clean:       2720 kB
+Shared_Dirty:       1136 kB
+Private_Clean:         0 kB
+Private_Dirty:       400 kB
+Referenced:         4256 kB
+Anonymous:          1536 kB
+AnonHugePages:         0 kB
+Swap:                  0 kB
+
 The /proc/PID/clear_refs is used to reset the PG_Referenced and ACCESSED/YOUNG
 bits on both physical and virtual pages associated with a process, and the
 soft-dirty bit on pte (see Documentation/vm/soft-dirty.txt for details).
Signature · 1 line
2.7.4
#7 lore raw

Re: [PACTH v3 3/3] Documentation/filesystems: Added /proc/PID/totmaps documentation

Quoted 2 lines · On Tue, Aug 16, 2016 at 01:34:16PM -0400, [email protected] wrote:

+ totmaps an extenssion based on maps, showing the total memory
+ consumption of all mappings

nit: s/extenssion/extension/

#8 lore raw

Re: [PACTH v3 3/3] Documentation/filesystems: Added /proc/PID/totmaps documentation

Quoted 1 line · Jann Horn

nit: s/extenssion/extension/

Thanks :)