blob: e73e911b7d0c029a2e978f4c6bb3d0b2c9b57c9a [file] [log] [blame]
Alexey Dobriyan6d80e532008-10-06 14:26:12 +04001#include <linux/bootmem.h>
2#include <linux/compiler.h>
3#include <linux/fs.h>
4#include <linux/init.h>
5#include <linux/mm.h>
6#include <linux/mmzone.h>
7#include <linux/proc_fs.h>
8#include <linux/seq_file.h>
Wu Fengguang20a03072009-06-16 15:32:22 -07009#include <linux/hugetlb.h>
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040010#include <asm/uaccess.h>
11#include "internal.h"
12
13#define KPMSIZE sizeof(u64)
14#define KPMMASK (KPMSIZE - 1)
Wu Fengguanged7ce0f2009-06-16 15:32:23 -070015
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040016/* /proc/kpagecount - an array exposing page counts
17 *
18 * Each entry is a u64 representing the corresponding
19 * physical page count.
20 */
21static ssize_t kpagecount_read(struct file *file, char __user *buf,
22 size_t count, loff_t *ppos)
23{
24 u64 __user *out = (u64 __user *)buf;
25 struct page *ppage;
26 unsigned long src = *ppos;
27 unsigned long pfn;
28 ssize_t ret = 0;
29 u64 pcount;
30
31 pfn = src / KPMSIZE;
32 count = min_t(size_t, count, (max_pfn * KPMSIZE) - src);
33 if (src & KPMMASK || count & KPMMASK)
34 return -EINVAL;
35
36 while (count > 0) {
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040037 if (pfn_valid(pfn))
38 ppage = pfn_to_page(pfn);
Wu Fengguanged7ce0f2009-06-16 15:32:23 -070039 else
40 ppage = NULL;
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040041 if (!ppage)
42 pcount = 0;
43 else
44 pcount = page_mapcount(ppage);
45
Wu Fengguanged7ce0f2009-06-16 15:32:23 -070046 if (put_user(pcount, out)) {
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040047 ret = -EFAULT;
48 break;
49 }
50
Wu Fengguanged7ce0f2009-06-16 15:32:23 -070051 pfn++;
52 out++;
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040053 count -= KPMSIZE;
54 }
55
56 *ppos += (char __user *)out - buf;
57 if (!ret)
58 ret = (char __user *)out - buf;
59 return ret;
60}
61
62static const struct file_operations proc_kpagecount_operations = {
63 .llseek = mem_lseek,
64 .read = kpagecount_read,
65};
66
67/* /proc/kpageflags - an array exposing page flags
68 *
69 * Each entry is a u64 representing the corresponding
70 * physical page flags.
71 */
72
73/* These macros are used to decouple internal flags from exported ones */
74
75#define KPF_LOCKED 0
76#define KPF_ERROR 1
77#define KPF_REFERENCED 2
78#define KPF_UPTODATE 3
79#define KPF_DIRTY 4
80#define KPF_LRU 5
81#define KPF_ACTIVE 6
82#define KPF_SLAB 7
83#define KPF_WRITEBACK 8
84#define KPF_RECLAIM 9
85#define KPF_BUDDY 10
86
Wu Fengguangad3bdef2009-03-11 09:00:04 +080087#define kpf_copy_bit(flags, dstpos, srcpos) (((flags >> srcpos) & 1) << dstpos)
Alexey Dobriyan6d80e532008-10-06 14:26:12 +040088
89static ssize_t kpageflags_read(struct file *file, char __user *buf,
90 size_t count, loff_t *ppos)
91{
92 u64 __user *out = (u64 __user *)buf;
93 struct page *ppage;
94 unsigned long src = *ppos;
95 unsigned long pfn;
96 ssize_t ret = 0;
97 u64 kflags, uflags;
98
99 pfn = src / KPMSIZE;
100 count = min_t(unsigned long, count, (max_pfn * KPMSIZE) - src);
101 if (src & KPMMASK || count & KPMMASK)
102 return -EINVAL;
103
104 while (count > 0) {
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400105 if (pfn_valid(pfn))
106 ppage = pfn_to_page(pfn);
Wu Fengguanged7ce0f2009-06-16 15:32:23 -0700107 else
108 ppage = NULL;
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400109 if (!ppage)
110 kflags = 0;
111 else
112 kflags = ppage->flags;
113
Helge Bahmanne07a4b92009-02-20 16:24:12 +0300114 uflags = kpf_copy_bit(kflags, KPF_LOCKED, PG_locked) |
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400115 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);
125
Wu Fengguanged7ce0f2009-06-16 15:32:23 -0700126 if (put_user(uflags, out)) {
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400127 ret = -EFAULT;
128 break;
129 }
130
Wu Fengguanged7ce0f2009-06-16 15:32:23 -0700131 pfn++;
132 out++;
Alexey Dobriyan6d80e532008-10-06 14:26:12 +0400133 count -= KPMSIZE;
134 }
135
136 *ppos += (char __user *)out - buf;
137 if (!ret)
138 ret = (char __user *)out - buf;
139 return ret;
140}
141
142static const struct file_operations proc_kpageflags_operations = {
143 .llseek = mem_lseek,
144 .read = kpageflags_read,
145};
146
147static int __init proc_page_init(void)
148{
149 proc_create("kpagecount", S_IRUSR, NULL, &proc_kpagecount_operations);
150 proc_create("kpageflags", S_IRUSR, NULL, &proc_kpageflags_operations);
151 return 0;
152}
153module_init(proc_page_init);