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