memcg: fix page_cgroup fatal error in FLATMEM
[linux-2.6.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
13 static void __meminit
14 __init_page_cgroup(struct page_cgroup *pc, unsigned long pfn)
15 {
16         pc->flags = 0;
17         pc->mem_cgroup = NULL;
18         pc->page = pfn_to_page(pfn);
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 static int __init alloc_node_page_cgroup(int nid)
46 {
47         struct page_cgroup *base, *pc;
48         unsigned long table_size;
49         unsigned long start_pfn, nr_pages, index;
50
51         start_pfn = NODE_DATA(nid)->node_start_pfn;
52         nr_pages = NODE_DATA(nid)->node_spanned_pages;
53
54         if (!nr_pages)
55                 return 0;
56
57         table_size = sizeof(struct page_cgroup) * nr_pages;
58
59         base = __alloc_bootmem_node_nopanic(NODE_DATA(nid),
60                         table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
61         if (!base)
62                 return -ENOMEM;
63         for (index = 0; index < nr_pages; index++) {
64                 pc = base + index;
65                 __init_page_cgroup(pc, start_pfn + index);
66         }
67         NODE_DATA(nid)->node_page_cgroup = base;
68         total_usage += table_size;
69         return 0;
70 }
71
72 void __init page_cgroup_init_flatmem(void)
73 {
74
75         int nid, fail;
76
77         if (mem_cgroup_disabled())
78                 return;
79
80         for_each_online_node(nid)  {
81                 fail = alloc_node_page_cgroup(nid);
82                 if (fail)
83                         goto fail;
84         }
85         printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
86         printk(KERN_INFO "please try cgroup_disable=memory option if you"
87         " don't want\n");
88         return;
89 fail:
90         printk(KERN_CRIT "allocation of page_cgroup was failed.\n");
91         printk(KERN_CRIT "please try cgroup_disable=memory boot option\n");
92         panic("Out of memory");
93 }
94
95 #else /* CONFIG_FLAT_NODE_MEM_MAP */
96
97 struct page_cgroup *lookup_page_cgroup(struct page *page)
98 {
99         unsigned long pfn = page_to_pfn(page);
100         struct mem_section *section = __pfn_to_section(pfn);
101
102         return section->page_cgroup + pfn;
103 }
104
105 /* __alloc_bootmem...() is protected by !slab_available() */
106 static int __init_refok init_section_page_cgroup(unsigned long pfn)
107 {
108         struct mem_section *section = __pfn_to_section(pfn);
109         struct page_cgroup *base, *pc;
110         unsigned long table_size;
111         int nid, index;
112
113         if (!section->page_cgroup) {
114                 nid = page_to_nid(pfn_to_page(pfn));
115                 table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
116                 VM_BUG_ON(!slab_is_available());
117                 base = kmalloc_node(table_size,
118                                 GFP_KERNEL | __GFP_NOWARN, nid);
119                 if (!base)
120                         base = vmalloc_node(table_size, nid);
121         } else {
122                 /*
123                  * We don't have to allocate page_cgroup again, but
124                  * address of memmap may be changed. So, we have to initialize
125                  * again.
126                  */
127                 base = section->page_cgroup + pfn;
128                 table_size = 0;
129                 /* check address of memmap is changed or not. */
130                 if (base->page == pfn_to_page(pfn))
131                         return 0;
132         }
133
134         if (!base) {
135                 printk(KERN_ERR "page cgroup allocation failure\n");
136                 return -ENOMEM;
137         }
138
139         for (index = 0; index < PAGES_PER_SECTION; index++) {
140                 pc = base + index;
141                 __init_page_cgroup(pc, pfn + index);
142         }
143
144         section->page_cgroup = base - pfn;
145         total_usage += table_size;
146         return 0;
147 }
148 #ifdef CONFIG_MEMORY_HOTPLUG
149 void __free_page_cgroup(unsigned long pfn)
150 {
151         struct mem_section *ms;
152         struct page_cgroup *base;
153
154         ms = __pfn_to_section(pfn);
155         if (!ms || !ms->page_cgroup)
156                 return;
157         base = ms->page_cgroup + pfn;
158         if (is_vmalloc_addr(base)) {
159                 vfree(base);
160                 ms->page_cgroup = NULL;
161         } else {
162                 struct page *page = virt_to_page(base);
163                 if (!PageReserved(page)) { /* Is bootmem ? */
164                         kfree(base);
165                         ms->page_cgroup = NULL;
166                 }
167         }
168 }
169
170 int __meminit online_page_cgroup(unsigned long start_pfn,
171                         unsigned long nr_pages,
172                         int nid)
173 {
174         unsigned long start, end, pfn;
175         int fail = 0;
176
177         start = start_pfn & ~(PAGES_PER_SECTION - 1);
178         end = ALIGN(start_pfn + nr_pages, PAGES_PER_SECTION);
179
180         for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
181                 if (!pfn_present(pfn))
182                         continue;
183                 fail = init_section_page_cgroup(pfn);
184         }
185         if (!fail)
186                 return 0;
187
188         /* rollback */
189         for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
190                 __free_page_cgroup(pfn);
191
192         return -ENOMEM;
193 }
194
195 int __meminit offline_page_cgroup(unsigned long start_pfn,
196                 unsigned long nr_pages, int nid)
197 {
198         unsigned long start, end, pfn;
199
200         start = start_pfn & ~(PAGES_PER_SECTION - 1);
201         end = ALIGN(start_pfn + nr_pages, PAGES_PER_SECTION);
202
203         for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
204                 __free_page_cgroup(pfn);
205         return 0;
206
207 }
208
209 static int __meminit page_cgroup_callback(struct notifier_block *self,
210                                unsigned long action, void *arg)
211 {
212         struct memory_notify *mn = arg;
213         int ret = 0;
214         switch (action) {
215         case MEM_GOING_ONLINE:
216                 ret = online_page_cgroup(mn->start_pfn,
217                                    mn->nr_pages, mn->status_change_nid);
218                 break;
219         case MEM_OFFLINE:
220                 offline_page_cgroup(mn->start_pfn,
221                                 mn->nr_pages, mn->status_change_nid);
222                 break;
223         case MEM_CANCEL_ONLINE:
224         case MEM_GOING_OFFLINE:
225                 break;
226         case MEM_ONLINE:
227         case MEM_CANCEL_OFFLINE:
228                 break;
229         }
230
231         if (ret)
232                 ret = notifier_from_errno(ret);
233         else
234                 ret = NOTIFY_OK;
235
236         return ret;
237 }
238
239 #endif
240
241 void __init page_cgroup_init(void)
242 {
243         unsigned long pfn;
244         int fail = 0;
245
246         if (mem_cgroup_disabled())
247                 return;
248
249         for (pfn = 0; !fail && pfn < max_pfn; pfn += PAGES_PER_SECTION) {
250                 if (!pfn_present(pfn))
251                         continue;
252                 fail = init_section_page_cgroup(pfn);
253         }
254         if (fail) {
255                 printk(KERN_CRIT "try cgroup_disable=memory boot option\n");
256                 panic("Out of memory");
257         } else {
258                 hotplug_memory_notifier(page_cgroup_callback, 0);
259         }
260         printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
261         printk(KERN_INFO "please try cgroup_disable=memory option if you don't"
262         " want\n");
263 }
264
265 void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
266 {
267         return;
268 }
269
270 #endif
271
272
273 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
274
275 static DEFINE_MUTEX(swap_cgroup_mutex);
276 struct swap_cgroup_ctrl {
277         struct page **map;
278         unsigned long length;
279 };
280
281 struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
282
283 struct swap_cgroup {
284         unsigned short          id;
285 };
286 #define SC_PER_PAGE     (PAGE_SIZE/sizeof(struct swap_cgroup))
287 #define SC_POS_MASK     (SC_PER_PAGE - 1)
288
289 /*
290  * SwapCgroup implements "lookup" and "exchange" operations.
291  * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
292  * against SwapCache. At swap_free(), this is accessed directly from swap.
293  *
294  * This means,
295  *  - we have no race in "exchange" when we're accessed via SwapCache because
296  *    SwapCache(and its swp_entry) is under lock.
297  *  - When called via swap_free(), there is no user of this entry and no race.
298  * Then, we don't need lock around "exchange".
299  *
300  * TODO: we can push these buffers out to HIGHMEM.
301  */
302
303 /*
304  * allocate buffer for swap_cgroup.
305  */
306 static int swap_cgroup_prepare(int type)
307 {
308         struct page *page;
309         struct swap_cgroup_ctrl *ctrl;
310         unsigned long idx, max;
311
312         if (!do_swap_account)
313                 return 0;
314         ctrl = &swap_cgroup_ctrl[type];
315
316         for (idx = 0; idx < ctrl->length; idx++) {
317                 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
318                 if (!page)
319                         goto not_enough_page;
320                 ctrl->map[idx] = page;
321         }
322         return 0;
323 not_enough_page:
324         max = idx;
325         for (idx = 0; idx < max; idx++)
326                 __free_page(ctrl->map[idx]);
327
328         return -ENOMEM;
329 }
330
331 /**
332  * swap_cgroup_record - record mem_cgroup for this swp_entry.
333  * @ent: swap entry to be recorded into
334  * @mem: mem_cgroup to be recorded
335  *
336  * Returns old value at success, 0 at failure.
337  * (Of course, old value can be 0.)
338  */
339 unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
340 {
341         int type = swp_type(ent);
342         unsigned long offset = swp_offset(ent);
343         unsigned long idx = offset / SC_PER_PAGE;
344         unsigned long pos = offset & SC_POS_MASK;
345         struct swap_cgroup_ctrl *ctrl;
346         struct page *mappage;
347         struct swap_cgroup *sc;
348         unsigned short old;
349
350         if (!do_swap_account)
351                 return 0;
352
353         ctrl = &swap_cgroup_ctrl[type];
354
355         mappage = ctrl->map[idx];
356         sc = page_address(mappage);
357         sc += pos;
358         old = sc->id;
359         sc->id = id;
360
361         return old;
362 }
363
364 /**
365  * lookup_swap_cgroup - lookup mem_cgroup tied to swap entry
366  * @ent: swap entry to be looked up.
367  *
368  * Returns CSS ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
369  */
370 unsigned short lookup_swap_cgroup(swp_entry_t ent)
371 {
372         int type = swp_type(ent);
373         unsigned long offset = swp_offset(ent);
374         unsigned long idx = offset / SC_PER_PAGE;
375         unsigned long pos = offset & SC_POS_MASK;
376         struct swap_cgroup_ctrl *ctrl;
377         struct page *mappage;
378         struct swap_cgroup *sc;
379         unsigned short ret;
380
381         if (!do_swap_account)
382                 return 0;
383
384         ctrl = &swap_cgroup_ctrl[type];
385         mappage = ctrl->map[idx];
386         sc = page_address(mappage);
387         sc += pos;
388         ret = sc->id;
389         return ret;
390 }
391
392 int swap_cgroup_swapon(int type, unsigned long max_pages)
393 {
394         void *array;
395         unsigned long array_size;
396         unsigned long length;
397         struct swap_cgroup_ctrl *ctrl;
398
399         if (!do_swap_account)
400                 return 0;
401
402         length = ((max_pages/SC_PER_PAGE) + 1);
403         array_size = length * sizeof(void *);
404
405         array = vmalloc(array_size);
406         if (!array)
407                 goto nomem;
408
409         memset(array, 0, array_size);
410         ctrl = &swap_cgroup_ctrl[type];
411         mutex_lock(&swap_cgroup_mutex);
412         ctrl->length = length;
413         ctrl->map = array;
414         if (swap_cgroup_prepare(type)) {
415                 /* memory shortage */
416                 ctrl->map = NULL;
417                 ctrl->length = 0;
418                 vfree(array);
419                 mutex_unlock(&swap_cgroup_mutex);
420                 goto nomem;
421         }
422         mutex_unlock(&swap_cgroup_mutex);
423
424         return 0;
425 nomem:
426         printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n");
427         printk(KERN_INFO
428                 "swap_cgroup can be disabled by noswapaccount boot option\n");
429         return -ENOMEM;
430 }
431
432 void swap_cgroup_swapoff(int type)
433 {
434         int i;
435         struct swap_cgroup_ctrl *ctrl;
436
437         if (!do_swap_account)
438                 return;
439
440         mutex_lock(&swap_cgroup_mutex);
441         ctrl = &swap_cgroup_ctrl[type];
442         if (ctrl->map) {
443                 for (i = 0; i < ctrl->length; i++) {
444                         struct page *page = ctrl->map[i];
445                         if (page)
446                                 __free_page(page);
447                 }
448                 vfree(ctrl->map);
449                 ctrl->map = NULL;
450                 ctrl->length = 0;
451         }
452         mutex_unlock(&swap_cgroup_mutex);
453 }
454
455 #endif