• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 unsigned long total_usage;
15 
16 #if !defined(CONFIG_SPARSEMEM)
17 
18 
pgdat_page_cgroup_init(struct pglist_data * pgdat)19 void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
20 {
21 	pgdat->node_page_cgroup = NULL;
22 }
23 
lookup_page_cgroup(struct page * page)24 struct 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 #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 	 */
38 	if (unlikely(!base))
39 		return NULL;
40 #endif
41 	offset = pfn - NODE_DATA(page_to_nid(page))->node_start_pfn;
42 	return base + offset;
43 }
44 
alloc_node_page_cgroup(int nid)45 static int __init alloc_node_page_cgroup(int nid)
46 {
47 	struct page_cgroup *base;
48 	unsigned long table_size;
49 	unsigned long nr_pages;
50 
51 	nr_pages = NODE_DATA(nid)->node_spanned_pages;
52 	if (!nr_pages)
53 		return 0;
54 
55 	table_size = sizeof(struct page_cgroup) * nr_pages;
56 
57 	base = memblock_virt_alloc_try_nid_nopanic(
58 			table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
59 			BOOTMEM_ALLOC_ACCESSIBLE, nid);
60 	if (!base)
61 		return -ENOMEM;
62 	NODE_DATA(nid)->node_page_cgroup = base;
63 	total_usage += table_size;
64 	return 0;
65 }
66 
page_cgroup_init_flatmem(void)67 void __init page_cgroup_init_flatmem(void)
68 {
69 
70 	int nid, fail;
71 
72 	if (mem_cgroup_disabled())
73 		return;
74 
75 	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);
81 	printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
82 	" don't want memory cgroups\n");
83 	return;
84 fail:
85 	printk(KERN_CRIT "allocation of page_cgroup failed.\n");
86 	printk(KERN_CRIT "please try 'cgroup_disable=memory' boot option\n");
87 	panic("Out of memory");
88 }
89 
90 #else /* CONFIG_FLAT_NODE_MEM_MAP */
91 
lookup_page_cgroup(struct page * page)92 struct 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);
96 #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 	 */
103 	if (!section->page_cgroup)
104 		return NULL;
105 #endif
106 	return section->page_cgroup + pfn;
107 }
108 
alloc_page_cgroup(size_t size,int nid)109 static void *__meminit alloc_page_cgroup(size_t size, int nid)
110 {
111 	gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
112 	void *addr = NULL;
113 
114 	addr = alloc_pages_exact_nid(nid, size, flags);
115 	if (addr) {
116 		kmemleak_alloc(addr, size, 1, flags);
117 		return addr;
118 	}
119 
120 	if (node_state(nid, N_HIGH_MEMORY))
121 		addr = vzalloc_node(size, nid);
122 	else
123 		addr = vzalloc(size);
124 
125 	return addr;
126 }
127 
init_section_page_cgroup(unsigned long pfn,int nid)128 static int __meminit init_section_page_cgroup(unsigned long pfn, int nid)
129 {
130 	struct mem_section *section;
131 	struct page_cgroup *base;
132 	unsigned long table_size;
133 
134 	section = __pfn_to_section(pfn);
135 
136 	if (section->page_cgroup)
137 		return 0;
138 
139 	table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
140 	base = alloc_page_cgroup(table_size, nid);
141 
142 	/*
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);
148 
149 	if (!base) {
150 		printk(KERN_ERR "page cgroup allocation failure\n");
151 		return -ENOMEM;
152 	}
153 
154 	/*
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;
159 	section->page_cgroup = base - pfn;
160 	total_usage += table_size;
161 	return 0;
162 }
163 #ifdef CONFIG_MEMORY_HOTPLUG
free_page_cgroup(void * addr)164 static 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));
174 		kmemleak_free(addr);
175 		free_pages_exact(addr, table_size);
176 	}
177 }
178 
__free_page_cgroup(unsigned long pfn)179 static void __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;
188 	free_page_cgroup(base);
189 	ms->page_cgroup = NULL;
190 }
191 
online_page_cgroup(unsigned long start_pfn,unsigned long nr_pages,int nid)192 static int __meminit online_page_cgroup(unsigned long start_pfn,
193 				unsigned long nr_pages,
194 				int nid)
195 {
196 	unsigned long start, end, pfn;
197 	int fail = 0;
198 
199 	start = SECTION_ALIGN_DOWN(start_pfn);
200 	end = SECTION_ALIGN_UP(start_pfn + nr_pages);
201 
202 	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 
212 	for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
213 		if (!pfn_present(pfn))
214 			continue;
215 		fail = init_section_page_cgroup(pfn, nid);
216 	}
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 
offline_page_cgroup(unsigned long start_pfn,unsigned long nr_pages,int nid)227 static int __meminit offline_page_cgroup(unsigned long start_pfn,
228 				unsigned long nr_pages, int nid)
229 {
230 	unsigned long start, end, pfn;
231 
232 	start = SECTION_ALIGN_DOWN(start_pfn);
233 	end = SECTION_ALIGN_UP(start_pfn + nr_pages);
234 
235 	for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
236 		__free_page_cgroup(pfn);
237 	return 0;
238 
239 }
240 
page_cgroup_callback(struct notifier_block * self,unsigned long action,void * arg)241 static int __meminit page_cgroup_callback(struct notifier_block *self,
242 			       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;
251 	case MEM_OFFLINE:
252 		offline_page_cgroup(mn->start_pfn,
253 				mn->nr_pages, mn->status_change_nid);
254 		break;
255 	case MEM_CANCEL_ONLINE:
256 		offline_page_cgroup(mn->start_pfn,
257 				mn->nr_pages, mn->status_change_nid);
258 		break;
259 	case MEM_GOING_OFFLINE:
260 		break;
261 	case MEM_ONLINE:
262 	case MEM_CANCEL_OFFLINE:
263 		break;
264 	}
265 
266 	return notifier_from_errno(ret);
267 }
268 
269 #endif
270 
page_cgroup_init(void)271 void __init page_cgroup_init(void)
272 {
273 	unsigned long pfn;
274 	int nid;
275 
276 	if (mem_cgroup_disabled())
277 		return;
278 
279 	for_each_node_state(nid, N_MEMORY) {
280 		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 		}
306 	}
307 	hotplug_memory_notifier(page_cgroup_callback, 0);
308 	printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
309 	printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
310 			 "don't want memory cgroups\n");
311 	return;
312 oom:
313 	printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
314 	panic("Out of memory");
315 }
316 
pgdat_page_cgroup_init(struct pglist_data * pgdat)317 void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
318 {
319 	return;
320 }
321 
322 #endif
323 
324 
325 #ifdef CONFIG_MEMCG_SWAP
326 
327 static DEFINE_MUTEX(swap_cgroup_mutex);
328 struct swap_cgroup_ctrl {
329 	struct page **map;
330 	unsigned long length;
331 	spinlock_t	lock;
332 };
333 
334 static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
335 
336 struct swap_cgroup {
337 	unsigned short		id;
338 };
339 #define SC_PER_PAGE	(PAGE_SIZE/sizeof(struct swap_cgroup))
340 
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  */
swap_cgroup_prepare(int type)358 static int swap_cgroup_prepare(int type)
359 {
360 	struct page *page;
361 	struct swap_cgroup_ctrl *ctrl;
362 	unsigned long idx, max;
363 
364 	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 		if (!(idx % SWAP_CLUSTER_MAX))
373 			cond_resched();
374 	}
375 	return 0;
376 not_enough_page:
377 	max = idx;
378 	for (idx = 0; idx < max; idx++)
379 		__free_page(ctrl->map[idx]);
380 
381 	return -ENOMEM;
382 }
383 
lookup_swap_cgroup(swp_entry_t ent,struct swap_cgroup_ctrl ** ctrlp)384 static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
385 					struct swap_cgroup_ctrl **ctrlp)
386 {
387 	pgoff_t offset = swp_offset(ent);
388 	struct swap_cgroup_ctrl *ctrl;
389 	struct page *mappage;
390 	struct swap_cgroup *sc;
391 
392 	ctrl = &swap_cgroup_ctrl[swp_type(ent)];
393 	if (ctrlp)
394 		*ctrlp = ctrl;
395 
396 	mappage = ctrl->map[offset / SC_PER_PAGE];
397 	sc = page_address(mappage);
398 	return sc + offset % SC_PER_PAGE;
399 }
400 
401 /**
402  * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
403  * @ent: swap entry to be cmpxchged
404  * @old: old id
405  * @new: new id
406  *
407  * Returns old id at success, 0 at failure.
408  * (There is no mem_cgroup using 0 as its id)
409  */
swap_cgroup_cmpxchg(swp_entry_t ent,unsigned short old,unsigned short new)410 unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
411 					unsigned short old, unsigned short new)
412 {
413 	struct swap_cgroup_ctrl *ctrl;
414 	struct swap_cgroup *sc;
415 	unsigned long flags;
416 	unsigned short retval;
417 
418 	sc = lookup_swap_cgroup(ent, &ctrl);
419 
420 	spin_lock_irqsave(&ctrl->lock, flags);
421 	retval = sc->id;
422 	if (retval == old)
423 		sc->id = new;
424 	else
425 		retval = 0;
426 	spin_unlock_irqrestore(&ctrl->lock, flags);
427 	return retval;
428 }
429 
430 /**
431  * swap_cgroup_record - record mem_cgroup for this swp_entry.
432  * @ent: swap entry to be recorded into
433  * @id: mem_cgroup to be recorded
434  *
435  * Returns old value at success, 0 at failure.
436  * (Of course, old value can be 0.)
437  */
swap_cgroup_record(swp_entry_t ent,unsigned short id)438 unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
439 {
440 	struct swap_cgroup_ctrl *ctrl;
441 	struct swap_cgroup *sc;
442 	unsigned short old;
443 	unsigned long flags;
444 
445 	sc = lookup_swap_cgroup(ent, &ctrl);
446 
447 	spin_lock_irqsave(&ctrl->lock, flags);
448 	old = sc->id;
449 	sc->id = id;
450 	spin_unlock_irqrestore(&ctrl->lock, flags);
451 
452 	return old;
453 }
454 
455 /**
456  * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
457  * @ent: swap entry to be looked up.
458  *
459  * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
460  */
lookup_swap_cgroup_id(swp_entry_t ent)461 unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
462 {
463 	return lookup_swap_cgroup(ent, NULL)->id;
464 }
465 
swap_cgroup_swapon(int type,unsigned long max_pages)466 int swap_cgroup_swapon(int type, unsigned long max_pages)
467 {
468 	void *array;
469 	unsigned long array_size;
470 	unsigned long length;
471 	struct swap_cgroup_ctrl *ctrl;
472 
473 	if (!do_swap_account)
474 		return 0;
475 
476 	length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
477 	array_size = length * sizeof(void *);
478 
479 	array = vzalloc(array_size);
480 	if (!array)
481 		goto nomem;
482 
483 	ctrl = &swap_cgroup_ctrl[type];
484 	mutex_lock(&swap_cgroup_mutex);
485 	ctrl->length = length;
486 	ctrl->map = array;
487 	spin_lock_init(&ctrl->lock);
488 	if (swap_cgroup_prepare(type)) {
489 		/* memory shortage */
490 		ctrl->map = NULL;
491 		ctrl->length = 0;
492 		mutex_unlock(&swap_cgroup_mutex);
493 		vfree(array);
494 		goto nomem;
495 	}
496 	mutex_unlock(&swap_cgroup_mutex);
497 
498 	return 0;
499 nomem:
500 	printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n");
501 	printk(KERN_INFO
502 		"swap_cgroup can be disabled by swapaccount=0 boot option\n");
503 	return -ENOMEM;
504 }
505 
swap_cgroup_swapoff(int type)506 void swap_cgroup_swapoff(int type)
507 {
508 	struct page **map;
509 	unsigned long i, length;
510 	struct swap_cgroup_ctrl *ctrl;
511 
512 	if (!do_swap_account)
513 		return;
514 
515 	mutex_lock(&swap_cgroup_mutex);
516 	ctrl = &swap_cgroup_ctrl[type];
517 	map = ctrl->map;
518 	length = ctrl->length;
519 	ctrl->map = NULL;
520 	ctrl->length = 0;
521 	mutex_unlock(&swap_cgroup_mutex);
522 
523 	if (map) {
524 		for (i = 0; i < length; i++) {
525 			struct page *page = map[i];
526 			if (page)
527 				__free_page(page);
528 		}
529 		vfree(map);
530 	}
531 }
532 
533 #endif
534