]>
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 | * | |
2e72b634 KS |
9 | * Memory thresholds |
10 | * Copyright (C) 2009 Nokia Corporation | |
11 | * Author: Kirill A. Shutemov | |
12 | * | |
8cdea7c0 BS |
13 | * This program is free software; you can redistribute it and/or modify |
14 | * it under the terms of the GNU General Public License as published by | |
15 | * the Free Software Foundation; either version 2 of the License, or | |
16 | * (at your option) any later version. | |
17 | * | |
18 | * This program is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | * GNU General Public License for more details. | |
22 | */ | |
23 | ||
24 | #include <linux/res_counter.h> | |
25 | #include <linux/memcontrol.h> | |
26 | #include <linux/cgroup.h> | |
78fb7466 | 27 | #include <linux/mm.h> |
4ffef5fe | 28 | #include <linux/hugetlb.h> |
d13d1443 | 29 | #include <linux/pagemap.h> |
d52aa412 | 30 | #include <linux/smp.h> |
8a9f3ccd | 31 | #include <linux/page-flags.h> |
66e1707b | 32 | #include <linux/backing-dev.h> |
8a9f3ccd BS |
33 | #include <linux/bit_spinlock.h> |
34 | #include <linux/rcupdate.h> | |
e222432b | 35 | #include <linux/limits.h> |
8c7c6e34 | 36 | #include <linux/mutex.h> |
f64c3f54 | 37 | #include <linux/rbtree.h> |
b6ac57d5 | 38 | #include <linux/slab.h> |
66e1707b | 39 | #include <linux/swap.h> |
02491447 | 40 | #include <linux/swapops.h> |
66e1707b | 41 | #include <linux/spinlock.h> |
2e72b634 KS |
42 | #include <linux/eventfd.h> |
43 | #include <linux/sort.h> | |
66e1707b | 44 | #include <linux/fs.h> |
d2ceb9b7 | 45 | #include <linux/seq_file.h> |
33327948 | 46 | #include <linux/vmalloc.h> |
b69408e8 | 47 | #include <linux/mm_inline.h> |
52d4b9ac | 48 | #include <linux/page_cgroup.h> |
cdec2e42 | 49 | #include <linux/cpu.h> |
08e552c6 | 50 | #include "internal.h" |
8cdea7c0 | 51 | |
8697d331 BS |
52 | #include <asm/uaccess.h> |
53 | ||
a181b0e8 | 54 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
a181b0e8 | 55 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
4b3bde4c | 56 | struct mem_cgroup *root_mem_cgroup __read_mostly; |
8cdea7c0 | 57 | |
c077719b | 58 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
338c8431 | 59 | /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */ |
c077719b KH |
60 | int do_swap_account __read_mostly; |
61 | static int really_do_swap_account __initdata = 1; /* for remember boot option*/ | |
62 | #else | |
63 | #define do_swap_account (0) | |
64 | #endif | |
65 | ||
f64c3f54 | 66 | #define SOFTLIMIT_EVENTS_THRESH (1000) |
2e72b634 | 67 | #define THRESHOLDS_EVENTS_THRESH (100) |
c077719b | 68 | |
d52aa412 KH |
69 | /* |
70 | * Statistics for memory cgroup. | |
71 | */ | |
72 | enum mem_cgroup_stat_index { | |
73 | /* | |
74 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. | |
75 | */ | |
76 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ | |
d69b042f | 77 | MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */ |
d8046582 | 78 | MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */ |
55e462b0 BR |
79 | MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */ |
80 | MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */ | |
0c3e73e8 | 81 | MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */ |
378ce724 KS |
82 | MEM_CGROUP_STAT_SOFTLIMIT, /* decrements on each page in/out. |
83 | used by soft limit implementation */ | |
2e72b634 KS |
84 | MEM_CGROUP_STAT_THRESHOLDS, /* decrements on each page in/out. |
85 | used by threshold implementation */ | |
d52aa412 KH |
86 | |
87 | MEM_CGROUP_STAT_NSTATS, | |
88 | }; | |
89 | ||
90 | struct mem_cgroup_stat_cpu { | |
91 | s64 count[MEM_CGROUP_STAT_NSTATS]; | |
92 | } ____cacheline_aligned_in_smp; | |
93 | ||
94 | struct mem_cgroup_stat { | |
c8dad2bb | 95 | struct mem_cgroup_stat_cpu cpustat[0]; |
d52aa412 KH |
96 | }; |
97 | ||
f64c3f54 | 98 | static inline void |
378ce724 KS |
99 | __mem_cgroup_stat_set_safe(struct mem_cgroup_stat_cpu *stat, |
100 | enum mem_cgroup_stat_index idx, s64 val) | |
f64c3f54 | 101 | { |
378ce724 | 102 | stat->count[idx] = val; |
f64c3f54 BS |
103 | } |
104 | ||
105 | static inline s64 | |
106 | __mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat, | |
107 | enum mem_cgroup_stat_index idx) | |
108 | { | |
109 | return stat->count[idx]; | |
110 | } | |
111 | ||
d52aa412 KH |
112 | /* |
113 | * For accounting under irq disable, no need for increment preempt count. | |
114 | */ | |
addb9efe | 115 | static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat, |
d52aa412 KH |
116 | enum mem_cgroup_stat_index idx, int val) |
117 | { | |
addb9efe | 118 | stat->count[idx] += val; |
d52aa412 KH |
119 | } |
120 | ||
121 | static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat, | |
122 | enum mem_cgroup_stat_index idx) | |
123 | { | |
124 | int cpu; | |
125 | s64 ret = 0; | |
126 | for_each_possible_cpu(cpu) | |
127 | ret += stat->cpustat[cpu].count[idx]; | |
128 | return ret; | |
129 | } | |
130 | ||
04046e1a KH |
131 | static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat) |
132 | { | |
133 | s64 ret; | |
134 | ||
135 | ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE); | |
136 | ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS); | |
137 | return ret; | |
138 | } | |
139 | ||
6d12e2d8 KH |
140 | /* |
141 | * per-zone information in memory controller. | |
142 | */ | |
6d12e2d8 | 143 | struct mem_cgroup_per_zone { |
072c56c1 KH |
144 | /* |
145 | * spin_lock to protect the per cgroup LRU | |
146 | */ | |
b69408e8 CL |
147 | struct list_head lists[NR_LRU_LISTS]; |
148 | unsigned long count[NR_LRU_LISTS]; | |
3e2f41f1 KM |
149 | |
150 | struct zone_reclaim_stat reclaim_stat; | |
f64c3f54 BS |
151 | struct rb_node tree_node; /* RB tree node */ |
152 | unsigned long long usage_in_excess;/* Set to the value by which */ | |
153 | /* the soft limit is exceeded*/ | |
154 | bool on_tree; | |
4e416953 BS |
155 | struct mem_cgroup *mem; /* Back pointer, we cannot */ |
156 | /* use container_of */ | |
6d12e2d8 KH |
157 | }; |
158 | /* Macro for accessing counter */ | |
159 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) | |
160 | ||
161 | struct mem_cgroup_per_node { | |
162 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; | |
163 | }; | |
164 | ||
165 | struct mem_cgroup_lru_info { | |
166 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; | |
167 | }; | |
168 | ||
f64c3f54 BS |
169 | /* |
170 | * Cgroups above their limits are maintained in a RB-Tree, independent of | |
171 | * their hierarchy representation | |
172 | */ | |
173 | ||
174 | struct mem_cgroup_tree_per_zone { | |
175 | struct rb_root rb_root; | |
176 | spinlock_t lock; | |
177 | }; | |
178 | ||
179 | struct mem_cgroup_tree_per_node { | |
180 | struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES]; | |
181 | }; | |
182 | ||
183 | struct mem_cgroup_tree { | |
184 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; | |
185 | }; | |
186 | ||
187 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; | |
188 | ||
2e72b634 KS |
189 | struct mem_cgroup_threshold { |
190 | struct eventfd_ctx *eventfd; | |
191 | u64 threshold; | |
192 | }; | |
193 | ||
194 | struct mem_cgroup_threshold_ary { | |
195 | /* An array index points to threshold just below usage. */ | |
196 | atomic_t current_threshold; | |
197 | /* Size of entries[] */ | |
198 | unsigned int size; | |
199 | /* Array of thresholds */ | |
200 | struct mem_cgroup_threshold entries[0]; | |
201 | }; | |
202 | ||
203 | static bool mem_cgroup_threshold_check(struct mem_cgroup *mem); | |
204 | static void mem_cgroup_threshold(struct mem_cgroup *mem); | |
205 | ||
8cdea7c0 BS |
206 | /* |
207 | * The memory controller data structure. The memory controller controls both | |
208 | * page cache and RSS per cgroup. We would eventually like to provide | |
209 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, | |
210 | * to help the administrator determine what knobs to tune. | |
211 | * | |
212 | * TODO: Add a water mark for the memory controller. Reclaim will begin when | |
8a9f3ccd BS |
213 | * we hit the water mark. May be even add a low water mark, such that |
214 | * no reclaim occurs from a cgroup at it's low water mark, this is | |
215 | * a feature that will be implemented much later in the future. | |
8cdea7c0 BS |
216 | */ |
217 | struct mem_cgroup { | |
218 | struct cgroup_subsys_state css; | |
219 | /* | |
220 | * the counter to account for memory usage | |
221 | */ | |
222 | struct res_counter res; | |
8c7c6e34 KH |
223 | /* |
224 | * the counter to account for mem+swap usage. | |
225 | */ | |
226 | struct res_counter memsw; | |
78fb7466 PE |
227 | /* |
228 | * Per cgroup active and inactive list, similar to the | |
229 | * per zone LRU lists. | |
78fb7466 | 230 | */ |
6d12e2d8 | 231 | struct mem_cgroup_lru_info info; |
072c56c1 | 232 | |
2733c06a KM |
233 | /* |
234 | protect against reclaim related member. | |
235 | */ | |
236 | spinlock_t reclaim_param_lock; | |
237 | ||
6c48a1d0 | 238 | int prev_priority; /* for recording reclaim priority */ |
6d61ef40 BS |
239 | |
240 | /* | |
af901ca1 | 241 | * While reclaiming in a hierarchy, we cache the last child we |
04046e1a | 242 | * reclaimed from. |
6d61ef40 | 243 | */ |
04046e1a | 244 | int last_scanned_child; |
18f59ea7 BS |
245 | /* |
246 | * Should the accounting and control be hierarchical, per subtree? | |
247 | */ | |
248 | bool use_hierarchy; | |
a636b327 | 249 | unsigned long last_oom_jiffies; |
8c7c6e34 | 250 | atomic_t refcnt; |
14797e23 | 251 | |
a7885eb8 KM |
252 | unsigned int swappiness; |
253 | ||
22a668d7 KH |
254 | /* set when res.limit == memsw.limit */ |
255 | bool memsw_is_minimum; | |
256 | ||
2e72b634 KS |
257 | /* protect arrays of thresholds */ |
258 | struct mutex thresholds_lock; | |
259 | ||
260 | /* thresholds for memory usage. RCU-protected */ | |
261 | struct mem_cgroup_threshold_ary *thresholds; | |
262 | ||
263 | /* thresholds for mem+swap usage. RCU-protected */ | |
264 | struct mem_cgroup_threshold_ary *memsw_thresholds; | |
265 | ||
7dc74be0 DN |
266 | /* |
267 | * Should we move charges of a task when a task is moved into this | |
268 | * mem_cgroup ? And what type of charges should we move ? | |
269 | */ | |
270 | unsigned long move_charge_at_immigrate; | |
271 | ||
d52aa412 | 272 | /* |
c8dad2bb | 273 | * statistics. This must be placed at the end of memcg. |
d52aa412 KH |
274 | */ |
275 | struct mem_cgroup_stat stat; | |
8cdea7c0 BS |
276 | }; |
277 | ||
7dc74be0 DN |
278 | /* Stuffs for move charges at task migration. */ |
279 | /* | |
280 | * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a | |
281 | * left-shifted bitmap of these types. | |
282 | */ | |
283 | enum move_type { | |
4ffef5fe | 284 | MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */ |
7dc74be0 DN |
285 | NR_MOVE_TYPE, |
286 | }; | |
287 | ||
4ffef5fe DN |
288 | /* "mc" and its members are protected by cgroup_mutex */ |
289 | static struct move_charge_struct { | |
290 | struct mem_cgroup *from; | |
291 | struct mem_cgroup *to; | |
292 | unsigned long precharge; | |
854ffa8d | 293 | unsigned long moved_charge; |
483c30b5 | 294 | unsigned long moved_swap; |
8033b97c DN |
295 | struct task_struct *moving_task; /* a task moving charges */ |
296 | wait_queue_head_t waitq; /* a waitq for other context */ | |
297 | } mc = { | |
298 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), | |
299 | }; | |
4ffef5fe | 300 | |
4e416953 BS |
301 | /* |
302 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft | |
303 | * limit reclaim to prevent infinite loops, if they ever occur. | |
304 | */ | |
305 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100) | |
306 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2) | |
307 | ||
217bc319 KH |
308 | enum charge_type { |
309 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, | |
310 | MEM_CGROUP_CHARGE_TYPE_MAPPED, | |
4f98a2fe | 311 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
c05555b5 | 312 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
d13d1443 | 313 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
8a9478ca | 314 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
c05555b5 KH |
315 | NR_CHARGE_TYPE, |
316 | }; | |
317 | ||
52d4b9ac KH |
318 | /* only for here (for easy reading.) */ |
319 | #define PCGF_CACHE (1UL << PCG_CACHE) | |
320 | #define PCGF_USED (1UL << PCG_USED) | |
52d4b9ac | 321 | #define PCGF_LOCK (1UL << PCG_LOCK) |
4b3bde4c BS |
322 | /* Not used, but added here for completeness */ |
323 | #define PCGF_ACCT (1UL << PCG_ACCT) | |
217bc319 | 324 | |
8c7c6e34 KH |
325 | /* for encoding cft->private value on file */ |
326 | #define _MEM (0) | |
327 | #define _MEMSWAP (1) | |
328 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) | |
329 | #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff) | |
330 | #define MEMFILE_ATTR(val) ((val) & 0xffff) | |
331 | ||
75822b44 BS |
332 | /* |
333 | * Reclaim flags for mem_cgroup_hierarchical_reclaim | |
334 | */ | |
335 | #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0 | |
336 | #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT) | |
337 | #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1 | |
338 | #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT) | |
4e416953 BS |
339 | #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2 |
340 | #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT) | |
75822b44 | 341 | |
8c7c6e34 KH |
342 | static void mem_cgroup_get(struct mem_cgroup *mem); |
343 | static void mem_cgroup_put(struct mem_cgroup *mem); | |
7bcc1bb1 | 344 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem); |
cdec2e42 | 345 | static void drain_all_stock_async(void); |
8c7c6e34 | 346 | |
f64c3f54 BS |
347 | static struct mem_cgroup_per_zone * |
348 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) | |
349 | { | |
350 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; | |
351 | } | |
352 | ||
d324236b WF |
353 | struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem) |
354 | { | |
355 | return &mem->css; | |
356 | } | |
357 | ||
f64c3f54 BS |
358 | static struct mem_cgroup_per_zone * |
359 | page_cgroup_zoneinfo(struct page_cgroup *pc) | |
360 | { | |
361 | struct mem_cgroup *mem = pc->mem_cgroup; | |
362 | int nid = page_cgroup_nid(pc); | |
363 | int zid = page_cgroup_zid(pc); | |
364 | ||
365 | if (!mem) | |
366 | return NULL; | |
367 | ||
368 | return mem_cgroup_zoneinfo(mem, nid, zid); | |
369 | } | |
370 | ||
371 | static struct mem_cgroup_tree_per_zone * | |
372 | soft_limit_tree_node_zone(int nid, int zid) | |
373 | { | |
374 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; | |
375 | } | |
376 | ||
377 | static struct mem_cgroup_tree_per_zone * | |
378 | soft_limit_tree_from_page(struct page *page) | |
379 | { | |
380 | int nid = page_to_nid(page); | |
381 | int zid = page_zonenum(page); | |
382 | ||
383 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; | |
384 | } | |
385 | ||
386 | static void | |
4e416953 | 387 | __mem_cgroup_insert_exceeded(struct mem_cgroup *mem, |
f64c3f54 | 388 | struct mem_cgroup_per_zone *mz, |
ef8745c1 KH |
389 | struct mem_cgroup_tree_per_zone *mctz, |
390 | unsigned long long new_usage_in_excess) | |
f64c3f54 BS |
391 | { |
392 | struct rb_node **p = &mctz->rb_root.rb_node; | |
393 | struct rb_node *parent = NULL; | |
394 | struct mem_cgroup_per_zone *mz_node; | |
395 | ||
396 | if (mz->on_tree) | |
397 | return; | |
398 | ||
ef8745c1 KH |
399 | mz->usage_in_excess = new_usage_in_excess; |
400 | if (!mz->usage_in_excess) | |
401 | return; | |
f64c3f54 BS |
402 | while (*p) { |
403 | parent = *p; | |
404 | mz_node = rb_entry(parent, struct mem_cgroup_per_zone, | |
405 | tree_node); | |
406 | if (mz->usage_in_excess < mz_node->usage_in_excess) | |
407 | p = &(*p)->rb_left; | |
408 | /* | |
409 | * We can't avoid mem cgroups that are over their soft | |
410 | * limit by the same amount | |
411 | */ | |
412 | else if (mz->usage_in_excess >= mz_node->usage_in_excess) | |
413 | p = &(*p)->rb_right; | |
414 | } | |
415 | rb_link_node(&mz->tree_node, parent, p); | |
416 | rb_insert_color(&mz->tree_node, &mctz->rb_root); | |
417 | mz->on_tree = true; | |
4e416953 BS |
418 | } |
419 | ||
420 | static void | |
421 | __mem_cgroup_remove_exceeded(struct mem_cgroup *mem, | |
422 | struct mem_cgroup_per_zone *mz, | |
423 | struct mem_cgroup_tree_per_zone *mctz) | |
424 | { | |
425 | if (!mz->on_tree) | |
426 | return; | |
427 | rb_erase(&mz->tree_node, &mctz->rb_root); | |
428 | mz->on_tree = false; | |
429 | } | |
430 | ||
f64c3f54 BS |
431 | static void |
432 | mem_cgroup_remove_exceeded(struct mem_cgroup *mem, | |
433 | struct mem_cgroup_per_zone *mz, | |
434 | struct mem_cgroup_tree_per_zone *mctz) | |
435 | { | |
436 | spin_lock(&mctz->lock); | |
4e416953 | 437 | __mem_cgroup_remove_exceeded(mem, mz, mctz); |
f64c3f54 BS |
438 | spin_unlock(&mctz->lock); |
439 | } | |
440 | ||
441 | static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem) | |
442 | { | |
443 | bool ret = false; | |
444 | int cpu; | |
445 | s64 val; | |
446 | struct mem_cgroup_stat_cpu *cpustat; | |
447 | ||
448 | cpu = get_cpu(); | |
449 | cpustat = &mem->stat.cpustat[cpu]; | |
378ce724 KS |
450 | val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_SOFTLIMIT); |
451 | if (unlikely(val < 0)) { | |
452 | __mem_cgroup_stat_set_safe(cpustat, MEM_CGROUP_STAT_SOFTLIMIT, | |
453 | SOFTLIMIT_EVENTS_THRESH); | |
f64c3f54 BS |
454 | ret = true; |
455 | } | |
456 | put_cpu(); | |
457 | return ret; | |
458 | } | |
459 | ||
460 | static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page) | |
461 | { | |
ef8745c1 | 462 | unsigned long long excess; |
f64c3f54 BS |
463 | struct mem_cgroup_per_zone *mz; |
464 | struct mem_cgroup_tree_per_zone *mctz; | |
4e649152 KH |
465 | int nid = page_to_nid(page); |
466 | int zid = page_zonenum(page); | |
f64c3f54 BS |
467 | mctz = soft_limit_tree_from_page(page); |
468 | ||
469 | /* | |
4e649152 KH |
470 | * Necessary to update all ancestors when hierarchy is used. |
471 | * because their event counter is not touched. | |
f64c3f54 | 472 | */ |
4e649152 KH |
473 | for (; mem; mem = parent_mem_cgroup(mem)) { |
474 | mz = mem_cgroup_zoneinfo(mem, nid, zid); | |
ef8745c1 | 475 | excess = res_counter_soft_limit_excess(&mem->res); |
4e649152 KH |
476 | /* |
477 | * We have to update the tree if mz is on RB-tree or | |
478 | * mem is over its softlimit. | |
479 | */ | |
ef8745c1 | 480 | if (excess || mz->on_tree) { |
4e649152 KH |
481 | spin_lock(&mctz->lock); |
482 | /* if on-tree, remove it */ | |
483 | if (mz->on_tree) | |
484 | __mem_cgroup_remove_exceeded(mem, mz, mctz); | |
485 | /* | |
ef8745c1 KH |
486 | * Insert again. mz->usage_in_excess will be updated. |
487 | * If excess is 0, no tree ops. | |
4e649152 | 488 | */ |
ef8745c1 | 489 | __mem_cgroup_insert_exceeded(mem, mz, mctz, excess); |
4e649152 KH |
490 | spin_unlock(&mctz->lock); |
491 | } | |
f64c3f54 BS |
492 | } |
493 | } | |
494 | ||
495 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem) | |
496 | { | |
497 | int node, zone; | |
498 | struct mem_cgroup_per_zone *mz; | |
499 | struct mem_cgroup_tree_per_zone *mctz; | |
500 | ||
501 | for_each_node_state(node, N_POSSIBLE) { | |
502 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
503 | mz = mem_cgroup_zoneinfo(mem, node, zone); | |
504 | mctz = soft_limit_tree_node_zone(node, zone); | |
505 | mem_cgroup_remove_exceeded(mem, mz, mctz); | |
506 | } | |
507 | } | |
508 | } | |
509 | ||
4e416953 BS |
510 | static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem) |
511 | { | |
512 | return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT; | |
513 | } | |
514 | ||
515 | static struct mem_cgroup_per_zone * | |
516 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) | |
517 | { | |
518 | struct rb_node *rightmost = NULL; | |
26251eaf | 519 | struct mem_cgroup_per_zone *mz; |
4e416953 BS |
520 | |
521 | retry: | |
26251eaf | 522 | mz = NULL; |
4e416953 BS |
523 | rightmost = rb_last(&mctz->rb_root); |
524 | if (!rightmost) | |
525 | goto done; /* Nothing to reclaim from */ | |
526 | ||
527 | mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node); | |
528 | /* | |
529 | * Remove the node now but someone else can add it back, | |
530 | * we will to add it back at the end of reclaim to its correct | |
531 | * position in the tree. | |
532 | */ | |
533 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); | |
534 | if (!res_counter_soft_limit_excess(&mz->mem->res) || | |
535 | !css_tryget(&mz->mem->css)) | |
536 | goto retry; | |
537 | done: | |
538 | return mz; | |
539 | } | |
540 | ||
541 | static struct mem_cgroup_per_zone * | |
542 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) | |
543 | { | |
544 | struct mem_cgroup_per_zone *mz; | |
545 | ||
546 | spin_lock(&mctz->lock); | |
547 | mz = __mem_cgroup_largest_soft_limit_node(mctz); | |
548 | spin_unlock(&mctz->lock); | |
549 | return mz; | |
550 | } | |
551 | ||
0c3e73e8 BS |
552 | static void mem_cgroup_swap_statistics(struct mem_cgroup *mem, |
553 | bool charge) | |
554 | { | |
555 | int val = (charge) ? 1 : -1; | |
556 | struct mem_cgroup_stat *stat = &mem->stat; | |
557 | struct mem_cgroup_stat_cpu *cpustat; | |
558 | int cpu = get_cpu(); | |
559 | ||
560 | cpustat = &stat->cpustat[cpu]; | |
561 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val); | |
562 | put_cpu(); | |
563 | } | |
564 | ||
c05555b5 KH |
565 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, |
566 | struct page_cgroup *pc, | |
567 | bool charge) | |
d52aa412 | 568 | { |
0c3e73e8 | 569 | int val = (charge) ? 1 : -1; |
d52aa412 | 570 | struct mem_cgroup_stat *stat = &mem->stat; |
addb9efe | 571 | struct mem_cgroup_stat_cpu *cpustat; |
08e552c6 | 572 | int cpu = get_cpu(); |
d52aa412 | 573 | |
08e552c6 | 574 | cpustat = &stat->cpustat[cpu]; |
c05555b5 | 575 | if (PageCgroupCache(pc)) |
addb9efe | 576 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val); |
d52aa412 | 577 | else |
addb9efe | 578 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val); |
55e462b0 BR |
579 | |
580 | if (charge) | |
addb9efe | 581 | __mem_cgroup_stat_add_safe(cpustat, |
55e462b0 BR |
582 | MEM_CGROUP_STAT_PGPGIN_COUNT, 1); |
583 | else | |
addb9efe | 584 | __mem_cgroup_stat_add_safe(cpustat, |
55e462b0 | 585 | MEM_CGROUP_STAT_PGPGOUT_COUNT, 1); |
378ce724 | 586 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SOFTLIMIT, -1); |
2e72b634 KS |
587 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_THRESHOLDS, -1); |
588 | ||
08e552c6 | 589 | put_cpu(); |
6d12e2d8 KH |
590 | } |
591 | ||
14067bb3 | 592 | static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem, |
b69408e8 | 593 | enum lru_list idx) |
6d12e2d8 KH |
594 | { |
595 | int nid, zid; | |
596 | struct mem_cgroup_per_zone *mz; | |
597 | u64 total = 0; | |
598 | ||
599 | for_each_online_node(nid) | |
600 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { | |
601 | mz = mem_cgroup_zoneinfo(mem, nid, zid); | |
602 | total += MEM_CGROUP_ZSTAT(mz, idx); | |
603 | } | |
604 | return total; | |
d52aa412 KH |
605 | } |
606 | ||
d5b69e38 | 607 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
8cdea7c0 BS |
608 | { |
609 | return container_of(cgroup_subsys_state(cont, | |
610 | mem_cgroup_subsys_id), struct mem_cgroup, | |
611 | css); | |
612 | } | |
613 | ||
cf475ad2 | 614 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
78fb7466 | 615 | { |
31a78f23 BS |
616 | /* |
617 | * mm_update_next_owner() may clear mm->owner to NULL | |
618 | * if it races with swapoff, page migration, etc. | |
619 | * So this can be called with p == NULL. | |
620 | */ | |
621 | if (unlikely(!p)) | |
622 | return NULL; | |
623 | ||
78fb7466 PE |
624 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
625 | struct mem_cgroup, css); | |
626 | } | |
627 | ||
54595fe2 KH |
628 | static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) |
629 | { | |
630 | struct mem_cgroup *mem = NULL; | |
0b7f569e KH |
631 | |
632 | if (!mm) | |
633 | return NULL; | |
54595fe2 KH |
634 | /* |
635 | * Because we have no locks, mm->owner's may be being moved to other | |
636 | * cgroup. We use css_tryget() here even if this looks | |
637 | * pessimistic (rather than adding locks here). | |
638 | */ | |
639 | rcu_read_lock(); | |
640 | do { | |
641 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); | |
642 | if (unlikely(!mem)) | |
643 | break; | |
644 | } while (!css_tryget(&mem->css)); | |
645 | rcu_read_unlock(); | |
646 | return mem; | |
647 | } | |
648 | ||
14067bb3 KH |
649 | /* |
650 | * Call callback function against all cgroup under hierarchy tree. | |
651 | */ | |
652 | static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data, | |
653 | int (*func)(struct mem_cgroup *, void *)) | |
654 | { | |
655 | int found, ret, nextid; | |
656 | struct cgroup_subsys_state *css; | |
657 | struct mem_cgroup *mem; | |
658 | ||
659 | if (!root->use_hierarchy) | |
660 | return (*func)(root, data); | |
661 | ||
662 | nextid = 1; | |
663 | do { | |
664 | ret = 0; | |
665 | mem = NULL; | |
666 | ||
667 | rcu_read_lock(); | |
668 | css = css_get_next(&mem_cgroup_subsys, nextid, &root->css, | |
669 | &found); | |
670 | if (css && css_tryget(css)) | |
671 | mem = container_of(css, struct mem_cgroup, css); | |
672 | rcu_read_unlock(); | |
673 | ||
674 | if (mem) { | |
675 | ret = (*func)(mem, data); | |
676 | css_put(&mem->css); | |
677 | } | |
678 | nextid = found + 1; | |
679 | } while (!ret && css); | |
680 | ||
681 | return ret; | |
682 | } | |
683 | ||
4b3bde4c BS |
684 | static inline bool mem_cgroup_is_root(struct mem_cgroup *mem) |
685 | { | |
686 | return (mem == root_mem_cgroup); | |
687 | } | |
688 | ||
08e552c6 KH |
689 | /* |
690 | * Following LRU functions are allowed to be used without PCG_LOCK. | |
691 | * Operations are called by routine of global LRU independently from memcg. | |
692 | * What we have to take care of here is validness of pc->mem_cgroup. | |
693 | * | |
694 | * Changes to pc->mem_cgroup happens when | |
695 | * 1. charge | |
696 | * 2. moving account | |
697 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. | |
698 | * It is added to LRU before charge. | |
699 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. | |
700 | * When moving account, the page is not on LRU. It's isolated. | |
701 | */ | |
4f98a2fe | 702 | |
08e552c6 KH |
703 | void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru) |
704 | { | |
705 | struct page_cgroup *pc; | |
08e552c6 | 706 | struct mem_cgroup_per_zone *mz; |
6d12e2d8 | 707 | |
f8d66542 | 708 | if (mem_cgroup_disabled()) |
08e552c6 KH |
709 | return; |
710 | pc = lookup_page_cgroup(page); | |
711 | /* can happen while we handle swapcache. */ | |
4b3bde4c | 712 | if (!TestClearPageCgroupAcctLRU(pc)) |
08e552c6 | 713 | return; |
4b3bde4c | 714 | VM_BUG_ON(!pc->mem_cgroup); |
544122e5 KH |
715 | /* |
716 | * We don't check PCG_USED bit. It's cleared when the "page" is finally | |
717 | * removed from global LRU. | |
718 | */ | |
08e552c6 | 719 | mz = page_cgroup_zoneinfo(pc); |
b69408e8 | 720 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
4b3bde4c BS |
721 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
722 | return; | |
723 | VM_BUG_ON(list_empty(&pc->lru)); | |
08e552c6 KH |
724 | list_del_init(&pc->lru); |
725 | return; | |
6d12e2d8 KH |
726 | } |
727 | ||
08e552c6 | 728 | void mem_cgroup_del_lru(struct page *page) |
6d12e2d8 | 729 | { |
08e552c6 KH |
730 | mem_cgroup_del_lru_list(page, page_lru(page)); |
731 | } | |
b69408e8 | 732 | |
08e552c6 KH |
733 | void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru) |
734 | { | |
735 | struct mem_cgroup_per_zone *mz; | |
736 | struct page_cgroup *pc; | |
b69408e8 | 737 | |
f8d66542 | 738 | if (mem_cgroup_disabled()) |
08e552c6 | 739 | return; |
6d12e2d8 | 740 | |
08e552c6 | 741 | pc = lookup_page_cgroup(page); |
bd112db8 DN |
742 | /* |
743 | * Used bit is set without atomic ops but after smp_wmb(). | |
744 | * For making pc->mem_cgroup visible, insert smp_rmb() here. | |
745 | */ | |
08e552c6 | 746 | smp_rmb(); |
4b3bde4c BS |
747 | /* unused or root page is not rotated. */ |
748 | if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup)) | |
08e552c6 KH |
749 | return; |
750 | mz = page_cgroup_zoneinfo(pc); | |
751 | list_move(&pc->lru, &mz->lists[lru]); | |
6d12e2d8 KH |
752 | } |
753 | ||
08e552c6 | 754 | void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru) |
66e1707b | 755 | { |
08e552c6 KH |
756 | struct page_cgroup *pc; |
757 | struct mem_cgroup_per_zone *mz; | |
6d12e2d8 | 758 | |
f8d66542 | 759 | if (mem_cgroup_disabled()) |
08e552c6 KH |
760 | return; |
761 | pc = lookup_page_cgroup(page); | |
4b3bde4c | 762 | VM_BUG_ON(PageCgroupAcctLRU(pc)); |
bd112db8 DN |
763 | /* |
764 | * Used bit is set without atomic ops but after smp_wmb(). | |
765 | * For making pc->mem_cgroup visible, insert smp_rmb() here. | |
766 | */ | |
08e552c6 KH |
767 | smp_rmb(); |
768 | if (!PageCgroupUsed(pc)) | |
894bc310 | 769 | return; |
b69408e8 | 770 | |
08e552c6 | 771 | mz = page_cgroup_zoneinfo(pc); |
b69408e8 | 772 | MEM_CGROUP_ZSTAT(mz, lru) += 1; |
4b3bde4c BS |
773 | SetPageCgroupAcctLRU(pc); |
774 | if (mem_cgroup_is_root(pc->mem_cgroup)) | |
775 | return; | |
08e552c6 KH |
776 | list_add(&pc->lru, &mz->lists[lru]); |
777 | } | |
544122e5 | 778 | |
08e552c6 | 779 | /* |
544122e5 KH |
780 | * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to |
781 | * lru because the page may.be reused after it's fully uncharged (because of | |
782 | * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge | |
783 | * it again. This function is only used to charge SwapCache. It's done under | |
784 | * lock_page and expected that zone->lru_lock is never held. | |
08e552c6 | 785 | */ |
544122e5 | 786 | static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page) |
08e552c6 | 787 | { |
544122e5 KH |
788 | unsigned long flags; |
789 | struct zone *zone = page_zone(page); | |
790 | struct page_cgroup *pc = lookup_page_cgroup(page); | |
791 | ||
792 | spin_lock_irqsave(&zone->lru_lock, flags); | |
793 | /* | |
794 | * Forget old LRU when this page_cgroup is *not* used. This Used bit | |
795 | * is guarded by lock_page() because the page is SwapCache. | |
796 | */ | |
797 | if (!PageCgroupUsed(pc)) | |
798 | mem_cgroup_del_lru_list(page, page_lru(page)); | |
799 | spin_unlock_irqrestore(&zone->lru_lock, flags); | |
08e552c6 KH |
800 | } |
801 | ||
544122e5 KH |
802 | static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page) |
803 | { | |
804 | unsigned long flags; | |
805 | struct zone *zone = page_zone(page); | |
806 | struct page_cgroup *pc = lookup_page_cgroup(page); | |
807 | ||
808 | spin_lock_irqsave(&zone->lru_lock, flags); | |
809 | /* link when the page is linked to LRU but page_cgroup isn't */ | |
4b3bde4c | 810 | if (PageLRU(page) && !PageCgroupAcctLRU(pc)) |
544122e5 KH |
811 | mem_cgroup_add_lru_list(page, page_lru(page)); |
812 | spin_unlock_irqrestore(&zone->lru_lock, flags); | |
813 | } | |
814 | ||
815 | ||
08e552c6 KH |
816 | void mem_cgroup_move_lists(struct page *page, |
817 | enum lru_list from, enum lru_list to) | |
818 | { | |
f8d66542 | 819 | if (mem_cgroup_disabled()) |
08e552c6 KH |
820 | return; |
821 | mem_cgroup_del_lru_list(page, from); | |
822 | mem_cgroup_add_lru_list(page, to); | |
66e1707b BS |
823 | } |
824 | ||
4c4a2214 DR |
825 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
826 | { | |
827 | int ret; | |
0b7f569e | 828 | struct mem_cgroup *curr = NULL; |
4c4a2214 DR |
829 | |
830 | task_lock(task); | |
0b7f569e KH |
831 | rcu_read_lock(); |
832 | curr = try_get_mem_cgroup_from_mm(task->mm); | |
833 | rcu_read_unlock(); | |
4c4a2214 | 834 | task_unlock(task); |
0b7f569e KH |
835 | if (!curr) |
836 | return 0; | |
d31f56db DN |
837 | /* |
838 | * We should check use_hierarchy of "mem" not "curr". Because checking | |
839 | * use_hierarchy of "curr" here make this function true if hierarchy is | |
840 | * enabled in "curr" and "curr" is a child of "mem" in *cgroup* | |
841 | * hierarchy(even if use_hierarchy is disabled in "mem"). | |
842 | */ | |
843 | if (mem->use_hierarchy) | |
0b7f569e KH |
844 | ret = css_is_ancestor(&curr->css, &mem->css); |
845 | else | |
846 | ret = (curr == mem); | |
847 | css_put(&curr->css); | |
4c4a2214 DR |
848 | return ret; |
849 | } | |
850 | ||
6c48a1d0 KH |
851 | /* |
852 | * prev_priority control...this will be used in memory reclaim path. | |
853 | */ | |
854 | int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem) | |
855 | { | |
2733c06a KM |
856 | int prev_priority; |
857 | ||
858 | spin_lock(&mem->reclaim_param_lock); | |
859 | prev_priority = mem->prev_priority; | |
860 | spin_unlock(&mem->reclaim_param_lock); | |
861 | ||
862 | return prev_priority; | |
6c48a1d0 KH |
863 | } |
864 | ||
865 | void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority) | |
866 | { | |
2733c06a | 867 | spin_lock(&mem->reclaim_param_lock); |
6c48a1d0 KH |
868 | if (priority < mem->prev_priority) |
869 | mem->prev_priority = priority; | |
2733c06a | 870 | spin_unlock(&mem->reclaim_param_lock); |
6c48a1d0 KH |
871 | } |
872 | ||
873 | void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority) | |
874 | { | |
2733c06a | 875 | spin_lock(&mem->reclaim_param_lock); |
6c48a1d0 | 876 | mem->prev_priority = priority; |
2733c06a | 877 | spin_unlock(&mem->reclaim_param_lock); |
6c48a1d0 KH |
878 | } |
879 | ||
c772be93 | 880 | static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages) |
14797e23 KM |
881 | { |
882 | unsigned long active; | |
883 | unsigned long inactive; | |
c772be93 KM |
884 | unsigned long gb; |
885 | unsigned long inactive_ratio; | |
14797e23 | 886 | |
14067bb3 KH |
887 | inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON); |
888 | active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON); | |
14797e23 | 889 | |
c772be93 KM |
890 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
891 | if (gb) | |
892 | inactive_ratio = int_sqrt(10 * gb); | |
893 | else | |
894 | inactive_ratio = 1; | |
895 | ||
896 | if (present_pages) { | |
897 | present_pages[0] = inactive; | |
898 | present_pages[1] = active; | |
899 | } | |
900 | ||
901 | return inactive_ratio; | |
902 | } | |
903 | ||
904 | int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg) | |
905 | { | |
906 | unsigned long active; | |
907 | unsigned long inactive; | |
908 | unsigned long present_pages[2]; | |
909 | unsigned long inactive_ratio; | |
910 | ||
911 | inactive_ratio = calc_inactive_ratio(memcg, present_pages); | |
912 | ||
913 | inactive = present_pages[0]; | |
914 | active = present_pages[1]; | |
915 | ||
916 | if (inactive * inactive_ratio < active) | |
14797e23 KM |
917 | return 1; |
918 | ||
919 | return 0; | |
920 | } | |
921 | ||
56e49d21 RR |
922 | int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg) |
923 | { | |
924 | unsigned long active; | |
925 | unsigned long inactive; | |
926 | ||
927 | inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE); | |
928 | active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE); | |
929 | ||
930 | return (active > inactive); | |
931 | } | |
932 | ||
a3d8e054 KM |
933 | unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg, |
934 | struct zone *zone, | |
935 | enum lru_list lru) | |
936 | { | |
937 | int nid = zone->zone_pgdat->node_id; | |
938 | int zid = zone_idx(zone); | |
939 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); | |
940 | ||
941 | return MEM_CGROUP_ZSTAT(mz, lru); | |
942 | } | |
943 | ||
3e2f41f1 KM |
944 | struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg, |
945 | struct zone *zone) | |
946 | { | |
947 | int nid = zone->zone_pgdat->node_id; | |
948 | int zid = zone_idx(zone); | |
949 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); | |
950 | ||
951 | return &mz->reclaim_stat; | |
952 | } | |
953 | ||
954 | struct zone_reclaim_stat * | |
955 | mem_cgroup_get_reclaim_stat_from_page(struct page *page) | |
956 | { | |
957 | struct page_cgroup *pc; | |
958 | struct mem_cgroup_per_zone *mz; | |
959 | ||
960 | if (mem_cgroup_disabled()) | |
961 | return NULL; | |
962 | ||
963 | pc = lookup_page_cgroup(page); | |
bd112db8 DN |
964 | /* |
965 | * Used bit is set without atomic ops but after smp_wmb(). | |
966 | * For making pc->mem_cgroup visible, insert smp_rmb() here. | |
967 | */ | |
968 | smp_rmb(); | |
969 | if (!PageCgroupUsed(pc)) | |
970 | return NULL; | |
971 | ||
3e2f41f1 KM |
972 | mz = page_cgroup_zoneinfo(pc); |
973 | if (!mz) | |
974 | return NULL; | |
975 | ||
976 | return &mz->reclaim_stat; | |
977 | } | |
978 | ||
66e1707b BS |
979 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
980 | struct list_head *dst, | |
981 | unsigned long *scanned, int order, | |
982 | int mode, struct zone *z, | |
983 | struct mem_cgroup *mem_cont, | |
4f98a2fe | 984 | int active, int file) |
66e1707b BS |
985 | { |
986 | unsigned long nr_taken = 0; | |
987 | struct page *page; | |
988 | unsigned long scan; | |
989 | LIST_HEAD(pc_list); | |
990 | struct list_head *src; | |
ff7283fa | 991 | struct page_cgroup *pc, *tmp; |
1ecaab2b KH |
992 | int nid = z->zone_pgdat->node_id; |
993 | int zid = zone_idx(z); | |
994 | struct mem_cgroup_per_zone *mz; | |
b7c46d15 | 995 | int lru = LRU_FILE * file + active; |
2ffebca6 | 996 | int ret; |
66e1707b | 997 | |
cf475ad2 | 998 | BUG_ON(!mem_cont); |
1ecaab2b | 999 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
b69408e8 | 1000 | src = &mz->lists[lru]; |
66e1707b | 1001 | |
ff7283fa KH |
1002 | scan = 0; |
1003 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { | |
436c6541 | 1004 | if (scan >= nr_to_scan) |
ff7283fa | 1005 | break; |
08e552c6 KH |
1006 | |
1007 | page = pc->page; | |
52d4b9ac KH |
1008 | if (unlikely(!PageCgroupUsed(pc))) |
1009 | continue; | |
436c6541 | 1010 | if (unlikely(!PageLRU(page))) |
ff7283fa | 1011 | continue; |
ff7283fa | 1012 | |
436c6541 | 1013 | scan++; |
2ffebca6 KH |
1014 | ret = __isolate_lru_page(page, mode, file); |
1015 | switch (ret) { | |
1016 | case 0: | |
66e1707b | 1017 | list_move(&page->lru, dst); |
2ffebca6 | 1018 | mem_cgroup_del_lru(page); |
66e1707b | 1019 | nr_taken++; |
2ffebca6 KH |
1020 | break; |
1021 | case -EBUSY: | |
1022 | /* we don't affect global LRU but rotate in our LRU */ | |
1023 | mem_cgroup_rotate_lru_list(page, page_lru(page)); | |
1024 | break; | |
1025 | default: | |
1026 | break; | |
66e1707b BS |
1027 | } |
1028 | } | |
1029 | ||
66e1707b BS |
1030 | *scanned = scan; |
1031 | return nr_taken; | |
1032 | } | |
1033 | ||
6d61ef40 BS |
1034 | #define mem_cgroup_from_res_counter(counter, member) \ |
1035 | container_of(counter, struct mem_cgroup, member) | |
1036 | ||
b85a96c0 DN |
1037 | static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem) |
1038 | { | |
1039 | if (do_swap_account) { | |
1040 | if (res_counter_check_under_limit(&mem->res) && | |
1041 | res_counter_check_under_limit(&mem->memsw)) | |
1042 | return true; | |
1043 | } else | |
1044 | if (res_counter_check_under_limit(&mem->res)) | |
1045 | return true; | |
1046 | return false; | |
1047 | } | |
1048 | ||
a7885eb8 KM |
1049 | static unsigned int get_swappiness(struct mem_cgroup *memcg) |
1050 | { | |
1051 | struct cgroup *cgrp = memcg->css.cgroup; | |
1052 | unsigned int swappiness; | |
1053 | ||
1054 | /* root ? */ | |
1055 | if (cgrp->parent == NULL) | |
1056 | return vm_swappiness; | |
1057 | ||
1058 | spin_lock(&memcg->reclaim_param_lock); | |
1059 | swappiness = memcg->swappiness; | |
1060 | spin_unlock(&memcg->reclaim_param_lock); | |
1061 | ||
1062 | return swappiness; | |
1063 | } | |
1064 | ||
81d39c20 KH |
1065 | static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data) |
1066 | { | |
1067 | int *val = data; | |
1068 | (*val)++; | |
1069 | return 0; | |
1070 | } | |
e222432b BS |
1071 | |
1072 | /** | |
1073 | * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode. | |
1074 | * @memcg: The memory cgroup that went over limit | |
1075 | * @p: Task that is going to be killed | |
1076 | * | |
1077 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is | |
1078 | * enabled | |
1079 | */ | |
1080 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) | |
1081 | { | |
1082 | struct cgroup *task_cgrp; | |
1083 | struct cgroup *mem_cgrp; | |
1084 | /* | |
1085 | * Need a buffer in BSS, can't rely on allocations. The code relies | |
1086 | * on the assumption that OOM is serialized for memory controller. | |
1087 | * If this assumption is broken, revisit this code. | |
1088 | */ | |
1089 | static char memcg_name[PATH_MAX]; | |
1090 | int ret; | |
1091 | ||
d31f56db | 1092 | if (!memcg || !p) |
e222432b BS |
1093 | return; |
1094 | ||
1095 | ||
1096 | rcu_read_lock(); | |
1097 | ||
1098 | mem_cgrp = memcg->css.cgroup; | |
1099 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); | |
1100 | ||
1101 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); | |
1102 | if (ret < 0) { | |
1103 | /* | |
1104 | * Unfortunately, we are unable to convert to a useful name | |
1105 | * But we'll still print out the usage information | |
1106 | */ | |
1107 | rcu_read_unlock(); | |
1108 | goto done; | |
1109 | } | |
1110 | rcu_read_unlock(); | |
1111 | ||
1112 | printk(KERN_INFO "Task in %s killed", memcg_name); | |
1113 | ||
1114 | rcu_read_lock(); | |
1115 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); | |
1116 | if (ret < 0) { | |
1117 | rcu_read_unlock(); | |
1118 | goto done; | |
1119 | } | |
1120 | rcu_read_unlock(); | |
1121 | ||
1122 | /* | |
1123 | * Continues from above, so we don't need an KERN_ level | |
1124 | */ | |
1125 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); | |
1126 | done: | |
1127 | ||
1128 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", | |
1129 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, | |
1130 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, | |
1131 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); | |
1132 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " | |
1133 | "failcnt %llu\n", | |
1134 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, | |
1135 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, | |
1136 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); | |
1137 | } | |
1138 | ||
81d39c20 KH |
1139 | /* |
1140 | * This function returns the number of memcg under hierarchy tree. Returns | |
1141 | * 1(self count) if no children. | |
1142 | */ | |
1143 | static int mem_cgroup_count_children(struct mem_cgroup *mem) | |
1144 | { | |
1145 | int num = 0; | |
1146 | mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb); | |
1147 | return num; | |
1148 | } | |
1149 | ||
6d61ef40 | 1150 | /* |
04046e1a KH |
1151 | * Visit the first child (need not be the first child as per the ordering |
1152 | * of the cgroup list, since we track last_scanned_child) of @mem and use | |
1153 | * that to reclaim free pages from. | |
1154 | */ | |
1155 | static struct mem_cgroup * | |
1156 | mem_cgroup_select_victim(struct mem_cgroup *root_mem) | |
1157 | { | |
1158 | struct mem_cgroup *ret = NULL; | |
1159 | struct cgroup_subsys_state *css; | |
1160 | int nextid, found; | |
1161 | ||
1162 | if (!root_mem->use_hierarchy) { | |
1163 | css_get(&root_mem->css); | |
1164 | ret = root_mem; | |
1165 | } | |
1166 | ||
1167 | while (!ret) { | |
1168 | rcu_read_lock(); | |
1169 | nextid = root_mem->last_scanned_child + 1; | |
1170 | css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css, | |
1171 | &found); | |
1172 | if (css && css_tryget(css)) | |
1173 | ret = container_of(css, struct mem_cgroup, css); | |
1174 | ||
1175 | rcu_read_unlock(); | |
1176 | /* Updates scanning parameter */ | |
1177 | spin_lock(&root_mem->reclaim_param_lock); | |
1178 | if (!css) { | |
1179 | /* this means start scan from ID:1 */ | |
1180 | root_mem->last_scanned_child = 0; | |
1181 | } else | |
1182 | root_mem->last_scanned_child = found; | |
1183 | spin_unlock(&root_mem->reclaim_param_lock); | |
1184 | } | |
1185 | ||
1186 | return ret; | |
1187 | } | |
1188 | ||
1189 | /* | |
1190 | * Scan the hierarchy if needed to reclaim memory. We remember the last child | |
1191 | * we reclaimed from, so that we don't end up penalizing one child extensively | |
1192 | * based on its position in the children list. | |
6d61ef40 BS |
1193 | * |
1194 | * root_mem is the original ancestor that we've been reclaim from. | |
04046e1a KH |
1195 | * |
1196 | * We give up and return to the caller when we visit root_mem twice. | |
1197 | * (other groups can be removed while we're walking....) | |
81d39c20 KH |
1198 | * |
1199 | * If shrink==true, for avoiding to free too much, this returns immedieately. | |
6d61ef40 BS |
1200 | */ |
1201 | static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem, | |
4e416953 | 1202 | struct zone *zone, |
75822b44 BS |
1203 | gfp_t gfp_mask, |
1204 | unsigned long reclaim_options) | |
6d61ef40 | 1205 | { |
04046e1a KH |
1206 | struct mem_cgroup *victim; |
1207 | int ret, total = 0; | |
1208 | int loop = 0; | |
75822b44 BS |
1209 | bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP; |
1210 | bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK; | |
4e416953 BS |
1211 | bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT; |
1212 | unsigned long excess = mem_cgroup_get_excess(root_mem); | |
04046e1a | 1213 | |
22a668d7 KH |
1214 | /* If memsw_is_minimum==1, swap-out is of-no-use. */ |
1215 | if (root_mem->memsw_is_minimum) | |
1216 | noswap = true; | |
1217 | ||
4e416953 | 1218 | while (1) { |
04046e1a | 1219 | victim = mem_cgroup_select_victim(root_mem); |
4e416953 | 1220 | if (victim == root_mem) { |
04046e1a | 1221 | loop++; |
cdec2e42 KH |
1222 | if (loop >= 1) |
1223 | drain_all_stock_async(); | |
4e416953 BS |
1224 | if (loop >= 2) { |
1225 | /* | |
1226 | * If we have not been able to reclaim | |
1227 | * anything, it might because there are | |
1228 | * no reclaimable pages under this hierarchy | |
1229 | */ | |
1230 | if (!check_soft || !total) { | |
1231 | css_put(&victim->css); | |
1232 | break; | |
1233 | } | |
1234 | /* | |
1235 | * We want to do more targetted reclaim. | |
1236 | * excess >> 2 is not to excessive so as to | |
1237 | * reclaim too much, nor too less that we keep | |
1238 | * coming back to reclaim from this cgroup | |
1239 | */ | |
1240 | if (total >= (excess >> 2) || | |
1241 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) { | |
1242 | css_put(&victim->css); | |
1243 | break; | |
1244 | } | |
1245 | } | |
1246 | } | |
04046e1a KH |
1247 | if (!mem_cgroup_local_usage(&victim->stat)) { |
1248 | /* this cgroup's local usage == 0 */ | |
1249 | css_put(&victim->css); | |
6d61ef40 BS |
1250 | continue; |
1251 | } | |
04046e1a | 1252 | /* we use swappiness of local cgroup */ |
4e416953 BS |
1253 | if (check_soft) |
1254 | ret = mem_cgroup_shrink_node_zone(victim, gfp_mask, | |
1255 | noswap, get_swappiness(victim), zone, | |
1256 | zone->zone_pgdat->node_id); | |
1257 | else | |
1258 | ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, | |
1259 | noswap, get_swappiness(victim)); | |
04046e1a | 1260 | css_put(&victim->css); |
81d39c20 KH |
1261 | /* |
1262 | * At shrinking usage, we can't check we should stop here or | |
1263 | * reclaim more. It's depends on callers. last_scanned_child | |
1264 | * will work enough for keeping fairness under tree. | |
1265 | */ | |
1266 | if (shrink) | |
1267 | return ret; | |
04046e1a | 1268 | total += ret; |
4e416953 BS |
1269 | if (check_soft) { |
1270 | if (res_counter_check_under_soft_limit(&root_mem->res)) | |
1271 | return total; | |
1272 | } else if (mem_cgroup_check_under_limit(root_mem)) | |
04046e1a | 1273 | return 1 + total; |
6d61ef40 | 1274 | } |
04046e1a | 1275 | return total; |
6d61ef40 BS |
1276 | } |
1277 | ||
a636b327 KH |
1278 | bool mem_cgroup_oom_called(struct task_struct *task) |
1279 | { | |
1280 | bool ret = false; | |
1281 | struct mem_cgroup *mem; | |
1282 | struct mm_struct *mm; | |
1283 | ||
1284 | rcu_read_lock(); | |
1285 | mm = task->mm; | |
1286 | if (!mm) | |
1287 | mm = &init_mm; | |
1288 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); | |
1289 | if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10)) | |
1290 | ret = true; | |
1291 | rcu_read_unlock(); | |
1292 | return ret; | |
1293 | } | |
0b7f569e KH |
1294 | |
1295 | static int record_last_oom_cb(struct mem_cgroup *mem, void *data) | |
1296 | { | |
1297 | mem->last_oom_jiffies = jiffies; | |
1298 | return 0; | |
1299 | } | |
1300 | ||
1301 | static void record_last_oom(struct mem_cgroup *mem) | |
1302 | { | |
1303 | mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb); | |
1304 | } | |
1305 | ||
d69b042f BS |
1306 | /* |
1307 | * Currently used to update mapped file statistics, but the routine can be | |
1308 | * generalized to update other statistics as well. | |
1309 | */ | |
d8046582 | 1310 | void mem_cgroup_update_file_mapped(struct page *page, int val) |
d69b042f BS |
1311 | { |
1312 | struct mem_cgroup *mem; | |
1313 | struct mem_cgroup_stat *stat; | |
1314 | struct mem_cgroup_stat_cpu *cpustat; | |
1315 | int cpu; | |
1316 | struct page_cgroup *pc; | |
1317 | ||
d69b042f BS |
1318 | pc = lookup_page_cgroup(page); |
1319 | if (unlikely(!pc)) | |
1320 | return; | |
1321 | ||
1322 | lock_page_cgroup(pc); | |
1323 | mem = pc->mem_cgroup; | |
1324 | if (!mem) | |
1325 | goto done; | |
1326 | ||
1327 | if (!PageCgroupUsed(pc)) | |
1328 | goto done; | |
1329 | ||
1330 | /* | |
1331 | * Preemption is already disabled, we don't need get_cpu() | |
1332 | */ | |
1333 | cpu = smp_processor_id(); | |
1334 | stat = &mem->stat; | |
1335 | cpustat = &stat->cpustat[cpu]; | |
1336 | ||
d8046582 | 1337 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, val); |
d69b042f BS |
1338 | done: |
1339 | unlock_page_cgroup(pc); | |
1340 | } | |
0b7f569e | 1341 | |
cdec2e42 KH |
1342 | /* |
1343 | * size of first charge trial. "32" comes from vmscan.c's magic value. | |
1344 | * TODO: maybe necessary to use big numbers in big irons. | |
1345 | */ | |
1346 | #define CHARGE_SIZE (32 * PAGE_SIZE) | |
1347 | struct memcg_stock_pcp { | |
1348 | struct mem_cgroup *cached; /* this never be root cgroup */ | |
1349 | int charge; | |
1350 | struct work_struct work; | |
1351 | }; | |
1352 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); | |
1353 | static atomic_t memcg_drain_count; | |
1354 | ||
1355 | /* | |
1356 | * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed | |
1357 | * from local stock and true is returned. If the stock is 0 or charges from a | |
1358 | * cgroup which is not current target, returns false. This stock will be | |
1359 | * refilled. | |
1360 | */ | |
1361 | static bool consume_stock(struct mem_cgroup *mem) | |
1362 | { | |
1363 | struct memcg_stock_pcp *stock; | |
1364 | bool ret = true; | |
1365 | ||
1366 | stock = &get_cpu_var(memcg_stock); | |
1367 | if (mem == stock->cached && stock->charge) | |
1368 | stock->charge -= PAGE_SIZE; | |
1369 | else /* need to call res_counter_charge */ | |
1370 | ret = false; | |
1371 | put_cpu_var(memcg_stock); | |
1372 | return ret; | |
1373 | } | |
1374 | ||
1375 | /* | |
1376 | * Returns stocks cached in percpu to res_counter and reset cached information. | |
1377 | */ | |
1378 | static void drain_stock(struct memcg_stock_pcp *stock) | |
1379 | { | |
1380 | struct mem_cgroup *old = stock->cached; | |
1381 | ||
1382 | if (stock->charge) { | |
1383 | res_counter_uncharge(&old->res, stock->charge); | |
1384 | if (do_swap_account) | |
1385 | res_counter_uncharge(&old->memsw, stock->charge); | |
1386 | } | |
1387 | stock->cached = NULL; | |
1388 | stock->charge = 0; | |
1389 | } | |
1390 | ||
1391 | /* | |
1392 | * This must be called under preempt disabled or must be called by | |
1393 | * a thread which is pinned to local cpu. | |
1394 | */ | |
1395 | static void drain_local_stock(struct work_struct *dummy) | |
1396 | { | |
1397 | struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock); | |
1398 | drain_stock(stock); | |
1399 | } | |
1400 | ||
1401 | /* | |
1402 | * Cache charges(val) which is from res_counter, to local per_cpu area. | |
1403 | * This will be consumed by consumt_stock() function, later. | |
1404 | */ | |
1405 | static void refill_stock(struct mem_cgroup *mem, int val) | |
1406 | { | |
1407 | struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock); | |
1408 | ||
1409 | if (stock->cached != mem) { /* reset if necessary */ | |
1410 | drain_stock(stock); | |
1411 | stock->cached = mem; | |
1412 | } | |
1413 | stock->charge += val; | |
1414 | put_cpu_var(memcg_stock); | |
1415 | } | |
1416 | ||
1417 | /* | |
1418 | * Tries to drain stocked charges in other cpus. This function is asynchronous | |
1419 | * and just put a work per cpu for draining localy on each cpu. Caller can | |
1420 | * expects some charges will be back to res_counter later but cannot wait for | |
1421 | * it. | |
1422 | */ | |
1423 | static void drain_all_stock_async(void) | |
1424 | { | |
1425 | int cpu; | |
1426 | /* This function is for scheduling "drain" in asynchronous way. | |
1427 | * The result of "drain" is not directly handled by callers. Then, | |
1428 | * if someone is calling drain, we don't have to call drain more. | |
1429 | * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if | |
1430 | * there is a race. We just do loose check here. | |
1431 | */ | |
1432 | if (atomic_read(&memcg_drain_count)) | |
1433 | return; | |
1434 | /* Notify other cpus that system-wide "drain" is running */ | |
1435 | atomic_inc(&memcg_drain_count); | |
1436 | get_online_cpus(); | |
1437 | for_each_online_cpu(cpu) { | |
1438 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); | |
1439 | schedule_work_on(cpu, &stock->work); | |
1440 | } | |
1441 | put_online_cpus(); | |
1442 | atomic_dec(&memcg_drain_count); | |
1443 | /* We don't wait for flush_work */ | |
1444 | } | |
1445 | ||
1446 | /* This is a synchronous drain interface. */ | |
1447 | static void drain_all_stock_sync(void) | |
1448 | { | |
1449 | /* called when force_empty is called */ | |
1450 | atomic_inc(&memcg_drain_count); | |
1451 | schedule_on_each_cpu(drain_local_stock); | |
1452 | atomic_dec(&memcg_drain_count); | |
1453 | } | |
1454 | ||
1455 | static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb, | |
1456 | unsigned long action, | |
1457 | void *hcpu) | |
1458 | { | |
1459 | int cpu = (unsigned long)hcpu; | |
1460 | struct memcg_stock_pcp *stock; | |
1461 | ||
1462 | if (action != CPU_DEAD) | |
1463 | return NOTIFY_OK; | |
1464 | stock = &per_cpu(memcg_stock, cpu); | |
1465 | drain_stock(stock); | |
1466 | return NOTIFY_OK; | |
1467 | } | |
1468 | ||
f817ed48 KH |
1469 | /* |
1470 | * Unlike exported interface, "oom" parameter is added. if oom==true, | |
1471 | * oom-killer can be invoked. | |
8a9f3ccd | 1472 | */ |
f817ed48 | 1473 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
8c7c6e34 | 1474 | gfp_t gfp_mask, struct mem_cgroup **memcg, |
f64c3f54 | 1475 | bool oom, struct page *page) |
8a9f3ccd | 1476 | { |
4e649152 | 1477 | struct mem_cgroup *mem, *mem_over_limit; |
7a81b88c | 1478 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
4e649152 | 1479 | struct res_counter *fail_res; |
cdec2e42 | 1480 | int csize = CHARGE_SIZE; |
a636b327 KH |
1481 | |
1482 | if (unlikely(test_thread_flag(TIF_MEMDIE))) { | |
1483 | /* Don't account this! */ | |
1484 | *memcg = NULL; | |
1485 | return 0; | |
1486 | } | |
1487 | ||
8a9f3ccd | 1488 | /* |
3be91277 HD |
1489 | * We always charge the cgroup the mm_struct belongs to. |
1490 | * The mm_struct's mem_cgroup changes on task migration if the | |
8a9f3ccd BS |
1491 | * thread group leader migrates. It's possible that mm is not |
1492 | * set, if so charge the init_mm (happens for pagecache usage). | |
1493 | */ | |
54595fe2 KH |
1494 | mem = *memcg; |
1495 | if (likely(!mem)) { | |
1496 | mem = try_get_mem_cgroup_from_mm(mm); | |
7a81b88c | 1497 | *memcg = mem; |
e8589cc1 | 1498 | } else { |
7a81b88c | 1499 | css_get(&mem->css); |
e8589cc1 | 1500 | } |
54595fe2 KH |
1501 | if (unlikely(!mem)) |
1502 | return 0; | |
1503 | ||
46f7e602 | 1504 | VM_BUG_ON(css_is_removed(&mem->css)); |
cdec2e42 KH |
1505 | if (mem_cgroup_is_root(mem)) |
1506 | goto done; | |
8a9f3ccd | 1507 | |
8c7c6e34 | 1508 | while (1) { |
0c3e73e8 | 1509 | int ret = 0; |
75822b44 | 1510 | unsigned long flags = 0; |
7a81b88c | 1511 | |
cdec2e42 KH |
1512 | if (consume_stock(mem)) |
1513 | goto charged; | |
1514 | ||
1515 | ret = res_counter_charge(&mem->res, csize, &fail_res); | |
8c7c6e34 KH |
1516 | if (likely(!ret)) { |
1517 | if (!do_swap_account) | |
1518 | break; | |
cdec2e42 | 1519 | ret = res_counter_charge(&mem->memsw, csize, &fail_res); |
8c7c6e34 KH |
1520 | if (likely(!ret)) |
1521 | break; | |
1522 | /* mem+swap counter fails */ | |
cdec2e42 | 1523 | res_counter_uncharge(&mem->res, csize); |
75822b44 | 1524 | flags |= MEM_CGROUP_RECLAIM_NOSWAP; |
6d61ef40 BS |
1525 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, |
1526 | memsw); | |
1527 | } else | |
1528 | /* mem counter fails */ | |
1529 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, | |
1530 | res); | |
1531 | ||
cdec2e42 KH |
1532 | /* reduce request size and retry */ |
1533 | if (csize > PAGE_SIZE) { | |
1534 | csize = PAGE_SIZE; | |
1535 | continue; | |
1536 | } | |
3be91277 | 1537 | if (!(gfp_mask & __GFP_WAIT)) |
7a81b88c | 1538 | goto nomem; |
e1a1cd59 | 1539 | |
4e416953 BS |
1540 | ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL, |
1541 | gfp_mask, flags); | |
4d1c6273 DN |
1542 | if (ret) |
1543 | continue; | |
66e1707b BS |
1544 | |
1545 | /* | |
8869b8f6 HD |
1546 | * try_to_free_mem_cgroup_pages() might not give us a full |
1547 | * picture of reclaim. Some pages are reclaimed and might be | |
1548 | * moved to swap cache or just unmapped from the cgroup. | |
1549 | * Check the limit again to see if the reclaim reduced the | |
1550 | * current usage of the cgroup before giving up | |
8c7c6e34 | 1551 | * |
8869b8f6 | 1552 | */ |
b85a96c0 DN |
1553 | if (mem_cgroup_check_under_limit(mem_over_limit)) |
1554 | continue; | |
3be91277 | 1555 | |
8033b97c DN |
1556 | /* try to avoid oom while someone is moving charge */ |
1557 | if (mc.moving_task && current != mc.moving_task) { | |
1558 | struct mem_cgroup *from, *to; | |
1559 | bool do_continue = false; | |
1560 | /* | |
1561 | * There is a small race that "from" or "to" can be | |
1562 | * freed by rmdir, so we use css_tryget(). | |
1563 | */ | |
1564 | rcu_read_lock(); | |
1565 | from = mc.from; | |
1566 | to = mc.to; | |
1567 | if (from && css_tryget(&from->css)) { | |
1568 | if (mem_over_limit->use_hierarchy) | |
1569 | do_continue = css_is_ancestor( | |
1570 | &from->css, | |
1571 | &mem_over_limit->css); | |
1572 | else | |
1573 | do_continue = (from == mem_over_limit); | |
1574 | css_put(&from->css); | |
1575 | } | |
1576 | if (!do_continue && to && css_tryget(&to->css)) { | |
1577 | if (mem_over_limit->use_hierarchy) | |
1578 | do_continue = css_is_ancestor( | |
1579 | &to->css, | |
1580 | &mem_over_limit->css); | |
1581 | else | |
1582 | do_continue = (to == mem_over_limit); | |
1583 | css_put(&to->css); | |
1584 | } | |
1585 | rcu_read_unlock(); | |
1586 | if (do_continue) { | |
1587 | DEFINE_WAIT(wait); | |
1588 | prepare_to_wait(&mc.waitq, &wait, | |
1589 | TASK_INTERRUPTIBLE); | |
1590 | /* moving charge context might have finished. */ | |
1591 | if (mc.moving_task) | |
1592 | schedule(); | |
1593 | finish_wait(&mc.waitq, &wait); | |
1594 | continue; | |
1595 | } | |
1596 | } | |
1597 | ||
3be91277 | 1598 | if (!nr_retries--) { |
a636b327 | 1599 | if (oom) { |
88700756 | 1600 | mem_cgroup_out_of_memory(mem_over_limit, gfp_mask); |
0b7f569e | 1601 | record_last_oom(mem_over_limit); |
a636b327 | 1602 | } |
7a81b88c | 1603 | goto nomem; |
66e1707b | 1604 | } |
8a9f3ccd | 1605 | } |
cdec2e42 KH |
1606 | if (csize > PAGE_SIZE) |
1607 | refill_stock(mem, csize - PAGE_SIZE); | |
1608 | charged: | |
f64c3f54 | 1609 | /* |
4e649152 KH |
1610 | * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree. |
1611 | * if they exceeds softlimit. | |
f64c3f54 | 1612 | */ |
4ffef5fe | 1613 | if (page && mem_cgroup_soft_limit_check(mem)) |
4e649152 | 1614 | mem_cgroup_update_tree(mem, page); |
0c3e73e8 | 1615 | done: |
2e72b634 KS |
1616 | if (mem_cgroup_threshold_check(mem)) |
1617 | mem_cgroup_threshold(mem); | |
7a81b88c KH |
1618 | return 0; |
1619 | nomem: | |
1620 | css_put(&mem->css); | |
1621 | return -ENOMEM; | |
1622 | } | |
8a9f3ccd | 1623 | |
a3032a2c DN |
1624 | /* |
1625 | * Somemtimes we have to undo a charge we got by try_charge(). | |
1626 | * This function is for that and do uncharge, put css's refcnt. | |
1627 | * gotten by try_charge(). | |
1628 | */ | |
854ffa8d DN |
1629 | static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem, |
1630 | unsigned long count) | |
a3032a2c DN |
1631 | { |
1632 | if (!mem_cgroup_is_root(mem)) { | |
854ffa8d | 1633 | res_counter_uncharge(&mem->res, PAGE_SIZE * count); |
a3032a2c | 1634 | if (do_swap_account) |
854ffa8d DN |
1635 | res_counter_uncharge(&mem->memsw, PAGE_SIZE * count); |
1636 | VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags)); | |
1637 | WARN_ON_ONCE(count > INT_MAX); | |
1638 | __css_put(&mem->css, (int)count); | |
a3032a2c | 1639 | } |
854ffa8d DN |
1640 | /* we don't need css_put for root */ |
1641 | } | |
1642 | ||
1643 | static void mem_cgroup_cancel_charge(struct mem_cgroup *mem) | |
1644 | { | |
1645 | __mem_cgroup_cancel_charge(mem, 1); | |
a3032a2c DN |
1646 | } |
1647 | ||
a3b2d692 KH |
1648 | /* |
1649 | * A helper function to get mem_cgroup from ID. must be called under | |
1650 | * rcu_read_lock(). The caller must check css_is_removed() or some if | |
1651 | * it's concern. (dropping refcnt from swap can be called against removed | |
1652 | * memcg.) | |
1653 | */ | |
1654 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) | |
1655 | { | |
1656 | struct cgroup_subsys_state *css; | |
1657 | ||
1658 | /* ID 0 is unused ID */ | |
1659 | if (!id) | |
1660 | return NULL; | |
1661 | css = css_lookup(&mem_cgroup_subsys, id); | |
1662 | if (!css) | |
1663 | return NULL; | |
1664 | return container_of(css, struct mem_cgroup, css); | |
1665 | } | |
1666 | ||
e42d9d5d | 1667 | struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
b5a84319 | 1668 | { |
e42d9d5d | 1669 | struct mem_cgroup *mem = NULL; |
3c776e64 | 1670 | struct page_cgroup *pc; |
a3b2d692 | 1671 | unsigned short id; |
b5a84319 KH |
1672 | swp_entry_t ent; |
1673 | ||
3c776e64 DN |
1674 | VM_BUG_ON(!PageLocked(page)); |
1675 | ||
3c776e64 | 1676 | pc = lookup_page_cgroup(page); |
c0bd3f63 | 1677 | lock_page_cgroup(pc); |
a3b2d692 | 1678 | if (PageCgroupUsed(pc)) { |
3c776e64 | 1679 | mem = pc->mem_cgroup; |
a3b2d692 KH |
1680 | if (mem && !css_tryget(&mem->css)) |
1681 | mem = NULL; | |
e42d9d5d | 1682 | } else if (PageSwapCache(page)) { |
3c776e64 | 1683 | ent.val = page_private(page); |
a3b2d692 KH |
1684 | id = lookup_swap_cgroup(ent); |
1685 | rcu_read_lock(); | |
1686 | mem = mem_cgroup_lookup(id); | |
1687 | if (mem && !css_tryget(&mem->css)) | |
1688 | mem = NULL; | |
1689 | rcu_read_unlock(); | |
3c776e64 | 1690 | } |
c0bd3f63 | 1691 | unlock_page_cgroup(pc); |
b5a84319 KH |
1692 | return mem; |
1693 | } | |
1694 | ||
7a81b88c | 1695 | /* |
a5e924f5 | 1696 | * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be |
7a81b88c KH |
1697 | * USED state. If already USED, uncharge and return. |
1698 | */ | |
1699 | ||
1700 | static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, | |
1701 | struct page_cgroup *pc, | |
1702 | enum charge_type ctype) | |
1703 | { | |
7a81b88c KH |
1704 | /* try_charge() can return NULL to *memcg, taking care of it. */ |
1705 | if (!mem) | |
1706 | return; | |
52d4b9ac KH |
1707 | |
1708 | lock_page_cgroup(pc); | |
1709 | if (unlikely(PageCgroupUsed(pc))) { | |
1710 | unlock_page_cgroup(pc); | |
a3032a2c | 1711 | mem_cgroup_cancel_charge(mem); |
7a81b88c | 1712 | return; |
52d4b9ac | 1713 | } |
4b3bde4c | 1714 | |
8a9f3ccd | 1715 | pc->mem_cgroup = mem; |
261fb61a KH |
1716 | /* |
1717 | * We access a page_cgroup asynchronously without lock_page_cgroup(). | |
1718 | * Especially when a page_cgroup is taken from a page, pc->mem_cgroup | |
1719 | * is accessed after testing USED bit. To make pc->mem_cgroup visible | |
1720 | * before USED bit, we need memory barrier here. | |
1721 | * See mem_cgroup_add_lru_list(), etc. | |
1722 | */ | |
08e552c6 | 1723 | smp_wmb(); |
4b3bde4c BS |
1724 | switch (ctype) { |
1725 | case MEM_CGROUP_CHARGE_TYPE_CACHE: | |
1726 | case MEM_CGROUP_CHARGE_TYPE_SHMEM: | |
1727 | SetPageCgroupCache(pc); | |
1728 | SetPageCgroupUsed(pc); | |
1729 | break; | |
1730 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: | |
1731 | ClearPageCgroupCache(pc); | |
1732 | SetPageCgroupUsed(pc); | |
1733 | break; | |
1734 | default: | |
1735 | break; | |
1736 | } | |
3be91277 | 1737 | |
08e552c6 | 1738 | mem_cgroup_charge_statistics(mem, pc, true); |
52d4b9ac | 1739 | |
52d4b9ac | 1740 | unlock_page_cgroup(pc); |
7a81b88c | 1741 | } |
66e1707b | 1742 | |
f817ed48 | 1743 | /** |
57f9fd7d | 1744 | * __mem_cgroup_move_account - move account of the page |
f817ed48 KH |
1745 | * @pc: page_cgroup of the page. |
1746 | * @from: mem_cgroup which the page is moved from. | |
1747 | * @to: mem_cgroup which the page is moved to. @from != @to. | |
854ffa8d | 1748 | * @uncharge: whether we should call uncharge and css_put against @from. |
f817ed48 KH |
1749 | * |
1750 | * The caller must confirm following. | |
08e552c6 | 1751 | * - page is not on LRU (isolate_page() is useful.) |
57f9fd7d | 1752 | * - the pc is locked, used, and ->mem_cgroup points to @from. |
f817ed48 | 1753 | * |
854ffa8d DN |
1754 | * This function doesn't do "charge" nor css_get to new cgroup. It should be |
1755 | * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is | |
1756 | * true, this function does "uncharge" from old cgroup, but it doesn't if | |
1757 | * @uncharge is false, so a caller should do "uncharge". | |
f817ed48 KH |
1758 | */ |
1759 | ||
57f9fd7d | 1760 | static void __mem_cgroup_move_account(struct page_cgroup *pc, |
854ffa8d | 1761 | struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge) |
f817ed48 | 1762 | { |
d69b042f BS |
1763 | struct page *page; |
1764 | int cpu; | |
1765 | struct mem_cgroup_stat *stat; | |
1766 | struct mem_cgroup_stat_cpu *cpustat; | |
f817ed48 | 1767 | |
f817ed48 | 1768 | VM_BUG_ON(from == to); |
08e552c6 | 1769 | VM_BUG_ON(PageLRU(pc->page)); |
57f9fd7d DN |
1770 | VM_BUG_ON(!PageCgroupLocked(pc)); |
1771 | VM_BUG_ON(!PageCgroupUsed(pc)); | |
1772 | VM_BUG_ON(pc->mem_cgroup != from); | |
f817ed48 | 1773 | |
d69b042f | 1774 | page = pc->page; |
d8046582 | 1775 | if (page_mapped(page) && !PageAnon(page)) { |
d69b042f BS |
1776 | cpu = smp_processor_id(); |
1777 | /* Update mapped_file data for mem_cgroup "from" */ | |
1778 | stat = &from->stat; | |
1779 | cpustat = &stat->cpustat[cpu]; | |
d8046582 | 1780 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, |
d69b042f BS |
1781 | -1); |
1782 | ||
1783 | /* Update mapped_file data for mem_cgroup "to" */ | |
1784 | stat = &to->stat; | |
1785 | cpustat = &stat->cpustat[cpu]; | |
d8046582 | 1786 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, |
d69b042f BS |
1787 | 1); |
1788 | } | |
854ffa8d DN |
1789 | mem_cgroup_charge_statistics(from, pc, false); |
1790 | if (uncharge) | |
1791 | /* This is not "cancel", but cancel_charge does all we need. */ | |
1792 | mem_cgroup_cancel_charge(from); | |
d69b042f | 1793 | |
854ffa8d | 1794 | /* caller should have done css_get */ |
08e552c6 KH |
1795 | pc->mem_cgroup = to; |
1796 | mem_cgroup_charge_statistics(to, pc, true); | |
88703267 KH |
1797 | /* |
1798 | * We charges against "to" which may not have any tasks. Then, "to" | |
1799 | * can be under rmdir(). But in current implementation, caller of | |
4ffef5fe DN |
1800 | * this function is just force_empty() and move charge, so it's |
1801 | * garanteed that "to" is never removed. So, we don't check rmdir | |
1802 | * status here. | |
88703267 | 1803 | */ |
57f9fd7d DN |
1804 | } |
1805 | ||
1806 | /* | |
1807 | * check whether the @pc is valid for moving account and call | |
1808 | * __mem_cgroup_move_account() | |
1809 | */ | |
1810 | static int mem_cgroup_move_account(struct page_cgroup *pc, | |
854ffa8d | 1811 | struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge) |
57f9fd7d DN |
1812 | { |
1813 | int ret = -EINVAL; | |
1814 | lock_page_cgroup(pc); | |
1815 | if (PageCgroupUsed(pc) && pc->mem_cgroup == from) { | |
854ffa8d | 1816 | __mem_cgroup_move_account(pc, from, to, uncharge); |
57f9fd7d DN |
1817 | ret = 0; |
1818 | } | |
1819 | unlock_page_cgroup(pc); | |
f817ed48 KH |
1820 | return ret; |
1821 | } | |
1822 | ||
1823 | /* | |
1824 | * move charges to its parent. | |
1825 | */ | |
1826 | ||
1827 | static int mem_cgroup_move_parent(struct page_cgroup *pc, | |
1828 | struct mem_cgroup *child, | |
1829 | gfp_t gfp_mask) | |
1830 | { | |
08e552c6 | 1831 | struct page *page = pc->page; |
f817ed48 KH |
1832 | struct cgroup *cg = child->css.cgroup; |
1833 | struct cgroup *pcg = cg->parent; | |
1834 | struct mem_cgroup *parent; | |
f817ed48 KH |
1835 | int ret; |
1836 | ||
1837 | /* Is ROOT ? */ | |
1838 | if (!pcg) | |
1839 | return -EINVAL; | |
1840 | ||
57f9fd7d DN |
1841 | ret = -EBUSY; |
1842 | if (!get_page_unless_zero(page)) | |
1843 | goto out; | |
1844 | if (isolate_lru_page(page)) | |
1845 | goto put; | |
08e552c6 | 1846 | |
f817ed48 | 1847 | parent = mem_cgroup_from_cont(pcg); |
f64c3f54 | 1848 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page); |
a636b327 | 1849 | if (ret || !parent) |
57f9fd7d | 1850 | goto put_back; |
f817ed48 | 1851 | |
854ffa8d DN |
1852 | ret = mem_cgroup_move_account(pc, child, parent, true); |
1853 | if (ret) | |
1854 | mem_cgroup_cancel_charge(parent); | |
57f9fd7d | 1855 | put_back: |
08e552c6 | 1856 | putback_lru_page(page); |
57f9fd7d | 1857 | put: |
40d58138 | 1858 | put_page(page); |
57f9fd7d | 1859 | out: |
f817ed48 KH |
1860 | return ret; |
1861 | } | |
1862 | ||
7a81b88c KH |
1863 | /* |
1864 | * Charge the memory controller for page usage. | |
1865 | * Return | |
1866 | * 0 if the charge was successful | |
1867 | * < 0 if the cgroup is over its limit | |
1868 | */ | |
1869 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, | |
1870 | gfp_t gfp_mask, enum charge_type ctype, | |
1871 | struct mem_cgroup *memcg) | |
1872 | { | |
1873 | struct mem_cgroup *mem; | |
1874 | struct page_cgroup *pc; | |
1875 | int ret; | |
1876 | ||
1877 | pc = lookup_page_cgroup(page); | |
1878 | /* can happen at boot */ | |
1879 | if (unlikely(!pc)) | |
1880 | return 0; | |
1881 | prefetchw(pc); | |
1882 | ||
1883 | mem = memcg; | |
f64c3f54 | 1884 | ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page); |
a636b327 | 1885 | if (ret || !mem) |
7a81b88c KH |
1886 | return ret; |
1887 | ||
1888 | __mem_cgroup_commit_charge(mem, pc, ctype); | |
8a9f3ccd | 1889 | return 0; |
8a9f3ccd BS |
1890 | } |
1891 | ||
7a81b88c KH |
1892 | int mem_cgroup_newpage_charge(struct page *page, |
1893 | struct mm_struct *mm, gfp_t gfp_mask) | |
217bc319 | 1894 | { |
f8d66542 | 1895 | if (mem_cgroup_disabled()) |
cede86ac | 1896 | return 0; |
52d4b9ac KH |
1897 | if (PageCompound(page)) |
1898 | return 0; | |
69029cd5 KH |
1899 | /* |
1900 | * If already mapped, we don't have to account. | |
1901 | * If page cache, page->mapping has address_space. | |
1902 | * But page->mapping may have out-of-use anon_vma pointer, | |
1903 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping | |
1904 | * is NULL. | |
1905 | */ | |
1906 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) | |
1907 | return 0; | |
1908 | if (unlikely(!mm)) | |
1909 | mm = &init_mm; | |
217bc319 | 1910 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
e8589cc1 | 1911 | MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL); |
217bc319 KH |
1912 | } |
1913 | ||
83aae4c7 DN |
1914 | static void |
1915 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, | |
1916 | enum charge_type ctype); | |
1917 | ||
e1a1cd59 BS |
1918 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
1919 | gfp_t gfp_mask) | |
8697d331 | 1920 | { |
b5a84319 KH |
1921 | struct mem_cgroup *mem = NULL; |
1922 | int ret; | |
1923 | ||
f8d66542 | 1924 | if (mem_cgroup_disabled()) |
cede86ac | 1925 | return 0; |
52d4b9ac KH |
1926 | if (PageCompound(page)) |
1927 | return 0; | |
accf163e KH |
1928 | /* |
1929 | * Corner case handling. This is called from add_to_page_cache() | |
1930 | * in usual. But some FS (shmem) precharges this page before calling it | |
1931 | * and call add_to_page_cache() with GFP_NOWAIT. | |
1932 | * | |
1933 | * For GFP_NOWAIT case, the page may be pre-charged before calling | |
1934 | * add_to_page_cache(). (See shmem.c) check it here and avoid to call | |
1935 | * charge twice. (It works but has to pay a bit larger cost.) | |
b5a84319 KH |
1936 | * And when the page is SwapCache, it should take swap information |
1937 | * into account. This is under lock_page() now. | |
accf163e KH |
1938 | */ |
1939 | if (!(gfp_mask & __GFP_WAIT)) { | |
1940 | struct page_cgroup *pc; | |
1941 | ||
52d4b9ac KH |
1942 | |
1943 | pc = lookup_page_cgroup(page); | |
1944 | if (!pc) | |
1945 | return 0; | |
1946 | lock_page_cgroup(pc); | |
1947 | if (PageCgroupUsed(pc)) { | |
1948 | unlock_page_cgroup(pc); | |
accf163e KH |
1949 | return 0; |
1950 | } | |
52d4b9ac | 1951 | unlock_page_cgroup(pc); |
accf163e KH |
1952 | } |
1953 | ||
b5a84319 | 1954 | if (unlikely(!mm && !mem)) |
8697d331 | 1955 | mm = &init_mm; |
accf163e | 1956 | |
c05555b5 KH |
1957 | if (page_is_file_cache(page)) |
1958 | return mem_cgroup_charge_common(page, mm, gfp_mask, | |
e8589cc1 | 1959 | MEM_CGROUP_CHARGE_TYPE_CACHE, NULL); |
b5a84319 | 1960 | |
83aae4c7 DN |
1961 | /* shmem */ |
1962 | if (PageSwapCache(page)) { | |
1963 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); | |
1964 | if (!ret) | |
1965 | __mem_cgroup_commit_charge_swapin(page, mem, | |
1966 | MEM_CGROUP_CHARGE_TYPE_SHMEM); | |
1967 | } else | |
1968 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, | |
1969 | MEM_CGROUP_CHARGE_TYPE_SHMEM, mem); | |
b5a84319 | 1970 | |
b5a84319 | 1971 | return ret; |
e8589cc1 KH |
1972 | } |
1973 | ||
54595fe2 KH |
1974 | /* |
1975 | * While swap-in, try_charge -> commit or cancel, the page is locked. | |
1976 | * And when try_charge() successfully returns, one refcnt to memcg without | |
21ae2956 | 1977 | * struct page_cgroup is acquired. This refcnt will be consumed by |
54595fe2 KH |
1978 | * "commit()" or removed by "cancel()" |
1979 | */ | |
8c7c6e34 KH |
1980 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
1981 | struct page *page, | |
1982 | gfp_t mask, struct mem_cgroup **ptr) | |
1983 | { | |
1984 | struct mem_cgroup *mem; | |
54595fe2 | 1985 | int ret; |
8c7c6e34 | 1986 | |
f8d66542 | 1987 | if (mem_cgroup_disabled()) |
8c7c6e34 KH |
1988 | return 0; |
1989 | ||
1990 | if (!do_swap_account) | |
1991 | goto charge_cur_mm; | |
8c7c6e34 KH |
1992 | /* |
1993 | * A racing thread's fault, or swapoff, may have already updated | |
407f9c8b HD |
1994 | * the pte, and even removed page from swap cache: in those cases |
1995 | * do_swap_page()'s pte_same() test will fail; but there's also a | |
1996 | * KSM case which does need to charge the page. | |
8c7c6e34 KH |
1997 | */ |
1998 | if (!PageSwapCache(page)) | |
407f9c8b | 1999 | goto charge_cur_mm; |
e42d9d5d | 2000 | mem = try_get_mem_cgroup_from_page(page); |
54595fe2 KH |
2001 | if (!mem) |
2002 | goto charge_cur_mm; | |
8c7c6e34 | 2003 | *ptr = mem; |
f64c3f54 | 2004 | ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page); |
54595fe2 KH |
2005 | /* drop extra refcnt from tryget */ |
2006 | css_put(&mem->css); | |
2007 | return ret; | |
8c7c6e34 KH |
2008 | charge_cur_mm: |
2009 | if (unlikely(!mm)) | |
2010 | mm = &init_mm; | |
f64c3f54 | 2011 | return __mem_cgroup_try_charge(mm, mask, ptr, true, page); |
8c7c6e34 KH |
2012 | } |
2013 | ||
83aae4c7 DN |
2014 | static void |
2015 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, | |
2016 | enum charge_type ctype) | |
7a81b88c KH |
2017 | { |
2018 | struct page_cgroup *pc; | |
2019 | ||
f8d66542 | 2020 | if (mem_cgroup_disabled()) |
7a81b88c KH |
2021 | return; |
2022 | if (!ptr) | |
2023 | return; | |
88703267 | 2024 | cgroup_exclude_rmdir(&ptr->css); |
7a81b88c | 2025 | pc = lookup_page_cgroup(page); |
544122e5 | 2026 | mem_cgroup_lru_del_before_commit_swapcache(page); |
83aae4c7 | 2027 | __mem_cgroup_commit_charge(ptr, pc, ctype); |
544122e5 | 2028 | mem_cgroup_lru_add_after_commit_swapcache(page); |
8c7c6e34 KH |
2029 | /* |
2030 | * Now swap is on-memory. This means this page may be | |
2031 | * counted both as mem and swap....double count. | |
03f3c433 KH |
2032 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
2033 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() | |
2034 | * may call delete_from_swap_cache() before reach here. | |
8c7c6e34 | 2035 | */ |
03f3c433 | 2036 | if (do_swap_account && PageSwapCache(page)) { |
8c7c6e34 | 2037 | swp_entry_t ent = {.val = page_private(page)}; |
a3b2d692 | 2038 | unsigned short id; |
8c7c6e34 | 2039 | struct mem_cgroup *memcg; |
a3b2d692 KH |
2040 | |
2041 | id = swap_cgroup_record(ent, 0); | |
2042 | rcu_read_lock(); | |
2043 | memcg = mem_cgroup_lookup(id); | |
8c7c6e34 | 2044 | if (memcg) { |
a3b2d692 KH |
2045 | /* |
2046 | * This recorded memcg can be obsolete one. So, avoid | |
2047 | * calling css_tryget | |
2048 | */ | |
0c3e73e8 | 2049 | if (!mem_cgroup_is_root(memcg)) |
4e649152 | 2050 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
0c3e73e8 | 2051 | mem_cgroup_swap_statistics(memcg, false); |
8c7c6e34 KH |
2052 | mem_cgroup_put(memcg); |
2053 | } | |
a3b2d692 | 2054 | rcu_read_unlock(); |
8c7c6e34 | 2055 | } |
88703267 KH |
2056 | /* |
2057 | * At swapin, we may charge account against cgroup which has no tasks. | |
2058 | * So, rmdir()->pre_destroy() can be called while we do this charge. | |
2059 | * In that case, we need to call pre_destroy() again. check it here. | |
2060 | */ | |
2061 | cgroup_release_and_wakeup_rmdir(&ptr->css); | |
7a81b88c KH |
2062 | } |
2063 | ||
83aae4c7 DN |
2064 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
2065 | { | |
2066 | __mem_cgroup_commit_charge_swapin(page, ptr, | |
2067 | MEM_CGROUP_CHARGE_TYPE_MAPPED); | |
2068 | } | |
2069 | ||
7a81b88c KH |
2070 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem) |
2071 | { | |
f8d66542 | 2072 | if (mem_cgroup_disabled()) |
7a81b88c KH |
2073 | return; |
2074 | if (!mem) | |
2075 | return; | |
a3032a2c | 2076 | mem_cgroup_cancel_charge(mem); |
7a81b88c KH |
2077 | } |
2078 | ||
569b846d KH |
2079 | static void |
2080 | __do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype) | |
2081 | { | |
2082 | struct memcg_batch_info *batch = NULL; | |
2083 | bool uncharge_memsw = true; | |
2084 | /* If swapout, usage of swap doesn't decrease */ | |
2085 | if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) | |
2086 | uncharge_memsw = false; | |
2087 | /* | |
2088 | * do_batch > 0 when unmapping pages or inode invalidate/truncate. | |
2089 | * In those cases, all pages freed continously can be expected to be in | |
2090 | * the same cgroup and we have chance to coalesce uncharges. | |
2091 | * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE) | |
2092 | * because we want to do uncharge as soon as possible. | |
2093 | */ | |
2094 | if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE)) | |
2095 | goto direct_uncharge; | |
2096 | ||
2097 | batch = ¤t->memcg_batch; | |
2098 | /* | |
2099 | * In usual, we do css_get() when we remember memcg pointer. | |
2100 | * But in this case, we keep res->usage until end of a series of | |
2101 | * uncharges. Then, it's ok to ignore memcg's refcnt. | |
2102 | */ | |
2103 | if (!batch->memcg) | |
2104 | batch->memcg = mem; | |
2105 | /* | |
2106 | * In typical case, batch->memcg == mem. This means we can | |
2107 | * merge a series of uncharges to an uncharge of res_counter. | |
2108 | * If not, we uncharge res_counter ony by one. | |
2109 | */ | |
2110 | if (batch->memcg != mem) | |
2111 | goto direct_uncharge; | |
2112 | /* remember freed charge and uncharge it later */ | |
2113 | batch->bytes += PAGE_SIZE; | |
2114 | if (uncharge_memsw) | |
2115 | batch->memsw_bytes += PAGE_SIZE; | |
2116 | return; | |
2117 | direct_uncharge: | |
2118 | res_counter_uncharge(&mem->res, PAGE_SIZE); | |
2119 | if (uncharge_memsw) | |
2120 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); | |
2121 | return; | |
2122 | } | |
7a81b88c | 2123 | |
8a9f3ccd | 2124 | /* |
69029cd5 | 2125 | * uncharge if !page_mapped(page) |
8a9f3ccd | 2126 | */ |
8c7c6e34 | 2127 | static struct mem_cgroup * |
69029cd5 | 2128 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
8a9f3ccd | 2129 | { |
8289546e | 2130 | struct page_cgroup *pc; |
8c7c6e34 | 2131 | struct mem_cgroup *mem = NULL; |
072c56c1 | 2132 | struct mem_cgroup_per_zone *mz; |
8a9f3ccd | 2133 | |
f8d66542 | 2134 | if (mem_cgroup_disabled()) |
8c7c6e34 | 2135 | return NULL; |
4077960e | 2136 | |
d13d1443 | 2137 | if (PageSwapCache(page)) |
8c7c6e34 | 2138 | return NULL; |
d13d1443 | 2139 | |
8697d331 | 2140 | /* |
3c541e14 | 2141 | * Check if our page_cgroup is valid |
8697d331 | 2142 | */ |
52d4b9ac KH |
2143 | pc = lookup_page_cgroup(page); |
2144 | if (unlikely(!pc || !PageCgroupUsed(pc))) | |
8c7c6e34 | 2145 | return NULL; |
b9c565d5 | 2146 | |
52d4b9ac | 2147 | lock_page_cgroup(pc); |
d13d1443 | 2148 | |
8c7c6e34 KH |
2149 | mem = pc->mem_cgroup; |
2150 | ||
d13d1443 KH |
2151 | if (!PageCgroupUsed(pc)) |
2152 | goto unlock_out; | |
2153 | ||
2154 | switch (ctype) { | |
2155 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: | |
8a9478ca | 2156 | case MEM_CGROUP_CHARGE_TYPE_DROP: |
d13d1443 KH |
2157 | if (page_mapped(page)) |
2158 | goto unlock_out; | |
2159 | break; | |
2160 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: | |
2161 | if (!PageAnon(page)) { /* Shared memory */ | |
2162 | if (page->mapping && !page_is_file_cache(page)) | |
2163 | goto unlock_out; | |
2164 | } else if (page_mapped(page)) /* Anon */ | |
2165 | goto unlock_out; | |
2166 | break; | |
2167 | default: | |
2168 | break; | |
52d4b9ac | 2169 | } |
d13d1443 | 2170 | |
569b846d KH |
2171 | if (!mem_cgroup_is_root(mem)) |
2172 | __do_uncharge(mem, ctype); | |
0c3e73e8 BS |
2173 | if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) |
2174 | mem_cgroup_swap_statistics(mem, true); | |
08e552c6 | 2175 | mem_cgroup_charge_statistics(mem, pc, false); |
04046e1a | 2176 | |
52d4b9ac | 2177 | ClearPageCgroupUsed(pc); |
544122e5 KH |
2178 | /* |
2179 | * pc->mem_cgroup is not cleared here. It will be accessed when it's | |
2180 | * freed from LRU. This is safe because uncharged page is expected not | |
2181 | * to be reused (freed soon). Exception is SwapCache, it's handled by | |
2182 | * special functions. | |
2183 | */ | |
b9c565d5 | 2184 | |
69029cd5 | 2185 | mz = page_cgroup_zoneinfo(pc); |
52d4b9ac | 2186 | unlock_page_cgroup(pc); |
fb59e9f1 | 2187 | |
4e649152 | 2188 | if (mem_cgroup_soft_limit_check(mem)) |
f64c3f54 | 2189 | mem_cgroup_update_tree(mem, page); |
2e72b634 KS |
2190 | if (mem_cgroup_threshold_check(mem)) |
2191 | mem_cgroup_threshold(mem); | |
a7fe942e KH |
2192 | /* at swapout, this memcg will be accessed to record to swap */ |
2193 | if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT) | |
2194 | css_put(&mem->css); | |
6d12e2d8 | 2195 | |
8c7c6e34 | 2196 | return mem; |
d13d1443 KH |
2197 | |
2198 | unlock_out: | |
2199 | unlock_page_cgroup(pc); | |
8c7c6e34 | 2200 | return NULL; |
3c541e14 BS |
2201 | } |
2202 | ||
69029cd5 KH |
2203 | void mem_cgroup_uncharge_page(struct page *page) |
2204 | { | |
52d4b9ac KH |
2205 | /* early check. */ |
2206 | if (page_mapped(page)) | |
2207 | return; | |
2208 | if (page->mapping && !PageAnon(page)) | |
2209 | return; | |
69029cd5 KH |
2210 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
2211 | } | |
2212 | ||
2213 | void mem_cgroup_uncharge_cache_page(struct page *page) | |
2214 | { | |
2215 | VM_BUG_ON(page_mapped(page)); | |
b7abea96 | 2216 | VM_BUG_ON(page->mapping); |
69029cd5 KH |
2217 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
2218 | } | |
2219 | ||
569b846d KH |
2220 | /* |
2221 | * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate. | |
2222 | * In that cases, pages are freed continuously and we can expect pages | |
2223 | * are in the same memcg. All these calls itself limits the number of | |
2224 | * pages freed at once, then uncharge_start/end() is called properly. | |
2225 | * This may be called prural(2) times in a context, | |
2226 | */ | |
2227 | ||
2228 | void mem_cgroup_uncharge_start(void) | |
2229 | { | |
2230 | current->memcg_batch.do_batch++; | |
2231 | /* We can do nest. */ | |
2232 | if (current->memcg_batch.do_batch == 1) { | |
2233 | current->memcg_batch.memcg = NULL; | |
2234 | current->memcg_batch.bytes = 0; | |
2235 | current->memcg_batch.memsw_bytes = 0; | |
2236 | } | |
2237 | } | |
2238 | ||
2239 | void mem_cgroup_uncharge_end(void) | |
2240 | { | |
2241 | struct memcg_batch_info *batch = ¤t->memcg_batch; | |
2242 | ||
2243 | if (!batch->do_batch) | |
2244 | return; | |
2245 | ||
2246 | batch->do_batch--; | |
2247 | if (batch->do_batch) /* If stacked, do nothing. */ | |
2248 | return; | |
2249 | ||
2250 | if (!batch->memcg) | |
2251 | return; | |
2252 | /* | |
2253 | * This "batch->memcg" is valid without any css_get/put etc... | |
2254 | * bacause we hide charges behind us. | |
2255 | */ | |
2256 | if (batch->bytes) | |
2257 | res_counter_uncharge(&batch->memcg->res, batch->bytes); | |
2258 | if (batch->memsw_bytes) | |
2259 | res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes); | |
2260 | /* forget this pointer (for sanity check) */ | |
2261 | batch->memcg = NULL; | |
2262 | } | |
2263 | ||
e767e056 | 2264 | #ifdef CONFIG_SWAP |
8c7c6e34 | 2265 | /* |
e767e056 | 2266 | * called after __delete_from_swap_cache() and drop "page" account. |
8c7c6e34 KH |
2267 | * memcg information is recorded to swap_cgroup of "ent" |
2268 | */ | |
8a9478ca KH |
2269 | void |
2270 | mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout) | |
8c7c6e34 KH |
2271 | { |
2272 | struct mem_cgroup *memcg; | |
8a9478ca KH |
2273 | int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT; |
2274 | ||
2275 | if (!swapout) /* this was a swap cache but the swap is unused ! */ | |
2276 | ctype = MEM_CGROUP_CHARGE_TYPE_DROP; | |
2277 | ||
2278 | memcg = __mem_cgroup_uncharge_common(page, ctype); | |
8c7c6e34 | 2279 | |
8c7c6e34 | 2280 | /* record memcg information */ |
8a9478ca | 2281 | if (do_swap_account && swapout && memcg) { |
a3b2d692 | 2282 | swap_cgroup_record(ent, css_id(&memcg->css)); |
8c7c6e34 KH |
2283 | mem_cgroup_get(memcg); |
2284 | } | |
8a9478ca | 2285 | if (swapout && memcg) |
a7fe942e | 2286 | css_put(&memcg->css); |
8c7c6e34 | 2287 | } |
e767e056 | 2288 | #endif |
8c7c6e34 KH |
2289 | |
2290 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP | |
2291 | /* | |
2292 | * called from swap_entry_free(). remove record in swap_cgroup and | |
2293 | * uncharge "memsw" account. | |
2294 | */ | |
2295 | void mem_cgroup_uncharge_swap(swp_entry_t ent) | |
d13d1443 | 2296 | { |
8c7c6e34 | 2297 | struct mem_cgroup *memcg; |
a3b2d692 | 2298 | unsigned short id; |
8c7c6e34 KH |
2299 | |
2300 | if (!do_swap_account) | |
2301 | return; | |
2302 | ||
a3b2d692 KH |
2303 | id = swap_cgroup_record(ent, 0); |
2304 | rcu_read_lock(); | |
2305 | memcg = mem_cgroup_lookup(id); | |
8c7c6e34 | 2306 | if (memcg) { |
a3b2d692 KH |
2307 | /* |
2308 | * We uncharge this because swap is freed. | |
2309 | * This memcg can be obsolete one. We avoid calling css_tryget | |
2310 | */ | |
0c3e73e8 | 2311 | if (!mem_cgroup_is_root(memcg)) |
4e649152 | 2312 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
0c3e73e8 | 2313 | mem_cgroup_swap_statistics(memcg, false); |
8c7c6e34 KH |
2314 | mem_cgroup_put(memcg); |
2315 | } | |
a3b2d692 | 2316 | rcu_read_unlock(); |
d13d1443 | 2317 | } |
02491447 DN |
2318 | |
2319 | /** | |
2320 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. | |
2321 | * @entry: swap entry to be moved | |
2322 | * @from: mem_cgroup which the entry is moved from | |
2323 | * @to: mem_cgroup which the entry is moved to | |
483c30b5 | 2324 | * @need_fixup: whether we should fixup res_counters and refcounts. |
02491447 DN |
2325 | * |
2326 | * It succeeds only when the swap_cgroup's record for this entry is the same | |
2327 | * as the mem_cgroup's id of @from. | |
2328 | * | |
2329 | * Returns 0 on success, -EINVAL on failure. | |
2330 | * | |
2331 | * The caller must have charged to @to, IOW, called res_counter_charge() about | |
2332 | * both res and memsw, and called css_get(). | |
2333 | */ | |
2334 | static int mem_cgroup_move_swap_account(swp_entry_t entry, | |
483c30b5 | 2335 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
02491447 DN |
2336 | { |
2337 | unsigned short old_id, new_id; | |
2338 | ||
2339 | old_id = css_id(&from->css); | |
2340 | new_id = css_id(&to->css); | |
2341 | ||
2342 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { | |
02491447 | 2343 | mem_cgroup_swap_statistics(from, false); |
483c30b5 | 2344 | mem_cgroup_swap_statistics(to, true); |
02491447 | 2345 | /* |
483c30b5 DN |
2346 | * This function is only called from task migration context now. |
2347 | * It postpones res_counter and refcount handling till the end | |
2348 | * of task migration(mem_cgroup_clear_mc()) for performance | |
2349 | * improvement. But we cannot postpone mem_cgroup_get(to) | |
2350 | * because if the process that has been moved to @to does | |
2351 | * swap-in, the refcount of @to might be decreased to 0. | |
02491447 | 2352 | */ |
02491447 | 2353 | mem_cgroup_get(to); |
483c30b5 DN |
2354 | if (need_fixup) { |
2355 | if (!mem_cgroup_is_root(from)) | |
2356 | res_counter_uncharge(&from->memsw, PAGE_SIZE); | |
2357 | mem_cgroup_put(from); | |
2358 | /* | |
2359 | * we charged both to->res and to->memsw, so we should | |
2360 | * uncharge to->res. | |
2361 | */ | |
2362 | if (!mem_cgroup_is_root(to)) | |
2363 | res_counter_uncharge(&to->res, PAGE_SIZE); | |
2364 | css_put(&to->css); | |
2365 | } | |
02491447 DN |
2366 | return 0; |
2367 | } | |
2368 | return -EINVAL; | |
2369 | } | |
2370 | #else | |
2371 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, | |
483c30b5 | 2372 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
02491447 DN |
2373 | { |
2374 | return -EINVAL; | |
2375 | } | |
8c7c6e34 | 2376 | #endif |
d13d1443 | 2377 | |
ae41be37 | 2378 | /* |
01b1ae63 KH |
2379 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
2380 | * page belongs to. | |
ae41be37 | 2381 | */ |
01b1ae63 | 2382 | int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr) |
ae41be37 KH |
2383 | { |
2384 | struct page_cgroup *pc; | |
e8589cc1 | 2385 | struct mem_cgroup *mem = NULL; |
e8589cc1 | 2386 | int ret = 0; |
8869b8f6 | 2387 | |
f8d66542 | 2388 | if (mem_cgroup_disabled()) |
4077960e BS |
2389 | return 0; |
2390 | ||
52d4b9ac KH |
2391 | pc = lookup_page_cgroup(page); |
2392 | lock_page_cgroup(pc); | |
2393 | if (PageCgroupUsed(pc)) { | |
e8589cc1 KH |
2394 | mem = pc->mem_cgroup; |
2395 | css_get(&mem->css); | |
e8589cc1 | 2396 | } |
52d4b9ac | 2397 | unlock_page_cgroup(pc); |
01b1ae63 | 2398 | |
e8589cc1 | 2399 | if (mem) { |
f64c3f54 BS |
2400 | ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false, |
2401 | page); | |
e8589cc1 KH |
2402 | css_put(&mem->css); |
2403 | } | |
01b1ae63 | 2404 | *ptr = mem; |
e8589cc1 | 2405 | return ret; |
ae41be37 | 2406 | } |
8869b8f6 | 2407 | |
69029cd5 | 2408 | /* remove redundant charge if migration failed*/ |
01b1ae63 KH |
2409 | void mem_cgroup_end_migration(struct mem_cgroup *mem, |
2410 | struct page *oldpage, struct page *newpage) | |
ae41be37 | 2411 | { |
01b1ae63 KH |
2412 | struct page *target, *unused; |
2413 | struct page_cgroup *pc; | |
2414 | enum charge_type ctype; | |
2415 | ||
2416 | if (!mem) | |
2417 | return; | |
88703267 | 2418 | cgroup_exclude_rmdir(&mem->css); |
01b1ae63 KH |
2419 | /* at migration success, oldpage->mapping is NULL. */ |
2420 | if (oldpage->mapping) { | |
2421 | target = oldpage; | |
2422 | unused = NULL; | |
2423 | } else { | |
2424 | target = newpage; | |
2425 | unused = oldpage; | |
2426 | } | |
2427 | ||
2428 | if (PageAnon(target)) | |
2429 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; | |
2430 | else if (page_is_file_cache(target)) | |
2431 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; | |
2432 | else | |
2433 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; | |
2434 | ||
2435 | /* unused page is not on radix-tree now. */ | |
d13d1443 | 2436 | if (unused) |
01b1ae63 KH |
2437 | __mem_cgroup_uncharge_common(unused, ctype); |
2438 | ||
2439 | pc = lookup_page_cgroup(target); | |
69029cd5 | 2440 | /* |
01b1ae63 KH |
2441 | * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup. |
2442 | * So, double-counting is effectively avoided. | |
2443 | */ | |
2444 | __mem_cgroup_commit_charge(mem, pc, ctype); | |
2445 | ||
2446 | /* | |
2447 | * Both of oldpage and newpage are still under lock_page(). | |
2448 | * Then, we don't have to care about race in radix-tree. | |
2449 | * But we have to be careful that this page is unmapped or not. | |
2450 | * | |
2451 | * There is a case for !page_mapped(). At the start of | |
2452 | * migration, oldpage was mapped. But now, it's zapped. | |
2453 | * But we know *target* page is not freed/reused under us. | |
2454 | * mem_cgroup_uncharge_page() does all necessary checks. | |
69029cd5 | 2455 | */ |
01b1ae63 KH |
2456 | if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED) |
2457 | mem_cgroup_uncharge_page(target); | |
88703267 KH |
2458 | /* |
2459 | * At migration, we may charge account against cgroup which has no tasks | |
2460 | * So, rmdir()->pre_destroy() can be called while we do this charge. | |
2461 | * In that case, we need to call pre_destroy() again. check it here. | |
2462 | */ | |
2463 | cgroup_release_and_wakeup_rmdir(&mem->css); | |
ae41be37 | 2464 | } |
78fb7466 | 2465 | |
c9b0ed51 | 2466 | /* |
ae3abae6 DN |
2467 | * A call to try to shrink memory usage on charge failure at shmem's swapin. |
2468 | * Calling hierarchical_reclaim is not enough because we should update | |
2469 | * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM. | |
2470 | * Moreover considering hierarchy, we should reclaim from the mem_over_limit, | |
2471 | * not from the memcg which this page would be charged to. | |
2472 | * try_charge_swapin does all of these works properly. | |
c9b0ed51 | 2473 | */ |
ae3abae6 | 2474 | int mem_cgroup_shmem_charge_fallback(struct page *page, |
b5a84319 KH |
2475 | struct mm_struct *mm, |
2476 | gfp_t gfp_mask) | |
c9b0ed51 | 2477 | { |
b5a84319 | 2478 | struct mem_cgroup *mem = NULL; |
ae3abae6 | 2479 | int ret; |
c9b0ed51 | 2480 | |
f8d66542 | 2481 | if (mem_cgroup_disabled()) |
cede86ac | 2482 | return 0; |
c9b0ed51 | 2483 | |
ae3abae6 DN |
2484 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
2485 | if (!ret) | |
2486 | mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */ | |
c9b0ed51 | 2487 | |
ae3abae6 | 2488 | return ret; |
c9b0ed51 KH |
2489 | } |
2490 | ||
8c7c6e34 KH |
2491 | static DEFINE_MUTEX(set_limit_mutex); |
2492 | ||
d38d2a75 | 2493 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
8c7c6e34 | 2494 | unsigned long long val) |
628f4235 | 2495 | { |
81d39c20 | 2496 | int retry_count; |
8c7c6e34 | 2497 | u64 memswlimit; |
628f4235 | 2498 | int ret = 0; |
81d39c20 KH |
2499 | int children = mem_cgroup_count_children(memcg); |
2500 | u64 curusage, oldusage; | |
2501 | ||
2502 | /* | |
2503 | * For keeping hierarchical_reclaim simple, how long we should retry | |
2504 | * is depends on callers. We set our retry-count to be function | |
2505 | * of # of children which we should visit in this loop. | |
2506 | */ | |
2507 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; | |
2508 | ||
2509 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); | |
628f4235 | 2510 | |
8c7c6e34 | 2511 | while (retry_count) { |
628f4235 KH |
2512 | if (signal_pending(current)) { |
2513 | ret = -EINTR; | |
2514 | break; | |
2515 | } | |
8c7c6e34 KH |
2516 | /* |
2517 | * Rather than hide all in some function, I do this in | |
2518 | * open coded manner. You see what this really does. | |
2519 | * We have to guarantee mem->res.limit < mem->memsw.limit. | |
2520 | */ | |
2521 | mutex_lock(&set_limit_mutex); | |
2522 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
2523 | if (memswlimit < val) { | |
2524 | ret = -EINVAL; | |
2525 | mutex_unlock(&set_limit_mutex); | |
628f4235 KH |
2526 | break; |
2527 | } | |
8c7c6e34 | 2528 | ret = res_counter_set_limit(&memcg->res, val); |
22a668d7 KH |
2529 | if (!ret) { |
2530 | if (memswlimit == val) | |
2531 | memcg->memsw_is_minimum = true; | |
2532 | else | |
2533 | memcg->memsw_is_minimum = false; | |
2534 | } | |
8c7c6e34 KH |
2535 | mutex_unlock(&set_limit_mutex); |
2536 | ||
2537 | if (!ret) | |
2538 | break; | |
2539 | ||
aa20d489 | 2540 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
4e416953 | 2541 | MEM_CGROUP_RECLAIM_SHRINK); |
81d39c20 KH |
2542 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
2543 | /* Usage is reduced ? */ | |
2544 | if (curusage >= oldusage) | |
2545 | retry_count--; | |
2546 | else | |
2547 | oldusage = curusage; | |
8c7c6e34 | 2548 | } |
14797e23 | 2549 | |
8c7c6e34 KH |
2550 | return ret; |
2551 | } | |
2552 | ||
338c8431 LZ |
2553 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
2554 | unsigned long long val) | |
8c7c6e34 | 2555 | { |
81d39c20 | 2556 | int retry_count; |
8c7c6e34 | 2557 | u64 memlimit, oldusage, curusage; |
81d39c20 KH |
2558 | int children = mem_cgroup_count_children(memcg); |
2559 | int ret = -EBUSY; | |
8c7c6e34 | 2560 | |
81d39c20 KH |
2561 | /* see mem_cgroup_resize_res_limit */ |
2562 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; | |
2563 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); | |
8c7c6e34 KH |
2564 | while (retry_count) { |
2565 | if (signal_pending(current)) { | |
2566 | ret = -EINTR; | |
2567 | break; | |
2568 | } | |
2569 | /* | |
2570 | * Rather than hide all in some function, I do this in | |
2571 | * open coded manner. You see what this really does. | |
2572 | * We have to guarantee mem->res.limit < mem->memsw.limit. | |
2573 | */ | |
2574 | mutex_lock(&set_limit_mutex); | |
2575 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
2576 | if (memlimit > val) { | |
2577 | ret = -EINVAL; | |
2578 | mutex_unlock(&set_limit_mutex); | |
2579 | break; | |
2580 | } | |
2581 | ret = res_counter_set_limit(&memcg->memsw, val); | |
22a668d7 KH |
2582 | if (!ret) { |
2583 | if (memlimit == val) | |
2584 | memcg->memsw_is_minimum = true; | |
2585 | else | |
2586 | memcg->memsw_is_minimum = false; | |
2587 | } | |
8c7c6e34 KH |
2588 | mutex_unlock(&set_limit_mutex); |
2589 | ||
2590 | if (!ret) | |
2591 | break; | |
2592 | ||
4e416953 | 2593 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
75822b44 BS |
2594 | MEM_CGROUP_RECLAIM_NOSWAP | |
2595 | MEM_CGROUP_RECLAIM_SHRINK); | |
8c7c6e34 | 2596 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
81d39c20 | 2597 | /* Usage is reduced ? */ |
8c7c6e34 | 2598 | if (curusage >= oldusage) |
628f4235 | 2599 | retry_count--; |
81d39c20 KH |
2600 | else |
2601 | oldusage = curusage; | |
628f4235 KH |
2602 | } |
2603 | return ret; | |
2604 | } | |
2605 | ||
4e416953 BS |
2606 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
2607 | gfp_t gfp_mask, int nid, | |
2608 | int zid) | |
2609 | { | |
2610 | unsigned long nr_reclaimed = 0; | |
2611 | struct mem_cgroup_per_zone *mz, *next_mz = NULL; | |
2612 | unsigned long reclaimed; | |
2613 | int loop = 0; | |
2614 | struct mem_cgroup_tree_per_zone *mctz; | |
ef8745c1 | 2615 | unsigned long long excess; |
4e416953 BS |
2616 | |
2617 | if (order > 0) | |
2618 | return 0; | |
2619 | ||
2620 | mctz = soft_limit_tree_node_zone(nid, zid); | |
2621 | /* | |
2622 | * This loop can run a while, specially if mem_cgroup's continuously | |
2623 | * keep exceeding their soft limit and putting the system under | |
2624 | * pressure | |
2625 | */ | |
2626 | do { | |
2627 | if (next_mz) | |
2628 | mz = next_mz; | |
2629 | else | |
2630 | mz = mem_cgroup_largest_soft_limit_node(mctz); | |
2631 | if (!mz) | |
2632 | break; | |
2633 | ||
2634 | reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone, | |
2635 | gfp_mask, | |
2636 | MEM_CGROUP_RECLAIM_SOFT); | |
2637 | nr_reclaimed += reclaimed; | |
2638 | spin_lock(&mctz->lock); | |
2639 | ||
2640 | /* | |
2641 | * If we failed to reclaim anything from this memory cgroup | |
2642 | * it is time to move on to the next cgroup | |
2643 | */ | |
2644 | next_mz = NULL; | |
2645 | if (!reclaimed) { | |
2646 | do { | |
2647 | /* | |
2648 | * Loop until we find yet another one. | |
2649 | * | |
2650 | * By the time we get the soft_limit lock | |
2651 | * again, someone might have aded the | |
2652 | * group back on the RB tree. Iterate to | |
2653 | * make sure we get a different mem. | |
2654 | * mem_cgroup_largest_soft_limit_node returns | |
2655 | * NULL if no other cgroup is present on | |
2656 | * the tree | |
2657 | */ | |
2658 | next_mz = | |
2659 | __mem_cgroup_largest_soft_limit_node(mctz); | |
2660 | if (next_mz == mz) { | |
2661 | css_put(&next_mz->mem->css); | |
2662 | next_mz = NULL; | |
2663 | } else /* next_mz == NULL or other memcg */ | |
2664 | break; | |
2665 | } while (1); | |
2666 | } | |
4e416953 | 2667 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
ef8745c1 | 2668 | excess = res_counter_soft_limit_excess(&mz->mem->res); |
4e416953 BS |
2669 | /* |
2670 | * One school of thought says that we should not add | |
2671 | * back the node to the tree if reclaim returns 0. | |
2672 | * But our reclaim could return 0, simply because due | |
2673 | * to priority we are exposing a smaller subset of | |
2674 | * memory to reclaim from. Consider this as a longer | |
2675 | * term TODO. | |
2676 | */ | |
ef8745c1 KH |
2677 | /* If excess == 0, no tree ops */ |
2678 | __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess); | |
4e416953 BS |
2679 | spin_unlock(&mctz->lock); |
2680 | css_put(&mz->mem->css); | |
2681 | loop++; | |
2682 | /* | |
2683 | * Could not reclaim anything and there are no more | |
2684 | * mem cgroups to try or we seem to be looping without | |
2685 | * reclaiming anything. | |
2686 | */ | |
2687 | if (!nr_reclaimed && | |
2688 | (next_mz == NULL || | |
2689 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) | |
2690 | break; | |
2691 | } while (!nr_reclaimed); | |
2692 | if (next_mz) | |
2693 | css_put(&next_mz->mem->css); | |
2694 | return nr_reclaimed; | |
2695 | } | |
2696 | ||
cc847582 KH |
2697 | /* |
2698 | * This routine traverse page_cgroup in given list and drop them all. | |
cc847582 KH |
2699 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
2700 | */ | |
f817ed48 | 2701 | static int mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
08e552c6 | 2702 | int node, int zid, enum lru_list lru) |
cc847582 | 2703 | { |
08e552c6 KH |
2704 | struct zone *zone; |
2705 | struct mem_cgroup_per_zone *mz; | |
f817ed48 | 2706 | struct page_cgroup *pc, *busy; |
08e552c6 | 2707 | unsigned long flags, loop; |
072c56c1 | 2708 | struct list_head *list; |
f817ed48 | 2709 | int ret = 0; |
072c56c1 | 2710 | |
08e552c6 KH |
2711 | zone = &NODE_DATA(node)->node_zones[zid]; |
2712 | mz = mem_cgroup_zoneinfo(mem, node, zid); | |
b69408e8 | 2713 | list = &mz->lists[lru]; |
cc847582 | 2714 | |
f817ed48 KH |
2715 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
2716 | /* give some margin against EBUSY etc...*/ | |
2717 | loop += 256; | |
2718 | busy = NULL; | |
2719 | while (loop--) { | |
2720 | ret = 0; | |
08e552c6 | 2721 | spin_lock_irqsave(&zone->lru_lock, flags); |
f817ed48 | 2722 | if (list_empty(list)) { |
08e552c6 | 2723 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
52d4b9ac | 2724 | break; |
f817ed48 KH |
2725 | } |
2726 | pc = list_entry(list->prev, struct page_cgroup, lru); | |
2727 | if (busy == pc) { | |
2728 | list_move(&pc->lru, list); | |
648bcc77 | 2729 | busy = NULL; |
08e552c6 | 2730 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
f817ed48 KH |
2731 | continue; |
2732 | } | |
08e552c6 | 2733 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
f817ed48 | 2734 | |
2c26fdd7 | 2735 | ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL); |
f817ed48 | 2736 | if (ret == -ENOMEM) |
52d4b9ac | 2737 | break; |
f817ed48 KH |
2738 | |
2739 | if (ret == -EBUSY || ret == -EINVAL) { | |
2740 | /* found lock contention or "pc" is obsolete. */ | |
2741 | busy = pc; | |
2742 | cond_resched(); | |
2743 | } else | |
2744 | busy = NULL; | |
cc847582 | 2745 | } |
08e552c6 | 2746 | |
f817ed48 KH |
2747 | if (!ret && !list_empty(list)) |
2748 | return -EBUSY; | |
2749 | return ret; | |
cc847582 KH |
2750 | } |
2751 | ||
2752 | /* | |
2753 | * make mem_cgroup's charge to be 0 if there is no task. | |
2754 | * This enables deleting this mem_cgroup. | |
2755 | */ | |
c1e862c1 | 2756 | static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all) |
cc847582 | 2757 | { |
f817ed48 KH |
2758 | int ret; |
2759 | int node, zid, shrink; | |
2760 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; | |
c1e862c1 | 2761 | struct cgroup *cgrp = mem->css.cgroup; |
8869b8f6 | 2762 | |
cc847582 | 2763 | css_get(&mem->css); |
f817ed48 KH |
2764 | |
2765 | shrink = 0; | |
c1e862c1 KH |
2766 | /* should free all ? */ |
2767 | if (free_all) | |
2768 | goto try_to_free; | |
f817ed48 | 2769 | move_account: |
fce66477 | 2770 | do { |
f817ed48 | 2771 | ret = -EBUSY; |
c1e862c1 KH |
2772 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
2773 | goto out; | |
2774 | ret = -EINTR; | |
2775 | if (signal_pending(current)) | |
cc847582 | 2776 | goto out; |
52d4b9ac KH |
2777 | /* This is for making all *used* pages to be on LRU. */ |
2778 | lru_add_drain_all(); | |
cdec2e42 | 2779 | drain_all_stock_sync(); |
f817ed48 | 2780 | ret = 0; |
299b4eaa | 2781 | for_each_node_state(node, N_HIGH_MEMORY) { |
f817ed48 | 2782 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
b69408e8 | 2783 | enum lru_list l; |
f817ed48 KH |
2784 | for_each_lru(l) { |
2785 | ret = mem_cgroup_force_empty_list(mem, | |
08e552c6 | 2786 | node, zid, l); |
f817ed48 KH |
2787 | if (ret) |
2788 | break; | |
2789 | } | |
1ecaab2b | 2790 | } |
f817ed48 KH |
2791 | if (ret) |
2792 | break; | |
2793 | } | |
2794 | /* it seems parent cgroup doesn't have enough mem */ | |
2795 | if (ret == -ENOMEM) | |
2796 | goto try_to_free; | |
52d4b9ac | 2797 | cond_resched(); |
fce66477 DN |
2798 | /* "ret" should also be checked to ensure all lists are empty. */ |
2799 | } while (mem->res.usage > 0 || ret); | |
cc847582 KH |
2800 | out: |
2801 | css_put(&mem->css); | |
2802 | return ret; | |
f817ed48 KH |
2803 | |
2804 | try_to_free: | |
c1e862c1 KH |
2805 | /* returns EBUSY if there is a task or if we come here twice. */ |
2806 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { | |
f817ed48 KH |
2807 | ret = -EBUSY; |
2808 | goto out; | |
2809 | } | |
c1e862c1 KH |
2810 | /* we call try-to-free pages for make this cgroup empty */ |
2811 | lru_add_drain_all(); | |
f817ed48 KH |
2812 | /* try to free all pages in this cgroup */ |
2813 | shrink = 1; | |
2814 | while (nr_retries && mem->res.usage > 0) { | |
2815 | int progress; | |
c1e862c1 KH |
2816 | |
2817 | if (signal_pending(current)) { | |
2818 | ret = -EINTR; | |
2819 | goto out; | |
2820 | } | |
a7885eb8 KM |
2821 | progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL, |
2822 | false, get_swappiness(mem)); | |
c1e862c1 | 2823 | if (!progress) { |
f817ed48 | 2824 | nr_retries--; |
c1e862c1 | 2825 | /* maybe some writeback is necessary */ |
8aa7e847 | 2826 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
c1e862c1 | 2827 | } |
f817ed48 KH |
2828 | |
2829 | } | |
08e552c6 | 2830 | lru_add_drain(); |
f817ed48 | 2831 | /* try move_account...there may be some *locked* pages. */ |
fce66477 | 2832 | goto move_account; |
cc847582 KH |
2833 | } |
2834 | ||
c1e862c1 KH |
2835 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
2836 | { | |
2837 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); | |
2838 | } | |
2839 | ||
2840 | ||
18f59ea7 BS |
2841 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
2842 | { | |
2843 | return mem_cgroup_from_cont(cont)->use_hierarchy; | |
2844 | } | |
2845 | ||
2846 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, | |
2847 | u64 val) | |
2848 | { | |
2849 | int retval = 0; | |
2850 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); | |
2851 | struct cgroup *parent = cont->parent; | |
2852 | struct mem_cgroup *parent_mem = NULL; | |
2853 | ||
2854 | if (parent) | |
2855 | parent_mem = mem_cgroup_from_cont(parent); | |
2856 | ||
2857 | cgroup_lock(); | |
2858 | /* | |
af901ca1 | 2859 | * If parent's use_hierarchy is set, we can't make any modifications |
18f59ea7 BS |
2860 | * in the child subtrees. If it is unset, then the change can |
2861 | * occur, provided the current cgroup has no children. | |
2862 | * | |
2863 | * For the root cgroup, parent_mem is NULL, we allow value to be | |
2864 | * set if there are no children. | |
2865 | */ | |
2866 | if ((!parent_mem || !parent_mem->use_hierarchy) && | |
2867 | (val == 1 || val == 0)) { | |
2868 | if (list_empty(&cont->children)) | |
2869 | mem->use_hierarchy = val; | |
2870 | else | |
2871 | retval = -EBUSY; | |
2872 | } else | |
2873 | retval = -EINVAL; | |
2874 | cgroup_unlock(); | |
2875 | ||
2876 | return retval; | |
2877 | } | |
2878 | ||
0c3e73e8 BS |
2879 | struct mem_cgroup_idx_data { |
2880 | s64 val; | |
2881 | enum mem_cgroup_stat_index idx; | |
2882 | }; | |
2883 | ||
2884 | static int | |
2885 | mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data) | |
2886 | { | |
2887 | struct mem_cgroup_idx_data *d = data; | |
2888 | d->val += mem_cgroup_read_stat(&mem->stat, d->idx); | |
2889 | return 0; | |
2890 | } | |
2891 | ||
2892 | static void | |
2893 | mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem, | |
2894 | enum mem_cgroup_stat_index idx, s64 *val) | |
2895 | { | |
2896 | struct mem_cgroup_idx_data d; | |
2897 | d.idx = idx; | |
2898 | d.val = 0; | |
2899 | mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat); | |
2900 | *val = d.val; | |
2901 | } | |
2902 | ||
104f3928 KS |
2903 | static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap) |
2904 | { | |
2905 | u64 idx_val, val; | |
2906 | ||
2907 | if (!mem_cgroup_is_root(mem)) { | |
2908 | if (!swap) | |
2909 | return res_counter_read_u64(&mem->res, RES_USAGE); | |
2910 | else | |
2911 | return res_counter_read_u64(&mem->memsw, RES_USAGE); | |
2912 | } | |
2913 | ||
2914 | mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE, &idx_val); | |
2915 | val = idx_val; | |
2916 | mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS, &idx_val); | |
2917 | val += idx_val; | |
2918 | ||
2919 | if (swap) { | |
2920 | mem_cgroup_get_recursive_idx_stat(mem, | |
2921 | MEM_CGROUP_STAT_SWAPOUT, &idx_val); | |
2922 | val += idx_val; | |
2923 | } | |
2924 | ||
2925 | return val << PAGE_SHIFT; | |
2926 | } | |
2927 | ||
2c3daa72 | 2928 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
8cdea7c0 | 2929 | { |
8c7c6e34 | 2930 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
104f3928 | 2931 | u64 val; |
8c7c6e34 KH |
2932 | int type, name; |
2933 | ||
2934 | type = MEMFILE_TYPE(cft->private); | |
2935 | name = MEMFILE_ATTR(cft->private); | |
2936 | switch (type) { | |
2937 | case _MEM: | |
104f3928 KS |
2938 | if (name == RES_USAGE) |
2939 | val = mem_cgroup_usage(mem, false); | |
2940 | else | |
0c3e73e8 | 2941 | val = res_counter_read_u64(&mem->res, name); |
8c7c6e34 KH |
2942 | break; |
2943 | case _MEMSWAP: | |
104f3928 KS |
2944 | if (name == RES_USAGE) |
2945 | val = mem_cgroup_usage(mem, true); | |
2946 | else | |
0c3e73e8 | 2947 | val = res_counter_read_u64(&mem->memsw, name); |
8c7c6e34 KH |
2948 | break; |
2949 | default: | |
2950 | BUG(); | |
2951 | break; | |
2952 | } | |
2953 | return val; | |
8cdea7c0 | 2954 | } |
628f4235 KH |
2955 | /* |
2956 | * The user of this function is... | |
2957 | * RES_LIMIT. | |
2958 | */ | |
856c13aa PM |
2959 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
2960 | const char *buffer) | |
8cdea7c0 | 2961 | { |
628f4235 | 2962 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
8c7c6e34 | 2963 | int type, name; |
628f4235 KH |
2964 | unsigned long long val; |
2965 | int ret; | |
2966 | ||
8c7c6e34 KH |
2967 | type = MEMFILE_TYPE(cft->private); |
2968 | name = MEMFILE_ATTR(cft->private); | |
2969 | switch (name) { | |
628f4235 | 2970 | case RES_LIMIT: |
4b3bde4c BS |
2971 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
2972 | ret = -EINVAL; | |
2973 | break; | |
2974 | } | |
628f4235 KH |
2975 | /* This function does all necessary parse...reuse it */ |
2976 | ret = res_counter_memparse_write_strategy(buffer, &val); | |
8c7c6e34 KH |
2977 | if (ret) |
2978 | break; | |
2979 | if (type == _MEM) | |
628f4235 | 2980 | ret = mem_cgroup_resize_limit(memcg, val); |
8c7c6e34 KH |
2981 | else |
2982 | ret = mem_cgroup_resize_memsw_limit(memcg, val); | |
628f4235 | 2983 | break; |
296c81d8 BS |
2984 | case RES_SOFT_LIMIT: |
2985 | ret = res_counter_memparse_write_strategy(buffer, &val); | |
2986 | if (ret) | |
2987 | break; | |
2988 | /* | |
2989 | * For memsw, soft limits are hard to implement in terms | |
2990 | * of semantics, for now, we support soft limits for | |
2991 | * control without swap | |
2992 | */ | |
2993 | if (type == _MEM) | |
2994 | ret = res_counter_set_soft_limit(&memcg->res, val); | |
2995 | else | |
2996 | ret = -EINVAL; | |
2997 | break; | |
628f4235 KH |
2998 | default: |
2999 | ret = -EINVAL; /* should be BUG() ? */ | |
3000 | break; | |
3001 | } | |
3002 | return ret; | |
8cdea7c0 BS |
3003 | } |
3004 | ||
fee7b548 KH |
3005 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
3006 | unsigned long long *mem_limit, unsigned long long *memsw_limit) | |
3007 | { | |
3008 | struct cgroup *cgroup; | |
3009 | unsigned long long min_limit, min_memsw_limit, tmp; | |
3010 | ||
3011 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
3012 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
3013 | cgroup = memcg->css.cgroup; | |
3014 | if (!memcg->use_hierarchy) | |
3015 | goto out; | |
3016 | ||
3017 | while (cgroup->parent) { | |
3018 | cgroup = cgroup->parent; | |
3019 | memcg = mem_cgroup_from_cont(cgroup); | |
3020 | if (!memcg->use_hierarchy) | |
3021 | break; | |
3022 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
3023 | min_limit = min(min_limit, tmp); | |
3024 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
3025 | min_memsw_limit = min(min_memsw_limit, tmp); | |
3026 | } | |
3027 | out: | |
3028 | *mem_limit = min_limit; | |
3029 | *memsw_limit = min_memsw_limit; | |
3030 | return; | |
3031 | } | |
3032 | ||
29f2a4da | 3033 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
c84872e1 PE |
3034 | { |
3035 | struct mem_cgroup *mem; | |
8c7c6e34 | 3036 | int type, name; |
c84872e1 PE |
3037 | |
3038 | mem = mem_cgroup_from_cont(cont); | |
8c7c6e34 KH |
3039 | type = MEMFILE_TYPE(event); |
3040 | name = MEMFILE_ATTR(event); | |
3041 | switch (name) { | |
29f2a4da | 3042 | case RES_MAX_USAGE: |
8c7c6e34 KH |
3043 | if (type == _MEM) |
3044 | res_counter_reset_max(&mem->res); | |
3045 | else | |
3046 | res_counter_reset_max(&mem->memsw); | |
29f2a4da PE |
3047 | break; |
3048 | case RES_FAILCNT: | |
8c7c6e34 KH |
3049 | if (type == _MEM) |
3050 | res_counter_reset_failcnt(&mem->res); | |
3051 | else | |
3052 | res_counter_reset_failcnt(&mem->memsw); | |
29f2a4da PE |
3053 | break; |
3054 | } | |
f64c3f54 | 3055 | |
85cc59db | 3056 | return 0; |
c84872e1 PE |
3057 | } |
3058 | ||
7dc74be0 DN |
3059 | static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp, |
3060 | struct cftype *cft) | |
3061 | { | |
3062 | return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate; | |
3063 | } | |
3064 | ||
02491447 | 3065 | #ifdef CONFIG_MMU |
7dc74be0 DN |
3066 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
3067 | struct cftype *cft, u64 val) | |
3068 | { | |
3069 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp); | |
3070 | ||
3071 | if (val >= (1 << NR_MOVE_TYPE)) | |
3072 | return -EINVAL; | |
3073 | /* | |
3074 | * We check this value several times in both in can_attach() and | |
3075 | * attach(), so we need cgroup lock to prevent this value from being | |
3076 | * inconsistent. | |
3077 | */ | |
3078 | cgroup_lock(); | |
3079 | mem->move_charge_at_immigrate = val; | |
3080 | cgroup_unlock(); | |
3081 | ||
3082 | return 0; | |
3083 | } | |
02491447 DN |
3084 | #else |
3085 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, | |
3086 | struct cftype *cft, u64 val) | |
3087 | { | |
3088 | return -ENOSYS; | |
3089 | } | |
3090 | #endif | |
7dc74be0 | 3091 | |
14067bb3 KH |
3092 | |
3093 | /* For read statistics */ | |
3094 | enum { | |
3095 | MCS_CACHE, | |
3096 | MCS_RSS, | |
d8046582 | 3097 | MCS_FILE_MAPPED, |
14067bb3 KH |
3098 | MCS_PGPGIN, |
3099 | MCS_PGPGOUT, | |
1dd3a273 | 3100 | MCS_SWAP, |
14067bb3 KH |
3101 | MCS_INACTIVE_ANON, |
3102 | MCS_ACTIVE_ANON, | |
3103 | MCS_INACTIVE_FILE, | |
3104 | MCS_ACTIVE_FILE, | |
3105 | MCS_UNEVICTABLE, | |
3106 | NR_MCS_STAT, | |
3107 | }; | |
3108 | ||
3109 | struct mcs_total_stat { | |
3110 | s64 stat[NR_MCS_STAT]; | |
d2ceb9b7 KH |
3111 | }; |
3112 | ||
14067bb3 KH |
3113 | struct { |
3114 | char *local_name; | |
3115 | char *total_name; | |
3116 | } memcg_stat_strings[NR_MCS_STAT] = { | |
3117 | {"cache", "total_cache"}, | |
3118 | {"rss", "total_rss"}, | |
d69b042f | 3119 | {"mapped_file", "total_mapped_file"}, |
14067bb3 KH |
3120 | {"pgpgin", "total_pgpgin"}, |
3121 | {"pgpgout", "total_pgpgout"}, | |
1dd3a273 | 3122 | {"swap", "total_swap"}, |
14067bb3 KH |
3123 | {"inactive_anon", "total_inactive_anon"}, |
3124 | {"active_anon", "total_active_anon"}, | |
3125 | {"inactive_file", "total_inactive_file"}, | |
3126 | {"active_file", "total_active_file"}, | |
3127 | {"unevictable", "total_unevictable"} | |
3128 | }; | |
3129 | ||
3130 | ||
3131 | static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data) | |
3132 | { | |
3133 | struct mcs_total_stat *s = data; | |
3134 | s64 val; | |
3135 | ||
3136 | /* per cpu stat */ | |
3137 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE); | |
3138 | s->stat[MCS_CACHE] += val * PAGE_SIZE; | |
3139 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS); | |
3140 | s->stat[MCS_RSS] += val * PAGE_SIZE; | |
d8046582 KH |
3141 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_FILE_MAPPED); |
3142 | s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE; | |
14067bb3 KH |
3143 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT); |
3144 | s->stat[MCS_PGPGIN] += val; | |
3145 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT); | |
3146 | s->stat[MCS_PGPGOUT] += val; | |
1dd3a273 DN |
3147 | if (do_swap_account) { |
3148 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT); | |
3149 | s->stat[MCS_SWAP] += val * PAGE_SIZE; | |
3150 | } | |
14067bb3 KH |
3151 | |
3152 | /* per zone stat */ | |
3153 | val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON); | |
3154 | s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE; | |
3155 | val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON); | |
3156 | s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE; | |
3157 | val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE); | |
3158 | s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE; | |
3159 | val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE); | |
3160 | s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE; | |
3161 | val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE); | |
3162 | s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE; | |
3163 | return 0; | |
3164 | } | |
3165 | ||
3166 | static void | |
3167 | mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s) | |
3168 | { | |
3169 | mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat); | |
3170 | } | |
3171 | ||
c64745cf PM |
3172 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
3173 | struct cgroup_map_cb *cb) | |
d2ceb9b7 | 3174 | { |
d2ceb9b7 | 3175 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
14067bb3 | 3176 | struct mcs_total_stat mystat; |
d2ceb9b7 KH |
3177 | int i; |
3178 | ||
14067bb3 KH |
3179 | memset(&mystat, 0, sizeof(mystat)); |
3180 | mem_cgroup_get_local_stat(mem_cont, &mystat); | |
d2ceb9b7 | 3181 | |
1dd3a273 DN |
3182 | for (i = 0; i < NR_MCS_STAT; i++) { |
3183 | if (i == MCS_SWAP && !do_swap_account) | |
3184 | continue; | |
14067bb3 | 3185 | cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]); |
1dd3a273 | 3186 | } |
7b854121 | 3187 | |
14067bb3 | 3188 | /* Hierarchical information */ |
fee7b548 KH |
3189 | { |
3190 | unsigned long long limit, memsw_limit; | |
3191 | memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit); | |
3192 | cb->fill(cb, "hierarchical_memory_limit", limit); | |
3193 | if (do_swap_account) | |
3194 | cb->fill(cb, "hierarchical_memsw_limit", memsw_limit); | |
3195 | } | |
7f016ee8 | 3196 | |
14067bb3 KH |
3197 | memset(&mystat, 0, sizeof(mystat)); |
3198 | mem_cgroup_get_total_stat(mem_cont, &mystat); | |
1dd3a273 DN |
3199 | for (i = 0; i < NR_MCS_STAT; i++) { |
3200 | if (i == MCS_SWAP && !do_swap_account) | |
3201 | continue; | |
14067bb3 | 3202 | cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]); |
1dd3a273 | 3203 | } |
14067bb3 | 3204 | |
7f016ee8 | 3205 | #ifdef CONFIG_DEBUG_VM |
c772be93 | 3206 | cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL)); |
7f016ee8 KM |
3207 | |
3208 | { | |
3209 | int nid, zid; | |
3210 | struct mem_cgroup_per_zone *mz; | |
3211 | unsigned long recent_rotated[2] = {0, 0}; | |
3212 | unsigned long recent_scanned[2] = {0, 0}; | |
3213 | ||
3214 | for_each_online_node(nid) | |
3215 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { | |
3216 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); | |
3217 | ||
3218 | recent_rotated[0] += | |
3219 | mz->reclaim_stat.recent_rotated[0]; | |
3220 | recent_rotated[1] += | |
3221 | mz->reclaim_stat.recent_rotated[1]; | |
3222 | recent_scanned[0] += | |
3223 | mz->reclaim_stat.recent_scanned[0]; | |
3224 | recent_scanned[1] += | |
3225 | mz->reclaim_stat.recent_scanned[1]; | |
3226 | } | |
3227 | cb->fill(cb, "recent_rotated_anon", recent_rotated[0]); | |
3228 | cb->fill(cb, "recent_rotated_file", recent_rotated[1]); | |
3229 | cb->fill(cb, "recent_scanned_anon", recent_scanned[0]); | |
3230 | cb->fill(cb, "recent_scanned_file", recent_scanned[1]); | |
3231 | } | |
3232 | #endif | |
3233 | ||
d2ceb9b7 KH |
3234 | return 0; |
3235 | } | |
3236 | ||
a7885eb8 KM |
3237 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
3238 | { | |
3239 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
3240 | ||
3241 | return get_swappiness(memcg); | |
3242 | } | |
3243 | ||
3244 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, | |
3245 | u64 val) | |
3246 | { | |
3247 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
3248 | struct mem_cgroup *parent; | |
068b38c1 | 3249 | |
a7885eb8 KM |
3250 | if (val > 100) |
3251 | return -EINVAL; | |
3252 | ||
3253 | if (cgrp->parent == NULL) | |
3254 | return -EINVAL; | |
3255 | ||
3256 | parent = mem_cgroup_from_cont(cgrp->parent); | |
068b38c1 LZ |
3257 | |
3258 | cgroup_lock(); | |
3259 | ||
a7885eb8 KM |
3260 | /* If under hierarchy, only empty-root can set this value */ |
3261 | if ((parent->use_hierarchy) || | |
068b38c1 LZ |
3262 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
3263 | cgroup_unlock(); | |
a7885eb8 | 3264 | return -EINVAL; |
068b38c1 | 3265 | } |
a7885eb8 KM |
3266 | |
3267 | spin_lock(&memcg->reclaim_param_lock); | |
3268 | memcg->swappiness = val; | |
3269 | spin_unlock(&memcg->reclaim_param_lock); | |
3270 | ||
068b38c1 LZ |
3271 | cgroup_unlock(); |
3272 | ||
a7885eb8 KM |
3273 | return 0; |
3274 | } | |
3275 | ||
2e72b634 KS |
3276 | static bool mem_cgroup_threshold_check(struct mem_cgroup *mem) |
3277 | { | |
3278 | bool ret = false; | |
3279 | int cpu; | |
3280 | s64 val; | |
3281 | struct mem_cgroup_stat_cpu *cpustat; | |
3282 | ||
3283 | cpu = get_cpu(); | |
3284 | cpustat = &mem->stat.cpustat[cpu]; | |
3285 | val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_THRESHOLDS); | |
3286 | if (unlikely(val < 0)) { | |
3287 | __mem_cgroup_stat_set_safe(cpustat, MEM_CGROUP_STAT_THRESHOLDS, | |
3288 | THRESHOLDS_EVENTS_THRESH); | |
3289 | ret = true; | |
3290 | } | |
3291 | put_cpu(); | |
3292 | return ret; | |
3293 | } | |
3294 | ||
3295 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) | |
3296 | { | |
3297 | struct mem_cgroup_threshold_ary *t; | |
3298 | u64 usage; | |
3299 | int i; | |
3300 | ||
3301 | rcu_read_lock(); | |
3302 | if (!swap) | |
3303 | t = rcu_dereference(memcg->thresholds); | |
3304 | else | |
3305 | t = rcu_dereference(memcg->memsw_thresholds); | |
3306 | ||
3307 | if (!t) | |
3308 | goto unlock; | |
3309 | ||
3310 | usage = mem_cgroup_usage(memcg, swap); | |
3311 | ||
3312 | /* | |
3313 | * current_threshold points to threshold just below usage. | |
3314 | * If it's not true, a threshold was crossed after last | |
3315 | * call of __mem_cgroup_threshold(). | |
3316 | */ | |
3317 | i = atomic_read(&t->current_threshold); | |
3318 | ||
3319 | /* | |
3320 | * Iterate backward over array of thresholds starting from | |
3321 | * current_threshold and check if a threshold is crossed. | |
3322 | * If none of thresholds below usage is crossed, we read | |
3323 | * only one element of the array here. | |
3324 | */ | |
3325 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) | |
3326 | eventfd_signal(t->entries[i].eventfd, 1); | |
3327 | ||
3328 | /* i = current_threshold + 1 */ | |
3329 | i++; | |
3330 | ||
3331 | /* | |
3332 | * Iterate forward over array of thresholds starting from | |
3333 | * current_threshold+1 and check if a threshold is crossed. | |
3334 | * If none of thresholds above usage is crossed, we read | |
3335 | * only one element of the array here. | |
3336 | */ | |
3337 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) | |
3338 | eventfd_signal(t->entries[i].eventfd, 1); | |
3339 | ||
3340 | /* Update current_threshold */ | |
3341 | atomic_set(&t->current_threshold, i - 1); | |
3342 | unlock: | |
3343 | rcu_read_unlock(); | |
3344 | } | |
3345 | ||
3346 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) | |
3347 | { | |
3348 | __mem_cgroup_threshold(memcg, false); | |
3349 | if (do_swap_account) | |
3350 | __mem_cgroup_threshold(memcg, true); | |
3351 | } | |
3352 | ||
3353 | static int compare_thresholds(const void *a, const void *b) | |
3354 | { | |
3355 | const struct mem_cgroup_threshold *_a = a; | |
3356 | const struct mem_cgroup_threshold *_b = b; | |
3357 | ||
3358 | return _a->threshold - _b->threshold; | |
3359 | } | |
3360 | ||
3361 | static int mem_cgroup_register_event(struct cgroup *cgrp, struct cftype *cft, | |
3362 | struct eventfd_ctx *eventfd, const char *args) | |
3363 | { | |
3364 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
3365 | struct mem_cgroup_threshold_ary *thresholds, *thresholds_new; | |
3366 | int type = MEMFILE_TYPE(cft->private); | |
3367 | u64 threshold, usage; | |
3368 | int size; | |
3369 | int i, ret; | |
3370 | ||
3371 | ret = res_counter_memparse_write_strategy(args, &threshold); | |
3372 | if (ret) | |
3373 | return ret; | |
3374 | ||
3375 | mutex_lock(&memcg->thresholds_lock); | |
3376 | if (type == _MEM) | |
3377 | thresholds = memcg->thresholds; | |
3378 | else if (type == _MEMSWAP) | |
3379 | thresholds = memcg->memsw_thresholds; | |
3380 | else | |
3381 | BUG(); | |
3382 | ||
3383 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); | |
3384 | ||
3385 | /* Check if a threshold crossed before adding a new one */ | |
3386 | if (thresholds) | |
3387 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); | |
3388 | ||
3389 | if (thresholds) | |
3390 | size = thresholds->size + 1; | |
3391 | else | |
3392 | size = 1; | |
3393 | ||
3394 | /* Allocate memory for new array of thresholds */ | |
3395 | thresholds_new = kmalloc(sizeof(*thresholds_new) + | |
3396 | size * sizeof(struct mem_cgroup_threshold), | |
3397 | GFP_KERNEL); | |
3398 | if (!thresholds_new) { | |
3399 | ret = -ENOMEM; | |
3400 | goto unlock; | |
3401 | } | |
3402 | thresholds_new->size = size; | |
3403 | ||
3404 | /* Copy thresholds (if any) to new array */ | |
3405 | if (thresholds) | |
3406 | memcpy(thresholds_new->entries, thresholds->entries, | |
3407 | thresholds->size * | |
3408 | sizeof(struct mem_cgroup_threshold)); | |
3409 | /* Add new threshold */ | |
3410 | thresholds_new->entries[size - 1].eventfd = eventfd; | |
3411 | thresholds_new->entries[size - 1].threshold = threshold; | |
3412 | ||
3413 | /* Sort thresholds. Registering of new threshold isn't time-critical */ | |
3414 | sort(thresholds_new->entries, size, | |
3415 | sizeof(struct mem_cgroup_threshold), | |
3416 | compare_thresholds, NULL); | |
3417 | ||
3418 | /* Find current threshold */ | |
3419 | atomic_set(&thresholds_new->current_threshold, -1); | |
3420 | for (i = 0; i < size; i++) { | |
3421 | if (thresholds_new->entries[i].threshold < usage) { | |
3422 | /* | |
3423 | * thresholds_new->current_threshold will not be used | |
3424 | * until rcu_assign_pointer(), so it's safe to increment | |
3425 | * it here. | |
3426 | */ | |
3427 | atomic_inc(&thresholds_new->current_threshold); | |
3428 | } | |
3429 | } | |
3430 | ||
3431 | /* | |
3432 | * We need to increment refcnt to be sure that all thresholds | |
3433 | * will be unregistered before calling __mem_cgroup_free() | |
3434 | */ | |
3435 | mem_cgroup_get(memcg); | |
3436 | ||
3437 | if (type == _MEM) | |
3438 | rcu_assign_pointer(memcg->thresholds, thresholds_new); | |
3439 | else | |
3440 | rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new); | |
3441 | ||
3442 | /* To be sure that nobody uses thresholds before freeing it */ | |
3443 | synchronize_rcu(); | |
3444 | ||
3445 | kfree(thresholds); | |
3446 | unlock: | |
3447 | mutex_unlock(&memcg->thresholds_lock); | |
3448 | ||
3449 | return ret; | |
3450 | } | |
3451 | ||
3452 | static int mem_cgroup_unregister_event(struct cgroup *cgrp, struct cftype *cft, | |
3453 | struct eventfd_ctx *eventfd) | |
3454 | { | |
3455 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
3456 | struct mem_cgroup_threshold_ary *thresholds, *thresholds_new; | |
3457 | int type = MEMFILE_TYPE(cft->private); | |
3458 | u64 usage; | |
3459 | int size = 0; | |
3460 | int i, j, ret; | |
3461 | ||
3462 | mutex_lock(&memcg->thresholds_lock); | |
3463 | if (type == _MEM) | |
3464 | thresholds = memcg->thresholds; | |
3465 | else if (type == _MEMSWAP) | |
3466 | thresholds = memcg->memsw_thresholds; | |
3467 | else | |
3468 | BUG(); | |
3469 | ||
3470 | /* | |
3471 | * Something went wrong if we trying to unregister a threshold | |
3472 | * if we don't have thresholds | |
3473 | */ | |
3474 | BUG_ON(!thresholds); | |
3475 | ||
3476 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); | |
3477 | ||
3478 | /* Check if a threshold crossed before removing */ | |
3479 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); | |
3480 | ||
3481 | /* Calculate new number of threshold */ | |
3482 | for (i = 0; i < thresholds->size; i++) { | |
3483 | if (thresholds->entries[i].eventfd != eventfd) | |
3484 | size++; | |
3485 | } | |
3486 | ||
3487 | /* Set thresholds array to NULL if we don't have thresholds */ | |
3488 | if (!size) { | |
3489 | thresholds_new = NULL; | |
3490 | goto assign; | |
3491 | } | |
3492 | ||
3493 | /* Allocate memory for new array of thresholds */ | |
3494 | thresholds_new = kmalloc(sizeof(*thresholds_new) + | |
3495 | size * sizeof(struct mem_cgroup_threshold), | |
3496 | GFP_KERNEL); | |
3497 | if (!thresholds_new) { | |
3498 | ret = -ENOMEM; | |
3499 | goto unlock; | |
3500 | } | |
3501 | thresholds_new->size = size; | |
3502 | ||
3503 | /* Copy thresholds and find current threshold */ | |
3504 | atomic_set(&thresholds_new->current_threshold, -1); | |
3505 | for (i = 0, j = 0; i < thresholds->size; i++) { | |
3506 | if (thresholds->entries[i].eventfd == eventfd) | |
3507 | continue; | |
3508 | ||
3509 | thresholds_new->entries[j] = thresholds->entries[i]; | |
3510 | if (thresholds_new->entries[j].threshold < usage) { | |
3511 | /* | |
3512 | * thresholds_new->current_threshold will not be used | |
3513 | * until rcu_assign_pointer(), so it's safe to increment | |
3514 | * it here. | |
3515 | */ | |
3516 | atomic_inc(&thresholds_new->current_threshold); | |
3517 | } | |
3518 | j++; | |
3519 | } | |
3520 | ||
3521 | assign: | |
3522 | if (type == _MEM) | |
3523 | rcu_assign_pointer(memcg->thresholds, thresholds_new); | |
3524 | else | |
3525 | rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new); | |
3526 | ||
3527 | /* To be sure that nobody uses thresholds before freeing it */ | |
3528 | synchronize_rcu(); | |
3529 | ||
3530 | for (i = 0; i < thresholds->size - size; i++) | |
3531 | mem_cgroup_put(memcg); | |
3532 | ||
3533 | kfree(thresholds); | |
3534 | unlock: | |
3535 | mutex_unlock(&memcg->thresholds_lock); | |
3536 | ||
3537 | return ret; | |
3538 | } | |
c1e862c1 | 3539 | |
8cdea7c0 BS |
3540 | static struct cftype mem_cgroup_files[] = { |
3541 | { | |
0eea1030 | 3542 | .name = "usage_in_bytes", |
8c7c6e34 | 3543 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
2c3daa72 | 3544 | .read_u64 = mem_cgroup_read, |
2e72b634 KS |
3545 | .register_event = mem_cgroup_register_event, |
3546 | .unregister_event = mem_cgroup_unregister_event, | |
8cdea7c0 | 3547 | }, |
c84872e1 PE |
3548 | { |
3549 | .name = "max_usage_in_bytes", | |
8c7c6e34 | 3550 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
29f2a4da | 3551 | .trigger = mem_cgroup_reset, |
c84872e1 PE |
3552 | .read_u64 = mem_cgroup_read, |
3553 | }, | |
8cdea7c0 | 3554 | { |
0eea1030 | 3555 | .name = "limit_in_bytes", |
8c7c6e34 | 3556 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
856c13aa | 3557 | .write_string = mem_cgroup_write, |
2c3daa72 | 3558 | .read_u64 = mem_cgroup_read, |
8cdea7c0 | 3559 | }, |
296c81d8 BS |
3560 | { |
3561 | .name = "soft_limit_in_bytes", | |
3562 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), | |
3563 | .write_string = mem_cgroup_write, | |
3564 | .read_u64 = mem_cgroup_read, | |
3565 | }, | |
8cdea7c0 BS |
3566 | { |
3567 | .name = "failcnt", | |
8c7c6e34 | 3568 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
29f2a4da | 3569 | .trigger = mem_cgroup_reset, |
2c3daa72 | 3570 | .read_u64 = mem_cgroup_read, |
8cdea7c0 | 3571 | }, |
d2ceb9b7 KH |
3572 | { |
3573 | .name = "stat", | |
c64745cf | 3574 | .read_map = mem_control_stat_show, |
d2ceb9b7 | 3575 | }, |
c1e862c1 KH |
3576 | { |
3577 | .name = "force_empty", | |
3578 | .trigger = mem_cgroup_force_empty_write, | |
3579 | }, | |
18f59ea7 BS |
3580 | { |
3581 | .name = "use_hierarchy", | |
3582 | .write_u64 = mem_cgroup_hierarchy_write, | |
3583 | .read_u64 = mem_cgroup_hierarchy_read, | |
3584 | }, | |
a7885eb8 KM |
3585 | { |
3586 | .name = "swappiness", | |
3587 | .read_u64 = mem_cgroup_swappiness_read, | |
3588 | .write_u64 = mem_cgroup_swappiness_write, | |
3589 | }, | |
7dc74be0 DN |
3590 | { |
3591 | .name = "move_charge_at_immigrate", | |
3592 | .read_u64 = mem_cgroup_move_charge_read, | |
3593 | .write_u64 = mem_cgroup_move_charge_write, | |
3594 | }, | |
8cdea7c0 BS |
3595 | }; |
3596 | ||
8c7c6e34 KH |
3597 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
3598 | static struct cftype memsw_cgroup_files[] = { | |
3599 | { | |
3600 | .name = "memsw.usage_in_bytes", | |
3601 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), | |
3602 | .read_u64 = mem_cgroup_read, | |
2e72b634 KS |
3603 | .register_event = mem_cgroup_register_event, |
3604 | .unregister_event = mem_cgroup_unregister_event, | |
8c7c6e34 KH |
3605 | }, |
3606 | { | |
3607 | .name = "memsw.max_usage_in_bytes", | |
3608 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), | |
3609 | .trigger = mem_cgroup_reset, | |
3610 | .read_u64 = mem_cgroup_read, | |
3611 | }, | |
3612 | { | |
3613 | .name = "memsw.limit_in_bytes", | |
3614 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), | |
3615 | .write_string = mem_cgroup_write, | |
3616 | .read_u64 = mem_cgroup_read, | |
3617 | }, | |
3618 | { | |
3619 | .name = "memsw.failcnt", | |
3620 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), | |
3621 | .trigger = mem_cgroup_reset, | |
3622 | .read_u64 = mem_cgroup_read, | |
3623 | }, | |
3624 | }; | |
3625 | ||
3626 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) | |
3627 | { | |
3628 | if (!do_swap_account) | |
3629 | return 0; | |
3630 | return cgroup_add_files(cont, ss, memsw_cgroup_files, | |
3631 | ARRAY_SIZE(memsw_cgroup_files)); | |
3632 | }; | |
3633 | #else | |
3634 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) | |
3635 | { | |
3636 | return 0; | |
3637 | } | |
3638 | #endif | |
3639 | ||
6d12e2d8 KH |
3640 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
3641 | { | |
3642 | struct mem_cgroup_per_node *pn; | |
1ecaab2b | 3643 | struct mem_cgroup_per_zone *mz; |
b69408e8 | 3644 | enum lru_list l; |
41e3355d | 3645 | int zone, tmp = node; |
1ecaab2b KH |
3646 | /* |
3647 | * This routine is called against possible nodes. | |
3648 | * But it's BUG to call kmalloc() against offline node. | |
3649 | * | |
3650 | * TODO: this routine can waste much memory for nodes which will | |
3651 | * never be onlined. It's better to use memory hotplug callback | |
3652 | * function. | |
3653 | */ | |
41e3355d KH |
3654 | if (!node_state(node, N_NORMAL_MEMORY)) |
3655 | tmp = -1; | |
3656 | pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp); | |
6d12e2d8 KH |
3657 | if (!pn) |
3658 | return 1; | |
1ecaab2b | 3659 | |
6d12e2d8 KH |
3660 | mem->info.nodeinfo[node] = pn; |
3661 | memset(pn, 0, sizeof(*pn)); | |
1ecaab2b KH |
3662 | |
3663 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
3664 | mz = &pn->zoneinfo[zone]; | |
b69408e8 CL |
3665 | for_each_lru(l) |
3666 | INIT_LIST_HEAD(&mz->lists[l]); | |
f64c3f54 | 3667 | mz->usage_in_excess = 0; |
4e416953 BS |
3668 | mz->on_tree = false; |
3669 | mz->mem = mem; | |
1ecaab2b | 3670 | } |
6d12e2d8 KH |
3671 | return 0; |
3672 | } | |
3673 | ||
1ecaab2b KH |
3674 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
3675 | { | |
3676 | kfree(mem->info.nodeinfo[node]); | |
3677 | } | |
3678 | ||
c8dad2bb JB |
3679 | static int mem_cgroup_size(void) |
3680 | { | |
3681 | int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu); | |
3682 | return sizeof(struct mem_cgroup) + cpustat_size; | |
3683 | } | |
3684 | ||
33327948 KH |
3685 | static struct mem_cgroup *mem_cgroup_alloc(void) |
3686 | { | |
3687 | struct mem_cgroup *mem; | |
c8dad2bb | 3688 | int size = mem_cgroup_size(); |
33327948 | 3689 | |
c8dad2bb JB |
3690 | if (size < PAGE_SIZE) |
3691 | mem = kmalloc(size, GFP_KERNEL); | |
33327948 | 3692 | else |
c8dad2bb | 3693 | mem = vmalloc(size); |
33327948 KH |
3694 | |
3695 | if (mem) | |
c8dad2bb | 3696 | memset(mem, 0, size); |
33327948 KH |
3697 | return mem; |
3698 | } | |
3699 | ||
8c7c6e34 KH |
3700 | /* |
3701 | * At destroying mem_cgroup, references from swap_cgroup can remain. | |
3702 | * (scanning all at force_empty is too costly...) | |
3703 | * | |
3704 | * Instead of clearing all references at force_empty, we remember | |
3705 | * the number of reference from swap_cgroup and free mem_cgroup when | |
3706 | * it goes down to 0. | |
3707 | * | |
8c7c6e34 KH |
3708 | * Removal of cgroup itself succeeds regardless of refs from swap. |
3709 | */ | |
3710 | ||
a7ba0eef | 3711 | static void __mem_cgroup_free(struct mem_cgroup *mem) |
33327948 | 3712 | { |
08e552c6 KH |
3713 | int node; |
3714 | ||
f64c3f54 | 3715 | mem_cgroup_remove_from_trees(mem); |
04046e1a KH |
3716 | free_css_id(&mem_cgroup_subsys, &mem->css); |
3717 | ||
08e552c6 KH |
3718 | for_each_node_state(node, N_POSSIBLE) |
3719 | free_mem_cgroup_per_zone_info(mem, node); | |
3720 | ||
c8dad2bb | 3721 | if (mem_cgroup_size() < PAGE_SIZE) |
33327948 KH |
3722 | kfree(mem); |
3723 | else | |
3724 | vfree(mem); | |
3725 | } | |
3726 | ||
8c7c6e34 KH |
3727 | static void mem_cgroup_get(struct mem_cgroup *mem) |
3728 | { | |
3729 | atomic_inc(&mem->refcnt); | |
3730 | } | |
3731 | ||
483c30b5 | 3732 | static void __mem_cgroup_put(struct mem_cgroup *mem, int count) |
8c7c6e34 | 3733 | { |
483c30b5 | 3734 | if (atomic_sub_and_test(count, &mem->refcnt)) { |
7bcc1bb1 | 3735 | struct mem_cgroup *parent = parent_mem_cgroup(mem); |
a7ba0eef | 3736 | __mem_cgroup_free(mem); |
7bcc1bb1 DN |
3737 | if (parent) |
3738 | mem_cgroup_put(parent); | |
3739 | } | |
8c7c6e34 KH |
3740 | } |
3741 | ||
483c30b5 DN |
3742 | static void mem_cgroup_put(struct mem_cgroup *mem) |
3743 | { | |
3744 | __mem_cgroup_put(mem, 1); | |
3745 | } | |
3746 | ||
7bcc1bb1 DN |
3747 | /* |
3748 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. | |
3749 | */ | |
3750 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem) | |
3751 | { | |
3752 | if (!mem->res.parent) | |
3753 | return NULL; | |
3754 | return mem_cgroup_from_res_counter(mem->res.parent, res); | |
3755 | } | |
33327948 | 3756 | |
c077719b KH |
3757 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
3758 | static void __init enable_swap_cgroup(void) | |
3759 | { | |
f8d66542 | 3760 | if (!mem_cgroup_disabled() && really_do_swap_account) |
c077719b KH |
3761 | do_swap_account = 1; |
3762 | } | |
3763 | #else | |
3764 | static void __init enable_swap_cgroup(void) | |
3765 | { | |
3766 | } | |
3767 | #endif | |
3768 | ||
f64c3f54 BS |
3769 | static int mem_cgroup_soft_limit_tree_init(void) |
3770 | { | |
3771 | struct mem_cgroup_tree_per_node *rtpn; | |
3772 | struct mem_cgroup_tree_per_zone *rtpz; | |
3773 | int tmp, node, zone; | |
3774 | ||
3775 | for_each_node_state(node, N_POSSIBLE) { | |
3776 | tmp = node; | |
3777 | if (!node_state(node, N_NORMAL_MEMORY)) | |
3778 | tmp = -1; | |
3779 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp); | |
3780 | if (!rtpn) | |
3781 | return 1; | |
3782 | ||
3783 | soft_limit_tree.rb_tree_per_node[node] = rtpn; | |
3784 | ||
3785 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
3786 | rtpz = &rtpn->rb_tree_per_zone[zone]; | |
3787 | rtpz->rb_root = RB_ROOT; | |
3788 | spin_lock_init(&rtpz->lock); | |
3789 | } | |
3790 | } | |
3791 | return 0; | |
3792 | } | |
3793 | ||
0eb253e2 | 3794 | static struct cgroup_subsys_state * __ref |
8cdea7c0 BS |
3795 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
3796 | { | |
28dbc4b6 | 3797 | struct mem_cgroup *mem, *parent; |
04046e1a | 3798 | long error = -ENOMEM; |
6d12e2d8 | 3799 | int node; |
8cdea7c0 | 3800 | |
c8dad2bb JB |
3801 | mem = mem_cgroup_alloc(); |
3802 | if (!mem) | |
04046e1a | 3803 | return ERR_PTR(error); |
78fb7466 | 3804 | |
6d12e2d8 KH |
3805 | for_each_node_state(node, N_POSSIBLE) |
3806 | if (alloc_mem_cgroup_per_zone_info(mem, node)) | |
3807 | goto free_out; | |
f64c3f54 | 3808 | |
c077719b | 3809 | /* root ? */ |
28dbc4b6 | 3810 | if (cont->parent == NULL) { |
cdec2e42 | 3811 | int cpu; |
c077719b | 3812 | enable_swap_cgroup(); |
28dbc4b6 | 3813 | parent = NULL; |
4b3bde4c | 3814 | root_mem_cgroup = mem; |
f64c3f54 BS |
3815 | if (mem_cgroup_soft_limit_tree_init()) |
3816 | goto free_out; | |
cdec2e42 KH |
3817 | for_each_possible_cpu(cpu) { |
3818 | struct memcg_stock_pcp *stock = | |
3819 | &per_cpu(memcg_stock, cpu); | |
3820 | INIT_WORK(&stock->work, drain_local_stock); | |
3821 | } | |
3822 | hotcpu_notifier(memcg_stock_cpu_callback, 0); | |
18f59ea7 | 3823 | } else { |
28dbc4b6 | 3824 | parent = mem_cgroup_from_cont(cont->parent); |
18f59ea7 BS |
3825 | mem->use_hierarchy = parent->use_hierarchy; |
3826 | } | |
28dbc4b6 | 3827 | |
18f59ea7 BS |
3828 | if (parent && parent->use_hierarchy) { |
3829 | res_counter_init(&mem->res, &parent->res); | |
3830 | res_counter_init(&mem->memsw, &parent->memsw); | |
7bcc1bb1 DN |
3831 | /* |
3832 | * We increment refcnt of the parent to ensure that we can | |
3833 | * safely access it on res_counter_charge/uncharge. | |
3834 | * This refcnt will be decremented when freeing this | |
3835 | * mem_cgroup(see mem_cgroup_put). | |
3836 | */ | |
3837 | mem_cgroup_get(parent); | |
18f59ea7 BS |
3838 | } else { |
3839 | res_counter_init(&mem->res, NULL); | |
3840 | res_counter_init(&mem->memsw, NULL); | |
3841 | } | |
04046e1a | 3842 | mem->last_scanned_child = 0; |
2733c06a | 3843 | spin_lock_init(&mem->reclaim_param_lock); |
6d61ef40 | 3844 | |
a7885eb8 KM |
3845 | if (parent) |
3846 | mem->swappiness = get_swappiness(parent); | |
a7ba0eef | 3847 | atomic_set(&mem->refcnt, 1); |
7dc74be0 | 3848 | mem->move_charge_at_immigrate = 0; |
2e72b634 | 3849 | mutex_init(&mem->thresholds_lock); |
8cdea7c0 | 3850 | return &mem->css; |
6d12e2d8 | 3851 | free_out: |
a7ba0eef | 3852 | __mem_cgroup_free(mem); |
4b3bde4c | 3853 | root_mem_cgroup = NULL; |
04046e1a | 3854 | return ERR_PTR(error); |
8cdea7c0 BS |
3855 | } |
3856 | ||
ec64f515 | 3857 | static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
df878fb0 KH |
3858 | struct cgroup *cont) |
3859 | { | |
3860 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); | |
ec64f515 KH |
3861 | |
3862 | return mem_cgroup_force_empty(mem, false); | |
df878fb0 KH |
3863 | } |
3864 | ||
8cdea7c0 BS |
3865 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
3866 | struct cgroup *cont) | |
3867 | { | |
c268e994 | 3868 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
c268e994 | 3869 | |
c268e994 | 3870 | mem_cgroup_put(mem); |
8cdea7c0 BS |
3871 | } |
3872 | ||
3873 | static int mem_cgroup_populate(struct cgroup_subsys *ss, | |
3874 | struct cgroup *cont) | |
3875 | { | |
8c7c6e34 KH |
3876 | int ret; |
3877 | ||
3878 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, | |
3879 | ARRAY_SIZE(mem_cgroup_files)); | |
3880 | ||
3881 | if (!ret) | |
3882 | ret = register_memsw_files(cont, ss); | |
3883 | return ret; | |
8cdea7c0 BS |
3884 | } |
3885 | ||
02491447 | 3886 | #ifdef CONFIG_MMU |
7dc74be0 | 3887 | /* Handlers for move charge at task migration. */ |
854ffa8d DN |
3888 | #define PRECHARGE_COUNT_AT_ONCE 256 |
3889 | static int mem_cgroup_do_precharge(unsigned long count) | |
7dc74be0 | 3890 | { |
854ffa8d DN |
3891 | int ret = 0; |
3892 | int batch_count = PRECHARGE_COUNT_AT_ONCE; | |
4ffef5fe DN |
3893 | struct mem_cgroup *mem = mc.to; |
3894 | ||
854ffa8d DN |
3895 | if (mem_cgroup_is_root(mem)) { |
3896 | mc.precharge += count; | |
3897 | /* we don't need css_get for root */ | |
3898 | return ret; | |
3899 | } | |
3900 | /* try to charge at once */ | |
3901 | if (count > 1) { | |
3902 | struct res_counter *dummy; | |
3903 | /* | |
3904 | * "mem" cannot be under rmdir() because we've already checked | |
3905 | * by cgroup_lock_live_cgroup() that it is not removed and we | |
3906 | * are still under the same cgroup_mutex. So we can postpone | |
3907 | * css_get(). | |
3908 | */ | |
3909 | if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy)) | |
3910 | goto one_by_one; | |
3911 | if (do_swap_account && res_counter_charge(&mem->memsw, | |
3912 | PAGE_SIZE * count, &dummy)) { | |
3913 | res_counter_uncharge(&mem->res, PAGE_SIZE * count); | |
3914 | goto one_by_one; | |
3915 | } | |
3916 | mc.precharge += count; | |
3917 | VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags)); | |
3918 | WARN_ON_ONCE(count > INT_MAX); | |
3919 | __css_get(&mem->css, (int)count); | |
3920 | return ret; | |
3921 | } | |
3922 | one_by_one: | |
3923 | /* fall back to one by one charge */ | |
3924 | while (count--) { | |
3925 | if (signal_pending(current)) { | |
3926 | ret = -EINTR; | |
3927 | break; | |
3928 | } | |
3929 | if (!batch_count--) { | |
3930 | batch_count = PRECHARGE_COUNT_AT_ONCE; | |
3931 | cond_resched(); | |
3932 | } | |
3933 | ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, | |
3934 | false, NULL); | |
3935 | if (ret || !mem) | |
3936 | /* mem_cgroup_clear_mc() will do uncharge later */ | |
3937 | return -ENOMEM; | |
3938 | mc.precharge++; | |
3939 | } | |
4ffef5fe DN |
3940 | return ret; |
3941 | } | |
02491447 DN |
3942 | #else /* !CONFIG_MMU */ |
3943 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, | |
3944 | struct cgroup *cgroup, | |
3945 | struct task_struct *p, | |
3946 | bool threadgroup) | |
3947 | { | |
3948 | return 0; | |
3949 | } | |
3950 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, | |
3951 | struct cgroup *cgroup, | |
3952 | struct task_struct *p, | |
3953 | bool threadgroup) | |
3954 | { | |
3955 | } | |
3956 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, | |
3957 | struct cgroup *cont, | |
3958 | struct cgroup *old_cont, | |
3959 | struct task_struct *p, | |
3960 | bool threadgroup) | |
3961 | { | |
3962 | } | |
3963 | #endif | |
4ffef5fe DN |
3964 | |
3965 | /** | |
3966 | * is_target_pte_for_mc - check a pte whether it is valid for move charge | |
3967 | * @vma: the vma the pte to be checked belongs | |
3968 | * @addr: the address corresponding to the pte to be checked | |
3969 | * @ptent: the pte to be checked | |
02491447 | 3970 | * @target: the pointer the target page or swap ent will be stored(can be NULL) |
4ffef5fe DN |
3971 | * |
3972 | * Returns | |
3973 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. | |
3974 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for | |
3975 | * move charge. if @target is not NULL, the page is stored in target->page | |
3976 | * with extra refcnt got(Callers should handle it). | |
02491447 DN |
3977 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a |
3978 | * target for charge migration. if @target is not NULL, the entry is stored | |
3979 | * in target->ent. | |
4ffef5fe DN |
3980 | * |
3981 | * Called with pte lock held. | |
3982 | */ | |
4ffef5fe DN |
3983 | union mc_target { |
3984 | struct page *page; | |
02491447 | 3985 | swp_entry_t ent; |
4ffef5fe DN |
3986 | }; |
3987 | ||
4ffef5fe DN |
3988 | enum mc_target_type { |
3989 | MC_TARGET_NONE, /* not used */ | |
3990 | MC_TARGET_PAGE, | |
02491447 | 3991 | MC_TARGET_SWAP, |
4ffef5fe DN |
3992 | }; |
3993 | ||
3994 | static int is_target_pte_for_mc(struct vm_area_struct *vma, | |
3995 | unsigned long addr, pte_t ptent, union mc_target *target) | |
3996 | { | |
02491447 | 3997 | struct page *page = NULL; |
4ffef5fe DN |
3998 | struct page_cgroup *pc; |
3999 | int ret = 0; | |
02491447 DN |
4000 | swp_entry_t ent = { .val = 0 }; |
4001 | int usage_count = 0; | |
4ffef5fe DN |
4002 | bool move_anon = test_bit(MOVE_CHARGE_TYPE_ANON, |
4003 | &mc.to->move_charge_at_immigrate); | |
4004 | ||
02491447 DN |
4005 | if (!pte_present(ptent)) { |
4006 | /* TODO: handle swap of shmes/tmpfs */ | |
4007 | if (pte_none(ptent) || pte_file(ptent)) | |
4008 | return 0; | |
4009 | else if (is_swap_pte(ptent)) { | |
4010 | ent = pte_to_swp_entry(ptent); | |
4011 | if (!move_anon || non_swap_entry(ent)) | |
4012 | return 0; | |
4013 | usage_count = mem_cgroup_count_swap_user(ent, &page); | |
4014 | } | |
4015 | } else { | |
4016 | page = vm_normal_page(vma, addr, ptent); | |
4017 | if (!page || !page_mapped(page)) | |
4018 | return 0; | |
4019 | /* | |
4020 | * TODO: We don't move charges of file(including shmem/tmpfs) | |
4021 | * pages for now. | |
4022 | */ | |
4023 | if (!move_anon || !PageAnon(page)) | |
4024 | return 0; | |
4025 | if (!get_page_unless_zero(page)) | |
4026 | return 0; | |
4027 | usage_count = page_mapcount(page); | |
4028 | } | |
4029 | if (usage_count > 1) { | |
4030 | /* | |
4031 | * TODO: We don't move charges of shared(used by multiple | |
4032 | * processes) pages for now. | |
4033 | */ | |
4034 | if (page) | |
4035 | put_page(page); | |
4ffef5fe | 4036 | return 0; |
02491447 DN |
4037 | } |
4038 | if (page) { | |
4039 | pc = lookup_page_cgroup(page); | |
4040 | /* | |
4041 | * Do only loose check w/o page_cgroup lock. | |
4042 | * mem_cgroup_move_account() checks the pc is valid or not under | |
4043 | * the lock. | |
4044 | */ | |
4045 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { | |
4046 | ret = MC_TARGET_PAGE; | |
4047 | if (target) | |
4048 | target->page = page; | |
4049 | } | |
4050 | if (!ret || !target) | |
4051 | put_page(page); | |
4052 | } | |
4053 | /* throught */ | |
4054 | if (ent.val && do_swap_account && !ret && | |
4055 | css_id(&mc.from->css) == lookup_swap_cgroup(ent)) { | |
4056 | ret = MC_TARGET_SWAP; | |
4ffef5fe | 4057 | if (target) |
02491447 | 4058 | target->ent = ent; |
4ffef5fe | 4059 | } |
4ffef5fe DN |
4060 | return ret; |
4061 | } | |
4062 | ||
4063 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, | |
4064 | unsigned long addr, unsigned long end, | |
4065 | struct mm_walk *walk) | |
4066 | { | |
4067 | struct vm_area_struct *vma = walk->private; | |
4068 | pte_t *pte; | |
4069 | spinlock_t *ptl; | |
4070 | ||
4071 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); | |
4072 | for (; addr != end; pte++, addr += PAGE_SIZE) | |
4073 | if (is_target_pte_for_mc(vma, addr, *pte, NULL)) | |
4074 | mc.precharge++; /* increment precharge temporarily */ | |
4075 | pte_unmap_unlock(pte - 1, ptl); | |
4076 | cond_resched(); | |
4077 | ||
7dc74be0 DN |
4078 | return 0; |
4079 | } | |
4080 | ||
4ffef5fe DN |
4081 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
4082 | { | |
4083 | unsigned long precharge; | |
4084 | struct vm_area_struct *vma; | |
4085 | ||
4086 | down_read(&mm->mmap_sem); | |
4087 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
4088 | struct mm_walk mem_cgroup_count_precharge_walk = { | |
4089 | .pmd_entry = mem_cgroup_count_precharge_pte_range, | |
4090 | .mm = mm, | |
4091 | .private = vma, | |
4092 | }; | |
4093 | if (is_vm_hugetlb_page(vma)) | |
4094 | continue; | |
4095 | /* TODO: We don't move charges of shmem/tmpfs pages for now. */ | |
4096 | if (vma->vm_flags & VM_SHARED) | |
4097 | continue; | |
4098 | walk_page_range(vma->vm_start, vma->vm_end, | |
4099 | &mem_cgroup_count_precharge_walk); | |
4100 | } | |
4101 | up_read(&mm->mmap_sem); | |
4102 | ||
4103 | precharge = mc.precharge; | |
4104 | mc.precharge = 0; | |
4105 | ||
4106 | return precharge; | |
4107 | } | |
4108 | ||
4ffef5fe DN |
4109 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
4110 | { | |
854ffa8d | 4111 | return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm)); |
4ffef5fe DN |
4112 | } |
4113 | ||
4114 | static void mem_cgroup_clear_mc(void) | |
4115 | { | |
4116 | /* we must uncharge all the leftover precharges from mc.to */ | |
854ffa8d DN |
4117 | if (mc.precharge) { |
4118 | __mem_cgroup_cancel_charge(mc.to, mc.precharge); | |
4119 | mc.precharge = 0; | |
4120 | } | |
4121 | /* | |
4122 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so | |
4123 | * we must uncharge here. | |
4124 | */ | |
4125 | if (mc.moved_charge) { | |
4126 | __mem_cgroup_cancel_charge(mc.from, mc.moved_charge); | |
4127 | mc.moved_charge = 0; | |
4ffef5fe | 4128 | } |
483c30b5 DN |
4129 | /* we must fixup refcnts and charges */ |
4130 | if (mc.moved_swap) { | |
4131 | WARN_ON_ONCE(mc.moved_swap > INT_MAX); | |
4132 | /* uncharge swap account from the old cgroup */ | |
4133 | if (!mem_cgroup_is_root(mc.from)) | |
4134 | res_counter_uncharge(&mc.from->memsw, | |
4135 | PAGE_SIZE * mc.moved_swap); | |
4136 | __mem_cgroup_put(mc.from, mc.moved_swap); | |
4137 | ||
4138 | if (!mem_cgroup_is_root(mc.to)) { | |
4139 | /* | |
4140 | * we charged both to->res and to->memsw, so we should | |
4141 | * uncharge to->res. | |
4142 | */ | |
4143 | res_counter_uncharge(&mc.to->res, | |
4144 | PAGE_SIZE * mc.moved_swap); | |
4145 | VM_BUG_ON(test_bit(CSS_ROOT, &mc.to->css.flags)); | |
4146 | __css_put(&mc.to->css, mc.moved_swap); | |
4147 | } | |
4148 | /* we've already done mem_cgroup_get(mc.to) */ | |
4149 | ||
4150 | mc.moved_swap = 0; | |
4151 | } | |
4ffef5fe DN |
4152 | mc.from = NULL; |
4153 | mc.to = NULL; | |
8033b97c DN |
4154 | mc.moving_task = NULL; |
4155 | wake_up_all(&mc.waitq); | |
4ffef5fe DN |
4156 | } |
4157 | ||
7dc74be0 DN |
4158 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
4159 | struct cgroup *cgroup, | |
4160 | struct task_struct *p, | |
4161 | bool threadgroup) | |
4162 | { | |
4163 | int ret = 0; | |
4164 | struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup); | |
4165 | ||
4166 | if (mem->move_charge_at_immigrate) { | |
4167 | struct mm_struct *mm; | |
4168 | struct mem_cgroup *from = mem_cgroup_from_task(p); | |
4169 | ||
4170 | VM_BUG_ON(from == mem); | |
4171 | ||
4172 | mm = get_task_mm(p); | |
4173 | if (!mm) | |
4174 | return 0; | |
7dc74be0 | 4175 | /* We move charges only when we move a owner of the mm */ |
4ffef5fe DN |
4176 | if (mm->owner == p) { |
4177 | VM_BUG_ON(mc.from); | |
4178 | VM_BUG_ON(mc.to); | |
4179 | VM_BUG_ON(mc.precharge); | |
854ffa8d | 4180 | VM_BUG_ON(mc.moved_charge); |
483c30b5 | 4181 | VM_BUG_ON(mc.moved_swap); |
8033b97c | 4182 | VM_BUG_ON(mc.moving_task); |
4ffef5fe DN |
4183 | mc.from = from; |
4184 | mc.to = mem; | |
4185 | mc.precharge = 0; | |
854ffa8d | 4186 | mc.moved_charge = 0; |
483c30b5 | 4187 | mc.moved_swap = 0; |
8033b97c | 4188 | mc.moving_task = current; |
4ffef5fe DN |
4189 | |
4190 | ret = mem_cgroup_precharge_mc(mm); | |
4191 | if (ret) | |
4192 | mem_cgroup_clear_mc(); | |
4193 | } | |
7dc74be0 DN |
4194 | mmput(mm); |
4195 | } | |
4196 | return ret; | |
4197 | } | |
4198 | ||
4199 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, | |
4200 | struct cgroup *cgroup, | |
4201 | struct task_struct *p, | |
4202 | bool threadgroup) | |
4203 | { | |
4ffef5fe | 4204 | mem_cgroup_clear_mc(); |
7dc74be0 DN |
4205 | } |
4206 | ||
4ffef5fe DN |
4207 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
4208 | unsigned long addr, unsigned long end, | |
4209 | struct mm_walk *walk) | |
7dc74be0 | 4210 | { |
4ffef5fe DN |
4211 | int ret = 0; |
4212 | struct vm_area_struct *vma = walk->private; | |
4213 | pte_t *pte; | |
4214 | spinlock_t *ptl; | |
4215 | ||
4216 | retry: | |
4217 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); | |
4218 | for (; addr != end; addr += PAGE_SIZE) { | |
4219 | pte_t ptent = *(pte++); | |
4220 | union mc_target target; | |
4221 | int type; | |
4222 | struct page *page; | |
4223 | struct page_cgroup *pc; | |
02491447 | 4224 | swp_entry_t ent; |
4ffef5fe DN |
4225 | |
4226 | if (!mc.precharge) | |
4227 | break; | |
4228 | ||
4229 | type = is_target_pte_for_mc(vma, addr, ptent, &target); | |
4230 | switch (type) { | |
4231 | case MC_TARGET_PAGE: | |
4232 | page = target.page; | |
4233 | if (isolate_lru_page(page)) | |
4234 | goto put; | |
4235 | pc = lookup_page_cgroup(page); | |
854ffa8d DN |
4236 | if (!mem_cgroup_move_account(pc, |
4237 | mc.from, mc.to, false)) { | |
4ffef5fe | 4238 | mc.precharge--; |
854ffa8d DN |
4239 | /* we uncharge from mc.from later. */ |
4240 | mc.moved_charge++; | |
4ffef5fe DN |
4241 | } |
4242 | putback_lru_page(page); | |
4243 | put: /* is_target_pte_for_mc() gets the page */ | |
4244 | put_page(page); | |
4245 | break; | |
02491447 DN |
4246 | case MC_TARGET_SWAP: |
4247 | ent = target.ent; | |
483c30b5 DN |
4248 | if (!mem_cgroup_move_swap_account(ent, |
4249 | mc.from, mc.to, false)) { | |
02491447 | 4250 | mc.precharge--; |
483c30b5 DN |
4251 | /* we fixup refcnts and charges later. */ |
4252 | mc.moved_swap++; | |
4253 | } | |
02491447 | 4254 | break; |
4ffef5fe DN |
4255 | default: |
4256 | break; | |
4257 | } | |
4258 | } | |
4259 | pte_unmap_unlock(pte - 1, ptl); | |
4260 | cond_resched(); | |
4261 | ||
4262 | if (addr != end) { | |
4263 | /* | |
4264 | * We have consumed all precharges we got in can_attach(). | |
4265 | * We try charge one by one, but don't do any additional | |
4266 | * charges to mc.to if we have failed in charge once in attach() | |
4267 | * phase. | |
4268 | */ | |
854ffa8d | 4269 | ret = mem_cgroup_do_precharge(1); |
4ffef5fe DN |
4270 | if (!ret) |
4271 | goto retry; | |
4272 | } | |
4273 | ||
4274 | return ret; | |
4275 | } | |
4276 | ||
4277 | static void mem_cgroup_move_charge(struct mm_struct *mm) | |
4278 | { | |
4279 | struct vm_area_struct *vma; | |
4280 | ||
4281 | lru_add_drain_all(); | |
4282 | down_read(&mm->mmap_sem); | |
4283 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
4284 | int ret; | |
4285 | struct mm_walk mem_cgroup_move_charge_walk = { | |
4286 | .pmd_entry = mem_cgroup_move_charge_pte_range, | |
4287 | .mm = mm, | |
4288 | .private = vma, | |
4289 | }; | |
4290 | if (is_vm_hugetlb_page(vma)) | |
4291 | continue; | |
4292 | /* TODO: We don't move charges of shmem/tmpfs pages for now. */ | |
4293 | if (vma->vm_flags & VM_SHARED) | |
4294 | continue; | |
4295 | ret = walk_page_range(vma->vm_start, vma->vm_end, | |
4296 | &mem_cgroup_move_charge_walk); | |
4297 | if (ret) | |
4298 | /* | |
4299 | * means we have consumed all precharges and failed in | |
4300 | * doing additional charge. Just abandon here. | |
4301 | */ | |
4302 | break; | |
4303 | } | |
4304 | up_read(&mm->mmap_sem); | |
7dc74be0 DN |
4305 | } |
4306 | ||
67e465a7 BS |
4307 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
4308 | struct cgroup *cont, | |
4309 | struct cgroup *old_cont, | |
be367d09 BB |
4310 | struct task_struct *p, |
4311 | bool threadgroup) | |
67e465a7 | 4312 | { |
4ffef5fe DN |
4313 | struct mm_struct *mm; |
4314 | ||
4315 | if (!mc.to) | |
4316 | /* no need to move charge */ | |
4317 | return; | |
4318 | ||
4319 | mm = get_task_mm(p); | |
4320 | if (mm) { | |
4321 | mem_cgroup_move_charge(mm); | |
4322 | mmput(mm); | |
4323 | } | |
4324 | mem_cgroup_clear_mc(); | |
67e465a7 BS |
4325 | } |
4326 | ||
8cdea7c0 BS |
4327 | struct cgroup_subsys mem_cgroup_subsys = { |
4328 | .name = "memory", | |
4329 | .subsys_id = mem_cgroup_subsys_id, | |
4330 | .create = mem_cgroup_create, | |
df878fb0 | 4331 | .pre_destroy = mem_cgroup_pre_destroy, |
8cdea7c0 BS |
4332 | .destroy = mem_cgroup_destroy, |
4333 | .populate = mem_cgroup_populate, | |
7dc74be0 DN |
4334 | .can_attach = mem_cgroup_can_attach, |
4335 | .cancel_attach = mem_cgroup_cancel_attach, | |
67e465a7 | 4336 | .attach = mem_cgroup_move_task, |
6d12e2d8 | 4337 | .early_init = 0, |
04046e1a | 4338 | .use_id = 1, |
8cdea7c0 | 4339 | }; |
c077719b KH |
4340 | |
4341 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP | |
4342 | ||
4343 | static int __init disable_swap_account(char *s) | |
4344 | { | |
4345 | really_do_swap_account = 0; | |
4346 | return 1; | |
4347 | } | |
4348 | __setup("noswapaccount", disable_swap_account); | |
4349 | #endif |