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