From: Matt Mackall <mpm@selenic.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH 13/13] maps#2: Add /proc/kpagemap interface
Date: Fri, 06 Apr 2007 17:03:14 -0500 [thread overview]
Message-ID: <14.469046093@selenic.com> (raw)
In-Reply-To: <1.469046093@selenic.com>
Add /proc/kpagemap interface
This makes physical page flags and counts available to userspace.
Together with /proc/pid/pagemap and /proc/pid/clear_refs, this can be
used to measure memory usage on a per-page basis.
Signed-off-by: Matt Mackall <mpm@selenic.com>
Index: mm/fs/proc/proc_misc.c
===================================================================
--- mm.orig/fs/proc/proc_misc.c 2007-04-05 14:18:49.000000000 -0500
+++ mm/fs/proc/proc_misc.c 2007-04-05 14:26:23.000000000 -0500
@@ -46,6 +46,8 @@
#include <linux/vmalloc.h>
#include <linux/crash_dump.h>
#include <linux/pid_namespace.h>
+#include <linux/ptrace.h>
+#include <linux/bootmem.h>
#include <asm/uaccess.h>
#include <asm/pgtable.h>
#include <asm/io.h>
@@ -733,6 +735,91 @@ static struct file_operations proc_page_
};
#endif
+#ifdef CONFIG_PROC_KPAGEMAP
+#define KPMSIZE (sizeof(unsigned long) * 2)
+#define KPMMASK (KPMSIZE - 1)
+/* /proc/kpagemap - an array exposing page flags and counts
+ *
+ * Each entry is a pair of unsigned longs representing the
+ * corresponding physical page, the first containing the page flags
+ * and the second containing the page use count.
+ *
+ * The first 4 bytes of this file form a simple header:
+ *
+ * first byte: 0 for big endian, 1 for little
+ * second byte: page shift (eg 12 for 4096 byte pages)
+ * third byte: entry size in bytes (currently either 4 or 8)
+ * fourth byte: header size
+ */
+static ssize_t kpagemap_read(struct file *file, char __user *buf,
+ size_t count, loff_t *ppos)
+{
+ unsigned long *page;
+ struct page *ppage;
+ unsigned long src = *ppos;
+ unsigned long pfn;
+ ssize_t ret = 0;
+ int chunk, i;
+
+ pfn = src / KPMSIZE - 1;
+ count = min_t(size_t, count, ((max_pfn + 1) * KPMSIZE) - src);
+ if (src & KPMMASK || count & KPMMASK)
+ return -EIO;
+
+ page = (unsigned long *)__get_free_page(GFP_USER);
+ if (!page)
+ return -ENOMEM;
+
+ while (count > 0) {
+ chunk = min_t(size_t, count, PAGE_SIZE);
+ i = 0;
+
+ if (pfn == -1) {
+ page[0] = 0;
+ page[1] = 0;
+ ((char *)page)[0] = (ntohl(1) != 1);
+ ((char *)page)[1] = PAGE_SHIFT;
+ ((char *)page)[2] = sizeof(unsigned long);
+ ((char *)page)[3] = KPMSIZE;
+ i = 2;
+ pfn++;
+ }
+
+ for (; i < 2 * chunk / KPMSIZE; i += 2, pfn++) {
+ ppage = pfn_to_page(pfn);
+ if (!ppage) {
+ page[i] = 0;
+ page[i + 1] = 0;
+ } else {
+ page[i] = ppage->flags;
+ page[i + 1] = atomic_read(&ppage->_count);
+ }
+ }
+ chunk = (i / 2) * KPMSIZE;
+
+ if (copy_to_user(buf, page, chunk)) {
+ ret = -EFAULT;
+ break;
+ }
+ ret += chunk;
+ src += chunk;
+ buf += chunk;
+ count -= chunk;
+ cond_resched();
+ }
+ *ppos = src;
+
+ free_page((unsigned long)page);
+ return ret;
+}
+
+struct proc_dir_entry *proc_kpagemap;
+static struct file_operations proc_kpagemap_operations = {
+ .llseek = mem_lseek,
+ .read = kpagemap_read,
+};
+#endif
+
struct proc_dir_entry *proc_root_kcore;
void create_seq_entry(char *name, mode_t mode, const struct file_operations *f)
@@ -812,6 +899,11 @@ void __init proc_misc_init(void)
(size_t)high_memory - PAGE_OFFSET + PAGE_SIZE;
}
#endif
+#ifdef CONFIG_PROC_KPAGEMAP
+ proc_kpagemap = create_proc_entry("kpagemap", S_IRUSR, NULL);
+ if (proc_kpagemap)
+ proc_kpagemap->proc_fops = &proc_kpagemap_operations;
+#endif
#ifdef CONFIG_PROC_VMCORE
proc_vmcore = create_proc_entry("vmcore", S_IRUSR, NULL);
if (proc_vmcore)
Index: mm/init/Kconfig
===================================================================
--- mm.orig/init/Kconfig 2007-04-05 14:18:49.000000000 -0500
+++ mm/init/Kconfig 2007-04-05 14:26:23.000000000 -0500
@@ -612,6 +612,15 @@ config PROC_PAGEMAP
with other processes. Disabling this interface will reduce the
size of the kernel for small machines.
+config PROC_KPAGEMAP
+ default y
+ bool "Enable /proc/kpagemap support" if EMBEDDED && PROC_FS
+ help
+ The /proc/pid/kpagemap interface allows reading the
+ kernel's per-page flag and usage counts to gather precise
+ information on page-level memory usage. Disabling this interface
+ will reduce the size of the kernel for small machines.
+
endmenu # General setup
config RT_MUTEXES
next prev parent reply other threads:[~2007-04-06 22:02 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-04-06 22:03 [PATCH 0/13] maps#2: pagemap, kpagemap, and related cleanups take 2 Matt Mackall
2007-04-06 22:03 ` [PATCH 1/13] maps#2: Uninline some functions in the page walker Matt Mackall
2007-04-06 22:03 ` [PATCH 2/13] maps#2: Eliminate the pmd_walker struct " Matt Mackall
2007-04-06 22:03 ` [PATCH 3/13] maps#2: Remove vma from args " Matt Mackall
2007-04-06 22:03 ` [PATCH 4/13] maps#2: Propagate errors from callback in " Matt Mackall
2007-04-06 22:03 ` [PATCH 5/13] maps#2: Add callbacks for each level to " Matt Mackall
2007-04-06 22:03 ` [PATCH 6/13] maps#2: Move the page walker code to lib/ Matt Mackall
2007-04-11 6:35 ` Nick Piggin
2007-04-11 7:17 ` Andrew Morton
2007-04-11 7:33 ` Nick Piggin
2007-04-11 14:40 ` Matt Mackall
2007-04-12 6:38 ` Nick Piggin
2007-04-17 20:45 ` Matt Mackall
2007-04-17 21:26 ` Permanent Kgdb integration into the kernel - lets get with it Jason Wessel
2007-04-17 22:09 ` Andi Kleen
2007-04-06 22:03 ` [PATCH 7/13] maps#2: Simplify interdependence of /proc/pid/maps and smaps Matt Mackall
2007-04-06 22:03 ` [PATCH 8/13] maps#2: Move clear_refs code to task_mmu.c Matt Mackall
2007-04-06 22:03 ` [PATCH 9/13] maps#2: Regroup task_mmu by interface Matt Mackall
2007-04-06 22:03 ` [PATCH 10/13] maps#2: Make /proc/pid/smaps optional under CONFIG_EMBEDDED Matt Mackall
2007-04-06 22:03 ` [PATCH 11/13] maps#2: Make /proc/pid/clear_refs option " Matt Mackall
2007-04-07 5:41 ` David Rientjes
2007-04-06 22:03 ` [PATCH 12/13] maps#2: Add /proc/pid/pagemap interface Matt Mackall
2007-04-07 6:55 ` Andrew Morton
2007-04-07 16:36 ` Matt Mackall
2007-04-19 19:12 ` Dave Hansen
2007-04-19 19:58 ` Matt Mackall
2007-04-06 22:03 ` Matt Mackall [this message]
2007-04-19 19:06 ` [PATCH 13/13] maps#2: Add /proc/kpagemap interface Dave Hansen
2007-04-19 20:02 ` Matt Mackall
2007-04-19 20:25 ` Dave Hansen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=14.469046093@selenic.com \
--to=mpm@selenic.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome