blob: f0559e049e002e48eda0160c41a71abc18a95460 [file] [log] [blame]
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001#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>
KAMEZAWA Hiroyuki94b6da52008-10-22 14:15:05 -07007#include <linux/slab.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07008#include <linux/memory.h>
Paul Mundt4c8210422008-10-22 14:14:58 -07009#include <linux/vmalloc.h>
KAMEZAWA Hiroyuki94b6da52008-10-22 14:15:05 -070010#include <linux/cgroup.h>
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080011#include <linux/swapops.h>
Catalin Marinas7952f982010-07-19 11:54:14 +010012#include <linux/kmemleak.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070013
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070014static unsigned long total_usage;
15
16#if !defined(CONFIG_SPARSEMEM)
17
18
Al Viro31168482008-11-22 17:33:24 +000019void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070020{
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 if (unlikely(!base))
32 return NULL;
33
34 offset = pfn - NODE_DATA(page_to_nid(page))->node_start_pfn;
35 return base + offset;
36}
37
38static int __init alloc_node_page_cgroup(int nid)
39{
Johannes Weiner6b208e32012-01-12 17:18:18 -080040 struct page_cgroup *base;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070041 unsigned long table_size;
Johannes Weiner6b208e32012-01-12 17:18:18 -080042 unsigned long nr_pages;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070043
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070044 nr_pages = NODE_DATA(nid)->node_spanned_pages;
KAMEZAWA Hiroyuki653d22c2008-12-09 13:14:20 -080045 if (!nr_pages)
46 return 0;
47
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070048 table_size = sizeof(struct page_cgroup) * nr_pages;
KAMEZAWA Hiroyukica371c02009-06-12 10:33:53 +030049
50 base = __alloc_bootmem_node_nopanic(NODE_DATA(nid),
51 table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
52 if (!base)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070053 return -ENOMEM;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070054 NODE_DATA(nid)->node_page_cgroup = base;
55 total_usage += table_size;
56 return 0;
57}
58
KAMEZAWA Hiroyukica371c02009-06-12 10:33:53 +030059void __init page_cgroup_init_flatmem(void)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070060{
61
62 int nid, fail;
63
Hirokazu Takahashif8d66542009-01-07 18:08:02 -080064 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki94b6da52008-10-22 14:15:05 -070065 return;
66
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070067 for_each_online_node(nid) {
68 fail = alloc_node_page_cgroup(nid);
69 if (fail)
70 goto fail;
71 }
72 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
Randy Dunlap8ca739e2009-06-17 16:26:32 -070073 printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
74 " don't want memory cgroups\n");
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070075 return;
76fail:
Randy Dunlap8ca739e2009-06-17 16:26:32 -070077 printk(KERN_CRIT "allocation of page_cgroup failed.\n");
78 printk(KERN_CRIT "please try 'cgroup_disable=memory' boot option\n");
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070079 panic("Out of memory");
80}
81
82#else /* CONFIG_FLAT_NODE_MEM_MAP */
83
84struct page_cgroup *lookup_page_cgroup(struct page *page)
85{
86 unsigned long pfn = page_to_pfn(page);
87 struct mem_section *section = __pfn_to_section(pfn);
88
Balbir Singhd69b0422009-06-17 16:26:34 -070089 if (!section->page_cgroup)
90 return NULL;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070091 return section->page_cgroup + pfn;
92}
93
Namhyung Kim268433b2011-05-26 16:25:29 -070094static void *__meminit alloc_page_cgroup(size_t size, int nid)
Michal Hockodde79e02011-03-23 16:42:40 -070095{
Johannes Weiner6b208e32012-01-12 17:18:18 -080096 gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
Michal Hockodde79e02011-03-23 16:42:40 -070097 void *addr = NULL;
98
Steven Rostedtff7ee932011-11-02 13:38:11 -070099 addr = alloc_pages_exact_nid(nid, size, flags);
100 if (addr) {
101 kmemleak_alloc(addr, size, 1, flags);
Michal Hockodde79e02011-03-23 16:42:40 -0700102 return addr;
Steven Rostedtff7ee932011-11-02 13:38:11 -0700103 }
Michal Hockodde79e02011-03-23 16:42:40 -0700104
105 if (node_state(nid, N_HIGH_MEMORY))
Johannes Weiner6b208e32012-01-12 17:18:18 -0800106 addr = vzalloc_node(size, nid);
Michal Hockodde79e02011-03-23 16:42:40 -0700107 else
Johannes Weiner6b208e32012-01-12 17:18:18 -0800108 addr = vzalloc(size);
Michal Hockodde79e02011-03-23 16:42:40 -0700109
110 return addr;
111}
112
113#ifdef CONFIG_MEMORY_HOTPLUG
114static void free_page_cgroup(void *addr)
115{
116 if (is_vmalloc_addr(addr)) {
117 vfree(addr);
118 } else {
119 struct page *page = virt_to_page(addr);
Michal Hocko6cfddb22011-03-23 16:42:41 -0700120 size_t table_size =
121 sizeof(struct page_cgroup) * PAGES_PER_SECTION;
122
123 BUG_ON(PageReserved(page));
124 free_pages_exact(addr, table_size);
Michal Hockodde79e02011-03-23 16:42:40 -0700125 }
126}
127#endif
128
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700129static int __meminit init_section_page_cgroup(unsigned long pfn, int nid)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700130{
Johannes Weiner6b3ae582011-03-23 16:42:30 -0700131 struct mem_section *section;
Johannes Weiner6b208e32012-01-12 17:18:18 -0800132 struct page_cgroup *base;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700133 unsigned long table_size;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700134
Johannes Weiner6b208e32012-01-12 17:18:18 -0800135 section = __pfn_to_section(pfn);
Johannes Weiner6b3ae582011-03-23 16:42:30 -0700136
137 if (section->page_cgroup)
138 return 0;
139
Johannes Weiner6b3ae582011-03-23 16:42:30 -0700140 table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
Michal Hockodde79e02011-03-23 16:42:40 -0700141 base = alloc_page_cgroup(table_size, nid);
142
Johannes Weiner6b3ae582011-03-23 16:42:30 -0700143 /*
144 * The value stored in section->page_cgroup is (base - pfn)
145 * and it does not point to the memory block allocated above,
146 * causing kmemleak false positives.
147 */
148 kmemleak_not_leak(base);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700149
150 if (!base) {
151 printk(KERN_ERR "page cgroup allocation failure\n");
152 return -ENOMEM;
153 }
154
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700155 /*
156 * The passed "pfn" may not be aligned to SECTION. For the calculation
157 * we need to apply a mask.
158 */
159 pfn &= PAGE_SECTION_MASK;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700160 section->page_cgroup = base - pfn;
161 total_usage += table_size;
162 return 0;
163}
164#ifdef CONFIG_MEMORY_HOTPLUG
165void __free_page_cgroup(unsigned long pfn)
166{
167 struct mem_section *ms;
168 struct page_cgroup *base;
169
170 ms = __pfn_to_section(pfn);
171 if (!ms || !ms->page_cgroup)
172 return;
173 base = ms->page_cgroup + pfn;
Michal Hockodde79e02011-03-23 16:42:40 -0700174 free_page_cgroup(base);
175 ms->page_cgroup = NULL;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700176}
177
Al Viro31168482008-11-22 17:33:24 +0000178int __meminit online_page_cgroup(unsigned long start_pfn,
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700179 unsigned long nr_pages,
180 int nid)
181{
182 unsigned long start, end, pfn;
183 int fail = 0;
184
Daniel Kiper1bb36fb2011-07-25 17:12:13 -0700185 start = SECTION_ALIGN_DOWN(start_pfn);
186 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700187
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700188 if (nid == -1) {
189 /*
190 * In this case, "nid" already exists and contains valid memory.
191 * "start_pfn" passed to us is a pfn which is an arg for
192 * online__pages(), and start_pfn should exist.
193 */
194 nid = pfn_to_nid(start_pfn);
195 VM_BUG_ON(!node_state(nid, N_ONLINE));
196 }
197
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700198 for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
199 if (!pfn_present(pfn))
200 continue;
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700201 fail = init_section_page_cgroup(pfn, nid);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700202 }
203 if (!fail)
204 return 0;
205
206 /* rollback */
207 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
208 __free_page_cgroup(pfn);
209
210 return -ENOMEM;
211}
212
Al Viro31168482008-11-22 17:33:24 +0000213int __meminit offline_page_cgroup(unsigned long start_pfn,
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700214 unsigned long nr_pages, int nid)
215{
216 unsigned long start, end, pfn;
217
Daniel Kiper1bb36fb2011-07-25 17:12:13 -0700218 start = SECTION_ALIGN_DOWN(start_pfn);
219 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700220
221 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
222 __free_page_cgroup(pfn);
223 return 0;
224
225}
226
Al Viro31168482008-11-22 17:33:24 +0000227static int __meminit page_cgroup_callback(struct notifier_block *self,
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700228 unsigned long action, void *arg)
229{
230 struct memory_notify *mn = arg;
231 int ret = 0;
232 switch (action) {
233 case MEM_GOING_ONLINE:
234 ret = online_page_cgroup(mn->start_pfn,
235 mn->nr_pages, mn->status_change_nid);
236 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700237 case MEM_OFFLINE:
238 offline_page_cgroup(mn->start_pfn,
239 mn->nr_pages, mn->status_change_nid);
240 break;
KAMEZAWA Hiroyukidc19f9d2008-12-01 13:13:48 -0800241 case MEM_CANCEL_ONLINE:
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700242 case MEM_GOING_OFFLINE:
243 break;
244 case MEM_ONLINE:
245 case MEM_CANCEL_OFFLINE:
246 break;
247 }
KAMEZAWA Hiroyukidc19f9d2008-12-01 13:13:48 -0800248
Prarit Bhargava5fda1bd2011-03-22 16:30:49 -0700249 return notifier_from_errno(ret);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700250}
251
252#endif
253
254void __init page_cgroup_init(void)
255{
256 unsigned long pfn;
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700257 int nid;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700258
Hirokazu Takahashif8d66542009-01-07 18:08:02 -0800259 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki94b6da52008-10-22 14:15:05 -0700260 return;
261
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700262 for_each_node_state(nid, N_HIGH_MEMORY) {
263 unsigned long start_pfn, end_pfn;
264
265 start_pfn = node_start_pfn(nid);
266 end_pfn = node_end_pfn(nid);
267 /*
268 * start_pfn and end_pfn may not be aligned to SECTION and the
269 * page->flags of out of node pages are not initialized. So we
270 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
271 */
272 for (pfn = start_pfn;
273 pfn < end_pfn;
274 pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
275
276 if (!pfn_valid(pfn))
277 continue;
278 /*
279 * Nodes's pfns can be overlapping.
280 * We know some arch can have a nodes layout such as
281 * -------------pfn-------------->
282 * N0 | N1 | N2 | N0 | N1 | N2|....
283 */
284 if (pfn_to_nid(pfn) != nid)
285 continue;
286 if (init_section_page_cgroup(pfn, nid))
287 goto oom;
288 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700289 }
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700290 hotplug_memory_notifier(page_cgroup_callback, 0);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700291 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700292 printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
293 "don't want memory cgroups\n");
294 return;
295oom:
296 printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
297 panic("Out of memory");
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700298}
299
Al Viro31168482008-11-22 17:33:24 +0000300void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700301{
302 return;
303}
304
305#endif
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800306
307
308#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
309
310static DEFINE_MUTEX(swap_cgroup_mutex);
311struct swap_cgroup_ctrl {
312 struct page **map;
313 unsigned long length;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400314 spinlock_t lock;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800315};
316
H Hartley Sweeten61600f52011-11-02 13:38:36 -0700317static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800318
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800319struct swap_cgroup {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700320 unsigned short id;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800321};
322#define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
323#define SC_POS_MASK (SC_PER_PAGE - 1)
324
325/*
326 * SwapCgroup implements "lookup" and "exchange" operations.
327 * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
328 * against SwapCache. At swap_free(), this is accessed directly from swap.
329 *
330 * This means,
331 * - we have no race in "exchange" when we're accessed via SwapCache because
332 * SwapCache(and its swp_entry) is under lock.
333 * - When called via swap_free(), there is no user of this entry and no race.
334 * Then, we don't need lock around "exchange".
335 *
336 * TODO: we can push these buffers out to HIGHMEM.
337 */
338
339/*
340 * allocate buffer for swap_cgroup.
341 */
342static int swap_cgroup_prepare(int type)
343{
344 struct page *page;
345 struct swap_cgroup_ctrl *ctrl;
346 unsigned long idx, max;
347
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800348 ctrl = &swap_cgroup_ctrl[type];
349
350 for (idx = 0; idx < ctrl->length; idx++) {
351 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
352 if (!page)
353 goto not_enough_page;
354 ctrl->map[idx] = page;
355 }
356 return 0;
357not_enough_page:
358 max = idx;
359 for (idx = 0; idx < max; idx++)
360 __free_page(ctrl->map[idx]);
361
362 return -ENOMEM;
363}
364
365/**
Daisuke Nishimura02491442010-03-10 15:22:17 -0800366 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
367 * @end: swap entry to be cmpxchged
368 * @old: old id
369 * @new: new id
370 *
371 * Returns old id at success, 0 at failure.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300372 * (There is no mem_cgroup using 0 as its id)
Daisuke Nishimura02491442010-03-10 15:22:17 -0800373 */
374unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
375 unsigned short old, unsigned short new)
376{
377 int type = swp_type(ent);
378 unsigned long offset = swp_offset(ent);
379 unsigned long idx = offset / SC_PER_PAGE;
380 unsigned long pos = offset & SC_POS_MASK;
381 struct swap_cgroup_ctrl *ctrl;
382 struct page *mappage;
383 struct swap_cgroup *sc;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400384 unsigned long flags;
385 unsigned short retval;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800386
387 ctrl = &swap_cgroup_ctrl[type];
388
389 mappage = ctrl->map[idx];
390 sc = page_address(mappage);
391 sc += pos;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400392 spin_lock_irqsave(&ctrl->lock, flags);
393 retval = sc->id;
394 if (retval == old)
395 sc->id = new;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800396 else
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400397 retval = 0;
398 spin_unlock_irqrestore(&ctrl->lock, flags);
399 return retval;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800400}
401
402/**
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800403 * swap_cgroup_record - record mem_cgroup for this swp_entry.
404 * @ent: swap entry to be recorded into
405 * @mem: mem_cgroup to be recorded
406 *
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700407 * Returns old value at success, 0 at failure.
408 * (Of course, old value can be 0.)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800409 */
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700410unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800411{
412 int type = swp_type(ent);
413 unsigned long offset = swp_offset(ent);
414 unsigned long idx = offset / SC_PER_PAGE;
415 unsigned long pos = offset & SC_POS_MASK;
416 struct swap_cgroup_ctrl *ctrl;
417 struct page *mappage;
418 struct swap_cgroup *sc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700419 unsigned short old;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400420 unsigned long flags;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800421
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800422 ctrl = &swap_cgroup_ctrl[type];
423
424 mappage = ctrl->map[idx];
425 sc = page_address(mappage);
426 sc += pos;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400427 spin_lock_irqsave(&ctrl->lock, flags);
428 old = sc->id;
429 sc->id = id;
430 spin_unlock_irqrestore(&ctrl->lock, flags);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800431
432 return old;
433}
434
435/**
436 * lookup_swap_cgroup - lookup mem_cgroup tied to swap entry
437 * @ent: swap entry to be looked up.
438 *
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700439 * Returns CSS ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800440 */
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700441unsigned short lookup_swap_cgroup(swp_entry_t ent)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800442{
443 int type = swp_type(ent);
444 unsigned long offset = swp_offset(ent);
445 unsigned long idx = offset / SC_PER_PAGE;
446 unsigned long pos = offset & SC_POS_MASK;
447 struct swap_cgroup_ctrl *ctrl;
448 struct page *mappage;
449 struct swap_cgroup *sc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700450 unsigned short ret;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800451
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800452 ctrl = &swap_cgroup_ctrl[type];
453 mappage = ctrl->map[idx];
454 sc = page_address(mappage);
455 sc += pos;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700456 ret = sc->id;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800457 return ret;
458}
459
460int swap_cgroup_swapon(int type, unsigned long max_pages)
461{
462 void *array;
463 unsigned long array_size;
464 unsigned long length;
465 struct swap_cgroup_ctrl *ctrl;
466
467 if (!do_swap_account)
468 return 0;
469
Namhyung Kim33278f72011-05-26 16:25:30 -0700470 length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800471 array_size = length * sizeof(void *);
472
Joe Perches8c1fec12011-05-28 10:36:34 -0700473 array = vzalloc(array_size);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800474 if (!array)
475 goto nomem;
476
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800477 ctrl = &swap_cgroup_ctrl[type];
478 mutex_lock(&swap_cgroup_mutex);
479 ctrl->length = length;
480 ctrl->map = array;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400481 spin_lock_init(&ctrl->lock);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800482 if (swap_cgroup_prepare(type)) {
483 /* memory shortage */
484 ctrl->map = NULL;
485 ctrl->length = 0;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800486 mutex_unlock(&swap_cgroup_mutex);
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700487 vfree(array);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800488 goto nomem;
489 }
490 mutex_unlock(&swap_cgroup_mutex);
491
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800492 return 0;
493nomem:
494 printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n");
495 printk(KERN_INFO
WANG Cong00a66d22011-07-25 17:12:12 -0700496 "swap_cgroup can be disabled by swapaccount=0 boot option\n");
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800497 return -ENOMEM;
498}
499
500void swap_cgroup_swapoff(int type)
501{
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700502 struct page **map;
503 unsigned long i, length;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800504 struct swap_cgroup_ctrl *ctrl;
505
506 if (!do_swap_account)
507 return;
508
509 mutex_lock(&swap_cgroup_mutex);
510 ctrl = &swap_cgroup_ctrl[type];
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700511 map = ctrl->map;
512 length = ctrl->length;
513 ctrl->map = NULL;
514 ctrl->length = 0;
515 mutex_unlock(&swap_cgroup_mutex);
516
517 if (map) {
518 for (i = 0; i < length; i++) {
519 struct page *page = map[i];
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800520 if (page)
521 __free_page(page);
522 }
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700523 vfree(map);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800524 }
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800525}
526
527#endif