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 | * | |
7ae1e1d0 GC |
13 | * Kernel Memory Controller |
14 | * Copyright (C) 2012 Parallels Inc. and Google Inc. | |
15 | * Authors: Glauber Costa and Suleiman Souhlal | |
16 | * | |
8cdea7c0 BS |
17 | * This program is free software; you can redistribute it and/or modify |
18 | * it under the terms of the GNU General Public License as published by | |
19 | * the Free Software Foundation; either version 2 of the License, or | |
20 | * (at your option) any later version. | |
21 | * | |
22 | * This program is distributed in the hope that it will be useful, | |
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 | * GNU General Public License for more details. | |
26 | */ | |
27 | ||
28 | #include <linux/res_counter.h> | |
29 | #include <linux/memcontrol.h> | |
30 | #include <linux/cgroup.h> | |
78fb7466 | 31 | #include <linux/mm.h> |
4ffef5fe | 32 | #include <linux/hugetlb.h> |
d13d1443 | 33 | #include <linux/pagemap.h> |
d52aa412 | 34 | #include <linux/smp.h> |
8a9f3ccd | 35 | #include <linux/page-flags.h> |
66e1707b | 36 | #include <linux/backing-dev.h> |
8a9f3ccd BS |
37 | #include <linux/bit_spinlock.h> |
38 | #include <linux/rcupdate.h> | |
e222432b | 39 | #include <linux/limits.h> |
b9e15baf | 40 | #include <linux/export.h> |
8c7c6e34 | 41 | #include <linux/mutex.h> |
f64c3f54 | 42 | #include <linux/rbtree.h> |
b6ac57d5 | 43 | #include <linux/slab.h> |
66e1707b | 44 | #include <linux/swap.h> |
02491447 | 45 | #include <linux/swapops.h> |
66e1707b | 46 | #include <linux/spinlock.h> |
2e72b634 KS |
47 | #include <linux/eventfd.h> |
48 | #include <linux/sort.h> | |
66e1707b | 49 | #include <linux/fs.h> |
d2ceb9b7 | 50 | #include <linux/seq_file.h> |
33327948 | 51 | #include <linux/vmalloc.h> |
b69408e8 | 52 | #include <linux/mm_inline.h> |
52d4b9ac | 53 | #include <linux/page_cgroup.h> |
cdec2e42 | 54 | #include <linux/cpu.h> |
158e0a2d | 55 | #include <linux/oom.h> |
08e552c6 | 56 | #include "internal.h" |
d1a4c0b3 | 57 | #include <net/sock.h> |
4bd2c1ee | 58 | #include <net/ip.h> |
d1a4c0b3 | 59 | #include <net/tcp_memcontrol.h> |
8cdea7c0 | 60 | |
8697d331 BS |
61 | #include <asm/uaccess.h> |
62 | ||
cc8e970c KM |
63 | #include <trace/events/vmscan.h> |
64 | ||
a181b0e8 | 65 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
68ae564b DR |
66 | EXPORT_SYMBOL(mem_cgroup_subsys); |
67 | ||
a181b0e8 | 68 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
6bbda35c | 69 | static struct mem_cgroup *root_mem_cgroup __read_mostly; |
8cdea7c0 | 70 | |
c255a458 | 71 | #ifdef CONFIG_MEMCG_SWAP |
338c8431 | 72 | /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */ |
c077719b | 73 | int do_swap_account __read_mostly; |
a42c390c MH |
74 | |
75 | /* for remember boot option*/ | |
c255a458 | 76 | #ifdef CONFIG_MEMCG_SWAP_ENABLED |
a42c390c MH |
77 | static int really_do_swap_account __initdata = 1; |
78 | #else | |
79 | static int really_do_swap_account __initdata = 0; | |
80 | #endif | |
81 | ||
c077719b | 82 | #else |
a0db00fc | 83 | #define do_swap_account 0 |
c077719b KH |
84 | #endif |
85 | ||
86 | ||
d52aa412 KH |
87 | /* |
88 | * Statistics for memory cgroup. | |
89 | */ | |
90 | enum mem_cgroup_stat_index { | |
91 | /* | |
92 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. | |
93 | */ | |
94 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ | |
d69b042f | 95 | MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */ |
d8046582 | 96 | MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */ |
bff6bb83 | 97 | MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */ |
d52aa412 KH |
98 | MEM_CGROUP_STAT_NSTATS, |
99 | }; | |
100 | ||
af7c4b0e JW |
101 | static const char * const mem_cgroup_stat_names[] = { |
102 | "cache", | |
103 | "rss", | |
104 | "mapped_file", | |
105 | "swap", | |
106 | }; | |
107 | ||
e9f8974f JW |
108 | enum mem_cgroup_events_index { |
109 | MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */ | |
110 | MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */ | |
456f998e YH |
111 | MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */ |
112 | MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */ | |
e9f8974f JW |
113 | MEM_CGROUP_EVENTS_NSTATS, |
114 | }; | |
af7c4b0e JW |
115 | |
116 | static const char * const mem_cgroup_events_names[] = { | |
117 | "pgpgin", | |
118 | "pgpgout", | |
119 | "pgfault", | |
120 | "pgmajfault", | |
121 | }; | |
122 | ||
58cf188e SZ |
123 | static const char * const mem_cgroup_lru_names[] = { |
124 | "inactive_anon", | |
125 | "active_anon", | |
126 | "inactive_file", | |
127 | "active_file", | |
128 | "unevictable", | |
129 | }; | |
130 | ||
7a159cc9 JW |
131 | /* |
132 | * Per memcg event counter is incremented at every pagein/pageout. With THP, | |
133 | * it will be incremated by the number of pages. This counter is used for | |
134 | * for trigger some periodic events. This is straightforward and better | |
135 | * than using jiffies etc. to handle periodic memcg event. | |
136 | */ | |
137 | enum mem_cgroup_events_target { | |
138 | MEM_CGROUP_TARGET_THRESH, | |
139 | MEM_CGROUP_TARGET_SOFTLIMIT, | |
453a9bf3 | 140 | MEM_CGROUP_TARGET_NUMAINFO, |
7a159cc9 JW |
141 | MEM_CGROUP_NTARGETS, |
142 | }; | |
a0db00fc KS |
143 | #define THRESHOLDS_EVENTS_TARGET 128 |
144 | #define SOFTLIMIT_EVENTS_TARGET 1024 | |
145 | #define NUMAINFO_EVENTS_TARGET 1024 | |
e9f8974f | 146 | |
d52aa412 | 147 | struct mem_cgroup_stat_cpu { |
7a159cc9 | 148 | long count[MEM_CGROUP_STAT_NSTATS]; |
e9f8974f | 149 | unsigned long events[MEM_CGROUP_EVENTS_NSTATS]; |
13114716 | 150 | unsigned long nr_page_events; |
7a159cc9 | 151 | unsigned long targets[MEM_CGROUP_NTARGETS]; |
d52aa412 KH |
152 | }; |
153 | ||
527a5ec9 JW |
154 | struct mem_cgroup_reclaim_iter { |
155 | /* css_id of the last scanned hierarchy member */ | |
156 | int position; | |
157 | /* scan generation, increased every round-trip */ | |
158 | unsigned int generation; | |
159 | }; | |
160 | ||
6d12e2d8 KH |
161 | /* |
162 | * per-zone information in memory controller. | |
163 | */ | |
6d12e2d8 | 164 | struct mem_cgroup_per_zone { |
6290df54 | 165 | struct lruvec lruvec; |
1eb49272 | 166 | unsigned long lru_size[NR_LRU_LISTS]; |
3e2f41f1 | 167 | |
527a5ec9 JW |
168 | struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1]; |
169 | ||
f64c3f54 BS |
170 | struct rb_node tree_node; /* RB tree node */ |
171 | unsigned long long usage_in_excess;/* Set to the value by which */ | |
172 | /* the soft limit is exceeded*/ | |
173 | bool on_tree; | |
d79154bb | 174 | struct mem_cgroup *memcg; /* Back pointer, we cannot */ |
4e416953 | 175 | /* use container_of */ |
6d12e2d8 | 176 | }; |
6d12e2d8 KH |
177 | |
178 | struct mem_cgroup_per_node { | |
179 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; | |
180 | }; | |
181 | ||
182 | struct mem_cgroup_lru_info { | |
183 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; | |
184 | }; | |
185 | ||
f64c3f54 BS |
186 | /* |
187 | * Cgroups above their limits are maintained in a RB-Tree, independent of | |
188 | * their hierarchy representation | |
189 | */ | |
190 | ||
191 | struct mem_cgroup_tree_per_zone { | |
192 | struct rb_root rb_root; | |
193 | spinlock_t lock; | |
194 | }; | |
195 | ||
196 | struct mem_cgroup_tree_per_node { | |
197 | struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES]; | |
198 | }; | |
199 | ||
200 | struct mem_cgroup_tree { | |
201 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; | |
202 | }; | |
203 | ||
204 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; | |
205 | ||
2e72b634 KS |
206 | struct mem_cgroup_threshold { |
207 | struct eventfd_ctx *eventfd; | |
208 | u64 threshold; | |
209 | }; | |
210 | ||
9490ff27 | 211 | /* For threshold */ |
2e72b634 | 212 | struct mem_cgroup_threshold_ary { |
748dad36 | 213 | /* An array index points to threshold just below or equal to usage. */ |
5407a562 | 214 | int current_threshold; |
2e72b634 KS |
215 | /* Size of entries[] */ |
216 | unsigned int size; | |
217 | /* Array of thresholds */ | |
218 | struct mem_cgroup_threshold entries[0]; | |
219 | }; | |
2c488db2 KS |
220 | |
221 | struct mem_cgroup_thresholds { | |
222 | /* Primary thresholds array */ | |
223 | struct mem_cgroup_threshold_ary *primary; | |
224 | /* | |
225 | * Spare threshold array. | |
226 | * This is needed to make mem_cgroup_unregister_event() "never fail". | |
227 | * It must be able to store at least primary->size - 1 entries. | |
228 | */ | |
229 | struct mem_cgroup_threshold_ary *spare; | |
230 | }; | |
231 | ||
9490ff27 KH |
232 | /* for OOM */ |
233 | struct mem_cgroup_eventfd_list { | |
234 | struct list_head list; | |
235 | struct eventfd_ctx *eventfd; | |
236 | }; | |
2e72b634 | 237 | |
c0ff4b85 R |
238 | static void mem_cgroup_threshold(struct mem_cgroup *memcg); |
239 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg); | |
2e72b634 | 240 | |
8cdea7c0 BS |
241 | /* |
242 | * The memory controller data structure. The memory controller controls both | |
243 | * page cache and RSS per cgroup. We would eventually like to provide | |
244 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, | |
245 | * to help the administrator determine what knobs to tune. | |
246 | * | |
247 | * TODO: Add a water mark for the memory controller. Reclaim will begin when | |
8a9f3ccd BS |
248 | * we hit the water mark. May be even add a low water mark, such that |
249 | * no reclaim occurs from a cgroup at it's low water mark, this is | |
250 | * a feature that will be implemented much later in the future. | |
8cdea7c0 BS |
251 | */ |
252 | struct mem_cgroup { | |
253 | struct cgroup_subsys_state css; | |
254 | /* | |
255 | * the counter to account for memory usage | |
256 | */ | |
257 | struct res_counter res; | |
59927fb9 HD |
258 | |
259 | union { | |
260 | /* | |
261 | * the counter to account for mem+swap usage. | |
262 | */ | |
263 | struct res_counter memsw; | |
264 | ||
265 | /* | |
266 | * rcu_freeing is used only when freeing struct mem_cgroup, | |
267 | * so put it into a union to avoid wasting more memory. | |
268 | * It must be disjoint from the css field. It could be | |
269 | * in a union with the res field, but res plays a much | |
270 | * larger part in mem_cgroup life than memsw, and might | |
271 | * be of interest, even at time of free, when debugging. | |
272 | * So share rcu_head with the less interesting memsw. | |
273 | */ | |
274 | struct rcu_head rcu_freeing; | |
275 | /* | |
3afe36b1 GC |
276 | * We also need some space for a worker in deferred freeing. |
277 | * By the time we call it, rcu_freeing is no longer in use. | |
59927fb9 HD |
278 | */ |
279 | struct work_struct work_freeing; | |
280 | }; | |
281 | ||
510fc4e1 GC |
282 | /* |
283 | * the counter to account for kernel memory usage. | |
284 | */ | |
285 | struct res_counter kmem; | |
78fb7466 PE |
286 | /* |
287 | * Per cgroup active and inactive list, similar to the | |
288 | * per zone LRU lists. | |
78fb7466 | 289 | */ |
6d12e2d8 | 290 | struct mem_cgroup_lru_info info; |
889976db YH |
291 | int last_scanned_node; |
292 | #if MAX_NUMNODES > 1 | |
293 | nodemask_t scan_nodes; | |
453a9bf3 KH |
294 | atomic_t numainfo_events; |
295 | atomic_t numainfo_updating; | |
889976db | 296 | #endif |
18f59ea7 BS |
297 | /* |
298 | * Should the accounting and control be hierarchical, per subtree? | |
299 | */ | |
300 | bool use_hierarchy; | |
510fc4e1 | 301 | unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */ |
79dfdacc MH |
302 | |
303 | bool oom_lock; | |
304 | atomic_t under_oom; | |
305 | ||
8c7c6e34 | 306 | atomic_t refcnt; |
14797e23 | 307 | |
1f4c025b | 308 | int swappiness; |
3c11ecf4 KH |
309 | /* OOM-Killer disable */ |
310 | int oom_kill_disable; | |
a7885eb8 | 311 | |
22a668d7 KH |
312 | /* set when res.limit == memsw.limit */ |
313 | bool memsw_is_minimum; | |
314 | ||
2e72b634 KS |
315 | /* protect arrays of thresholds */ |
316 | struct mutex thresholds_lock; | |
317 | ||
318 | /* thresholds for memory usage. RCU-protected */ | |
2c488db2 | 319 | struct mem_cgroup_thresholds thresholds; |
907860ed | 320 | |
2e72b634 | 321 | /* thresholds for mem+swap usage. RCU-protected */ |
2c488db2 | 322 | struct mem_cgroup_thresholds memsw_thresholds; |
907860ed | 323 | |
9490ff27 KH |
324 | /* For oom notifier event fd */ |
325 | struct list_head oom_notify; | |
185efc0f | 326 | |
7dc74be0 DN |
327 | /* |
328 | * Should we move charges of a task when a task is moved into this | |
329 | * mem_cgroup ? And what type of charges should we move ? | |
330 | */ | |
331 | unsigned long move_charge_at_immigrate; | |
619d094b KH |
332 | /* |
333 | * set > 0 if pages under this cgroup are moving to other cgroup. | |
334 | */ | |
335 | atomic_t moving_account; | |
312734c0 KH |
336 | /* taken only while moving_account > 0 */ |
337 | spinlock_t move_lock; | |
d52aa412 | 338 | /* |
c62b1a3b | 339 | * percpu counter. |
d52aa412 | 340 | */ |
3a7951b4 | 341 | struct mem_cgroup_stat_cpu __percpu *stat; |
711d3d2c KH |
342 | /* |
343 | * used when a cpu is offlined or other synchronizations | |
344 | * See mem_cgroup_read_stat(). | |
345 | */ | |
346 | struct mem_cgroup_stat_cpu nocpu_base; | |
347 | spinlock_t pcp_counter_lock; | |
d1a4c0b3 | 348 | |
4bd2c1ee | 349 | #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET) |
d1a4c0b3 GC |
350 | struct tcp_memcontrol tcp_mem; |
351 | #endif | |
2633d7a0 GC |
352 | #if defined(CONFIG_MEMCG_KMEM) |
353 | /* analogous to slab_common's slab_caches list. per-memcg */ | |
354 | struct list_head memcg_slab_caches; | |
355 | /* Not a spinlock, we can take a lot of time walking the list */ | |
356 | struct mutex slab_caches_mutex; | |
357 | /* Index in the kmem_cache->memcg_params->memcg_caches array */ | |
358 | int kmemcg_id; | |
359 | #endif | |
8cdea7c0 BS |
360 | }; |
361 | ||
510fc4e1 GC |
362 | /* internal only representation about the status of kmem accounting. */ |
363 | enum { | |
364 | KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */ | |
a8964b9b | 365 | KMEM_ACCOUNTED_ACTIVATED, /* static key enabled. */ |
7de37682 | 366 | KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */ |
510fc4e1 GC |
367 | }; |
368 | ||
a8964b9b GC |
369 | /* We account when limit is on, but only after call sites are patched */ |
370 | #define KMEM_ACCOUNTED_MASK \ | |
371 | ((1 << KMEM_ACCOUNTED_ACTIVE) | (1 << KMEM_ACCOUNTED_ACTIVATED)) | |
510fc4e1 GC |
372 | |
373 | #ifdef CONFIG_MEMCG_KMEM | |
374 | static inline void memcg_kmem_set_active(struct mem_cgroup *memcg) | |
375 | { | |
376 | set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags); | |
377 | } | |
7de37682 GC |
378 | |
379 | static bool memcg_kmem_is_active(struct mem_cgroup *memcg) | |
380 | { | |
381 | return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags); | |
382 | } | |
383 | ||
a8964b9b GC |
384 | static void memcg_kmem_set_activated(struct mem_cgroup *memcg) |
385 | { | |
386 | set_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags); | |
387 | } | |
388 | ||
55007d84 GC |
389 | static void memcg_kmem_clear_activated(struct mem_cgroup *memcg) |
390 | { | |
391 | clear_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags); | |
392 | } | |
393 | ||
7de37682 GC |
394 | static void memcg_kmem_mark_dead(struct mem_cgroup *memcg) |
395 | { | |
396 | if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags)) | |
397 | set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags); | |
398 | } | |
399 | ||
400 | static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg) | |
401 | { | |
402 | return test_and_clear_bit(KMEM_ACCOUNTED_DEAD, | |
403 | &memcg->kmem_account_flags); | |
404 | } | |
510fc4e1 GC |
405 | #endif |
406 | ||
7dc74be0 DN |
407 | /* Stuffs for move charges at task migration. */ |
408 | /* | |
409 | * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a | |
410 | * left-shifted bitmap of these types. | |
411 | */ | |
412 | enum move_type { | |
4ffef5fe | 413 | MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */ |
87946a72 | 414 | MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */ |
7dc74be0 DN |
415 | NR_MOVE_TYPE, |
416 | }; | |
417 | ||
4ffef5fe DN |
418 | /* "mc" and its members are protected by cgroup_mutex */ |
419 | static struct move_charge_struct { | |
b1dd693e | 420 | spinlock_t lock; /* for from, to */ |
4ffef5fe DN |
421 | struct mem_cgroup *from; |
422 | struct mem_cgroup *to; | |
423 | unsigned long precharge; | |
854ffa8d | 424 | unsigned long moved_charge; |
483c30b5 | 425 | unsigned long moved_swap; |
8033b97c DN |
426 | struct task_struct *moving_task; /* a task moving charges */ |
427 | wait_queue_head_t waitq; /* a waitq for other context */ | |
428 | } mc = { | |
2bd9bb20 | 429 | .lock = __SPIN_LOCK_UNLOCKED(mc.lock), |
8033b97c DN |
430 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), |
431 | }; | |
4ffef5fe | 432 | |
90254a65 DN |
433 | static bool move_anon(void) |
434 | { | |
435 | return test_bit(MOVE_CHARGE_TYPE_ANON, | |
436 | &mc.to->move_charge_at_immigrate); | |
437 | } | |
438 | ||
87946a72 DN |
439 | static bool move_file(void) |
440 | { | |
441 | return test_bit(MOVE_CHARGE_TYPE_FILE, | |
442 | &mc.to->move_charge_at_immigrate); | |
443 | } | |
444 | ||
4e416953 BS |
445 | /* |
446 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft | |
447 | * limit reclaim to prevent infinite loops, if they ever occur. | |
448 | */ | |
a0db00fc KS |
449 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS 100 |
450 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2 | |
4e416953 | 451 | |
217bc319 KH |
452 | enum charge_type { |
453 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, | |
41326c17 | 454 | MEM_CGROUP_CHARGE_TYPE_ANON, |
d13d1443 | 455 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
8a9478ca | 456 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
c05555b5 KH |
457 | NR_CHARGE_TYPE, |
458 | }; | |
459 | ||
8c7c6e34 | 460 | /* for encoding cft->private value on file */ |
86ae53e1 GC |
461 | enum res_type { |
462 | _MEM, | |
463 | _MEMSWAP, | |
464 | _OOM_TYPE, | |
510fc4e1 | 465 | _KMEM, |
86ae53e1 GC |
466 | }; |
467 | ||
a0db00fc KS |
468 | #define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val)) |
469 | #define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff) | |
8c7c6e34 | 470 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
9490ff27 KH |
471 | /* Used for OOM nofiier */ |
472 | #define OOM_CONTROL (0) | |
8c7c6e34 | 473 | |
75822b44 BS |
474 | /* |
475 | * Reclaim flags for mem_cgroup_hierarchical_reclaim | |
476 | */ | |
477 | #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0 | |
478 | #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT) | |
479 | #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1 | |
480 | #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT) | |
481 | ||
c0ff4b85 R |
482 | static void mem_cgroup_get(struct mem_cgroup *memcg); |
483 | static void mem_cgroup_put(struct mem_cgroup *memcg); | |
e1aab161 | 484 | |
b2145145 WL |
485 | static inline |
486 | struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s) | |
487 | { | |
488 | return container_of(s, struct mem_cgroup, css); | |
489 | } | |
490 | ||
7ffc0edc MH |
491 | static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) |
492 | { | |
493 | return (memcg == root_mem_cgroup); | |
494 | } | |
495 | ||
e1aab161 | 496 | /* Writing them here to avoid exposing memcg's inner layout */ |
4bd2c1ee | 497 | #if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM) |
e1aab161 | 498 | |
e1aab161 GC |
499 | void sock_update_memcg(struct sock *sk) |
500 | { | |
376be5ff | 501 | if (mem_cgroup_sockets_enabled) { |
e1aab161 | 502 | struct mem_cgroup *memcg; |
3f134619 | 503 | struct cg_proto *cg_proto; |
e1aab161 GC |
504 | |
505 | BUG_ON(!sk->sk_prot->proto_cgroup); | |
506 | ||
f3f511e1 GC |
507 | /* Socket cloning can throw us here with sk_cgrp already |
508 | * filled. It won't however, necessarily happen from | |
509 | * process context. So the test for root memcg given | |
510 | * the current task's memcg won't help us in this case. | |
511 | * | |
512 | * Respecting the original socket's memcg is a better | |
513 | * decision in this case. | |
514 | */ | |
515 | if (sk->sk_cgrp) { | |
516 | BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg)); | |
517 | mem_cgroup_get(sk->sk_cgrp->memcg); | |
518 | return; | |
519 | } | |
520 | ||
e1aab161 GC |
521 | rcu_read_lock(); |
522 | memcg = mem_cgroup_from_task(current); | |
3f134619 GC |
523 | cg_proto = sk->sk_prot->proto_cgroup(memcg); |
524 | if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) { | |
e1aab161 | 525 | mem_cgroup_get(memcg); |
3f134619 | 526 | sk->sk_cgrp = cg_proto; |
e1aab161 GC |
527 | } |
528 | rcu_read_unlock(); | |
529 | } | |
530 | } | |
531 | EXPORT_SYMBOL(sock_update_memcg); | |
532 | ||
533 | void sock_release_memcg(struct sock *sk) | |
534 | { | |
376be5ff | 535 | if (mem_cgroup_sockets_enabled && sk->sk_cgrp) { |
e1aab161 GC |
536 | struct mem_cgroup *memcg; |
537 | WARN_ON(!sk->sk_cgrp->memcg); | |
538 | memcg = sk->sk_cgrp->memcg; | |
539 | mem_cgroup_put(memcg); | |
540 | } | |
541 | } | |
d1a4c0b3 GC |
542 | |
543 | struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg) | |
544 | { | |
545 | if (!memcg || mem_cgroup_is_root(memcg)) | |
546 | return NULL; | |
547 | ||
548 | return &memcg->tcp_mem.cg_proto; | |
549 | } | |
550 | EXPORT_SYMBOL(tcp_proto_cgroup); | |
e1aab161 | 551 | |
3f134619 GC |
552 | static void disarm_sock_keys(struct mem_cgroup *memcg) |
553 | { | |
554 | if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto)) | |
555 | return; | |
556 | static_key_slow_dec(&memcg_socket_limit_enabled); | |
557 | } | |
558 | #else | |
559 | static void disarm_sock_keys(struct mem_cgroup *memcg) | |
560 | { | |
561 | } | |
562 | #endif | |
563 | ||
a8964b9b | 564 | #ifdef CONFIG_MEMCG_KMEM |
55007d84 GC |
565 | /* |
566 | * This will be the memcg's index in each cache's ->memcg_params->memcg_caches. | |
567 | * There are two main reasons for not using the css_id for this: | |
568 | * 1) this works better in sparse environments, where we have a lot of memcgs, | |
569 | * but only a few kmem-limited. Or also, if we have, for instance, 200 | |
570 | * memcgs, and none but the 200th is kmem-limited, we'd have to have a | |
571 | * 200 entry array for that. | |
572 | * | |
573 | * 2) In order not to violate the cgroup API, we would like to do all memory | |
574 | * allocation in ->create(). At that point, we haven't yet allocated the | |
575 | * css_id. Having a separate index prevents us from messing with the cgroup | |
576 | * core for this | |
577 | * | |
578 | * The current size of the caches array is stored in | |
579 | * memcg_limited_groups_array_size. It will double each time we have to | |
580 | * increase it. | |
581 | */ | |
582 | static DEFINE_IDA(kmem_limited_groups); | |
749c5415 GC |
583 | int memcg_limited_groups_array_size; |
584 | ||
55007d84 GC |
585 | /* |
586 | * MIN_SIZE is different than 1, because we would like to avoid going through | |
587 | * the alloc/free process all the time. In a small machine, 4 kmem-limited | |
588 | * cgroups is a reasonable guess. In the future, it could be a parameter or | |
589 | * tunable, but that is strictly not necessary. | |
590 | * | |
591 | * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get | |
592 | * this constant directly from cgroup, but it is understandable that this is | |
593 | * better kept as an internal representation in cgroup.c. In any case, the | |
594 | * css_id space is not getting any smaller, and we don't have to necessarily | |
595 | * increase ours as well if it increases. | |
596 | */ | |
597 | #define MEMCG_CACHES_MIN_SIZE 4 | |
598 | #define MEMCG_CACHES_MAX_SIZE 65535 | |
599 | ||
d7f25f8a GC |
600 | /* |
601 | * A lot of the calls to the cache allocation functions are expected to be | |
602 | * inlined by the compiler. Since the calls to memcg_kmem_get_cache are | |
603 | * conditional to this static branch, we'll have to allow modules that does | |
604 | * kmem_cache_alloc and the such to see this symbol as well | |
605 | */ | |
a8964b9b | 606 | struct static_key memcg_kmem_enabled_key; |
d7f25f8a | 607 | EXPORT_SYMBOL(memcg_kmem_enabled_key); |
a8964b9b GC |
608 | |
609 | static void disarm_kmem_keys(struct mem_cgroup *memcg) | |
610 | { | |
55007d84 | 611 | if (memcg_kmem_is_active(memcg)) { |
a8964b9b | 612 | static_key_slow_dec(&memcg_kmem_enabled_key); |
55007d84 GC |
613 | ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id); |
614 | } | |
bea207c8 GC |
615 | /* |
616 | * This check can't live in kmem destruction function, | |
617 | * since the charges will outlive the cgroup | |
618 | */ | |
619 | WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0); | |
a8964b9b GC |
620 | } |
621 | #else | |
622 | static void disarm_kmem_keys(struct mem_cgroup *memcg) | |
623 | { | |
624 | } | |
625 | #endif /* CONFIG_MEMCG_KMEM */ | |
626 | ||
627 | static void disarm_static_keys(struct mem_cgroup *memcg) | |
628 | { | |
629 | disarm_sock_keys(memcg); | |
630 | disarm_kmem_keys(memcg); | |
631 | } | |
632 | ||
c0ff4b85 | 633 | static void drain_all_stock_async(struct mem_cgroup *memcg); |
8c7c6e34 | 634 | |
f64c3f54 | 635 | static struct mem_cgroup_per_zone * |
c0ff4b85 | 636 | mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid) |
f64c3f54 | 637 | { |
c0ff4b85 | 638 | return &memcg->info.nodeinfo[nid]->zoneinfo[zid]; |
f64c3f54 BS |
639 | } |
640 | ||
c0ff4b85 | 641 | struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg) |
d324236b | 642 | { |
c0ff4b85 | 643 | return &memcg->css; |
d324236b WF |
644 | } |
645 | ||
f64c3f54 | 646 | static struct mem_cgroup_per_zone * |
c0ff4b85 | 647 | page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page) |
f64c3f54 | 648 | { |
97a6c37b JW |
649 | int nid = page_to_nid(page); |
650 | int zid = page_zonenum(page); | |
f64c3f54 | 651 | |
c0ff4b85 | 652 | return mem_cgroup_zoneinfo(memcg, nid, zid); |
f64c3f54 BS |
653 | } |
654 | ||
655 | static struct mem_cgroup_tree_per_zone * | |
656 | soft_limit_tree_node_zone(int nid, int zid) | |
657 | { | |
658 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; | |
659 | } | |
660 | ||
661 | static struct mem_cgroup_tree_per_zone * | |
662 | soft_limit_tree_from_page(struct page *page) | |
663 | { | |
664 | int nid = page_to_nid(page); | |
665 | int zid = page_zonenum(page); | |
666 | ||
667 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; | |
668 | } | |
669 | ||
670 | static void | |
c0ff4b85 | 671 | __mem_cgroup_insert_exceeded(struct mem_cgroup *memcg, |
f64c3f54 | 672 | struct mem_cgroup_per_zone *mz, |
ef8745c1 KH |
673 | struct mem_cgroup_tree_per_zone *mctz, |
674 | unsigned long long new_usage_in_excess) | |
f64c3f54 BS |
675 | { |
676 | struct rb_node **p = &mctz->rb_root.rb_node; | |
677 | struct rb_node *parent = NULL; | |
678 | struct mem_cgroup_per_zone *mz_node; | |
679 | ||
680 | if (mz->on_tree) | |
681 | return; | |
682 | ||
ef8745c1 KH |
683 | mz->usage_in_excess = new_usage_in_excess; |
684 | if (!mz->usage_in_excess) | |
685 | return; | |
f64c3f54 BS |
686 | while (*p) { |
687 | parent = *p; | |
688 | mz_node = rb_entry(parent, struct mem_cgroup_per_zone, | |
689 | tree_node); | |
690 | if (mz->usage_in_excess < mz_node->usage_in_excess) | |
691 | p = &(*p)->rb_left; | |
692 | /* | |
693 | * We can't avoid mem cgroups that are over their soft | |
694 | * limit by the same amount | |
695 | */ | |
696 | else if (mz->usage_in_excess >= mz_node->usage_in_excess) | |
697 | p = &(*p)->rb_right; | |
698 | } | |
699 | rb_link_node(&mz->tree_node, parent, p); | |
700 | rb_insert_color(&mz->tree_node, &mctz->rb_root); | |
701 | mz->on_tree = true; | |
4e416953 BS |
702 | } |
703 | ||
704 | static void | |
c0ff4b85 | 705 | __mem_cgroup_remove_exceeded(struct mem_cgroup *memcg, |
4e416953 BS |
706 | struct mem_cgroup_per_zone *mz, |
707 | struct mem_cgroup_tree_per_zone *mctz) | |
708 | { | |
709 | if (!mz->on_tree) | |
710 | return; | |
711 | rb_erase(&mz->tree_node, &mctz->rb_root); | |
712 | mz->on_tree = false; | |
713 | } | |
714 | ||
f64c3f54 | 715 | static void |
c0ff4b85 | 716 | mem_cgroup_remove_exceeded(struct mem_cgroup *memcg, |
f64c3f54 BS |
717 | struct mem_cgroup_per_zone *mz, |
718 | struct mem_cgroup_tree_per_zone *mctz) | |
719 | { | |
720 | spin_lock(&mctz->lock); | |
c0ff4b85 | 721 | __mem_cgroup_remove_exceeded(memcg, mz, mctz); |
f64c3f54 BS |
722 | spin_unlock(&mctz->lock); |
723 | } | |
724 | ||
f64c3f54 | 725 | |
c0ff4b85 | 726 | static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page) |
f64c3f54 | 727 | { |
ef8745c1 | 728 | unsigned long long excess; |
f64c3f54 BS |
729 | struct mem_cgroup_per_zone *mz; |
730 | struct mem_cgroup_tree_per_zone *mctz; | |
4e649152 KH |
731 | int nid = page_to_nid(page); |
732 | int zid = page_zonenum(page); | |
f64c3f54 BS |
733 | mctz = soft_limit_tree_from_page(page); |
734 | ||
735 | /* | |
4e649152 KH |
736 | * Necessary to update all ancestors when hierarchy is used. |
737 | * because their event counter is not touched. | |
f64c3f54 | 738 | */ |
c0ff4b85 R |
739 | for (; memcg; memcg = parent_mem_cgroup(memcg)) { |
740 | mz = mem_cgroup_zoneinfo(memcg, nid, zid); | |
741 | excess = res_counter_soft_limit_excess(&memcg->res); | |
4e649152 KH |
742 | /* |
743 | * We have to update the tree if mz is on RB-tree or | |
744 | * mem is over its softlimit. | |
745 | */ | |
ef8745c1 | 746 | if (excess || mz->on_tree) { |
4e649152 KH |
747 | spin_lock(&mctz->lock); |
748 | /* if on-tree, remove it */ | |
749 | if (mz->on_tree) | |
c0ff4b85 | 750 | __mem_cgroup_remove_exceeded(memcg, mz, mctz); |
4e649152 | 751 | /* |
ef8745c1 KH |
752 | * Insert again. mz->usage_in_excess will be updated. |
753 | * If excess is 0, no tree ops. | |
4e649152 | 754 | */ |
c0ff4b85 | 755 | __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess); |
4e649152 KH |
756 | spin_unlock(&mctz->lock); |
757 | } | |
f64c3f54 BS |
758 | } |
759 | } | |
760 | ||
c0ff4b85 | 761 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg) |
f64c3f54 BS |
762 | { |
763 | int node, zone; | |
764 | struct mem_cgroup_per_zone *mz; | |
765 | struct mem_cgroup_tree_per_zone *mctz; | |
766 | ||
3ed28fa1 | 767 | for_each_node(node) { |
f64c3f54 | 768 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
c0ff4b85 | 769 | mz = mem_cgroup_zoneinfo(memcg, node, zone); |
f64c3f54 | 770 | mctz = soft_limit_tree_node_zone(node, zone); |
c0ff4b85 | 771 | mem_cgroup_remove_exceeded(memcg, mz, mctz); |
f64c3f54 BS |
772 | } |
773 | } | |
774 | } | |
775 | ||
4e416953 BS |
776 | static struct mem_cgroup_per_zone * |
777 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) | |
778 | { | |
779 | struct rb_node *rightmost = NULL; | |
26251eaf | 780 | struct mem_cgroup_per_zone *mz; |
4e416953 BS |
781 | |
782 | retry: | |
26251eaf | 783 | mz = NULL; |
4e416953 BS |
784 | rightmost = rb_last(&mctz->rb_root); |
785 | if (!rightmost) | |
786 | goto done; /* Nothing to reclaim from */ | |
787 | ||
788 | mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node); | |
789 | /* | |
790 | * Remove the node now but someone else can add it back, | |
791 | * we will to add it back at the end of reclaim to its correct | |
792 | * position in the tree. | |
793 | */ | |
d79154bb HD |
794 | __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz); |
795 | if (!res_counter_soft_limit_excess(&mz->memcg->res) || | |
796 | !css_tryget(&mz->memcg->css)) | |
4e416953 BS |
797 | goto retry; |
798 | done: | |
799 | return mz; | |
800 | } | |
801 | ||
802 | static struct mem_cgroup_per_zone * | |
803 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) | |
804 | { | |
805 | struct mem_cgroup_per_zone *mz; | |
806 | ||
807 | spin_lock(&mctz->lock); | |
808 | mz = __mem_cgroup_largest_soft_limit_node(mctz); | |
809 | spin_unlock(&mctz->lock); | |
810 | return mz; | |
811 | } | |
812 | ||
711d3d2c KH |
813 | /* |
814 | * Implementation Note: reading percpu statistics for memcg. | |
815 | * | |
816 | * Both of vmstat[] and percpu_counter has threshold and do periodic | |
817 | * synchronization to implement "quick" read. There are trade-off between | |
818 | * reading cost and precision of value. Then, we may have a chance to implement | |
819 | * a periodic synchronizion of counter in memcg's counter. | |
820 | * | |
821 | * But this _read() function is used for user interface now. The user accounts | |
822 | * memory usage by memory cgroup and he _always_ requires exact value because | |
823 | * he accounts memory. Even if we provide quick-and-fuzzy read, we always | |
824 | * have to visit all online cpus and make sum. So, for now, unnecessary | |
825 | * synchronization is not implemented. (just implemented for cpu hotplug) | |
826 | * | |
827 | * If there are kernel internal actions which can make use of some not-exact | |
828 | * value, and reading all cpu value can be performance bottleneck in some | |
829 | * common workload, threashold and synchonization as vmstat[] should be | |
830 | * implemented. | |
831 | */ | |
c0ff4b85 | 832 | static long mem_cgroup_read_stat(struct mem_cgroup *memcg, |
7a159cc9 | 833 | enum mem_cgroup_stat_index idx) |
c62b1a3b | 834 | { |
7a159cc9 | 835 | long val = 0; |
c62b1a3b | 836 | int cpu; |
c62b1a3b | 837 | |
711d3d2c KH |
838 | get_online_cpus(); |
839 | for_each_online_cpu(cpu) | |
c0ff4b85 | 840 | val += per_cpu(memcg->stat->count[idx], cpu); |
711d3d2c | 841 | #ifdef CONFIG_HOTPLUG_CPU |
c0ff4b85 R |
842 | spin_lock(&memcg->pcp_counter_lock); |
843 | val += memcg->nocpu_base.count[idx]; | |
844 | spin_unlock(&memcg->pcp_counter_lock); | |
711d3d2c KH |
845 | #endif |
846 | put_online_cpus(); | |
c62b1a3b KH |
847 | return val; |
848 | } | |
849 | ||
c0ff4b85 | 850 | static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg, |
0c3e73e8 BS |
851 | bool charge) |
852 | { | |
853 | int val = (charge) ? 1 : -1; | |
bff6bb83 | 854 | this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val); |
0c3e73e8 BS |
855 | } |
856 | ||
c0ff4b85 | 857 | static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg, |
e9f8974f JW |
858 | enum mem_cgroup_events_index idx) |
859 | { | |
860 | unsigned long val = 0; | |
861 | int cpu; | |
862 | ||
863 | for_each_online_cpu(cpu) | |
c0ff4b85 | 864 | val += per_cpu(memcg->stat->events[idx], cpu); |
e9f8974f | 865 | #ifdef CONFIG_HOTPLUG_CPU |
c0ff4b85 R |
866 | spin_lock(&memcg->pcp_counter_lock); |
867 | val += memcg->nocpu_base.events[idx]; | |
868 | spin_unlock(&memcg->pcp_counter_lock); | |
e9f8974f JW |
869 | #endif |
870 | return val; | |
871 | } | |
872 | ||
c0ff4b85 | 873 | static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg, |
b2402857 | 874 | bool anon, int nr_pages) |
d52aa412 | 875 | { |
c62b1a3b KH |
876 | preempt_disable(); |
877 | ||
b2402857 KH |
878 | /* |
879 | * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is | |
880 | * counted as CACHE even if it's on ANON LRU. | |
881 | */ | |
882 | if (anon) | |
883 | __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS], | |
c0ff4b85 | 884 | nr_pages); |
d52aa412 | 885 | else |
b2402857 | 886 | __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE], |
c0ff4b85 | 887 | nr_pages); |
55e462b0 | 888 | |
e401f176 KH |
889 | /* pagein of a big page is an event. So, ignore page size */ |
890 | if (nr_pages > 0) | |
c0ff4b85 | 891 | __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]); |
3751d604 | 892 | else { |
c0ff4b85 | 893 | __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]); |
3751d604 KH |
894 | nr_pages = -nr_pages; /* for event */ |
895 | } | |
e401f176 | 896 | |
13114716 | 897 | __this_cpu_add(memcg->stat->nr_page_events, nr_pages); |
2e72b634 | 898 | |
c62b1a3b | 899 | preempt_enable(); |
6d12e2d8 KH |
900 | } |
901 | ||
bb2a0de9 | 902 | unsigned long |
4d7dcca2 | 903 | mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru) |
074291fe KK |
904 | { |
905 | struct mem_cgroup_per_zone *mz; | |
906 | ||
907 | mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec); | |
908 | return mz->lru_size[lru]; | |
909 | } | |
910 | ||
911 | static unsigned long | |
c0ff4b85 | 912 | mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid, |
bb2a0de9 | 913 | unsigned int lru_mask) |
889976db YH |
914 | { |
915 | struct mem_cgroup_per_zone *mz; | |
f156ab93 | 916 | enum lru_list lru; |
bb2a0de9 KH |
917 | unsigned long ret = 0; |
918 | ||
c0ff4b85 | 919 | mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
bb2a0de9 | 920 | |
f156ab93 HD |
921 | for_each_lru(lru) { |
922 | if (BIT(lru) & lru_mask) | |
923 | ret += mz->lru_size[lru]; | |
bb2a0de9 KH |
924 | } |
925 | return ret; | |
926 | } | |
927 | ||
928 | static unsigned long | |
c0ff4b85 | 929 | mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg, |
bb2a0de9 KH |
930 | int nid, unsigned int lru_mask) |
931 | { | |
889976db YH |
932 | u64 total = 0; |
933 | int zid; | |
934 | ||
bb2a0de9 | 935 | for (zid = 0; zid < MAX_NR_ZONES; zid++) |
c0ff4b85 R |
936 | total += mem_cgroup_zone_nr_lru_pages(memcg, |
937 | nid, zid, lru_mask); | |
bb2a0de9 | 938 | |
889976db YH |
939 | return total; |
940 | } | |
bb2a0de9 | 941 | |
c0ff4b85 | 942 | static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg, |
bb2a0de9 | 943 | unsigned int lru_mask) |
6d12e2d8 | 944 | { |
889976db | 945 | int nid; |
6d12e2d8 KH |
946 | u64 total = 0; |
947 | ||
31aaea4a | 948 | for_each_node_state(nid, N_MEMORY) |
c0ff4b85 | 949 | total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask); |
6d12e2d8 | 950 | return total; |
d52aa412 KH |
951 | } |
952 | ||
f53d7ce3 JW |
953 | static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg, |
954 | enum mem_cgroup_events_target target) | |
7a159cc9 JW |
955 | { |
956 | unsigned long val, next; | |
957 | ||
13114716 | 958 | val = __this_cpu_read(memcg->stat->nr_page_events); |
4799401f | 959 | next = __this_cpu_read(memcg->stat->targets[target]); |
7a159cc9 | 960 | /* from time_after() in jiffies.h */ |
f53d7ce3 JW |
961 | if ((long)next - (long)val < 0) { |
962 | switch (target) { | |
963 | case MEM_CGROUP_TARGET_THRESH: | |
964 | next = val + THRESHOLDS_EVENTS_TARGET; | |
965 | break; | |
966 | case MEM_CGROUP_TARGET_SOFTLIMIT: | |
967 | next = val + SOFTLIMIT_EVENTS_TARGET; | |
968 | break; | |
969 | case MEM_CGROUP_TARGET_NUMAINFO: | |
970 | next = val + NUMAINFO_EVENTS_TARGET; | |
971 | break; | |
972 | default: | |
973 | break; | |
974 | } | |
975 | __this_cpu_write(memcg->stat->targets[target], next); | |
976 | return true; | |
7a159cc9 | 977 | } |
f53d7ce3 | 978 | return false; |
d2265e6f KH |
979 | } |
980 | ||
981 | /* | |
982 | * Check events in order. | |
983 | * | |
984 | */ | |
c0ff4b85 | 985 | static void memcg_check_events(struct mem_cgroup *memcg, struct page *page) |
d2265e6f | 986 | { |
4799401f | 987 | preempt_disable(); |
d2265e6f | 988 | /* threshold event is triggered in finer grain than soft limit */ |
f53d7ce3 JW |
989 | if (unlikely(mem_cgroup_event_ratelimit(memcg, |
990 | MEM_CGROUP_TARGET_THRESH))) { | |
82b3f2a7 AM |
991 | bool do_softlimit; |
992 | bool do_numainfo __maybe_unused; | |
f53d7ce3 JW |
993 | |
994 | do_softlimit = mem_cgroup_event_ratelimit(memcg, | |
995 | MEM_CGROUP_TARGET_SOFTLIMIT); | |
996 | #if MAX_NUMNODES > 1 | |
997 | do_numainfo = mem_cgroup_event_ratelimit(memcg, | |
998 | MEM_CGROUP_TARGET_NUMAINFO); | |
999 | #endif | |
1000 | preempt_enable(); | |
1001 | ||
c0ff4b85 | 1002 | mem_cgroup_threshold(memcg); |
f53d7ce3 | 1003 | if (unlikely(do_softlimit)) |
c0ff4b85 | 1004 | mem_cgroup_update_tree(memcg, page); |
453a9bf3 | 1005 | #if MAX_NUMNODES > 1 |
f53d7ce3 | 1006 | if (unlikely(do_numainfo)) |
c0ff4b85 | 1007 | atomic_inc(&memcg->numainfo_events); |
453a9bf3 | 1008 | #endif |
f53d7ce3 JW |
1009 | } else |
1010 | preempt_enable(); | |
d2265e6f KH |
1011 | } |
1012 | ||
d1a4c0b3 | 1013 | struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
8cdea7c0 | 1014 | { |
b2145145 WL |
1015 | return mem_cgroup_from_css( |
1016 | cgroup_subsys_state(cont, mem_cgroup_subsys_id)); | |
8cdea7c0 BS |
1017 | } |
1018 | ||
cf475ad2 | 1019 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
78fb7466 | 1020 | { |
31a78f23 BS |
1021 | /* |
1022 | * mm_update_next_owner() may clear mm->owner to NULL | |
1023 | * if it races with swapoff, page migration, etc. | |
1024 | * So this can be called with p == NULL. | |
1025 | */ | |
1026 | if (unlikely(!p)) | |
1027 | return NULL; | |
1028 | ||
b2145145 | 1029 | return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id)); |
78fb7466 PE |
1030 | } |
1031 | ||
a433658c | 1032 | struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) |
54595fe2 | 1033 | { |
c0ff4b85 | 1034 | struct mem_cgroup *memcg = NULL; |
0b7f569e KH |
1035 | |
1036 | if (!mm) | |
1037 | return NULL; | |
54595fe2 KH |
1038 | /* |
1039 | * Because we have no locks, mm->owner's may be being moved to other | |
1040 | * cgroup. We use css_tryget() here even if this looks | |
1041 | * pessimistic (rather than adding locks here). | |
1042 | */ | |
1043 | rcu_read_lock(); | |
1044 | do { | |
c0ff4b85 R |
1045 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
1046 | if (unlikely(!memcg)) | |
54595fe2 | 1047 | break; |
c0ff4b85 | 1048 | } while (!css_tryget(&memcg->css)); |
54595fe2 | 1049 | rcu_read_unlock(); |
c0ff4b85 | 1050 | return memcg; |
54595fe2 KH |
1051 | } |
1052 | ||
5660048c JW |
1053 | /** |
1054 | * mem_cgroup_iter - iterate over memory cgroup hierarchy | |
1055 | * @root: hierarchy root | |
1056 | * @prev: previously returned memcg, NULL on first invocation | |
1057 | * @reclaim: cookie for shared reclaim walks, NULL for full walks | |
1058 | * | |
1059 | * Returns references to children of the hierarchy below @root, or | |
1060 | * @root itself, or %NULL after a full round-trip. | |
1061 | * | |
1062 | * Caller must pass the return value in @prev on subsequent | |
1063 | * invocations for reference counting, or use mem_cgroup_iter_break() | |
1064 | * to cancel a hierarchy walk before the round-trip is complete. | |
1065 | * | |
1066 | * Reclaimers can specify a zone and a priority level in @reclaim to | |
1067 | * divide up the memcgs in the hierarchy among all concurrent | |
1068 | * reclaimers operating on the same zone and priority. | |
1069 | */ | |
1070 | struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, | |
1071 | struct mem_cgroup *prev, | |
1072 | struct mem_cgroup_reclaim_cookie *reclaim) | |
14067bb3 | 1073 | { |
9f3a0d09 JW |
1074 | struct mem_cgroup *memcg = NULL; |
1075 | int id = 0; | |
711d3d2c | 1076 | |
5660048c JW |
1077 | if (mem_cgroup_disabled()) |
1078 | return NULL; | |
1079 | ||
9f3a0d09 JW |
1080 | if (!root) |
1081 | root = root_mem_cgroup; | |
7d74b06f | 1082 | |
9f3a0d09 JW |
1083 | if (prev && !reclaim) |
1084 | id = css_id(&prev->css); | |
14067bb3 | 1085 | |
9f3a0d09 JW |
1086 | if (prev && prev != root) |
1087 | css_put(&prev->css); | |
14067bb3 | 1088 | |
9f3a0d09 JW |
1089 | if (!root->use_hierarchy && root != root_mem_cgroup) { |
1090 | if (prev) | |
1091 | return NULL; | |
1092 | return root; | |
1093 | } | |
14067bb3 | 1094 | |
9f3a0d09 | 1095 | while (!memcg) { |
527a5ec9 | 1096 | struct mem_cgroup_reclaim_iter *uninitialized_var(iter); |
9f3a0d09 | 1097 | struct cgroup_subsys_state *css; |
711d3d2c | 1098 | |
527a5ec9 JW |
1099 | if (reclaim) { |
1100 | int nid = zone_to_nid(reclaim->zone); | |
1101 | int zid = zone_idx(reclaim->zone); | |
1102 | struct mem_cgroup_per_zone *mz; | |
1103 | ||
1104 | mz = mem_cgroup_zoneinfo(root, nid, zid); | |
1105 | iter = &mz->reclaim_iter[reclaim->priority]; | |
1106 | if (prev && reclaim->generation != iter->generation) | |
1107 | return NULL; | |
1108 | id = iter->position; | |
1109 | } | |
7d74b06f | 1110 | |
9f3a0d09 JW |
1111 | rcu_read_lock(); |
1112 | css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id); | |
1113 | if (css) { | |
1114 | if (css == &root->css || css_tryget(css)) | |
b2145145 | 1115 | memcg = mem_cgroup_from_css(css); |
9f3a0d09 JW |
1116 | } else |
1117 | id = 0; | |
14067bb3 | 1118 | rcu_read_unlock(); |
14067bb3 | 1119 | |
527a5ec9 JW |
1120 | if (reclaim) { |
1121 | iter->position = id; | |
1122 | if (!css) | |
1123 | iter->generation++; | |
1124 | else if (!prev && memcg) | |
1125 | reclaim->generation = iter->generation; | |
1126 | } | |
9f3a0d09 JW |
1127 | |
1128 | if (prev && !css) | |
1129 | return NULL; | |
1130 | } | |
1131 | return memcg; | |
14067bb3 | 1132 | } |
7d74b06f | 1133 | |
5660048c JW |
1134 | /** |
1135 | * mem_cgroup_iter_break - abort a hierarchy walk prematurely | |
1136 | * @root: hierarchy root | |
1137 | * @prev: last visited hierarchy member as returned by mem_cgroup_iter() | |
1138 | */ | |
1139 | void mem_cgroup_iter_break(struct mem_cgroup *root, | |
1140 | struct mem_cgroup *prev) | |
9f3a0d09 JW |
1141 | { |
1142 | if (!root) | |
1143 | root = root_mem_cgroup; | |
1144 | if (prev && prev != root) | |
1145 | css_put(&prev->css); | |
1146 | } | |
7d74b06f | 1147 | |
9f3a0d09 JW |
1148 | /* |
1149 | * Iteration constructs for visiting all cgroups (under a tree). If | |
1150 | * loops are exited prematurely (break), mem_cgroup_iter_break() must | |
1151 | * be used for reference counting. | |
1152 | */ | |
1153 | #define for_each_mem_cgroup_tree(iter, root) \ | |
527a5ec9 | 1154 | for (iter = mem_cgroup_iter(root, NULL, NULL); \ |
9f3a0d09 | 1155 | iter != NULL; \ |
527a5ec9 | 1156 | iter = mem_cgroup_iter(root, iter, NULL)) |
711d3d2c | 1157 | |
9f3a0d09 | 1158 | #define for_each_mem_cgroup(iter) \ |
527a5ec9 | 1159 | for (iter = mem_cgroup_iter(NULL, NULL, NULL); \ |
9f3a0d09 | 1160 | iter != NULL; \ |
527a5ec9 | 1161 | iter = mem_cgroup_iter(NULL, iter, NULL)) |
14067bb3 | 1162 | |
68ae564b | 1163 | void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx) |
456f998e | 1164 | { |
c0ff4b85 | 1165 | struct mem_cgroup *memcg; |
456f998e | 1166 | |
456f998e | 1167 | rcu_read_lock(); |
c0ff4b85 R |
1168 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
1169 | if (unlikely(!memcg)) | |
456f998e YH |
1170 | goto out; |
1171 | ||
1172 | switch (idx) { | |
456f998e | 1173 | case PGFAULT: |
0e574a93 JW |
1174 | this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]); |
1175 | break; | |
1176 | case PGMAJFAULT: | |
1177 | this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]); | |
456f998e YH |
1178 | break; |
1179 | default: | |
1180 | BUG(); | |
1181 | } | |
1182 | out: | |
1183 | rcu_read_unlock(); | |
1184 | } | |
68ae564b | 1185 | EXPORT_SYMBOL(__mem_cgroup_count_vm_event); |
456f998e | 1186 | |
925b7673 JW |
1187 | /** |
1188 | * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg | |
1189 | * @zone: zone of the wanted lruvec | |
fa9add64 | 1190 | * @memcg: memcg of the wanted lruvec |
925b7673 JW |
1191 | * |
1192 | * Returns the lru list vector holding pages for the given @zone and | |
1193 | * @mem. This can be the global zone lruvec, if the memory controller | |
1194 | * is disabled. | |
1195 | */ | |
1196 | struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone, | |
1197 | struct mem_cgroup *memcg) | |
1198 | { | |
1199 | struct mem_cgroup_per_zone *mz; | |
bea8c150 | 1200 | struct lruvec *lruvec; |
925b7673 | 1201 | |
bea8c150 HD |
1202 | if (mem_cgroup_disabled()) { |
1203 | lruvec = &zone->lruvec; | |
1204 | goto out; | |
1205 | } | |
925b7673 JW |
1206 | |
1207 | mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone)); | |
bea8c150 HD |
1208 | lruvec = &mz->lruvec; |
1209 | out: | |
1210 | /* | |
1211 | * Since a node can be onlined after the mem_cgroup was created, | |
1212 | * we have to be prepared to initialize lruvec->zone here; | |
1213 | * and if offlined then reonlined, we need to reinitialize it. | |
1214 | */ | |
1215 | if (unlikely(lruvec->zone != zone)) | |
1216 | lruvec->zone = zone; | |
1217 | return lruvec; | |
925b7673 JW |
1218 | } |
1219 | ||
08e552c6 KH |
1220 | /* |
1221 | * Following LRU functions are allowed to be used without PCG_LOCK. | |
1222 | * Operations are called by routine of global LRU independently from memcg. | |
1223 | * What we have to take care of here is validness of pc->mem_cgroup. | |
1224 | * | |
1225 | * Changes to pc->mem_cgroup happens when | |
1226 | * 1. charge | |
1227 | * 2. moving account | |
1228 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. | |
1229 | * It is added to LRU before charge. | |
1230 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. | |
1231 | * When moving account, the page is not on LRU. It's isolated. | |
1232 | */ | |
4f98a2fe | 1233 | |
925b7673 | 1234 | /** |
fa9add64 | 1235 | * mem_cgroup_page_lruvec - return lruvec for adding an lru page |
925b7673 | 1236 | * @page: the page |
fa9add64 | 1237 | * @zone: zone of the page |
925b7673 | 1238 | */ |
fa9add64 | 1239 | struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone) |
08e552c6 | 1240 | { |
08e552c6 | 1241 | struct mem_cgroup_per_zone *mz; |
925b7673 JW |
1242 | struct mem_cgroup *memcg; |
1243 | struct page_cgroup *pc; | |
bea8c150 | 1244 | struct lruvec *lruvec; |
6d12e2d8 | 1245 | |
bea8c150 HD |
1246 | if (mem_cgroup_disabled()) { |
1247 | lruvec = &zone->lruvec; | |
1248 | goto out; | |
1249 | } | |
925b7673 | 1250 | |
08e552c6 | 1251 | pc = lookup_page_cgroup(page); |
38c5d72f | 1252 | memcg = pc->mem_cgroup; |
7512102c HD |
1253 | |
1254 | /* | |
fa9add64 | 1255 | * Surreptitiously switch any uncharged offlist page to root: |
7512102c HD |
1256 | * an uncharged page off lru does nothing to secure |
1257 | * its former mem_cgroup from sudden removal. | |
1258 | * | |
1259 | * Our caller holds lru_lock, and PageCgroupUsed is updated | |
1260 | * under page_cgroup lock: between them, they make all uses | |
1261 | * of pc->mem_cgroup safe. | |
1262 | */ | |
fa9add64 | 1263 | if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup) |
7512102c HD |
1264 | pc->mem_cgroup = memcg = root_mem_cgroup; |
1265 | ||
925b7673 | 1266 | mz = page_cgroup_zoneinfo(memcg, page); |
bea8c150 HD |
1267 | lruvec = &mz->lruvec; |
1268 | out: | |
1269 | /* | |
1270 | * Since a node can be onlined after the mem_cgroup was created, | |
1271 | * we have to be prepared to initialize lruvec->zone here; | |
1272 | * and if offlined then reonlined, we need to reinitialize it. | |
1273 | */ | |
1274 | if (unlikely(lruvec->zone != zone)) | |
1275 | lruvec->zone = zone; | |
1276 | return lruvec; | |
08e552c6 | 1277 | } |
b69408e8 | 1278 | |
925b7673 | 1279 | /** |
fa9add64 HD |
1280 | * mem_cgroup_update_lru_size - account for adding or removing an lru page |
1281 | * @lruvec: mem_cgroup per zone lru vector | |
1282 | * @lru: index of lru list the page is sitting on | |
1283 | * @nr_pages: positive when adding or negative when removing | |
925b7673 | 1284 | * |
fa9add64 HD |
1285 | * This function must be called when a page is added to or removed from an |
1286 | * lru list. | |
3f58a829 | 1287 | */ |
fa9add64 HD |
1288 | void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru, |
1289 | int nr_pages) | |
3f58a829 MK |
1290 | { |
1291 | struct mem_cgroup_per_zone *mz; | |
fa9add64 | 1292 | unsigned long *lru_size; |
3f58a829 MK |
1293 | |
1294 | if (mem_cgroup_disabled()) | |
1295 | return; | |
1296 | ||
fa9add64 HD |
1297 | mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec); |
1298 | lru_size = mz->lru_size + lru; | |
1299 | *lru_size += nr_pages; | |
1300 | VM_BUG_ON((long)(*lru_size) < 0); | |
08e552c6 | 1301 | } |
544122e5 | 1302 | |
3e92041d | 1303 | /* |
c0ff4b85 | 1304 | * Checks whether given mem is same or in the root_mem_cgroup's |
3e92041d MH |
1305 | * hierarchy subtree |
1306 | */ | |
c3ac9a8a JW |
1307 | bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg, |
1308 | struct mem_cgroup *memcg) | |
3e92041d | 1309 | { |
91c63734 JW |
1310 | if (root_memcg == memcg) |
1311 | return true; | |
3a981f48 | 1312 | if (!root_memcg->use_hierarchy || !memcg) |
91c63734 | 1313 | return false; |
c3ac9a8a JW |
1314 | return css_is_ancestor(&memcg->css, &root_memcg->css); |
1315 | } | |
1316 | ||
1317 | static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg, | |
1318 | struct mem_cgroup *memcg) | |
1319 | { | |
1320 | bool ret; | |
1321 | ||
91c63734 | 1322 | rcu_read_lock(); |
c3ac9a8a | 1323 | ret = __mem_cgroup_same_or_subtree(root_memcg, memcg); |
91c63734 JW |
1324 | rcu_read_unlock(); |
1325 | return ret; | |
3e92041d MH |
1326 | } |
1327 | ||
c0ff4b85 | 1328 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg) |
4c4a2214 DR |
1329 | { |
1330 | int ret; | |
0b7f569e | 1331 | struct mem_cgroup *curr = NULL; |
158e0a2d | 1332 | struct task_struct *p; |
4c4a2214 | 1333 | |
158e0a2d | 1334 | p = find_lock_task_mm(task); |
de077d22 DR |
1335 | if (p) { |
1336 | curr = try_get_mem_cgroup_from_mm(p->mm); | |
1337 | task_unlock(p); | |
1338 | } else { | |
1339 | /* | |
1340 | * All threads may have already detached their mm's, but the oom | |
1341 | * killer still needs to detect if they have already been oom | |
1342 | * killed to prevent needlessly killing additional tasks. | |
1343 | */ | |
1344 | task_lock(task); | |
1345 | curr = mem_cgroup_from_task(task); | |
1346 | if (curr) | |
1347 | css_get(&curr->css); | |
1348 | task_unlock(task); | |
1349 | } | |
0b7f569e KH |
1350 | if (!curr) |
1351 | return 0; | |
d31f56db | 1352 | /* |
c0ff4b85 | 1353 | * We should check use_hierarchy of "memcg" not "curr". Because checking |
d31f56db | 1354 | * use_hierarchy of "curr" here make this function true if hierarchy is |
c0ff4b85 R |
1355 | * enabled in "curr" and "curr" is a child of "memcg" in *cgroup* |
1356 | * hierarchy(even if use_hierarchy is disabled in "memcg"). | |
d31f56db | 1357 | */ |
c0ff4b85 | 1358 | ret = mem_cgroup_same_or_subtree(memcg, curr); |
0b7f569e | 1359 | css_put(&curr->css); |
4c4a2214 DR |
1360 | return ret; |
1361 | } | |
1362 | ||
c56d5c7d | 1363 | int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec) |
14797e23 | 1364 | { |
9b272977 | 1365 | unsigned long inactive_ratio; |
14797e23 | 1366 | unsigned long inactive; |
9b272977 | 1367 | unsigned long active; |
c772be93 | 1368 | unsigned long gb; |
14797e23 | 1369 | |
4d7dcca2 HD |
1370 | inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON); |
1371 | active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON); | |
14797e23 | 1372 | |
c772be93 KM |
1373 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
1374 | if (gb) | |
1375 | inactive_ratio = int_sqrt(10 * gb); | |
1376 | else | |
1377 | inactive_ratio = 1; | |
1378 | ||
9b272977 | 1379 | return inactive * inactive_ratio < active; |
14797e23 KM |
1380 | } |
1381 | ||
c56d5c7d | 1382 | int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec) |
56e49d21 RR |
1383 | { |
1384 | unsigned long active; | |
1385 | unsigned long inactive; | |
1386 | ||
4d7dcca2 HD |
1387 | inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_FILE); |
1388 | active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_FILE); | |
56e49d21 RR |
1389 | |
1390 | return (active > inactive); | |
1391 | } | |
1392 | ||
6d61ef40 BS |
1393 | #define mem_cgroup_from_res_counter(counter, member) \ |
1394 | container_of(counter, struct mem_cgroup, member) | |
1395 | ||
19942822 | 1396 | /** |
9d11ea9f | 1397 | * mem_cgroup_margin - calculate chargeable space of a memory cgroup |
dad7557e | 1398 | * @memcg: the memory cgroup |
19942822 | 1399 | * |
9d11ea9f | 1400 | * Returns the maximum amount of memory @mem can be charged with, in |
7ec99d62 | 1401 | * pages. |
19942822 | 1402 | */ |
c0ff4b85 | 1403 | static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg) |
19942822 | 1404 | { |
9d11ea9f JW |
1405 | unsigned long long margin; |
1406 | ||
c0ff4b85 | 1407 | margin = res_counter_margin(&memcg->res); |
9d11ea9f | 1408 | if (do_swap_account) |
c0ff4b85 | 1409 | margin = min(margin, res_counter_margin(&memcg->memsw)); |
7ec99d62 | 1410 | return margin >> PAGE_SHIFT; |
19942822 JW |
1411 | } |
1412 | ||
1f4c025b | 1413 | int mem_cgroup_swappiness(struct mem_cgroup *memcg) |
a7885eb8 KM |
1414 | { |
1415 | struct cgroup *cgrp = memcg->css.cgroup; | |
a7885eb8 KM |
1416 | |
1417 | /* root ? */ | |
1418 | if (cgrp->parent == NULL) | |
1419 | return vm_swappiness; | |
1420 | ||
bf1ff263 | 1421 | return memcg->swappiness; |
a7885eb8 KM |
1422 | } |
1423 | ||
619d094b KH |
1424 | /* |
1425 | * memcg->moving_account is used for checking possibility that some thread is | |
1426 | * calling move_account(). When a thread on CPU-A starts moving pages under | |
1427 | * a memcg, other threads should check memcg->moving_account under | |
1428 | * rcu_read_lock(), like this: | |
1429 | * | |
1430 | * CPU-A CPU-B | |
1431 | * rcu_read_lock() | |
1432 | * memcg->moving_account+1 if (memcg->mocing_account) | |
1433 | * take heavy locks. | |
1434 | * synchronize_rcu() update something. | |
1435 | * rcu_read_unlock() | |
1436 | * start move here. | |
1437 | */ | |
4331f7d3 KH |
1438 | |
1439 | /* for quick checking without looking up memcg */ | |
1440 | atomic_t memcg_moving __read_mostly; | |
1441 | ||
c0ff4b85 | 1442 | static void mem_cgroup_start_move(struct mem_cgroup *memcg) |
32047e2a | 1443 | { |
4331f7d3 | 1444 | atomic_inc(&memcg_moving); |
619d094b | 1445 | atomic_inc(&memcg->moving_account); |
32047e2a KH |
1446 | synchronize_rcu(); |
1447 | } | |
1448 | ||
c0ff4b85 | 1449 | static void mem_cgroup_end_move(struct mem_cgroup *memcg) |
32047e2a | 1450 | { |
619d094b KH |
1451 | /* |
1452 | * Now, mem_cgroup_clear_mc() may call this function with NULL. | |
1453 | * We check NULL in callee rather than caller. | |
1454 | */ | |
4331f7d3 KH |
1455 | if (memcg) { |
1456 | atomic_dec(&memcg_moving); | |
619d094b | 1457 | atomic_dec(&memcg->moving_account); |
4331f7d3 | 1458 | } |
32047e2a | 1459 | } |
619d094b | 1460 | |
32047e2a KH |
1461 | /* |
1462 | * 2 routines for checking "mem" is under move_account() or not. | |
1463 | * | |
13fd1dd9 AM |
1464 | * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This |
1465 | * is used for avoiding races in accounting. If true, | |
32047e2a KH |
1466 | * pc->mem_cgroup may be overwritten. |
1467 | * | |
1468 | * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or | |
1469 | * under hierarchy of moving cgroups. This is for | |
1470 | * waiting at hith-memory prressure caused by "move". | |
1471 | */ | |
1472 | ||
13fd1dd9 | 1473 | static bool mem_cgroup_stolen(struct mem_cgroup *memcg) |
32047e2a KH |
1474 | { |
1475 | VM_BUG_ON(!rcu_read_lock_held()); | |
619d094b | 1476 | return atomic_read(&memcg->moving_account) > 0; |
32047e2a | 1477 | } |
4b534334 | 1478 | |
c0ff4b85 | 1479 | static bool mem_cgroup_under_move(struct mem_cgroup *memcg) |
4b534334 | 1480 | { |
2bd9bb20 KH |
1481 | struct mem_cgroup *from; |
1482 | struct mem_cgroup *to; | |
4b534334 | 1483 | bool ret = false; |
2bd9bb20 KH |
1484 | /* |
1485 | * Unlike task_move routines, we access mc.to, mc.from not under | |
1486 | * mutual exclusion by cgroup_mutex. Here, we take spinlock instead. | |
1487 | */ | |
1488 | spin_lock(&mc.lock); | |
1489 | from = mc.from; | |
1490 | to = mc.to; | |
1491 | if (!from) | |
1492 | goto unlock; | |
3e92041d | 1493 | |
c0ff4b85 R |
1494 | ret = mem_cgroup_same_or_subtree(memcg, from) |
1495 | || mem_cgroup_same_or_subtree(memcg, to); | |
2bd9bb20 KH |
1496 | unlock: |
1497 | spin_unlock(&mc.lock); | |
4b534334 KH |
1498 | return ret; |
1499 | } | |
1500 | ||
c0ff4b85 | 1501 | static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg) |
4b534334 KH |
1502 | { |
1503 | if (mc.moving_task && current != mc.moving_task) { | |
c0ff4b85 | 1504 | if (mem_cgroup_under_move(memcg)) { |
4b534334 KH |
1505 | DEFINE_WAIT(wait); |
1506 | prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE); | |
1507 | /* moving charge context might have finished. */ | |
1508 | if (mc.moving_task) | |
1509 | schedule(); | |
1510 | finish_wait(&mc.waitq, &wait); | |
1511 | return true; | |
1512 | } | |
1513 | } | |
1514 | return false; | |
1515 | } | |
1516 | ||
312734c0 KH |
1517 | /* |
1518 | * Take this lock when | |
1519 | * - a code tries to modify page's memcg while it's USED. | |
1520 | * - a code tries to modify page state accounting in a memcg. | |
13fd1dd9 | 1521 | * see mem_cgroup_stolen(), too. |
312734c0 KH |
1522 | */ |
1523 | static void move_lock_mem_cgroup(struct mem_cgroup *memcg, | |
1524 | unsigned long *flags) | |
1525 | { | |
1526 | spin_lock_irqsave(&memcg->move_lock, *flags); | |
1527 | } | |
1528 | ||
1529 | static void move_unlock_mem_cgroup(struct mem_cgroup *memcg, | |
1530 | unsigned long *flags) | |
1531 | { | |
1532 | spin_unlock_irqrestore(&memcg->move_lock, *flags); | |
1533 | } | |
1534 | ||
58cf188e | 1535 | #define K(x) ((x) << (PAGE_SHIFT-10)) |
e222432b | 1536 | /** |
58cf188e | 1537 | * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller. |
e222432b BS |
1538 | * @memcg: The memory cgroup that went over limit |
1539 | * @p: Task that is going to be killed | |
1540 | * | |
1541 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is | |
1542 | * enabled | |
1543 | */ | |
1544 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) | |
1545 | { | |
1546 | struct cgroup *task_cgrp; | |
1547 | struct cgroup *mem_cgrp; | |
1548 | /* | |
1549 | * Need a buffer in BSS, can't rely on allocations. The code relies | |
1550 | * on the assumption that OOM is serialized for memory controller. | |
1551 | * If this assumption is broken, revisit this code. | |
1552 | */ | |
1553 | static char memcg_name[PATH_MAX]; | |
1554 | int ret; | |
58cf188e SZ |
1555 | struct mem_cgroup *iter; |
1556 | unsigned int i; | |
e222432b | 1557 | |
58cf188e | 1558 | if (!p) |
e222432b BS |
1559 | return; |
1560 | ||
e222432b BS |
1561 | rcu_read_lock(); |
1562 | ||
1563 | mem_cgrp = memcg->css.cgroup; | |
1564 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); | |
1565 | ||
1566 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); | |
1567 | if (ret < 0) { | |
1568 | /* | |
1569 | * Unfortunately, we are unable to convert to a useful name | |
1570 | * But we'll still print out the usage information | |
1571 | */ | |
1572 | rcu_read_unlock(); | |
1573 | goto done; | |
1574 | } | |
1575 | rcu_read_unlock(); | |
1576 | ||
1577 | printk(KERN_INFO "Task in %s killed", memcg_name); | |
1578 | ||
1579 | rcu_read_lock(); | |
1580 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); | |
1581 | if (ret < 0) { | |
1582 | rcu_read_unlock(); | |
1583 | goto done; | |
1584 | } | |
1585 | rcu_read_unlock(); | |
1586 | ||
1587 | /* | |
1588 | * Continues from above, so we don't need an KERN_ level | |
1589 | */ | |
1590 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); | |
1591 | done: | |
1592 | ||
1593 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", | |
1594 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, | |
1595 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, | |
1596 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); | |
1597 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " | |
1598 | "failcnt %llu\n", | |
1599 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, | |
1600 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, | |
1601 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); | |
510fc4e1 GC |
1602 | printk(KERN_INFO "kmem: usage %llukB, limit %llukB, failcnt %llu\n", |
1603 | res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10, | |
1604 | res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10, | |
1605 | res_counter_read_u64(&memcg->kmem, RES_FAILCNT)); | |
58cf188e SZ |
1606 | |
1607 | for_each_mem_cgroup_tree(iter, memcg) { | |
1608 | pr_info("Memory cgroup stats"); | |
1609 | ||
1610 | rcu_read_lock(); | |
1611 | ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX); | |
1612 | if (!ret) | |
1613 | pr_cont(" for %s", memcg_name); | |
1614 | rcu_read_unlock(); | |
1615 | pr_cont(":"); | |
1616 | ||
1617 | for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) { | |
1618 | if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account) | |
1619 | continue; | |
1620 | pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i], | |
1621 | K(mem_cgroup_read_stat(iter, i))); | |
1622 | } | |
1623 | ||
1624 | for (i = 0; i < NR_LRU_LISTS; i++) | |
1625 | pr_cont(" %s:%luKB", mem_cgroup_lru_names[i], | |
1626 | K(mem_cgroup_nr_lru_pages(iter, BIT(i)))); | |
1627 | ||
1628 | pr_cont("\n"); | |
1629 | } | |
e222432b BS |
1630 | } |
1631 | ||
81d39c20 KH |
1632 | /* |
1633 | * This function returns the number of memcg under hierarchy tree. Returns | |
1634 | * 1(self count) if no children. | |
1635 | */ | |
c0ff4b85 | 1636 | static int mem_cgroup_count_children(struct mem_cgroup *memcg) |
81d39c20 KH |
1637 | { |
1638 | int num = 0; | |
7d74b06f KH |
1639 | struct mem_cgroup *iter; |
1640 | ||
c0ff4b85 | 1641 | for_each_mem_cgroup_tree(iter, memcg) |
7d74b06f | 1642 | num++; |
81d39c20 KH |
1643 | return num; |
1644 | } | |
1645 | ||
a63d83f4 DR |
1646 | /* |
1647 | * Return the memory (and swap, if configured) limit for a memcg. | |
1648 | */ | |
9cbb78bb | 1649 | static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg) |
a63d83f4 DR |
1650 | { |
1651 | u64 limit; | |
a63d83f4 | 1652 | |
f3e8eb70 | 1653 | limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
f3e8eb70 | 1654 | |
a63d83f4 | 1655 | /* |
9a5a8f19 | 1656 | * Do not consider swap space if we cannot swap due to swappiness |
a63d83f4 | 1657 | */ |
9a5a8f19 MH |
1658 | if (mem_cgroup_swappiness(memcg)) { |
1659 | u64 memsw; | |
1660 | ||
1661 | limit += total_swap_pages << PAGE_SHIFT; | |
1662 | memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
1663 | ||
1664 | /* | |
1665 | * If memsw is finite and limits the amount of swap space | |
1666 | * available to this memcg, return that limit. | |
1667 | */ | |
1668 | limit = min(limit, memsw); | |
1669 | } | |
1670 | ||
1671 | return limit; | |
a63d83f4 DR |
1672 | } |
1673 | ||
19965460 DR |
1674 | static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, |
1675 | int order) | |
9cbb78bb DR |
1676 | { |
1677 | struct mem_cgroup *iter; | |
1678 | unsigned long chosen_points = 0; | |
1679 | unsigned long totalpages; | |
1680 | unsigned int points = 0; | |
1681 | struct task_struct *chosen = NULL; | |
1682 | ||
876aafbf DR |
1683 | /* |
1684 | * If current has a pending SIGKILL, then automatically select it. The | |
1685 | * goal is to allow it to allocate so that it may quickly exit and free | |
1686 | * its memory. | |
1687 | */ | |
1688 | if (fatal_signal_pending(current)) { | |
1689 | set_thread_flag(TIF_MEMDIE); | |
1690 | return; | |
1691 | } | |
1692 | ||
1693 | check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL); | |
9cbb78bb DR |
1694 | totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1; |
1695 | for_each_mem_cgroup_tree(iter, memcg) { | |
1696 | struct cgroup *cgroup = iter->css.cgroup; | |
1697 | struct cgroup_iter it; | |
1698 | struct task_struct *task; | |
1699 | ||
1700 | cgroup_iter_start(cgroup, &it); | |
1701 | while ((task = cgroup_iter_next(cgroup, &it))) { | |
1702 | switch (oom_scan_process_thread(task, totalpages, NULL, | |
1703 | false)) { | |
1704 | case OOM_SCAN_SELECT: | |
1705 | if (chosen) | |
1706 | put_task_struct(chosen); | |
1707 | chosen = task; | |
1708 | chosen_points = ULONG_MAX; | |
1709 | get_task_struct(chosen); | |
1710 | /* fall through */ | |
1711 | case OOM_SCAN_CONTINUE: | |
1712 | continue; | |
1713 | case OOM_SCAN_ABORT: | |
1714 | cgroup_iter_end(cgroup, &it); | |
1715 | mem_cgroup_iter_break(memcg, iter); | |
1716 | if (chosen) | |
1717 | put_task_struct(chosen); | |
1718 | return; | |
1719 | case OOM_SCAN_OK: | |
1720 | break; | |
1721 | }; | |
1722 | points = oom_badness(task, memcg, NULL, totalpages); | |
1723 | if (points > chosen_points) { | |
1724 | if (chosen) | |
1725 | put_task_struct(chosen); | |
1726 | chosen = task; | |
1727 | chosen_points = points; | |
1728 | get_task_struct(chosen); | |
1729 | } | |
1730 | } | |
1731 | cgroup_iter_end(cgroup, &it); | |
1732 | } | |
1733 | ||
1734 | if (!chosen) | |
1735 | return; | |
1736 | points = chosen_points * 1000 / totalpages; | |
9cbb78bb DR |
1737 | oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg, |
1738 | NULL, "Memory cgroup out of memory"); | |
9cbb78bb DR |
1739 | } |
1740 | ||
5660048c JW |
1741 | static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg, |
1742 | gfp_t gfp_mask, | |
1743 | unsigned long flags) | |
1744 | { | |
1745 | unsigned long total = 0; | |
1746 | bool noswap = false; | |
1747 | int loop; | |
1748 | ||
1749 | if (flags & MEM_CGROUP_RECLAIM_NOSWAP) | |
1750 | noswap = true; | |
1751 | if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum) | |
1752 | noswap = true; | |
1753 | ||
1754 | for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) { | |
1755 | if (loop) | |
1756 | drain_all_stock_async(memcg); | |
1757 | total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap); | |
1758 | /* | |
1759 | * Allow limit shrinkers, which are triggered directly | |
1760 | * by userspace, to catch signals and stop reclaim | |
1761 | * after minimal progress, regardless of the margin. | |
1762 | */ | |
1763 | if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK)) | |
1764 | break; | |
1765 | if (mem_cgroup_margin(memcg)) | |
1766 | break; | |
1767 | /* | |
1768 | * If nothing was reclaimed after two attempts, there | |
1769 | * may be no reclaimable pages in this hierarchy. | |
1770 | */ | |
1771 | if (loop && !total) | |
1772 | break; | |
1773 | } | |
1774 | return total; | |
1775 | } | |
1776 | ||
4d0c066d KH |
1777 | /** |
1778 | * test_mem_cgroup_node_reclaimable | |
dad7557e | 1779 | * @memcg: the target memcg |
4d0c066d KH |
1780 | * @nid: the node ID to be checked. |
1781 | * @noswap : specify true here if the user wants flle only information. | |
1782 | * | |
1783 | * This function returns whether the specified memcg contains any | |
1784 | * reclaimable pages on a node. Returns true if there are any reclaimable | |
1785 | * pages in the node. | |
1786 | */ | |
c0ff4b85 | 1787 | static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg, |
4d0c066d KH |
1788 | int nid, bool noswap) |
1789 | { | |
c0ff4b85 | 1790 | if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE)) |
4d0c066d KH |
1791 | return true; |
1792 | if (noswap || !total_swap_pages) | |
1793 | return false; | |
c0ff4b85 | 1794 | if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON)) |
4d0c066d KH |
1795 | return true; |
1796 | return false; | |
1797 | ||
1798 | } | |
889976db YH |
1799 | #if MAX_NUMNODES > 1 |
1800 | ||
1801 | /* | |
1802 | * Always updating the nodemask is not very good - even if we have an empty | |
1803 | * list or the wrong list here, we can start from some node and traverse all | |
1804 | * nodes based on the zonelist. So update the list loosely once per 10 secs. | |
1805 | * | |
1806 | */ | |
c0ff4b85 | 1807 | static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg) |
889976db YH |
1808 | { |
1809 | int nid; | |
453a9bf3 KH |
1810 | /* |
1811 | * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET | |
1812 | * pagein/pageout changes since the last update. | |
1813 | */ | |
c0ff4b85 | 1814 | if (!atomic_read(&memcg->numainfo_events)) |
453a9bf3 | 1815 | return; |
c0ff4b85 | 1816 | if (atomic_inc_return(&memcg->numainfo_updating) > 1) |
889976db YH |
1817 | return; |
1818 | ||
889976db | 1819 | /* make a nodemask where this memcg uses memory from */ |
31aaea4a | 1820 | memcg->scan_nodes = node_states[N_MEMORY]; |
889976db | 1821 | |
31aaea4a | 1822 | for_each_node_mask(nid, node_states[N_MEMORY]) { |
889976db | 1823 | |
c0ff4b85 R |
1824 | if (!test_mem_cgroup_node_reclaimable(memcg, nid, false)) |
1825 | node_clear(nid, memcg->scan_nodes); | |
889976db | 1826 | } |
453a9bf3 | 1827 | |
c0ff4b85 R |
1828 | atomic_set(&memcg->numainfo_events, 0); |
1829 | atomic_set(&memcg->numainfo_updating, 0); | |
889976db YH |
1830 | } |
1831 | ||
1832 | /* | |
1833 | * Selecting a node where we start reclaim from. Because what we need is just | |
1834 | * reducing usage counter, start from anywhere is O,K. Considering | |
1835 | * memory reclaim from current node, there are pros. and cons. | |
1836 | * | |
1837 | * Freeing memory from current node means freeing memory from a node which | |
1838 | * we'll use or we've used. So, it may make LRU bad. And if several threads | |
1839 | * hit limits, it will see a contention on a node. But freeing from remote | |
1840 | * node means more costs for memory reclaim because of memory latency. | |
1841 | * | |
1842 | * Now, we use round-robin. Better algorithm is welcomed. | |
1843 | */ | |
c0ff4b85 | 1844 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg) |
889976db YH |
1845 | { |
1846 | int node; | |
1847 | ||
c0ff4b85 R |
1848 | mem_cgroup_may_update_nodemask(memcg); |
1849 | node = memcg->last_scanned_node; | |
889976db | 1850 | |
c0ff4b85 | 1851 | node = next_node(node, memcg->scan_nodes); |
889976db | 1852 | if (node == MAX_NUMNODES) |
c0ff4b85 | 1853 | node = first_node(memcg->scan_nodes); |
889976db YH |
1854 | /* |
1855 | * We call this when we hit limit, not when pages are added to LRU. | |
1856 | * No LRU may hold pages because all pages are UNEVICTABLE or | |
1857 | * memcg is too small and all pages are not on LRU. In that case, | |
1858 | * we use curret node. | |
1859 | */ | |
1860 | if (unlikely(node == MAX_NUMNODES)) | |
1861 | node = numa_node_id(); | |
1862 | ||
c0ff4b85 | 1863 | memcg->last_scanned_node = node; |
889976db YH |
1864 | return node; |
1865 | } | |
1866 | ||
4d0c066d KH |
1867 | /* |
1868 | * Check all nodes whether it contains reclaimable pages or not. | |
1869 | * For quick scan, we make use of scan_nodes. This will allow us to skip | |
1870 | * unused nodes. But scan_nodes is lazily updated and may not cotain | |
1871 | * enough new information. We need to do double check. | |
1872 | */ | |
6bbda35c | 1873 | static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap) |
4d0c066d KH |
1874 | { |
1875 | int nid; | |
1876 | ||
1877 | /* | |
1878 | * quick check...making use of scan_node. | |
1879 | * We can skip unused nodes. | |
1880 | */ | |
c0ff4b85 R |
1881 | if (!nodes_empty(memcg->scan_nodes)) { |
1882 | for (nid = first_node(memcg->scan_nodes); | |
4d0c066d | 1883 | nid < MAX_NUMNODES; |
c0ff4b85 | 1884 | nid = next_node(nid, memcg->scan_nodes)) { |
4d0c066d | 1885 | |
c0ff4b85 | 1886 | if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap)) |
4d0c066d KH |
1887 | return true; |
1888 | } | |
1889 | } | |
1890 | /* | |
1891 | * Check rest of nodes. | |
1892 | */ | |
31aaea4a | 1893 | for_each_node_state(nid, N_MEMORY) { |
c0ff4b85 | 1894 | if (node_isset(nid, memcg->scan_nodes)) |
4d0c066d | 1895 | continue; |
c0ff4b85 | 1896 | if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap)) |
4d0c066d KH |
1897 | return true; |
1898 | } | |
1899 | return false; | |
1900 | } | |
1901 | ||
889976db | 1902 | #else |
c0ff4b85 | 1903 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg) |
889976db YH |
1904 | { |
1905 | return 0; | |
1906 | } | |
4d0c066d | 1907 | |
6bbda35c | 1908 | static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap) |
4d0c066d | 1909 | { |
c0ff4b85 | 1910 | return test_mem_cgroup_node_reclaimable(memcg, 0, noswap); |
4d0c066d | 1911 | } |
889976db YH |
1912 | #endif |
1913 | ||
5660048c JW |
1914 | static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg, |
1915 | struct zone *zone, | |
1916 | gfp_t gfp_mask, | |
1917 | unsigned long *total_scanned) | |
6d61ef40 | 1918 | { |
9f3a0d09 | 1919 | struct mem_cgroup *victim = NULL; |
5660048c | 1920 | int total = 0; |
04046e1a | 1921 | int loop = 0; |
9d11ea9f | 1922 | unsigned long excess; |
185efc0f | 1923 | unsigned long nr_scanned; |
527a5ec9 JW |
1924 | struct mem_cgroup_reclaim_cookie reclaim = { |
1925 | .zone = zone, | |
1926 | .priority = 0, | |
1927 | }; | |
9d11ea9f | 1928 | |
c0ff4b85 | 1929 | excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT; |
04046e1a | 1930 | |
4e416953 | 1931 | while (1) { |
527a5ec9 | 1932 | victim = mem_cgroup_iter(root_memcg, victim, &reclaim); |
9f3a0d09 | 1933 | if (!victim) { |
04046e1a | 1934 | loop++; |
4e416953 BS |
1935 | if (loop >= 2) { |
1936 | /* | |
1937 | * If we have not been able to reclaim | |
1938 | * anything, it might because there are | |
1939 | * no reclaimable pages under this hierarchy | |
1940 | */ | |
5660048c | 1941 | if (!total) |
4e416953 | 1942 | break; |
4e416953 | 1943 | /* |
25985edc | 1944 | * We want to do more targeted reclaim. |
4e416953 BS |
1945 | * excess >> 2 is not to excessive so as to |
1946 | * reclaim too much, nor too less that we keep | |
1947 | * coming back to reclaim from this cgroup | |
1948 | */ | |
1949 | if (total >= (excess >> 2) || | |
9f3a0d09 | 1950 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) |
4e416953 | 1951 | break; |
4e416953 | 1952 | } |
9f3a0d09 | 1953 | continue; |
4e416953 | 1954 | } |
5660048c | 1955 | if (!mem_cgroup_reclaimable(victim, false)) |
6d61ef40 | 1956 | continue; |
5660048c JW |
1957 | total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false, |
1958 | zone, &nr_scanned); | |
1959 | *total_scanned += nr_scanned; | |
1960 | if (!res_counter_soft_limit_excess(&root_memcg->res)) | |
9f3a0d09 | 1961 | break; |
6d61ef40 | 1962 | } |
9f3a0d09 | 1963 | mem_cgroup_iter_break(root_memcg, victim); |
04046e1a | 1964 | return total; |
6d61ef40 BS |
1965 | } |
1966 | ||
867578cb KH |
1967 | /* |
1968 | * Check OOM-Killer is already running under our hierarchy. | |
1969 | * If someone is running, return false. | |
1af8efe9 | 1970 | * Has to be called with memcg_oom_lock |
867578cb | 1971 | */ |
c0ff4b85 | 1972 | static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg) |
867578cb | 1973 | { |
79dfdacc | 1974 | struct mem_cgroup *iter, *failed = NULL; |
a636b327 | 1975 | |
9f3a0d09 | 1976 | for_each_mem_cgroup_tree(iter, memcg) { |
23751be0 | 1977 | if (iter->oom_lock) { |
79dfdacc MH |
1978 | /* |
1979 | * this subtree of our hierarchy is already locked | |
1980 | * so we cannot give a lock. | |
1981 | */ | |
79dfdacc | 1982 | failed = iter; |
9f3a0d09 JW |
1983 | mem_cgroup_iter_break(memcg, iter); |
1984 | break; | |
23751be0 JW |
1985 | } else |
1986 | iter->oom_lock = true; | |
7d74b06f | 1987 | } |
867578cb | 1988 | |
79dfdacc | 1989 | if (!failed) |
23751be0 | 1990 | return true; |
79dfdacc MH |
1991 | |
1992 | /* | |
1993 | * OK, we failed to lock the whole subtree so we have to clean up | |
1994 | * what we set up to the failing subtree | |
1995 | */ | |
9f3a0d09 | 1996 | for_each_mem_cgroup_tree(iter, memcg) { |
79dfdacc | 1997 | if (iter == failed) { |
9f3a0d09 JW |
1998 | mem_cgroup_iter_break(memcg, iter); |
1999 | break; | |
79dfdacc MH |
2000 | } |
2001 | iter->oom_lock = false; | |
2002 | } | |
23751be0 | 2003 | return false; |
a636b327 | 2004 | } |
0b7f569e | 2005 | |
79dfdacc | 2006 | /* |
1af8efe9 | 2007 | * Has to be called with memcg_oom_lock |
79dfdacc | 2008 | */ |
c0ff4b85 | 2009 | static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg) |
0b7f569e | 2010 | { |
7d74b06f KH |
2011 | struct mem_cgroup *iter; |
2012 | ||
c0ff4b85 | 2013 | for_each_mem_cgroup_tree(iter, memcg) |
79dfdacc MH |
2014 | iter->oom_lock = false; |
2015 | return 0; | |
2016 | } | |
2017 | ||
c0ff4b85 | 2018 | static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg) |
79dfdacc MH |
2019 | { |
2020 | struct mem_cgroup *iter; | |
2021 | ||
c0ff4b85 | 2022 | for_each_mem_cgroup_tree(iter, memcg) |
79dfdacc MH |
2023 | atomic_inc(&iter->under_oom); |
2024 | } | |
2025 | ||
c0ff4b85 | 2026 | static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg) |
79dfdacc MH |
2027 | { |
2028 | struct mem_cgroup *iter; | |
2029 | ||
867578cb KH |
2030 | /* |
2031 | * When a new child is created while the hierarchy is under oom, | |
2032 | * mem_cgroup_oom_lock() may not be called. We have to use | |
2033 | * atomic_add_unless() here. | |
2034 | */ | |
c0ff4b85 | 2035 | for_each_mem_cgroup_tree(iter, memcg) |
79dfdacc | 2036 | atomic_add_unless(&iter->under_oom, -1, 0); |
0b7f569e KH |
2037 | } |
2038 | ||
1af8efe9 | 2039 | static DEFINE_SPINLOCK(memcg_oom_lock); |
867578cb KH |
2040 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
2041 | ||
dc98df5a | 2042 | struct oom_wait_info { |
d79154bb | 2043 | struct mem_cgroup *memcg; |
dc98df5a KH |
2044 | wait_queue_t wait; |
2045 | }; | |
2046 | ||
2047 | static int memcg_oom_wake_function(wait_queue_t *wait, | |
2048 | unsigned mode, int sync, void *arg) | |
2049 | { | |
d79154bb HD |
2050 | struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg; |
2051 | struct mem_cgroup *oom_wait_memcg; | |
dc98df5a KH |
2052 | struct oom_wait_info *oom_wait_info; |
2053 | ||
2054 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); | |
d79154bb | 2055 | oom_wait_memcg = oom_wait_info->memcg; |
dc98df5a | 2056 | |
dc98df5a | 2057 | /* |
d79154bb | 2058 | * Both of oom_wait_info->memcg and wake_memcg are stable under us. |
dc98df5a KH |
2059 | * Then we can use css_is_ancestor without taking care of RCU. |
2060 | */ | |
c0ff4b85 R |
2061 | if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg) |
2062 | && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg)) | |
dc98df5a | 2063 | return 0; |
dc98df5a KH |
2064 | return autoremove_wake_function(wait, mode, sync, arg); |
2065 | } | |
2066 | ||
c0ff4b85 | 2067 | static void memcg_wakeup_oom(struct mem_cgroup *memcg) |
dc98df5a | 2068 | { |
c0ff4b85 R |
2069 | /* for filtering, pass "memcg" as argument. */ |
2070 | __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg); | |
dc98df5a KH |
2071 | } |
2072 | ||
c0ff4b85 | 2073 | static void memcg_oom_recover(struct mem_cgroup *memcg) |
3c11ecf4 | 2074 | { |
c0ff4b85 R |
2075 | if (memcg && atomic_read(&memcg->under_oom)) |
2076 | memcg_wakeup_oom(memcg); | |
3c11ecf4 KH |
2077 | } |
2078 | ||
867578cb KH |
2079 | /* |
2080 | * try to call OOM killer. returns false if we should exit memory-reclaim loop. | |
2081 | */ | |
6bbda35c KS |
2082 | static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask, |
2083 | int order) | |
0b7f569e | 2084 | { |
dc98df5a | 2085 | struct oom_wait_info owait; |
3c11ecf4 | 2086 | bool locked, need_to_kill; |
867578cb | 2087 | |
d79154bb | 2088 | owait.memcg = memcg; |
dc98df5a KH |
2089 | owait.wait.flags = 0; |
2090 | owait.wait.func = memcg_oom_wake_function; | |
2091 | owait.wait.private = current; | |
2092 | INIT_LIST_HEAD(&owait.wait.task_list); | |
3c11ecf4 | 2093 | need_to_kill = true; |
c0ff4b85 | 2094 | mem_cgroup_mark_under_oom(memcg); |
79dfdacc | 2095 | |
c0ff4b85 | 2096 | /* At first, try to OOM lock hierarchy under memcg.*/ |
1af8efe9 | 2097 | spin_lock(&memcg_oom_lock); |
c0ff4b85 | 2098 | locked = mem_cgroup_oom_lock(memcg); |
867578cb KH |
2099 | /* |
2100 | * Even if signal_pending(), we can't quit charge() loop without | |
2101 | * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL | |
2102 | * under OOM is always welcomed, use TASK_KILLABLE here. | |
2103 | */ | |
3c11ecf4 | 2104 | prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE); |
c0ff4b85 | 2105 | if (!locked || memcg->oom_kill_disable) |
3c11ecf4 KH |
2106 | need_to_kill = false; |
2107 | if (locked) | |
c0ff4b85 | 2108 | mem_cgroup_oom_notify(memcg); |
1af8efe9 | 2109 | spin_unlock(&memcg_oom_lock); |
867578cb | 2110 | |
3c11ecf4 KH |
2111 | if (need_to_kill) { |
2112 | finish_wait(&memcg_oom_waitq, &owait.wait); | |
e845e199 | 2113 | mem_cgroup_out_of_memory(memcg, mask, order); |
3c11ecf4 | 2114 | } else { |
867578cb | 2115 | schedule(); |
dc98df5a | 2116 | finish_wait(&memcg_oom_waitq, &owait.wait); |
867578cb | 2117 | } |
1af8efe9 | 2118 | spin_lock(&memcg_oom_lock); |
79dfdacc | 2119 | if (locked) |
c0ff4b85 R |
2120 | mem_cgroup_oom_unlock(memcg); |
2121 | memcg_wakeup_oom(memcg); | |
1af8efe9 | 2122 | spin_unlock(&memcg_oom_lock); |
867578cb | 2123 | |
c0ff4b85 | 2124 | mem_cgroup_unmark_under_oom(memcg); |
79dfdacc | 2125 | |
867578cb KH |
2126 | if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current)) |
2127 | return false; | |
2128 | /* Give chance to dying process */ | |
715a5ee8 | 2129 | schedule_timeout_uninterruptible(1); |
867578cb | 2130 | return true; |
0b7f569e KH |
2131 | } |
2132 | ||
d69b042f BS |
2133 | /* |
2134 | * Currently used to update mapped file statistics, but the routine can be | |
2135 | * generalized to update other statistics as well. | |
32047e2a KH |
2136 | * |
2137 | * Notes: Race condition | |
2138 | * | |
2139 | * We usually use page_cgroup_lock() for accessing page_cgroup member but | |
2140 | * it tends to be costly. But considering some conditions, we doesn't need | |
2141 | * to do so _always_. | |
2142 | * | |
2143 | * Considering "charge", lock_page_cgroup() is not required because all | |
2144 | * file-stat operations happen after a page is attached to radix-tree. There | |
2145 | * are no race with "charge". | |
2146 | * | |
2147 | * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup | |
2148 | * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even | |
2149 | * if there are race with "uncharge". Statistics itself is properly handled | |
2150 | * by flags. | |
2151 | * | |
2152 | * Considering "move", this is an only case we see a race. To make the race | |
619d094b KH |
2153 | * small, we check mm->moving_account and detect there are possibility of race |
2154 | * If there is, we take a lock. | |
d69b042f | 2155 | */ |
26174efd | 2156 | |
89c06bd5 KH |
2157 | void __mem_cgroup_begin_update_page_stat(struct page *page, |
2158 | bool *locked, unsigned long *flags) | |
2159 | { | |
2160 | struct mem_cgroup *memcg; | |
2161 | struct page_cgroup *pc; | |
2162 | ||
2163 | pc = lookup_page_cgroup(page); | |
2164 | again: | |
2165 | memcg = pc->mem_cgroup; | |
2166 | if (unlikely(!memcg || !PageCgroupUsed(pc))) | |
2167 | return; | |
2168 | /* | |
2169 | * If this memory cgroup is not under account moving, we don't | |
da92c47d | 2170 | * need to take move_lock_mem_cgroup(). Because we already hold |
89c06bd5 | 2171 | * rcu_read_lock(), any calls to move_account will be delayed until |
13fd1dd9 | 2172 | * rcu_read_unlock() if mem_cgroup_stolen() == true. |
89c06bd5 | 2173 | */ |
13fd1dd9 | 2174 | if (!mem_cgroup_stolen(memcg)) |
89c06bd5 KH |
2175 | return; |
2176 | ||
2177 | move_lock_mem_cgroup(memcg, flags); | |
2178 | if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) { | |
2179 | move_unlock_mem_cgroup(memcg, flags); | |
2180 | goto again; | |
2181 | } | |
2182 | *locked = true; | |
2183 | } | |
2184 | ||
2185 | void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags) | |
2186 | { | |
2187 | struct page_cgroup *pc = lookup_page_cgroup(page); | |
2188 | ||
2189 | /* | |
2190 | * It's guaranteed that pc->mem_cgroup never changes while | |
2191 | * lock is held because a routine modifies pc->mem_cgroup | |
da92c47d | 2192 | * should take move_lock_mem_cgroup(). |
89c06bd5 KH |
2193 | */ |
2194 | move_unlock_mem_cgroup(pc->mem_cgroup, flags); | |
2195 | } | |
2196 | ||
2a7106f2 GT |
2197 | void mem_cgroup_update_page_stat(struct page *page, |
2198 | enum mem_cgroup_page_stat_item idx, int val) | |
d69b042f | 2199 | { |
c0ff4b85 | 2200 | struct mem_cgroup *memcg; |
32047e2a | 2201 | struct page_cgroup *pc = lookup_page_cgroup(page); |
dbd4ea78 | 2202 | unsigned long uninitialized_var(flags); |
d69b042f | 2203 | |
cfa44946 | 2204 | if (mem_cgroup_disabled()) |
d69b042f | 2205 | return; |
89c06bd5 | 2206 | |
c0ff4b85 R |
2207 | memcg = pc->mem_cgroup; |
2208 | if (unlikely(!memcg || !PageCgroupUsed(pc))) | |
89c06bd5 | 2209 | return; |
26174efd | 2210 | |
26174efd | 2211 | switch (idx) { |
2a7106f2 | 2212 | case MEMCG_NR_FILE_MAPPED: |
2a7106f2 | 2213 | idx = MEM_CGROUP_STAT_FILE_MAPPED; |
26174efd KH |
2214 | break; |
2215 | default: | |
2216 | BUG(); | |
8725d541 | 2217 | } |
d69b042f | 2218 | |
c0ff4b85 | 2219 | this_cpu_add(memcg->stat->count[idx], val); |
d69b042f | 2220 | } |
26174efd | 2221 | |
cdec2e42 KH |
2222 | /* |
2223 | * size of first charge trial. "32" comes from vmscan.c's magic value. | |
2224 | * TODO: maybe necessary to use big numbers in big irons. | |
2225 | */ | |
7ec99d62 | 2226 | #define CHARGE_BATCH 32U |
cdec2e42 KH |
2227 | struct memcg_stock_pcp { |
2228 | struct mem_cgroup *cached; /* this never be root cgroup */ | |
11c9ea4e | 2229 | unsigned int nr_pages; |
cdec2e42 | 2230 | struct work_struct work; |
26fe6168 | 2231 | unsigned long flags; |
a0db00fc | 2232 | #define FLUSHING_CACHED_CHARGE 0 |
cdec2e42 KH |
2233 | }; |
2234 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); | |
9f50fad6 | 2235 | static DEFINE_MUTEX(percpu_charge_mutex); |
cdec2e42 | 2236 | |
a0956d54 SS |
2237 | /** |
2238 | * consume_stock: Try to consume stocked charge on this cpu. | |
2239 | * @memcg: memcg to consume from. | |
2240 | * @nr_pages: how many pages to charge. | |
2241 | * | |
2242 | * The charges will only happen if @memcg matches the current cpu's memcg | |
2243 | * stock, and at least @nr_pages are available in that stock. Failure to | |
2244 | * service an allocation will refill the stock. | |
2245 | * | |
2246 | * returns true if successful, false otherwise. | |
cdec2e42 | 2247 | */ |
a0956d54 | 2248 | static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
cdec2e42 KH |
2249 | { |
2250 | struct memcg_stock_pcp *stock; | |
2251 | bool ret = true; | |
2252 | ||
a0956d54 SS |
2253 | if (nr_pages > CHARGE_BATCH) |
2254 | return false; | |
2255 | ||
cdec2e42 | 2256 | stock = &get_cpu_var(memcg_stock); |
a0956d54 SS |
2257 | if (memcg == stock->cached && stock->nr_pages >= nr_pages) |
2258 | stock->nr_pages -= nr_pages; | |
cdec2e42 KH |
2259 | else /* need to call res_counter_charge */ |
2260 | ret = false; | |
2261 | put_cpu_var(memcg_stock); | |
2262 | return ret; | |
2263 | } | |
2264 | ||
2265 | /* | |
2266 | * Returns stocks cached in percpu to res_counter and reset cached information. | |
2267 | */ | |
2268 | static void drain_stock(struct memcg_stock_pcp *stock) | |
2269 | { | |
2270 | struct mem_cgroup *old = stock->cached; | |
2271 | ||
11c9ea4e JW |
2272 | if (stock->nr_pages) { |
2273 | unsigned long bytes = stock->nr_pages * PAGE_SIZE; | |
2274 | ||
2275 | res_counter_uncharge(&old->res, bytes); | |
cdec2e42 | 2276 | if (do_swap_account) |
11c9ea4e JW |
2277 | res_counter_uncharge(&old->memsw, bytes); |
2278 | stock->nr_pages = 0; | |
cdec2e42 KH |
2279 | } |
2280 | stock->cached = NULL; | |
cdec2e42 KH |
2281 | } |
2282 | ||
2283 | /* | |
2284 | * This must be called under preempt disabled or must be called by | |
2285 | * a thread which is pinned to local cpu. | |
2286 | */ | |
2287 | static void drain_local_stock(struct work_struct *dummy) | |
2288 | { | |
2289 | struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock); | |
2290 | drain_stock(stock); | |
26fe6168 | 2291 | clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); |
cdec2e42 KH |
2292 | } |
2293 | ||
2294 | /* | |
2295 | * Cache charges(val) which is from res_counter, to local per_cpu area. | |
320cc51d | 2296 | * This will be consumed by consume_stock() function, later. |
cdec2e42 | 2297 | */ |
c0ff4b85 | 2298 | static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
cdec2e42 KH |
2299 | { |
2300 | struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock); | |
2301 | ||
c0ff4b85 | 2302 | if (stock->cached != memcg) { /* reset if necessary */ |
cdec2e42 | 2303 | drain_stock(stock); |
c0ff4b85 | 2304 | stock->cached = memcg; |
cdec2e42 | 2305 | } |
11c9ea4e | 2306 | stock->nr_pages += nr_pages; |
cdec2e42 KH |
2307 | put_cpu_var(memcg_stock); |
2308 | } | |
2309 | ||
2310 | /* | |
c0ff4b85 | 2311 | * Drains all per-CPU charge caches for given root_memcg resp. subtree |
d38144b7 MH |
2312 | * of the hierarchy under it. sync flag says whether we should block |
2313 | * until the work is done. | |
cdec2e42 | 2314 | */ |
c0ff4b85 | 2315 | static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync) |
cdec2e42 | 2316 | { |
26fe6168 | 2317 | int cpu, curcpu; |
d38144b7 | 2318 | |
cdec2e42 | 2319 | /* Notify other cpus that system-wide "drain" is running */ |
cdec2e42 | 2320 | get_online_cpus(); |
5af12d0e | 2321 | curcpu = get_cpu(); |
cdec2e42 KH |
2322 | for_each_online_cpu(cpu) { |
2323 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); | |
c0ff4b85 | 2324 | struct mem_cgroup *memcg; |
26fe6168 | 2325 | |
c0ff4b85 R |
2326 | memcg = stock->cached; |
2327 | if (!memcg || !stock->nr_pages) | |
26fe6168 | 2328 | continue; |
c0ff4b85 | 2329 | if (!mem_cgroup_same_or_subtree(root_memcg, memcg)) |
3e92041d | 2330 | continue; |
d1a05b69 MH |
2331 | if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) { |
2332 | if (cpu == curcpu) | |
2333 | drain_local_stock(&stock->work); | |
2334 | else | |
2335 | schedule_work_on(cpu, &stock->work); | |
2336 | } | |
cdec2e42 | 2337 | } |
5af12d0e | 2338 | put_cpu(); |
d38144b7 MH |
2339 | |
2340 | if (!sync) | |
2341 | goto out; | |
2342 | ||
2343 | for_each_online_cpu(cpu) { | |
2344 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); | |
9f50fad6 | 2345 | if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) |
d38144b7 MH |
2346 | flush_work(&stock->work); |
2347 | } | |
2348 | out: | |
cdec2e42 | 2349 | put_online_cpus(); |
d38144b7 MH |
2350 | } |
2351 | ||
2352 | /* | |
2353 | * Tries to drain stocked charges in other cpus. This function is asynchronous | |
2354 | * and just put a work per cpu for draining localy on each cpu. Caller can | |
2355 | * expects some charges will be back to res_counter later but cannot wait for | |
2356 | * it. | |
2357 | */ | |
c0ff4b85 | 2358 | static void drain_all_stock_async(struct mem_cgroup *root_memcg) |
d38144b7 | 2359 | { |
9f50fad6 MH |
2360 | /* |
2361 | * If someone calls draining, avoid adding more kworker runs. | |
2362 | */ | |
2363 | if (!mutex_trylock(&percpu_charge_mutex)) | |
2364 | return; | |
c0ff4b85 | 2365 | drain_all_stock(root_memcg, false); |
9f50fad6 | 2366 | mutex_unlock(&percpu_charge_mutex); |
cdec2e42 KH |
2367 | } |
2368 | ||
2369 | /* This is a synchronous drain interface. */ | |
c0ff4b85 | 2370 | static void drain_all_stock_sync(struct mem_cgroup *root_memcg) |
cdec2e42 KH |
2371 | { |
2372 | /* called when force_empty is called */ | |
9f50fad6 | 2373 | mutex_lock(&percpu_charge_mutex); |
c0ff4b85 | 2374 | drain_all_stock(root_memcg, true); |
9f50fad6 | 2375 | mutex_unlock(&percpu_charge_mutex); |
cdec2e42 KH |
2376 | } |
2377 | ||
711d3d2c KH |
2378 | /* |
2379 | * This function drains percpu counter value from DEAD cpu and | |
2380 | * move it to local cpu. Note that this function can be preempted. | |
2381 | */ | |
c0ff4b85 | 2382 | static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu) |
711d3d2c KH |
2383 | { |
2384 | int i; | |
2385 | ||
c0ff4b85 | 2386 | spin_lock(&memcg->pcp_counter_lock); |
6104621d | 2387 | for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) { |
c0ff4b85 | 2388 | long x = per_cpu(memcg->stat->count[i], cpu); |
711d3d2c | 2389 | |
c0ff4b85 R |
2390 | per_cpu(memcg->stat->count[i], cpu) = 0; |
2391 | memcg->nocpu_base.count[i] += x; | |
711d3d2c | 2392 | } |
e9f8974f | 2393 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) { |
c0ff4b85 | 2394 | unsigned long x = per_cpu(memcg->stat->events[i], cpu); |
e9f8974f | 2395 | |
c0ff4b85 R |
2396 | per_cpu(memcg->stat->events[i], cpu) = 0; |
2397 | memcg->nocpu_base.events[i] += x; | |
e9f8974f | 2398 | } |
c0ff4b85 | 2399 | spin_unlock(&memcg->pcp_counter_lock); |
711d3d2c KH |
2400 | } |
2401 | ||
2402 | static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb, | |
cdec2e42 KH |
2403 | unsigned long action, |
2404 | void *hcpu) | |
2405 | { | |
2406 | int cpu = (unsigned long)hcpu; | |
2407 | struct memcg_stock_pcp *stock; | |
711d3d2c | 2408 | struct mem_cgroup *iter; |
cdec2e42 | 2409 | |
619d094b | 2410 | if (action == CPU_ONLINE) |
1489ebad | 2411 | return NOTIFY_OK; |
1489ebad | 2412 | |
d833049b | 2413 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) |
cdec2e42 | 2414 | return NOTIFY_OK; |
711d3d2c | 2415 | |
9f3a0d09 | 2416 | for_each_mem_cgroup(iter) |
711d3d2c KH |
2417 | mem_cgroup_drain_pcp_counter(iter, cpu); |
2418 | ||
cdec2e42 KH |
2419 | stock = &per_cpu(memcg_stock, cpu); |
2420 | drain_stock(stock); | |
2421 | return NOTIFY_OK; | |
2422 | } | |
2423 | ||
4b534334 KH |
2424 | |
2425 | /* See __mem_cgroup_try_charge() for details */ | |
2426 | enum { | |
2427 | CHARGE_OK, /* success */ | |
2428 | CHARGE_RETRY, /* need to retry but retry is not bad */ | |
2429 | CHARGE_NOMEM, /* we can't do more. return -ENOMEM */ | |
2430 | CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */ | |
2431 | CHARGE_OOM_DIE, /* the current is killed because of OOM */ | |
2432 | }; | |
2433 | ||
c0ff4b85 | 2434 | static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, |
4c9c5359 SS |
2435 | unsigned int nr_pages, unsigned int min_pages, |
2436 | bool oom_check) | |
4b534334 | 2437 | { |
7ec99d62 | 2438 | unsigned long csize = nr_pages * PAGE_SIZE; |
4b534334 KH |
2439 | struct mem_cgroup *mem_over_limit; |
2440 | struct res_counter *fail_res; | |
2441 | unsigned long flags = 0; | |
2442 | int ret; | |
2443 | ||
c0ff4b85 | 2444 | ret = res_counter_charge(&memcg->res, csize, &fail_res); |
4b534334 KH |
2445 | |
2446 | if (likely(!ret)) { | |
2447 | if (!do_swap_account) | |
2448 | return CHARGE_OK; | |
c0ff4b85 | 2449 | ret = res_counter_charge(&memcg->memsw, csize, &fail_res); |
4b534334 KH |
2450 | if (likely(!ret)) |
2451 | return CHARGE_OK; | |
2452 | ||
c0ff4b85 | 2453 | res_counter_uncharge(&memcg->res, csize); |
4b534334 KH |
2454 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw); |
2455 | flags |= MEM_CGROUP_RECLAIM_NOSWAP; | |
2456 | } else | |
2457 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, res); | |
9221edb7 | 2458 | /* |
9221edb7 JW |
2459 | * Never reclaim on behalf of optional batching, retry with a |
2460 | * single page instead. | |
2461 | */ | |
4c9c5359 | 2462 | if (nr_pages > min_pages) |
4b534334 KH |
2463 | return CHARGE_RETRY; |
2464 | ||
2465 | if (!(gfp_mask & __GFP_WAIT)) | |
2466 | return CHARGE_WOULDBLOCK; | |
2467 | ||
4c9c5359 SS |
2468 | if (gfp_mask & __GFP_NORETRY) |
2469 | return CHARGE_NOMEM; | |
2470 | ||
5660048c | 2471 | ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags); |
7ec99d62 | 2472 | if (mem_cgroup_margin(mem_over_limit) >= nr_pages) |
19942822 | 2473 | return CHARGE_RETRY; |
4b534334 | 2474 | /* |
19942822 JW |
2475 | * Even though the limit is exceeded at this point, reclaim |
2476 | * may have been able to free some pages. Retry the charge | |
2477 | * before killing the task. | |
2478 | * | |
2479 | * Only for regular pages, though: huge pages are rather | |
2480 | * unlikely to succeed so close to the limit, and we fall back | |
2481 | * to regular pages anyway in case of failure. | |
4b534334 | 2482 | */ |
4c9c5359 | 2483 | if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret) |
4b534334 KH |
2484 | return CHARGE_RETRY; |
2485 | ||
2486 | /* | |
2487 | * At task move, charge accounts can be doubly counted. So, it's | |
2488 | * better to wait until the end of task_move if something is going on. | |
2489 | */ | |
2490 | if (mem_cgroup_wait_acct_move(mem_over_limit)) | |
2491 | return CHARGE_RETRY; | |
2492 | ||
2493 | /* If we don't need to call oom-killer at el, return immediately */ | |
2494 | if (!oom_check) | |
2495 | return CHARGE_NOMEM; | |
2496 | /* check OOM */ | |
e845e199 | 2497 | if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize))) |
4b534334 KH |
2498 | return CHARGE_OOM_DIE; |
2499 | ||
2500 | return CHARGE_RETRY; | |
2501 | } | |
2502 | ||
f817ed48 | 2503 | /* |
38c5d72f KH |
2504 | * __mem_cgroup_try_charge() does |
2505 | * 1. detect memcg to be charged against from passed *mm and *ptr, | |
2506 | * 2. update res_counter | |
2507 | * 3. call memory reclaim if necessary. | |
2508 | * | |
2509 | * In some special case, if the task is fatal, fatal_signal_pending() or | |
2510 | * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup | |
2511 | * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon | |
2512 | * as possible without any hazards. 2: all pages should have a valid | |
2513 | * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg | |
2514 | * pointer, that is treated as a charge to root_mem_cgroup. | |
2515 | * | |
2516 | * So __mem_cgroup_try_charge() will return | |
2517 | * 0 ... on success, filling *ptr with a valid memcg pointer. | |
2518 | * -ENOMEM ... charge failure because of resource limits. | |
2519 | * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup. | |
2520 | * | |
2521 | * Unlike the exported interface, an "oom" parameter is added. if oom==true, | |
2522 | * the oom-killer can be invoked. | |
8a9f3ccd | 2523 | */ |
f817ed48 | 2524 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
ec168510 | 2525 | gfp_t gfp_mask, |
7ec99d62 | 2526 | unsigned int nr_pages, |
c0ff4b85 | 2527 | struct mem_cgroup **ptr, |
7ec99d62 | 2528 | bool oom) |
8a9f3ccd | 2529 | { |
7ec99d62 | 2530 | unsigned int batch = max(CHARGE_BATCH, nr_pages); |
4b534334 | 2531 | int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
c0ff4b85 | 2532 | struct mem_cgroup *memcg = NULL; |
4b534334 | 2533 | int ret; |
a636b327 | 2534 | |
867578cb KH |
2535 | /* |
2536 | * Unlike gloval-vm's OOM-kill, we're not in memory shortage | |
2537 | * in system level. So, allow to go ahead dying process in addition to | |
2538 | * MEMDIE process. | |
2539 | */ | |
2540 | if (unlikely(test_thread_flag(TIF_MEMDIE) | |
2541 | || fatal_signal_pending(current))) | |
2542 | goto bypass; | |
a636b327 | 2543 | |
8a9f3ccd | 2544 | /* |
3be91277 HD |
2545 | * We always charge the cgroup the mm_struct belongs to. |
2546 | * The mm_struct's mem_cgroup changes on task migration if the | |
8a9f3ccd | 2547 | * thread group leader migrates. It's possible that mm is not |
24467cac | 2548 | * set, if so charge the root memcg (happens for pagecache usage). |
8a9f3ccd | 2549 | */ |
c0ff4b85 | 2550 | if (!*ptr && !mm) |
38c5d72f | 2551 | *ptr = root_mem_cgroup; |
f75ca962 | 2552 | again: |
c0ff4b85 R |
2553 | if (*ptr) { /* css should be a valid one */ |
2554 | memcg = *ptr; | |
c0ff4b85 | 2555 | if (mem_cgroup_is_root(memcg)) |
f75ca962 | 2556 | goto done; |
a0956d54 | 2557 | if (consume_stock(memcg, nr_pages)) |
f75ca962 | 2558 | goto done; |
c0ff4b85 | 2559 | css_get(&memcg->css); |
4b534334 | 2560 | } else { |
f75ca962 | 2561 | struct task_struct *p; |
54595fe2 | 2562 | |
f75ca962 KH |
2563 | rcu_read_lock(); |
2564 | p = rcu_dereference(mm->owner); | |
f75ca962 | 2565 | /* |
ebb76ce1 | 2566 | * Because we don't have task_lock(), "p" can exit. |
c0ff4b85 | 2567 | * In that case, "memcg" can point to root or p can be NULL with |
ebb76ce1 KH |
2568 | * race with swapoff. Then, we have small risk of mis-accouning. |
2569 | * But such kind of mis-account by race always happens because | |
2570 | * we don't have cgroup_mutex(). It's overkill and we allo that | |
2571 | * small race, here. | |
2572 | * (*) swapoff at el will charge against mm-struct not against | |
2573 | * task-struct. So, mm->owner can be NULL. | |
f75ca962 | 2574 | */ |
c0ff4b85 | 2575 | memcg = mem_cgroup_from_task(p); |
38c5d72f KH |
2576 | if (!memcg) |
2577 | memcg = root_mem_cgroup; | |
2578 | if (mem_cgroup_is_root(memcg)) { | |
f75ca962 KH |
2579 | rcu_read_unlock(); |
2580 | goto done; | |
2581 | } | |
a0956d54 | 2582 | if (consume_stock(memcg, nr_pages)) { |
f75ca962 KH |
2583 | /* |
2584 | * It seems dagerous to access memcg without css_get(). | |
2585 | * But considering how consume_stok works, it's not | |
2586 | * necessary. If consume_stock success, some charges | |
2587 | * from this memcg are cached on this cpu. So, we | |
2588 | * don't need to call css_get()/css_tryget() before | |
2589 | * calling consume_stock(). | |
2590 | */ | |
2591 | rcu_read_unlock(); | |
2592 | goto done; | |
2593 | } | |
2594 | /* after here, we may be blocked. we need to get refcnt */ | |
c0ff4b85 | 2595 | if (!css_tryget(&memcg->css)) { |
f75ca962 KH |
2596 | rcu_read_unlock(); |
2597 | goto again; | |
2598 | } | |
2599 | rcu_read_unlock(); | |
2600 | } | |
8a9f3ccd | 2601 | |
4b534334 KH |
2602 | do { |
2603 | bool oom_check; | |
7a81b88c | 2604 | |
4b534334 | 2605 | /* If killed, bypass charge */ |
f75ca962 | 2606 | if (fatal_signal_pending(current)) { |
c0ff4b85 | 2607 | css_put(&memcg->css); |
4b534334 | 2608 | goto bypass; |
f75ca962 | 2609 | } |
6d61ef40 | 2610 | |
4b534334 KH |
2611 | oom_check = false; |
2612 | if (oom && !nr_oom_retries) { | |
2613 | oom_check = true; | |
2614 | nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; | |
cdec2e42 | 2615 | } |
66e1707b | 2616 | |
4c9c5359 SS |
2617 | ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages, |
2618 | oom_check); | |
4b534334 KH |
2619 | switch (ret) { |
2620 | case CHARGE_OK: | |
2621 | break; | |
2622 | case CHARGE_RETRY: /* not in OOM situation but retry */ | |
7ec99d62 | 2623 | batch = nr_pages; |
c0ff4b85 R |
2624 | css_put(&memcg->css); |
2625 | memcg = NULL; | |
f75ca962 | 2626 | goto again; |
4b534334 | 2627 | case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */ |
c0ff4b85 | 2628 | css_put(&memcg->css); |
4b534334 KH |
2629 | goto nomem; |
2630 | case CHARGE_NOMEM: /* OOM routine works */ | |
f75ca962 | 2631 | if (!oom) { |
c0ff4b85 | 2632 | css_put(&memcg->css); |
867578cb | 2633 | goto nomem; |
f75ca962 | 2634 | } |
4b534334 KH |
2635 | /* If oom, we never return -ENOMEM */ |
2636 | nr_oom_retries--; | |
2637 | break; | |
2638 | case CHARGE_OOM_DIE: /* Killed by OOM Killer */ | |
c0ff4b85 | 2639 | css_put(&memcg->css); |
867578cb | 2640 | goto bypass; |
66e1707b | 2641 | } |
4b534334 KH |
2642 | } while (ret != CHARGE_OK); |
2643 | ||
7ec99d62 | 2644 | if (batch > nr_pages) |
c0ff4b85 R |
2645 | refill_stock(memcg, batch - nr_pages); |
2646 | css_put(&memcg->css); | |
0c3e73e8 | 2647 | done: |
c0ff4b85 | 2648 | *ptr = memcg; |
7a81b88c KH |
2649 | return 0; |
2650 | nomem: | |
c0ff4b85 | 2651 | *ptr = NULL; |
7a81b88c | 2652 | return -ENOMEM; |
867578cb | 2653 | bypass: |
38c5d72f KH |
2654 | *ptr = root_mem_cgroup; |
2655 | return -EINTR; | |
7a81b88c | 2656 | } |
8a9f3ccd | 2657 | |
a3032a2c DN |
2658 | /* |
2659 | * Somemtimes we have to undo a charge we got by try_charge(). | |
2660 | * This function is for that and do uncharge, put css's refcnt. | |
2661 | * gotten by try_charge(). | |
2662 | */ | |
c0ff4b85 | 2663 | static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg, |
e7018b8d | 2664 | unsigned int nr_pages) |
a3032a2c | 2665 | { |
c0ff4b85 | 2666 | if (!mem_cgroup_is_root(memcg)) { |
e7018b8d JW |
2667 | unsigned long bytes = nr_pages * PAGE_SIZE; |
2668 | ||
c0ff4b85 | 2669 | res_counter_uncharge(&memcg->res, bytes); |
a3032a2c | 2670 | if (do_swap_account) |
c0ff4b85 | 2671 | res_counter_uncharge(&memcg->memsw, bytes); |
a3032a2c | 2672 | } |
854ffa8d DN |
2673 | } |
2674 | ||
d01dd17f KH |
2675 | /* |
2676 | * Cancel chrages in this cgroup....doesn't propagate to parent cgroup. | |
2677 | * This is useful when moving usage to parent cgroup. | |
2678 | */ | |
2679 | static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg, | |
2680 | unsigned int nr_pages) | |
2681 | { | |
2682 | unsigned long bytes = nr_pages * PAGE_SIZE; | |
2683 | ||
2684 | if (mem_cgroup_is_root(memcg)) | |
2685 | return; | |
2686 | ||
2687 | res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes); | |
2688 | if (do_swap_account) | |
2689 | res_counter_uncharge_until(&memcg->memsw, | |
2690 | memcg->memsw.parent, bytes); | |
2691 | } | |
2692 | ||
a3b2d692 KH |
2693 | /* |
2694 | * A helper function to get mem_cgroup from ID. must be called under | |
e9316080 TH |
2695 | * rcu_read_lock(). The caller is responsible for calling css_tryget if |
2696 | * the mem_cgroup is used for charging. (dropping refcnt from swap can be | |
2697 | * called against removed memcg.) | |
a3b2d692 KH |
2698 | */ |
2699 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) | |
2700 | { | |
2701 | struct cgroup_subsys_state *css; | |
2702 | ||
2703 | /* ID 0 is unused ID */ | |
2704 | if (!id) | |
2705 | return NULL; | |
2706 | css = css_lookup(&mem_cgroup_subsys, id); | |
2707 | if (!css) | |
2708 | return NULL; | |
b2145145 | 2709 | return mem_cgroup_from_css(css); |
a3b2d692 KH |
2710 | } |
2711 | ||
e42d9d5d | 2712 | struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
b5a84319 | 2713 | { |
c0ff4b85 | 2714 | struct mem_cgroup *memcg = NULL; |
3c776e64 | 2715 | struct page_cgroup *pc; |
a3b2d692 | 2716 | unsigned short id; |
b5a84319 KH |
2717 | swp_entry_t ent; |
2718 | ||
3c776e64 DN |
2719 | VM_BUG_ON(!PageLocked(page)); |
2720 | ||
3c776e64 | 2721 | pc = lookup_page_cgroup(page); |
c0bd3f63 | 2722 | lock_page_cgroup(pc); |
a3b2d692 | 2723 | if (PageCgroupUsed(pc)) { |
c0ff4b85 R |
2724 | memcg = pc->mem_cgroup; |
2725 | if (memcg && !css_tryget(&memcg->css)) | |
2726 | memcg = NULL; | |
e42d9d5d | 2727 | } else if (PageSwapCache(page)) { |
3c776e64 | 2728 | ent.val = page_private(page); |
9fb4b7cc | 2729 | id = lookup_swap_cgroup_id(ent); |
a3b2d692 | 2730 | rcu_read_lock(); |
c0ff4b85 R |
2731 | memcg = mem_cgroup_lookup(id); |
2732 | if (memcg && !css_tryget(&memcg->css)) | |
2733 | memcg = NULL; | |
a3b2d692 | 2734 | rcu_read_unlock(); |
3c776e64 | 2735 | } |
c0bd3f63 | 2736 | unlock_page_cgroup(pc); |
c0ff4b85 | 2737 | return memcg; |
b5a84319 KH |
2738 | } |
2739 | ||
c0ff4b85 | 2740 | static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg, |
5564e88b | 2741 | struct page *page, |
7ec99d62 | 2742 | unsigned int nr_pages, |
9ce70c02 HD |
2743 | enum charge_type ctype, |
2744 | bool lrucare) | |
7a81b88c | 2745 | { |
ce587e65 | 2746 | struct page_cgroup *pc = lookup_page_cgroup(page); |
9ce70c02 | 2747 | struct zone *uninitialized_var(zone); |
fa9add64 | 2748 | struct lruvec *lruvec; |
9ce70c02 | 2749 | bool was_on_lru = false; |
b2402857 | 2750 | bool anon; |
9ce70c02 | 2751 | |
ca3e0214 | 2752 | lock_page_cgroup(pc); |
90deb788 | 2753 | VM_BUG_ON(PageCgroupUsed(pc)); |
ca3e0214 KH |
2754 | /* |
2755 | * we don't need page_cgroup_lock about tail pages, becase they are not | |
2756 | * accessed by any other context at this point. | |
2757 | */ | |
9ce70c02 HD |
2758 | |
2759 | /* | |
2760 | * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page | |
2761 | * may already be on some other mem_cgroup's LRU. Take care of it. | |
2762 | */ | |
2763 | if (lrucare) { | |
2764 | zone = page_zone(page); | |
2765 | spin_lock_irq(&zone->lru_lock); | |
2766 | if (PageLRU(page)) { | |
fa9add64 | 2767 | lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup); |
9ce70c02 | 2768 | ClearPageLRU(page); |
fa9add64 | 2769 | del_page_from_lru_list(page, lruvec, page_lru(page)); |
9ce70c02 HD |
2770 | was_on_lru = true; |
2771 | } | |
2772 | } | |
2773 | ||
c0ff4b85 | 2774 | pc->mem_cgroup = memcg; |
261fb61a KH |
2775 | /* |
2776 | * We access a page_cgroup asynchronously without lock_page_cgroup(). | |
2777 | * Especially when a page_cgroup is taken from a page, pc->mem_cgroup | |
2778 | * is accessed after testing USED bit. To make pc->mem_cgroup visible | |
2779 | * before USED bit, we need memory barrier here. | |
2780 | * See mem_cgroup_add_lru_list(), etc. | |
2781 | */ | |
08e552c6 | 2782 | smp_wmb(); |
b2402857 | 2783 | SetPageCgroupUsed(pc); |
3be91277 | 2784 | |
9ce70c02 HD |
2785 | if (lrucare) { |
2786 | if (was_on_lru) { | |
fa9add64 | 2787 | lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup); |
9ce70c02 HD |
2788 | VM_BUG_ON(PageLRU(page)); |
2789 | SetPageLRU(page); | |
fa9add64 | 2790 | add_page_to_lru_list(page, lruvec, page_lru(page)); |
9ce70c02 HD |
2791 | } |
2792 | spin_unlock_irq(&zone->lru_lock); | |
2793 | } | |
2794 | ||
41326c17 | 2795 | if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON) |
b2402857 KH |
2796 | anon = true; |
2797 | else | |
2798 | anon = false; | |
2799 | ||
2800 | mem_cgroup_charge_statistics(memcg, anon, nr_pages); | |
52d4b9ac | 2801 | unlock_page_cgroup(pc); |
9ce70c02 | 2802 | |
430e4863 KH |
2803 | /* |
2804 | * "charge_statistics" updated event counter. Then, check it. | |
2805 | * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree. | |
2806 | * if they exceeds softlimit. | |
2807 | */ | |
c0ff4b85 | 2808 | memcg_check_events(memcg, page); |
7a81b88c | 2809 | } |
66e1707b | 2810 | |
7cf27982 GC |
2811 | static DEFINE_MUTEX(set_limit_mutex); |
2812 | ||
7ae1e1d0 GC |
2813 | #ifdef CONFIG_MEMCG_KMEM |
2814 | static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg) | |
2815 | { | |
2816 | return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) && | |
2817 | (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK); | |
2818 | } | |
2819 | ||
1f458cbf GC |
2820 | /* |
2821 | * This is a bit cumbersome, but it is rarely used and avoids a backpointer | |
2822 | * in the memcg_cache_params struct. | |
2823 | */ | |
2824 | static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p) | |
2825 | { | |
2826 | struct kmem_cache *cachep; | |
2827 | ||
2828 | VM_BUG_ON(p->is_root_cache); | |
2829 | cachep = p->root_cache; | |
2830 | return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)]; | |
2831 | } | |
2832 | ||
749c5415 GC |
2833 | #ifdef CONFIG_SLABINFO |
2834 | static int mem_cgroup_slabinfo_read(struct cgroup *cont, struct cftype *cft, | |
2835 | struct seq_file *m) | |
2836 | { | |
2837 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); | |
2838 | struct memcg_cache_params *params; | |
2839 | ||
2840 | if (!memcg_can_account_kmem(memcg)) | |
2841 | return -EIO; | |
2842 | ||
2843 | print_slabinfo_header(m); | |
2844 | ||
2845 | mutex_lock(&memcg->slab_caches_mutex); | |
2846 | list_for_each_entry(params, &memcg->memcg_slab_caches, list) | |
2847 | cache_show(memcg_params_to_cache(params), m); | |
2848 | mutex_unlock(&memcg->slab_caches_mutex); | |
2849 | ||
2850 | return 0; | |
2851 | } | |
2852 | #endif | |
2853 | ||
7ae1e1d0 GC |
2854 | static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size) |
2855 | { | |
2856 | struct res_counter *fail_res; | |
2857 | struct mem_cgroup *_memcg; | |
2858 | int ret = 0; | |
2859 | bool may_oom; | |
2860 | ||
2861 | ret = res_counter_charge(&memcg->kmem, size, &fail_res); | |
2862 | if (ret) | |
2863 | return ret; | |
2864 | ||
2865 | /* | |
2866 | * Conditions under which we can wait for the oom_killer. Those are | |
2867 | * the same conditions tested by the core page allocator | |
2868 | */ | |
2869 | may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY); | |
2870 | ||
2871 | _memcg = memcg; | |
2872 | ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT, | |
2873 | &_memcg, may_oom); | |
2874 | ||
2875 | if (ret == -EINTR) { | |
2876 | /* | |
2877 | * __mem_cgroup_try_charge() chosed to bypass to root due to | |
2878 | * OOM kill or fatal signal. Since our only options are to | |
2879 | * either fail the allocation or charge it to this cgroup, do | |
2880 | * it as a temporary condition. But we can't fail. From a | |
2881 | * kmem/slab perspective, the cache has already been selected, | |
2882 | * by mem_cgroup_kmem_get_cache(), so it is too late to change | |
2883 | * our minds. | |
2884 | * | |
2885 | * This condition will only trigger if the task entered | |
2886 | * memcg_charge_kmem in a sane state, but was OOM-killed during | |
2887 | * __mem_cgroup_try_charge() above. Tasks that were already | |
2888 | * dying when the allocation triggers should have been already | |
2889 | * directed to the root cgroup in memcontrol.h | |
2890 | */ | |
2891 | res_counter_charge_nofail(&memcg->res, size, &fail_res); | |
2892 | if (do_swap_account) | |
2893 | res_counter_charge_nofail(&memcg->memsw, size, | |
2894 | &fail_res); | |
2895 | ret = 0; | |
2896 | } else if (ret) | |
2897 | res_counter_uncharge(&memcg->kmem, size); | |
2898 | ||
2899 | return ret; | |
2900 | } | |
2901 | ||
2902 | static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size) | |
2903 | { | |
7ae1e1d0 GC |
2904 | res_counter_uncharge(&memcg->res, size); |
2905 | if (do_swap_account) | |
2906 | res_counter_uncharge(&memcg->memsw, size); | |
7de37682 GC |
2907 | |
2908 | /* Not down to 0 */ | |
2909 | if (res_counter_uncharge(&memcg->kmem, size)) | |
2910 | return; | |
2911 | ||
2912 | if (memcg_kmem_test_and_clear_dead(memcg)) | |
2913 | mem_cgroup_put(memcg); | |
7ae1e1d0 GC |
2914 | } |
2915 | ||
2633d7a0 GC |
2916 | void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep) |
2917 | { | |
2918 | if (!memcg) | |
2919 | return; | |
2920 | ||
2921 | mutex_lock(&memcg->slab_caches_mutex); | |
2922 | list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches); | |
2923 | mutex_unlock(&memcg->slab_caches_mutex); | |
2924 | } | |
2925 | ||
2926 | /* | |
2927 | * helper for acessing a memcg's index. It will be used as an index in the | |
2928 | * child cache array in kmem_cache, and also to derive its name. This function | |
2929 | * will return -1 when this is not a kmem-limited memcg. | |
2930 | */ | |
2931 | int memcg_cache_id(struct mem_cgroup *memcg) | |
2932 | { | |
2933 | return memcg ? memcg->kmemcg_id : -1; | |
2934 | } | |
2935 | ||
55007d84 GC |
2936 | /* |
2937 | * This ends up being protected by the set_limit mutex, during normal | |
2938 | * operation, because that is its main call site. | |
2939 | * | |
2940 | * But when we create a new cache, we can call this as well if its parent | |
2941 | * is kmem-limited. That will have to hold set_limit_mutex as well. | |
2942 | */ | |
2943 | int memcg_update_cache_sizes(struct mem_cgroup *memcg) | |
2944 | { | |
2945 | int num, ret; | |
2946 | ||
2947 | num = ida_simple_get(&kmem_limited_groups, | |
2948 | 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL); | |
2949 | if (num < 0) | |
2950 | return num; | |
2951 | /* | |
2952 | * After this point, kmem_accounted (that we test atomically in | |
2953 | * the beginning of this conditional), is no longer 0. This | |
2954 | * guarantees only one process will set the following boolean | |
2955 | * to true. We don't need test_and_set because we're protected | |
2956 | * by the set_limit_mutex anyway. | |
2957 | */ | |
2958 | memcg_kmem_set_activated(memcg); | |
2959 | ||
2960 | ret = memcg_update_all_caches(num+1); | |
2961 | if (ret) { | |
2962 | ida_simple_remove(&kmem_limited_groups, num); | |
2963 | memcg_kmem_clear_activated(memcg); | |
2964 | return ret; | |
2965 | } | |
2966 | ||
2967 | memcg->kmemcg_id = num; | |
2968 | INIT_LIST_HEAD(&memcg->memcg_slab_caches); | |
2969 | mutex_init(&memcg->slab_caches_mutex); | |
2970 | return 0; | |
2971 | } | |
2972 | ||
2973 | static size_t memcg_caches_array_size(int num_groups) | |
2974 | { | |
2975 | ssize_t size; | |
2976 | if (num_groups <= 0) | |
2977 | return 0; | |
2978 | ||
2979 | size = 2 * num_groups; | |
2980 | if (size < MEMCG_CACHES_MIN_SIZE) | |
2981 | size = MEMCG_CACHES_MIN_SIZE; | |
2982 | else if (size > MEMCG_CACHES_MAX_SIZE) | |
2983 | size = MEMCG_CACHES_MAX_SIZE; | |
2984 | ||
2985 | return size; | |
2986 | } | |
2987 | ||
2988 | /* | |
2989 | * We should update the current array size iff all caches updates succeed. This | |
2990 | * can only be done from the slab side. The slab mutex needs to be held when | |
2991 | * calling this. | |
2992 | */ | |
2993 | void memcg_update_array_size(int num) | |
2994 | { | |
2995 | if (num > memcg_limited_groups_array_size) | |
2996 | memcg_limited_groups_array_size = memcg_caches_array_size(num); | |
2997 | } | |
2998 | ||
2999 | int memcg_update_cache_size(struct kmem_cache *s, int num_groups) | |
3000 | { | |
3001 | struct memcg_cache_params *cur_params = s->memcg_params; | |
3002 | ||
3003 | VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache); | |
3004 | ||
3005 | if (num_groups > memcg_limited_groups_array_size) { | |
3006 | int i; | |
3007 | ssize_t size = memcg_caches_array_size(num_groups); | |
3008 | ||
3009 | size *= sizeof(void *); | |
3010 | size += sizeof(struct memcg_cache_params); | |
3011 | ||
3012 | s->memcg_params = kzalloc(size, GFP_KERNEL); | |
3013 | if (!s->memcg_params) { | |
3014 | s->memcg_params = cur_params; | |
3015 | return -ENOMEM; | |
3016 | } | |
3017 | ||
3018 | s->memcg_params->is_root_cache = true; | |
3019 | ||
3020 | /* | |
3021 | * There is the chance it will be bigger than | |
3022 | * memcg_limited_groups_array_size, if we failed an allocation | |
3023 | * in a cache, in which case all caches updated before it, will | |
3024 | * have a bigger array. | |
3025 | * | |
3026 | * But if that is the case, the data after | |
3027 | * memcg_limited_groups_array_size is certainly unused | |
3028 | */ | |
3029 | for (i = 0; i < memcg_limited_groups_array_size; i++) { | |
3030 | if (!cur_params->memcg_caches[i]) | |
3031 | continue; | |
3032 | s->memcg_params->memcg_caches[i] = | |
3033 | cur_params->memcg_caches[i]; | |
3034 | } | |
3035 | ||
3036 | /* | |
3037 | * Ideally, we would wait until all caches succeed, and only | |
3038 | * then free the old one. But this is not worth the extra | |
3039 | * pointer per-cache we'd have to have for this. | |
3040 | * | |
3041 | * It is not a big deal if some caches are left with a size | |
3042 | * bigger than the others. And all updates will reset this | |
3043 | * anyway. | |
3044 | */ | |
3045 | kfree(cur_params); | |
3046 | } | |
3047 | return 0; | |
3048 | } | |
3049 | ||
943a451a GC |
3050 | int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s, |
3051 | struct kmem_cache *root_cache) | |
2633d7a0 GC |
3052 | { |
3053 | size_t size = sizeof(struct memcg_cache_params); | |
3054 | ||
3055 | if (!memcg_kmem_enabled()) | |
3056 | return 0; | |
3057 | ||
55007d84 GC |
3058 | if (!memcg) |
3059 | size += memcg_limited_groups_array_size * sizeof(void *); | |
3060 | ||
2633d7a0 GC |
3061 | s->memcg_params = kzalloc(size, GFP_KERNEL); |
3062 | if (!s->memcg_params) | |
3063 | return -ENOMEM; | |
3064 | ||
943a451a | 3065 | if (memcg) { |
2633d7a0 | 3066 | s->memcg_params->memcg = memcg; |
943a451a | 3067 | s->memcg_params->root_cache = root_cache; |
4ba902b5 GC |
3068 | } else |
3069 | s->memcg_params->is_root_cache = true; | |
3070 | ||
2633d7a0 GC |
3071 | return 0; |
3072 | } | |
3073 | ||
3074 | void memcg_release_cache(struct kmem_cache *s) | |
3075 | { | |
d7f25f8a GC |
3076 | struct kmem_cache *root; |
3077 | struct mem_cgroup *memcg; | |
3078 | int id; | |
3079 | ||
3080 | /* | |
3081 | * This happens, for instance, when a root cache goes away before we | |
3082 | * add any memcg. | |
3083 | */ | |
3084 | if (!s->memcg_params) | |
3085 | return; | |
3086 | ||
3087 | if (s->memcg_params->is_root_cache) | |
3088 | goto out; | |
3089 | ||
3090 | memcg = s->memcg_params->memcg; | |
3091 | id = memcg_cache_id(memcg); | |
3092 | ||
3093 | root = s->memcg_params->root_cache; | |
3094 | root->memcg_params->memcg_caches[id] = NULL; | |
3095 | mem_cgroup_put(memcg); | |
3096 | ||
3097 | mutex_lock(&memcg->slab_caches_mutex); | |
3098 | list_del(&s->memcg_params->list); | |
3099 | mutex_unlock(&memcg->slab_caches_mutex); | |
3100 | ||
3101 | out: | |
2633d7a0 GC |
3102 | kfree(s->memcg_params); |
3103 | } | |
3104 | ||
0e9d92f2 GC |
3105 | /* |
3106 | * During the creation a new cache, we need to disable our accounting mechanism | |
3107 | * altogether. This is true even if we are not creating, but rather just | |
3108 | * enqueing new caches to be created. | |
3109 | * | |
3110 | * This is because that process will trigger allocations; some visible, like | |
3111 | * explicit kmallocs to auxiliary data structures, name strings and internal | |
3112 | * cache structures; some well concealed, like INIT_WORK() that can allocate | |
3113 | * objects during debug. | |
3114 | * | |
3115 | * If any allocation happens during memcg_kmem_get_cache, we will recurse back | |
3116 | * to it. This may not be a bounded recursion: since the first cache creation | |
3117 | * failed to complete (waiting on the allocation), we'll just try to create the | |
3118 | * cache again, failing at the same point. | |
3119 | * | |
3120 | * memcg_kmem_get_cache is prepared to abort after seeing a positive count of | |
3121 | * memcg_kmem_skip_account. So we enclose anything that might allocate memory | |
3122 | * inside the following two functions. | |
3123 | */ | |
3124 | static inline void memcg_stop_kmem_account(void) | |
3125 | { | |
3126 | VM_BUG_ON(!current->mm); | |
3127 | current->memcg_kmem_skip_account++; | |
3128 | } | |
3129 | ||
3130 | static inline void memcg_resume_kmem_account(void) | |
3131 | { | |
3132 | VM_BUG_ON(!current->mm); | |
3133 | current->memcg_kmem_skip_account--; | |
3134 | } | |
3135 | ||
1f458cbf GC |
3136 | static void kmem_cache_destroy_work_func(struct work_struct *w) |
3137 | { | |
3138 | struct kmem_cache *cachep; | |
3139 | struct memcg_cache_params *p; | |
3140 | ||
3141 | p = container_of(w, struct memcg_cache_params, destroy); | |
3142 | ||
3143 | cachep = memcg_params_to_cache(p); | |
3144 | ||
22933152 GC |
3145 | /* |
3146 | * If we get down to 0 after shrink, we could delete right away. | |
3147 | * However, memcg_release_pages() already puts us back in the workqueue | |
3148 | * in that case. If we proceed deleting, we'll get a dangling | |
3149 | * reference, and removing the object from the workqueue in that case | |
3150 | * is unnecessary complication. We are not a fast path. | |
3151 | * | |
3152 | * Note that this case is fundamentally different from racing with | |
3153 | * shrink_slab(): if memcg_cgroup_destroy_cache() is called in | |
3154 | * kmem_cache_shrink, not only we would be reinserting a dead cache | |
3155 | * into the queue, but doing so from inside the worker racing to | |
3156 | * destroy it. | |
3157 | * | |
3158 | * So if we aren't down to zero, we'll just schedule a worker and try | |
3159 | * again | |
3160 | */ | |
3161 | if (atomic_read(&cachep->memcg_params->nr_pages) != 0) { | |
3162 | kmem_cache_shrink(cachep); | |
3163 | if (atomic_read(&cachep->memcg_params->nr_pages) == 0) | |
3164 | return; | |
3165 | } else | |
1f458cbf GC |
3166 | kmem_cache_destroy(cachep); |
3167 | } | |
3168 | ||
3169 | void mem_cgroup_destroy_cache(struct kmem_cache *cachep) | |
3170 | { | |
3171 | if (!cachep->memcg_params->dead) | |
3172 | return; | |
3173 | ||
22933152 GC |
3174 | /* |
3175 | * There are many ways in which we can get here. | |
3176 | * | |
3177 | * We can get to a memory-pressure situation while the delayed work is | |
3178 | * still pending to run. The vmscan shrinkers can then release all | |
3179 | * cache memory and get us to destruction. If this is the case, we'll | |
3180 | * be executed twice, which is a bug (the second time will execute over | |
3181 | * bogus data). In this case, cancelling the work should be fine. | |
3182 | * | |
3183 | * But we can also get here from the worker itself, if | |
3184 | * kmem_cache_shrink is enough to shake all the remaining objects and | |
3185 | * get the page count to 0. In this case, we'll deadlock if we try to | |
3186 | * cancel the work (the worker runs with an internal lock held, which | |
3187 | * is the same lock we would hold for cancel_work_sync().) | |
3188 | * | |
3189 | * Since we can't possibly know who got us here, just refrain from | |
3190 | * running if there is already work pending | |
3191 | */ | |
3192 | if (work_pending(&cachep->memcg_params->destroy)) | |
3193 | return; | |
1f458cbf GC |
3194 | /* |
3195 | * We have to defer the actual destroying to a workqueue, because | |
3196 | * we might currently be in a context that cannot sleep. | |
3197 | */ | |
3198 | schedule_work(&cachep->memcg_params->destroy); | |
3199 | } | |
3200 | ||
d7f25f8a GC |
3201 | static char *memcg_cache_name(struct mem_cgroup *memcg, struct kmem_cache *s) |
3202 | { | |
3203 | char *name; | |
3204 | struct dentry *dentry; | |
3205 | ||
3206 | rcu_read_lock(); | |
3207 | dentry = rcu_dereference(memcg->css.cgroup->dentry); | |
3208 | rcu_read_unlock(); | |
3209 | ||
3210 | BUG_ON(dentry == NULL); | |
3211 | ||
3212 | name = kasprintf(GFP_KERNEL, "%s(%d:%s)", s->name, | |
3213 | memcg_cache_id(memcg), dentry->d_name.name); | |
3214 | ||
3215 | return name; | |
3216 | } | |
3217 | ||
3218 | static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg, | |
3219 | struct kmem_cache *s) | |
3220 | { | |
3221 | char *name; | |
3222 | struct kmem_cache *new; | |
3223 | ||
3224 | name = memcg_cache_name(memcg, s); | |
3225 | if (!name) | |
3226 | return NULL; | |
3227 | ||
3228 | new = kmem_cache_create_memcg(memcg, name, s->object_size, s->align, | |
943a451a | 3229 | (s->flags & ~SLAB_PANIC), s->ctor, s); |
d7f25f8a | 3230 | |
d79923fa GC |
3231 | if (new) |
3232 | new->allocflags |= __GFP_KMEMCG; | |
3233 | ||
d7f25f8a GC |
3234 | kfree(name); |
3235 | return new; | |
3236 | } | |
3237 | ||
3238 | /* | |
3239 | * This lock protects updaters, not readers. We want readers to be as fast as | |
3240 | * they can, and they will either see NULL or a valid cache value. Our model | |
3241 | * allow them to see NULL, in which case the root memcg will be selected. | |
3242 | * | |
3243 | * We need this lock because multiple allocations to the same cache from a non | |
3244 | * will span more than one worker. Only one of them can create the cache. | |
3245 | */ | |
3246 | static DEFINE_MUTEX(memcg_cache_mutex); | |
3247 | static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg, | |
3248 | struct kmem_cache *cachep) | |
3249 | { | |
3250 | struct kmem_cache *new_cachep; | |
3251 | int idx; | |
3252 | ||
3253 | BUG_ON(!memcg_can_account_kmem(memcg)); | |
3254 | ||
3255 | idx = memcg_cache_id(memcg); | |
3256 | ||
3257 | mutex_lock(&memcg_cache_mutex); | |
3258 | new_cachep = cachep->memcg_params->memcg_caches[idx]; | |
3259 | if (new_cachep) | |
3260 | goto out; | |
3261 | ||
3262 | new_cachep = kmem_cache_dup(memcg, cachep); | |
d7f25f8a GC |
3263 | if (new_cachep == NULL) { |
3264 | new_cachep = cachep; | |
3265 | goto out; | |
3266 | } | |
3267 | ||
3268 | mem_cgroup_get(memcg); | |
1f458cbf | 3269 | atomic_set(&new_cachep->memcg_params->nr_pages , 0); |
d7f25f8a GC |
3270 | |
3271 | cachep->memcg_params->memcg_caches[idx] = new_cachep; | |
3272 | /* | |
3273 | * the readers won't lock, make sure everybody sees the updated value, | |
3274 | * so they won't put stuff in the queue again for no reason | |
3275 | */ | |
3276 | wmb(); | |
3277 | out: | |
3278 | mutex_unlock(&memcg_cache_mutex); | |
3279 | return new_cachep; | |
3280 | } | |
3281 | ||
7cf27982 GC |
3282 | void kmem_cache_destroy_memcg_children(struct kmem_cache *s) |
3283 | { | |
3284 | struct kmem_cache *c; | |
3285 | int i; | |
3286 | ||
3287 | if (!s->memcg_params) | |
3288 | return; | |
3289 | if (!s->memcg_params->is_root_cache) | |
3290 | return; | |
3291 | ||
3292 | /* | |
3293 | * If the cache is being destroyed, we trust that there is no one else | |
3294 | * requesting objects from it. Even if there are, the sanity checks in | |
3295 | * kmem_cache_destroy should caught this ill-case. | |
3296 | * | |
3297 | * Still, we don't want anyone else freeing memcg_caches under our | |
3298 | * noses, which can happen if a new memcg comes to life. As usual, | |
3299 | * we'll take the set_limit_mutex to protect ourselves against this. | |
3300 | */ | |
3301 | mutex_lock(&set_limit_mutex); | |
3302 | for (i = 0; i < memcg_limited_groups_array_size; i++) { | |
3303 | c = s->memcg_params->memcg_caches[i]; | |
3304 | if (!c) | |
3305 | continue; | |
3306 | ||
3307 | /* | |
3308 | * We will now manually delete the caches, so to avoid races | |
3309 | * we need to cancel all pending destruction workers and | |
3310 | * proceed with destruction ourselves. | |
3311 | * | |
3312 | * kmem_cache_destroy() will call kmem_cache_shrink internally, | |
3313 | * and that could spawn the workers again: it is likely that | |
3314 | * the cache still have active pages until this very moment. | |
3315 | * This would lead us back to mem_cgroup_destroy_cache. | |
3316 | * | |
3317 | * But that will not execute at all if the "dead" flag is not | |
3318 | * set, so flip it down to guarantee we are in control. | |
3319 | */ | |
3320 | c->memcg_params->dead = false; | |
22933152 | 3321 | cancel_work_sync(&c->memcg_params->destroy); |
7cf27982 GC |
3322 | kmem_cache_destroy(c); |
3323 | } | |
3324 | mutex_unlock(&set_limit_mutex); | |
3325 | } | |
3326 | ||
d7f25f8a GC |
3327 | struct create_work { |
3328 | struct mem_cgroup *memcg; | |
3329 | struct kmem_cache *cachep; | |
3330 | struct work_struct work; | |
3331 | }; | |
3332 | ||
1f458cbf GC |
3333 | static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg) |
3334 | { | |
3335 | struct kmem_cache *cachep; | |
3336 | struct memcg_cache_params *params; | |
3337 | ||
3338 | if (!memcg_kmem_is_active(memcg)) | |
3339 | return; | |
3340 | ||
3341 | mutex_lock(&memcg->slab_caches_mutex); | |
3342 | list_for_each_entry(params, &memcg->memcg_slab_caches, list) { | |
3343 | cachep = memcg_params_to_cache(params); | |
3344 | cachep->memcg_params->dead = true; | |
3345 | INIT_WORK(&cachep->memcg_params->destroy, | |
22933152 | 3346 | kmem_cache_destroy_work_func); |
1f458cbf GC |
3347 | schedule_work(&cachep->memcg_params->destroy); |
3348 | } | |
3349 | mutex_unlock(&memcg->slab_caches_mutex); | |
3350 | } | |
3351 | ||
d7f25f8a GC |
3352 | static void memcg_create_cache_work_func(struct work_struct *w) |
3353 | { | |
3354 | struct create_work *cw; | |
3355 | ||
3356 | cw = container_of(w, struct create_work, work); | |
3357 | memcg_create_kmem_cache(cw->memcg, cw->cachep); | |
3358 | /* Drop the reference gotten when we enqueued. */ | |
3359 | css_put(&cw->memcg->css); | |
3360 | kfree(cw); | |
3361 | } | |
3362 | ||
3363 | /* | |
3364 | * Enqueue the creation of a per-memcg kmem_cache. | |
3365 | * Called with rcu_read_lock. | |
3366 | */ | |
0e9d92f2 GC |
3367 | static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg, |
3368 | struct kmem_cache *cachep) | |
d7f25f8a GC |
3369 | { |
3370 | struct create_work *cw; | |
3371 | ||
3372 | cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT); | |
3373 | if (cw == NULL) | |
3374 | return; | |
3375 | ||
3376 | /* The corresponding put will be done in the workqueue. */ | |
3377 | if (!css_tryget(&memcg->css)) { | |
3378 | kfree(cw); | |
3379 | return; | |
3380 | } | |
3381 | ||
3382 | cw->memcg = memcg; | |
3383 | cw->cachep = cachep; | |
3384 | ||
3385 | INIT_WORK(&cw->work, memcg_create_cache_work_func); | |
3386 | schedule_work(&cw->work); | |
3387 | } | |
3388 | ||
0e9d92f2 GC |
3389 | static void memcg_create_cache_enqueue(struct mem_cgroup *memcg, |
3390 | struct kmem_cache *cachep) | |
3391 | { | |
3392 | /* | |
3393 | * We need to stop accounting when we kmalloc, because if the | |
3394 | * corresponding kmalloc cache is not yet created, the first allocation | |
3395 | * in __memcg_create_cache_enqueue will recurse. | |
3396 | * | |
3397 | * However, it is better to enclose the whole function. Depending on | |
3398 | * the debugging options enabled, INIT_WORK(), for instance, can | |
3399 | * trigger an allocation. This too, will make us recurse. Because at | |
3400 | * this point we can't allow ourselves back into memcg_kmem_get_cache, | |
3401 | * the safest choice is to do it like this, wrapping the whole function. | |
3402 | */ | |
3403 | memcg_stop_kmem_account(); | |
3404 | __memcg_create_cache_enqueue(memcg, cachep); | |
3405 | memcg_resume_kmem_account(); | |
3406 | } | |
d7f25f8a GC |
3407 | /* |
3408 | * Return the kmem_cache we're supposed to use for a slab allocation. | |
3409 | * We try to use the current memcg's version of the cache. | |
3410 | * | |
3411 | * If the cache does not exist yet, if we are the first user of it, | |
3412 | * we either create it immediately, if possible, or create it asynchronously | |
3413 | * in a workqueue. | |
3414 | * In the latter case, we will let the current allocation go through with | |
3415 | * the original cache. | |
3416 | * | |
3417 | * Can't be called in interrupt context or from kernel threads. | |
3418 | * This function needs to be called with rcu_read_lock() held. | |
3419 | */ | |
3420 | struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep, | |
3421 | gfp_t gfp) | |
3422 | { | |
3423 | struct mem_cgroup *memcg; | |
3424 | int idx; | |
3425 | ||
3426 | VM_BUG_ON(!cachep->memcg_params); | |
3427 | VM_BUG_ON(!cachep->memcg_params->is_root_cache); | |
3428 | ||
0e9d92f2 GC |
3429 | if (!current->mm || current->memcg_kmem_skip_account) |
3430 | return cachep; | |
3431 | ||
d7f25f8a GC |
3432 | rcu_read_lock(); |
3433 | memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner)); | |
3434 | rcu_read_unlock(); | |
3435 | ||
3436 | if (!memcg_can_account_kmem(memcg)) | |
3437 | return cachep; | |
3438 | ||
3439 | idx = memcg_cache_id(memcg); | |
3440 | ||
3441 | /* | |
3442 | * barrier to mare sure we're always seeing the up to date value. The | |
3443 | * code updating memcg_caches will issue a write barrier to match this. | |
3444 | */ | |
3445 | read_barrier_depends(); | |
3446 | if (unlikely(cachep->memcg_params->memcg_caches[idx] == NULL)) { | |
3447 | /* | |
3448 | * If we are in a safe context (can wait, and not in interrupt | |
3449 | * context), we could be be predictable and return right away. | |
3450 | * This would guarantee that the allocation being performed | |
3451 | * already belongs in the new cache. | |
3452 | * | |
3453 | * However, there are some clashes that can arrive from locking. | |
3454 | * For instance, because we acquire the slab_mutex while doing | |
3455 | * kmem_cache_dup, this means no further allocation could happen | |
3456 | * with the slab_mutex held. | |
3457 | * | |
3458 | * Also, because cache creation issue get_online_cpus(), this | |
3459 | * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex, | |
3460 | * that ends up reversed during cpu hotplug. (cpuset allocates | |
3461 | * a bunch of GFP_KERNEL memory during cpuup). Due to all that, | |
3462 | * better to defer everything. | |
3463 | */ | |
3464 | memcg_create_cache_enqueue(memcg, cachep); | |
3465 | return cachep; | |
3466 | } | |
3467 | ||
3468 | return cachep->memcg_params->memcg_caches[idx]; | |
3469 | } | |
3470 | EXPORT_SYMBOL(__memcg_kmem_get_cache); | |
3471 | ||
7ae1e1d0 GC |
3472 | /* |
3473 | * We need to verify if the allocation against current->mm->owner's memcg is | |
3474 | * possible for the given order. But the page is not allocated yet, so we'll | |
3475 | * need a further commit step to do the final arrangements. | |
3476 | * | |
3477 | * It is possible for the task to switch cgroups in this mean time, so at | |
3478 | * commit time, we can't rely on task conversion any longer. We'll then use | |
3479 | * the handle argument to return to the caller which cgroup we should commit | |
3480 | * against. We could also return the memcg directly and avoid the pointer | |
3481 | * passing, but a boolean return value gives better semantics considering | |
3482 | * the compiled-out case as well. | |
3483 | * | |
3484 | * Returning true means the allocation is possible. | |
3485 | */ | |
3486 | bool | |
3487 | __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order) | |
3488 | { | |
3489 | struct mem_cgroup *memcg; | |
3490 | int ret; | |
3491 | ||
3492 | *_memcg = NULL; | |
3493 | memcg = try_get_mem_cgroup_from_mm(current->mm); | |
3494 | ||
3495 | /* | |
3496 | * very rare case described in mem_cgroup_from_task. Unfortunately there | |
3497 | * isn't much we can do without complicating this too much, and it would | |
3498 | * be gfp-dependent anyway. Just let it go | |
3499 | */ | |
3500 | if (unlikely(!memcg)) | |
3501 | return true; | |
3502 | ||
3503 | if (!memcg_can_account_kmem(memcg)) { | |
3504 | css_put(&memcg->css); | |
3505 | return true; | |
3506 | } | |
3507 | ||
7ae1e1d0 GC |
3508 | ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order); |
3509 | if (!ret) | |
3510 | *_memcg = memcg; | |
7ae1e1d0 GC |
3511 | |
3512 | css_put(&memcg->css); | |
3513 | return (ret == 0); | |
3514 | } | |
3515 | ||
3516 | void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, | |
3517 | int order) | |
3518 | { | |
3519 | struct page_cgroup *pc; | |
3520 | ||
3521 | VM_BUG_ON(mem_cgroup_is_root(memcg)); | |
3522 | ||
3523 | /* The page allocation failed. Revert */ | |
3524 | if (!page) { | |
3525 | memcg_uncharge_kmem(memcg, PAGE_SIZE << order); | |
7ae1e1d0 GC |
3526 | return; |
3527 | } | |
3528 | ||
3529 | pc = lookup_page_cgroup(page); | |
3530 | lock_page_cgroup(pc); | |
3531 | pc->mem_cgroup = memcg; | |
3532 | SetPageCgroupUsed(pc); | |
3533 | unlock_page_cgroup(pc); | |
3534 | } | |
3535 | ||
3536 | void __memcg_kmem_uncharge_pages(struct page *page, int order) | |
3537 | { | |
3538 | struct mem_cgroup *memcg = NULL; | |
3539 | struct page_cgroup *pc; | |
3540 | ||
3541 | ||
3542 | pc = lookup_page_cgroup(page); | |
3543 | /* | |
3544 | * Fast unlocked return. Theoretically might have changed, have to | |
3545 | * check again after locking. | |
3546 | */ | |
3547 | if (!PageCgroupUsed(pc)) | |
3548 | return; | |
3549 | ||
3550 | lock_page_cgroup(pc); | |
3551 | if (PageCgroupUsed(pc)) { | |
3552 | memcg = pc->mem_cgroup; | |
3553 | ClearPageCgroupUsed(pc); | |
3554 | } | |
3555 | unlock_page_cgroup(pc); | |
3556 | ||
3557 | /* | |
3558 | * We trust that only if there is a memcg associated with the page, it | |
3559 | * is a valid allocation | |
3560 | */ | |
3561 | if (!memcg) | |
3562 | return; | |
3563 | ||
3564 | VM_BUG_ON(mem_cgroup_is_root(memcg)); | |
3565 | memcg_uncharge_kmem(memcg, PAGE_SIZE << order); | |
7ae1e1d0 | 3566 | } |
1f458cbf GC |
3567 | #else |
3568 | static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg) | |
3569 | { | |
3570 | } | |
7ae1e1d0 GC |
3571 | #endif /* CONFIG_MEMCG_KMEM */ |
3572 | ||
ca3e0214 KH |
3573 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
3574 | ||
a0db00fc | 3575 | #define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION) |
ca3e0214 KH |
3576 | /* |
3577 | * Because tail pages are not marked as "used", set it. We're under | |
e94c8a9c KH |
3578 | * zone->lru_lock, 'splitting on pmd' and compound_lock. |
3579 | * charge/uncharge will be never happen and move_account() is done under | |
3580 | * compound_lock(), so we don't have to take care of races. | |
ca3e0214 | 3581 | */ |
e94c8a9c | 3582 | void mem_cgroup_split_huge_fixup(struct page *head) |
ca3e0214 KH |
3583 | { |
3584 | struct page_cgroup *head_pc = lookup_page_cgroup(head); | |
e94c8a9c KH |
3585 | struct page_cgroup *pc; |
3586 | int i; | |
ca3e0214 | 3587 | |
3d37c4a9 KH |
3588 | if (mem_cgroup_disabled()) |
3589 | return; | |
e94c8a9c KH |
3590 | for (i = 1; i < HPAGE_PMD_NR; i++) { |
3591 | pc = head_pc + i; | |
3592 | pc->mem_cgroup = head_pc->mem_cgroup; | |
3593 | smp_wmb();/* see __commit_charge() */ | |
e94c8a9c KH |
3594 | pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT; |
3595 | } | |
ca3e0214 | 3596 | } |
12d27107 | 3597 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
ca3e0214 | 3598 | |
f817ed48 | 3599 | /** |
de3638d9 | 3600 | * mem_cgroup_move_account - move account of the page |
5564e88b | 3601 | * @page: the page |
7ec99d62 | 3602 | * @nr_pages: number of regular pages (>1 for huge pages) |
f817ed48 KH |
3603 | * @pc: page_cgroup of the page. |
3604 | * @from: mem_cgroup which the page is moved from. | |
3605 | * @to: mem_cgroup which the page is moved to. @from != @to. | |
3606 | * | |
3607 | * The caller must confirm following. | |
08e552c6 | 3608 | * - page is not on LRU (isolate_page() is useful.) |
7ec99d62 | 3609 | * - compound_lock is held when nr_pages > 1 |
f817ed48 | 3610 | * |
2f3479b1 KH |
3611 | * This function doesn't do "charge" to new cgroup and doesn't do "uncharge" |
3612 | * from old cgroup. | |
f817ed48 | 3613 | */ |
7ec99d62 JW |
3614 | static int mem_cgroup_move_account(struct page *page, |
3615 | unsigned int nr_pages, | |
3616 | struct page_cgroup *pc, | |
3617 | struct mem_cgroup *from, | |
2f3479b1 | 3618 | struct mem_cgroup *to) |
f817ed48 | 3619 | { |
de3638d9 JW |
3620 | unsigned long flags; |
3621 | int ret; | |
b2402857 | 3622 | bool anon = PageAnon(page); |
987eba66 | 3623 | |
f817ed48 | 3624 | VM_BUG_ON(from == to); |
5564e88b | 3625 | VM_BUG_ON(PageLRU(page)); |
de3638d9 JW |
3626 | /* |
3627 | * The page is isolated from LRU. So, collapse function | |
3628 | * will not handle this page. But page splitting can happen. | |
3629 | * Do this check under compound_page_lock(). The caller should | |
3630 | * hold it. | |
3631 | */ | |
3632 | ret = -EBUSY; | |
7ec99d62 | 3633 | if (nr_pages > 1 && !PageTransHuge(page)) |
de3638d9 JW |
3634 | goto out; |
3635 | ||
3636 | lock_page_cgroup(pc); | |
3637 | ||
3638 | ret = -EINVAL; | |
3639 | if (!PageCgroupUsed(pc) || pc->mem_cgroup != from) | |
3640 | goto unlock; | |
3641 | ||
312734c0 | 3642 | move_lock_mem_cgroup(from, &flags); |
f817ed48 | 3643 | |
2ff76f11 | 3644 | if (!anon && page_mapped(page)) { |
c62b1a3b KH |
3645 | /* Update mapped_file data for mem_cgroup */ |
3646 | preempt_disable(); | |
3647 | __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); | |
3648 | __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); | |
3649 | preempt_enable(); | |
d69b042f | 3650 | } |
b2402857 | 3651 | mem_cgroup_charge_statistics(from, anon, -nr_pages); |
d69b042f | 3652 | |
854ffa8d | 3653 | /* caller should have done css_get */ |
08e552c6 | 3654 | pc->mem_cgroup = to; |
b2402857 | 3655 | mem_cgroup_charge_statistics(to, anon, nr_pages); |
312734c0 | 3656 | move_unlock_mem_cgroup(from, &flags); |
de3638d9 JW |
3657 | ret = 0; |
3658 | unlock: | |
57f9fd7d | 3659 | unlock_page_cgroup(pc); |
d2265e6f KH |
3660 | /* |
3661 | * check events | |
3662 | */ | |
5564e88b JW |
3663 | memcg_check_events(to, page); |
3664 | memcg_check_events(from, page); | |
de3638d9 | 3665 | out: |
f817ed48 KH |
3666 | return ret; |
3667 | } | |
3668 | ||
2ef37d3f MH |
3669 | /** |
3670 | * mem_cgroup_move_parent - moves page to the parent group | |
3671 | * @page: the page to move | |
3672 | * @pc: page_cgroup of the page | |
3673 | * @child: page's cgroup | |
3674 | * | |
3675 | * move charges to its parent or the root cgroup if the group has no | |
3676 | * parent (aka use_hierarchy==0). | |
3677 | * Although this might fail (get_page_unless_zero, isolate_lru_page or | |
3678 | * mem_cgroup_move_account fails) the failure is always temporary and | |
3679 | * it signals a race with a page removal/uncharge or migration. In the | |
3680 | * first case the page is on the way out and it will vanish from the LRU | |
3681 | * on the next attempt and the call should be retried later. | |
3682 | * Isolation from the LRU fails only if page has been isolated from | |
3683 | * the LRU since we looked at it and that usually means either global | |
3684 | * reclaim or migration going on. The page will either get back to the | |
3685 | * LRU or vanish. | |
3686 | * Finaly mem_cgroup_move_account fails only if the page got uncharged | |
3687 | * (!PageCgroupUsed) or moved to a different group. The page will | |
3688 | * disappear in the next attempt. | |
f817ed48 | 3689 | */ |
5564e88b JW |
3690 | static int mem_cgroup_move_parent(struct page *page, |
3691 | struct page_cgroup *pc, | |
6068bf01 | 3692 | struct mem_cgroup *child) |
f817ed48 | 3693 | { |
f817ed48 | 3694 | struct mem_cgroup *parent; |
7ec99d62 | 3695 | unsigned int nr_pages; |
4be4489f | 3696 | unsigned long uninitialized_var(flags); |
f817ed48 KH |
3697 | int ret; |
3698 | ||
d8423011 | 3699 | VM_BUG_ON(mem_cgroup_is_root(child)); |
f817ed48 | 3700 | |
57f9fd7d DN |
3701 | ret = -EBUSY; |
3702 | if (!get_page_unless_zero(page)) | |
3703 | goto out; | |
3704 | if (isolate_lru_page(page)) | |
3705 | goto put; | |
52dbb905 | 3706 | |
7ec99d62 | 3707 | nr_pages = hpage_nr_pages(page); |
08e552c6 | 3708 | |
cc926f78 KH |
3709 | parent = parent_mem_cgroup(child); |
3710 | /* | |
3711 | * If no parent, move charges to root cgroup. | |
3712 | */ | |
3713 | if (!parent) | |
3714 | parent = root_mem_cgroup; | |
f817ed48 | 3715 | |
2ef37d3f MH |
3716 | if (nr_pages > 1) { |
3717 | VM_BUG_ON(!PageTransHuge(page)); | |
987eba66 | 3718 | flags = compound_lock_irqsave(page); |
2ef37d3f | 3719 | } |
987eba66 | 3720 | |
cc926f78 | 3721 | ret = mem_cgroup_move_account(page, nr_pages, |
2f3479b1 | 3722 | pc, child, parent); |
cc926f78 KH |
3723 | if (!ret) |
3724 | __mem_cgroup_cancel_local_charge(child, nr_pages); | |
8dba474f | 3725 | |
7ec99d62 | 3726 | if (nr_pages > 1) |
987eba66 | 3727 | compound_unlock_irqrestore(page, flags); |
08e552c6 | 3728 | putback_lru_page(page); |
57f9fd7d | 3729 | put: |
40d58138 | 3730 | put_page(page); |
57f9fd7d | 3731 | out: |
f817ed48 KH |
3732 | return ret; |
3733 | } | |
3734 | ||
7a81b88c KH |
3735 | /* |
3736 | * Charge the memory controller for page usage. | |
3737 | * Return | |
3738 | * 0 if the charge was successful | |
3739 | * < 0 if the cgroup is over its limit | |
3740 | */ | |
3741 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, | |
73045c47 | 3742 | gfp_t gfp_mask, enum charge_type ctype) |
7a81b88c | 3743 | { |
c0ff4b85 | 3744 | struct mem_cgroup *memcg = NULL; |
7ec99d62 | 3745 | unsigned int nr_pages = 1; |
8493ae43 | 3746 | bool oom = true; |
7a81b88c | 3747 | int ret; |
ec168510 | 3748 | |
37c2ac78 | 3749 | if (PageTransHuge(page)) { |
7ec99d62 | 3750 | nr_pages <<= compound_order(page); |
37c2ac78 | 3751 | VM_BUG_ON(!PageTransHuge(page)); |
8493ae43 JW |
3752 | /* |
3753 | * Never OOM-kill a process for a huge page. The | |
3754 | * fault handler will fall back to regular pages. | |
3755 | */ | |
3756 | oom = false; | |
37c2ac78 | 3757 | } |
7a81b88c | 3758 | |
c0ff4b85 | 3759 | ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom); |
38c5d72f | 3760 | if (ret == -ENOMEM) |
7a81b88c | 3761 | return ret; |
ce587e65 | 3762 | __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false); |
8a9f3ccd | 3763 | return 0; |
8a9f3ccd BS |
3764 | } |
3765 | ||
7a81b88c KH |
3766 | int mem_cgroup_newpage_charge(struct page *page, |
3767 | struct mm_struct *mm, gfp_t gfp_mask) | |
217bc319 | 3768 | { |
f8d66542 | 3769 | if (mem_cgroup_disabled()) |
cede86ac | 3770 | return 0; |
7a0524cf JW |
3771 | VM_BUG_ON(page_mapped(page)); |
3772 | VM_BUG_ON(page->mapping && !PageAnon(page)); | |
3773 | VM_BUG_ON(!mm); | |
217bc319 | 3774 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
41326c17 | 3775 | MEM_CGROUP_CHARGE_TYPE_ANON); |
217bc319 KH |
3776 | } |
3777 | ||
54595fe2 KH |
3778 | /* |
3779 | * While swap-in, try_charge -> commit or cancel, the page is locked. | |
3780 | * And when try_charge() successfully returns, one refcnt to memcg without | |
21ae2956 | 3781 | * struct page_cgroup is acquired. This refcnt will be consumed by |
54595fe2 KH |
3782 | * "commit()" or removed by "cancel()" |
3783 | */ | |
0435a2fd JW |
3784 | static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
3785 | struct page *page, | |
3786 | gfp_t mask, | |
3787 | struct mem_cgroup **memcgp) | |
8c7c6e34 | 3788 | { |
c0ff4b85 | 3789 | struct mem_cgroup *memcg; |
90deb788 | 3790 | struct page_cgroup *pc; |
54595fe2 | 3791 | int ret; |
8c7c6e34 | 3792 | |
90deb788 JW |
3793 | pc = lookup_page_cgroup(page); |
3794 | /* | |
3795 | * Every swap fault against a single page tries to charge the | |
3796 | * page, bail as early as possible. shmem_unuse() encounters | |
3797 | * already charged pages, too. The USED bit is protected by | |
3798 | * the page lock, which serializes swap cache removal, which | |
3799 | * in turn serializes uncharging. | |
3800 | */ | |
3801 | if (PageCgroupUsed(pc)) | |
3802 | return 0; | |
8c7c6e34 KH |
3803 | if (!do_swap_account) |
3804 | goto charge_cur_mm; | |
c0ff4b85 R |
3805 | memcg = try_get_mem_cgroup_from_page(page); |
3806 | if (!memcg) | |
54595fe2 | 3807 | goto charge_cur_mm; |
72835c86 JW |
3808 | *memcgp = memcg; |
3809 | ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true); | |
c0ff4b85 | 3810 | css_put(&memcg->css); |
38c5d72f KH |
3811 | if (ret == -EINTR) |
3812 | ret = 0; | |
54595fe2 | 3813 | return ret; |
8c7c6e34 | 3814 | charge_cur_mm: |
38c5d72f KH |
3815 | ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true); |
3816 | if (ret == -EINTR) | |
3817 | ret = 0; | |
3818 | return ret; | |
8c7c6e34 KH |
3819 | } |
3820 | ||
0435a2fd JW |
3821 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page, |
3822 | gfp_t gfp_mask, struct mem_cgroup **memcgp) | |
3823 | { | |
3824 | *memcgp = NULL; | |
3825 | if (mem_cgroup_disabled()) | |
3826 | return 0; | |
bdf4f4d2 JW |
3827 | /* |
3828 | * A racing thread's fault, or swapoff, may have already | |
3829 | * updated the pte, and even removed page from swap cache: in | |
3830 | * those cases unuse_pte()'s pte_same() test will fail; but | |
3831 | * there's also a KSM case which does need to charge the page. | |
3832 | */ | |
3833 | if (!PageSwapCache(page)) { | |
3834 | int ret; | |
3835 | ||
3836 | ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true); | |
3837 | if (ret == -EINTR) | |
3838 | ret = 0; | |
3839 | return ret; | |
3840 | } | |
0435a2fd JW |
3841 | return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp); |
3842 | } | |
3843 | ||
827a03d2 JW |
3844 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg) |
3845 | { | |
3846 | if (mem_cgroup_disabled()) | |
3847 | return; | |
3848 | if (!memcg) | |
3849 | return; | |
3850 | __mem_cgroup_cancel_charge(memcg, 1); | |
3851 | } | |
3852 | ||
83aae4c7 | 3853 | static void |
72835c86 | 3854 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg, |
83aae4c7 | 3855 | enum charge_type ctype) |
7a81b88c | 3856 | { |
f8d66542 | 3857 | if (mem_cgroup_disabled()) |
7a81b88c | 3858 | return; |
72835c86 | 3859 | if (!memcg) |
7a81b88c | 3860 | return; |
5a6475a4 | 3861 | |
ce587e65 | 3862 | __mem_cgroup_commit_charge(memcg, page, 1, ctype, true); |
8c7c6e34 KH |
3863 | /* |
3864 | * Now swap is on-memory. This means this page may be | |
3865 | * counted both as mem and swap....double count. | |
03f3c433 KH |
3866 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
3867 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() | |
3868 | * may call delete_from_swap_cache() before reach here. | |
8c7c6e34 | 3869 | */ |
03f3c433 | 3870 | if (do_swap_account && PageSwapCache(page)) { |
8c7c6e34 | 3871 | swp_entry_t ent = {.val = page_private(page)}; |
86493009 | 3872 | mem_cgroup_uncharge_swap(ent); |
8c7c6e34 | 3873 | } |
7a81b88c KH |
3874 | } |
3875 | ||
72835c86 JW |
3876 | void mem_cgroup_commit_charge_swapin(struct page *page, |
3877 | struct mem_cgroup *memcg) | |
83aae4c7 | 3878 | { |
72835c86 | 3879 | __mem_cgroup_commit_charge_swapin(page, memcg, |
41326c17 | 3880 | MEM_CGROUP_CHARGE_TYPE_ANON); |
83aae4c7 DN |
3881 | } |
3882 | ||
827a03d2 JW |
3883 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
3884 | gfp_t gfp_mask) | |
7a81b88c | 3885 | { |
827a03d2 JW |
3886 | struct mem_cgroup *memcg = NULL; |
3887 | enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE; | |
3888 | int ret; | |
3889 | ||
f8d66542 | 3890 | if (mem_cgroup_disabled()) |
827a03d2 JW |
3891 | return 0; |
3892 | if (PageCompound(page)) | |
3893 | return 0; | |
3894 | ||
827a03d2 JW |
3895 | if (!PageSwapCache(page)) |
3896 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, type); | |
3897 | else { /* page is swapcache/shmem */ | |
0435a2fd JW |
3898 | ret = __mem_cgroup_try_charge_swapin(mm, page, |
3899 | gfp_mask, &memcg); | |
827a03d2 JW |
3900 | if (!ret) |
3901 | __mem_cgroup_commit_charge_swapin(page, memcg, type); | |
3902 | } | |
3903 | return ret; | |
7a81b88c KH |
3904 | } |
3905 | ||
c0ff4b85 | 3906 | static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg, |
7ec99d62 JW |
3907 | unsigned int nr_pages, |
3908 | const enum charge_type ctype) | |
569b846d KH |
3909 | { |
3910 | struct memcg_batch_info *batch = NULL; | |
3911 | bool uncharge_memsw = true; | |
7ec99d62 | 3912 | |
569b846d KH |
3913 | /* If swapout, usage of swap doesn't decrease */ |
3914 | if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) | |
3915 | uncharge_memsw = false; | |
569b846d KH |
3916 | |
3917 | batch = ¤t->memcg_batch; | |
3918 | /* | |
3919 | * In usual, we do css_get() when we remember memcg pointer. | |
3920 | * But in this case, we keep res->usage until end of a series of | |
3921 | * uncharges. Then, it's ok to ignore memcg's refcnt. | |
3922 | */ | |
3923 | if (!batch->memcg) | |
c0ff4b85 | 3924 | batch->memcg = memcg; |
3c11ecf4 KH |
3925 | /* |
3926 | * do_batch > 0 when unmapping pages or inode invalidate/truncate. | |
25985edc | 3927 | * In those cases, all pages freed continuously can be expected to be in |
3c11ecf4 KH |
3928 | * the same cgroup and we have chance to coalesce uncharges. |
3929 | * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE) | |
3930 | * because we want to do uncharge as soon as possible. | |
3931 | */ | |
3932 | ||
3933 | if (!batch->do_batch || test_thread_flag(TIF_MEMDIE)) | |
3934 | goto direct_uncharge; | |
3935 | ||
7ec99d62 | 3936 | if (nr_pages > 1) |
ec168510 AA |
3937 | goto direct_uncharge; |
3938 | ||
569b846d KH |
3939 | /* |
3940 | * In typical case, batch->memcg == mem. This means we can | |
3941 | * merge a series of uncharges to an uncharge of res_counter. | |
3942 | * If not, we uncharge res_counter ony by one. | |
3943 | */ | |
c0ff4b85 | 3944 | if (batch->memcg != memcg) |
569b846d KH |
3945 | goto direct_uncharge; |
3946 | /* remember freed charge and uncharge it later */ | |
7ffd4ca7 | 3947 | batch->nr_pages++; |
569b846d | 3948 | if (uncharge_memsw) |
7ffd4ca7 | 3949 | batch->memsw_nr_pages++; |
569b846d KH |
3950 | return; |
3951 | direct_uncharge: | |
c0ff4b85 | 3952 | res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE); |
569b846d | 3953 | if (uncharge_memsw) |
c0ff4b85 R |
3954 | res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE); |
3955 | if (unlikely(batch->memcg != memcg)) | |
3956 | memcg_oom_recover(memcg); | |
569b846d | 3957 | } |
7a81b88c | 3958 | |
8a9f3ccd | 3959 | /* |
69029cd5 | 3960 | * uncharge if !page_mapped(page) |
8a9f3ccd | 3961 | */ |
8c7c6e34 | 3962 | static struct mem_cgroup * |
0030f535 JW |
3963 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype, |
3964 | bool end_migration) | |
8a9f3ccd | 3965 | { |
c0ff4b85 | 3966 | struct mem_cgroup *memcg = NULL; |
7ec99d62 JW |
3967 | unsigned int nr_pages = 1; |
3968 | struct page_cgroup *pc; | |
b2402857 | 3969 | bool anon; |
8a9f3ccd | 3970 | |
f8d66542 | 3971 | if (mem_cgroup_disabled()) |
8c7c6e34 | 3972 | return NULL; |
4077960e | 3973 | |
0c59b89c | 3974 | VM_BUG_ON(PageSwapCache(page)); |
d13d1443 | 3975 | |
37c2ac78 | 3976 | if (PageTransHuge(page)) { |
7ec99d62 | 3977 | nr_pages <<= compound_order(page); |
37c2ac78 AA |
3978 | VM_BUG_ON(!PageTransHuge(page)); |
3979 | } | |
8697d331 | 3980 | /* |
3c541e14 | 3981 | * Check if our page_cgroup is valid |
8697d331 | 3982 | */ |
52d4b9ac | 3983 | pc = lookup_page_cgroup(page); |
cfa44946 | 3984 | if (unlikely(!PageCgroupUsed(pc))) |
8c7c6e34 | 3985 | return NULL; |
b9c565d5 | 3986 | |
52d4b9ac | 3987 | lock_page_cgroup(pc); |
d13d1443 | 3988 | |
c0ff4b85 | 3989 | memcg = pc->mem_cgroup; |
8c7c6e34 | 3990 | |
d13d1443 KH |
3991 | if (!PageCgroupUsed(pc)) |
3992 | goto unlock_out; | |
3993 | ||
b2402857 KH |
3994 | anon = PageAnon(page); |
3995 | ||
d13d1443 | 3996 | switch (ctype) { |
41326c17 | 3997 | case MEM_CGROUP_CHARGE_TYPE_ANON: |
2ff76f11 KH |
3998 | /* |
3999 | * Generally PageAnon tells if it's the anon statistics to be | |
4000 | * updated; but sometimes e.g. mem_cgroup_uncharge_page() is | |
4001 | * used before page reached the stage of being marked PageAnon. | |
4002 | */ | |
b2402857 KH |
4003 | anon = true; |
4004 | /* fallthrough */ | |
8a9478ca | 4005 | case MEM_CGROUP_CHARGE_TYPE_DROP: |
ac39cf8c | 4006 | /* See mem_cgroup_prepare_migration() */ |
0030f535 JW |
4007 | if (page_mapped(page)) |
4008 | goto unlock_out; | |
4009 | /* | |
4010 | * Pages under migration may not be uncharged. But | |
4011 | * end_migration() /must/ be the one uncharging the | |
4012 | * unused post-migration page and so it has to call | |
4013 | * here with the migration bit still set. See the | |
4014 | * res_counter handling below. | |
4015 | */ | |
4016 | if (!end_migration && PageCgroupMigration(pc)) | |
d13d1443 KH |
4017 | goto unlock_out; |
4018 | break; | |
4019 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: | |
4020 | if (!PageAnon(page)) { /* Shared memory */ | |
4021 | if (page->mapping && !page_is_file_cache(page)) | |
4022 | goto unlock_out; | |
4023 | } else if (page_mapped(page)) /* Anon */ | |
4024 | goto unlock_out; | |
4025 | break; | |
4026 | default: | |
4027 | break; | |
52d4b9ac | 4028 | } |
d13d1443 | 4029 | |
b2402857 | 4030 | mem_cgroup_charge_statistics(memcg, anon, -nr_pages); |
04046e1a | 4031 | |
52d4b9ac | 4032 | ClearPageCgroupUsed(pc); |
544122e5 KH |
4033 | /* |
4034 | * pc->mem_cgroup is not cleared here. It will be accessed when it's | |
4035 | * freed from LRU. This is safe because uncharged page is expected not | |
4036 | * to be reused (freed soon). Exception is SwapCache, it's handled by | |
4037 | * special functions. | |
4038 | */ | |
b9c565d5 | 4039 | |
52d4b9ac | 4040 | unlock_page_cgroup(pc); |
f75ca962 | 4041 | /* |
c0ff4b85 | 4042 | * even after unlock, we have memcg->res.usage here and this memcg |
f75ca962 KH |
4043 | * will never be freed. |
4044 | */ | |
c0ff4b85 | 4045 | memcg_check_events(memcg, page); |
f75ca962 | 4046 | if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) { |
c0ff4b85 R |
4047 | mem_cgroup_swap_statistics(memcg, true); |
4048 | mem_cgroup_get(memcg); | |
f75ca962 | 4049 | } |
0030f535 JW |
4050 | /* |
4051 | * Migration does not charge the res_counter for the | |
4052 | * replacement page, so leave it alone when phasing out the | |
4053 | * page that is unused after the migration. | |
4054 | */ | |
4055 | if (!end_migration && !mem_cgroup_is_root(memcg)) | |
c0ff4b85 | 4056 | mem_cgroup_do_uncharge(memcg, nr_pages, ctype); |
6d12e2d8 | 4057 | |
c0ff4b85 | 4058 | return memcg; |
d13d1443 KH |
4059 | |
4060 | unlock_out: | |
4061 | unlock_page_cgroup(pc); | |
8c7c6e34 | 4062 | return NULL; |
3c541e14 BS |
4063 | } |
4064 | ||
69029cd5 KH |
4065 | void mem_cgroup_uncharge_page(struct page *page) |
4066 | { | |
52d4b9ac KH |
4067 | /* early check. */ |
4068 | if (page_mapped(page)) | |
4069 | return; | |
40f23a21 | 4070 | VM_BUG_ON(page->mapping && !PageAnon(page)); |
0c59b89c JW |
4071 | if (PageSwapCache(page)) |
4072 | return; | |
0030f535 | 4073 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false); |
69029cd5 KH |
4074 | } |
4075 | ||
4076 | void mem_cgroup_uncharge_cache_page(struct page *page) | |
4077 | { | |
4078 | VM_BUG_ON(page_mapped(page)); | |
b7abea96 | 4079 | VM_BUG_ON(page->mapping); |
0030f535 | 4080 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false); |
69029cd5 KH |
4081 | } |
4082 | ||
569b846d KH |
4083 | /* |
4084 | * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate. | |
4085 | * In that cases, pages are freed continuously and we can expect pages | |
4086 | * are in the same memcg. All these calls itself limits the number of | |
4087 | * pages freed at once, then uncharge_start/end() is called properly. | |
4088 | * This may be called prural(2) times in a context, | |
4089 | */ | |
4090 | ||
4091 | void mem_cgroup_uncharge_start(void) | |
4092 | { | |
4093 | current->memcg_batch.do_batch++; | |
4094 | /* We can do nest. */ | |
4095 | if (current->memcg_batch.do_batch == 1) { | |
4096 | current->memcg_batch.memcg = NULL; | |
7ffd4ca7 JW |
4097 | current->memcg_batch.nr_pages = 0; |
4098 | current->memcg_batch.memsw_nr_pages = 0; | |
569b846d KH |
4099 | } |
4100 | } | |
4101 | ||
4102 | void mem_cgroup_uncharge_end(void) | |
4103 | { | |
4104 | struct memcg_batch_info *batch = ¤t->memcg_batch; | |
4105 | ||
4106 | if (!batch->do_batch) | |
4107 | return; | |
4108 | ||
4109 | batch->do_batch--; | |
4110 | if (batch->do_batch) /* If stacked, do nothing. */ | |
4111 | return; | |
4112 | ||
4113 | if (!batch->memcg) | |
4114 | return; | |
4115 | /* | |
4116 | * This "batch->memcg" is valid without any css_get/put etc... | |
4117 | * bacause we hide charges behind us. | |
4118 | */ | |
7ffd4ca7 JW |
4119 | if (batch->nr_pages) |
4120 | res_counter_uncharge(&batch->memcg->res, | |
4121 | batch->nr_pages * PAGE_SIZE); | |
4122 | if (batch->memsw_nr_pages) | |
4123 | res_counter_uncharge(&batch->memcg->memsw, | |
4124 | batch->memsw_nr_pages * PAGE_SIZE); | |
3c11ecf4 | 4125 | memcg_oom_recover(batch->memcg); |
569b846d KH |
4126 | /* forget this pointer (for sanity check) */ |
4127 | batch->memcg = NULL; | |
4128 | } | |
4129 | ||
e767e056 | 4130 | #ifdef CONFIG_SWAP |
8c7c6e34 | 4131 | /* |
e767e056 | 4132 | * called after __delete_from_swap_cache() and drop "page" account. |
8c7c6e34 KH |
4133 | * memcg information is recorded to swap_cgroup of "ent" |
4134 | */ | |
8a9478ca KH |
4135 | void |
4136 | mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout) | |
8c7c6e34 KH |
4137 | { |
4138 | struct mem_cgroup *memcg; | |
8a9478ca KH |
4139 | int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT; |
4140 | ||
4141 | if (!swapout) /* this was a swap cache but the swap is unused ! */ | |
4142 | ctype = MEM_CGROUP_CHARGE_TYPE_DROP; | |
4143 | ||
0030f535 | 4144 | memcg = __mem_cgroup_uncharge_common(page, ctype, false); |
8c7c6e34 | 4145 | |
f75ca962 KH |
4146 | /* |
4147 | * record memcg information, if swapout && memcg != NULL, | |
4148 | * mem_cgroup_get() was called in uncharge(). | |
4149 | */ | |
4150 | if (do_swap_account && swapout && memcg) | |
a3b2d692 | 4151 | swap_cgroup_record(ent, css_id(&memcg->css)); |
8c7c6e34 | 4152 | } |
e767e056 | 4153 | #endif |
8c7c6e34 | 4154 | |
c255a458 | 4155 | #ifdef CONFIG_MEMCG_SWAP |
8c7c6e34 KH |
4156 | /* |
4157 | * called from swap_entry_free(). remove record in swap_cgroup and | |
4158 | * uncharge "memsw" account. | |
4159 | */ | |
4160 | void mem_cgroup_uncharge_swap(swp_entry_t ent) | |
d13d1443 | 4161 | { |
8c7c6e34 | 4162 | struct mem_cgroup *memcg; |
a3b2d692 | 4163 | unsigned short id; |
8c7c6e34 KH |
4164 | |
4165 | if (!do_swap_account) | |
4166 | return; | |
4167 | ||
a3b2d692 KH |
4168 | id = swap_cgroup_record(ent, 0); |
4169 | rcu_read_lock(); | |
4170 | memcg = mem_cgroup_lookup(id); | |
8c7c6e34 | 4171 | if (memcg) { |
a3b2d692 KH |
4172 | /* |
4173 | * We uncharge this because swap is freed. | |
4174 | * This memcg can be obsolete one. We avoid calling css_tryget | |
4175 | */ | |
0c3e73e8 | 4176 | if (!mem_cgroup_is_root(memcg)) |
4e649152 | 4177 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
0c3e73e8 | 4178 | mem_cgroup_swap_statistics(memcg, false); |
8c7c6e34 KH |
4179 | mem_cgroup_put(memcg); |
4180 | } | |
a3b2d692 | 4181 | rcu_read_unlock(); |
d13d1443 | 4182 | } |
02491447 DN |
4183 | |
4184 | /** | |
4185 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. | |
4186 | * @entry: swap entry to be moved | |
4187 | * @from: mem_cgroup which the entry is moved from | |
4188 | * @to: mem_cgroup which the entry is moved to | |
4189 | * | |
4190 | * It succeeds only when the swap_cgroup's record for this entry is the same | |
4191 | * as the mem_cgroup's id of @from. | |
4192 | * | |
4193 | * Returns 0 on success, -EINVAL on failure. | |
4194 | * | |
4195 | * The caller must have charged to @to, IOW, called res_counter_charge() about | |
4196 | * both res and memsw, and called css_get(). | |
4197 | */ | |
4198 | static int mem_cgroup_move_swap_account(swp_entry_t entry, | |
e91cbb42 | 4199 | struct mem_cgroup *from, struct mem_cgroup *to) |
02491447 DN |
4200 | { |
4201 | unsigned short old_id, new_id; | |
4202 | ||
4203 | old_id = css_id(&from->css); | |
4204 | new_id = css_id(&to->css); | |
4205 | ||
4206 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { | |
02491447 | 4207 | mem_cgroup_swap_statistics(from, false); |
483c30b5 | 4208 | mem_cgroup_swap_statistics(to, true); |
02491447 | 4209 | /* |
483c30b5 DN |
4210 | * This function is only called from task migration context now. |
4211 | * It postpones res_counter and refcount handling till the end | |
4212 | * of task migration(mem_cgroup_clear_mc()) for performance | |
4213 | * improvement. But we cannot postpone mem_cgroup_get(to) | |
4214 | * because if the process that has been moved to @to does | |
4215 | * swap-in, the refcount of @to might be decreased to 0. | |
02491447 | 4216 | */ |
02491447 | 4217 | mem_cgroup_get(to); |
02491447 DN |
4218 | return 0; |
4219 | } | |
4220 | return -EINVAL; | |
4221 | } | |
4222 | #else | |
4223 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, | |
e91cbb42 | 4224 | struct mem_cgroup *from, struct mem_cgroup *to) |
02491447 DN |
4225 | { |
4226 | return -EINVAL; | |
4227 | } | |
8c7c6e34 | 4228 | #endif |
d13d1443 | 4229 | |
ae41be37 | 4230 | /* |
01b1ae63 KH |
4231 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
4232 | * page belongs to. | |
ae41be37 | 4233 | */ |
0030f535 JW |
4234 | void mem_cgroup_prepare_migration(struct page *page, struct page *newpage, |
4235 | struct mem_cgroup **memcgp) | |
ae41be37 | 4236 | { |
c0ff4b85 | 4237 | struct mem_cgroup *memcg = NULL; |
b32967ff | 4238 | unsigned int nr_pages = 1; |
7ec99d62 | 4239 | struct page_cgroup *pc; |
ac39cf8c | 4240 | enum charge_type ctype; |
8869b8f6 | 4241 | |
72835c86 | 4242 | *memcgp = NULL; |
56039efa | 4243 | |
f8d66542 | 4244 | if (mem_cgroup_disabled()) |
0030f535 | 4245 | return; |
4077960e | 4246 | |
b32967ff MG |
4247 | if (PageTransHuge(page)) |
4248 | nr_pages <<= compound_order(page); | |
4249 | ||
52d4b9ac KH |
4250 | pc = lookup_page_cgroup(page); |
4251 | lock_page_cgroup(pc); | |
4252 | if (PageCgroupUsed(pc)) { | |
c0ff4b85 R |
4253 | memcg = pc->mem_cgroup; |
4254 | css_get(&memcg->css); | |
ac39cf8c | 4255 | /* |
4256 | * At migrating an anonymous page, its mapcount goes down | |
4257 | * to 0 and uncharge() will be called. But, even if it's fully | |
4258 | * unmapped, migration may fail and this page has to be | |
4259 | * charged again. We set MIGRATION flag here and delay uncharge | |
4260 | * until end_migration() is called | |
4261 | * | |
4262 | * Corner Case Thinking | |
4263 | * A) | |
4264 | * When the old page was mapped as Anon and it's unmap-and-freed | |
4265 | * while migration was ongoing. | |
4266 | * If unmap finds the old page, uncharge() of it will be delayed | |
4267 | * until end_migration(). If unmap finds a new page, it's | |
4268 | * uncharged when it make mapcount to be 1->0. If unmap code | |
4269 | * finds swap_migration_entry, the new page will not be mapped | |
4270 | * and end_migration() will find it(mapcount==0). | |
4271 | * | |
4272 | * B) | |
4273 | * When the old page was mapped but migraion fails, the kernel | |
4274 | * remaps it. A charge for it is kept by MIGRATION flag even | |
4275 | * if mapcount goes down to 0. We can do remap successfully | |
4276 | * without charging it again. | |
4277 | * | |
4278 | * C) | |
4279 | * The "old" page is under lock_page() until the end of | |
4280 | * migration, so, the old page itself will not be swapped-out. | |
4281 | * If the new page is swapped out before end_migraton, our | |
4282 | * hook to usual swap-out path will catch the event. | |
4283 | */ | |
4284 | if (PageAnon(page)) | |
4285 | SetPageCgroupMigration(pc); | |
e8589cc1 | 4286 | } |
52d4b9ac | 4287 | unlock_page_cgroup(pc); |
ac39cf8c | 4288 | /* |
4289 | * If the page is not charged at this point, | |
4290 | * we return here. | |
4291 | */ | |
c0ff4b85 | 4292 | if (!memcg) |
0030f535 | 4293 | return; |
01b1ae63 | 4294 | |
72835c86 | 4295 | *memcgp = memcg; |
ac39cf8c | 4296 | /* |
4297 | * We charge new page before it's used/mapped. So, even if unlock_page() | |
4298 | * is called before end_migration, we can catch all events on this new | |
4299 | * page. In the case new page is migrated but not remapped, new page's | |
4300 | * mapcount will be finally 0 and we call uncharge in end_migration(). | |
4301 | */ | |
ac39cf8c | 4302 | if (PageAnon(page)) |
41326c17 | 4303 | ctype = MEM_CGROUP_CHARGE_TYPE_ANON; |
ac39cf8c | 4304 | else |
62ba7442 | 4305 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
0030f535 JW |
4306 | /* |
4307 | * The page is committed to the memcg, but it's not actually | |
4308 | * charged to the res_counter since we plan on replacing the | |
4309 | * old one and only one page is going to be left afterwards. | |
4310 | */ | |
b32967ff | 4311 | __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false); |
ae41be37 | 4312 | } |
8869b8f6 | 4313 | |
69029cd5 | 4314 | /* remove redundant charge if migration failed*/ |
c0ff4b85 | 4315 | void mem_cgroup_end_migration(struct mem_cgroup *memcg, |
50de1dd9 | 4316 | struct page *oldpage, struct page *newpage, bool migration_ok) |
ae41be37 | 4317 | { |
ac39cf8c | 4318 | struct page *used, *unused; |
01b1ae63 | 4319 | struct page_cgroup *pc; |
b2402857 | 4320 | bool anon; |
01b1ae63 | 4321 | |
c0ff4b85 | 4322 | if (!memcg) |
01b1ae63 | 4323 | return; |
b25ed609 | 4324 | |
50de1dd9 | 4325 | if (!migration_ok) { |
ac39cf8c | 4326 | used = oldpage; |
4327 | unused = newpage; | |
01b1ae63 | 4328 | } else { |
ac39cf8c | 4329 | used = newpage; |
01b1ae63 KH |
4330 | unused = oldpage; |
4331 | } | |
0030f535 | 4332 | anon = PageAnon(used); |
7d188958 JW |
4333 | __mem_cgroup_uncharge_common(unused, |
4334 | anon ? MEM_CGROUP_CHARGE_TYPE_ANON | |
4335 | : MEM_CGROUP_CHARGE_TYPE_CACHE, | |
4336 | true); | |
0030f535 | 4337 | css_put(&memcg->css); |
69029cd5 | 4338 | /* |
ac39cf8c | 4339 | * We disallowed uncharge of pages under migration because mapcount |
4340 | * of the page goes down to zero, temporarly. | |
4341 | * Clear the flag and check the page should be charged. | |
01b1ae63 | 4342 | */ |
ac39cf8c | 4343 | pc = lookup_page_cgroup(oldpage); |
4344 | lock_page_cgroup(pc); | |
4345 | ClearPageCgroupMigration(pc); | |
4346 | unlock_page_cgroup(pc); | |
ac39cf8c | 4347 | |
01b1ae63 | 4348 | /* |
ac39cf8c | 4349 | * If a page is a file cache, radix-tree replacement is very atomic |
4350 | * and we can skip this check. When it was an Anon page, its mapcount | |
4351 | * goes down to 0. But because we added MIGRATION flage, it's not | |
4352 | * uncharged yet. There are several case but page->mapcount check | |
4353 | * and USED bit check in mem_cgroup_uncharge_page() will do enough | |
4354 | * check. (see prepare_charge() also) | |
69029cd5 | 4355 | */ |
b2402857 | 4356 | if (anon) |
ac39cf8c | 4357 | mem_cgroup_uncharge_page(used); |
ae41be37 | 4358 | } |
78fb7466 | 4359 | |
ab936cbc KH |
4360 | /* |
4361 | * At replace page cache, newpage is not under any memcg but it's on | |
4362 | * LRU. So, this function doesn't touch res_counter but handles LRU | |
4363 | * in correct way. Both pages are locked so we cannot race with uncharge. | |
4364 | */ | |
4365 | void mem_cgroup_replace_page_cache(struct page *oldpage, | |
4366 | struct page *newpage) | |
4367 | { | |
bde05d1c | 4368 | struct mem_cgroup *memcg = NULL; |
ab936cbc | 4369 | struct page_cgroup *pc; |
ab936cbc | 4370 | enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE; |
ab936cbc KH |
4371 | |
4372 | if (mem_cgroup_disabled()) | |
4373 | return; | |
4374 | ||
4375 | pc = lookup_page_cgroup(oldpage); | |
4376 | /* fix accounting on old pages */ | |
4377 | lock_page_cgroup(pc); | |
bde05d1c HD |
4378 | if (PageCgroupUsed(pc)) { |
4379 | memcg = pc->mem_cgroup; | |
4380 | mem_cgroup_charge_statistics(memcg, false, -1); | |
4381 | ClearPageCgroupUsed(pc); | |
4382 | } | |
ab936cbc KH |
4383 | unlock_page_cgroup(pc); |
4384 | ||
bde05d1c HD |
4385 | /* |
4386 | * When called from shmem_replace_page(), in some cases the | |
4387 | * oldpage has already been charged, and in some cases not. | |
4388 | */ | |
4389 | if (!memcg) | |
4390 | return; | |
ab936cbc KH |
4391 | /* |
4392 | * Even if newpage->mapping was NULL before starting replacement, | |
4393 | * the newpage may be on LRU(or pagevec for LRU) already. We lock | |
4394 | * LRU while we overwrite pc->mem_cgroup. | |
4395 | */ | |
ce587e65 | 4396 | __mem_cgroup_commit_charge(memcg, newpage, 1, type, true); |
ab936cbc KH |
4397 | } |
4398 | ||
f212ad7c DN |
4399 | #ifdef CONFIG_DEBUG_VM |
4400 | static struct page_cgroup *lookup_page_cgroup_used(struct page *page) | |
4401 | { | |
4402 | struct page_cgroup *pc; | |
4403 | ||
4404 | pc = lookup_page_cgroup(page); | |
cfa44946 JW |
4405 | /* |
4406 | * Can be NULL while feeding pages into the page allocator for | |
4407 | * the first time, i.e. during boot or memory hotplug; | |
4408 | * or when mem_cgroup_disabled(). | |
4409 | */ | |
f212ad7c DN |
4410 | if (likely(pc) && PageCgroupUsed(pc)) |
4411 | return pc; | |
4412 | return NULL; | |
4413 | } | |
4414 | ||
4415 | bool mem_cgroup_bad_page_check(struct page *page) | |
4416 | { | |
4417 | if (mem_cgroup_disabled()) | |
4418 | return false; | |
4419 | ||
4420 | return lookup_page_cgroup_used(page) != NULL; | |
4421 | } | |
4422 | ||
4423 | void mem_cgroup_print_bad_page(struct page *page) | |
4424 | { | |
4425 | struct page_cgroup *pc; | |
4426 | ||
4427 | pc = lookup_page_cgroup_used(page); | |
4428 | if (pc) { | |
90b3feae | 4429 | printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p\n", |
f212ad7c | 4430 | pc, pc->flags, pc->mem_cgroup); |
f212ad7c DN |
4431 | } |
4432 | } | |
4433 | #endif | |
4434 | ||
d38d2a75 | 4435 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
8c7c6e34 | 4436 | unsigned long long val) |
628f4235 | 4437 | { |
81d39c20 | 4438 | int retry_count; |
3c11ecf4 | 4439 | u64 memswlimit, memlimit; |
628f4235 | 4440 | int ret = 0; |
81d39c20 KH |
4441 | int children = mem_cgroup_count_children(memcg); |
4442 | u64 curusage, oldusage; | |
3c11ecf4 | 4443 | int enlarge; |
81d39c20 KH |
4444 | |
4445 | /* | |
4446 | * For keeping hierarchical_reclaim simple, how long we should retry | |
4447 | * is depends on callers. We set our retry-count to be function | |
4448 | * of # of children which we should visit in this loop. | |
4449 | */ | |
4450 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; | |
4451 | ||
4452 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); | |
628f4235 | 4453 | |
3c11ecf4 | 4454 | enlarge = 0; |
8c7c6e34 | 4455 | while (retry_count) { |
628f4235 KH |
4456 | if (signal_pending(current)) { |
4457 | ret = -EINTR; | |
4458 | break; | |
4459 | } | |
8c7c6e34 KH |
4460 | /* |
4461 | * Rather than hide all in some function, I do this in | |
4462 | * open coded manner. You see what this really does. | |
aaad153e | 4463 | * We have to guarantee memcg->res.limit <= memcg->memsw.limit. |
8c7c6e34 KH |
4464 | */ |
4465 | mutex_lock(&set_limit_mutex); | |
4466 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
4467 | if (memswlimit < val) { | |
4468 | ret = -EINVAL; | |
4469 | mutex_unlock(&set_limit_mutex); | |
628f4235 KH |
4470 | break; |
4471 | } | |
3c11ecf4 KH |
4472 | |
4473 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
4474 | if (memlimit < val) | |
4475 | enlarge = 1; | |
4476 | ||
8c7c6e34 | 4477 | ret = res_counter_set_limit(&memcg->res, val); |
22a668d7 KH |
4478 | if (!ret) { |
4479 | if (memswlimit == val) | |
4480 | memcg->memsw_is_minimum = true; | |
4481 | else | |
4482 | memcg->memsw_is_minimum = false; | |
4483 | } | |
8c7c6e34 KH |
4484 | mutex_unlock(&set_limit_mutex); |
4485 | ||
4486 | if (!ret) | |
4487 | break; | |
4488 | ||
5660048c JW |
4489 | mem_cgroup_reclaim(memcg, GFP_KERNEL, |
4490 | MEM_CGROUP_RECLAIM_SHRINK); | |
81d39c20 KH |
4491 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
4492 | /* Usage is reduced ? */ | |
4493 | if (curusage >= oldusage) | |
4494 | retry_count--; | |
4495 | else | |
4496 | oldusage = curusage; | |
8c7c6e34 | 4497 | } |
3c11ecf4 KH |
4498 | if (!ret && enlarge) |
4499 | memcg_oom_recover(memcg); | |
14797e23 | 4500 | |
8c7c6e34 KH |
4501 | return ret; |
4502 | } | |
4503 | ||
338c8431 LZ |
4504 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
4505 | unsigned long long val) | |
8c7c6e34 | 4506 | { |
81d39c20 | 4507 | int retry_count; |
3c11ecf4 | 4508 | u64 memlimit, memswlimit, oldusage, curusage; |
81d39c20 KH |
4509 | int children = mem_cgroup_count_children(memcg); |
4510 | int ret = -EBUSY; | |
3c11ecf4 | 4511 | int enlarge = 0; |
8c7c6e34 | 4512 | |
81d39c20 KH |
4513 | /* see mem_cgroup_resize_res_limit */ |
4514 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; | |
4515 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); | |
8c7c6e34 KH |
4516 | while (retry_count) { |
4517 | if (signal_pending(current)) { | |
4518 | ret = -EINTR; | |
4519 | break; | |
4520 | } | |
4521 | /* | |
4522 | * Rather than hide all in some function, I do this in | |
4523 | * open coded manner. You see what this really does. | |
aaad153e | 4524 | * We have to guarantee memcg->res.limit <= memcg->memsw.limit. |
8c7c6e34 KH |
4525 | */ |
4526 | mutex_lock(&set_limit_mutex); | |
4527 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
4528 | if (memlimit > val) { | |
4529 | ret = -EINVAL; | |
4530 | mutex_unlock(&set_limit_mutex); | |
4531 | break; | |
4532 | } | |
3c11ecf4 KH |
4533 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
4534 | if (memswlimit < val) | |
4535 | enlarge = 1; | |
8c7c6e34 | 4536 | ret = res_counter_set_limit(&memcg->memsw, val); |
22a668d7 KH |
4537 | if (!ret) { |
4538 | if (memlimit == val) | |
4539 | memcg->memsw_is_minimum = true; | |
4540 | else | |
4541 | memcg->memsw_is_minimum = false; | |
4542 | } | |
8c7c6e34 KH |
4543 | mutex_unlock(&set_limit_mutex); |
4544 | ||
4545 | if (!ret) | |
4546 | break; | |
4547 | ||
5660048c JW |
4548 | mem_cgroup_reclaim(memcg, GFP_KERNEL, |
4549 | MEM_CGROUP_RECLAIM_NOSWAP | | |
4550 | MEM_CGROUP_RECLAIM_SHRINK); | |
8c7c6e34 | 4551 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
81d39c20 | 4552 | /* Usage is reduced ? */ |
8c7c6e34 | 4553 | if (curusage >= oldusage) |
628f4235 | 4554 | retry_count--; |
81d39c20 KH |
4555 | else |
4556 | oldusage = curusage; | |
628f4235 | 4557 | } |
3c11ecf4 KH |
4558 | if (!ret && enlarge) |
4559 | memcg_oom_recover(memcg); | |
628f4235 KH |
4560 | return ret; |
4561 | } | |
4562 | ||
4e416953 | 4563 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
0ae5e89c YH |
4564 | gfp_t gfp_mask, |
4565 | unsigned long *total_scanned) | |
4e416953 BS |
4566 | { |
4567 | unsigned long nr_reclaimed = 0; | |
4568 | struct mem_cgroup_per_zone *mz, *next_mz = NULL; | |
4569 | unsigned long reclaimed; | |
4570 | int loop = 0; | |
4571 | struct mem_cgroup_tree_per_zone *mctz; | |
ef8745c1 | 4572 | unsigned long long excess; |
0ae5e89c | 4573 | unsigned long nr_scanned; |
4e416953 BS |
4574 | |
4575 | if (order > 0) | |
4576 | return 0; | |
4577 | ||
00918b6a | 4578 | mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone)); |
4e416953 BS |
4579 | /* |
4580 | * This loop can run a while, specially if mem_cgroup's continuously | |
4581 | * keep exceeding their soft limit and putting the system under | |
4582 | * pressure | |
4583 | */ | |
4584 | do { | |
4585 | if (next_mz) | |
4586 | mz = next_mz; | |
4587 | else | |
4588 | mz = mem_cgroup_largest_soft_limit_node(mctz); | |
4589 | if (!mz) | |
4590 | break; | |
4591 | ||
0ae5e89c | 4592 | nr_scanned = 0; |
d79154bb | 4593 | reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone, |
5660048c | 4594 | gfp_mask, &nr_scanned); |
4e416953 | 4595 | nr_reclaimed += reclaimed; |
0ae5e89c | 4596 | *total_scanned += nr_scanned; |
4e416953 BS |
4597 | spin_lock(&mctz->lock); |
4598 | ||
4599 | /* | |
4600 | * If we failed to reclaim anything from this memory cgroup | |
4601 | * it is time to move on to the next cgroup | |
4602 | */ | |
4603 | next_mz = NULL; | |
4604 | if (!reclaimed) { | |
4605 | do { | |
4606 | /* | |
4607 | * Loop until we find yet another one. | |
4608 | * | |
4609 | * By the time we get the soft_limit lock | |
4610 | * again, someone might have aded the | |
4611 | * group back on the RB tree. Iterate to | |
4612 | * make sure we get a different mem. | |
4613 | * mem_cgroup_largest_soft_limit_node returns | |
4614 | * NULL if no other cgroup is present on | |
4615 | * the tree | |
4616 | */ | |
4617 | next_mz = | |
4618 | __mem_cgroup_largest_soft_limit_node(mctz); | |
39cc98f1 | 4619 | if (next_mz == mz) |
d79154bb | 4620 | css_put(&next_mz->memcg->css); |
39cc98f1 | 4621 | else /* next_mz == NULL or other memcg */ |
4e416953 BS |
4622 | break; |
4623 | } while (1); | |
4624 | } | |
d79154bb HD |
4625 | __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz); |
4626 | excess = res_counter_soft_limit_excess(&mz->memcg->res); | |
4e416953 BS |
4627 | /* |
4628 | * One school of thought says that we should not add | |
4629 | * back the node to the tree if reclaim returns 0. | |
4630 | * But our reclaim could return 0, simply because due | |
4631 | * to priority we are exposing a smaller subset of | |
4632 | * memory to reclaim from. Consider this as a longer | |
4633 | * term TODO. | |
4634 | */ | |
ef8745c1 | 4635 | /* If excess == 0, no tree ops */ |
d79154bb | 4636 | __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess); |
4e416953 | 4637 | spin_unlock(&mctz->lock); |
d79154bb | 4638 | css_put(&mz->memcg->css); |
4e416953 BS |
4639 | loop++; |
4640 | /* | |
4641 | * Could not reclaim anything and there are no more | |
4642 | * mem cgroups to try or we seem to be looping without | |
4643 | * reclaiming anything. | |
4644 | */ | |
4645 | if (!nr_reclaimed && | |
4646 | (next_mz == NULL || | |
4647 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) | |
4648 | break; | |
4649 | } while (!nr_reclaimed); | |
4650 | if (next_mz) | |
d79154bb | 4651 | css_put(&next_mz->memcg->css); |
4e416953 BS |
4652 | return nr_reclaimed; |
4653 | } | |
4654 | ||
2ef37d3f MH |
4655 | /** |
4656 | * mem_cgroup_force_empty_list - clears LRU of a group | |
4657 | * @memcg: group to clear | |
4658 | * @node: NUMA node | |
4659 | * @zid: zone id | |
4660 | * @lru: lru to to clear | |
4661 | * | |
3c935d18 | 4662 | * Traverse a specified page_cgroup list and try to drop them all. This doesn't |
2ef37d3f MH |
4663 | * reclaim the pages page themselves - pages are moved to the parent (or root) |
4664 | * group. | |
cc847582 | 4665 | */ |
2ef37d3f | 4666 | static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg, |
08e552c6 | 4667 | int node, int zid, enum lru_list lru) |
cc847582 | 4668 | { |
bea8c150 | 4669 | struct lruvec *lruvec; |
2ef37d3f | 4670 | unsigned long flags; |
072c56c1 | 4671 | struct list_head *list; |
925b7673 JW |
4672 | struct page *busy; |
4673 | struct zone *zone; | |
072c56c1 | 4674 | |
08e552c6 | 4675 | zone = &NODE_DATA(node)->node_zones[zid]; |
bea8c150 HD |
4676 | lruvec = mem_cgroup_zone_lruvec(zone, memcg); |
4677 | list = &lruvec->lists[lru]; | |
cc847582 | 4678 | |
f817ed48 | 4679 | busy = NULL; |
2ef37d3f | 4680 | do { |
925b7673 | 4681 | struct page_cgroup *pc; |
5564e88b JW |
4682 | struct page *page; |
4683 | ||
08e552c6 | 4684 | spin_lock_irqsave(&zone->lru_lock, flags); |
f817ed48 | 4685 | if (list_empty(list)) { |
08e552c6 | 4686 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
52d4b9ac | 4687 | break; |
f817ed48 | 4688 | } |
925b7673 JW |
4689 | page = list_entry(list->prev, struct page, lru); |
4690 | if (busy == page) { | |
4691 | list_move(&page->lru, list); | |
648bcc77 | 4692 | busy = NULL; |
08e552c6 | 4693 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
f817ed48 KH |
4694 | continue; |
4695 | } | |
08e552c6 | 4696 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
f817ed48 | 4697 | |
925b7673 | 4698 | pc = lookup_page_cgroup(page); |
5564e88b | 4699 | |
3c935d18 | 4700 | if (mem_cgroup_move_parent(page, pc, memcg)) { |
f817ed48 | 4701 | /* found lock contention or "pc" is obsolete. */ |
925b7673 | 4702 | busy = page; |
f817ed48 KH |
4703 | cond_resched(); |
4704 | } else | |
4705 | busy = NULL; | |
2ef37d3f | 4706 | } while (!list_empty(list)); |
cc847582 KH |
4707 | } |
4708 | ||
4709 | /* | |
c26251f9 MH |
4710 | * make mem_cgroup's charge to be 0 if there is no task by moving |
4711 | * all the charges and pages to the parent. | |
cc847582 | 4712 | * This enables deleting this mem_cgroup. |
c26251f9 MH |
4713 | * |
4714 | * Caller is responsible for holding css reference on the memcg. | |
cc847582 | 4715 | */ |
ab5196c2 | 4716 | static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg) |
cc847582 | 4717 | { |
c26251f9 | 4718 | int node, zid; |
bea207c8 | 4719 | u64 usage; |
f817ed48 | 4720 | |
fce66477 | 4721 | do { |
52d4b9ac KH |
4722 | /* This is for making all *used* pages to be on LRU. */ |
4723 | lru_add_drain_all(); | |
c0ff4b85 | 4724 | drain_all_stock_sync(memcg); |
c0ff4b85 | 4725 | mem_cgroup_start_move(memcg); |
31aaea4a | 4726 | for_each_node_state(node, N_MEMORY) { |
2ef37d3f | 4727 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
f156ab93 HD |
4728 | enum lru_list lru; |
4729 | for_each_lru(lru) { | |
2ef37d3f | 4730 | mem_cgroup_force_empty_list(memcg, |
f156ab93 | 4731 | node, zid, lru); |
f817ed48 | 4732 | } |
1ecaab2b | 4733 | } |
f817ed48 | 4734 | } |
c0ff4b85 R |
4735 | mem_cgroup_end_move(memcg); |
4736 | memcg_oom_recover(memcg); | |
52d4b9ac | 4737 | cond_resched(); |
f817ed48 | 4738 | |
2ef37d3f | 4739 | /* |
bea207c8 GC |
4740 | * Kernel memory may not necessarily be trackable to a specific |
4741 | * process. So they are not migrated, and therefore we can't | |
4742 | * expect their value to drop to 0 here. | |
4743 | * Having res filled up with kmem only is enough. | |
4744 | * | |
2ef37d3f MH |
4745 | * This is a safety check because mem_cgroup_force_empty_list |
4746 | * could have raced with mem_cgroup_replace_page_cache callers | |
4747 | * so the lru seemed empty but the page could have been added | |
4748 | * right after the check. RES_USAGE should be safe as we always | |
4749 | * charge before adding to the LRU. | |
4750 | */ | |
bea207c8 GC |
4751 | usage = res_counter_read_u64(&memcg->res, RES_USAGE) - |
4752 | res_counter_read_u64(&memcg->kmem, RES_USAGE); | |
4753 | } while (usage > 0); | |
c26251f9 MH |
4754 | } |
4755 | ||
4756 | /* | |
4757 | * Reclaims as many pages from the given memcg as possible and moves | |
4758 | * the rest to the parent. | |
4759 | * | |
4760 | * Caller is responsible for holding css reference for memcg. | |
4761 | */ | |
4762 | static int mem_cgroup_force_empty(struct mem_cgroup *memcg) | |
4763 | { | |
4764 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; | |
4765 | struct cgroup *cgrp = memcg->css.cgroup; | |
f817ed48 | 4766 | |
c1e862c1 | 4767 | /* returns EBUSY if there is a task or if we come here twice. */ |
c26251f9 MH |
4768 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
4769 | return -EBUSY; | |
4770 | ||
c1e862c1 KH |
4771 | /* we call try-to-free pages for make this cgroup empty */ |
4772 | lru_add_drain_all(); | |
f817ed48 | 4773 | /* try to free all pages in this cgroup */ |
569530fb | 4774 | while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) { |
f817ed48 | 4775 | int progress; |
c1e862c1 | 4776 | |
c26251f9 MH |
4777 | if (signal_pending(current)) |
4778 | return -EINTR; | |
4779 | ||
c0ff4b85 | 4780 | progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL, |
185efc0f | 4781 | false); |
c1e862c1 | 4782 | if (!progress) { |
f817ed48 | 4783 | nr_retries--; |
c1e862c1 | 4784 | /* maybe some writeback is necessary */ |
8aa7e847 | 4785 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
c1e862c1 | 4786 | } |
f817ed48 KH |
4787 | |
4788 | } | |
08e552c6 | 4789 | lru_add_drain(); |
ab5196c2 MH |
4790 | mem_cgroup_reparent_charges(memcg); |
4791 | ||
4792 | return 0; | |
cc847582 KH |
4793 | } |
4794 | ||
6bbda35c | 4795 | static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
c1e862c1 | 4796 | { |
c26251f9 MH |
4797 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
4798 | int ret; | |
4799 | ||
d8423011 MH |
4800 | if (mem_cgroup_is_root(memcg)) |
4801 | return -EINVAL; | |
c26251f9 MH |
4802 | css_get(&memcg->css); |
4803 | ret = mem_cgroup_force_empty(memcg); | |
4804 | css_put(&memcg->css); | |
4805 | ||
4806 | return ret; | |
c1e862c1 KH |
4807 | } |
4808 | ||
4809 | ||
18f59ea7 BS |
4810 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
4811 | { | |
4812 | return mem_cgroup_from_cont(cont)->use_hierarchy; | |
4813 | } | |
4814 | ||
4815 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, | |
4816 | u64 val) | |
4817 | { | |
4818 | int retval = 0; | |
c0ff4b85 | 4819 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
18f59ea7 | 4820 | struct cgroup *parent = cont->parent; |
c0ff4b85 | 4821 | struct mem_cgroup *parent_memcg = NULL; |
18f59ea7 BS |
4822 | |
4823 | if (parent) | |
c0ff4b85 | 4824 | parent_memcg = mem_cgroup_from_cont(parent); |
18f59ea7 BS |
4825 | |
4826 | cgroup_lock(); | |
567fb435 GC |
4827 | |
4828 | if (memcg->use_hierarchy == val) | |
4829 | goto out; | |
4830 | ||
18f59ea7 | 4831 | /* |
af901ca1 | 4832 | * If parent's use_hierarchy is set, we can't make any modifications |
18f59ea7 BS |
4833 | * in the child subtrees. If it is unset, then the change can |
4834 | * occur, provided the current cgroup has no children. | |
4835 | * | |
4836 | * For the root cgroup, parent_mem is NULL, we allow value to be | |
4837 | * set if there are no children. | |
4838 | */ | |
c0ff4b85 | 4839 | if ((!parent_memcg || !parent_memcg->use_hierarchy) && |
18f59ea7 BS |
4840 | (val == 1 || val == 0)) { |
4841 | if (list_empty(&cont->children)) | |
c0ff4b85 | 4842 | memcg->use_hierarchy = val; |
18f59ea7 BS |
4843 | else |
4844 | retval = -EBUSY; | |
4845 | } else | |
4846 | retval = -EINVAL; | |
567fb435 GC |
4847 | |
4848 | out: | |
18f59ea7 BS |
4849 | cgroup_unlock(); |
4850 | ||
4851 | return retval; | |
4852 | } | |
4853 | ||
0c3e73e8 | 4854 | |
c0ff4b85 | 4855 | static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg, |
7a159cc9 | 4856 | enum mem_cgroup_stat_index idx) |
0c3e73e8 | 4857 | { |
7d74b06f | 4858 | struct mem_cgroup *iter; |
7a159cc9 | 4859 | long val = 0; |
0c3e73e8 | 4860 | |
7a159cc9 | 4861 | /* Per-cpu values can be negative, use a signed accumulator */ |
c0ff4b85 | 4862 | for_each_mem_cgroup_tree(iter, memcg) |
7d74b06f KH |
4863 | val += mem_cgroup_read_stat(iter, idx); |
4864 | ||
4865 | if (val < 0) /* race ? */ | |
4866 | val = 0; | |
4867 | return val; | |
0c3e73e8 BS |
4868 | } |
4869 | ||
c0ff4b85 | 4870 | static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap) |
104f3928 | 4871 | { |
7d74b06f | 4872 | u64 val; |
104f3928 | 4873 | |
c0ff4b85 | 4874 | if (!mem_cgroup_is_root(memcg)) { |
104f3928 | 4875 | if (!swap) |
65c64ce8 | 4876 | return res_counter_read_u64(&memcg->res, RES_USAGE); |
104f3928 | 4877 | else |
65c64ce8 | 4878 | return res_counter_read_u64(&memcg->memsw, RES_USAGE); |
104f3928 KS |
4879 | } |
4880 | ||
c0ff4b85 R |
4881 | val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE); |
4882 | val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS); | |
104f3928 | 4883 | |
7d74b06f | 4884 | if (swap) |
bff6bb83 | 4885 | val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP); |
104f3928 KS |
4886 | |
4887 | return val << PAGE_SHIFT; | |
4888 | } | |
4889 | ||
af36f906 TH |
4890 | static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft, |
4891 | struct file *file, char __user *buf, | |
4892 | size_t nbytes, loff_t *ppos) | |
8cdea7c0 | 4893 | { |
c0ff4b85 | 4894 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
af36f906 | 4895 | char str[64]; |
104f3928 | 4896 | u64 val; |
86ae53e1 GC |
4897 | int name, len; |
4898 | enum res_type type; | |
8c7c6e34 KH |
4899 | |
4900 | type = MEMFILE_TYPE(cft->private); | |
4901 | name = MEMFILE_ATTR(cft->private); | |
af36f906 TH |
4902 | |
4903 | if (!do_swap_account && type == _MEMSWAP) | |
4904 | return -EOPNOTSUPP; | |
4905 | ||
8c7c6e34 KH |
4906 | switch (type) { |
4907 | case _MEM: | |
104f3928 | 4908 | if (name == RES_USAGE) |
c0ff4b85 | 4909 | val = mem_cgroup_usage(memcg, false); |
104f3928 | 4910 | else |
c0ff4b85 | 4911 | val = res_counter_read_u64(&memcg->res, name); |
8c7c6e34 KH |
4912 | break; |
4913 | case _MEMSWAP: | |
104f3928 | 4914 | if (name == RES_USAGE) |
c0ff4b85 | 4915 | val = mem_cgroup_usage(memcg, true); |
104f3928 | 4916 | else |
c0ff4b85 | 4917 | val = res_counter_read_u64(&memcg->memsw, name); |
8c7c6e34 | 4918 | break; |
510fc4e1 GC |
4919 | case _KMEM: |
4920 | val = res_counter_read_u64(&memcg->kmem, name); | |
4921 | break; | |
8c7c6e34 KH |
4922 | default: |
4923 | BUG(); | |
8c7c6e34 | 4924 | } |
af36f906 TH |
4925 | |
4926 | len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val); | |
4927 | return simple_read_from_buffer(buf, nbytes, ppos, str, len); | |
8cdea7c0 | 4928 | } |
510fc4e1 GC |
4929 | |
4930 | static int memcg_update_kmem_limit(struct cgroup *cont, u64 val) | |
4931 | { | |
4932 | int ret = -EINVAL; | |
4933 | #ifdef CONFIG_MEMCG_KMEM | |
a8964b9b GC |
4934 | bool must_inc_static_branch = false; |
4935 | ||
510fc4e1 GC |
4936 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
4937 | /* | |
4938 | * For simplicity, we won't allow this to be disabled. It also can't | |
4939 | * be changed if the cgroup has children already, or if tasks had | |
4940 | * already joined. | |
4941 | * | |
4942 | * If tasks join before we set the limit, a person looking at | |
4943 | * kmem.usage_in_bytes will have no way to determine when it took | |
4944 | * place, which makes the value quite meaningless. | |
4945 | * | |
4946 | * After it first became limited, changes in the value of the limit are | |
4947 | * of course permitted. | |
4948 | * | |
4949 | * Taking the cgroup_lock is really offensive, but it is so far the only | |
4950 | * way to guarantee that no children will appear. There are plenty of | |
4951 | * other offenders, and they should all go away. Fine grained locking | |
4952 | * is probably the way to go here. When we are fully hierarchical, we | |
4953 | * can also get rid of the use_hierarchy check. | |
4954 | */ | |
4955 | cgroup_lock(); | |
4956 | mutex_lock(&set_limit_mutex); | |
4957 | if (!memcg->kmem_account_flags && val != RESOURCE_MAX) { | |
4958 | if (cgroup_task_count(cont) || (memcg->use_hierarchy && | |
4959 | !list_empty(&cont->children))) { | |
4960 | ret = -EBUSY; | |
4961 | goto out; | |
4962 | } | |
4963 | ret = res_counter_set_limit(&memcg->kmem, val); | |
4964 | VM_BUG_ON(ret); | |
4965 | ||
55007d84 GC |
4966 | ret = memcg_update_cache_sizes(memcg); |
4967 | if (ret) { | |
4968 | res_counter_set_limit(&memcg->kmem, RESOURCE_MAX); | |
4969 | goto out; | |
4970 | } | |
a8964b9b | 4971 | must_inc_static_branch = true; |
7de37682 GC |
4972 | /* |
4973 | * kmem charges can outlive the cgroup. In the case of slab | |
4974 | * pages, for instance, a page contain objects from various | |
4975 | * processes, so it is unfeasible to migrate them away. We | |
4976 | * need to reference count the memcg because of that. | |
4977 | */ | |
4978 | mem_cgroup_get(memcg); | |
510fc4e1 GC |
4979 | } else |
4980 | ret = res_counter_set_limit(&memcg->kmem, val); | |
4981 | out: | |
4982 | mutex_unlock(&set_limit_mutex); | |
4983 | cgroup_unlock(); | |
a8964b9b GC |
4984 | |
4985 | /* | |
4986 | * We are by now familiar with the fact that we can't inc the static | |
4987 | * branch inside cgroup_lock. See disarm functions for details. A | |
4988 | * worker here is overkill, but also wrong: After the limit is set, we | |
4989 | * must start accounting right away. Since this operation can't fail, | |
4990 | * we can safely defer it to here - no rollback will be needed. | |
4991 | * | |
4992 | * The boolean used to control this is also safe, because | |
4993 | * KMEM_ACCOUNTED_ACTIVATED guarantees that only one process will be | |
4994 | * able to set it to true; | |
4995 | */ | |
4996 | if (must_inc_static_branch) { | |
4997 | static_key_slow_inc(&memcg_kmem_enabled_key); | |
4998 | /* | |
4999 | * setting the active bit after the inc will guarantee no one | |
5000 | * starts accounting before all call sites are patched | |
5001 | */ | |
5002 | memcg_kmem_set_active(memcg); | |
5003 | } | |
5004 | ||
510fc4e1 GC |
5005 | #endif |
5006 | return ret; | |
5007 | } | |
5008 | ||
55007d84 | 5009 | static int memcg_propagate_kmem(struct mem_cgroup *memcg) |
510fc4e1 | 5010 | { |
55007d84 | 5011 | int ret = 0; |
510fc4e1 GC |
5012 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); |
5013 | if (!parent) | |
55007d84 GC |
5014 | goto out; |
5015 | ||
510fc4e1 | 5016 | memcg->kmem_account_flags = parent->kmem_account_flags; |
7de37682 | 5017 | #ifdef CONFIG_MEMCG_KMEM |
a8964b9b GC |
5018 | /* |
5019 | * When that happen, we need to disable the static branch only on those | |
5020 | * memcgs that enabled it. To achieve this, we would be forced to | |
5021 | * complicate the code by keeping track of which memcgs were the ones | |
5022 | * that actually enabled limits, and which ones got it from its | |
5023 | * parents. | |
5024 | * | |
5025 | * It is a lot simpler just to do static_key_slow_inc() on every child | |
5026 | * that is accounted. | |
5027 | */ | |
55007d84 GC |
5028 | if (!memcg_kmem_is_active(memcg)) |
5029 | goto out; | |
5030 | ||
5031 | /* | |
5032 | * destroy(), called if we fail, will issue static_key_slow_inc() and | |
5033 | * mem_cgroup_put() if kmem is enabled. We have to either call them | |
5034 | * unconditionally, or clear the KMEM_ACTIVE flag. I personally find | |
5035 | * this more consistent, since it always leads to the same destroy path | |
5036 | */ | |
5037 | mem_cgroup_get(memcg); | |
5038 | static_key_slow_inc(&memcg_kmem_enabled_key); | |
5039 | ||
5040 | mutex_lock(&set_limit_mutex); | |
5041 | ret = memcg_update_cache_sizes(memcg); | |
5042 | mutex_unlock(&set_limit_mutex); | |
7de37682 | 5043 | #endif |
55007d84 GC |
5044 | out: |
5045 | return ret; | |
510fc4e1 GC |
5046 | } |
5047 | ||
628f4235 KH |
5048 | /* |
5049 | * The user of this function is... | |
5050 | * RES_LIMIT. | |
5051 | */ | |
856c13aa PM |
5052 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
5053 | const char *buffer) | |
8cdea7c0 | 5054 | { |
628f4235 | 5055 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
86ae53e1 GC |
5056 | enum res_type type; |
5057 | int name; | |
628f4235 KH |
5058 | unsigned long long val; |
5059 | int ret; | |
5060 | ||
8c7c6e34 KH |
5061 | type = MEMFILE_TYPE(cft->private); |
5062 | name = MEMFILE_ATTR(cft->private); | |
af36f906 TH |
5063 | |
5064 | if (!do_swap_account && type == _MEMSWAP) | |
5065 | return -EOPNOTSUPP; | |
5066 | ||
8c7c6e34 | 5067 | switch (name) { |
628f4235 | 5068 | case RES_LIMIT: |
4b3bde4c BS |
5069 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
5070 | ret = -EINVAL; | |
5071 | break; | |
5072 | } | |
628f4235 KH |
5073 | /* This function does all necessary parse...reuse it */ |
5074 | ret = res_counter_memparse_write_strategy(buffer, &val); | |
8c7c6e34 KH |
5075 | if (ret) |
5076 | break; | |
5077 | if (type == _MEM) | |
628f4235 | 5078 | ret = mem_cgroup_resize_limit(memcg, val); |
510fc4e1 | 5079 | else if (type == _MEMSWAP) |
8c7c6e34 | 5080 | ret = mem_cgroup_resize_memsw_limit(memcg, val); |
510fc4e1 GC |
5081 | else if (type == _KMEM) |
5082 | ret = memcg_update_kmem_limit(cont, val); | |
5083 | else | |
5084 | return -EINVAL; | |
628f4235 | 5085 | break; |
296c81d8 BS |
5086 | case RES_SOFT_LIMIT: |
5087 | ret = res_counter_memparse_write_strategy(buffer, &val); | |
5088 | if (ret) | |
5089 | break; | |
5090 | /* | |
5091 | * For memsw, soft limits are hard to implement in terms | |
5092 | * of semantics, for now, we support soft limits for | |
5093 | * control without swap | |
5094 | */ | |
5095 | if (type == _MEM) | |
5096 | ret = res_counter_set_soft_limit(&memcg->res, val); | |
5097 | else | |
5098 | ret = -EINVAL; | |
5099 | break; | |
628f4235 KH |
5100 | default: |
5101 | ret = -EINVAL; /* should be BUG() ? */ | |
5102 | break; | |
5103 | } | |
5104 | return ret; | |
8cdea7c0 BS |
5105 | } |
5106 | ||
fee7b548 KH |
5107 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
5108 | unsigned long long *mem_limit, unsigned long long *memsw_limit) | |
5109 | { | |
5110 | struct cgroup *cgroup; | |
5111 | unsigned long long min_limit, min_memsw_limit, tmp; | |
5112 | ||
5113 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
5114 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
5115 | cgroup = memcg->css.cgroup; | |
5116 | if (!memcg->use_hierarchy) | |
5117 | goto out; | |
5118 | ||
5119 | while (cgroup->parent) { | |
5120 | cgroup = cgroup->parent; | |
5121 | memcg = mem_cgroup_from_cont(cgroup); | |
5122 | if (!memcg->use_hierarchy) | |
5123 | break; | |
5124 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
5125 | min_limit = min(min_limit, tmp); | |
5126 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
5127 | min_memsw_limit = min(min_memsw_limit, tmp); | |
5128 | } | |
5129 | out: | |
5130 | *mem_limit = min_limit; | |
5131 | *memsw_limit = min_memsw_limit; | |
fee7b548 KH |
5132 | } |
5133 | ||
29f2a4da | 5134 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
c84872e1 | 5135 | { |
af36f906 | 5136 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
86ae53e1 GC |
5137 | int name; |
5138 | enum res_type type; | |
c84872e1 | 5139 | |
8c7c6e34 KH |
5140 | type = MEMFILE_TYPE(event); |
5141 | name = MEMFILE_ATTR(event); | |
af36f906 TH |
5142 | |
5143 | if (!do_swap_account && type == _MEMSWAP) | |
5144 | return -EOPNOTSUPP; | |
5145 | ||
8c7c6e34 | 5146 | switch (name) { |
29f2a4da | 5147 | case RES_MAX_USAGE: |
8c7c6e34 | 5148 | if (type == _MEM) |
c0ff4b85 | 5149 | res_counter_reset_max(&memcg->res); |
510fc4e1 | 5150 | else if (type == _MEMSWAP) |
c0ff4b85 | 5151 | res_counter_reset_max(&memcg->memsw); |
510fc4e1 GC |
5152 | else if (type == _KMEM) |
5153 | res_counter_reset_max(&memcg->kmem); | |
5154 | else | |
5155 | return -EINVAL; | |
29f2a4da PE |
5156 | break; |
5157 | case RES_FAILCNT: | |
8c7c6e34 | 5158 | if (type == _MEM) |
c0ff4b85 | 5159 | res_counter_reset_failcnt(&memcg->res); |
510fc4e1 | 5160 | else if (type == _MEMSWAP) |
c0ff4b85 | 5161 | res_counter_reset_failcnt(&memcg->memsw); |
510fc4e1 GC |
5162 | else if (type == _KMEM) |
5163 | res_counter_reset_failcnt(&memcg->kmem); | |
5164 | else | |
5165 | return -EINVAL; | |
29f2a4da PE |
5166 | break; |
5167 | } | |
f64c3f54 | 5168 | |
85cc59db | 5169 | return 0; |
c84872e1 PE |
5170 | } |
5171 | ||
7dc74be0 DN |
5172 | static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp, |
5173 | struct cftype *cft) | |
5174 | { | |
5175 | return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate; | |
5176 | } | |
5177 | ||
02491447 | 5178 | #ifdef CONFIG_MMU |
7dc74be0 DN |
5179 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
5180 | struct cftype *cft, u64 val) | |
5181 | { | |
c0ff4b85 | 5182 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
7dc74be0 DN |
5183 | |
5184 | if (val >= (1 << NR_MOVE_TYPE)) | |
5185 | return -EINVAL; | |
5186 | /* | |
5187 | * We check this value several times in both in can_attach() and | |
5188 | * attach(), so we need cgroup lock to prevent this value from being | |
5189 | * inconsistent. | |
5190 | */ | |
5191 | cgroup_lock(); | |
c0ff4b85 | 5192 | memcg->move_charge_at_immigrate = val; |
7dc74be0 DN |
5193 | cgroup_unlock(); |
5194 | ||
5195 | return 0; | |
5196 | } | |
02491447 DN |
5197 | #else |
5198 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, | |
5199 | struct cftype *cft, u64 val) | |
5200 | { | |
5201 | return -ENOSYS; | |
5202 | } | |
5203 | #endif | |
7dc74be0 | 5204 | |
406eb0c9 | 5205 | #ifdef CONFIG_NUMA |
ab215884 | 5206 | static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft, |
fada52ca | 5207 | struct seq_file *m) |
406eb0c9 YH |
5208 | { |
5209 | int nid; | |
5210 | unsigned long total_nr, file_nr, anon_nr, unevictable_nr; | |
5211 | unsigned long node_nr; | |
d79154bb | 5212 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
406eb0c9 | 5213 | |
d79154bb | 5214 | total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL); |
406eb0c9 | 5215 | seq_printf(m, "total=%lu", total_nr); |
31aaea4a | 5216 | for_each_node_state(nid, N_MEMORY) { |
d79154bb | 5217 | node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL); |
406eb0c9 YH |
5218 | seq_printf(m, " N%d=%lu", nid, node_nr); |
5219 | } | |
5220 | seq_putc(m, '\n'); | |
5221 | ||
d79154bb | 5222 | file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE); |
406eb0c9 | 5223 | seq_printf(m, "file=%lu", file_nr); |
31aaea4a | 5224 | for_each_node_state(nid, N_MEMORY) { |
d79154bb | 5225 | node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, |
bb2a0de9 | 5226 | LRU_ALL_FILE); |
406eb0c9 YH |
5227 | seq_printf(m, " N%d=%lu", nid, node_nr); |
5228 | } | |
5229 | seq_putc(m, '\n'); | |
5230 | ||
d79154bb | 5231 | anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON); |
406eb0c9 | 5232 | seq_printf(m, "anon=%lu", anon_nr); |
31aaea4a | 5233 | for_each_node_state(nid, N_MEMORY) { |
d79154bb | 5234 | node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, |
bb2a0de9 | 5235 | LRU_ALL_ANON); |
406eb0c9 YH |
5236 | seq_printf(m, " N%d=%lu", nid, node_nr); |
5237 | } | |
5238 | seq_putc(m, '\n'); | |
5239 | ||
d79154bb | 5240 | unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE)); |
406eb0c9 | 5241 | seq_printf(m, "unevictable=%lu", unevictable_nr); |
31aaea4a | 5242 | for_each_node_state(nid, N_MEMORY) { |
d79154bb | 5243 | node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, |
bb2a0de9 | 5244 | BIT(LRU_UNEVICTABLE)); |
406eb0c9 YH |
5245 | seq_printf(m, " N%d=%lu", nid, node_nr); |
5246 | } | |
5247 | seq_putc(m, '\n'); | |
5248 | return 0; | |
5249 | } | |
5250 | #endif /* CONFIG_NUMA */ | |
5251 | ||
af7c4b0e JW |
5252 | static inline void mem_cgroup_lru_names_not_uptodate(void) |
5253 | { | |
5254 | BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS); | |
5255 | } | |
5256 | ||
ab215884 | 5257 | static int memcg_stat_show(struct cgroup *cont, struct cftype *cft, |
78ccf5b5 | 5258 | struct seq_file *m) |
d2ceb9b7 | 5259 | { |
d79154bb | 5260 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
af7c4b0e JW |
5261 | struct mem_cgroup *mi; |
5262 | unsigned int i; | |
406eb0c9 | 5263 | |
af7c4b0e | 5264 | for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) { |
bff6bb83 | 5265 | if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account) |
1dd3a273 | 5266 | continue; |
af7c4b0e JW |
5267 | seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i], |
5268 | mem_cgroup_read_stat(memcg, i) * PAGE_SIZE); | |
1dd3a273 | 5269 | } |
7b854121 | 5270 | |
af7c4b0e JW |
5271 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) |
5272 | seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i], | |
5273 | mem_cgroup_read_events(memcg, i)); | |
5274 | ||
5275 | for (i = 0; i < NR_LRU_LISTS; i++) | |
5276 | seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i], | |
5277 | mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE); | |
5278 | ||
14067bb3 | 5279 | /* Hierarchical information */ |
fee7b548 KH |
5280 | { |
5281 | unsigned long long limit, memsw_limit; | |
d79154bb | 5282 | memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit); |
78ccf5b5 | 5283 | seq_printf(m, "hierarchical_memory_limit %llu\n", limit); |
fee7b548 | 5284 | if (do_swap_account) |
78ccf5b5 JW |
5285 | seq_printf(m, "hierarchical_memsw_limit %llu\n", |
5286 | memsw_limit); | |
fee7b548 | 5287 | } |
7f016ee8 | 5288 | |
af7c4b0e JW |
5289 | for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) { |
5290 | long long val = 0; | |
5291 | ||
bff6bb83 | 5292 | if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account) |
1dd3a273 | 5293 | continue; |
af7c4b0e JW |
5294 | for_each_mem_cgroup_tree(mi, memcg) |
5295 | val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE; | |
5296 | seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val); | |
5297 | } | |
5298 | ||
5299 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) { | |
5300 | unsigned long long val = 0; | |
5301 | ||
5302 | for_each_mem_cgroup_tree(mi, memcg) | |
5303 | val += mem_cgroup_read_events(mi, i); | |
5304 | seq_printf(m, "total_%s %llu\n", | |
5305 | mem_cgroup_events_names[i], val); | |
5306 | } | |
5307 | ||
5308 | for (i = 0; i < NR_LRU_LISTS; i++) { | |
5309 | unsigned long long val = 0; | |
5310 | ||
5311 | for_each_mem_cgroup_tree(mi, memcg) | |
5312 | val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE; | |
5313 | seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val); | |
1dd3a273 | 5314 | } |
14067bb3 | 5315 | |
7f016ee8 | 5316 | #ifdef CONFIG_DEBUG_VM |
7f016ee8 KM |
5317 | { |
5318 | int nid, zid; | |
5319 | struct mem_cgroup_per_zone *mz; | |
89abfab1 | 5320 | struct zone_reclaim_stat *rstat; |
7f016ee8 KM |
5321 | unsigned long recent_rotated[2] = {0, 0}; |
5322 | unsigned long recent_scanned[2] = {0, 0}; | |
5323 | ||
5324 | for_each_online_node(nid) | |
5325 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { | |
d79154bb | 5326 | mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
89abfab1 | 5327 | rstat = &mz->lruvec.reclaim_stat; |
7f016ee8 | 5328 | |
89abfab1 HD |
5329 | recent_rotated[0] += rstat->recent_rotated[0]; |
5330 | recent_rotated[1] += rstat->recent_rotated[1]; | |
5331 | recent_scanned[0] += rstat->recent_scanned[0]; | |
5332 | recent_scanned[1] += rstat->recent_scanned[1]; | |
7f016ee8 | 5333 | } |
78ccf5b5 JW |
5334 | seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]); |
5335 | seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]); | |
5336 | seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]); | |
5337 | seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]); | |
7f016ee8 KM |
5338 | } |
5339 | #endif | |
5340 | ||
d2ceb9b7 KH |
5341 | return 0; |
5342 | } | |
5343 | ||
a7885eb8 KM |
5344 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
5345 | { | |
5346 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
5347 | ||
1f4c025b | 5348 | return mem_cgroup_swappiness(memcg); |
a7885eb8 KM |
5349 | } |
5350 | ||
5351 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, | |
5352 | u64 val) | |
5353 | { | |
5354 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
5355 | struct mem_cgroup *parent; | |
068b38c1 | 5356 | |
a7885eb8 KM |
5357 | if (val > 100) |
5358 | return -EINVAL; | |
5359 | ||
5360 | if (cgrp->parent == NULL) | |
5361 | return -EINVAL; | |
5362 | ||
5363 | parent = mem_cgroup_from_cont(cgrp->parent); | |
068b38c1 LZ |
5364 | |
5365 | cgroup_lock(); | |
5366 | ||
a7885eb8 KM |
5367 | /* If under hierarchy, only empty-root can set this value */ |
5368 | if ((parent->use_hierarchy) || | |
068b38c1 LZ |
5369 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
5370 | cgroup_unlock(); | |
a7885eb8 | 5371 | return -EINVAL; |
068b38c1 | 5372 | } |
a7885eb8 | 5373 | |
a7885eb8 | 5374 | memcg->swappiness = val; |
a7885eb8 | 5375 | |
068b38c1 LZ |
5376 | cgroup_unlock(); |
5377 | ||
a7885eb8 KM |
5378 | return 0; |
5379 | } | |
5380 | ||
2e72b634 KS |
5381 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
5382 | { | |
5383 | struct mem_cgroup_threshold_ary *t; | |
5384 | u64 usage; | |
5385 | int i; | |
5386 | ||
5387 | rcu_read_lock(); | |
5388 | if (!swap) | |
2c488db2 | 5389 | t = rcu_dereference(memcg->thresholds.primary); |
2e72b634 | 5390 | else |
2c488db2 | 5391 | t = rcu_dereference(memcg->memsw_thresholds.primary); |
2e72b634 KS |
5392 | |
5393 | if (!t) | |
5394 | goto unlock; | |
5395 | ||
5396 | usage = mem_cgroup_usage(memcg, swap); | |
5397 | ||
5398 | /* | |
748dad36 | 5399 | * current_threshold points to threshold just below or equal to usage. |
2e72b634 KS |
5400 | * If it's not true, a threshold was crossed after last |
5401 | * call of __mem_cgroup_threshold(). | |
5402 | */ | |
5407a562 | 5403 | i = t->current_threshold; |
2e72b634 KS |
5404 | |
5405 | /* | |
5406 | * Iterate backward over array of thresholds starting from | |
5407 | * current_threshold and check if a threshold is crossed. | |
5408 | * If none of thresholds below usage is crossed, we read | |
5409 | * only one element of the array here. | |
5410 | */ | |
5411 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) | |
5412 | eventfd_signal(t->entries[i].eventfd, 1); | |
5413 | ||
5414 | /* i = current_threshold + 1 */ | |
5415 | i++; | |
5416 | ||
5417 | /* | |
5418 | * Iterate forward over array of thresholds starting from | |
5419 | * current_threshold+1 and check if a threshold is crossed. | |
5420 | * If none of thresholds above usage is crossed, we read | |
5421 | * only one element of the array here. | |
5422 | */ | |
5423 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) | |
5424 | eventfd_signal(t->entries[i].eventfd, 1); | |
5425 | ||
5426 | /* Update current_threshold */ | |
5407a562 | 5427 | t->current_threshold = i - 1; |
2e72b634 KS |
5428 | unlock: |
5429 | rcu_read_unlock(); | |
5430 | } | |
5431 | ||
5432 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) | |
5433 | { | |
ad4ca5f4 KS |
5434 | while (memcg) { |
5435 | __mem_cgroup_threshold(memcg, false); | |
5436 | if (do_swap_account) | |
5437 | __mem_cgroup_threshold(memcg, true); | |
5438 | ||
5439 | memcg = parent_mem_cgroup(memcg); | |
5440 | } | |
2e72b634 KS |
5441 | } |
5442 | ||
5443 | static int compare_thresholds(const void *a, const void *b) | |
5444 | { | |
5445 | const struct mem_cgroup_threshold *_a = a; | |
5446 | const struct mem_cgroup_threshold *_b = b; | |
5447 | ||
5448 | return _a->threshold - _b->threshold; | |
5449 | } | |
5450 | ||
c0ff4b85 | 5451 | static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg) |
9490ff27 KH |
5452 | { |
5453 | struct mem_cgroup_eventfd_list *ev; | |
5454 | ||
c0ff4b85 | 5455 | list_for_each_entry(ev, &memcg->oom_notify, list) |
9490ff27 KH |
5456 | eventfd_signal(ev->eventfd, 1); |
5457 | return 0; | |
5458 | } | |
5459 | ||
c0ff4b85 | 5460 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg) |
9490ff27 | 5461 | { |
7d74b06f KH |
5462 | struct mem_cgroup *iter; |
5463 | ||
c0ff4b85 | 5464 | for_each_mem_cgroup_tree(iter, memcg) |
7d74b06f | 5465 | mem_cgroup_oom_notify_cb(iter); |
9490ff27 KH |
5466 | } |
5467 | ||
5468 | static int mem_cgroup_usage_register_event(struct cgroup *cgrp, | |
5469 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) | |
2e72b634 KS |
5470 | { |
5471 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
2c488db2 KS |
5472 | struct mem_cgroup_thresholds *thresholds; |
5473 | struct mem_cgroup_threshold_ary *new; | |
86ae53e1 | 5474 | enum res_type type = MEMFILE_TYPE(cft->private); |
2e72b634 | 5475 | u64 threshold, usage; |
2c488db2 | 5476 | int i, size, ret; |
2e72b634 KS |
5477 | |
5478 | ret = res_counter_memparse_write_strategy(args, &threshold); | |
5479 | if (ret) | |
5480 | return ret; | |
5481 | ||
5482 | mutex_lock(&memcg->thresholds_lock); | |
2c488db2 | 5483 | |
2e72b634 | 5484 | if (type == _MEM) |
2c488db2 | 5485 | thresholds = &memcg->thresholds; |
2e72b634 | 5486 | else if (type == _MEMSWAP) |
2c488db2 | 5487 | thresholds = &memcg->memsw_thresholds; |
2e72b634 KS |
5488 | else |
5489 | BUG(); | |
5490 | ||
5491 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); | |
5492 | ||
5493 | /* Check if a threshold crossed before adding a new one */ | |
2c488db2 | 5494 | if (thresholds->primary) |
2e72b634 KS |
5495 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
5496 | ||
2c488db2 | 5497 | size = thresholds->primary ? thresholds->primary->size + 1 : 1; |
2e72b634 KS |
5498 | |
5499 | /* Allocate memory for new array of thresholds */ | |
2c488db2 | 5500 | new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold), |
2e72b634 | 5501 | GFP_KERNEL); |
2c488db2 | 5502 | if (!new) { |
2e72b634 KS |
5503 | ret = -ENOMEM; |
5504 | goto unlock; | |
5505 | } | |
2c488db2 | 5506 | new->size = size; |
2e72b634 KS |
5507 | |
5508 | /* Copy thresholds (if any) to new array */ | |
2c488db2 KS |
5509 | if (thresholds->primary) { |
5510 | memcpy(new->entries, thresholds->primary->entries, (size - 1) * | |
2e72b634 | 5511 | sizeof(struct mem_cgroup_threshold)); |
2c488db2 KS |
5512 | } |
5513 | ||
2e72b634 | 5514 | /* Add new threshold */ |
2c488db2 KS |
5515 | new->entries[size - 1].eventfd = eventfd; |
5516 | new->entries[size - 1].threshold = threshold; | |
2e72b634 KS |
5517 | |
5518 | /* Sort thresholds. Registering of new threshold isn't time-critical */ | |
2c488db2 | 5519 | sort(new->entries, size, sizeof(struct mem_cgroup_threshold), |
2e72b634 KS |
5520 | compare_thresholds, NULL); |
5521 | ||
5522 | /* Find current threshold */ | |
2c488db2 | 5523 | new->current_threshold = -1; |
2e72b634 | 5524 | for (i = 0; i < size; i++) { |
748dad36 | 5525 | if (new->entries[i].threshold <= usage) { |
2e72b634 | 5526 | /* |
2c488db2 KS |
5527 | * new->current_threshold will not be used until |
5528 | * rcu_assign_pointer(), so it's safe to increment | |
2e72b634 KS |
5529 | * it here. |
5530 | */ | |
2c488db2 | 5531 | ++new->current_threshold; |
748dad36 SZ |
5532 | } else |
5533 | break; | |
2e72b634 KS |
5534 | } |
5535 | ||
2c488db2 KS |
5536 | /* Free old spare buffer and save old primary buffer as spare */ |
5537 | kfree(thresholds->spare); | |
5538 | thresholds->spare = thresholds->primary; | |
5539 | ||
5540 | rcu_assign_pointer(thresholds->primary, new); | |
2e72b634 | 5541 | |
907860ed | 5542 | /* To be sure that nobody uses thresholds */ |
2e72b634 KS |
5543 | synchronize_rcu(); |
5544 | ||
2e72b634 KS |
5545 | unlock: |
5546 | mutex_unlock(&memcg->thresholds_lock); | |
5547 | ||
5548 | return ret; | |
5549 | } | |
5550 | ||
907860ed | 5551 | static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp, |
9490ff27 | 5552 | struct cftype *cft, struct eventfd_ctx *eventfd) |
2e72b634 KS |
5553 | { |
5554 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
2c488db2 KS |
5555 | struct mem_cgroup_thresholds *thresholds; |
5556 | struct mem_cgroup_threshold_ary *new; | |
86ae53e1 | 5557 | enum res_type type = MEMFILE_TYPE(cft->private); |
2e72b634 | 5558 | u64 usage; |
2c488db2 | 5559 | int i, j, size; |
2e72b634 KS |
5560 | |
5561 | mutex_lock(&memcg->thresholds_lock); | |
5562 | if (type == _MEM) | |
2c488db2 | 5563 | thresholds = &memcg->thresholds; |
2e72b634 | 5564 | else if (type == _MEMSWAP) |
2c488db2 | 5565 | thresholds = &memcg->memsw_thresholds; |
2e72b634 KS |
5566 | else |
5567 | BUG(); | |
5568 | ||
371528ca AV |
5569 | if (!thresholds->primary) |
5570 | goto unlock; | |
5571 | ||
2e72b634 KS |
5572 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); |
5573 | ||
5574 | /* Check if a threshold crossed before removing */ | |
5575 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); | |
5576 | ||
5577 | /* Calculate new number of threshold */ | |
2c488db2 KS |
5578 | size = 0; |
5579 | for (i = 0; i < thresholds->primary->size; i++) { | |
5580 | if (thresholds->primary->entries[i].eventfd != eventfd) | |
2e72b634 KS |
5581 | size++; |
5582 | } | |
5583 | ||
2c488db2 | 5584 | new = thresholds->spare; |
907860ed | 5585 | |
2e72b634 KS |
5586 | /* Set thresholds array to NULL if we don't have thresholds */ |
5587 | if (!size) { | |
2c488db2 KS |
5588 | kfree(new); |
5589 | new = NULL; | |
907860ed | 5590 | goto swap_buffers; |
2e72b634 KS |
5591 | } |
5592 | ||
2c488db2 | 5593 | new->size = size; |
2e72b634 KS |
5594 | |
5595 | /* Copy thresholds and find current threshold */ | |
2c488db2 KS |
5596 | new->current_threshold = -1; |
5597 | for (i = 0, j = 0; i < thresholds->primary->size; i++) { | |
5598 | if (thresholds->primary->entries[i].eventfd == eventfd) | |
2e72b634 KS |
5599 | continue; |
5600 | ||
2c488db2 | 5601 | new->entries[j] = thresholds->primary->entries[i]; |
748dad36 | 5602 | if (new->entries[j].threshold <= usage) { |
2e72b634 | 5603 | /* |
2c488db2 | 5604 | * new->current_threshold will not be used |
2e72b634 KS |
5605 | * until rcu_assign_pointer(), so it's safe to increment |
5606 | * it here. | |
5607 | */ | |
2c488db2 | 5608 | ++new->current_threshold; |
2e72b634 KS |
5609 | } |
5610 | j++; | |
5611 | } | |
5612 | ||
907860ed | 5613 | swap_buffers: |
2c488db2 KS |
5614 | /* Swap primary and spare array */ |
5615 | thresholds->spare = thresholds->primary; | |
8c757763 SZ |
5616 | /* If all events are unregistered, free the spare array */ |
5617 | if (!new) { | |
5618 | kfree(thresholds->spare); | |
5619 | thresholds->spare = NULL; | |
5620 | } | |
5621 | ||
2c488db2 | 5622 | rcu_assign_pointer(thresholds->primary, new); |
2e72b634 | 5623 | |
907860ed | 5624 | /* To be sure that nobody uses thresholds */ |
2e72b634 | 5625 | synchronize_rcu(); |
371528ca | 5626 | unlock: |
2e72b634 | 5627 | mutex_unlock(&memcg->thresholds_lock); |
2e72b634 | 5628 | } |
c1e862c1 | 5629 | |
9490ff27 KH |
5630 | static int mem_cgroup_oom_register_event(struct cgroup *cgrp, |
5631 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) | |
5632 | { | |
5633 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
5634 | struct mem_cgroup_eventfd_list *event; | |
86ae53e1 | 5635 | enum res_type type = MEMFILE_TYPE(cft->private); |
9490ff27 KH |
5636 | |
5637 | BUG_ON(type != _OOM_TYPE); | |
5638 | event = kmalloc(sizeof(*event), GFP_KERNEL); | |
5639 | if (!event) | |
5640 | return -ENOMEM; | |
5641 | ||
1af8efe9 | 5642 | spin_lock(&memcg_oom_lock); |
9490ff27 KH |
5643 | |
5644 | event->eventfd = eventfd; | |
5645 | list_add(&event->list, &memcg->oom_notify); | |
5646 | ||
5647 | /* already in OOM ? */ | |
79dfdacc | 5648 | if (atomic_read(&memcg->under_oom)) |
9490ff27 | 5649 | eventfd_signal(eventfd, 1); |
1af8efe9 | 5650 | spin_unlock(&memcg_oom_lock); |
9490ff27 KH |
5651 | |
5652 | return 0; | |
5653 | } | |
5654 | ||
907860ed | 5655 | static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp, |
9490ff27 KH |
5656 | struct cftype *cft, struct eventfd_ctx *eventfd) |
5657 | { | |
c0ff4b85 | 5658 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
9490ff27 | 5659 | struct mem_cgroup_eventfd_list *ev, *tmp; |
86ae53e1 | 5660 | enum res_type type = MEMFILE_TYPE(cft->private); |
9490ff27 KH |
5661 | |
5662 | BUG_ON(type != _OOM_TYPE); | |
5663 | ||
1af8efe9 | 5664 | spin_lock(&memcg_oom_lock); |
9490ff27 | 5665 | |
c0ff4b85 | 5666 | list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) { |
9490ff27 KH |
5667 | if (ev->eventfd == eventfd) { |
5668 | list_del(&ev->list); | |
5669 | kfree(ev); | |
5670 | } | |
5671 | } | |
5672 | ||
1af8efe9 | 5673 | spin_unlock(&memcg_oom_lock); |
9490ff27 KH |
5674 | } |
5675 | ||
3c11ecf4 KH |
5676 | static int mem_cgroup_oom_control_read(struct cgroup *cgrp, |
5677 | struct cftype *cft, struct cgroup_map_cb *cb) | |
5678 | { | |
c0ff4b85 | 5679 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
3c11ecf4 | 5680 | |
c0ff4b85 | 5681 | cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable); |
3c11ecf4 | 5682 | |
c0ff4b85 | 5683 | if (atomic_read(&memcg->under_oom)) |
3c11ecf4 KH |
5684 | cb->fill(cb, "under_oom", 1); |
5685 | else | |
5686 | cb->fill(cb, "under_oom", 0); | |
5687 | return 0; | |
5688 | } | |
5689 | ||
3c11ecf4 KH |
5690 | static int mem_cgroup_oom_control_write(struct cgroup *cgrp, |
5691 | struct cftype *cft, u64 val) | |
5692 | { | |
c0ff4b85 | 5693 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
3c11ecf4 KH |
5694 | struct mem_cgroup *parent; |
5695 | ||
5696 | /* cannot set to root cgroup and only 0 and 1 are allowed */ | |
5697 | if (!cgrp->parent || !((val == 0) || (val == 1))) | |
5698 | return -EINVAL; | |
5699 | ||
5700 | parent = mem_cgroup_from_cont(cgrp->parent); | |
5701 | ||
5702 | cgroup_lock(); | |
5703 | /* oom-kill-disable is a flag for subhierarchy. */ | |
5704 | if ((parent->use_hierarchy) || | |
c0ff4b85 | 5705 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
3c11ecf4 KH |
5706 | cgroup_unlock(); |
5707 | return -EINVAL; | |
5708 | } | |
c0ff4b85 | 5709 | memcg->oom_kill_disable = val; |
4d845ebf | 5710 | if (!val) |
c0ff4b85 | 5711 | memcg_oom_recover(memcg); |
3c11ecf4 KH |
5712 | cgroup_unlock(); |
5713 | return 0; | |
5714 | } | |
5715 | ||
c255a458 | 5716 | #ifdef CONFIG_MEMCG_KMEM |
cbe128e3 | 5717 | static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss) |
e5671dfa | 5718 | { |
55007d84 GC |
5719 | int ret; |
5720 | ||
2633d7a0 | 5721 | memcg->kmemcg_id = -1; |
55007d84 GC |
5722 | ret = memcg_propagate_kmem(memcg); |
5723 | if (ret) | |
5724 | return ret; | |
2633d7a0 | 5725 | |
1d62e436 | 5726 | return mem_cgroup_sockets_init(memcg, ss); |
e5671dfa GC |
5727 | }; |
5728 | ||
1d62e436 | 5729 | static void kmem_cgroup_destroy(struct mem_cgroup *memcg) |
d1a4c0b3 | 5730 | { |
1d62e436 | 5731 | mem_cgroup_sockets_destroy(memcg); |
7de37682 GC |
5732 | |
5733 | memcg_kmem_mark_dead(memcg); | |
5734 | ||
5735 | if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0) | |
5736 | return; | |
5737 | ||
5738 | /* | |
5739 | * Charges already down to 0, undo mem_cgroup_get() done in the charge | |
5740 | * path here, being careful not to race with memcg_uncharge_kmem: it is | |
5741 | * possible that the charges went down to 0 between mark_dead and the | |
5742 | * res_counter read, so in that case, we don't need the put | |
5743 | */ | |
5744 | if (memcg_kmem_test_and_clear_dead(memcg)) | |
5745 | mem_cgroup_put(memcg); | |
d1a4c0b3 | 5746 | } |
e5671dfa | 5747 | #else |
cbe128e3 | 5748 | static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss) |
e5671dfa GC |
5749 | { |
5750 | return 0; | |
5751 | } | |
d1a4c0b3 | 5752 | |
1d62e436 | 5753 | static void kmem_cgroup_destroy(struct mem_cgroup *memcg) |
d1a4c0b3 GC |
5754 | { |
5755 | } | |
e5671dfa GC |
5756 | #endif |
5757 | ||
8cdea7c0 BS |
5758 | static struct cftype mem_cgroup_files[] = { |
5759 | { | |
0eea1030 | 5760 | .name = "usage_in_bytes", |
8c7c6e34 | 5761 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
af36f906 | 5762 | .read = mem_cgroup_read, |
9490ff27 KH |
5763 | .register_event = mem_cgroup_usage_register_event, |
5764 | .unregister_event = mem_cgroup_usage_unregister_event, | |
8cdea7c0 | 5765 | }, |
c84872e1 PE |
5766 | { |
5767 | .name = "max_usage_in_bytes", | |
8c7c6e34 | 5768 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
29f2a4da | 5769 | .trigger = mem_cgroup_reset, |
af36f906 | 5770 | .read = mem_cgroup_read, |
c84872e1 | 5771 | }, |
8cdea7c0 | 5772 | { |
0eea1030 | 5773 | .name = "limit_in_bytes", |
8c7c6e34 | 5774 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
856c13aa | 5775 | .write_string = mem_cgroup_write, |
af36f906 | 5776 | .read = mem_cgroup_read, |
8cdea7c0 | 5777 | }, |
296c81d8 BS |
5778 | { |
5779 | .name = "soft_limit_in_bytes", | |
5780 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), | |
5781 | .write_string = mem_cgroup_write, | |
af36f906 | 5782 | .read = mem_cgroup_read, |
296c81d8 | 5783 | }, |
8cdea7c0 BS |
5784 | { |
5785 | .name = "failcnt", | |
8c7c6e34 | 5786 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
29f2a4da | 5787 | .trigger = mem_cgroup_reset, |
af36f906 | 5788 | .read = mem_cgroup_read, |
8cdea7c0 | 5789 | }, |
d2ceb9b7 KH |
5790 | { |
5791 | .name = "stat", | |
ab215884 | 5792 | .read_seq_string = memcg_stat_show, |
d2ceb9b7 | 5793 | }, |
c1e862c1 KH |
5794 | { |
5795 | .name = "force_empty", | |
5796 | .trigger = mem_cgroup_force_empty_write, | |
5797 | }, | |
18f59ea7 BS |
5798 | { |
5799 | .name = "use_hierarchy", | |
5800 | .write_u64 = mem_cgroup_hierarchy_write, | |
5801 | .read_u64 = mem_cgroup_hierarchy_read, | |
5802 | }, | |
a7885eb8 KM |
5803 | { |
5804 | .name = "swappiness", | |
5805 | .read_u64 = mem_cgroup_swappiness_read, | |
5806 | .write_u64 = mem_cgroup_swappiness_write, | |
5807 | }, | |
7dc74be0 DN |
5808 | { |
5809 | .name = "move_charge_at_immigrate", | |
5810 | .read_u64 = mem_cgroup_move_charge_read, | |
5811 | .write_u64 = mem_cgroup_move_charge_write, | |
5812 | }, | |
9490ff27 KH |
5813 | { |
5814 | .name = "oom_control", | |
3c11ecf4 KH |
5815 | .read_map = mem_cgroup_oom_control_read, |
5816 | .write_u64 = mem_cgroup_oom_control_write, | |
9490ff27 KH |
5817 | .register_event = mem_cgroup_oom_register_event, |
5818 | .unregister_event = mem_cgroup_oom_unregister_event, | |
5819 | .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL), | |
5820 | }, | |
406eb0c9 YH |
5821 | #ifdef CONFIG_NUMA |
5822 | { | |
5823 | .name = "numa_stat", | |
ab215884 | 5824 | .read_seq_string = memcg_numa_stat_show, |
406eb0c9 YH |
5825 | }, |
5826 | #endif | |
c255a458 | 5827 | #ifdef CONFIG_MEMCG_SWAP |
8c7c6e34 KH |
5828 | { |
5829 | .name = "memsw.usage_in_bytes", | |
5830 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), | |
af36f906 | 5831 | .read = mem_cgroup_read, |
9490ff27 KH |
5832 | .register_event = mem_cgroup_usage_register_event, |
5833 | .unregister_event = mem_cgroup_usage_unregister_event, | |
8c7c6e34 KH |
5834 | }, |
5835 | { | |
5836 | .name = "memsw.max_usage_in_bytes", | |
5837 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), | |
5838 | .trigger = mem_cgroup_reset, | |
af36f906 | 5839 | .read = mem_cgroup_read, |
8c7c6e34 KH |
5840 | }, |
5841 | { | |
5842 | .name = "memsw.limit_in_bytes", | |
5843 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), | |
5844 | .write_string = mem_cgroup_write, | |
af36f906 | 5845 | .read = mem_cgroup_read, |
8c7c6e34 KH |
5846 | }, |
5847 | { | |
5848 | .name = "memsw.failcnt", | |
5849 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), | |
5850 | .trigger = mem_cgroup_reset, | |
af36f906 | 5851 | .read = mem_cgroup_read, |
8c7c6e34 | 5852 | }, |
510fc4e1 GC |
5853 | #endif |
5854 | #ifdef CONFIG_MEMCG_KMEM | |
5855 | { | |
5856 | .name = "kmem.limit_in_bytes", | |
5857 | .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT), | |
5858 | .write_string = mem_cgroup_write, | |
5859 | .read = mem_cgroup_read, | |
5860 | }, | |
5861 | { | |
5862 | .name = "kmem.usage_in_bytes", | |
5863 | .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE), | |
5864 | .read = mem_cgroup_read, | |
5865 | }, | |
5866 | { | |
5867 | .name = "kmem.failcnt", | |
5868 | .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT), | |
5869 | .trigger = mem_cgroup_reset, | |
5870 | .read = mem_cgroup_read, | |
5871 | }, | |
5872 | { | |
5873 | .name = "kmem.max_usage_in_bytes", | |
5874 | .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE), | |
5875 | .trigger = mem_cgroup_reset, | |
5876 | .read = mem_cgroup_read, | |
5877 | }, | |
749c5415 GC |
5878 | #ifdef CONFIG_SLABINFO |
5879 | { | |
5880 | .name = "kmem.slabinfo", | |
5881 | .read_seq_string = mem_cgroup_slabinfo_read, | |
5882 | }, | |
5883 | #endif | |
8c7c6e34 | 5884 | #endif |
6bc10349 | 5885 | { }, /* terminate */ |
af36f906 | 5886 | }; |
8c7c6e34 | 5887 | |
c0ff4b85 | 5888 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node) |
6d12e2d8 KH |
5889 | { |
5890 | struct mem_cgroup_per_node *pn; | |
1ecaab2b | 5891 | struct mem_cgroup_per_zone *mz; |
41e3355d | 5892 | int zone, tmp = node; |
1ecaab2b KH |
5893 | /* |
5894 | * This routine is called against possible nodes. | |
5895 | * But it's BUG to call kmalloc() against offline node. | |
5896 | * | |
5897 | * TODO: this routine can waste much memory for nodes which will | |
5898 | * never be onlined. It's better to use memory hotplug callback | |
5899 | * function. | |
5900 | */ | |
41e3355d KH |
5901 | if (!node_state(node, N_NORMAL_MEMORY)) |
5902 | tmp = -1; | |
17295c88 | 5903 | pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
6d12e2d8 KH |
5904 | if (!pn) |
5905 | return 1; | |
1ecaab2b | 5906 | |
1ecaab2b KH |
5907 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
5908 | mz = &pn->zoneinfo[zone]; | |
bea8c150 | 5909 | lruvec_init(&mz->lruvec); |
f64c3f54 | 5910 | mz->usage_in_excess = 0; |
4e416953 | 5911 | mz->on_tree = false; |
d79154bb | 5912 | mz->memcg = memcg; |
1ecaab2b | 5913 | } |
0a619e58 | 5914 | memcg->info.nodeinfo[node] = pn; |
6d12e2d8 KH |
5915 | return 0; |
5916 | } | |
5917 | ||
c0ff4b85 | 5918 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node) |
1ecaab2b | 5919 | { |
c0ff4b85 | 5920 | kfree(memcg->info.nodeinfo[node]); |
1ecaab2b KH |
5921 | } |
5922 | ||
33327948 KH |
5923 | static struct mem_cgroup *mem_cgroup_alloc(void) |
5924 | { | |
d79154bb | 5925 | struct mem_cgroup *memcg; |
c62b1a3b | 5926 | int size = sizeof(struct mem_cgroup); |
33327948 | 5927 | |
c62b1a3b | 5928 | /* Can be very big if MAX_NUMNODES is very big */ |
c8dad2bb | 5929 | if (size < PAGE_SIZE) |
d79154bb | 5930 | memcg = kzalloc(size, GFP_KERNEL); |
33327948 | 5931 | else |
d79154bb | 5932 | memcg = vzalloc(size); |
33327948 | 5933 | |
d79154bb | 5934 | if (!memcg) |
e7bbcdf3 DC |
5935 | return NULL; |
5936 | ||
d79154bb HD |
5937 | memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu); |
5938 | if (!memcg->stat) | |
d2e61b8d | 5939 | goto out_free; |
d79154bb HD |
5940 | spin_lock_init(&memcg->pcp_counter_lock); |
5941 | return memcg; | |
d2e61b8d DC |
5942 | |
5943 | out_free: | |
5944 | if (size < PAGE_SIZE) | |
d79154bb | 5945 | kfree(memcg); |
d2e61b8d | 5946 | else |
d79154bb | 5947 | vfree(memcg); |
d2e61b8d | 5948 | return NULL; |
33327948 KH |
5949 | } |
5950 | ||
59927fb9 | 5951 | /* |
c8b2a36f GC |
5952 | * At destroying mem_cgroup, references from swap_cgroup can remain. |
5953 | * (scanning all at force_empty is too costly...) | |
5954 | * | |
5955 | * Instead of clearing all references at force_empty, we remember | |
5956 | * the number of reference from swap_cgroup and free mem_cgroup when | |
5957 | * it goes down to 0. | |
5958 | * | |
5959 | * Removal of cgroup itself succeeds regardless of refs from swap. | |
59927fb9 | 5960 | */ |
c8b2a36f GC |
5961 | |
5962 | static void __mem_cgroup_free(struct mem_cgroup *memcg) | |
59927fb9 | 5963 | { |
c8b2a36f | 5964 | int node; |
3afe36b1 | 5965 | int size = sizeof(struct mem_cgroup); |
59927fb9 | 5966 | |
c8b2a36f GC |
5967 | mem_cgroup_remove_from_trees(memcg); |
5968 | free_css_id(&mem_cgroup_subsys, &memcg->css); | |
5969 | ||
5970 | for_each_node(node) | |
5971 | free_mem_cgroup_per_zone_info(memcg, node); | |
5972 | ||
5973 | free_percpu(memcg->stat); | |
5974 | ||
3f134619 GC |
5975 | /* |
5976 | * We need to make sure that (at least for now), the jump label | |
5977 | * destruction code runs outside of the cgroup lock. This is because | |
5978 | * get_online_cpus(), which is called from the static_branch update, | |
5979 | * can't be called inside the cgroup_lock. cpusets are the ones | |
5980 | * enforcing this dependency, so if they ever change, we might as well. | |
5981 | * | |
5982 | * schedule_work() will guarantee this happens. Be careful if you need | |
5983 | * to move this code around, and make sure it is outside | |
5984 | * the cgroup_lock. | |
5985 | */ | |
a8964b9b | 5986 | disarm_static_keys(memcg); |
3afe36b1 GC |
5987 | if (size < PAGE_SIZE) |
5988 | kfree(memcg); | |
5989 | else | |
5990 | vfree(memcg); | |
59927fb9 | 5991 | } |
3afe36b1 | 5992 | |
59927fb9 | 5993 | |
8c7c6e34 | 5994 | /* |
c8b2a36f GC |
5995 | * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU, |
5996 | * but in process context. The work_freeing structure is overlaid | |
5997 | * on the rcu_freeing structure, which itself is overlaid on memsw. | |
8c7c6e34 | 5998 | */ |
c8b2a36f | 5999 | static void free_work(struct work_struct *work) |
33327948 | 6000 | { |
c8b2a36f | 6001 | struct mem_cgroup *memcg; |
08e552c6 | 6002 | |
c8b2a36f GC |
6003 | memcg = container_of(work, struct mem_cgroup, work_freeing); |
6004 | __mem_cgroup_free(memcg); | |
6005 | } | |
04046e1a | 6006 | |
c8b2a36f GC |
6007 | static void free_rcu(struct rcu_head *rcu_head) |
6008 | { | |
6009 | struct mem_cgroup *memcg; | |
08e552c6 | 6010 | |
c8b2a36f GC |
6011 | memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing); |
6012 | INIT_WORK(&memcg->work_freeing, free_work); | |
6013 | schedule_work(&memcg->work_freeing); | |
33327948 KH |
6014 | } |
6015 | ||
c0ff4b85 | 6016 | static void mem_cgroup_get(struct mem_cgroup *memcg) |
8c7c6e34 | 6017 | { |
c0ff4b85 | 6018 | atomic_inc(&memcg->refcnt); |
8c7c6e34 KH |
6019 | } |
6020 | ||
c0ff4b85 | 6021 | static void __mem_cgroup_put(struct mem_cgroup *memcg, int count) |
8c7c6e34 | 6022 | { |
c0ff4b85 R |
6023 | if (atomic_sub_and_test(count, &memcg->refcnt)) { |
6024 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); | |
c8b2a36f | 6025 | call_rcu(&memcg->rcu_freeing, free_rcu); |
7bcc1bb1 DN |
6026 | if (parent) |
6027 | mem_cgroup_put(parent); | |
6028 | } | |
8c7c6e34 KH |
6029 | } |
6030 | ||
c0ff4b85 | 6031 | static void mem_cgroup_put(struct mem_cgroup *memcg) |
483c30b5 | 6032 | { |
c0ff4b85 | 6033 | __mem_cgroup_put(memcg, 1); |
483c30b5 DN |
6034 | } |
6035 | ||
7bcc1bb1 DN |
6036 | /* |
6037 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. | |
6038 | */ | |
e1aab161 | 6039 | struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg) |
7bcc1bb1 | 6040 | { |
c0ff4b85 | 6041 | if (!memcg->res.parent) |
7bcc1bb1 | 6042 | return NULL; |
c0ff4b85 | 6043 | return mem_cgroup_from_res_counter(memcg->res.parent, res); |
7bcc1bb1 | 6044 | } |
e1aab161 | 6045 | EXPORT_SYMBOL(parent_mem_cgroup); |
33327948 | 6046 | |
c255a458 | 6047 | #ifdef CONFIG_MEMCG_SWAP |
c077719b KH |
6048 | static void __init enable_swap_cgroup(void) |
6049 | { | |
f8d66542 | 6050 | if (!mem_cgroup_disabled() && really_do_swap_account) |
c077719b KH |
6051 | do_swap_account = 1; |
6052 | } | |
6053 | #else | |
6054 | static void __init enable_swap_cgroup(void) | |
6055 | { | |
6056 | } | |
6057 | #endif | |
6058 | ||
f64c3f54 BS |
6059 | static int mem_cgroup_soft_limit_tree_init(void) |
6060 | { | |
6061 | struct mem_cgroup_tree_per_node *rtpn; | |
6062 | struct mem_cgroup_tree_per_zone *rtpz; | |
6063 | int tmp, node, zone; | |
6064 | ||
3ed28fa1 | 6065 | for_each_node(node) { |
f64c3f54 BS |
6066 | tmp = node; |
6067 | if (!node_state(node, N_NORMAL_MEMORY)) | |
6068 | tmp = -1; | |
6069 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp); | |
6070 | if (!rtpn) | |
c3cecc68 | 6071 | goto err_cleanup; |
f64c3f54 BS |
6072 | |
6073 | soft_limit_tree.rb_tree_per_node[node] = rtpn; | |
6074 | ||
6075 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
6076 | rtpz = &rtpn->rb_tree_per_zone[zone]; | |
6077 | rtpz->rb_root = RB_ROOT; | |
6078 | spin_lock_init(&rtpz->lock); | |
6079 | } | |
6080 | } | |
6081 | return 0; | |
c3cecc68 MH |
6082 | |
6083 | err_cleanup: | |
3ed28fa1 | 6084 | for_each_node(node) { |
c3cecc68 MH |
6085 | if (!soft_limit_tree.rb_tree_per_node[node]) |
6086 | break; | |
6087 | kfree(soft_limit_tree.rb_tree_per_node[node]); | |
6088 | soft_limit_tree.rb_tree_per_node[node] = NULL; | |
6089 | } | |
6090 | return 1; | |
6091 | ||
f64c3f54 BS |
6092 | } |
6093 | ||
0eb253e2 | 6094 | static struct cgroup_subsys_state * __ref |
92fb9748 | 6095 | mem_cgroup_css_alloc(struct cgroup *cont) |
8cdea7c0 | 6096 | { |
c0ff4b85 | 6097 | struct mem_cgroup *memcg, *parent; |
04046e1a | 6098 | long error = -ENOMEM; |
6d12e2d8 | 6099 | int node; |
8cdea7c0 | 6100 | |
c0ff4b85 R |
6101 | memcg = mem_cgroup_alloc(); |
6102 | if (!memcg) | |
04046e1a | 6103 | return ERR_PTR(error); |
78fb7466 | 6104 | |
3ed28fa1 | 6105 | for_each_node(node) |
c0ff4b85 | 6106 | if (alloc_mem_cgroup_per_zone_info(memcg, node)) |
6d12e2d8 | 6107 | goto free_out; |
f64c3f54 | 6108 | |
c077719b | 6109 | /* root ? */ |
28dbc4b6 | 6110 | if (cont->parent == NULL) { |
cdec2e42 | 6111 | int cpu; |
c077719b | 6112 | enable_swap_cgroup(); |
28dbc4b6 | 6113 | parent = NULL; |
f64c3f54 BS |
6114 | if (mem_cgroup_soft_limit_tree_init()) |
6115 | goto free_out; | |
a41c58a6 | 6116 | root_mem_cgroup = memcg; |
cdec2e42 KH |
6117 | for_each_possible_cpu(cpu) { |
6118 | struct memcg_stock_pcp *stock = | |
6119 | &per_cpu(memcg_stock, cpu); | |
6120 | INIT_WORK(&stock->work, drain_local_stock); | |
6121 | } | |
18f59ea7 | 6122 | } else { |
28dbc4b6 | 6123 | parent = mem_cgroup_from_cont(cont->parent); |
c0ff4b85 R |
6124 | memcg->use_hierarchy = parent->use_hierarchy; |
6125 | memcg->oom_kill_disable = parent->oom_kill_disable; | |
18f59ea7 | 6126 | } |
28dbc4b6 | 6127 | |
18f59ea7 | 6128 | if (parent && parent->use_hierarchy) { |
c0ff4b85 R |
6129 | res_counter_init(&memcg->res, &parent->res); |
6130 | res_counter_init(&memcg->memsw, &parent->memsw); | |
510fc4e1 | 6131 | res_counter_init(&memcg->kmem, &parent->kmem); |
55007d84 | 6132 | |
7bcc1bb1 DN |
6133 | /* |
6134 | * We increment refcnt of the parent to ensure that we can | |
6135 | * safely access it on res_counter_charge/uncharge. | |
6136 | * This refcnt will be decremented when freeing this | |
6137 | * mem_cgroup(see mem_cgroup_put). | |
6138 | */ | |
6139 | mem_cgroup_get(parent); | |
18f59ea7 | 6140 | } else { |
c0ff4b85 R |
6141 | res_counter_init(&memcg->res, NULL); |
6142 | res_counter_init(&memcg->memsw, NULL); | |
510fc4e1 | 6143 | res_counter_init(&memcg->kmem, NULL); |
8c7f6edb TH |
6144 | /* |
6145 | * Deeper hierachy with use_hierarchy == false doesn't make | |
6146 | * much sense so let cgroup subsystem know about this | |
6147 | * unfortunate state in our controller. | |
6148 | */ | |
6149 | if (parent && parent != root_mem_cgroup) | |
6150 | mem_cgroup_subsys.broken_hierarchy = true; | |
18f59ea7 | 6151 | } |
c0ff4b85 R |
6152 | memcg->last_scanned_node = MAX_NUMNODES; |
6153 | INIT_LIST_HEAD(&memcg->oom_notify); | |
6d61ef40 | 6154 | |
a7885eb8 | 6155 | if (parent) |
c0ff4b85 R |
6156 | memcg->swappiness = mem_cgroup_swappiness(parent); |
6157 | atomic_set(&memcg->refcnt, 1); | |
6158 | memcg->move_charge_at_immigrate = 0; | |
6159 | mutex_init(&memcg->thresholds_lock); | |
312734c0 | 6160 | spin_lock_init(&memcg->move_lock); |
cbe128e3 GC |
6161 | |
6162 | error = memcg_init_kmem(memcg, &mem_cgroup_subsys); | |
6163 | if (error) { | |
6164 | /* | |
6165 | * We call put now because our (and parent's) refcnts | |
6166 | * are already in place. mem_cgroup_put() will internally | |
6167 | * call __mem_cgroup_free, so return directly | |
6168 | */ | |
6169 | mem_cgroup_put(memcg); | |
6170 | return ERR_PTR(error); | |
6171 | } | |
c0ff4b85 | 6172 | return &memcg->css; |
6d12e2d8 | 6173 | free_out: |
c0ff4b85 | 6174 | __mem_cgroup_free(memcg); |
04046e1a | 6175 | return ERR_PTR(error); |
8cdea7c0 BS |
6176 | } |
6177 | ||
92fb9748 | 6178 | static void mem_cgroup_css_offline(struct cgroup *cont) |
df878fb0 | 6179 | { |
c0ff4b85 | 6180 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
ec64f515 | 6181 | |
ab5196c2 | 6182 | mem_cgroup_reparent_charges(memcg); |
1f458cbf | 6183 | mem_cgroup_destroy_all_caches(memcg); |
df878fb0 KH |
6184 | } |
6185 | ||
92fb9748 | 6186 | static void mem_cgroup_css_free(struct cgroup *cont) |
8cdea7c0 | 6187 | { |
c0ff4b85 | 6188 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
c268e994 | 6189 | |
1d62e436 | 6190 | kmem_cgroup_destroy(memcg); |
d1a4c0b3 | 6191 | |
c0ff4b85 | 6192 | mem_cgroup_put(memcg); |
8cdea7c0 BS |
6193 | } |
6194 | ||
02491447 | 6195 | #ifdef CONFIG_MMU |
7dc74be0 | 6196 | /* Handlers for move charge at task migration. */ |
854ffa8d DN |
6197 | #define PRECHARGE_COUNT_AT_ONCE 256 |
6198 | static int mem_cgroup_do_precharge(unsigned long count) | |
7dc74be0 | 6199 | { |
854ffa8d DN |
6200 | int ret = 0; |
6201 | int batch_count = PRECHARGE_COUNT_AT_ONCE; | |
c0ff4b85 | 6202 | struct mem_cgroup *memcg = mc.to; |
4ffef5fe | 6203 | |
c0ff4b85 | 6204 | if (mem_cgroup_is_root(memcg)) { |
854ffa8d DN |
6205 | mc.precharge += count; |
6206 | /* we don't need css_get for root */ | |
6207 | return ret; | |
6208 | } | |
6209 | /* try to charge at once */ | |
6210 | if (count > 1) { | |
6211 | struct res_counter *dummy; | |
6212 | /* | |
c0ff4b85 | 6213 | * "memcg" cannot be under rmdir() because we've already checked |
854ffa8d DN |
6214 | * by cgroup_lock_live_cgroup() that it is not removed and we |
6215 | * are still under the same cgroup_mutex. So we can postpone | |
6216 | * css_get(). | |
6217 | */ | |
c0ff4b85 | 6218 | if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy)) |
854ffa8d | 6219 | goto one_by_one; |
c0ff4b85 | 6220 | if (do_swap_account && res_counter_charge(&memcg->memsw, |
854ffa8d | 6221 | PAGE_SIZE * count, &dummy)) { |
c0ff4b85 | 6222 | res_counter_uncharge(&memcg->res, PAGE_SIZE * count); |
854ffa8d DN |
6223 | goto one_by_one; |
6224 | } | |
6225 | mc.precharge += count; | |
854ffa8d DN |
6226 | return ret; |
6227 | } | |
6228 | one_by_one: | |
6229 | /* fall back to one by one charge */ | |
6230 | while (count--) { | |
6231 | if (signal_pending(current)) { | |
6232 | ret = -EINTR; | |
6233 | break; | |
6234 | } | |
6235 | if (!batch_count--) { | |
6236 | batch_count = PRECHARGE_COUNT_AT_ONCE; | |
6237 | cond_resched(); | |
6238 | } | |
c0ff4b85 R |
6239 | ret = __mem_cgroup_try_charge(NULL, |
6240 | GFP_KERNEL, 1, &memcg, false); | |
38c5d72f | 6241 | if (ret) |
854ffa8d | 6242 | /* mem_cgroup_clear_mc() will do uncharge later */ |
38c5d72f | 6243 | return ret; |
854ffa8d DN |
6244 | mc.precharge++; |
6245 | } | |
4ffef5fe DN |
6246 | return ret; |
6247 | } | |
6248 | ||
6249 | /** | |
8d32ff84 | 6250 | * get_mctgt_type - get target type of moving charge |
4ffef5fe DN |
6251 | * @vma: the vma the pte to be checked belongs |
6252 | * @addr: the address corresponding to the pte to be checked | |
6253 | * @ptent: the pte to be checked | |
02491447 | 6254 | * @target: the pointer the target page or swap ent will be stored(can be NULL) |
4ffef5fe DN |
6255 | * |
6256 | * Returns | |
6257 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. | |
6258 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for | |
6259 | * move charge. if @target is not NULL, the page is stored in target->page | |
6260 | * with extra refcnt got(Callers should handle it). | |
02491447 DN |
6261 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a |
6262 | * target for charge migration. if @target is not NULL, the entry is stored | |
6263 | * in target->ent. | |
4ffef5fe DN |
6264 | * |
6265 | * Called with pte lock held. | |
6266 | */ | |
4ffef5fe DN |
6267 | union mc_target { |
6268 | struct page *page; | |
02491447 | 6269 | swp_entry_t ent; |
4ffef5fe DN |
6270 | }; |
6271 | ||
4ffef5fe | 6272 | enum mc_target_type { |
8d32ff84 | 6273 | MC_TARGET_NONE = 0, |
4ffef5fe | 6274 | MC_TARGET_PAGE, |
02491447 | 6275 | MC_TARGET_SWAP, |
4ffef5fe DN |
6276 | }; |
6277 | ||
90254a65 DN |
6278 | static struct page *mc_handle_present_pte(struct vm_area_struct *vma, |
6279 | unsigned long addr, pte_t ptent) | |
4ffef5fe | 6280 | { |
90254a65 | 6281 | struct page *page = vm_normal_page(vma, addr, ptent); |
4ffef5fe | 6282 | |
90254a65 DN |
6283 | if (!page || !page_mapped(page)) |
6284 | return NULL; | |
6285 | if (PageAnon(page)) { | |
6286 | /* we don't move shared anon */ | |
4b91355e | 6287 | if (!move_anon()) |
90254a65 | 6288 | return NULL; |
87946a72 DN |
6289 | } else if (!move_file()) |
6290 | /* we ignore mapcount for file pages */ | |
90254a65 DN |
6291 | return NULL; |
6292 | if (!get_page_unless_zero(page)) | |
6293 | return NULL; | |
6294 | ||
6295 | return page; | |
6296 | } | |
6297 | ||
4b91355e | 6298 | #ifdef CONFIG_SWAP |
90254a65 DN |
6299 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
6300 | unsigned long addr, pte_t ptent, swp_entry_t *entry) | |
6301 | { | |
90254a65 DN |
6302 | struct page *page = NULL; |
6303 | swp_entry_t ent = pte_to_swp_entry(ptent); | |
6304 | ||
6305 | if (!move_anon() || non_swap_entry(ent)) | |
6306 | return NULL; | |
4b91355e KH |
6307 | /* |
6308 | * Because lookup_swap_cache() updates some statistics counter, | |
6309 | * we call find_get_page() with swapper_space directly. | |
6310 | */ | |
6311 | page = find_get_page(&swapper_space, ent.val); | |
90254a65 DN |
6312 | if (do_swap_account) |
6313 | entry->val = ent.val; | |
6314 | ||
6315 | return page; | |
6316 | } | |
4b91355e KH |
6317 | #else |
6318 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, | |
6319 | unsigned long addr, pte_t ptent, swp_entry_t *entry) | |
6320 | { | |
6321 | return NULL; | |
6322 | } | |
6323 | #endif | |
90254a65 | 6324 | |
87946a72 DN |
6325 | static struct page *mc_handle_file_pte(struct vm_area_struct *vma, |
6326 | unsigned long addr, pte_t ptent, swp_entry_t *entry) | |
6327 | { | |
6328 | struct page *page = NULL; | |
87946a72 DN |
6329 | struct address_space *mapping; |
6330 | pgoff_t pgoff; | |
6331 | ||
6332 | if (!vma->vm_file) /* anonymous vma */ | |
6333 | return NULL; | |
6334 | if (!move_file()) | |
6335 | return NULL; | |
6336 | ||
87946a72 DN |
6337 | mapping = vma->vm_file->f_mapping; |
6338 | if (pte_none(ptent)) | |
6339 | pgoff = linear_page_index(vma, addr); | |
6340 | else /* pte_file(ptent) is true */ | |
6341 | pgoff = pte_to_pgoff(ptent); | |
6342 | ||
6343 | /* page is moved even if it's not RSS of this task(page-faulted). */ | |
aa3b1895 HD |
6344 | page = find_get_page(mapping, pgoff); |
6345 | ||
6346 | #ifdef CONFIG_SWAP | |
6347 | /* shmem/tmpfs may report page out on swap: account for that too. */ | |
6348 | if (radix_tree_exceptional_entry(page)) { | |
6349 | swp_entry_t swap = radix_to_swp_entry(page); | |
87946a72 | 6350 | if (do_swap_account) |
aa3b1895 HD |
6351 | *entry = swap; |
6352 | page = find_get_page(&swapper_space, swap.val); | |
87946a72 | 6353 | } |
aa3b1895 | 6354 | #endif |
87946a72 DN |
6355 | return page; |
6356 | } | |
6357 | ||
8d32ff84 | 6358 | static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma, |
90254a65 DN |
6359 | unsigned long addr, pte_t ptent, union mc_target *target) |
6360 | { | |
6361 | struct page *page = NULL; | |
6362 | struct page_cgroup *pc; | |
8d32ff84 | 6363 | enum mc_target_type ret = MC_TARGET_NONE; |
90254a65 DN |
6364 | swp_entry_t ent = { .val = 0 }; |
6365 | ||
6366 | if (pte_present(ptent)) | |
6367 | page = mc_handle_present_pte(vma, addr, ptent); | |
6368 | else if (is_swap_pte(ptent)) | |
6369 | page = mc_handle_swap_pte(vma, addr, ptent, &ent); | |
87946a72 DN |
6370 | else if (pte_none(ptent) || pte_file(ptent)) |
6371 | page = mc_handle_file_pte(vma, addr, ptent, &ent); | |
90254a65 DN |
6372 | |
6373 | if (!page && !ent.val) | |
8d32ff84 | 6374 | return ret; |
02491447 DN |
6375 | if (page) { |
6376 | pc = lookup_page_cgroup(page); | |
6377 | /* | |
6378 | * Do only loose check w/o page_cgroup lock. | |
6379 | * mem_cgroup_move_account() checks the pc is valid or not under | |
6380 | * the lock. | |
6381 | */ | |
6382 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { | |
6383 | ret = MC_TARGET_PAGE; | |
6384 | if (target) | |
6385 | target->page = page; | |
6386 | } | |
6387 | if (!ret || !target) | |
6388 | put_page(page); | |
6389 | } | |
90254a65 DN |
6390 | /* There is a swap entry and a page doesn't exist or isn't charged */ |
6391 | if (ent.val && !ret && | |
9fb4b7cc | 6392 | css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) { |
7f0f1546 KH |
6393 | ret = MC_TARGET_SWAP; |
6394 | if (target) | |
6395 | target->ent = ent; | |
4ffef5fe | 6396 | } |
4ffef5fe DN |
6397 | return ret; |
6398 | } | |
6399 | ||
12724850 NH |
6400 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
6401 | /* | |
6402 | * We don't consider swapping or file mapped pages because THP does not | |
6403 | * support them for now. | |
6404 | * Caller should make sure that pmd_trans_huge(pmd) is true. | |
6405 | */ | |
6406 | static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma, | |
6407 | unsigned long addr, pmd_t pmd, union mc_target *target) | |
6408 | { | |
6409 | struct page *page = NULL; | |
6410 | struct page_cgroup *pc; | |
6411 | enum mc_target_type ret = MC_TARGET_NONE; | |
6412 | ||
6413 | page = pmd_page(pmd); | |
6414 | VM_BUG_ON(!page || !PageHead(page)); | |
6415 | if (!move_anon()) | |
6416 | return ret; | |
6417 | pc = lookup_page_cgroup(page); | |
6418 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { | |
6419 | ret = MC_TARGET_PAGE; | |
6420 | if (target) { | |
6421 | get_page(page); | |
6422 | target->page = page; | |
6423 | } | |
6424 | } | |
6425 | return ret; | |
6426 | } | |
6427 | #else | |
6428 | static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma, | |
6429 | unsigned long addr, pmd_t pmd, union mc_target *target) | |
6430 | { | |
6431 | return MC_TARGET_NONE; | |
6432 | } | |
6433 | #endif | |
6434 | ||
4ffef5fe DN |
6435 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, |
6436 | unsigned long addr, unsigned long end, | |
6437 | struct mm_walk *walk) | |
6438 | { | |
6439 | struct vm_area_struct *vma = walk->private; | |
6440 | pte_t *pte; | |
6441 | spinlock_t *ptl; | |
6442 | ||
12724850 NH |
6443 | if (pmd_trans_huge_lock(pmd, vma) == 1) { |
6444 | if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE) | |
6445 | mc.precharge += HPAGE_PMD_NR; | |
6446 | spin_unlock(&vma->vm_mm->page_table_lock); | |
1a5a9906 | 6447 | return 0; |
12724850 | 6448 | } |
03319327 | 6449 | |
45f83cef AA |
6450 | if (pmd_trans_unstable(pmd)) |
6451 | return 0; | |
4ffef5fe DN |
6452 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
6453 | for (; addr != end; pte++, addr += PAGE_SIZE) | |
8d32ff84 | 6454 | if (get_mctgt_type(vma, addr, *pte, NULL)) |
4ffef5fe DN |
6455 | mc.precharge++; /* increment precharge temporarily */ |
6456 | pte_unmap_unlock(pte - 1, ptl); | |
6457 | cond_resched(); | |
6458 | ||
7dc74be0 DN |
6459 | return 0; |
6460 | } | |
6461 | ||
4ffef5fe DN |
6462 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
6463 | { | |
6464 | unsigned long precharge; | |
6465 | struct vm_area_struct *vma; | |
6466 | ||
dfe076b0 | 6467 | down_read(&mm->mmap_sem); |
4ffef5fe DN |
6468 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
6469 | struct mm_walk mem_cgroup_count_precharge_walk = { | |
6470 | .pmd_entry = mem_cgroup_count_precharge_pte_range, | |
6471 | .mm = mm, | |
6472 | .private = vma, | |
6473 | }; | |
6474 | if (is_vm_hugetlb_page(vma)) | |
6475 | continue; | |
4ffef5fe DN |
6476 | walk_page_range(vma->vm_start, vma->vm_end, |
6477 | &mem_cgroup_count_precharge_walk); | |
6478 | } | |
dfe076b0 | 6479 | up_read(&mm->mmap_sem); |
4ffef5fe DN |
6480 | |
6481 | precharge = mc.precharge; | |
6482 | mc.precharge = 0; | |
6483 | ||
6484 | return precharge; | |
6485 | } | |
6486 | ||
4ffef5fe DN |
6487 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
6488 | { | |
dfe076b0 DN |
6489 | unsigned long precharge = mem_cgroup_count_precharge(mm); |
6490 | ||
6491 | VM_BUG_ON(mc.moving_task); | |
6492 | mc.moving_task = current; | |
6493 | return mem_cgroup_do_precharge(precharge); | |
4ffef5fe DN |
6494 | } |
6495 | ||
dfe076b0 DN |
6496 | /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */ |
6497 | static void __mem_cgroup_clear_mc(void) | |
4ffef5fe | 6498 | { |
2bd9bb20 KH |
6499 | struct mem_cgroup *from = mc.from; |
6500 | struct mem_cgroup *to = mc.to; | |
6501 | ||
4ffef5fe | 6502 | /* we must uncharge all the leftover precharges from mc.to */ |
854ffa8d DN |
6503 | if (mc.precharge) { |
6504 | __mem_cgroup_cancel_charge(mc.to, mc.precharge); | |
6505 | mc.precharge = 0; | |
6506 | } | |
6507 | /* | |
6508 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so | |
6509 | * we must uncharge here. | |
6510 | */ | |
6511 | if (mc.moved_charge) { | |
6512 | __mem_cgroup_cancel_charge(mc.from, mc.moved_charge); | |
6513 | mc.moved_charge = 0; | |
4ffef5fe | 6514 | } |
483c30b5 DN |
6515 | /* we must fixup refcnts and charges */ |
6516 | if (mc.moved_swap) { | |
483c30b5 DN |
6517 | /* uncharge swap account from the old cgroup */ |
6518 | if (!mem_cgroup_is_root(mc.from)) | |
6519 | res_counter_uncharge(&mc.from->memsw, | |
6520 | PAGE_SIZE * mc.moved_swap); | |
6521 | __mem_cgroup_put(mc.from, mc.moved_swap); | |
6522 | ||
6523 | if (!mem_cgroup_is_root(mc.to)) { | |
6524 | /* | |
6525 | * we charged both to->res and to->memsw, so we should | |
6526 | * uncharge to->res. | |
6527 | */ | |
6528 | res_counter_uncharge(&mc.to->res, | |
6529 | PAGE_SIZE * mc.moved_swap); | |
483c30b5 DN |
6530 | } |
6531 | /* we've already done mem_cgroup_get(mc.to) */ | |
483c30b5 DN |
6532 | mc.moved_swap = 0; |
6533 | } | |
dfe076b0 DN |
6534 | memcg_oom_recover(from); |
6535 | memcg_oom_recover(to); | |
6536 | wake_up_all(&mc.waitq); | |
6537 | } | |
6538 | ||
6539 | static void mem_cgroup_clear_mc(void) | |
6540 | { | |
6541 | struct mem_cgroup *from = mc.from; | |
6542 | ||
6543 | /* | |
6544 | * we must clear moving_task before waking up waiters at the end of | |
6545 | * task migration. | |
6546 | */ | |
6547 | mc.moving_task = NULL; | |
6548 | __mem_cgroup_clear_mc(); | |
2bd9bb20 | 6549 | spin_lock(&mc.lock); |
4ffef5fe DN |
6550 | mc.from = NULL; |
6551 | mc.to = NULL; | |
2bd9bb20 | 6552 | spin_unlock(&mc.lock); |
32047e2a | 6553 | mem_cgroup_end_move(from); |
4ffef5fe DN |
6554 | } |
6555 | ||
761b3ef5 LZ |
6556 | static int mem_cgroup_can_attach(struct cgroup *cgroup, |
6557 | struct cgroup_taskset *tset) | |
7dc74be0 | 6558 | { |
2f7ee569 | 6559 | struct task_struct *p = cgroup_taskset_first(tset); |
7dc74be0 | 6560 | int ret = 0; |
c0ff4b85 | 6561 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup); |
7dc74be0 | 6562 | |
c0ff4b85 | 6563 | if (memcg->move_charge_at_immigrate) { |
7dc74be0 DN |
6564 | struct mm_struct *mm; |
6565 | struct mem_cgroup *from = mem_cgroup_from_task(p); | |
6566 | ||
c0ff4b85 | 6567 | VM_BUG_ON(from == memcg); |
7dc74be0 DN |
6568 | |
6569 | mm = get_task_mm(p); | |
6570 | if (!mm) | |
6571 | return 0; | |
7dc74be0 | 6572 | /* We move charges only when we move a owner of the mm */ |
4ffef5fe DN |
6573 | if (mm->owner == p) { |
6574 | VM_BUG_ON(mc.from); | |
6575 | VM_BUG_ON(mc.to); | |
6576 | VM_BUG_ON(mc.precharge); | |
854ffa8d | 6577 | VM_BUG_ON(mc.moved_charge); |
483c30b5 | 6578 | VM_BUG_ON(mc.moved_swap); |
32047e2a | 6579 | mem_cgroup_start_move(from); |
2bd9bb20 | 6580 | spin_lock(&mc.lock); |
4ffef5fe | 6581 | mc.from = from; |
c0ff4b85 | 6582 | mc.to = memcg; |
2bd9bb20 | 6583 | spin_unlock(&mc.lock); |
dfe076b0 | 6584 | /* We set mc.moving_task later */ |
4ffef5fe DN |
6585 | |
6586 | ret = mem_cgroup_precharge_mc(mm); | |
6587 | if (ret) | |
6588 | mem_cgroup_clear_mc(); | |
dfe076b0 DN |
6589 | } |
6590 | mmput(mm); | |
7dc74be0 DN |
6591 | } |
6592 | return ret; | |
6593 | } | |
6594 | ||
761b3ef5 LZ |
6595 | static void mem_cgroup_cancel_attach(struct cgroup *cgroup, |
6596 | struct cgroup_taskset *tset) | |
7dc74be0 | 6597 | { |
4ffef5fe | 6598 | mem_cgroup_clear_mc(); |
7dc74be0 DN |
6599 | } |
6600 | ||
4ffef5fe DN |
6601 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
6602 | unsigned long addr, unsigned long end, | |
6603 | struct mm_walk *walk) | |
7dc74be0 | 6604 | { |
4ffef5fe DN |
6605 | int ret = 0; |
6606 | struct vm_area_struct *vma = walk->private; | |
6607 | pte_t *pte; | |
6608 | spinlock_t *ptl; | |
12724850 NH |
6609 | enum mc_target_type target_type; |
6610 | union mc_target target; | |
6611 | struct page *page; | |
6612 | struct page_cgroup *pc; | |
4ffef5fe | 6613 | |
12724850 NH |
6614 | /* |
6615 | * We don't take compound_lock() here but no race with splitting thp | |
6616 | * happens because: | |
6617 | * - if pmd_trans_huge_lock() returns 1, the relevant thp is not | |
6618 | * under splitting, which means there's no concurrent thp split, | |
6619 | * - if another thread runs into split_huge_page() just after we | |
6620 | * entered this if-block, the thread must wait for page table lock | |
6621 | * to be unlocked in __split_huge_page_splitting(), where the main | |
6622 | * part of thp split is not executed yet. | |
6623 | */ | |
6624 | if (pmd_trans_huge_lock(pmd, vma) == 1) { | |
62ade86a | 6625 | if (mc.precharge < HPAGE_PMD_NR) { |
12724850 NH |
6626 | spin_unlock(&vma->vm_mm->page_table_lock); |
6627 | return 0; | |
6628 | } | |
6629 | target_type = get_mctgt_type_thp(vma, addr, *pmd, &target); | |
6630 | if (target_type == MC_TARGET_PAGE) { | |
6631 | page = target.page; | |
6632 | if (!isolate_lru_page(page)) { | |
6633 | pc = lookup_page_cgroup(page); | |
6634 | if (!mem_cgroup_move_account(page, HPAGE_PMD_NR, | |
2f3479b1 | 6635 | pc, mc.from, mc.to)) { |
12724850 NH |
6636 | mc.precharge -= HPAGE_PMD_NR; |
6637 | mc.moved_charge += HPAGE_PMD_NR; | |
6638 | } | |
6639 | putback_lru_page(page); | |
6640 | } | |
6641 | put_page(page); | |
6642 | } | |
6643 | spin_unlock(&vma->vm_mm->page_table_lock); | |
1a5a9906 | 6644 | return 0; |
12724850 NH |
6645 | } |
6646 | ||
45f83cef AA |
6647 | if (pmd_trans_unstable(pmd)) |
6648 | return 0; | |
4ffef5fe DN |
6649 | retry: |
6650 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); | |
6651 | for (; addr != end; addr += PAGE_SIZE) { | |
6652 | pte_t ptent = *(pte++); | |
02491447 | 6653 | swp_entry_t ent; |
4ffef5fe DN |
6654 | |
6655 | if (!mc.precharge) | |
6656 | break; | |
6657 | ||
8d32ff84 | 6658 | switch (get_mctgt_type(vma, addr, ptent, &target)) { |
4ffef5fe DN |
6659 | case MC_TARGET_PAGE: |
6660 | page = target.page; | |
6661 | if (isolate_lru_page(page)) | |
6662 | goto put; | |
6663 | pc = lookup_page_cgroup(page); | |
7ec99d62 | 6664 | if (!mem_cgroup_move_account(page, 1, pc, |
2f3479b1 | 6665 | mc.from, mc.to)) { |
4ffef5fe | 6666 | mc.precharge--; |
854ffa8d DN |
6667 | /* we uncharge from mc.from later. */ |
6668 | mc.moved_charge++; | |
4ffef5fe DN |
6669 | } |
6670 | putback_lru_page(page); | |
8d32ff84 | 6671 | put: /* get_mctgt_type() gets the page */ |
4ffef5fe DN |
6672 | put_page(page); |
6673 | break; | |
02491447 DN |
6674 | case MC_TARGET_SWAP: |
6675 | ent = target.ent; | |
e91cbb42 | 6676 | if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) { |
02491447 | 6677 | mc.precharge--; |
483c30b5 DN |
6678 | /* we fixup refcnts and charges later. */ |
6679 | mc.moved_swap++; | |
6680 | } | |
02491447 | 6681 | break; |
4ffef5fe DN |
6682 | default: |
6683 | break; | |
6684 | } | |
6685 | } | |
6686 | pte_unmap_unlock(pte - 1, ptl); | |
6687 | cond_resched(); | |
6688 | ||
6689 | if (addr != end) { | |
6690 | /* | |
6691 | * We have consumed all precharges we got in can_attach(). | |
6692 | * We try charge one by one, but don't do any additional | |
6693 | * charges to mc.to if we have failed in charge once in attach() | |
6694 | * phase. | |
6695 | */ | |
854ffa8d | 6696 | ret = mem_cgroup_do_precharge(1); |
4ffef5fe DN |
6697 | if (!ret) |
6698 | goto retry; | |
6699 | } | |
6700 | ||
6701 | return ret; | |
6702 | } | |
6703 | ||
6704 | static void mem_cgroup_move_charge(struct mm_struct *mm) | |
6705 | { | |
6706 | struct vm_area_struct *vma; | |
6707 | ||
6708 | lru_add_drain_all(); | |
dfe076b0 DN |
6709 | retry: |
6710 | if (unlikely(!down_read_trylock(&mm->mmap_sem))) { | |
6711 | /* | |
6712 | * Someone who are holding the mmap_sem might be waiting in | |
6713 | * waitq. So we cancel all extra charges, wake up all waiters, | |
6714 | * and retry. Because we cancel precharges, we might not be able | |
6715 | * to move enough charges, but moving charge is a best-effort | |
6716 | * feature anyway, so it wouldn't be a big problem. | |
6717 | */ | |
6718 | __mem_cgroup_clear_mc(); | |
6719 | cond_resched(); | |
6720 | goto retry; | |
6721 | } | |
4ffef5fe DN |
6722 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
6723 | int ret; | |
6724 | struct mm_walk mem_cgroup_move_charge_walk = { | |
6725 | .pmd_entry = mem_cgroup_move_charge_pte_range, | |
6726 | .mm = mm, | |
6727 | .private = vma, | |
6728 | }; | |
6729 | if (is_vm_hugetlb_page(vma)) | |
6730 | continue; | |
4ffef5fe DN |
6731 | ret = walk_page_range(vma->vm_start, vma->vm_end, |
6732 | &mem_cgroup_move_charge_walk); | |
6733 | if (ret) | |
6734 | /* | |
6735 | * means we have consumed all precharges and failed in | |
6736 | * doing additional charge. Just abandon here. | |
6737 | */ | |
6738 | break; | |
6739 | } | |
dfe076b0 | 6740 | up_read(&mm->mmap_sem); |
7dc74be0 DN |
6741 | } |
6742 | ||
761b3ef5 LZ |
6743 | static void mem_cgroup_move_task(struct cgroup *cont, |
6744 | struct cgroup_taskset *tset) | |
67e465a7 | 6745 | { |
2f7ee569 | 6746 | struct task_struct *p = cgroup_taskset_first(tset); |
a433658c | 6747 | struct mm_struct *mm = get_task_mm(p); |
dfe076b0 | 6748 | |
dfe076b0 | 6749 | if (mm) { |
a433658c KM |
6750 | if (mc.to) |
6751 | mem_cgroup_move_charge(mm); | |
dfe076b0 DN |
6752 | mmput(mm); |
6753 | } | |
a433658c KM |
6754 | if (mc.to) |
6755 | mem_cgroup_clear_mc(); | |
67e465a7 | 6756 | } |
5cfb80a7 | 6757 | #else /* !CONFIG_MMU */ |
761b3ef5 LZ |
6758 | static int mem_cgroup_can_attach(struct cgroup *cgroup, |
6759 | struct cgroup_taskset *tset) | |
5cfb80a7 DN |
6760 | { |
6761 | return 0; | |
6762 | } | |
761b3ef5 LZ |
6763 | static void mem_cgroup_cancel_attach(struct cgroup *cgroup, |
6764 | struct cgroup_taskset *tset) | |
5cfb80a7 DN |
6765 | { |
6766 | } | |
761b3ef5 LZ |
6767 | static void mem_cgroup_move_task(struct cgroup *cont, |
6768 | struct cgroup_taskset *tset) | |
5cfb80a7 DN |
6769 | { |
6770 | } | |
6771 | #endif | |
67e465a7 | 6772 | |
8cdea7c0 BS |
6773 | struct cgroup_subsys mem_cgroup_subsys = { |
6774 | .name = "memory", | |
6775 | .subsys_id = mem_cgroup_subsys_id, | |
92fb9748 TH |
6776 | .css_alloc = mem_cgroup_css_alloc, |
6777 | .css_offline = mem_cgroup_css_offline, | |
6778 | .css_free = mem_cgroup_css_free, | |
7dc74be0 DN |
6779 | .can_attach = mem_cgroup_can_attach, |
6780 | .cancel_attach = mem_cgroup_cancel_attach, | |
67e465a7 | 6781 | .attach = mem_cgroup_move_task, |
6bc10349 | 6782 | .base_cftypes = mem_cgroup_files, |
6d12e2d8 | 6783 | .early_init = 0, |
04046e1a | 6784 | .use_id = 1, |
8cdea7c0 | 6785 | }; |
c077719b | 6786 | |
154b454e TH |
6787 | /* |
6788 | * The rest of init is performed during ->css_alloc() for root css which | |
6789 | * happens before initcalls. hotcpu_notifier() can't be done together as | |
6790 | * it would introduce circular locking by adding cgroup_lock -> cpu hotplug | |
6791 | * dependency. Do it from a subsys_initcall(). | |
6792 | */ | |
6793 | static int __init mem_cgroup_init(void) | |
6794 | { | |
6795 | hotcpu_notifier(memcg_cpu_hotplug_callback, 0); | |
6796 | return 0; | |
6797 | } | |
6798 | subsys_initcall(mem_cgroup_init); | |
6799 | ||
c255a458 | 6800 | #ifdef CONFIG_MEMCG_SWAP |
a42c390c MH |
6801 | static int __init enable_swap_account(char *s) |
6802 | { | |
6803 | /* consider enabled if no parameter or 1 is given */ | |
a2c8990a | 6804 | if (!strcmp(s, "1")) |
a42c390c | 6805 | really_do_swap_account = 1; |
a2c8990a | 6806 | else if (!strcmp(s, "0")) |
a42c390c MH |
6807 | really_do_swap_account = 0; |
6808 | return 1; | |
6809 | } | |
a2c8990a | 6810 | __setup("swapaccount=", enable_swap_account); |
c077719b | 6811 | |
c077719b | 6812 | #endif |