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