1 #include <linux/bootmem.h>
2 #include <linux/compiler.h>
4 #include <linux/init.h>
6 #include <linux/mmzone.h>
7 #include <linux/proc_fs.h>
8 #include <linux/seq_file.h>
9 #include <linux/hugetlb.h>
10 #include <asm/uaccess.h>
13 #define KPMSIZE sizeof(u64)
14 #define KPMMASK (KPMSIZE - 1)
16 /* /proc/kpagecount - an array exposing page counts
18 * Each entry is a u64 representing the corresponding
19 * physical page count.
21 static ssize_t kpagecount_read(struct file *file, char __user *buf,
22 size_t count, loff_t *ppos)
24 u64 __user *out = (u64 __user *)buf;
26 unsigned long src = *ppos;
32 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
33 if (src & KPMMASK || count & KPMMASK)
38 ppage = pfn_to_page(pfn);
44 pcount = page_mapcount(ppage);
46 if (put_user(pcount, out)) {
56 *ppos += (char __user *)out - buf;
58 ret = (char __user *)out - buf;
62 static const struct file_operations proc_kpagecount_operations = {
64 .read = kpagecount_read,
67 /* /proc/kpageflags - an array exposing page flags
69 * Each entry is a u64 representing the corresponding
70 * physical page flags.
73 /* These macros are used to decouple internal flags from exported ones */
77 #define KPF_REFERENCED 2
78 #define KPF_UPTODATE 3
83 #define KPF_WRITEBACK 8
87 #define kpf_copy_bit(flags, dstpos, srcpos) (((flags >> srcpos) & 1) << dstpos)
89 static ssize_t kpageflags_read(struct file *file, char __user *buf,
90 size_t count, loff_t *ppos)
92 u64 __user *out = (u64 __user *)buf;
94 unsigned long src = *ppos;
100 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
101 if (src & KPMMASK || count & KPMMASK)
106 ppage = pfn_to_page(pfn);
112 kflags = ppage->flags;
114 uflags = kpf_copy_bit(kflags, KPF_LOCKED, PG_locked) |
115 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
116 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
117 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
118 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
119 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
120 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
121 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
122 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
123 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
124 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
126 if (put_user(uflags, out)) {
136 *ppos += (char __user *)out - buf;
138 ret = (char __user *)out - buf;
142 static const struct file_operations proc_kpageflags_operations = {
144 .read = kpageflags_read,
147 static int __init proc_page_init(void)
149 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
150 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
153 module_init(proc_page_init);