blob: e910524e5a086c1969f25f70fa00777ff6c706e7 [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;
Johannes Weiner00c54c02012-01-12 17:18:40 -080031#ifdef CONFIG_DEBUG_VM
32 /*
33 * The sanity checks the page allocator does upon freeing a
34 * page can reach here before the page_cgroup arrays are
35 * allocated when feeding a range of pages to the allocator
36 * for the first time during bootup or memory hotplug.
37 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070038 if (unlikely(!base))
39 return NULL;
Johannes Weiner00c54c02012-01-12 17:18:40 -080040#endif
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070041 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{
Johannes Weiner6b208e32012-01-12 17:18:18 -080047 struct page_cgroup *base;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070048 unsigned long table_size;
Johannes Weiner6b208e32012-01-12 17:18:18 -080049 unsigned long nr_pages;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070050
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070051 nr_pages = NODE_DATA(nid)->node_spanned_pages;
KAMEZAWA Hiroyuki653d22c2008-12-09 13:14:20 -080052 if (!nr_pages)
53 return 0;
54
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070055 table_size = sizeof(struct page_cgroup) * nr_pages;
KAMEZAWA Hiroyukica371c02009-06-12 10:33:53 +030056
57 base = __alloc_bootmem_node_nopanic(NODE_DATA(nid),
58 table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
59 if (!base)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070060 return -ENOMEM;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070061 NODE_DATA(nid)->node_page_cgroup = base;
62 total_usage += table_size;
63 return 0;
64}
65
KAMEZAWA Hiroyukica371c02009-06-12 10:33:53 +030066void __init page_cgroup_init_flatmem(void)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070067{
68
69 int nid, fail;
70
Hirokazu Takahashif8d665422009-01-07 18:08:02 -080071 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki94b6da52008-10-22 14:15:05 -070072 return;
73
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070074 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);
Randy Dunlap8ca739e2009-06-17 16:26:32 -070080 printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
81 " don't want memory cgroups\n");
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070082 return;
83fail:
Randy Dunlap8ca739e2009-06-17 16:26:32 -070084 printk(KERN_CRIT "allocation of page_cgroup failed.\n");
85 printk(KERN_CRIT "please try 'cgroup_disable=memory' boot option\n");
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070086 panic("Out of memory");
87}
88
89#else /* CONFIG_FLAT_NODE_MEM_MAP */
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);
Johannes Weiner00c54c02012-01-12 17:18:40 -080095#ifdef CONFIG_DEBUG_VM
96 /*
97 * The sanity checks the page allocator does upon freeing a
98 * page can reach here before the page_cgroup arrays are
99 * allocated when feeding a range of pages to the allocator
100 * for the first time during bootup or memory hotplug.
101 */
Balbir Singhd69b0422009-06-17 16:26:34 -0700102 if (!section->page_cgroup)
103 return NULL;
Johannes Weiner00c54c02012-01-12 17:18:40 -0800104#endif
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700105 return section->page_cgroup + pfn;
106}
107
Namhyung Kim268433b2011-05-26 16:25:29 -0700108static void *__meminit alloc_page_cgroup(size_t size, int nid)
Michal Hockodde79e02011-03-23 16:42:40 -0700109{
Johannes Weiner6b208e32012-01-12 17:18:18 -0800110 gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
Michal Hockodde79e02011-03-23 16:42:40 -0700111 void *addr = NULL;
112
Steven Rostedtff7ee932011-11-02 13:38:11 -0700113 addr = alloc_pages_exact_nid(nid, size, flags);
114 if (addr) {
115 kmemleak_alloc(addr, size, 1, flags);
Michal Hockodde79e02011-03-23 16:42:40 -0700116 return addr;
Steven Rostedtff7ee932011-11-02 13:38:11 -0700117 }
Michal Hockodde79e02011-03-23 16:42:40 -0700118
119 if (node_state(nid, N_HIGH_MEMORY))
Johannes Weiner6b208e32012-01-12 17:18:18 -0800120 addr = vzalloc_node(size, nid);
Michal Hockodde79e02011-03-23 16:42:40 -0700121 else
Johannes Weiner6b208e32012-01-12 17:18:18 -0800122 addr = vzalloc(size);
Michal Hockodde79e02011-03-23 16:42:40 -0700123
124 return addr;
125}
126
127#ifdef CONFIG_MEMORY_HOTPLUG
128static void free_page_cgroup(void *addr)
129{
130 if (is_vmalloc_addr(addr)) {
131 vfree(addr);
132 } else {
133 struct page *page = virt_to_page(addr);
Michal Hocko6cfddb22011-03-23 16:42:41 -0700134 size_t table_size =
135 sizeof(struct page_cgroup) * PAGES_PER_SECTION;
136
137 BUG_ON(PageReserved(page));
138 free_pages_exact(addr, table_size);
Michal Hockodde79e02011-03-23 16:42:40 -0700139 }
140}
141#endif
142
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700143static int __meminit init_section_page_cgroup(unsigned long pfn, int nid)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700144{
Johannes Weiner6b3ae582011-03-23 16:42:30 -0700145 struct mem_section *section;
Johannes Weiner6b208e32012-01-12 17:18:18 -0800146 struct page_cgroup *base;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700147 unsigned long table_size;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700148
Johannes Weiner6b208e32012-01-12 17:18:18 -0800149 section = __pfn_to_section(pfn);
Johannes Weiner6b3ae582011-03-23 16:42:30 -0700150
151 if (section->page_cgroup)
152 return 0;
153
Johannes Weiner6b3ae582011-03-23 16:42:30 -0700154 table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
Michal Hockodde79e02011-03-23 16:42:40 -0700155 base = alloc_page_cgroup(table_size, nid);
156
Johannes Weiner6b3ae582011-03-23 16:42:30 -0700157 /*
158 * The value stored in section->page_cgroup is (base - pfn)
159 * and it does not point to the memory block allocated above,
160 * causing kmemleak false positives.
161 */
162 kmemleak_not_leak(base);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700163
164 if (!base) {
165 printk(KERN_ERR "page cgroup allocation failure\n");
166 return -ENOMEM;
167 }
168
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700169 /*
170 * The passed "pfn" may not be aligned to SECTION. For the calculation
171 * we need to apply a mask.
172 */
173 pfn &= PAGE_SECTION_MASK;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700174 section->page_cgroup = base - pfn;
175 total_usage += table_size;
176 return 0;
177}
178#ifdef CONFIG_MEMORY_HOTPLUG
179void __free_page_cgroup(unsigned long pfn)
180{
181 struct mem_section *ms;
182 struct page_cgroup *base;
183
184 ms = __pfn_to_section(pfn);
185 if (!ms || !ms->page_cgroup)
186 return;
187 base = ms->page_cgroup + pfn;
Michal Hockodde79e02011-03-23 16:42:40 -0700188 free_page_cgroup(base);
189 ms->page_cgroup = NULL;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700190}
191
Al Viro31168482008-11-22 17:33:24 +0000192int __meminit online_page_cgroup(unsigned long start_pfn,
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700193 unsigned long nr_pages,
194 int nid)
195{
196 unsigned long start, end, pfn;
197 int fail = 0;
198
Daniel Kiper1bb36fb2011-07-25 17:12:13 -0700199 start = SECTION_ALIGN_DOWN(start_pfn);
200 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700201
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700202 if (nid == -1) {
203 /*
204 * In this case, "nid" already exists and contains valid memory.
205 * "start_pfn" passed to us is a pfn which is an arg for
206 * online__pages(), and start_pfn should exist.
207 */
208 nid = pfn_to_nid(start_pfn);
209 VM_BUG_ON(!node_state(nid, N_ONLINE));
210 }
211
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700212 for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
213 if (!pfn_present(pfn))
214 continue;
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700215 fail = init_section_page_cgroup(pfn, nid);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700216 }
217 if (!fail)
218 return 0;
219
220 /* rollback */
221 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
222 __free_page_cgroup(pfn);
223
224 return -ENOMEM;
225}
226
Al Viro31168482008-11-22 17:33:24 +0000227int __meminit offline_page_cgroup(unsigned long start_pfn,
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700228 unsigned long nr_pages, int nid)
229{
230 unsigned long start, end, pfn;
231
Daniel Kiper1bb36fb2011-07-25 17:12:13 -0700232 start = SECTION_ALIGN_DOWN(start_pfn);
233 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700234
235 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
236 __free_page_cgroup(pfn);
237 return 0;
238
239}
240
Al Viro31168482008-11-22 17:33:24 +0000241static int __meminit page_cgroup_callback(struct notifier_block *self,
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700242 unsigned long action, void *arg)
243{
244 struct memory_notify *mn = arg;
245 int ret = 0;
246 switch (action) {
247 case MEM_GOING_ONLINE:
248 ret = online_page_cgroup(mn->start_pfn,
249 mn->nr_pages, mn->status_change_nid);
250 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700251 case MEM_OFFLINE:
252 offline_page_cgroup(mn->start_pfn,
253 mn->nr_pages, mn->status_change_nid);
254 break;
KAMEZAWA Hiroyukidc19f9d2008-12-01 13:13:48 -0800255 case MEM_CANCEL_ONLINE:
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700256 case MEM_GOING_OFFLINE:
257 break;
258 case MEM_ONLINE:
259 case MEM_CANCEL_OFFLINE:
260 break;
261 }
KAMEZAWA Hiroyukidc19f9d2008-12-01 13:13:48 -0800262
Prarit Bhargava5fda1bd2011-03-22 16:30:49 -0700263 return notifier_from_errno(ret);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700264}
265
266#endif
267
268void __init page_cgroup_init(void)
269{
270 unsigned long pfn;
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700271 int nid;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700272
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800273 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki94b6da52008-10-22 14:15:05 -0700274 return;
275
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700276 for_each_node_state(nid, N_HIGH_MEMORY) {
277 unsigned long start_pfn, end_pfn;
278
279 start_pfn = node_start_pfn(nid);
280 end_pfn = node_end_pfn(nid);
281 /*
282 * start_pfn and end_pfn may not be aligned to SECTION and the
283 * page->flags of out of node pages are not initialized. So we
284 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
285 */
286 for (pfn = start_pfn;
287 pfn < end_pfn;
288 pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
289
290 if (!pfn_valid(pfn))
291 continue;
292 /*
293 * Nodes's pfns can be overlapping.
294 * We know some arch can have a nodes layout such as
295 * -------------pfn-------------->
296 * N0 | N1 | N2 | N0 | N1 | N2|....
297 */
298 if (pfn_to_nid(pfn) != nid)
299 continue;
300 if (init_section_page_cgroup(pfn, nid))
301 goto oom;
302 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700303 }
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700304 hotplug_memory_notifier(page_cgroup_callback, 0);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700305 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700306 printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
307 "don't want memory cgroups\n");
308 return;
309oom:
310 printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
311 panic("Out of memory");
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700312}
313
Al Viro31168482008-11-22 17:33:24 +0000314void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700315{
316 return;
317}
318
319#endif
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800320
321
322#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
323
324static DEFINE_MUTEX(swap_cgroup_mutex);
325struct swap_cgroup_ctrl {
326 struct page **map;
327 unsigned long length;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400328 spinlock_t lock;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800329};
330
H Hartley Sweeten61600f52011-11-02 13:38:36 -0700331static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800332
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800333struct swap_cgroup {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700334 unsigned short id;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800335};
336#define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
337#define SC_POS_MASK (SC_PER_PAGE - 1)
338
339/*
340 * SwapCgroup implements "lookup" and "exchange" operations.
341 * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
342 * against SwapCache. At swap_free(), this is accessed directly from swap.
343 *
344 * This means,
345 * - we have no race in "exchange" when we're accessed via SwapCache because
346 * SwapCache(and its swp_entry) is under lock.
347 * - When called via swap_free(), there is no user of this entry and no race.
348 * Then, we don't need lock around "exchange".
349 *
350 * TODO: we can push these buffers out to HIGHMEM.
351 */
352
353/*
354 * allocate buffer for swap_cgroup.
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
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800362 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
379/**
Daisuke Nishimura02491442010-03-10 15:22:17 -0800380 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
381 * @end: swap entry to be cmpxchged
382 * @old: old id
383 * @new: new id
384 *
385 * Returns old id at success, 0 at failure.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300386 * (There is no mem_cgroup using 0 as its id)
Daisuke Nishimura02491442010-03-10 15:22:17 -0800387 */
388unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
389 unsigned short old, unsigned short new)
390{
391 int type = swp_type(ent);
392 unsigned long offset = swp_offset(ent);
393 unsigned long idx = offset / SC_PER_PAGE;
394 unsigned long pos = offset & SC_POS_MASK;
395 struct swap_cgroup_ctrl *ctrl;
396 struct page *mappage;
397 struct swap_cgroup *sc;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400398 unsigned long flags;
399 unsigned short retval;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800400
401 ctrl = &swap_cgroup_ctrl[type];
402
403 mappage = ctrl->map[idx];
404 sc = page_address(mappage);
405 sc += pos;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400406 spin_lock_irqsave(&ctrl->lock, flags);
407 retval = sc->id;
408 if (retval == old)
409 sc->id = new;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800410 else
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400411 retval = 0;
412 spin_unlock_irqrestore(&ctrl->lock, flags);
413 return retval;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800414}
415
416/**
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800417 * swap_cgroup_record - record mem_cgroup for this swp_entry.
418 * @ent: swap entry to be recorded into
419 * @mem: mem_cgroup to be recorded
420 *
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700421 * Returns old value at success, 0 at failure.
422 * (Of course, old value can be 0.)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800423 */
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700424unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800425{
426 int type = swp_type(ent);
427 unsigned long offset = swp_offset(ent);
428 unsigned long idx = offset / SC_PER_PAGE;
429 unsigned long pos = offset & SC_POS_MASK;
430 struct swap_cgroup_ctrl *ctrl;
431 struct page *mappage;
432 struct swap_cgroup *sc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700433 unsigned short old;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400434 unsigned long flags;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800435
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800436 ctrl = &swap_cgroup_ctrl[type];
437
438 mappage = ctrl->map[idx];
439 sc = page_address(mappage);
440 sc += pos;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400441 spin_lock_irqsave(&ctrl->lock, flags);
442 old = sc->id;
443 sc->id = id;
444 spin_unlock_irqrestore(&ctrl->lock, flags);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800445
446 return old;
447}
448
449/**
450 * lookup_swap_cgroup - lookup mem_cgroup tied to swap entry
451 * @ent: swap entry to be looked up.
452 *
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700453 * Returns CSS ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800454 */
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700455unsigned short lookup_swap_cgroup(swp_entry_t ent)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800456{
457 int type = swp_type(ent);
458 unsigned long offset = swp_offset(ent);
459 unsigned long idx = offset / SC_PER_PAGE;
460 unsigned long pos = offset & SC_POS_MASK;
461 struct swap_cgroup_ctrl *ctrl;
462 struct page *mappage;
463 struct swap_cgroup *sc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700464 unsigned short ret;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800465
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800466 ctrl = &swap_cgroup_ctrl[type];
467 mappage = ctrl->map[idx];
468 sc = page_address(mappage);
469 sc += pos;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700470 ret = sc->id;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800471 return ret;
472}
473
474int swap_cgroup_swapon(int type, unsigned long max_pages)
475{
476 void *array;
477 unsigned long array_size;
478 unsigned long length;
479 struct swap_cgroup_ctrl *ctrl;
480
481 if (!do_swap_account)
482 return 0;
483
Namhyung Kim33278f72011-05-26 16:25:30 -0700484 length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800485 array_size = length * sizeof(void *);
486
Joe Perches8c1fec12011-05-28 10:36:34 -0700487 array = vzalloc(array_size);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800488 if (!array)
489 goto nomem;
490
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800491 ctrl = &swap_cgroup_ctrl[type];
492 mutex_lock(&swap_cgroup_mutex);
493 ctrl->length = length;
494 ctrl->map = array;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400495 spin_lock_init(&ctrl->lock);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800496 if (swap_cgroup_prepare(type)) {
497 /* memory shortage */
498 ctrl->map = NULL;
499 ctrl->length = 0;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800500 mutex_unlock(&swap_cgroup_mutex);
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700501 vfree(array);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800502 goto nomem;
503 }
504 mutex_unlock(&swap_cgroup_mutex);
505
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800506 return 0;
507nomem:
508 printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n");
509 printk(KERN_INFO
WANG Cong00a66d22011-07-25 17:12:12 -0700510 "swap_cgroup can be disabled by swapaccount=0 boot option\n");
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800511 return -ENOMEM;
512}
513
514void swap_cgroup_swapoff(int type)
515{
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700516 struct page **map;
517 unsigned long i, length;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800518 struct swap_cgroup_ctrl *ctrl;
519
520 if (!do_swap_account)
521 return;
522
523 mutex_lock(&swap_cgroup_mutex);
524 ctrl = &swap_cgroup_ctrl[type];
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700525 map = ctrl->map;
526 length = ctrl->length;
527 ctrl->map = NULL;
528 ctrl->length = 0;
529 mutex_unlock(&swap_cgroup_mutex);
530
531 if (map) {
532 for (i = 0; i < length; i++) {
533 struct page *page = map[i];
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800534 if (page)
535 __free_page(page);
536 }
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700537 vfree(map);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800538 }
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800539}
540
541#endif