[media] rtl28xxu: move usb buffers to state
[linux/fpc-iii.git] / mm / page_owner.c
blob9ab4a9b5bc091b80915850038d4d3f04eb331ee1
1 #include <linux/debugfs.h>
2 #include <linux/mm.h>
3 #include <linux/slab.h>
4 #include <linux/uaccess.h>
5 #include <linux/bootmem.h>
6 #include <linux/stacktrace.h>
7 #include <linux/page_owner.h>
8 #include "internal.h"
10 static bool page_owner_disabled = true;
11 bool page_owner_inited __read_mostly;
13 static void init_early_allocated_pages(void);
15 static int early_page_owner_param(char *buf)
17 if (!buf)
18 return -EINVAL;
20 if (strcmp(buf, "on") == 0)
21 page_owner_disabled = false;
23 return 0;
25 early_param("page_owner", early_page_owner_param);
27 static bool need_page_owner(void)
29 if (page_owner_disabled)
30 return false;
32 return true;
35 static void init_page_owner(void)
37 if (page_owner_disabled)
38 return;
40 page_owner_inited = true;
41 init_early_allocated_pages();
44 struct page_ext_operations page_owner_ops = {
45 .need = need_page_owner,
46 .init = init_page_owner,
49 void __reset_page_owner(struct page *page, unsigned int order)
51 int i;
52 struct page_ext *page_ext;
54 for (i = 0; i < (1 << order); i++) {
55 page_ext = lookup_page_ext(page + i);
56 __clear_bit(PAGE_EXT_OWNER, &page_ext->flags);
60 void __set_page_owner(struct page *page, unsigned int order, gfp_t gfp_mask)
62 struct page_ext *page_ext;
63 struct stack_trace *trace;
65 page_ext = lookup_page_ext(page);
67 trace = &page_ext->trace;
68 trace->nr_entries = 0;
69 trace->max_entries = ARRAY_SIZE(page_ext->trace_entries);
70 trace->entries = &page_ext->trace_entries[0];
71 trace->skip = 3;
72 save_stack_trace(&page_ext->trace);
74 page_ext->order = order;
75 page_ext->gfp_mask = gfp_mask;
77 __set_bit(PAGE_EXT_OWNER, &page_ext->flags);
80 static ssize_t
81 print_page_owner(char __user *buf, size_t count, unsigned long pfn,
82 struct page *page, struct page_ext *page_ext)
84 int ret;
85 int pageblock_mt, page_mt;
86 char *kbuf;
88 kbuf = kmalloc(count, GFP_KERNEL);
89 if (!kbuf)
90 return -ENOMEM;
92 ret = snprintf(kbuf, count,
93 "Page allocated via order %u, mask 0x%x\n",
94 page_ext->order, page_ext->gfp_mask);
96 if (ret >= count)
97 goto err;
99 /* Print information relevant to grouping pages by mobility */
100 pageblock_mt = get_pfnblock_migratetype(page, pfn);
101 page_mt = gfpflags_to_migratetype(page_ext->gfp_mask);
102 ret += snprintf(kbuf + ret, count - ret,
103 "PFN %lu Block %lu type %d %s Flags %s%s%s%s%s%s%s%s%s%s%s%s\n",
104 pfn,
105 pfn >> pageblock_order,
106 pageblock_mt,
107 pageblock_mt != page_mt ? "Fallback" : " ",
108 PageLocked(page) ? "K" : " ",
109 PageError(page) ? "E" : " ",
110 PageReferenced(page) ? "R" : " ",
111 PageUptodate(page) ? "U" : " ",
112 PageDirty(page) ? "D" : " ",
113 PageLRU(page) ? "L" : " ",
114 PageActive(page) ? "A" : " ",
115 PageSlab(page) ? "S" : " ",
116 PageWriteback(page) ? "W" : " ",
117 PageCompound(page) ? "C" : " ",
118 PageSwapCache(page) ? "B" : " ",
119 PageMappedToDisk(page) ? "M" : " ");
121 if (ret >= count)
122 goto err;
124 ret += snprint_stack_trace(kbuf + ret, count - ret,
125 &page_ext->trace, 0);
126 if (ret >= count)
127 goto err;
129 ret += snprintf(kbuf + ret, count - ret, "\n");
130 if (ret >= count)
131 goto err;
133 if (copy_to_user(buf, kbuf, ret))
134 ret = -EFAULT;
136 kfree(kbuf);
137 return ret;
139 err:
140 kfree(kbuf);
141 return -ENOMEM;
144 static ssize_t
145 read_page_owner(struct file *file, char __user *buf, size_t count, loff_t *ppos)
147 unsigned long pfn;
148 struct page *page;
149 struct page_ext *page_ext;
151 if (!page_owner_inited)
152 return -EINVAL;
154 page = NULL;
155 pfn = min_low_pfn + *ppos;
157 /* Find a valid PFN or the start of a MAX_ORDER_NR_PAGES area */
158 while (!pfn_valid(pfn) && (pfn & (MAX_ORDER_NR_PAGES - 1)) != 0)
159 pfn++;
161 drain_all_pages(NULL);
163 /* Find an allocated page */
164 for (; pfn < max_pfn; pfn++) {
166 * If the new page is in a new MAX_ORDER_NR_PAGES area,
167 * validate the area as existing, skip it if not
169 if ((pfn & (MAX_ORDER_NR_PAGES - 1)) == 0 && !pfn_valid(pfn)) {
170 pfn += MAX_ORDER_NR_PAGES - 1;
171 continue;
174 /* Check for holes within a MAX_ORDER area */
175 if (!pfn_valid_within(pfn))
176 continue;
178 page = pfn_to_page(pfn);
179 if (PageBuddy(page)) {
180 unsigned long freepage_order = page_order_unsafe(page);
182 if (freepage_order < MAX_ORDER)
183 pfn += (1UL << freepage_order) - 1;
184 continue;
187 page_ext = lookup_page_ext(page);
190 * Some pages could be missed by concurrent allocation or free,
191 * because we don't hold the zone lock.
193 if (!test_bit(PAGE_EXT_OWNER, &page_ext->flags))
194 continue;
196 /* Record the next PFN to read in the file offset */
197 *ppos = (pfn - min_low_pfn) + 1;
199 return print_page_owner(buf, count, pfn, page, page_ext);
202 return 0;
205 static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
207 struct page *page;
208 struct page_ext *page_ext;
209 unsigned long pfn = zone->zone_start_pfn, block_end_pfn;
210 unsigned long end_pfn = pfn + zone->spanned_pages;
211 unsigned long count = 0;
213 /* Scan block by block. First and last block may be incomplete */
214 pfn = zone->zone_start_pfn;
217 * Walk the zone in pageblock_nr_pages steps. If a page block spans
218 * a zone boundary, it will be double counted between zones. This does
219 * not matter as the mixed block count will still be correct
221 for (; pfn < end_pfn; ) {
222 if (!pfn_valid(pfn)) {
223 pfn = ALIGN(pfn + 1, MAX_ORDER_NR_PAGES);
224 continue;
227 block_end_pfn = ALIGN(pfn + 1, pageblock_nr_pages);
228 block_end_pfn = min(block_end_pfn, end_pfn);
230 page = pfn_to_page(pfn);
232 for (; pfn < block_end_pfn; pfn++) {
233 if (!pfn_valid_within(pfn))
234 continue;
236 page = pfn_to_page(pfn);
239 * We are safe to check buddy flag and order, because
240 * this is init stage and only single thread runs.
242 if (PageBuddy(page)) {
243 pfn += (1UL << page_order(page)) - 1;
244 continue;
247 if (PageReserved(page))
248 continue;
250 page_ext = lookup_page_ext(page);
252 /* Maybe overraping zone */
253 if (test_bit(PAGE_EXT_OWNER, &page_ext->flags))
254 continue;
256 /* Found early allocated page */
257 set_page_owner(page, 0, 0);
258 count++;
262 pr_info("Node %d, zone %8s: page owner found early allocated %lu pages\n",
263 pgdat->node_id, zone->name, count);
266 static void init_zones_in_node(pg_data_t *pgdat)
268 struct zone *zone;
269 struct zone *node_zones = pgdat->node_zones;
270 unsigned long flags;
272 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
273 if (!populated_zone(zone))
274 continue;
276 spin_lock_irqsave(&zone->lock, flags);
277 init_pages_in_zone(pgdat, zone);
278 spin_unlock_irqrestore(&zone->lock, flags);
282 static void init_early_allocated_pages(void)
284 pg_data_t *pgdat;
286 drain_all_pages(NULL);
287 for_each_online_pgdat(pgdat)
288 init_zones_in_node(pgdat);
291 static const struct file_operations proc_page_owner_operations = {
292 .read = read_page_owner,
295 static int __init pageowner_init(void)
297 struct dentry *dentry;
299 if (!page_owner_inited) {
300 pr_info("page_owner is disabled\n");
301 return 0;
304 dentry = debugfs_create_file("page_owner", S_IRUSR, NULL,
305 NULL, &proc_page_owner_operations);
306 if (IS_ERR(dentry))
307 return PTR_ERR(dentry);
309 return 0;
311 module_init(pageowner_init)