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