blob: 5331c2bd85a2cc5c838881cdd12870ad9502029d [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
Grygorii Strashko0d036e92014-01-21 15:50:38 -080057 base = memblock_virt_alloc_try_nid_nopanic(
58 table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
59 BOOTMEM_ALLOC_ACCESSIBLE, nid);
KAMEZAWA Hiroyukica371c02009-06-12 10:33:53 +030060 if (!base)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070061 return -ENOMEM;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070062 NODE_DATA(nid)->node_page_cgroup = base;
63 total_usage += table_size;
64 return 0;
65}
66
KAMEZAWA Hiroyukica371c02009-06-12 10:33:53 +030067void __init page_cgroup_init_flatmem(void)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070068{
69
70 int nid, fail;
71
Hirokazu Takahashif8d665422009-01-07 18:08:02 -080072 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki94b6da52008-10-22 14:15:05 -070073 return;
74
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070075 for_each_online_node(nid) {
76 fail = alloc_node_page_cgroup(nid);
77 if (fail)
78 goto fail;
79 }
80 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
Randy Dunlap8ca739e2009-06-17 16:26:32 -070081 printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
82 " don't want memory cgroups\n");
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070083 return;
84fail:
Randy Dunlap8ca739e2009-06-17 16:26:32 -070085 printk(KERN_CRIT "allocation of page_cgroup failed.\n");
86 printk(KERN_CRIT "please try 'cgroup_disable=memory' boot option\n");
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070087 panic("Out of memory");
88}
89
90#else /* CONFIG_FLAT_NODE_MEM_MAP */
91
92struct page_cgroup *lookup_page_cgroup(struct page *page)
93{
94 unsigned long pfn = page_to_pfn(page);
95 struct mem_section *section = __pfn_to_section(pfn);
Johannes Weiner00c54c02012-01-12 17:18:40 -080096#ifdef CONFIG_DEBUG_VM
97 /*
98 * The sanity checks the page allocator does upon freeing a
99 * page can reach here before the page_cgroup arrays are
100 * allocated when feeding a range of pages to the allocator
101 * for the first time during bootup or memory hotplug.
102 */
Balbir Singhd69b0422009-06-17 16:26:34 -0700103 if (!section->page_cgroup)
104 return NULL;
Johannes Weiner00c54c02012-01-12 17:18:40 -0800105#endif
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700106 return section->page_cgroup + pfn;
107}
108
Namhyung Kim268433b2011-05-26 16:25:29 -0700109static void *__meminit alloc_page_cgroup(size_t size, int nid)
Michal Hockodde79e02011-03-23 16:42:40 -0700110{
Johannes Weiner6b208e32012-01-12 17:18:18 -0800111 gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
Michal Hockodde79e02011-03-23 16:42:40 -0700112 void *addr = NULL;
113
Steven Rostedtff7ee932011-11-02 13:38:11 -0700114 addr = alloc_pages_exact_nid(nid, size, flags);
115 if (addr) {
116 kmemleak_alloc(addr, size, 1, flags);
Michal Hockodde79e02011-03-23 16:42:40 -0700117 return addr;
Steven Rostedtff7ee932011-11-02 13:38:11 -0700118 }
Michal Hockodde79e02011-03-23 16:42:40 -0700119
120 if (node_state(nid, N_HIGH_MEMORY))
Johannes Weiner6b208e32012-01-12 17:18:18 -0800121 addr = vzalloc_node(size, nid);
Michal Hockodde79e02011-03-23 16:42:40 -0700122 else
Johannes Weiner6b208e32012-01-12 17:18:18 -0800123 addr = vzalloc(size);
Michal Hockodde79e02011-03-23 16:42:40 -0700124
125 return addr;
126}
127
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700128static int __meminit init_section_page_cgroup(unsigned long pfn, int nid)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700129{
Johannes Weiner6b3ae582011-03-23 16:42:30 -0700130 struct mem_section *section;
Johannes Weiner6b208e32012-01-12 17:18:18 -0800131 struct page_cgroup *base;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700132 unsigned long table_size;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700133
Johannes Weiner6b208e32012-01-12 17:18:18 -0800134 section = __pfn_to_section(pfn);
Johannes Weiner6b3ae582011-03-23 16:42:30 -0700135
136 if (section->page_cgroup)
137 return 0;
138
Johannes Weiner6b3ae582011-03-23 16:42:30 -0700139 table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
Michal Hockodde79e02011-03-23 16:42:40 -0700140 base = alloc_page_cgroup(table_size, nid);
141
Johannes Weiner6b3ae582011-03-23 16:42:30 -0700142 /*
143 * The value stored in section->page_cgroup is (base - pfn)
144 * and it does not point to the memory block allocated above,
145 * causing kmemleak false positives.
146 */
147 kmemleak_not_leak(base);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700148
149 if (!base) {
150 printk(KERN_ERR "page cgroup allocation failure\n");
151 return -ENOMEM;
152 }
153
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700154 /*
155 * The passed "pfn" may not be aligned to SECTION. For the calculation
156 * we need to apply a mask.
157 */
158 pfn &= PAGE_SECTION_MASK;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700159 section->page_cgroup = base - pfn;
160 total_usage += table_size;
161 return 0;
162}
163#ifdef CONFIG_MEMORY_HOTPLUG
Bob Liu0efc8eb2012-01-12 17:19:08 -0800164static void free_page_cgroup(void *addr)
165{
166 if (is_vmalloc_addr(addr)) {
167 vfree(addr);
168 } else {
169 struct page *page = virt_to_page(addr);
170 size_t table_size =
171 sizeof(struct page_cgroup) * PAGES_PER_SECTION;
172
173 BUG_ON(PageReserved(page));
Wang Nan401507d2014-10-29 14:50:18 -0700174 kmemleak_free(addr);
Bob Liu0efc8eb2012-01-12 17:19:08 -0800175 free_pages_exact(addr, table_size);
176 }
177}
178
Rashika Kheriad20199e2014-04-03 14:48:05 -0700179static void __free_page_cgroup(unsigned long pfn)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700180{
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
Rashika Kheriad20199e2014-04-03 14:48:05 -0700192static int __meminit online_page_cgroup(unsigned long start_pfn,
193 unsigned long nr_pages,
194 int nid)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700195{
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
Rashika Kheriad20199e2014-04-03 14:48:05 -0700227static int __meminit offline_page_cgroup(unsigned long start_pfn,
228 unsigned long nr_pages, int nid)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700229{
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:
Wen Congyang7c72eb32012-12-11 16:00:49 -0800256 offline_page_cgroup(mn->start_pfn,
257 mn->nr_pages, mn->status_change_nid);
258 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700259 case MEM_GOING_OFFLINE:
260 break;
261 case MEM_ONLINE:
262 case MEM_CANCEL_OFFLINE:
263 break;
264 }
KAMEZAWA Hiroyukidc19f9d2008-12-01 13:13:48 -0800265
Prarit Bhargava5fda1bd2011-03-22 16:30:49 -0700266 return notifier_from_errno(ret);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700267}
268
269#endif
270
271void __init page_cgroup_init(void)
272{
273 unsigned long pfn;
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700274 int nid;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700275
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800276 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki94b6da52008-10-22 14:15:05 -0700277 return;
278
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800279 for_each_node_state(nid, N_MEMORY) {
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700280 unsigned long start_pfn, end_pfn;
281
282 start_pfn = node_start_pfn(nid);
283 end_pfn = node_end_pfn(nid);
284 /*
285 * start_pfn and end_pfn may not be aligned to SECTION and the
286 * page->flags of out of node pages are not initialized. So we
287 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
288 */
289 for (pfn = start_pfn;
290 pfn < end_pfn;
291 pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
292
293 if (!pfn_valid(pfn))
294 continue;
295 /*
296 * Nodes's pfns can be overlapping.
297 * We know some arch can have a nodes layout such as
298 * -------------pfn-------------->
299 * N0 | N1 | N2 | N0 | N1 | N2|....
300 */
301 if (pfn_to_nid(pfn) != nid)
302 continue;
303 if (init_section_page_cgroup(pfn, nid))
304 goto oom;
305 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700306 }
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700307 hotplug_memory_notifier(page_cgroup_callback, 0);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700308 printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
KAMEZAWA Hiroyuki37573e82011-06-15 15:08:42 -0700309 printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
310 "don't want memory cgroups\n");
311 return;
312oom:
313 printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
314 panic("Out of memory");
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700315}
316
Al Viro31168482008-11-22 17:33:24 +0000317void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700318{
319 return;
320}
321
322#endif
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800323
324
Andrew Mortonc255a452012-07-31 16:43:02 -0700325#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800326
327static DEFINE_MUTEX(swap_cgroup_mutex);
328struct swap_cgroup_ctrl {
329 struct page **map;
330 unsigned long length;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400331 spinlock_t lock;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800332};
333
H Hartley Sweeten61600f52011-11-02 13:38:36 -0700334static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800335
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800336struct swap_cgroup {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700337 unsigned short id;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800338};
339#define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800340
341/*
342 * SwapCgroup implements "lookup" and "exchange" operations.
343 * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
344 * against SwapCache. At swap_free(), this is accessed directly from swap.
345 *
346 * This means,
347 * - we have no race in "exchange" when we're accessed via SwapCache because
348 * SwapCache(and its swp_entry) is under lock.
349 * - When called via swap_free(), there is no user of this entry and no race.
350 * Then, we don't need lock around "exchange".
351 *
352 * TODO: we can push these buffers out to HIGHMEM.
353 */
354
355/*
356 * allocate buffer for swap_cgroup.
357 */
358static int swap_cgroup_prepare(int type)
359{
360 struct page *page;
361 struct swap_cgroup_ctrl *ctrl;
362 unsigned long idx, max;
363
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800364 ctrl = &swap_cgroup_ctrl[type];
365
366 for (idx = 0; idx < ctrl->length; idx++) {
367 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
368 if (!page)
369 goto not_enough_page;
370 ctrl->map[idx] = page;
371 }
372 return 0;
373not_enough_page:
374 max = idx;
375 for (idx = 0; idx < max; idx++)
376 __free_page(ctrl->map[idx]);
377
378 return -ENOMEM;
379}
380
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800381static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
382 struct swap_cgroup_ctrl **ctrlp)
383{
384 pgoff_t offset = swp_offset(ent);
385 struct swap_cgroup_ctrl *ctrl;
386 struct page *mappage;
Hugh Dickinsc09ff082012-03-05 20:52:55 -0800387 struct swap_cgroup *sc;
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800388
389 ctrl = &swap_cgroup_ctrl[swp_type(ent)];
390 if (ctrlp)
391 *ctrlp = ctrl;
392
393 mappage = ctrl->map[offset / SC_PER_PAGE];
Hugh Dickinsc09ff082012-03-05 20:52:55 -0800394 sc = page_address(mappage);
395 return sc + offset % SC_PER_PAGE;
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800396}
397
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800398/**
Daisuke Nishimura02491442010-03-10 15:22:17 -0800399 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
Wanpeng Lidad75572012-06-20 12:53:01 -0700400 * @ent: swap entry to be cmpxchged
Daisuke Nishimura02491442010-03-10 15:22:17 -0800401 * @old: old id
402 * @new: new id
403 *
404 * Returns old id at success, 0 at failure.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300405 * (There is no mem_cgroup using 0 as its id)
Daisuke Nishimura02491442010-03-10 15:22:17 -0800406 */
407unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
408 unsigned short old, unsigned short new)
409{
Daisuke Nishimura02491442010-03-10 15:22:17 -0800410 struct swap_cgroup_ctrl *ctrl;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800411 struct swap_cgroup *sc;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400412 unsigned long flags;
413 unsigned short retval;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800414
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800415 sc = lookup_swap_cgroup(ent, &ctrl);
Daisuke Nishimura02491442010-03-10 15:22:17 -0800416
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400417 spin_lock_irqsave(&ctrl->lock, flags);
418 retval = sc->id;
419 if (retval == old)
420 sc->id = new;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800421 else
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400422 retval = 0;
423 spin_unlock_irqrestore(&ctrl->lock, flags);
424 return retval;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800425}
426
427/**
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800428 * swap_cgroup_record - record mem_cgroup for this swp_entry.
429 * @ent: swap entry to be recorded into
Wanpeng Lidad75572012-06-20 12:53:01 -0700430 * @id: mem_cgroup to be recorded
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800431 *
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700432 * Returns old value at success, 0 at failure.
433 * (Of course, old value can be 0.)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800434 */
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700435unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800436{
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800437 struct swap_cgroup_ctrl *ctrl;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800438 struct swap_cgroup *sc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700439 unsigned short old;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400440 unsigned long flags;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800441
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800442 sc = lookup_swap_cgroup(ent, &ctrl);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800443
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400444 spin_lock_irqsave(&ctrl->lock, flags);
445 old = sc->id;
446 sc->id = id;
447 spin_unlock_irqrestore(&ctrl->lock, flags);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800448
449 return old;
450}
451
452/**
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800453 * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800454 * @ent: swap entry to be looked up.
455 *
Hugh Dickinsb3ff8a22014-01-12 20:23:27 -0800456 * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800457 */
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800458unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800459{
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800460 return lookup_swap_cgroup(ent, NULL)->id;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800461}
462
463int swap_cgroup_swapon(int type, unsigned long max_pages)
464{
465 void *array;
466 unsigned long array_size;
467 unsigned long length;
468 struct swap_cgroup_ctrl *ctrl;
469
470 if (!do_swap_account)
471 return 0;
472
Namhyung Kim33278f72011-05-26 16:25:30 -0700473 length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800474 array_size = length * sizeof(void *);
475
Joe Perches8c1fec12011-05-28 10:36:34 -0700476 array = vzalloc(array_size);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800477 if (!array)
478 goto nomem;
479
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800480 ctrl = &swap_cgroup_ctrl[type];
481 mutex_lock(&swap_cgroup_mutex);
482 ctrl->length = length;
483 ctrl->map = array;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400484 spin_lock_init(&ctrl->lock);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800485 if (swap_cgroup_prepare(type)) {
486 /* memory shortage */
487 ctrl->map = NULL;
488 ctrl->length = 0;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800489 mutex_unlock(&swap_cgroup_mutex);
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700490 vfree(array);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800491 goto nomem;
492 }
493 mutex_unlock(&swap_cgroup_mutex);
494
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800495 return 0;
496nomem:
497 printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n");
498 printk(KERN_INFO
WANG Cong00a66d22011-07-25 17:12:12 -0700499 "swap_cgroup can be disabled by swapaccount=0 boot option\n");
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800500 return -ENOMEM;
501}
502
503void swap_cgroup_swapoff(int type)
504{
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700505 struct page **map;
506 unsigned long i, length;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800507 struct swap_cgroup_ctrl *ctrl;
508
509 if (!do_swap_account)
510 return;
511
512 mutex_lock(&swap_cgroup_mutex);
513 ctrl = &swap_cgroup_ctrl[type];
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700514 map = ctrl->map;
515 length = ctrl->length;
516 ctrl->map = NULL;
517 ctrl->length = 0;
518 mutex_unlock(&swap_cgroup_mutex);
519
520 if (map) {
521 for (i = 0; i < length; i++) {
522 struct page *page = map[i];
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800523 if (page)
524 __free_page(page);
525 }
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700526 vfree(map);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800527 }
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800528}
529
530#endif