per-zone and reclaim enhancements for memory controller: remember reclaim priority...
[deliverable/linux.git] / mm / memcontrol.c
1 /* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
6 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20 #include <linux/res_counter.h>
21 #include <linux/memcontrol.h>
22 #include <linux/cgroup.h>
23 #include <linux/mm.h>
24 #include <linux/smp.h>
25 #include <linux/page-flags.h>
26 #include <linux/backing-dev.h>
27 #include <linux/bit_spinlock.h>
28 #include <linux/rcupdate.h>
29 #include <linux/swap.h>
30 #include <linux/spinlock.h>
31 #include <linux/fs.h>
32 #include <linux/seq_file.h>
33
34 #include <asm/uaccess.h>
35
36 struct cgroup_subsys mem_cgroup_subsys;
37 static const int MEM_CGROUP_RECLAIM_RETRIES = 5;
38
39 /*
40 * Statistics for memory cgroup.
41 */
42 enum mem_cgroup_stat_index {
43 /*
44 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
45 */
46 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
47 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
48
49 MEM_CGROUP_STAT_NSTATS,
50 };
51
52 struct mem_cgroup_stat_cpu {
53 s64 count[MEM_CGROUP_STAT_NSTATS];
54 } ____cacheline_aligned_in_smp;
55
56 struct mem_cgroup_stat {
57 struct mem_cgroup_stat_cpu cpustat[NR_CPUS];
58 };
59
60 /*
61 * For accounting under irq disable, no need for increment preempt count.
62 */
63 static void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat *stat,
64 enum mem_cgroup_stat_index idx, int val)
65 {
66 int cpu = smp_processor_id();
67 stat->cpustat[cpu].count[idx] += val;
68 }
69
70 static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
71 enum mem_cgroup_stat_index idx)
72 {
73 int cpu;
74 s64 ret = 0;
75 for_each_possible_cpu(cpu)
76 ret += stat->cpustat[cpu].count[idx];
77 return ret;
78 }
79
80 /*
81 * per-zone information in memory controller.
82 */
83
84 enum mem_cgroup_zstat_index {
85 MEM_CGROUP_ZSTAT_ACTIVE,
86 MEM_CGROUP_ZSTAT_INACTIVE,
87
88 NR_MEM_CGROUP_ZSTAT,
89 };
90
91 struct mem_cgroup_per_zone {
92 unsigned long count[NR_MEM_CGROUP_ZSTAT];
93 };
94 /* Macro for accessing counter */
95 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
96
97 struct mem_cgroup_per_node {
98 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
99 };
100
101 struct mem_cgroup_lru_info {
102 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
103 };
104
105 /*
106 * The memory controller data structure. The memory controller controls both
107 * page cache and RSS per cgroup. We would eventually like to provide
108 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
109 * to help the administrator determine what knobs to tune.
110 *
111 * TODO: Add a water mark for the memory controller. Reclaim will begin when
112 * we hit the water mark. May be even add a low water mark, such that
113 * no reclaim occurs from a cgroup at it's low water mark, this is
114 * a feature that will be implemented much later in the future.
115 */
116 struct mem_cgroup {
117 struct cgroup_subsys_state css;
118 /*
119 * the counter to account for memory usage
120 */
121 struct res_counter res;
122 /*
123 * Per cgroup active and inactive list, similar to the
124 * per zone LRU lists.
125 * TODO: Consider making these lists per zone
126 */
127 struct list_head active_list;
128 struct list_head inactive_list;
129 struct mem_cgroup_lru_info info;
130 /*
131 * spin_lock to protect the per cgroup LRU
132 */
133 spinlock_t lru_lock;
134 unsigned long control_type; /* control RSS or RSS+Pagecache */
135 int prev_priority; /* for recording reclaim priority */
136 /*
137 * statistics.
138 */
139 struct mem_cgroup_stat stat;
140 };
141
142 /*
143 * We use the lower bit of the page->page_cgroup pointer as a bit spin
144 * lock. We need to ensure that page->page_cgroup is atleast two
145 * byte aligned (based on comments from Nick Piggin)
146 */
147 #define PAGE_CGROUP_LOCK_BIT 0x0
148 #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
149
150 /*
151 * A page_cgroup page is associated with every page descriptor. The
152 * page_cgroup helps us identify information about the cgroup
153 */
154 struct page_cgroup {
155 struct list_head lru; /* per cgroup LRU list */
156 struct page *page;
157 struct mem_cgroup *mem_cgroup;
158 atomic_t ref_cnt; /* Helpful when pages move b/w */
159 /* mapped and cached states */
160 int flags;
161 };
162 #define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
163 #define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
164
165 static inline int page_cgroup_nid(struct page_cgroup *pc)
166 {
167 return page_to_nid(pc->page);
168 }
169
170 static inline enum zone_type page_cgroup_zid(struct page_cgroup *pc)
171 {
172 return page_zonenum(pc->page);
173 }
174
175 enum {
176 MEM_CGROUP_TYPE_UNSPEC = 0,
177 MEM_CGROUP_TYPE_MAPPED,
178 MEM_CGROUP_TYPE_CACHED,
179 MEM_CGROUP_TYPE_ALL,
180 MEM_CGROUP_TYPE_MAX,
181 };
182
183 enum charge_type {
184 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
185 MEM_CGROUP_CHARGE_TYPE_MAPPED,
186 };
187
188
189 /*
190 * Always modified under lru lock. Then, not necessary to preempt_disable()
191 */
192 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
193 bool charge)
194 {
195 int val = (charge)? 1 : -1;
196 struct mem_cgroup_stat *stat = &mem->stat;
197 VM_BUG_ON(!irqs_disabled());
198
199 if (flags & PAGE_CGROUP_FLAG_CACHE)
200 __mem_cgroup_stat_add_safe(stat,
201 MEM_CGROUP_STAT_CACHE, val);
202 else
203 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
204 }
205
206 static inline struct mem_cgroup_per_zone *
207 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
208 {
209 BUG_ON(!mem->info.nodeinfo[nid]);
210 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
211 }
212
213 static inline struct mem_cgroup_per_zone *
214 page_cgroup_zoneinfo(struct page_cgroup *pc)
215 {
216 struct mem_cgroup *mem = pc->mem_cgroup;
217 int nid = page_cgroup_nid(pc);
218 int zid = page_cgroup_zid(pc);
219
220 return mem_cgroup_zoneinfo(mem, nid, zid);
221 }
222
223 static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
224 enum mem_cgroup_zstat_index idx)
225 {
226 int nid, zid;
227 struct mem_cgroup_per_zone *mz;
228 u64 total = 0;
229
230 for_each_online_node(nid)
231 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
232 mz = mem_cgroup_zoneinfo(mem, nid, zid);
233 total += MEM_CGROUP_ZSTAT(mz, idx);
234 }
235 return total;
236 }
237
238 static struct mem_cgroup init_mem_cgroup;
239
240 static inline
241 struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
242 {
243 return container_of(cgroup_subsys_state(cont,
244 mem_cgroup_subsys_id), struct mem_cgroup,
245 css);
246 }
247
248 static inline
249 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
250 {
251 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
252 struct mem_cgroup, css);
253 }
254
255 void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p)
256 {
257 struct mem_cgroup *mem;
258
259 mem = mem_cgroup_from_task(p);
260 css_get(&mem->css);
261 mm->mem_cgroup = mem;
262 }
263
264 void mm_free_cgroup(struct mm_struct *mm)
265 {
266 css_put(&mm->mem_cgroup->css);
267 }
268
269 static inline int page_cgroup_locked(struct page *page)
270 {
271 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT,
272 &page->page_cgroup);
273 }
274
275 void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
276 {
277 int locked;
278
279 /*
280 * While resetting the page_cgroup we might not hold the
281 * page_cgroup lock. free_hot_cold_page() is an example
282 * of such a scenario
283 */
284 if (pc)
285 VM_BUG_ON(!page_cgroup_locked(page));
286 locked = (page->page_cgroup & PAGE_CGROUP_LOCK);
287 page->page_cgroup = ((unsigned long)pc | locked);
288 }
289
290 struct page_cgroup *page_get_page_cgroup(struct page *page)
291 {
292 return (struct page_cgroup *)
293 (page->page_cgroup & ~PAGE_CGROUP_LOCK);
294 }
295
296 static void __always_inline lock_page_cgroup(struct page *page)
297 {
298 bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
299 VM_BUG_ON(!page_cgroup_locked(page));
300 }
301
302 static void __always_inline unlock_page_cgroup(struct page *page)
303 {
304 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
305 }
306
307 /*
308 * Tie new page_cgroup to struct page under lock_page_cgroup()
309 * This can fail if the page has been tied to a page_cgroup.
310 * If success, returns 0.
311 */
312 static int page_cgroup_assign_new_page_cgroup(struct page *page,
313 struct page_cgroup *pc)
314 {
315 int ret = 0;
316
317 lock_page_cgroup(page);
318 if (!page_get_page_cgroup(page))
319 page_assign_page_cgroup(page, pc);
320 else /* A page is tied to other pc. */
321 ret = 1;
322 unlock_page_cgroup(page);
323 return ret;
324 }
325
326 /*
327 * Clear page->page_cgroup member under lock_page_cgroup().
328 * If given "pc" value is different from one page->page_cgroup,
329 * page->cgroup is not cleared.
330 * Returns a value of page->page_cgroup at lock taken.
331 * A can can detect failure of clearing by following
332 * clear_page_cgroup(page, pc) == pc
333 */
334
335 static struct page_cgroup *clear_page_cgroup(struct page *page,
336 struct page_cgroup *pc)
337 {
338 struct page_cgroup *ret;
339 /* lock and clear */
340 lock_page_cgroup(page);
341 ret = page_get_page_cgroup(page);
342 if (likely(ret == pc))
343 page_assign_page_cgroup(page, NULL);
344 unlock_page_cgroup(page);
345 return ret;
346 }
347
348 static void __mem_cgroup_remove_list(struct page_cgroup *pc)
349 {
350 int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
351 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
352
353 if (from)
354 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
355 else
356 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
357
358 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false);
359 list_del_init(&pc->lru);
360 }
361
362 static void __mem_cgroup_add_list(struct page_cgroup *pc)
363 {
364 int to = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
365 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
366
367 if (!to) {
368 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
369 list_add(&pc->lru, &pc->mem_cgroup->inactive_list);
370 } else {
371 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
372 list_add(&pc->lru, &pc->mem_cgroup->active_list);
373 }
374 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true);
375 }
376
377 static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
378 {
379 int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
380 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
381
382 if (from)
383 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
384 else
385 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
386
387 if (active) {
388 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
389 pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
390 list_move(&pc->lru, &pc->mem_cgroup->active_list);
391 } else {
392 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
393 pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
394 list_move(&pc->lru, &pc->mem_cgroup->inactive_list);
395 }
396 }
397
398 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
399 {
400 int ret;
401
402 task_lock(task);
403 ret = task->mm && mm_cgroup(task->mm) == mem;
404 task_unlock(task);
405 return ret;
406 }
407
408 /*
409 * This routine assumes that the appropriate zone's lru lock is already held
410 */
411 void mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
412 {
413 struct mem_cgroup *mem;
414 if (!pc)
415 return;
416
417 mem = pc->mem_cgroup;
418
419 spin_lock(&mem->lru_lock);
420 __mem_cgroup_move_lists(pc, active);
421 spin_unlock(&mem->lru_lock);
422 }
423
424 /*
425 * Calculate mapped_ratio under memory controller. This will be used in
426 * vmscan.c for deteremining we have to reclaim mapped pages.
427 */
428 int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
429 {
430 long total, rss;
431
432 /*
433 * usage is recorded in bytes. But, here, we assume the number of
434 * physical pages can be represented by "long" on any arch.
435 */
436 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
437 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
438 return (int)((rss * 100L) / total);
439 }
440 /*
441 * This function is called from vmscan.c. In page reclaiming loop. balance
442 * between active and inactive list is calculated. For memory controller
443 * page reclaiming, we should use using mem_cgroup's imbalance rather than
444 * zone's global lru imbalance.
445 */
446 long mem_cgroup_reclaim_imbalance(struct mem_cgroup *mem)
447 {
448 unsigned long active, inactive;
449 /* active and inactive are the number of pages. 'long' is ok.*/
450 active = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_ACTIVE);
451 inactive = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_INACTIVE);
452 return (long) (active / (inactive + 1));
453 }
454
455 /*
456 * prev_priority control...this will be used in memory reclaim path.
457 */
458 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
459 {
460 return mem->prev_priority;
461 }
462
463 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
464 {
465 if (priority < mem->prev_priority)
466 mem->prev_priority = priority;
467 }
468
469 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
470 {
471 mem->prev_priority = priority;
472 }
473
474 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
475 struct list_head *dst,
476 unsigned long *scanned, int order,
477 int mode, struct zone *z,
478 struct mem_cgroup *mem_cont,
479 int active)
480 {
481 unsigned long nr_taken = 0;
482 struct page *page;
483 unsigned long scan;
484 LIST_HEAD(pc_list);
485 struct list_head *src;
486 struct page_cgroup *pc, *tmp;
487
488 if (active)
489 src = &mem_cont->active_list;
490 else
491 src = &mem_cont->inactive_list;
492
493 spin_lock(&mem_cont->lru_lock);
494 scan = 0;
495 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
496 if (scan >= nr_to_scan)
497 break;
498 page = pc->page;
499 VM_BUG_ON(!pc);
500
501 if (unlikely(!PageLRU(page)))
502 continue;
503
504 if (PageActive(page) && !active) {
505 __mem_cgroup_move_lists(pc, true);
506 continue;
507 }
508 if (!PageActive(page) && active) {
509 __mem_cgroup_move_lists(pc, false);
510 continue;
511 }
512
513 /*
514 * Reclaim, per zone
515 * TODO: make the active/inactive lists per zone
516 */
517 if (page_zone(page) != z)
518 continue;
519
520 scan++;
521 list_move(&pc->lru, &pc_list);
522
523 if (__isolate_lru_page(page, mode) == 0) {
524 list_move(&page->lru, dst);
525 nr_taken++;
526 }
527 }
528
529 list_splice(&pc_list, src);
530 spin_unlock(&mem_cont->lru_lock);
531
532 *scanned = scan;
533 return nr_taken;
534 }
535
536 /*
537 * Charge the memory controller for page usage.
538 * Return
539 * 0 if the charge was successful
540 * < 0 if the cgroup is over its limit
541 */
542 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
543 gfp_t gfp_mask, enum charge_type ctype)
544 {
545 struct mem_cgroup *mem;
546 struct page_cgroup *pc;
547 unsigned long flags;
548 unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
549
550 /*
551 * Should page_cgroup's go to their own slab?
552 * One could optimize the performance of the charging routine
553 * by saving a bit in the page_flags and using it as a lock
554 * to see if the cgroup page already has a page_cgroup associated
555 * with it
556 */
557 retry:
558 if (page) {
559 lock_page_cgroup(page);
560 pc = page_get_page_cgroup(page);
561 /*
562 * The page_cgroup exists and
563 * the page has already been accounted.
564 */
565 if (pc) {
566 if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) {
567 /* this page is under being uncharged ? */
568 unlock_page_cgroup(page);
569 cpu_relax();
570 goto retry;
571 } else {
572 unlock_page_cgroup(page);
573 goto done;
574 }
575 }
576 unlock_page_cgroup(page);
577 }
578
579 pc = kzalloc(sizeof(struct page_cgroup), gfp_mask);
580 if (pc == NULL)
581 goto err;
582
583 /*
584 * We always charge the cgroup the mm_struct belongs to.
585 * The mm_struct's mem_cgroup changes on task migration if the
586 * thread group leader migrates. It's possible that mm is not
587 * set, if so charge the init_mm (happens for pagecache usage).
588 */
589 if (!mm)
590 mm = &init_mm;
591
592 rcu_read_lock();
593 mem = rcu_dereference(mm->mem_cgroup);
594 /*
595 * For every charge from the cgroup, increment reference
596 * count
597 */
598 css_get(&mem->css);
599 rcu_read_unlock();
600
601 /*
602 * If we created the page_cgroup, we should free it on exceeding
603 * the cgroup limit.
604 */
605 while (res_counter_charge(&mem->res, PAGE_SIZE)) {
606 if (!(gfp_mask & __GFP_WAIT))
607 goto out;
608
609 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
610 continue;
611
612 /*
613 * try_to_free_mem_cgroup_pages() might not give us a full
614 * picture of reclaim. Some pages are reclaimed and might be
615 * moved to swap cache or just unmapped from the cgroup.
616 * Check the limit again to see if the reclaim reduced the
617 * current usage of the cgroup before giving up
618 */
619 if (res_counter_check_under_limit(&mem->res))
620 continue;
621
622 if (!nr_retries--) {
623 mem_cgroup_out_of_memory(mem, gfp_mask);
624 goto out;
625 }
626 congestion_wait(WRITE, HZ/10);
627 }
628
629 atomic_set(&pc->ref_cnt, 1);
630 pc->mem_cgroup = mem;
631 pc->page = page;
632 pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
633 if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
634 pc->flags |= PAGE_CGROUP_FLAG_CACHE;
635
636 if (!page || page_cgroup_assign_new_page_cgroup(page, pc)) {
637 /*
638 * Another charge has been added to this page already.
639 * We take lock_page_cgroup(page) again and read
640 * page->cgroup, increment refcnt.... just retry is OK.
641 */
642 res_counter_uncharge(&mem->res, PAGE_SIZE);
643 css_put(&mem->css);
644 kfree(pc);
645 if (!page)
646 goto done;
647 goto retry;
648 }
649
650 spin_lock_irqsave(&mem->lru_lock, flags);
651 /* Update statistics vector */
652 __mem_cgroup_add_list(pc);
653 spin_unlock_irqrestore(&mem->lru_lock, flags);
654
655 done:
656 return 0;
657 out:
658 css_put(&mem->css);
659 kfree(pc);
660 err:
661 return -ENOMEM;
662 }
663
664 int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
665 gfp_t gfp_mask)
666 {
667 return mem_cgroup_charge_common(page, mm, gfp_mask,
668 MEM_CGROUP_CHARGE_TYPE_MAPPED);
669 }
670
671 /*
672 * See if the cached pages should be charged at all?
673 */
674 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
675 gfp_t gfp_mask)
676 {
677 int ret = 0;
678 struct mem_cgroup *mem;
679 if (!mm)
680 mm = &init_mm;
681
682 rcu_read_lock();
683 mem = rcu_dereference(mm->mem_cgroup);
684 css_get(&mem->css);
685 rcu_read_unlock();
686 if (mem->control_type == MEM_CGROUP_TYPE_ALL)
687 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
688 MEM_CGROUP_CHARGE_TYPE_CACHE);
689 css_put(&mem->css);
690 return ret;
691 }
692
693 /*
694 * Uncharging is always a welcome operation, we never complain, simply
695 * uncharge.
696 */
697 void mem_cgroup_uncharge(struct page_cgroup *pc)
698 {
699 struct mem_cgroup *mem;
700 struct page *page;
701 unsigned long flags;
702
703 /*
704 * This can handle cases when a page is not charged at all and we
705 * are switching between handling the control_type.
706 */
707 if (!pc)
708 return;
709
710 if (atomic_dec_and_test(&pc->ref_cnt)) {
711 page = pc->page;
712 /*
713 * get page->cgroup and clear it under lock.
714 * force_empty can drop page->cgroup without checking refcnt.
715 */
716 if (clear_page_cgroup(page, pc) == pc) {
717 mem = pc->mem_cgroup;
718 css_put(&mem->css);
719 res_counter_uncharge(&mem->res, PAGE_SIZE);
720 spin_lock_irqsave(&mem->lru_lock, flags);
721 __mem_cgroup_remove_list(pc);
722 spin_unlock_irqrestore(&mem->lru_lock, flags);
723 kfree(pc);
724 }
725 }
726 }
727
728 /*
729 * Returns non-zero if a page (under migration) has valid page_cgroup member.
730 * Refcnt of page_cgroup is incremented.
731 */
732
733 int mem_cgroup_prepare_migration(struct page *page)
734 {
735 struct page_cgroup *pc;
736 int ret = 0;
737 lock_page_cgroup(page);
738 pc = page_get_page_cgroup(page);
739 if (pc && atomic_inc_not_zero(&pc->ref_cnt))
740 ret = 1;
741 unlock_page_cgroup(page);
742 return ret;
743 }
744
745 void mem_cgroup_end_migration(struct page *page)
746 {
747 struct page_cgroup *pc = page_get_page_cgroup(page);
748 mem_cgroup_uncharge(pc);
749 }
750 /*
751 * We know both *page* and *newpage* are now not-on-LRU and Pg_locked.
752 * And no race with uncharge() routines because page_cgroup for *page*
753 * has extra one reference by mem_cgroup_prepare_migration.
754 */
755
756 void mem_cgroup_page_migration(struct page *page, struct page *newpage)
757 {
758 struct page_cgroup *pc;
759 struct mem_cgroup *mem;
760 unsigned long flags;
761 retry:
762 pc = page_get_page_cgroup(page);
763 if (!pc)
764 return;
765 mem = pc->mem_cgroup;
766 if (clear_page_cgroup(page, pc) != pc)
767 goto retry;
768
769 spin_lock_irqsave(&mem->lru_lock, flags);
770
771 __mem_cgroup_remove_list(pc);
772 pc->page = newpage;
773 lock_page_cgroup(newpage);
774 page_assign_page_cgroup(newpage, pc);
775 unlock_page_cgroup(newpage);
776 __mem_cgroup_add_list(pc);
777
778 spin_unlock_irqrestore(&mem->lru_lock, flags);
779 return;
780 }
781
782 /*
783 * This routine traverse page_cgroup in given list and drop them all.
784 * This routine ignores page_cgroup->ref_cnt.
785 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
786 */
787 #define FORCE_UNCHARGE_BATCH (128)
788 static void
789 mem_cgroup_force_empty_list(struct mem_cgroup *mem, struct list_head *list)
790 {
791 struct page_cgroup *pc;
792 struct page *page;
793 int count;
794 unsigned long flags;
795
796 retry:
797 count = FORCE_UNCHARGE_BATCH;
798 spin_lock_irqsave(&mem->lru_lock, flags);
799
800 while (--count && !list_empty(list)) {
801 pc = list_entry(list->prev, struct page_cgroup, lru);
802 page = pc->page;
803 /* Avoid race with charge */
804 atomic_set(&pc->ref_cnt, 0);
805 if (clear_page_cgroup(page, pc) == pc) {
806 css_put(&mem->css);
807 res_counter_uncharge(&mem->res, PAGE_SIZE);
808 __mem_cgroup_remove_list(pc);
809 kfree(pc);
810 } else /* being uncharged ? ...do relax */
811 break;
812 }
813 spin_unlock_irqrestore(&mem->lru_lock, flags);
814 if (!list_empty(list)) {
815 cond_resched();
816 goto retry;
817 }
818 return;
819 }
820
821 /*
822 * make mem_cgroup's charge to be 0 if there is no task.
823 * This enables deleting this mem_cgroup.
824 */
825
826 int mem_cgroup_force_empty(struct mem_cgroup *mem)
827 {
828 int ret = -EBUSY;
829 css_get(&mem->css);
830 /*
831 * page reclaim code (kswapd etc..) will move pages between
832 ` * active_list <-> inactive_list while we don't take a lock.
833 * So, we have to do loop here until all lists are empty.
834 */
835 while (!(list_empty(&mem->active_list) &&
836 list_empty(&mem->inactive_list))) {
837 if (atomic_read(&mem->css.cgroup->count) > 0)
838 goto out;
839 /* drop all page_cgroup in active_list */
840 mem_cgroup_force_empty_list(mem, &mem->active_list);
841 /* drop all page_cgroup in inactive_list */
842 mem_cgroup_force_empty_list(mem, &mem->inactive_list);
843 }
844 ret = 0;
845 out:
846 css_put(&mem->css);
847 return ret;
848 }
849
850
851
852 int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp)
853 {
854 *tmp = memparse(buf, &buf);
855 if (*buf != '\0')
856 return -EINVAL;
857
858 /*
859 * Round up the value to the closest page size
860 */
861 *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT;
862 return 0;
863 }
864
865 static ssize_t mem_cgroup_read(struct cgroup *cont,
866 struct cftype *cft, struct file *file,
867 char __user *userbuf, size_t nbytes, loff_t *ppos)
868 {
869 return res_counter_read(&mem_cgroup_from_cont(cont)->res,
870 cft->private, userbuf, nbytes, ppos,
871 NULL);
872 }
873
874 static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
875 struct file *file, const char __user *userbuf,
876 size_t nbytes, loff_t *ppos)
877 {
878 return res_counter_write(&mem_cgroup_from_cont(cont)->res,
879 cft->private, userbuf, nbytes, ppos,
880 mem_cgroup_write_strategy);
881 }
882
883 static ssize_t mem_control_type_write(struct cgroup *cont,
884 struct cftype *cft, struct file *file,
885 const char __user *userbuf,
886 size_t nbytes, loff_t *pos)
887 {
888 int ret;
889 char *buf, *end;
890 unsigned long tmp;
891 struct mem_cgroup *mem;
892
893 mem = mem_cgroup_from_cont(cont);
894 buf = kmalloc(nbytes + 1, GFP_KERNEL);
895 ret = -ENOMEM;
896 if (buf == NULL)
897 goto out;
898
899 buf[nbytes] = 0;
900 ret = -EFAULT;
901 if (copy_from_user(buf, userbuf, nbytes))
902 goto out_free;
903
904 ret = -EINVAL;
905 tmp = simple_strtoul(buf, &end, 10);
906 if (*end != '\0')
907 goto out_free;
908
909 if (tmp <= MEM_CGROUP_TYPE_UNSPEC || tmp >= MEM_CGROUP_TYPE_MAX)
910 goto out_free;
911
912 mem->control_type = tmp;
913 ret = nbytes;
914 out_free:
915 kfree(buf);
916 out:
917 return ret;
918 }
919
920 static ssize_t mem_control_type_read(struct cgroup *cont,
921 struct cftype *cft,
922 struct file *file, char __user *userbuf,
923 size_t nbytes, loff_t *ppos)
924 {
925 unsigned long val;
926 char buf[64], *s;
927 struct mem_cgroup *mem;
928
929 mem = mem_cgroup_from_cont(cont);
930 s = buf;
931 val = mem->control_type;
932 s += sprintf(s, "%lu\n", val);
933 return simple_read_from_buffer((void __user *)userbuf, nbytes,
934 ppos, buf, s - buf);
935 }
936
937
938 static ssize_t mem_force_empty_write(struct cgroup *cont,
939 struct cftype *cft, struct file *file,
940 const char __user *userbuf,
941 size_t nbytes, loff_t *ppos)
942 {
943 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
944 int ret;
945 ret = mem_cgroup_force_empty(mem);
946 if (!ret)
947 ret = nbytes;
948 return ret;
949 }
950
951 /*
952 * Note: This should be removed if cgroup supports write-only file.
953 */
954
955 static ssize_t mem_force_empty_read(struct cgroup *cont,
956 struct cftype *cft,
957 struct file *file, char __user *userbuf,
958 size_t nbytes, loff_t *ppos)
959 {
960 return -EINVAL;
961 }
962
963
964 static const struct mem_cgroup_stat_desc {
965 const char *msg;
966 u64 unit;
967 } mem_cgroup_stat_desc[] = {
968 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
969 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
970 };
971
972 static int mem_control_stat_show(struct seq_file *m, void *arg)
973 {
974 struct cgroup *cont = m->private;
975 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
976 struct mem_cgroup_stat *stat = &mem_cont->stat;
977 int i;
978
979 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
980 s64 val;
981
982 val = mem_cgroup_read_stat(stat, i);
983 val *= mem_cgroup_stat_desc[i].unit;
984 seq_printf(m, "%s %lld\n", mem_cgroup_stat_desc[i].msg,
985 (long long)val);
986 }
987 /* showing # of active pages */
988 {
989 unsigned long active, inactive;
990
991 inactive = mem_cgroup_get_all_zonestat(mem_cont,
992 MEM_CGROUP_ZSTAT_INACTIVE);
993 active = mem_cgroup_get_all_zonestat(mem_cont,
994 MEM_CGROUP_ZSTAT_ACTIVE);
995 seq_printf(m, "active %ld\n", (active) * PAGE_SIZE);
996 seq_printf(m, "inactive %ld\n", (inactive) * PAGE_SIZE);
997 }
998 return 0;
999 }
1000
1001 static const struct file_operations mem_control_stat_file_operations = {
1002 .read = seq_read,
1003 .llseek = seq_lseek,
1004 .release = single_release,
1005 };
1006
1007 static int mem_control_stat_open(struct inode *unused, struct file *file)
1008 {
1009 /* XXX __d_cont */
1010 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
1011
1012 file->f_op = &mem_control_stat_file_operations;
1013 return single_open(file, mem_control_stat_show, cont);
1014 }
1015
1016
1017
1018 static struct cftype mem_cgroup_files[] = {
1019 {
1020 .name = "usage_in_bytes",
1021 .private = RES_USAGE,
1022 .read = mem_cgroup_read,
1023 },
1024 {
1025 .name = "limit_in_bytes",
1026 .private = RES_LIMIT,
1027 .write = mem_cgroup_write,
1028 .read = mem_cgroup_read,
1029 },
1030 {
1031 .name = "failcnt",
1032 .private = RES_FAILCNT,
1033 .read = mem_cgroup_read,
1034 },
1035 {
1036 .name = "control_type",
1037 .write = mem_control_type_write,
1038 .read = mem_control_type_read,
1039 },
1040 {
1041 .name = "force_empty",
1042 .write = mem_force_empty_write,
1043 .read = mem_force_empty_read,
1044 },
1045 {
1046 .name = "stat",
1047 .open = mem_control_stat_open,
1048 },
1049 };
1050
1051 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1052 {
1053 struct mem_cgroup_per_node *pn;
1054
1055 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, node);
1056 if (!pn)
1057 return 1;
1058 mem->info.nodeinfo[node] = pn;
1059 memset(pn, 0, sizeof(*pn));
1060 return 0;
1061 }
1062
1063 static struct mem_cgroup init_mem_cgroup;
1064
1065 static struct cgroup_subsys_state *
1066 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1067 {
1068 struct mem_cgroup *mem;
1069 int node;
1070
1071 if (unlikely((cont->parent) == NULL)) {
1072 mem = &init_mem_cgroup;
1073 init_mm.mem_cgroup = mem;
1074 } else
1075 mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL);
1076
1077 if (mem == NULL)
1078 return NULL;
1079
1080 res_counter_init(&mem->res);
1081 INIT_LIST_HEAD(&mem->active_list);
1082 INIT_LIST_HEAD(&mem->inactive_list);
1083 spin_lock_init(&mem->lru_lock);
1084 mem->control_type = MEM_CGROUP_TYPE_ALL;
1085 memset(&mem->info, 0, sizeof(mem->info));
1086
1087 for_each_node_state(node, N_POSSIBLE)
1088 if (alloc_mem_cgroup_per_zone_info(mem, node))
1089 goto free_out;
1090
1091 return &mem->css;
1092 free_out:
1093 for_each_node_state(node, N_POSSIBLE)
1094 kfree(mem->info.nodeinfo[node]);
1095 if (cont->parent != NULL)
1096 kfree(mem);
1097 return NULL;
1098 }
1099
1100 static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1101 struct cgroup *cont)
1102 {
1103 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1104 mem_cgroup_force_empty(mem);
1105 }
1106
1107 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1108 struct cgroup *cont)
1109 {
1110 int node;
1111 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1112
1113 for_each_node_state(node, N_POSSIBLE)
1114 kfree(mem->info.nodeinfo[node]);
1115
1116 kfree(mem_cgroup_from_cont(cont));
1117 }
1118
1119 static int mem_cgroup_populate(struct cgroup_subsys *ss,
1120 struct cgroup *cont)
1121 {
1122 return cgroup_add_files(cont, ss, mem_cgroup_files,
1123 ARRAY_SIZE(mem_cgroup_files));
1124 }
1125
1126 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1127 struct cgroup *cont,
1128 struct cgroup *old_cont,
1129 struct task_struct *p)
1130 {
1131 struct mm_struct *mm;
1132 struct mem_cgroup *mem, *old_mem;
1133
1134 mm = get_task_mm(p);
1135 if (mm == NULL)
1136 return;
1137
1138 mem = mem_cgroup_from_cont(cont);
1139 old_mem = mem_cgroup_from_cont(old_cont);
1140
1141 if (mem == old_mem)
1142 goto out;
1143
1144 /*
1145 * Only thread group leaders are allowed to migrate, the mm_struct is
1146 * in effect owned by the leader
1147 */
1148 if (p->tgid != p->pid)
1149 goto out;
1150
1151 css_get(&mem->css);
1152 rcu_assign_pointer(mm->mem_cgroup, mem);
1153 css_put(&old_mem->css);
1154
1155 out:
1156 mmput(mm);
1157 return;
1158 }
1159
1160 struct cgroup_subsys mem_cgroup_subsys = {
1161 .name = "memory",
1162 .subsys_id = mem_cgroup_subsys_id,
1163 .create = mem_cgroup_create,
1164 .pre_destroy = mem_cgroup_pre_destroy,
1165 .destroy = mem_cgroup_destroy,
1166 .populate = mem_cgroup_populate,
1167 .attach = mem_cgroup_move_task,
1168 .early_init = 0,
1169 };
This page took 0.056079 seconds and 6 git commands to generate.