ALSA: pcm: potential uninitialized return values
[pandora-kernel.git] / mm / page_cgroup.c
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
14 static void __meminit init_page_cgroup(struct page_cgroup *pc, unsigned long id)
15 {
16         pc->flags = 0;
17         set_page_cgroup_array_id(pc, id);
18         pc->mem_cgroup = NULL;
19         INIT_LIST_HEAD(&pc->lru);
20 }
21 static unsigned long total_usage;
22
23 #if !defined(CONFIG_SPARSEMEM)
24
25
26 void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
27 {
28         pgdat->node_page_cgroup = NULL;
29 }
30
31 struct page_cgroup *lookup_page_cgroup(struct page *page)
32 {
33         unsigned long pfn = page_to_pfn(page);
34         unsigned long offset;
35         struct page_cgroup *base;
36
37         base = NODE_DATA(page_to_nid(page))->node_page_cgroup;
38         if (unlikely(!base))
39                 return NULL;
40
41         offset = pfn - NODE_DATA(page_to_nid(page))->node_start_pfn;
42         return base + offset;
43 }
44
45 struct page *lookup_cgroup_page(struct page_cgroup *pc)
46 {
47         unsigned long pfn;
48         struct page *page;
49         pg_data_t *pgdat;
50
51         pgdat = NODE_DATA(page_cgroup_array_id(pc));
52         pfn = pc - pgdat->node_page_cgroup + pgdat->node_start_pfn;
53         page = pfn_to_page(pfn);
54         VM_BUG_ON(pc != lookup_page_cgroup(page));
55         return page;
56 }
57
58 static int __init alloc_node_page_cgroup(int nid)
59 {
60         struct page_cgroup *base, *pc;
61         unsigned long table_size;
62         unsigned long start_pfn, nr_pages, index;
63
64         start_pfn = NODE_DATA(nid)->node_start_pfn;
65         nr_pages = NODE_DATA(nid)->node_spanned_pages;
66
67         if (!nr_pages)
68                 return 0;
69
70         table_size = sizeof(struct page_cgroup) * nr_pages;
71
72         base = __alloc_bootmem_node_nopanic(NODE_DATA(nid),
73                         table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
74         if (!base)
75                 return -ENOMEM;
76         for (index = 0; index < nr_pages; index++) {
77                 pc = base + index;
78                 init_page_cgroup(pc, nid);
79         }
80         NODE_DATA(nid)->node_page_cgroup = base;
81         total_usage += table_size;
82         return 0;
83 }
84
85 void __init page_cgroup_init_flatmem(void)
86 {
87
88         int nid, fail;
89
90         if (mem_cgroup_disabled())
91                 return;
92
93         for_each_online_node(nid)  {
94                 fail = alloc_node_page_cgroup(nid);
95                 if (fail)
96                         goto fail;
97         }
98         printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
99         printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
100         " don't want memory cgroups\n");
101         return;
102 fail:
103         printk(KERN_CRIT "allocation of page_cgroup failed.\n");
104         printk(KERN_CRIT "please try 'cgroup_disable=memory' boot option\n");
105         panic("Out of memory");
106 }
107
108 #else /* CONFIG_FLAT_NODE_MEM_MAP */
109
110 struct page_cgroup *lookup_page_cgroup(struct page *page)
111 {
112         unsigned long pfn = page_to_pfn(page);
113         struct mem_section *section = __pfn_to_section(pfn);
114
115         if (!section->page_cgroup)
116                 return NULL;
117         return section->page_cgroup + pfn;
118 }
119
120 struct page *lookup_cgroup_page(struct page_cgroup *pc)
121 {
122         struct mem_section *section;
123         struct page *page;
124         unsigned long nr;
125
126         nr = page_cgroup_array_id(pc);
127         section = __nr_to_section(nr);
128         page = pfn_to_page(pc - section->page_cgroup);
129         VM_BUG_ON(pc != lookup_page_cgroup(page));
130         return page;
131 }
132
133 static void *__meminit alloc_page_cgroup(size_t size, int nid)
134 {
135         void *addr = NULL;
136         gfp_t flags = GFP_KERNEL | __GFP_NOWARN;
137
138         addr = alloc_pages_exact_nid(nid, size, flags);
139         if (addr) {
140                 kmemleak_alloc(addr, size, 1, flags);
141                 return addr;
142         }
143
144         if (node_state(nid, N_HIGH_MEMORY))
145                 addr = vmalloc_node(size, nid);
146         else
147                 addr = vmalloc(size);
148
149         return addr;
150 }
151
152 #ifdef CONFIG_MEMORY_HOTPLUG
153 static void free_page_cgroup(void *addr)
154 {
155         if (is_vmalloc_addr(addr)) {
156                 vfree(addr);
157         } else {
158                 struct page *page = virt_to_page(addr);
159                 size_t table_size =
160                         sizeof(struct page_cgroup) * PAGES_PER_SECTION;
161
162                 BUG_ON(PageReserved(page));
163                 kmemleak_free(addr);
164                 free_pages_exact(addr, table_size);
165         }
166 }
167 #endif
168
169 static int __meminit init_section_page_cgroup(unsigned long pfn, int nid)
170 {
171         struct page_cgroup *base, *pc;
172         struct mem_section *section;
173         unsigned long table_size;
174         unsigned long nr;
175         int index;
176
177         nr = pfn_to_section_nr(pfn);
178         section = __nr_to_section(nr);
179
180         if (section->page_cgroup)
181                 return 0;
182
183         table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
184         base = alloc_page_cgroup(table_size, nid);
185
186         /*
187          * The value stored in section->page_cgroup is (base - pfn)
188          * and it does not point to the memory block allocated above,
189          * causing kmemleak false positives.
190          */
191         kmemleak_not_leak(base);
192
193         if (!base) {
194                 printk(KERN_ERR "page cgroup allocation failure\n");
195                 return -ENOMEM;
196         }
197
198         for (index = 0; index < PAGES_PER_SECTION; index++) {
199                 pc = base + index;
200                 init_page_cgroup(pc, nr);
201         }
202         /*
203          * The passed "pfn" may not be aligned to SECTION.  For the calculation
204          * we need to apply a mask.
205          */
206         pfn &= PAGE_SECTION_MASK;
207         section->page_cgroup = base - pfn;
208         total_usage += table_size;
209         return 0;
210 }
211 #ifdef CONFIG_MEMORY_HOTPLUG
212 void __free_page_cgroup(unsigned long pfn)
213 {
214         struct mem_section *ms;
215         struct page_cgroup *base;
216
217         ms = __pfn_to_section(pfn);
218         if (!ms || !ms->page_cgroup)
219                 return;
220         base = ms->page_cgroup + pfn;
221         free_page_cgroup(base);
222         ms->page_cgroup = NULL;
223 }
224
225 int __meminit online_page_cgroup(unsigned long start_pfn,
226                         unsigned long nr_pages,
227                         int nid)
228 {
229         unsigned long start, end, pfn;
230         int fail = 0;
231
232         start = SECTION_ALIGN_DOWN(start_pfn);
233         end = SECTION_ALIGN_UP(start_pfn + nr_pages);
234
235         if (nid == -1) {
236                 /*
237                  * In this case, "nid" already exists and contains valid memory.
238                  * "start_pfn" passed to us is a pfn which is an arg for
239                  * online__pages(), and start_pfn should exist.
240                  */
241                 nid = pfn_to_nid(start_pfn);
242                 VM_BUG_ON(!node_state(nid, N_ONLINE));
243         }
244
245         for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
246                 if (!pfn_present(pfn))
247                         continue;
248                 fail = init_section_page_cgroup(pfn, nid);
249         }
250         if (!fail)
251                 return 0;
252
253         /* rollback */
254         for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
255                 __free_page_cgroup(pfn);
256
257         return -ENOMEM;
258 }
259
260 int __meminit offline_page_cgroup(unsigned long start_pfn,
261                 unsigned long nr_pages, int nid)
262 {
263         unsigned long start, end, pfn;
264
265         start = SECTION_ALIGN_DOWN(start_pfn);
266         end = SECTION_ALIGN_UP(start_pfn + nr_pages);
267
268         for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
269                 __free_page_cgroup(pfn);
270         return 0;
271
272 }
273
274 static int __meminit page_cgroup_callback(struct notifier_block *self,
275                                unsigned long action, void *arg)
276 {
277         struct memory_notify *mn = arg;
278         int ret = 0;
279         switch (action) {
280         case MEM_GOING_ONLINE:
281                 ret = online_page_cgroup(mn->start_pfn,
282                                    mn->nr_pages, mn->status_change_nid);
283                 break;
284         case MEM_OFFLINE:
285                 offline_page_cgroup(mn->start_pfn,
286                                 mn->nr_pages, mn->status_change_nid);
287                 break;
288         case MEM_CANCEL_ONLINE:
289         case MEM_GOING_OFFLINE:
290                 break;
291         case MEM_ONLINE:
292         case MEM_CANCEL_OFFLINE:
293                 break;
294         }
295
296         return notifier_from_errno(ret);
297 }
298
299 #endif
300
301 void __init page_cgroup_init(void)
302 {
303         unsigned long pfn;
304         int nid;
305
306         if (mem_cgroup_disabled())
307                 return;
308
309         for_each_node_state(nid, N_HIGH_MEMORY) {
310                 unsigned long start_pfn, end_pfn;
311
312                 start_pfn = node_start_pfn(nid);
313                 end_pfn = node_end_pfn(nid);
314                 /*
315                  * start_pfn and end_pfn may not be aligned to SECTION and the
316                  * page->flags of out of node pages are not initialized.  So we
317                  * scan [start_pfn, the biggest section's pfn < end_pfn) here.
318                  */
319                 for (pfn = start_pfn;
320                      pfn < end_pfn;
321                      pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
322
323                         if (!pfn_valid(pfn))
324                                 continue;
325                         /*
326                          * Nodes's pfns can be overlapping.
327                          * We know some arch can have a nodes layout such as
328                          * -------------pfn-------------->
329                          * N0 | N1 | N2 | N0 | N1 | N2|....
330                          */
331                         if (pfn_to_nid(pfn) != nid)
332                                 continue;
333                         if (init_section_page_cgroup(pfn, nid))
334                                 goto oom;
335                 }
336         }
337         hotplug_memory_notifier(page_cgroup_callback, 0);
338         printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
339         printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
340                          "don't want memory cgroups\n");
341         return;
342 oom:
343         printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
344         panic("Out of memory");
345 }
346
347 void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
348 {
349         return;
350 }
351
352 #endif
353
354
355 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
356
357 static DEFINE_MUTEX(swap_cgroup_mutex);
358 struct swap_cgroup_ctrl {
359         struct page **map;
360         unsigned long length;
361         spinlock_t      lock;
362 };
363
364 static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
365
366 struct swap_cgroup {
367         unsigned short          id;
368 };
369 #define SC_PER_PAGE     (PAGE_SIZE/sizeof(struct swap_cgroup))
370 #define SC_POS_MASK     (SC_PER_PAGE - 1)
371
372 /*
373  * SwapCgroup implements "lookup" and "exchange" operations.
374  * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
375  * against SwapCache. At swap_free(), this is accessed directly from swap.
376  *
377  * This means,
378  *  - we have no race in "exchange" when we're accessed via SwapCache because
379  *    SwapCache(and its swp_entry) is under lock.
380  *  - When called via swap_free(), there is no user of this entry and no race.
381  * Then, we don't need lock around "exchange".
382  *
383  * TODO: we can push these buffers out to HIGHMEM.
384  */
385
386 /*
387  * allocate buffer for swap_cgroup.
388  */
389 static int swap_cgroup_prepare(int type)
390 {
391         struct page *page;
392         struct swap_cgroup_ctrl *ctrl;
393         unsigned long idx, max;
394
395         ctrl = &swap_cgroup_ctrl[type];
396
397         for (idx = 0; idx < ctrl->length; idx++) {
398                 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
399                 if (!page)
400                         goto not_enough_page;
401                 ctrl->map[idx] = page;
402
403                 if (!(idx % SWAP_CLUSTER_MAX))
404                         cond_resched();
405         }
406         return 0;
407 not_enough_page:
408         max = idx;
409         for (idx = 0; idx < max; idx++)
410                 __free_page(ctrl->map[idx]);
411
412         return -ENOMEM;
413 }
414
415 /**
416  * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
417  * @end: swap entry to be cmpxchged
418  * @old: old id
419  * @new: new id
420  *
421  * Returns old id at success, 0 at failure.
422  * (There is no mem_cgroup using 0 as its id)
423  */
424 unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
425                                         unsigned short old, unsigned short new)
426 {
427         int type = swp_type(ent);
428         unsigned long offset = swp_offset(ent);
429         unsigned long idx = offset / SC_PER_PAGE;
430         unsigned long pos = offset & SC_POS_MASK;
431         struct swap_cgroup_ctrl *ctrl;
432         struct page *mappage;
433         struct swap_cgroup *sc;
434         unsigned long flags;
435         unsigned short retval;
436
437         ctrl = &swap_cgroup_ctrl[type];
438
439         mappage = ctrl->map[idx];
440         sc = page_address(mappage);
441         sc += pos;
442         spin_lock_irqsave(&ctrl->lock, flags);
443         retval = sc->id;
444         if (retval == old)
445                 sc->id = new;
446         else
447                 retval = 0;
448         spin_unlock_irqrestore(&ctrl->lock, flags);
449         return retval;
450 }
451
452 /**
453  * swap_cgroup_record - record mem_cgroup for this swp_entry.
454  * @ent: swap entry to be recorded into
455  * @mem: mem_cgroup to be recorded
456  *
457  * Returns old value at success, 0 at failure.
458  * (Of course, old value can be 0.)
459  */
460 unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
461 {
462         int type = swp_type(ent);
463         unsigned long offset = swp_offset(ent);
464         unsigned long idx = offset / SC_PER_PAGE;
465         unsigned long pos = offset & SC_POS_MASK;
466         struct swap_cgroup_ctrl *ctrl;
467         struct page *mappage;
468         struct swap_cgroup *sc;
469         unsigned short old;
470         unsigned long flags;
471
472         ctrl = &swap_cgroup_ctrl[type];
473
474         mappage = ctrl->map[idx];
475         sc = page_address(mappage);
476         sc += pos;
477         spin_lock_irqsave(&ctrl->lock, flags);
478         old = sc->id;
479         sc->id = id;
480         spin_unlock_irqrestore(&ctrl->lock, flags);
481
482         return old;
483 }
484
485 /**
486  * lookup_swap_cgroup - lookup mem_cgroup tied to swap entry
487  * @ent: swap entry to be looked up.
488  *
489  * Returns CSS ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
490  */
491 unsigned short lookup_swap_cgroup(swp_entry_t ent)
492 {
493         int type = swp_type(ent);
494         unsigned long offset = swp_offset(ent);
495         unsigned long idx = offset / SC_PER_PAGE;
496         unsigned long pos = offset & SC_POS_MASK;
497         struct swap_cgroup_ctrl *ctrl;
498         struct page *mappage;
499         struct swap_cgroup *sc;
500         unsigned short ret;
501
502         ctrl = &swap_cgroup_ctrl[type];
503         mappage = ctrl->map[idx];
504         sc = page_address(mappage);
505         sc += pos;
506         ret = sc->id;
507         return ret;
508 }
509
510 int swap_cgroup_swapon(int type, unsigned long max_pages)
511 {
512         void *array;
513         unsigned long array_size;
514         unsigned long length;
515         struct swap_cgroup_ctrl *ctrl;
516
517         if (!do_swap_account)
518                 return 0;
519
520         length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
521         array_size = length * sizeof(void *);
522
523         array = vzalloc(array_size);
524         if (!array)
525                 goto nomem;
526
527         ctrl = &swap_cgroup_ctrl[type];
528         mutex_lock(&swap_cgroup_mutex);
529         ctrl->length = length;
530         ctrl->map = array;
531         spin_lock_init(&ctrl->lock);
532         if (swap_cgroup_prepare(type)) {
533                 /* memory shortage */
534                 ctrl->map = NULL;
535                 ctrl->length = 0;
536                 mutex_unlock(&swap_cgroup_mutex);
537                 vfree(array);
538                 goto nomem;
539         }
540         mutex_unlock(&swap_cgroup_mutex);
541
542         return 0;
543 nomem:
544         printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n");
545         printk(KERN_INFO
546                 "swap_cgroup can be disabled by swapaccount=0 boot option\n");
547         return -ENOMEM;
548 }
549
550 void swap_cgroup_swapoff(int type)
551 {
552         struct page **map;
553         unsigned long i, length;
554         struct swap_cgroup_ctrl *ctrl;
555
556         if (!do_swap_account)
557                 return;
558
559         mutex_lock(&swap_cgroup_mutex);
560         ctrl = &swap_cgroup_ctrl[type];
561         map = ctrl->map;
562         length = ctrl->length;
563         ctrl->map = NULL;
564         ctrl->length = 0;
565         mutex_unlock(&swap_cgroup_mutex);
566
567         if (map) {
568                 for (i = 0; i < length; i++) {
569                         struct page *page = map[i];
570                         if (page)
571                                 __free_page(page);
572                 }
573                 vfree(map);
574         }
575 }
576
577 #endif