Commit | Line | Data |
---|---|---|
8cdea7c0 BS |
1 | /* memcontrol.c - Memory Controller |
2 | * | |
3 | * Copyright IBM Corporation, 2007 | |
4 | * Author Balbir Singh <balbir@linux.vnet.ibm.com> | |
5 | * | |
78fb7466 PE |
6 | * Copyright 2007 OpenVZ SWsoft Inc |
7 | * Author: Pavel Emelianov <xemul@openvz.org> | |
8 | * | |
8cdea7c0 BS |
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> | |
78fb7466 | 23 | #include <linux/mm.h> |
d52aa412 | 24 | #include <linux/smp.h> |
8a9f3ccd | 25 | #include <linux/page-flags.h> |
66e1707b | 26 | #include <linux/backing-dev.h> |
8a9f3ccd BS |
27 | #include <linux/bit_spinlock.h> |
28 | #include <linux/rcupdate.h> | |
b6ac57d5 | 29 | #include <linux/slab.h> |
66e1707b BS |
30 | #include <linux/swap.h> |
31 | #include <linux/spinlock.h> | |
32 | #include <linux/fs.h> | |
d2ceb9b7 | 33 | #include <linux/seq_file.h> |
33327948 | 34 | #include <linux/vmalloc.h> |
8cdea7c0 | 35 | |
8697d331 BS |
36 | #include <asm/uaccess.h> |
37 | ||
a181b0e8 KH |
38 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
39 | static struct kmem_cache *page_cgroup_cache __read_mostly; | |
40 | #define MEM_CGROUP_RECLAIM_RETRIES 5 | |
8cdea7c0 | 41 | |
d52aa412 KH |
42 | /* |
43 | * Statistics for memory cgroup. | |
44 | */ | |
45 | enum mem_cgroup_stat_index { | |
46 | /* | |
47 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. | |
48 | */ | |
49 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ | |
50 | MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */ | |
55e462b0 BR |
51 | MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */ |
52 | MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */ | |
d52aa412 KH |
53 | |
54 | MEM_CGROUP_STAT_NSTATS, | |
55 | }; | |
56 | ||
57 | struct mem_cgroup_stat_cpu { | |
58 | s64 count[MEM_CGROUP_STAT_NSTATS]; | |
59 | } ____cacheline_aligned_in_smp; | |
60 | ||
61 | struct mem_cgroup_stat { | |
62 | struct mem_cgroup_stat_cpu cpustat[NR_CPUS]; | |
63 | }; | |
64 | ||
65 | /* | |
66 | * For accounting under irq disable, no need for increment preempt count. | |
67 | */ | |
68 | static void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat *stat, | |
69 | enum mem_cgroup_stat_index idx, int val) | |
70 | { | |
71 | int cpu = smp_processor_id(); | |
72 | stat->cpustat[cpu].count[idx] += val; | |
73 | } | |
74 | ||
75 | static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat, | |
76 | enum mem_cgroup_stat_index idx) | |
77 | { | |
78 | int cpu; | |
79 | s64 ret = 0; | |
80 | for_each_possible_cpu(cpu) | |
81 | ret += stat->cpustat[cpu].count[idx]; | |
82 | return ret; | |
83 | } | |
84 | ||
6d12e2d8 KH |
85 | /* |
86 | * per-zone information in memory controller. | |
87 | */ | |
88 | ||
89 | enum mem_cgroup_zstat_index { | |
90 | MEM_CGROUP_ZSTAT_ACTIVE, | |
91 | MEM_CGROUP_ZSTAT_INACTIVE, | |
92 | ||
93 | NR_MEM_CGROUP_ZSTAT, | |
94 | }; | |
95 | ||
96 | struct mem_cgroup_per_zone { | |
072c56c1 KH |
97 | /* |
98 | * spin_lock to protect the per cgroup LRU | |
99 | */ | |
100 | spinlock_t lru_lock; | |
1ecaab2b KH |
101 | struct list_head active_list; |
102 | struct list_head inactive_list; | |
6d12e2d8 KH |
103 | unsigned long count[NR_MEM_CGROUP_ZSTAT]; |
104 | }; | |
105 | /* Macro for accessing counter */ | |
106 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) | |
107 | ||
108 | struct mem_cgroup_per_node { | |
109 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; | |
110 | }; | |
111 | ||
112 | struct mem_cgroup_lru_info { | |
113 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; | |
114 | }; | |
115 | ||
8cdea7c0 BS |
116 | /* |
117 | * The memory controller data structure. The memory controller controls both | |
118 | * page cache and RSS per cgroup. We would eventually like to provide | |
119 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, | |
120 | * to help the administrator determine what knobs to tune. | |
121 | * | |
122 | * TODO: Add a water mark for the memory controller. Reclaim will begin when | |
8a9f3ccd BS |
123 | * we hit the water mark. May be even add a low water mark, such that |
124 | * no reclaim occurs from a cgroup at it's low water mark, this is | |
125 | * a feature that will be implemented much later in the future. | |
8cdea7c0 BS |
126 | */ |
127 | struct mem_cgroup { | |
128 | struct cgroup_subsys_state css; | |
129 | /* | |
130 | * the counter to account for memory usage | |
131 | */ | |
132 | struct res_counter res; | |
78fb7466 PE |
133 | /* |
134 | * Per cgroup active and inactive list, similar to the | |
135 | * per zone LRU lists. | |
78fb7466 | 136 | */ |
6d12e2d8 | 137 | struct mem_cgroup_lru_info info; |
072c56c1 | 138 | |
6c48a1d0 | 139 | int prev_priority; /* for recording reclaim priority */ |
d52aa412 KH |
140 | /* |
141 | * statistics. | |
142 | */ | |
143 | struct mem_cgroup_stat stat; | |
8cdea7c0 | 144 | }; |
8869b8f6 | 145 | static struct mem_cgroup init_mem_cgroup; |
8cdea7c0 | 146 | |
8a9f3ccd BS |
147 | /* |
148 | * We use the lower bit of the page->page_cgroup pointer as a bit spin | |
9442ec9d HD |
149 | * lock. We need to ensure that page->page_cgroup is at least two |
150 | * byte aligned (based on comments from Nick Piggin). But since | |
151 | * bit_spin_lock doesn't actually set that lock bit in a non-debug | |
152 | * uniprocessor kernel, we should avoid setting it here too. | |
8a9f3ccd BS |
153 | */ |
154 | #define PAGE_CGROUP_LOCK_BIT 0x0 | |
9442ec9d HD |
155 | #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) |
156 | #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT) | |
157 | #else | |
158 | #define PAGE_CGROUP_LOCK 0x0 | |
159 | #endif | |
8a9f3ccd | 160 | |
8cdea7c0 BS |
161 | /* |
162 | * A page_cgroup page is associated with every page descriptor. The | |
163 | * page_cgroup helps us identify information about the cgroup | |
164 | */ | |
165 | struct page_cgroup { | |
166 | struct list_head lru; /* per cgroup LRU list */ | |
167 | struct page *page; | |
168 | struct mem_cgroup *mem_cgroup; | |
8869b8f6 | 169 | int flags; |
8cdea7c0 | 170 | }; |
217bc319 | 171 | #define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */ |
3564c7c4 | 172 | #define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */ |
8cdea7c0 | 173 | |
d5b69e38 | 174 | static int page_cgroup_nid(struct page_cgroup *pc) |
c0149530 KH |
175 | { |
176 | return page_to_nid(pc->page); | |
177 | } | |
178 | ||
d5b69e38 | 179 | static enum zone_type page_cgroup_zid(struct page_cgroup *pc) |
c0149530 KH |
180 | { |
181 | return page_zonenum(pc->page); | |
182 | } | |
183 | ||
217bc319 KH |
184 | enum charge_type { |
185 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, | |
186 | MEM_CGROUP_CHARGE_TYPE_MAPPED, | |
69029cd5 | 187 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
217bc319 KH |
188 | }; |
189 | ||
d52aa412 KH |
190 | /* |
191 | * Always modified under lru lock. Then, not necessary to preempt_disable() | |
192 | */ | |
193 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags, | |
194 | bool charge) | |
195 | { | |
196 | int val = (charge)? 1 : -1; | |
197 | struct mem_cgroup_stat *stat = &mem->stat; | |
d52aa412 | 198 | |
8869b8f6 | 199 | VM_BUG_ON(!irqs_disabled()); |
d52aa412 | 200 | if (flags & PAGE_CGROUP_FLAG_CACHE) |
8869b8f6 | 201 | __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_CACHE, val); |
d52aa412 KH |
202 | else |
203 | __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val); | |
55e462b0 BR |
204 | |
205 | if (charge) | |
206 | __mem_cgroup_stat_add_safe(stat, | |
207 | MEM_CGROUP_STAT_PGPGIN_COUNT, 1); | |
208 | else | |
209 | __mem_cgroup_stat_add_safe(stat, | |
210 | MEM_CGROUP_STAT_PGPGOUT_COUNT, 1); | |
6d12e2d8 KH |
211 | } |
212 | ||
d5b69e38 | 213 | static struct mem_cgroup_per_zone * |
6d12e2d8 KH |
214 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) |
215 | { | |
6d12e2d8 KH |
216 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; |
217 | } | |
218 | ||
d5b69e38 | 219 | static struct mem_cgroup_per_zone * |
6d12e2d8 KH |
220 | page_cgroup_zoneinfo(struct page_cgroup *pc) |
221 | { | |
222 | struct mem_cgroup *mem = pc->mem_cgroup; | |
223 | int nid = page_cgroup_nid(pc); | |
224 | int zid = page_cgroup_zid(pc); | |
d52aa412 | 225 | |
6d12e2d8 KH |
226 | return mem_cgroup_zoneinfo(mem, nid, zid); |
227 | } | |
228 | ||
229 | static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem, | |
230 | enum mem_cgroup_zstat_index idx) | |
231 | { | |
232 | int nid, zid; | |
233 | struct mem_cgroup_per_zone *mz; | |
234 | u64 total = 0; | |
235 | ||
236 | for_each_online_node(nid) | |
237 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { | |
238 | mz = mem_cgroup_zoneinfo(mem, nid, zid); | |
239 | total += MEM_CGROUP_ZSTAT(mz, idx); | |
240 | } | |
241 | return total; | |
d52aa412 KH |
242 | } |
243 | ||
d5b69e38 | 244 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
8cdea7c0 BS |
245 | { |
246 | return container_of(cgroup_subsys_state(cont, | |
247 | mem_cgroup_subsys_id), struct mem_cgroup, | |
248 | css); | |
249 | } | |
250 | ||
cf475ad2 | 251 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
78fb7466 PE |
252 | { |
253 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), | |
254 | struct mem_cgroup, css); | |
255 | } | |
256 | ||
8a9f3ccd BS |
257 | static inline int page_cgroup_locked(struct page *page) |
258 | { | |
8869b8f6 | 259 | return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); |
8a9f3ccd BS |
260 | } |
261 | ||
9442ec9d | 262 | static void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc) |
78fb7466 | 263 | { |
9442ec9d HD |
264 | VM_BUG_ON(!page_cgroup_locked(page)); |
265 | page->page_cgroup = ((unsigned long)pc | PAGE_CGROUP_LOCK); | |
78fb7466 PE |
266 | } |
267 | ||
268 | struct page_cgroup *page_get_page_cgroup(struct page *page) | |
269 | { | |
8869b8f6 | 270 | return (struct page_cgroup *) (page->page_cgroup & ~PAGE_CGROUP_LOCK); |
8a9f3ccd BS |
271 | } |
272 | ||
d5b69e38 | 273 | static void lock_page_cgroup(struct page *page) |
8a9f3ccd BS |
274 | { |
275 | bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); | |
8a9f3ccd BS |
276 | } |
277 | ||
2680eed7 HD |
278 | static int try_lock_page_cgroup(struct page *page) |
279 | { | |
280 | return bit_spin_trylock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); | |
281 | } | |
282 | ||
d5b69e38 | 283 | static void unlock_page_cgroup(struct page *page) |
8a9f3ccd BS |
284 | { |
285 | bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); | |
286 | } | |
287 | ||
3eae90c3 KH |
288 | static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz, |
289 | struct page_cgroup *pc) | |
6d12e2d8 KH |
290 | { |
291 | int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE; | |
6d12e2d8 KH |
292 | |
293 | if (from) | |
294 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1; | |
295 | else | |
296 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1; | |
297 | ||
298 | mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false); | |
508b7be0 | 299 | list_del(&pc->lru); |
6d12e2d8 KH |
300 | } |
301 | ||
3eae90c3 KH |
302 | static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz, |
303 | struct page_cgroup *pc) | |
6d12e2d8 KH |
304 | { |
305 | int to = pc->flags & PAGE_CGROUP_FLAG_ACTIVE; | |
6d12e2d8 KH |
306 | |
307 | if (!to) { | |
308 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1; | |
1ecaab2b | 309 | list_add(&pc->lru, &mz->inactive_list); |
6d12e2d8 KH |
310 | } else { |
311 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1; | |
1ecaab2b | 312 | list_add(&pc->lru, &mz->active_list); |
6d12e2d8 KH |
313 | } |
314 | mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true); | |
315 | } | |
316 | ||
8697d331 | 317 | static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active) |
66e1707b | 318 | { |
6d12e2d8 KH |
319 | int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE; |
320 | struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc); | |
321 | ||
322 | if (from) | |
323 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1; | |
324 | else | |
325 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1; | |
326 | ||
3564c7c4 | 327 | if (active) { |
6d12e2d8 | 328 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1; |
3564c7c4 | 329 | pc->flags |= PAGE_CGROUP_FLAG_ACTIVE; |
1ecaab2b | 330 | list_move(&pc->lru, &mz->active_list); |
3564c7c4 | 331 | } else { |
6d12e2d8 | 332 | MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1; |
3564c7c4 | 333 | pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE; |
1ecaab2b | 334 | list_move(&pc->lru, &mz->inactive_list); |
3564c7c4 | 335 | } |
66e1707b BS |
336 | } |
337 | ||
4c4a2214 DR |
338 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
339 | { | |
340 | int ret; | |
341 | ||
342 | task_lock(task); | |
bd845e38 | 343 | ret = task->mm && mm_match_cgroup(task->mm, mem); |
4c4a2214 DR |
344 | task_unlock(task); |
345 | return ret; | |
346 | } | |
347 | ||
66e1707b BS |
348 | /* |
349 | * This routine assumes that the appropriate zone's lru lock is already held | |
350 | */ | |
427d5416 | 351 | void mem_cgroup_move_lists(struct page *page, bool active) |
66e1707b | 352 | { |
427d5416 | 353 | struct page_cgroup *pc; |
072c56c1 KH |
354 | struct mem_cgroup_per_zone *mz; |
355 | unsigned long flags; | |
356 | ||
2680eed7 HD |
357 | /* |
358 | * We cannot lock_page_cgroup while holding zone's lru_lock, | |
359 | * because other holders of lock_page_cgroup can be interrupted | |
360 | * with an attempt to rotate_reclaimable_page. But we cannot | |
361 | * safely get to page_cgroup without it, so just try_lock it: | |
362 | * mem_cgroup_isolate_pages allows for page left on wrong list. | |
363 | */ | |
364 | if (!try_lock_page_cgroup(page)) | |
66e1707b BS |
365 | return; |
366 | ||
2680eed7 HD |
367 | pc = page_get_page_cgroup(page); |
368 | if (pc) { | |
2680eed7 | 369 | mz = page_cgroup_zoneinfo(pc); |
2680eed7 | 370 | spin_lock_irqsave(&mz->lru_lock, flags); |
9b3c0a07 | 371 | __mem_cgroup_move_lists(pc, active); |
2680eed7 | 372 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
9b3c0a07 HT |
373 | } |
374 | unlock_page_cgroup(page); | |
66e1707b BS |
375 | } |
376 | ||
58ae83db KH |
377 | /* |
378 | * Calculate mapped_ratio under memory controller. This will be used in | |
379 | * vmscan.c for deteremining we have to reclaim mapped pages. | |
380 | */ | |
381 | int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem) | |
382 | { | |
383 | long total, rss; | |
384 | ||
385 | /* | |
386 | * usage is recorded in bytes. But, here, we assume the number of | |
387 | * physical pages can be represented by "long" on any arch. | |
388 | */ | |
389 | total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L; | |
390 | rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS); | |
391 | return (int)((rss * 100L) / total); | |
392 | } | |
8869b8f6 | 393 | |
5932f367 KH |
394 | /* |
395 | * This function is called from vmscan.c. In page reclaiming loop. balance | |
396 | * between active and inactive list is calculated. For memory controller | |
397 | * page reclaiming, we should use using mem_cgroup's imbalance rather than | |
398 | * zone's global lru imbalance. | |
399 | */ | |
400 | long mem_cgroup_reclaim_imbalance(struct mem_cgroup *mem) | |
401 | { | |
402 | unsigned long active, inactive; | |
403 | /* active and inactive are the number of pages. 'long' is ok.*/ | |
404 | active = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_ACTIVE); | |
405 | inactive = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_INACTIVE); | |
406 | return (long) (active / (inactive + 1)); | |
407 | } | |
58ae83db | 408 | |
6c48a1d0 KH |
409 | /* |
410 | * prev_priority control...this will be used in memory reclaim path. | |
411 | */ | |
412 | int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem) | |
413 | { | |
414 | return mem->prev_priority; | |
415 | } | |
416 | ||
417 | void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority) | |
418 | { | |
419 | if (priority < mem->prev_priority) | |
420 | mem->prev_priority = priority; | |
421 | } | |
422 | ||
423 | void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority) | |
424 | { | |
425 | mem->prev_priority = priority; | |
426 | } | |
427 | ||
cc38108e KH |
428 | /* |
429 | * Calculate # of pages to be scanned in this priority/zone. | |
430 | * See also vmscan.c | |
431 | * | |
432 | * priority starts from "DEF_PRIORITY" and decremented in each loop. | |
433 | * (see include/linux/mmzone.h) | |
434 | */ | |
435 | ||
436 | long mem_cgroup_calc_reclaim_active(struct mem_cgroup *mem, | |
437 | struct zone *zone, int priority) | |
438 | { | |
439 | long nr_active; | |
440 | int nid = zone->zone_pgdat->node_id; | |
441 | int zid = zone_idx(zone); | |
442 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid); | |
443 | ||
444 | nr_active = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE); | |
445 | return (nr_active >> priority); | |
446 | } | |
447 | ||
448 | long mem_cgroup_calc_reclaim_inactive(struct mem_cgroup *mem, | |
449 | struct zone *zone, int priority) | |
450 | { | |
451 | long nr_inactive; | |
452 | int nid = zone->zone_pgdat->node_id; | |
453 | int zid = zone_idx(zone); | |
454 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid); | |
455 | ||
456 | nr_inactive = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE); | |
cc38108e KH |
457 | return (nr_inactive >> priority); |
458 | } | |
459 | ||
66e1707b BS |
460 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
461 | struct list_head *dst, | |
462 | unsigned long *scanned, int order, | |
463 | int mode, struct zone *z, | |
464 | struct mem_cgroup *mem_cont, | |
465 | int active) | |
466 | { | |
467 | unsigned long nr_taken = 0; | |
468 | struct page *page; | |
469 | unsigned long scan; | |
470 | LIST_HEAD(pc_list); | |
471 | struct list_head *src; | |
ff7283fa | 472 | struct page_cgroup *pc, *tmp; |
1ecaab2b KH |
473 | int nid = z->zone_pgdat->node_id; |
474 | int zid = zone_idx(z); | |
475 | struct mem_cgroup_per_zone *mz; | |
66e1707b | 476 | |
cf475ad2 | 477 | BUG_ON(!mem_cont); |
1ecaab2b | 478 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
66e1707b | 479 | if (active) |
1ecaab2b | 480 | src = &mz->active_list; |
66e1707b | 481 | else |
1ecaab2b KH |
482 | src = &mz->inactive_list; |
483 | ||
66e1707b | 484 | |
072c56c1 | 485 | spin_lock(&mz->lru_lock); |
ff7283fa KH |
486 | scan = 0; |
487 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { | |
436c6541 | 488 | if (scan >= nr_to_scan) |
ff7283fa | 489 | break; |
66e1707b | 490 | page = pc->page; |
66e1707b | 491 | |
436c6541 | 492 | if (unlikely(!PageLRU(page))) |
ff7283fa | 493 | continue; |
ff7283fa | 494 | |
66e1707b BS |
495 | if (PageActive(page) && !active) { |
496 | __mem_cgroup_move_lists(pc, true); | |
66e1707b BS |
497 | continue; |
498 | } | |
499 | if (!PageActive(page) && active) { | |
500 | __mem_cgroup_move_lists(pc, false); | |
66e1707b BS |
501 | continue; |
502 | } | |
503 | ||
436c6541 HD |
504 | scan++; |
505 | list_move(&pc->lru, &pc_list); | |
66e1707b BS |
506 | |
507 | if (__isolate_lru_page(page, mode) == 0) { | |
508 | list_move(&page->lru, dst); | |
509 | nr_taken++; | |
510 | } | |
511 | } | |
512 | ||
513 | list_splice(&pc_list, src); | |
072c56c1 | 514 | spin_unlock(&mz->lru_lock); |
66e1707b BS |
515 | |
516 | *scanned = scan; | |
517 | return nr_taken; | |
518 | } | |
519 | ||
8a9f3ccd BS |
520 | /* |
521 | * Charge the memory controller for page usage. | |
522 | * Return | |
523 | * 0 if the charge was successful | |
524 | * < 0 if the cgroup is over its limit | |
525 | */ | |
217bc319 | 526 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, |
e8589cc1 KH |
527 | gfp_t gfp_mask, enum charge_type ctype, |
528 | struct mem_cgroup *memcg) | |
8a9f3ccd BS |
529 | { |
530 | struct mem_cgroup *mem; | |
9175e031 | 531 | struct page_cgroup *pc; |
66e1707b BS |
532 | unsigned long flags; |
533 | unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES; | |
072c56c1 | 534 | struct mem_cgroup_per_zone *mz; |
8a9f3ccd | 535 | |
4077960e BS |
536 | if (mem_cgroup_subsys.disabled) |
537 | return 0; | |
538 | ||
8a9f3ccd BS |
539 | /* |
540 | * Should page_cgroup's go to their own slab? | |
541 | * One could optimize the performance of the charging routine | |
542 | * by saving a bit in the page_flags and using it as a lock | |
543 | * to see if the cgroup page already has a page_cgroup associated | |
544 | * with it | |
545 | */ | |
66e1707b | 546 | retry: |
7e924aaf HD |
547 | lock_page_cgroup(page); |
548 | pc = page_get_page_cgroup(page); | |
549 | /* | |
550 | * The page_cgroup exists and | |
551 | * the page has already been accounted. | |
552 | */ | |
553 | if (pc) { | |
b9c565d5 | 554 | VM_BUG_ON(pc->page != page); |
69029cd5 | 555 | VM_BUG_ON(!pc->mem_cgroup); |
b9c565d5 HD |
556 | unlock_page_cgroup(page); |
557 | goto done; | |
8a9f3ccd | 558 | } |
7e924aaf | 559 | unlock_page_cgroup(page); |
8a9f3ccd | 560 | |
508b7be0 | 561 | pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask); |
8a9f3ccd BS |
562 | if (pc == NULL) |
563 | goto err; | |
564 | ||
8a9f3ccd | 565 | /* |
3be91277 HD |
566 | * We always charge the cgroup the mm_struct belongs to. |
567 | * The mm_struct's mem_cgroup changes on task migration if the | |
8a9f3ccd BS |
568 | * thread group leader migrates. It's possible that mm is not |
569 | * set, if so charge the init_mm (happens for pagecache usage). | |
570 | */ | |
69029cd5 | 571 | if (likely(!memcg)) { |
e8589cc1 KH |
572 | rcu_read_lock(); |
573 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); | |
574 | /* | |
575 | * For every charge from the cgroup, increment reference count | |
576 | */ | |
577 | css_get(&mem->css); | |
578 | rcu_read_unlock(); | |
579 | } else { | |
580 | mem = memcg; | |
581 | css_get(&memcg->css); | |
582 | } | |
8a9f3ccd | 583 | |
0eea1030 | 584 | while (res_counter_charge(&mem->res, PAGE_SIZE)) { |
3be91277 HD |
585 | if (!(gfp_mask & __GFP_WAIT)) |
586 | goto out; | |
e1a1cd59 BS |
587 | |
588 | if (try_to_free_mem_cgroup_pages(mem, gfp_mask)) | |
66e1707b BS |
589 | continue; |
590 | ||
591 | /* | |
8869b8f6 HD |
592 | * try_to_free_mem_cgroup_pages() might not give us a full |
593 | * picture of reclaim. Some pages are reclaimed and might be | |
594 | * moved to swap cache or just unmapped from the cgroup. | |
595 | * Check the limit again to see if the reclaim reduced the | |
596 | * current usage of the cgroup before giving up | |
597 | */ | |
66e1707b BS |
598 | if (res_counter_check_under_limit(&mem->res)) |
599 | continue; | |
3be91277 HD |
600 | |
601 | if (!nr_retries--) { | |
602 | mem_cgroup_out_of_memory(mem, gfp_mask); | |
603 | goto out; | |
66e1707b | 604 | } |
8a9f3ccd BS |
605 | } |
606 | ||
8a9f3ccd BS |
607 | pc->mem_cgroup = mem; |
608 | pc->page = page; | |
508b7be0 KH |
609 | /* |
610 | * If a page is accounted as a page cache, insert to inactive list. | |
611 | * If anon, insert to active list. | |
612 | */ | |
217bc319 | 613 | if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE) |
4a56d02e | 614 | pc->flags = PAGE_CGROUP_FLAG_CACHE; |
508b7be0 KH |
615 | else |
616 | pc->flags = PAGE_CGROUP_FLAG_ACTIVE; | |
3be91277 | 617 | |
7e924aaf HD |
618 | lock_page_cgroup(page); |
619 | if (page_get_page_cgroup(page)) { | |
620 | unlock_page_cgroup(page); | |
9175e031 | 621 | /* |
3be91277 HD |
622 | * Another charge has been added to this page already. |
623 | * We take lock_page_cgroup(page) again and read | |
9175e031 KH |
624 | * page->cgroup, increment refcnt.... just retry is OK. |
625 | */ | |
626 | res_counter_uncharge(&mem->res, PAGE_SIZE); | |
627 | css_put(&mem->css); | |
b6ac57d5 | 628 | kmem_cache_free(page_cgroup_cache, pc); |
9175e031 KH |
629 | goto retry; |
630 | } | |
7e924aaf | 631 | page_assign_page_cgroup(page, pc); |
8a9f3ccd | 632 | |
072c56c1 KH |
633 | mz = page_cgroup_zoneinfo(pc); |
634 | spin_lock_irqsave(&mz->lru_lock, flags); | |
3eae90c3 | 635 | __mem_cgroup_add_list(mz, pc); |
072c56c1 | 636 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
66e1707b | 637 | |
fb59e9f1 | 638 | unlock_page_cgroup(page); |
8a9f3ccd | 639 | done: |
8a9f3ccd | 640 | return 0; |
3be91277 HD |
641 | out: |
642 | css_put(&mem->css); | |
b6ac57d5 | 643 | kmem_cache_free(page_cgroup_cache, pc); |
8a9f3ccd | 644 | err: |
8a9f3ccd BS |
645 | return -ENOMEM; |
646 | } | |
647 | ||
8869b8f6 | 648 | int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask) |
217bc319 | 649 | { |
69029cd5 KH |
650 | /* |
651 | * If already mapped, we don't have to account. | |
652 | * If page cache, page->mapping has address_space. | |
653 | * But page->mapping may have out-of-use anon_vma pointer, | |
654 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping | |
655 | * is NULL. | |
656 | */ | |
657 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) | |
658 | return 0; | |
659 | if (unlikely(!mm)) | |
660 | mm = &init_mm; | |
217bc319 | 661 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
e8589cc1 | 662 | MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL); |
217bc319 KH |
663 | } |
664 | ||
e1a1cd59 BS |
665 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
666 | gfp_t gfp_mask) | |
8697d331 | 667 | { |
69029cd5 | 668 | if (unlikely(!mm)) |
8697d331 | 669 | mm = &init_mm; |
8869b8f6 | 670 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
e8589cc1 KH |
671 | MEM_CGROUP_CHARGE_TYPE_CACHE, NULL); |
672 | } | |
673 | ||
8a9f3ccd | 674 | /* |
69029cd5 | 675 | * uncharge if !page_mapped(page) |
8a9f3ccd | 676 | */ |
69029cd5 KH |
677 | static void |
678 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) | |
8a9f3ccd | 679 | { |
8289546e | 680 | struct page_cgroup *pc; |
8a9f3ccd | 681 | struct mem_cgroup *mem; |
072c56c1 | 682 | struct mem_cgroup_per_zone *mz; |
66e1707b | 683 | unsigned long flags; |
8a9f3ccd | 684 | |
4077960e BS |
685 | if (mem_cgroup_subsys.disabled) |
686 | return; | |
687 | ||
8697d331 | 688 | /* |
3c541e14 | 689 | * Check if our page_cgroup is valid |
8697d331 | 690 | */ |
8289546e HD |
691 | lock_page_cgroup(page); |
692 | pc = page_get_page_cgroup(page); | |
8a9f3ccd | 693 | if (!pc) |
8289546e | 694 | goto unlock; |
8a9f3ccd | 695 | |
b9c565d5 | 696 | VM_BUG_ON(pc->page != page); |
b9c565d5 | 697 | |
69029cd5 KH |
698 | if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED) |
699 | && ((pc->flags & PAGE_CGROUP_FLAG_CACHE) | |
700 | || page_mapped(page))) | |
701 | goto unlock; | |
b9c565d5 | 702 | |
69029cd5 KH |
703 | mz = page_cgroup_zoneinfo(pc); |
704 | spin_lock_irqsave(&mz->lru_lock, flags); | |
705 | __mem_cgroup_remove_list(mz, pc); | |
706 | spin_unlock_irqrestore(&mz->lru_lock, flags); | |
fb59e9f1 | 707 | |
69029cd5 KH |
708 | page_assign_page_cgroup(page, NULL); |
709 | unlock_page_cgroup(page); | |
6d48ff8b | 710 | |
69029cd5 KH |
711 | mem = pc->mem_cgroup; |
712 | res_counter_uncharge(&mem->res, PAGE_SIZE); | |
713 | css_put(&mem->css); | |
6d12e2d8 | 714 | |
69029cd5 KH |
715 | kmem_cache_free(page_cgroup_cache, pc); |
716 | return; | |
8289546e | 717 | unlock: |
3c541e14 BS |
718 | unlock_page_cgroup(page); |
719 | } | |
720 | ||
69029cd5 KH |
721 | void mem_cgroup_uncharge_page(struct page *page) |
722 | { | |
723 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); | |
724 | } | |
725 | ||
726 | void mem_cgroup_uncharge_cache_page(struct page *page) | |
727 | { | |
728 | VM_BUG_ON(page_mapped(page)); | |
729 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); | |
730 | } | |
731 | ||
ae41be37 | 732 | /* |
e8589cc1 | 733 | * Before starting migration, account against new page. |
ae41be37 | 734 | */ |
e8589cc1 | 735 | int mem_cgroup_prepare_migration(struct page *page, struct page *newpage) |
ae41be37 KH |
736 | { |
737 | struct page_cgroup *pc; | |
e8589cc1 KH |
738 | struct mem_cgroup *mem = NULL; |
739 | enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; | |
740 | int ret = 0; | |
8869b8f6 | 741 | |
4077960e BS |
742 | if (mem_cgroup_subsys.disabled) |
743 | return 0; | |
744 | ||
ae41be37 KH |
745 | lock_page_cgroup(page); |
746 | pc = page_get_page_cgroup(page); | |
e8589cc1 KH |
747 | if (pc) { |
748 | mem = pc->mem_cgroup; | |
749 | css_get(&mem->css); | |
750 | if (pc->flags & PAGE_CGROUP_FLAG_CACHE) | |
751 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; | |
752 | } | |
ae41be37 | 753 | unlock_page_cgroup(page); |
e8589cc1 KH |
754 | if (mem) { |
755 | ret = mem_cgroup_charge_common(newpage, NULL, GFP_KERNEL, | |
756 | ctype, mem); | |
757 | css_put(&mem->css); | |
758 | } | |
759 | return ret; | |
ae41be37 | 760 | } |
8869b8f6 | 761 | |
69029cd5 | 762 | /* remove redundant charge if migration failed*/ |
e8589cc1 | 763 | void mem_cgroup_end_migration(struct page *newpage) |
ae41be37 | 764 | { |
69029cd5 KH |
765 | /* |
766 | * At success, page->mapping is not NULL. | |
767 | * special rollback care is necessary when | |
768 | * 1. at migration failure. (newpage->mapping is cleared in this case) | |
769 | * 2. the newpage was moved but not remapped again because the task | |
770 | * exits and the newpage is obsolete. In this case, the new page | |
771 | * may be a swapcache. So, we just call mem_cgroup_uncharge_page() | |
772 | * always for avoiding mess. The page_cgroup will be removed if | |
773 | * unnecessary. File cache pages is still on radix-tree. Don't | |
774 | * care it. | |
775 | */ | |
776 | if (!newpage->mapping) | |
777 | __mem_cgroup_uncharge_common(newpage, | |
778 | MEM_CGROUP_CHARGE_TYPE_FORCE); | |
779 | else if (PageAnon(newpage)) | |
780 | mem_cgroup_uncharge_page(newpage); | |
ae41be37 | 781 | } |
78fb7466 | 782 | |
cc847582 KH |
783 | /* |
784 | * This routine traverse page_cgroup in given list and drop them all. | |
cc847582 KH |
785 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
786 | */ | |
787 | #define FORCE_UNCHARGE_BATCH (128) | |
8869b8f6 | 788 | static void mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
072c56c1 KH |
789 | struct mem_cgroup_per_zone *mz, |
790 | int active) | |
cc847582 KH |
791 | { |
792 | struct page_cgroup *pc; | |
793 | struct page *page; | |
9b3c0a07 | 794 | int count = FORCE_UNCHARGE_BATCH; |
cc847582 | 795 | unsigned long flags; |
072c56c1 KH |
796 | struct list_head *list; |
797 | ||
798 | if (active) | |
799 | list = &mz->active_list; | |
800 | else | |
801 | list = &mz->inactive_list; | |
cc847582 | 802 | |
072c56c1 | 803 | spin_lock_irqsave(&mz->lru_lock, flags); |
9b3c0a07 | 804 | while (!list_empty(list)) { |
cc847582 KH |
805 | pc = list_entry(list->prev, struct page_cgroup, lru); |
806 | page = pc->page; | |
9b3c0a07 HT |
807 | get_page(page); |
808 | spin_unlock_irqrestore(&mz->lru_lock, flags); | |
e8589cc1 KH |
809 | /* |
810 | * Check if this page is on LRU. !LRU page can be found | |
811 | * if it's under page migration. | |
812 | */ | |
813 | if (PageLRU(page)) { | |
69029cd5 KH |
814 | __mem_cgroup_uncharge_common(page, |
815 | MEM_CGROUP_CHARGE_TYPE_FORCE); | |
e8589cc1 KH |
816 | put_page(page); |
817 | if (--count <= 0) { | |
818 | count = FORCE_UNCHARGE_BATCH; | |
819 | cond_resched(); | |
820 | } | |
821 | } else | |
9b3c0a07 | 822 | cond_resched(); |
9b3c0a07 | 823 | spin_lock_irqsave(&mz->lru_lock, flags); |
cc847582 | 824 | } |
072c56c1 | 825 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
cc847582 KH |
826 | } |
827 | ||
828 | /* | |
829 | * make mem_cgroup's charge to be 0 if there is no task. | |
830 | * This enables deleting this mem_cgroup. | |
831 | */ | |
d5b69e38 | 832 | static int mem_cgroup_force_empty(struct mem_cgroup *mem) |
cc847582 KH |
833 | { |
834 | int ret = -EBUSY; | |
1ecaab2b | 835 | int node, zid; |
8869b8f6 | 836 | |
4077960e BS |
837 | if (mem_cgroup_subsys.disabled) |
838 | return 0; | |
839 | ||
cc847582 KH |
840 | css_get(&mem->css); |
841 | /* | |
842 | * page reclaim code (kswapd etc..) will move pages between | |
8869b8f6 | 843 | * active_list <-> inactive_list while we don't take a lock. |
cc847582 KH |
844 | * So, we have to do loop here until all lists are empty. |
845 | */ | |
1ecaab2b | 846 | while (mem->res.usage > 0) { |
cc847582 KH |
847 | if (atomic_read(&mem->css.cgroup->count) > 0) |
848 | goto out; | |
1ecaab2b KH |
849 | for_each_node_state(node, N_POSSIBLE) |
850 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { | |
851 | struct mem_cgroup_per_zone *mz; | |
852 | mz = mem_cgroup_zoneinfo(mem, node, zid); | |
853 | /* drop all page_cgroup in active_list */ | |
072c56c1 | 854 | mem_cgroup_force_empty_list(mem, mz, 1); |
1ecaab2b | 855 | /* drop all page_cgroup in inactive_list */ |
072c56c1 | 856 | mem_cgroup_force_empty_list(mem, mz, 0); |
1ecaab2b | 857 | } |
cc847582 KH |
858 | } |
859 | ret = 0; | |
860 | out: | |
861 | css_put(&mem->css); | |
862 | return ret; | |
863 | } | |
864 | ||
2c3daa72 | 865 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
8cdea7c0 | 866 | { |
2c3daa72 PM |
867 | return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res, |
868 | cft->private); | |
8cdea7c0 BS |
869 | } |
870 | ||
856c13aa PM |
871 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
872 | const char *buffer) | |
8cdea7c0 BS |
873 | { |
874 | return res_counter_write(&mem_cgroup_from_cont(cont)->res, | |
856c13aa PM |
875 | cft->private, buffer, |
876 | res_counter_memparse_write_strategy); | |
8cdea7c0 BS |
877 | } |
878 | ||
29f2a4da | 879 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
c84872e1 PE |
880 | { |
881 | struct mem_cgroup *mem; | |
882 | ||
883 | mem = mem_cgroup_from_cont(cont); | |
29f2a4da PE |
884 | switch (event) { |
885 | case RES_MAX_USAGE: | |
886 | res_counter_reset_max(&mem->res); | |
887 | break; | |
888 | case RES_FAILCNT: | |
889 | res_counter_reset_failcnt(&mem->res); | |
890 | break; | |
891 | } | |
85cc59db | 892 | return 0; |
c84872e1 PE |
893 | } |
894 | ||
85cc59db | 895 | static int mem_force_empty_write(struct cgroup *cont, unsigned int event) |
cc847582 | 896 | { |
85cc59db | 897 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont)); |
cc847582 KH |
898 | } |
899 | ||
d2ceb9b7 KH |
900 | static const struct mem_cgroup_stat_desc { |
901 | const char *msg; | |
902 | u64 unit; | |
903 | } mem_cgroup_stat_desc[] = { | |
904 | [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, }, | |
905 | [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, }, | |
55e462b0 BR |
906 | [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, }, |
907 | [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, }, | |
d2ceb9b7 KH |
908 | }; |
909 | ||
c64745cf PM |
910 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
911 | struct cgroup_map_cb *cb) | |
d2ceb9b7 | 912 | { |
d2ceb9b7 KH |
913 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
914 | struct mem_cgroup_stat *stat = &mem_cont->stat; | |
915 | int i; | |
916 | ||
917 | for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) { | |
918 | s64 val; | |
919 | ||
920 | val = mem_cgroup_read_stat(stat, i); | |
921 | val *= mem_cgroup_stat_desc[i].unit; | |
c64745cf | 922 | cb->fill(cb, mem_cgroup_stat_desc[i].msg, val); |
d2ceb9b7 | 923 | } |
6d12e2d8 KH |
924 | /* showing # of active pages */ |
925 | { | |
926 | unsigned long active, inactive; | |
927 | ||
928 | inactive = mem_cgroup_get_all_zonestat(mem_cont, | |
929 | MEM_CGROUP_ZSTAT_INACTIVE); | |
930 | active = mem_cgroup_get_all_zonestat(mem_cont, | |
931 | MEM_CGROUP_ZSTAT_ACTIVE); | |
c64745cf PM |
932 | cb->fill(cb, "active", (active) * PAGE_SIZE); |
933 | cb->fill(cb, "inactive", (inactive) * PAGE_SIZE); | |
6d12e2d8 | 934 | } |
d2ceb9b7 KH |
935 | return 0; |
936 | } | |
937 | ||
8cdea7c0 BS |
938 | static struct cftype mem_cgroup_files[] = { |
939 | { | |
0eea1030 | 940 | .name = "usage_in_bytes", |
8cdea7c0 | 941 | .private = RES_USAGE, |
2c3daa72 | 942 | .read_u64 = mem_cgroup_read, |
8cdea7c0 | 943 | }, |
c84872e1 PE |
944 | { |
945 | .name = "max_usage_in_bytes", | |
946 | .private = RES_MAX_USAGE, | |
29f2a4da | 947 | .trigger = mem_cgroup_reset, |
c84872e1 PE |
948 | .read_u64 = mem_cgroup_read, |
949 | }, | |
8cdea7c0 | 950 | { |
0eea1030 | 951 | .name = "limit_in_bytes", |
8cdea7c0 | 952 | .private = RES_LIMIT, |
856c13aa | 953 | .write_string = mem_cgroup_write, |
2c3daa72 | 954 | .read_u64 = mem_cgroup_read, |
8cdea7c0 BS |
955 | }, |
956 | { | |
957 | .name = "failcnt", | |
958 | .private = RES_FAILCNT, | |
29f2a4da | 959 | .trigger = mem_cgroup_reset, |
2c3daa72 | 960 | .read_u64 = mem_cgroup_read, |
8cdea7c0 | 961 | }, |
cc847582 KH |
962 | { |
963 | .name = "force_empty", | |
85cc59db | 964 | .trigger = mem_force_empty_write, |
cc847582 | 965 | }, |
d2ceb9b7 KH |
966 | { |
967 | .name = "stat", | |
c64745cf | 968 | .read_map = mem_control_stat_show, |
d2ceb9b7 | 969 | }, |
8cdea7c0 BS |
970 | }; |
971 | ||
6d12e2d8 KH |
972 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
973 | { | |
974 | struct mem_cgroup_per_node *pn; | |
1ecaab2b | 975 | struct mem_cgroup_per_zone *mz; |
41e3355d | 976 | int zone, tmp = node; |
1ecaab2b KH |
977 | /* |
978 | * This routine is called against possible nodes. | |
979 | * But it's BUG to call kmalloc() against offline node. | |
980 | * | |
981 | * TODO: this routine can waste much memory for nodes which will | |
982 | * never be onlined. It's better to use memory hotplug callback | |
983 | * function. | |
984 | */ | |
41e3355d KH |
985 | if (!node_state(node, N_NORMAL_MEMORY)) |
986 | tmp = -1; | |
987 | pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp); | |
6d12e2d8 KH |
988 | if (!pn) |
989 | return 1; | |
1ecaab2b | 990 | |
6d12e2d8 KH |
991 | mem->info.nodeinfo[node] = pn; |
992 | memset(pn, 0, sizeof(*pn)); | |
1ecaab2b KH |
993 | |
994 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
995 | mz = &pn->zoneinfo[zone]; | |
996 | INIT_LIST_HEAD(&mz->active_list); | |
997 | INIT_LIST_HEAD(&mz->inactive_list); | |
072c56c1 | 998 | spin_lock_init(&mz->lru_lock); |
1ecaab2b | 999 | } |
6d12e2d8 KH |
1000 | return 0; |
1001 | } | |
1002 | ||
1ecaab2b KH |
1003 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
1004 | { | |
1005 | kfree(mem->info.nodeinfo[node]); | |
1006 | } | |
1007 | ||
33327948 KH |
1008 | static struct mem_cgroup *mem_cgroup_alloc(void) |
1009 | { | |
1010 | struct mem_cgroup *mem; | |
1011 | ||
1012 | if (sizeof(*mem) < PAGE_SIZE) | |
1013 | mem = kmalloc(sizeof(*mem), GFP_KERNEL); | |
1014 | else | |
1015 | mem = vmalloc(sizeof(*mem)); | |
1016 | ||
1017 | if (mem) | |
1018 | memset(mem, 0, sizeof(*mem)); | |
1019 | return mem; | |
1020 | } | |
1021 | ||
1022 | static void mem_cgroup_free(struct mem_cgroup *mem) | |
1023 | { | |
1024 | if (sizeof(*mem) < PAGE_SIZE) | |
1025 | kfree(mem); | |
1026 | else | |
1027 | vfree(mem); | |
1028 | } | |
1029 | ||
1030 | ||
8cdea7c0 BS |
1031 | static struct cgroup_subsys_state * |
1032 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) | |
1033 | { | |
1034 | struct mem_cgroup *mem; | |
6d12e2d8 | 1035 | int node; |
8cdea7c0 | 1036 | |
b6ac57d5 | 1037 | if (unlikely((cont->parent) == NULL)) { |
78fb7466 | 1038 | mem = &init_mem_cgroup; |
b6ac57d5 BS |
1039 | page_cgroup_cache = KMEM_CACHE(page_cgroup, SLAB_PANIC); |
1040 | } else { | |
33327948 KH |
1041 | mem = mem_cgroup_alloc(); |
1042 | if (!mem) | |
1043 | return ERR_PTR(-ENOMEM); | |
b6ac57d5 | 1044 | } |
78fb7466 | 1045 | |
8cdea7c0 | 1046 | res_counter_init(&mem->res); |
1ecaab2b | 1047 | |
6d12e2d8 KH |
1048 | for_each_node_state(node, N_POSSIBLE) |
1049 | if (alloc_mem_cgroup_per_zone_info(mem, node)) | |
1050 | goto free_out; | |
1051 | ||
8cdea7c0 | 1052 | return &mem->css; |
6d12e2d8 KH |
1053 | free_out: |
1054 | for_each_node_state(node, N_POSSIBLE) | |
1ecaab2b | 1055 | free_mem_cgroup_per_zone_info(mem, node); |
6d12e2d8 | 1056 | if (cont->parent != NULL) |
33327948 | 1057 | mem_cgroup_free(mem); |
2dda81ca | 1058 | return ERR_PTR(-ENOMEM); |
8cdea7c0 BS |
1059 | } |
1060 | ||
df878fb0 KH |
1061 | static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
1062 | struct cgroup *cont) | |
1063 | { | |
1064 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); | |
1065 | mem_cgroup_force_empty(mem); | |
1066 | } | |
1067 | ||
8cdea7c0 BS |
1068 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
1069 | struct cgroup *cont) | |
1070 | { | |
6d12e2d8 KH |
1071 | int node; |
1072 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); | |
1073 | ||
1074 | for_each_node_state(node, N_POSSIBLE) | |
1ecaab2b | 1075 | free_mem_cgroup_per_zone_info(mem, node); |
6d12e2d8 | 1076 | |
33327948 | 1077 | mem_cgroup_free(mem_cgroup_from_cont(cont)); |
8cdea7c0 BS |
1078 | } |
1079 | ||
1080 | static int mem_cgroup_populate(struct cgroup_subsys *ss, | |
1081 | struct cgroup *cont) | |
1082 | { | |
4077960e BS |
1083 | if (mem_cgroup_subsys.disabled) |
1084 | return 0; | |
8cdea7c0 BS |
1085 | return cgroup_add_files(cont, ss, mem_cgroup_files, |
1086 | ARRAY_SIZE(mem_cgroup_files)); | |
1087 | } | |
1088 | ||
67e465a7 BS |
1089 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
1090 | struct cgroup *cont, | |
1091 | struct cgroup *old_cont, | |
1092 | struct task_struct *p) | |
1093 | { | |
1094 | struct mm_struct *mm; | |
1095 | struct mem_cgroup *mem, *old_mem; | |
1096 | ||
4077960e BS |
1097 | if (mem_cgroup_subsys.disabled) |
1098 | return; | |
1099 | ||
67e465a7 BS |
1100 | mm = get_task_mm(p); |
1101 | if (mm == NULL) | |
1102 | return; | |
1103 | ||
1104 | mem = mem_cgroup_from_cont(cont); | |
1105 | old_mem = mem_cgroup_from_cont(old_cont); | |
1106 | ||
1107 | if (mem == old_mem) | |
1108 | goto out; | |
1109 | ||
1110 | /* | |
1111 | * Only thread group leaders are allowed to migrate, the mm_struct is | |
1112 | * in effect owned by the leader | |
1113 | */ | |
52ea27eb | 1114 | if (!thread_group_leader(p)) |
67e465a7 BS |
1115 | goto out; |
1116 | ||
67e465a7 BS |
1117 | out: |
1118 | mmput(mm); | |
67e465a7 BS |
1119 | } |
1120 | ||
8cdea7c0 BS |
1121 | struct cgroup_subsys mem_cgroup_subsys = { |
1122 | .name = "memory", | |
1123 | .subsys_id = mem_cgroup_subsys_id, | |
1124 | .create = mem_cgroup_create, | |
df878fb0 | 1125 | .pre_destroy = mem_cgroup_pre_destroy, |
8cdea7c0 BS |
1126 | .destroy = mem_cgroup_destroy, |
1127 | .populate = mem_cgroup_populate, | |
67e465a7 | 1128 | .attach = mem_cgroup_move_task, |
6d12e2d8 | 1129 | .early_init = 0, |
8cdea7c0 | 1130 | }; |