1#include <linux/mm.h>
2#include <linux/mmzone.h>
3#include <linux/bootmem.h>
4#include <linux/bit_spinlock.h>
5#include <linux/page_cgroup.h>
6#include <linux/hash.h>
7#include <linux/slab.h>
8#include <linux/memory.h>
9#include <linux/vmalloc.h>
10#include <linux/cgroup.h>
11#include <linux/swapops.h>
12#include <linux/kmemleak.h>
13
14static unsigned long total_usage;
15
16#if !defined(CONFIG_SPARSEMEM)
17
18
19void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
20{
21 pgdat->node_page_cgroup = NULL;
22}
23
24struct page_cgroup *lookup_page_cgroup(struct page *page)
25{
26 unsigned long pfn = page_to_pfn(page);
27 unsigned long offset;
28 struct page_cgroup *base;
29
30 base = NODE_DATA(page_to_nid(page))->node_page_cgroup;
31#ifdef CONFIG_DEBUG_VM
32
33
34
35
36
37
38 if (unlikely(!base))
39 return NULL;
40#endif
41 offset = pfn - NODE_DATA(page_to_nid(page))->node_start_pfn;
42 return base + offset;
43}
44
45static int __init alloc_node_page_cgroup(int nid)
46{
47 struct page_cgroup *base;
48 unsigned long table_size;
49 unsigned long nr_pages;
50
51 nr_pages = NODE_DATA(nid)->node_spanned_pages;
52 if (!nr_pages)
53 return 0;
54
55 table_size = sizeof(struct page_cgroup) * nr_pages;
56
57 base = __alloc_bootmem_node_nopanic(NODE_DATA(nid),
58 table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
59 if (!base)
60 return -ENOMEM;
61 NODE_DATA(nid)->node_page_cgroup = base;
62 total_usage += table_size;
63 return 0;
64}
65
66void __init page_cgroup_init_flatmem(void)
67{
68
69 int nid, fail;
70
71 if (mem_cgroup_disabled())
72 return;
73
74 for_each_online_node(nid) {
75 fail = alloc_node_page_cgroup(nid);
76 if (fail)
77 goto fail;
78 }
79 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
80 printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
81 " don't want memory cgroups\n");
82 return;
83fail:
84 printk(KERN_CRIT "allocation of page_cgroup failed.\n");
85 printk(KERN_CRIT "please try 'cgroup_disable=memory' boot option\n");
86 panic("Out of memory");
87}
88
89#else
90
91struct page_cgroup *lookup_page_cgroup(struct page *page)
92{
93 unsigned long pfn = page_to_pfn(page);
94 struct mem_section *section = __pfn_to_section(pfn);
95#ifdef CONFIG_DEBUG_VM
96
97
98
99
100
101
102 if (!section->page_cgroup)
103 return NULL;
104#endif
105 return section->page_cgroup + pfn;
106}
107
108static void *__meminit alloc_page_cgroup(size_t size, int nid)
109{
110 gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
111 void *addr = NULL;
112
113 addr = alloc_pages_exact_nid(nid, size, flags);
114 if (addr) {
115 kmemleak_alloc(addr, size, 1, flags);
116 return addr;
117 }
118
119 if (node_state(nid, N_HIGH_MEMORY))
120 addr = vzalloc_node(size, nid);
121 else
122 addr = vzalloc(size);
123
124 return addr;
125}
126
127static int __meminit init_section_page_cgroup(unsigned long pfn, int nid)
128{
129 struct mem_section *section;
130 struct page_cgroup *base;
131 unsigned long table_size;
132
133 section = __pfn_to_section(pfn);
134
135 if (section->page_cgroup)
136 return 0;
137
138 table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
139 base = alloc_page_cgroup(table_size, nid);
140
141
142
143
144
145
146 kmemleak_not_leak(base);
147
148 if (!base) {
149 printk(KERN_ERR "page cgroup allocation failure\n");
150 return -ENOMEM;
151 }
152
153
154
155
156
157 pfn &= PAGE_SECTION_MASK;
158 section->page_cgroup = base - pfn;
159 total_usage += table_size;
160 return 0;
161}
162#ifdef CONFIG_MEMORY_HOTPLUG
163static void free_page_cgroup(void *addr)
164{
165 if (is_vmalloc_addr(addr)) {
166 vfree(addr);
167 } else {
168 struct page *page = virt_to_page(addr);
169 size_t table_size =
170 sizeof(struct page_cgroup) * PAGES_PER_SECTION;
171
172 BUG_ON(PageReserved(page));
173 free_pages_exact(addr, table_size);
174 }
175}
176
177void __free_page_cgroup(unsigned long pfn)
178{
179 struct mem_section *ms;
180 struct page_cgroup *base;
181
182 ms = __pfn_to_section(pfn);
183 if (!ms || !ms->page_cgroup)
184 return;
185 base = ms->page_cgroup + pfn;
186 free_page_cgroup(base);
187 ms->page_cgroup = NULL;
188}
189
190int __meminit online_page_cgroup(unsigned long start_pfn,
191 unsigned long nr_pages,
192 int nid)
193{
194 unsigned long start, end, pfn;
195 int fail = 0;
196
197 start = SECTION_ALIGN_DOWN(start_pfn);
198 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
199
200 if (nid == -1) {
201
202
203
204
205
206 nid = pfn_to_nid(start_pfn);
207 VM_BUG_ON(!node_state(nid, N_ONLINE));
208 }
209
210 for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
211 if (!pfn_present(pfn))
212 continue;
213 fail = init_section_page_cgroup(pfn, nid);
214 }
215 if (!fail)
216 return 0;
217
218
219 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
220 __free_page_cgroup(pfn);
221
222 return -ENOMEM;
223}
224
225int __meminit offline_page_cgroup(unsigned long start_pfn,
226 unsigned long nr_pages, int nid)
227{
228 unsigned long start, end, pfn;
229
230 start = SECTION_ALIGN_DOWN(start_pfn);
231 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
232
233 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
234 __free_page_cgroup(pfn);
235 return 0;
236
237}
238
239static int __meminit page_cgroup_callback(struct notifier_block *self,
240 unsigned long action, void *arg)
241{
242 struct memory_notify *mn = arg;
243 int ret = 0;
244 switch (action) {
245 case MEM_GOING_ONLINE:
246 ret = online_page_cgroup(mn->start_pfn,
247 mn->nr_pages, mn->status_change_nid);
248 break;
249 case MEM_OFFLINE:
250 offline_page_cgroup(mn->start_pfn,
251 mn->nr_pages, mn->status_change_nid);
252 break;
253 case MEM_CANCEL_ONLINE:
254 offline_page_cgroup(mn->start_pfn,
255 mn->nr_pages, mn->status_change_nid);
256 break;
257 case MEM_GOING_OFFLINE:
258 break;
259 case MEM_ONLINE:
260 case MEM_CANCEL_OFFLINE:
261 break;
262 }
263
264 return notifier_from_errno(ret);
265}
266
267#endif
268
269void __init page_cgroup_init(void)
270{
271 unsigned long pfn;
272 int nid;
273
274 if (mem_cgroup_disabled())
275 return;
276
277 for_each_node_state(nid, N_MEMORY) {
278 unsigned long start_pfn, end_pfn;
279
280 start_pfn = node_start_pfn(nid);
281 end_pfn = node_end_pfn(nid);
282
283
284
285
286
287 for (pfn = start_pfn;
288 pfn < end_pfn;
289 pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
290
291 if (!pfn_valid(pfn))
292 continue;
293
294
295
296
297
298
299 if (pfn_to_nid(pfn) != nid)
300 continue;
301 if (init_section_page_cgroup(pfn, nid))
302 goto oom;
303 }
304 }
305 hotplug_memory_notifier(page_cgroup_callback, 0);
306 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
307 printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
308 "don't want memory cgroups\n");
309 return;
310oom:
311 printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
312 panic("Out of memory");
313}
314
315void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
316{
317 return;
318}
319
320#endif
321
322
323#ifdef CONFIG_MEMCG_SWAP
324
325static DEFINE_MUTEX(swap_cgroup_mutex);
326struct swap_cgroup_ctrl {
327 struct page **map;
328 unsigned long length;
329 spinlock_t lock;
330};
331
332static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
333
334struct swap_cgroup {
335 unsigned short id;
336};
337#define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356static int swap_cgroup_prepare(int type)
357{
358 struct page *page;
359 struct swap_cgroup_ctrl *ctrl;
360 unsigned long idx, max;
361
362 ctrl = &swap_cgroup_ctrl[type];
363
364 for (idx = 0; idx < ctrl->length; idx++) {
365 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
366 if (!page)
367 goto not_enough_page;
368 ctrl->map[idx] = page;
369 }
370 return 0;
371not_enough_page:
372 max = idx;
373 for (idx = 0; idx < max; idx++)
374 __free_page(ctrl->map[idx]);
375
376 return -ENOMEM;
377}
378
379static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
380 struct swap_cgroup_ctrl **ctrlp)
381{
382 pgoff_t offset = swp_offset(ent);
383 struct swap_cgroup_ctrl *ctrl;
384 struct page *mappage;
385 struct swap_cgroup *sc;
386
387 ctrl = &swap_cgroup_ctrl[swp_type(ent)];
388 if (ctrlp)
389 *ctrlp = ctrl;
390
391 mappage = ctrl->map[offset / SC_PER_PAGE];
392 sc = page_address(mappage);
393 return sc + offset % SC_PER_PAGE;
394}
395
396
397
398
399
400
401
402
403
404
405unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
406 unsigned short old, unsigned short new)
407{
408 struct swap_cgroup_ctrl *ctrl;
409 struct swap_cgroup *sc;
410 unsigned long flags;
411 unsigned short retval;
412
413 sc = lookup_swap_cgroup(ent, &ctrl);
414
415 spin_lock_irqsave(&ctrl->lock, flags);
416 retval = sc->id;
417 if (retval == old)
418 sc->id = new;
419 else
420 retval = 0;
421 spin_unlock_irqrestore(&ctrl->lock, flags);
422 return retval;
423}
424
425
426
427
428
429
430
431
432
433unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
434{
435 struct swap_cgroup_ctrl *ctrl;
436 struct swap_cgroup *sc;
437 unsigned short old;
438 unsigned long flags;
439
440 sc = lookup_swap_cgroup(ent, &ctrl);
441
442 spin_lock_irqsave(&ctrl->lock, flags);
443 old = sc->id;
444 sc->id = id;
445 spin_unlock_irqrestore(&ctrl->lock, flags);
446
447 return old;
448}
449
450
451
452
453
454
455
456unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
457{
458 return lookup_swap_cgroup(ent, NULL)->id;
459}
460
461int swap_cgroup_swapon(int type, unsigned long max_pages)
462{
463 void *array;
464 unsigned long array_size;
465 unsigned long length;
466 struct swap_cgroup_ctrl *ctrl;
467
468 if (!do_swap_account)
469 return 0;
470
471 length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
472 array_size = length * sizeof(void *);
473
474 array = vzalloc(array_size);
475 if (!array)
476 goto nomem;
477
478 ctrl = &swap_cgroup_ctrl[type];
479 mutex_lock(&swap_cgroup_mutex);
480 ctrl->length = length;
481 ctrl->map = array;
482 spin_lock_init(&ctrl->lock);
483 if (swap_cgroup_prepare(type)) {
484
485 ctrl->map = NULL;
486 ctrl->length = 0;
487 mutex_unlock(&swap_cgroup_mutex);
488 vfree(array);
489 goto nomem;
490 }
491 mutex_unlock(&swap_cgroup_mutex);
492
493 return 0;
494nomem:
495 printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n");
496 printk(KERN_INFO
497 "swap_cgroup can be disabled by swapaccount=0 boot option\n");
498 return -ENOMEM;
499}
500
501void swap_cgroup_swapoff(int type)
502{
503 struct page **map;
504 unsigned long i, length;
505 struct swap_cgroup_ctrl *ctrl;
506
507 if (!do_swap_account)
508 return;
509
510 mutex_lock(&swap_cgroup_mutex);
511 ctrl = &swap_cgroup_ctrl[type];
512 map = ctrl->map;
513 length = ctrl->length;
514 ctrl->map = NULL;
515 ctrl->length = 0;
516 mutex_unlock(&swap_cgroup_mutex);
517
518 if (map) {
519 for (i = 0; i < length; i++) {
520 struct page *page = map[i];
521 if (page)
522 __free_page(page);
523 }
524 vfree(map);
525 }
526}
527
528#endif
529