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