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)
15 /* /proc/kpagecount - an array exposing page counts
17 * Each entry is a u64 representing the corresponding
18 * physical page count.
20 static ssize_t kpagecount_read(struct file *file, char __user *buf,
21 size_t count, loff_t *ppos)
23 u64 __user *out = (u64 __user *)buf;
25 unsigned long src = *ppos;
31 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
32 if (src & KPMMASK || count & KPMMASK)
38 ppage = pfn_to_page(pfn);
43 pcount = page_mapcount(ppage);
45 if (put_user(pcount, out++)) {
53 *ppos += (char __user *)out - buf;
55 ret = (char __user *)out - buf;
59 static const struct file_operations proc_kpagecount_operations = {
61 .read = kpagecount_read,
64 /* /proc/kpageflags - an array exposing page flags
66 * Each entry is a u64 representing the corresponding
67 * physical page flags.
70 /* These macros are used to decouple internal flags from exported ones */
74 #define KPF_REFERENCED 2
75 #define KPF_UPTODATE 3
80 #define KPF_WRITEBACK 8
84 #define kpf_copy_bit(flags, dstpos, srcpos) (((flags >> srcpos) & 1) << dstpos)
86 static ssize_t kpageflags_read(struct file *file, char __user *buf,
87 size_t count, loff_t *ppos)
89 u64 __user *out = (u64 __user *)buf;
91 unsigned long src = *ppos;
97 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
98 if (src & KPMMASK || count & KPMMASK)
104 ppage = pfn_to_page(pfn);
109 kflags = ppage->flags;
111 uflags = kpf_copy_bit(kflags, KPF_LOCKED, PG_locked) |
112 kpf_copy_bit(kflags, KPF_ERROR, PG_error) |
113 kpf_copy_bit(kflags, KPF_REFERENCED, PG_referenced) |
114 kpf_copy_bit(kflags, KPF_UPTODATE, PG_uptodate) |
115 kpf_copy_bit(kflags, KPF_DIRTY, PG_dirty) |
116 kpf_copy_bit(kflags, KPF_LRU, PG_lru) |
117 kpf_copy_bit(kflags, KPF_ACTIVE, PG_active) |
118 kpf_copy_bit(kflags, KPF_SLAB, PG_slab) |
119 kpf_copy_bit(kflags, KPF_WRITEBACK, PG_writeback) |
120 kpf_copy_bit(kflags, KPF_RECLAIM, PG_reclaim) |
121 kpf_copy_bit(kflags, KPF_BUDDY, PG_buddy);
123 if (put_user(uflags, out++)) {
131 *ppos += (char __user *)out - buf;
133 ret = (char __user *)out - buf;
137 static const struct file_operations proc_kpageflags_operations = {
139 .read = kpageflags_read,
142 static int __init proc_page_init(void)
144 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
145 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
148 module_init(proc_page_init);