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