]> Pileus Git - ~andy/linux/blob - fs/proc/page.c
proc: kpagecount/kpageflags code cleanup
[~andy/linux] / fs / proc / page.c
1 #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>
9 #include <linux/hugetlb.h>
10 #include <asm/uaccess.h>
11 #include "internal.h"
12
13 #define KPMSIZE sizeof(u64)
14 #define KPMMASK (KPMSIZE - 1)
15
16 /* /proc/kpagecount - an array exposing page counts
17  *
18  * Each entry is a u64 representing the corresponding
19  * physical page count.
20  */
21 static 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) {
37                 if (pfn_valid(pfn))
38                         ppage = pfn_to_page(pfn);
39                 else
40                         ppage = NULL;
41                 if (!ppage)
42                         pcount = 0;
43                 else
44                         pcount = page_mapcount(ppage);
45
46                 if (put_user(pcount, out)) {
47                         ret = -EFAULT;
48                         break;
49                 }
50
51                 pfn++;
52                 out++;
53                 count -= KPMSIZE;
54         }
55
56         *ppos += (char __user *)out - buf;
57         if (!ret)
58                 ret = (char __user *)out - buf;
59         return ret;
60 }
61
62 static 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
87 #define kpf_copy_bit(flags, dstpos, srcpos) (((flags >> srcpos) & 1) << dstpos)
88
89 static 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) {
105                 if (pfn_valid(pfn))
106                         ppage = pfn_to_page(pfn);
107                 else
108                         ppage = NULL;
109                 if (!ppage)
110                         kflags = 0;
111                 else
112                         kflags = ppage->flags;
113
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);
125
126                 if (put_user(uflags, out)) {
127                         ret = -EFAULT;
128                         break;
129                 }
130
131                 pfn++;
132                 out++;
133                 count -= KPMSIZE;
134         }
135
136         *ppos += (char __user *)out - buf;
137         if (!ret)
138                 ret = (char __user *)out - buf;
139         return ret;
140 }
141
142 static const struct file_operations proc_kpageflags_operations = {
143         .llseek = mem_lseek,
144         .read = kpageflags_read,
145 };
146
147 static 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 }
153 module_init(proc_page_init);