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