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