-
Notifications
You must be signed in to change notification settings - Fork 2
/
pageowner.c
163 lines (140 loc) · 3.85 KB
/
pageowner.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <linux/debugfs.h>
#include <linux/mm.h>
#include <linux/hugetlb.h>
#include <linux/huge_mm.h>
#include <linux/mount.h>
#include <linux/seq_file.h>
#include <linux/highmem.h>
#include <linux/ptrace.h>
#include <linux/slab.h>
#include <linux/pagemap.h>
#include <linux/mempolicy.h>
#include <linux/rmap.h>
#include <linux/swap.h>
#include <linux/swapops.h>
#include <asm/elf.h>
#include <asm/uaccess.h>
#include <asm/tlbflush.h>
#include "internal.h"
#include <linux/bootmem.h>
#include <linux/kallsyms.h>
static ssize_t
read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
unsigned long pfn;
struct page *page;
char *kbuf;
int ret = 0;
ssize_t num_written = 0;
int blocktype = 0, pagetype = 0;
page = NULL;
pfn = min_low_pfn + *ppos;
/* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
pfn++;
//printk("pfn: %ld max_pfn: %ld\n", pfn, max_pfn);
/* Find an allocated page */
for (; pfn < max_pfn; pfn++) {
/*
* If the new page is in a new MAX_ORDER_NR_PAGES area,
* validate the area as existing, skip it if not
*/
if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
pfn += MAX_ORDER_NR_PAGES - 1;
continue;
}
/* Check for holes within a MAX_ORDER area */
if (!pfn_valid_within(pfn))
continue;
page = pfn_to_page(pfn);
/* Catch situations where free pages have a bad ->order */
if (page->order >= 0 && PageBuddy(page))
printk(KERN_WARNING
"PageOwner info inaccurate for PFN %lu\n",
pfn);
/* Stop search if page is allocated and has trace info */
if (page->order >= 0 && page->trace.nr_entries) {
//intk("stopped search at pfn: %ld\n", pfn);
break;
}
}
if (!pfn_valid(pfn))
return 0;
/*
* If memory does not end at a SECTION_SIZE boundary, then
* we might have a pfn_valid() above max_pfn
*/
if (pfn >= max_pfn)
return 0;
/* Record the next PFN to read in the file offset */
*ppos = (pfn - min_low_pfn) + 1;
kbuf = kmalloc(count, GFP_KERNEL);
if (!kbuf)
return -ENOMEM;
//printk("page: %p\n", page);
ret = snprintf(kbuf, count, "Page allocated via order %d, mask 0x%x\n",
page->order, page->gfp_mask);
if (ret >= count) {
ret = -ENOMEM;
goto out;
}
/* Print information relevant to grouping pages by mobility */
blocktype = get_pageblock_migratetype(page);
pagetype = allocflags_to_migratetype(page->gfp_mask);
ret += snprintf(kbuf+ret, count-ret,
"PFN %lu Block %lu type %d %s "
"Flags %s%s%s%s%s%s%s%s%s%s%s%s\n",
pfn,
pfn >> pageblock_order,
blocktype,
blocktype != pagetype ? "Fallback" : " ",
PageLocked(page) ? "K" : " ",
PageError(page) ? "E" : " ",
PageReferenced(page) ? "R" : " ",
PageUptodate(page) ? "U" : " ",
PageDirty(page) ? "D" : " ",
PageLRU(page) ? "L" : " ",
PageActive(page) ? "A" : " ",
PageSlab(page) ? "S" : " ",
PageWriteback(page) ? "W" : " ",
PageCompound(page) ? "C" : " ",
PageSwapCache(page) ? "B" : " ",
PageMappedToDisk(page) ? "M" : " ");
if (ret >= count) {
ret = -ENOMEM;
goto out;
}
num_written = ret;
ret = snprint_stack_trace(kbuf + num_written, count - num_written,
&page->trace, 0);
if (ret >= count - num_written) {
ret = -ENOMEM;
goto out;
}
num_written += ret;
ret = snprintf(kbuf + num_written, count - num_written, "\n");
if (ret >= count - num_written) {
ret = -ENOMEM;
goto out;
}
num_written += ret;
ret = num_written;
if (copy_to_user(buf, kbuf, ret))
ret = -EFAULT;
out:
kfree(kbuf);
return ret;
}
static struct file_operations proc_page_owner_operations = {
.read = read_page_owner,
};
static int __init pageowner_init(void)
{
struct dentry *dentry;
dentry = debugfs_create_file("page_owner", S_IRUSR, NULL,
NULL, &proc_page_owner_operations);
if (IS_ERR(dentry))
return PTR_ERR(dentry);
return 0;
}
module_init(pageowner_init)