]>
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 | * | |
1575e68b JW |
17 | * Native page reclaim |
18 | * Charge lifetime sanitation | |
19 | * Lockless page tracking & accounting | |
20 | * Unified hierarchy configuration model | |
21 | * Copyright (C) 2015 Red Hat, Inc., Johannes Weiner | |
22 | * | |
8cdea7c0 BS |
23 | * This program is free software; you can redistribute it and/or modify |
24 | * it under the terms of the GNU General Public License as published by | |
25 | * the Free Software Foundation; either version 2 of the License, or | |
26 | * (at your option) any later version. | |
27 | * | |
28 | * This program is distributed in the hope that it will be useful, | |
29 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
30 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
31 | * GNU General Public License for more details. | |
32 | */ | |
33 | ||
3e32cb2e | 34 | #include <linux/page_counter.h> |
8cdea7c0 BS |
35 | #include <linux/memcontrol.h> |
36 | #include <linux/cgroup.h> | |
78fb7466 | 37 | #include <linux/mm.h> |
6e84f315 | 38 | #include <linux/sched/mm.h> |
3a4f8a0b | 39 | #include <linux/shmem_fs.h> |
4ffef5fe | 40 | #include <linux/hugetlb.h> |
d13d1443 | 41 | #include <linux/pagemap.h> |
d52aa412 | 42 | #include <linux/smp.h> |
8a9f3ccd | 43 | #include <linux/page-flags.h> |
66e1707b | 44 | #include <linux/backing-dev.h> |
8a9f3ccd BS |
45 | #include <linux/bit_spinlock.h> |
46 | #include <linux/rcupdate.h> | |
e222432b | 47 | #include <linux/limits.h> |
b9e15baf | 48 | #include <linux/export.h> |
8c7c6e34 | 49 | #include <linux/mutex.h> |
bb4cc1a8 | 50 | #include <linux/rbtree.h> |
b6ac57d5 | 51 | #include <linux/slab.h> |
66e1707b | 52 | #include <linux/swap.h> |
02491447 | 53 | #include <linux/swapops.h> |
66e1707b | 54 | #include <linux/spinlock.h> |
2e72b634 | 55 | #include <linux/eventfd.h> |
79bd9814 | 56 | #include <linux/poll.h> |
2e72b634 | 57 | #include <linux/sort.h> |
66e1707b | 58 | #include <linux/fs.h> |
d2ceb9b7 | 59 | #include <linux/seq_file.h> |
70ddf637 | 60 | #include <linux/vmpressure.h> |
b69408e8 | 61 | #include <linux/mm_inline.h> |
5d1ea48b | 62 | #include <linux/swap_cgroup.h> |
cdec2e42 | 63 | #include <linux/cpu.h> |
158e0a2d | 64 | #include <linux/oom.h> |
0056f4e6 | 65 | #include <linux/lockdep.h> |
79bd9814 | 66 | #include <linux/file.h> |
b23afb93 | 67 | #include <linux/tracehook.h> |
08e552c6 | 68 | #include "internal.h" |
d1a4c0b3 | 69 | #include <net/sock.h> |
4bd2c1ee | 70 | #include <net/ip.h> |
f35c3a8e | 71 | #include "slab.h" |
8cdea7c0 | 72 | |
7c0f6ba6 | 73 | #include <linux/uaccess.h> |
8697d331 | 74 | |
cc8e970c KM |
75 | #include <trace/events/vmscan.h> |
76 | ||
073219e9 TH |
77 | struct cgroup_subsys memory_cgrp_subsys __read_mostly; |
78 | EXPORT_SYMBOL(memory_cgrp_subsys); | |
68ae564b | 79 | |
7d828602 JW |
80 | struct mem_cgroup *root_mem_cgroup __read_mostly; |
81 | ||
a181b0e8 | 82 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
8cdea7c0 | 83 | |
f7e1cb6e JW |
84 | /* Socket memory accounting disabled? */ |
85 | static bool cgroup_memory_nosocket; | |
86 | ||
04823c83 VD |
87 | /* Kernel memory accounting disabled? */ |
88 | static bool cgroup_memory_nokmem; | |
89 | ||
21afa38e | 90 | /* Whether the swap controller is active */ |
c255a458 | 91 | #ifdef CONFIG_MEMCG_SWAP |
c077719b | 92 | int do_swap_account __read_mostly; |
c077719b | 93 | #else |
a0db00fc | 94 | #define do_swap_account 0 |
c077719b KH |
95 | #endif |
96 | ||
7941d214 JW |
97 | /* Whether legacy memory+swap accounting is active */ |
98 | static bool do_memsw_account(void) | |
99 | { | |
100 | return !cgroup_subsys_on_dfl(memory_cgrp_subsys) && do_swap_account; | |
101 | } | |
102 | ||
71cd3113 | 103 | static const char *const mem_cgroup_lru_names[] = { |
58cf188e SZ |
104 | "inactive_anon", |
105 | "active_anon", | |
106 | "inactive_file", | |
107 | "active_file", | |
108 | "unevictable", | |
109 | }; | |
110 | ||
a0db00fc KS |
111 | #define THRESHOLDS_EVENTS_TARGET 128 |
112 | #define SOFTLIMIT_EVENTS_TARGET 1024 | |
113 | #define NUMAINFO_EVENTS_TARGET 1024 | |
e9f8974f | 114 | |
bb4cc1a8 AM |
115 | /* |
116 | * Cgroups above their limits are maintained in a RB-Tree, independent of | |
117 | * their hierarchy representation | |
118 | */ | |
119 | ||
ef8f2327 | 120 | struct mem_cgroup_tree_per_node { |
bb4cc1a8 | 121 | struct rb_root rb_root; |
fa90b2fd | 122 | struct rb_node *rb_rightmost; |
bb4cc1a8 AM |
123 | spinlock_t lock; |
124 | }; | |
125 | ||
bb4cc1a8 AM |
126 | struct mem_cgroup_tree { |
127 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; | |
128 | }; | |
129 | ||
130 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; | |
131 | ||
9490ff27 KH |
132 | /* for OOM */ |
133 | struct mem_cgroup_eventfd_list { | |
134 | struct list_head list; | |
135 | struct eventfd_ctx *eventfd; | |
136 | }; | |
2e72b634 | 137 | |
79bd9814 TH |
138 | /* |
139 | * cgroup_event represents events which userspace want to receive. | |
140 | */ | |
3bc942f3 | 141 | struct mem_cgroup_event { |
79bd9814 | 142 | /* |
59b6f873 | 143 | * memcg which the event belongs to. |
79bd9814 | 144 | */ |
59b6f873 | 145 | struct mem_cgroup *memcg; |
79bd9814 TH |
146 | /* |
147 | * eventfd to signal userspace about the event. | |
148 | */ | |
149 | struct eventfd_ctx *eventfd; | |
150 | /* | |
151 | * Each of these stored in a list by the cgroup. | |
152 | */ | |
153 | struct list_head list; | |
fba94807 TH |
154 | /* |
155 | * register_event() callback will be used to add new userspace | |
156 | * waiter for changes related to this event. Use eventfd_signal() | |
157 | * on eventfd to send notification to userspace. | |
158 | */ | |
59b6f873 | 159 | int (*register_event)(struct mem_cgroup *memcg, |
347c4a87 | 160 | struct eventfd_ctx *eventfd, const char *args); |
fba94807 TH |
161 | /* |
162 | * unregister_event() callback will be called when userspace closes | |
163 | * the eventfd or on cgroup removing. This callback must be set, | |
164 | * if you want provide notification functionality. | |
165 | */ | |
59b6f873 | 166 | void (*unregister_event)(struct mem_cgroup *memcg, |
fba94807 | 167 | struct eventfd_ctx *eventfd); |
79bd9814 TH |
168 | /* |
169 | * All fields below needed to unregister event when | |
170 | * userspace closes eventfd. | |
171 | */ | |
172 | poll_table pt; | |
173 | wait_queue_head_t *wqh; | |
ac6424b9 | 174 | wait_queue_entry_t wait; |
79bd9814 TH |
175 | struct work_struct remove; |
176 | }; | |
177 | ||
c0ff4b85 R |
178 | static void mem_cgroup_threshold(struct mem_cgroup *memcg); |
179 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg); | |
2e72b634 | 180 | |
7dc74be0 DN |
181 | /* Stuffs for move charges at task migration. */ |
182 | /* | |
1dfab5ab | 183 | * Types of charges to be moved. |
7dc74be0 | 184 | */ |
1dfab5ab JW |
185 | #define MOVE_ANON 0x1U |
186 | #define MOVE_FILE 0x2U | |
187 | #define MOVE_MASK (MOVE_ANON | MOVE_FILE) | |
7dc74be0 | 188 | |
4ffef5fe DN |
189 | /* "mc" and its members are protected by cgroup_mutex */ |
190 | static struct move_charge_struct { | |
b1dd693e | 191 | spinlock_t lock; /* for from, to */ |
264a0ae1 | 192 | struct mm_struct *mm; |
4ffef5fe DN |
193 | struct mem_cgroup *from; |
194 | struct mem_cgroup *to; | |
1dfab5ab | 195 | unsigned long flags; |
4ffef5fe | 196 | unsigned long precharge; |
854ffa8d | 197 | unsigned long moved_charge; |
483c30b5 | 198 | unsigned long moved_swap; |
8033b97c DN |
199 | struct task_struct *moving_task; /* a task moving charges */ |
200 | wait_queue_head_t waitq; /* a waitq for other context */ | |
201 | } mc = { | |
2bd9bb20 | 202 | .lock = __SPIN_LOCK_UNLOCKED(mc.lock), |
8033b97c DN |
203 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), |
204 | }; | |
4ffef5fe | 205 | |
4e416953 BS |
206 | /* |
207 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft | |
208 | * limit reclaim to prevent infinite loops, if they ever occur. | |
209 | */ | |
a0db00fc | 210 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS 100 |
bb4cc1a8 | 211 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2 |
4e416953 | 212 | |
217bc319 KH |
213 | enum charge_type { |
214 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, | |
41326c17 | 215 | MEM_CGROUP_CHARGE_TYPE_ANON, |
d13d1443 | 216 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
8a9478ca | 217 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
c05555b5 KH |
218 | NR_CHARGE_TYPE, |
219 | }; | |
220 | ||
8c7c6e34 | 221 | /* for encoding cft->private value on file */ |
86ae53e1 GC |
222 | enum res_type { |
223 | _MEM, | |
224 | _MEMSWAP, | |
225 | _OOM_TYPE, | |
510fc4e1 | 226 | _KMEM, |
d55f90bf | 227 | _TCP, |
86ae53e1 GC |
228 | }; |
229 | ||
a0db00fc KS |
230 | #define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val)) |
231 | #define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff) | |
8c7c6e34 | 232 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
9490ff27 KH |
233 | /* Used for OOM nofiier */ |
234 | #define OOM_CONTROL (0) | |
8c7c6e34 | 235 | |
70ddf637 AV |
236 | /* Some nice accessors for the vmpressure. */ |
237 | struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg) | |
238 | { | |
239 | if (!memcg) | |
240 | memcg = root_mem_cgroup; | |
241 | return &memcg->vmpressure; | |
242 | } | |
243 | ||
244 | struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr) | |
245 | { | |
246 | return &container_of(vmpr, struct mem_cgroup, vmpressure)->css; | |
247 | } | |
248 | ||
7ffc0edc MH |
249 | static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) |
250 | { | |
251 | return (memcg == root_mem_cgroup); | |
252 | } | |
253 | ||
127424c8 | 254 | #ifndef CONFIG_SLOB |
55007d84 | 255 | /* |
f7ce3190 | 256 | * This will be the memcg's index in each cache's ->memcg_params.memcg_caches. |
b8627835 LZ |
257 | * The main reason for not using cgroup id for this: |
258 | * this works better in sparse environments, where we have a lot of memcgs, | |
259 | * but only a few kmem-limited. Or also, if we have, for instance, 200 | |
260 | * memcgs, and none but the 200th is kmem-limited, we'd have to have a | |
261 | * 200 entry array for that. | |
55007d84 | 262 | * |
dbcf73e2 VD |
263 | * The current size of the caches array is stored in memcg_nr_cache_ids. It |
264 | * will double each time we have to increase it. | |
55007d84 | 265 | */ |
dbcf73e2 VD |
266 | static DEFINE_IDA(memcg_cache_ida); |
267 | int memcg_nr_cache_ids; | |
749c5415 | 268 | |
05257a1a VD |
269 | /* Protects memcg_nr_cache_ids */ |
270 | static DECLARE_RWSEM(memcg_cache_ids_sem); | |
271 | ||
272 | void memcg_get_cache_ids(void) | |
273 | { | |
274 | down_read(&memcg_cache_ids_sem); | |
275 | } | |
276 | ||
277 | void memcg_put_cache_ids(void) | |
278 | { | |
279 | up_read(&memcg_cache_ids_sem); | |
280 | } | |
281 | ||
55007d84 GC |
282 | /* |
283 | * MIN_SIZE is different than 1, because we would like to avoid going through | |
284 | * the alloc/free process all the time. In a small machine, 4 kmem-limited | |
285 | * cgroups is a reasonable guess. In the future, it could be a parameter or | |
286 | * tunable, but that is strictly not necessary. | |
287 | * | |
b8627835 | 288 | * MAX_SIZE should be as large as the number of cgrp_ids. Ideally, we could get |
55007d84 GC |
289 | * this constant directly from cgroup, but it is understandable that this is |
290 | * better kept as an internal representation in cgroup.c. In any case, the | |
b8627835 | 291 | * cgrp_id space is not getting any smaller, and we don't have to necessarily |
55007d84 GC |
292 | * increase ours as well if it increases. |
293 | */ | |
294 | #define MEMCG_CACHES_MIN_SIZE 4 | |
b8627835 | 295 | #define MEMCG_CACHES_MAX_SIZE MEM_CGROUP_ID_MAX |
55007d84 | 296 | |
d7f25f8a GC |
297 | /* |
298 | * A lot of the calls to the cache allocation functions are expected to be | |
299 | * inlined by the compiler. Since the calls to memcg_kmem_get_cache are | |
300 | * conditional to this static branch, we'll have to allow modules that does | |
301 | * kmem_cache_alloc and the such to see this symbol as well | |
302 | */ | |
ef12947c | 303 | DEFINE_STATIC_KEY_FALSE(memcg_kmem_enabled_key); |
d7f25f8a | 304 | EXPORT_SYMBOL(memcg_kmem_enabled_key); |
a8964b9b | 305 | |
17cc4dfe TH |
306 | struct workqueue_struct *memcg_kmem_cache_wq; |
307 | ||
127424c8 | 308 | #endif /* !CONFIG_SLOB */ |
a8964b9b | 309 | |
ad7fa852 TH |
310 | /** |
311 | * mem_cgroup_css_from_page - css of the memcg associated with a page | |
312 | * @page: page of interest | |
313 | * | |
314 | * If memcg is bound to the default hierarchy, css of the memcg associated | |
315 | * with @page is returned. The returned css remains associated with @page | |
316 | * until it is released. | |
317 | * | |
318 | * If memcg is bound to a traditional hierarchy, the css of root_mem_cgroup | |
319 | * is returned. | |
ad7fa852 TH |
320 | */ |
321 | struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page) | |
322 | { | |
323 | struct mem_cgroup *memcg; | |
324 | ||
ad7fa852 TH |
325 | memcg = page->mem_cgroup; |
326 | ||
9e10a130 | 327 | if (!memcg || !cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
ad7fa852 TH |
328 | memcg = root_mem_cgroup; |
329 | ||
ad7fa852 TH |
330 | return &memcg->css; |
331 | } | |
332 | ||
2fc04524 VD |
333 | /** |
334 | * page_cgroup_ino - return inode number of the memcg a page is charged to | |
335 | * @page: the page | |
336 | * | |
337 | * Look up the closest online ancestor of the memory cgroup @page is charged to | |
338 | * and return its inode number or 0 if @page is not charged to any cgroup. It | |
339 | * is safe to call this function without holding a reference to @page. | |
340 | * | |
341 | * Note, this function is inherently racy, because there is nothing to prevent | |
342 | * the cgroup inode from getting torn down and potentially reallocated a moment | |
343 | * after page_cgroup_ino() returns, so it only should be used by callers that | |
344 | * do not care (such as procfs interfaces). | |
345 | */ | |
346 | ino_t page_cgroup_ino(struct page *page) | |
347 | { | |
348 | struct mem_cgroup *memcg; | |
349 | unsigned long ino = 0; | |
350 | ||
351 | rcu_read_lock(); | |
352 | memcg = READ_ONCE(page->mem_cgroup); | |
353 | while (memcg && !(memcg->css.flags & CSS_ONLINE)) | |
354 | memcg = parent_mem_cgroup(memcg); | |
355 | if (memcg) | |
356 | ino = cgroup_ino(memcg->css.cgroup); | |
357 | rcu_read_unlock(); | |
358 | return ino; | |
359 | } | |
360 | ||
ef8f2327 MG |
361 | static struct mem_cgroup_per_node * |
362 | mem_cgroup_page_nodeinfo(struct mem_cgroup *memcg, struct page *page) | |
f64c3f54 | 363 | { |
97a6c37b | 364 | int nid = page_to_nid(page); |
f64c3f54 | 365 | |
ef8f2327 | 366 | return memcg->nodeinfo[nid]; |
f64c3f54 BS |
367 | } |
368 | ||
ef8f2327 MG |
369 | static struct mem_cgroup_tree_per_node * |
370 | soft_limit_tree_node(int nid) | |
bb4cc1a8 | 371 | { |
ef8f2327 | 372 | return soft_limit_tree.rb_tree_per_node[nid]; |
bb4cc1a8 AM |
373 | } |
374 | ||
ef8f2327 | 375 | static struct mem_cgroup_tree_per_node * |
bb4cc1a8 AM |
376 | soft_limit_tree_from_page(struct page *page) |
377 | { | |
378 | int nid = page_to_nid(page); | |
bb4cc1a8 | 379 | |
ef8f2327 | 380 | return soft_limit_tree.rb_tree_per_node[nid]; |
bb4cc1a8 AM |
381 | } |
382 | ||
ef8f2327 MG |
383 | static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_node *mz, |
384 | struct mem_cgroup_tree_per_node *mctz, | |
3e32cb2e | 385 | unsigned long new_usage_in_excess) |
bb4cc1a8 AM |
386 | { |
387 | struct rb_node **p = &mctz->rb_root.rb_node; | |
388 | struct rb_node *parent = NULL; | |
ef8f2327 | 389 | struct mem_cgroup_per_node *mz_node; |
fa90b2fd | 390 | bool rightmost = true; |
bb4cc1a8 AM |
391 | |
392 | if (mz->on_tree) | |
393 | return; | |
394 | ||
395 | mz->usage_in_excess = new_usage_in_excess; | |
396 | if (!mz->usage_in_excess) | |
397 | return; | |
398 | while (*p) { | |
399 | parent = *p; | |
ef8f2327 | 400 | mz_node = rb_entry(parent, struct mem_cgroup_per_node, |
bb4cc1a8 | 401 | tree_node); |
fa90b2fd | 402 | if (mz->usage_in_excess < mz_node->usage_in_excess) { |
bb4cc1a8 | 403 | p = &(*p)->rb_left; |
fa90b2fd DB |
404 | rightmost = false; |
405 | } | |
406 | ||
bb4cc1a8 AM |
407 | /* |
408 | * We can't avoid mem cgroups that are over their soft | |
409 | * limit by the same amount | |
410 | */ | |
411 | else if (mz->usage_in_excess >= mz_node->usage_in_excess) | |
412 | p = &(*p)->rb_right; | |
413 | } | |
fa90b2fd DB |
414 | |
415 | if (rightmost) | |
416 | mctz->rb_rightmost = &mz->tree_node; | |
417 | ||
bb4cc1a8 AM |
418 | rb_link_node(&mz->tree_node, parent, p); |
419 | rb_insert_color(&mz->tree_node, &mctz->rb_root); | |
420 | mz->on_tree = true; | |
421 | } | |
422 | ||
ef8f2327 MG |
423 | static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz, |
424 | struct mem_cgroup_tree_per_node *mctz) | |
bb4cc1a8 AM |
425 | { |
426 | if (!mz->on_tree) | |
427 | return; | |
fa90b2fd DB |
428 | |
429 | if (&mz->tree_node == mctz->rb_rightmost) | |
430 | mctz->rb_rightmost = rb_prev(&mz->tree_node); | |
431 | ||
bb4cc1a8 AM |
432 | rb_erase(&mz->tree_node, &mctz->rb_root); |
433 | mz->on_tree = false; | |
434 | } | |
435 | ||
ef8f2327 MG |
436 | static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz, |
437 | struct mem_cgroup_tree_per_node *mctz) | |
bb4cc1a8 | 438 | { |
0a31bc97 JW |
439 | unsigned long flags; |
440 | ||
441 | spin_lock_irqsave(&mctz->lock, flags); | |
cf2c8127 | 442 | __mem_cgroup_remove_exceeded(mz, mctz); |
0a31bc97 | 443 | spin_unlock_irqrestore(&mctz->lock, flags); |
bb4cc1a8 AM |
444 | } |
445 | ||
3e32cb2e JW |
446 | static unsigned long soft_limit_excess(struct mem_cgroup *memcg) |
447 | { | |
448 | unsigned long nr_pages = page_counter_read(&memcg->memory); | |
4db0c3c2 | 449 | unsigned long soft_limit = READ_ONCE(memcg->soft_limit); |
3e32cb2e JW |
450 | unsigned long excess = 0; |
451 | ||
452 | if (nr_pages > soft_limit) | |
453 | excess = nr_pages - soft_limit; | |
454 | ||
455 | return excess; | |
456 | } | |
bb4cc1a8 AM |
457 | |
458 | static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page) | |
459 | { | |
3e32cb2e | 460 | unsigned long excess; |
ef8f2327 MG |
461 | struct mem_cgroup_per_node *mz; |
462 | struct mem_cgroup_tree_per_node *mctz; | |
bb4cc1a8 | 463 | |
e231875b | 464 | mctz = soft_limit_tree_from_page(page); |
bfc7228b LD |
465 | if (!mctz) |
466 | return; | |
bb4cc1a8 AM |
467 | /* |
468 | * Necessary to update all ancestors when hierarchy is used. | |
469 | * because their event counter is not touched. | |
470 | */ | |
471 | for (; memcg; memcg = parent_mem_cgroup(memcg)) { | |
ef8f2327 | 472 | mz = mem_cgroup_page_nodeinfo(memcg, page); |
3e32cb2e | 473 | excess = soft_limit_excess(memcg); |
bb4cc1a8 AM |
474 | /* |
475 | * We have to update the tree if mz is on RB-tree or | |
476 | * mem is over its softlimit. | |
477 | */ | |
478 | if (excess || mz->on_tree) { | |
0a31bc97 JW |
479 | unsigned long flags; |
480 | ||
481 | spin_lock_irqsave(&mctz->lock, flags); | |
bb4cc1a8 AM |
482 | /* if on-tree, remove it */ |
483 | if (mz->on_tree) | |
cf2c8127 | 484 | __mem_cgroup_remove_exceeded(mz, mctz); |
bb4cc1a8 AM |
485 | /* |
486 | * Insert again. mz->usage_in_excess will be updated. | |
487 | * If excess is 0, no tree ops. | |
488 | */ | |
cf2c8127 | 489 | __mem_cgroup_insert_exceeded(mz, mctz, excess); |
0a31bc97 | 490 | spin_unlock_irqrestore(&mctz->lock, flags); |
bb4cc1a8 AM |
491 | } |
492 | } | |
493 | } | |
494 | ||
495 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg) | |
496 | { | |
ef8f2327 MG |
497 | struct mem_cgroup_tree_per_node *mctz; |
498 | struct mem_cgroup_per_node *mz; | |
499 | int nid; | |
bb4cc1a8 | 500 | |
e231875b | 501 | for_each_node(nid) { |
ef8f2327 MG |
502 | mz = mem_cgroup_nodeinfo(memcg, nid); |
503 | mctz = soft_limit_tree_node(nid); | |
bfc7228b LD |
504 | if (mctz) |
505 | mem_cgroup_remove_exceeded(mz, mctz); | |
bb4cc1a8 AM |
506 | } |
507 | } | |
508 | ||
ef8f2327 MG |
509 | static struct mem_cgroup_per_node * |
510 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz) | |
bb4cc1a8 | 511 | { |
ef8f2327 | 512 | struct mem_cgroup_per_node *mz; |
bb4cc1a8 AM |
513 | |
514 | retry: | |
515 | mz = NULL; | |
fa90b2fd | 516 | if (!mctz->rb_rightmost) |
bb4cc1a8 AM |
517 | goto done; /* Nothing to reclaim from */ |
518 | ||
fa90b2fd DB |
519 | mz = rb_entry(mctz->rb_rightmost, |
520 | struct mem_cgroup_per_node, tree_node); | |
bb4cc1a8 AM |
521 | /* |
522 | * Remove the node now but someone else can add it back, | |
523 | * we will to add it back at the end of reclaim to its correct | |
524 | * position in the tree. | |
525 | */ | |
cf2c8127 | 526 | __mem_cgroup_remove_exceeded(mz, mctz); |
3e32cb2e | 527 | if (!soft_limit_excess(mz->memcg) || |
ec903c0c | 528 | !css_tryget_online(&mz->memcg->css)) |
bb4cc1a8 AM |
529 | goto retry; |
530 | done: | |
531 | return mz; | |
532 | } | |
533 | ||
ef8f2327 MG |
534 | static struct mem_cgroup_per_node * |
535 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz) | |
bb4cc1a8 | 536 | { |
ef8f2327 | 537 | struct mem_cgroup_per_node *mz; |
bb4cc1a8 | 538 | |
0a31bc97 | 539 | spin_lock_irq(&mctz->lock); |
bb4cc1a8 | 540 | mz = __mem_cgroup_largest_soft_limit_node(mctz); |
0a31bc97 | 541 | spin_unlock_irq(&mctz->lock); |
bb4cc1a8 AM |
542 | return mz; |
543 | } | |
544 | ||
ccda7f43 | 545 | static unsigned long memcg_sum_events(struct mem_cgroup *memcg, |
04fecbf5 | 546 | int event) |
e9f8974f | 547 | { |
a983b5eb | 548 | return atomic_long_read(&memcg->events[event]); |
e9f8974f JW |
549 | } |
550 | ||
c0ff4b85 | 551 | static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg, |
b070e65c | 552 | struct page *page, |
f627c2f5 | 553 | bool compound, int nr_pages) |
d52aa412 | 554 | { |
b2402857 KH |
555 | /* |
556 | * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is | |
557 | * counted as CACHE even if it's on ANON LRU. | |
558 | */ | |
0a31bc97 | 559 | if (PageAnon(page)) |
c9019e9b | 560 | __mod_memcg_state(memcg, MEMCG_RSS, nr_pages); |
9a4caf1e | 561 | else { |
c9019e9b | 562 | __mod_memcg_state(memcg, MEMCG_CACHE, nr_pages); |
9a4caf1e | 563 | if (PageSwapBacked(page)) |
c9019e9b | 564 | __mod_memcg_state(memcg, NR_SHMEM, nr_pages); |
9a4caf1e | 565 | } |
55e462b0 | 566 | |
f627c2f5 KS |
567 | if (compound) { |
568 | VM_BUG_ON_PAGE(!PageTransHuge(page), page); | |
c9019e9b | 569 | __mod_memcg_state(memcg, MEMCG_RSS_HUGE, nr_pages); |
f627c2f5 | 570 | } |
b070e65c | 571 | |
e401f176 KH |
572 | /* pagein of a big page is an event. So, ignore page size */ |
573 | if (nr_pages > 0) | |
c9019e9b | 574 | __count_memcg_events(memcg, PGPGIN, 1); |
3751d604 | 575 | else { |
c9019e9b | 576 | __count_memcg_events(memcg, PGPGOUT, 1); |
3751d604 KH |
577 | nr_pages = -nr_pages; /* for event */ |
578 | } | |
e401f176 | 579 | |
a983b5eb | 580 | __this_cpu_add(memcg->stat_cpu->nr_page_events, nr_pages); |
6d12e2d8 KH |
581 | } |
582 | ||
0a6b76dd VD |
583 | unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg, |
584 | int nid, unsigned int lru_mask) | |
bb2a0de9 | 585 | { |
b4536f0c | 586 | struct lruvec *lruvec = mem_cgroup_lruvec(NODE_DATA(nid), memcg); |
e231875b | 587 | unsigned long nr = 0; |
ef8f2327 | 588 | enum lru_list lru; |
889976db | 589 | |
e231875b | 590 | VM_BUG_ON((unsigned)nid >= nr_node_ids); |
bb2a0de9 | 591 | |
ef8f2327 MG |
592 | for_each_lru(lru) { |
593 | if (!(BIT(lru) & lru_mask)) | |
594 | continue; | |
b4536f0c | 595 | nr += mem_cgroup_get_lru_size(lruvec, lru); |
e231875b JZ |
596 | } |
597 | return nr; | |
889976db | 598 | } |
bb2a0de9 | 599 | |
c0ff4b85 | 600 | static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg, |
bb2a0de9 | 601 | unsigned int lru_mask) |
6d12e2d8 | 602 | { |
e231875b | 603 | unsigned long nr = 0; |
889976db | 604 | int nid; |
6d12e2d8 | 605 | |
31aaea4a | 606 | for_each_node_state(nid, N_MEMORY) |
e231875b JZ |
607 | nr += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask); |
608 | return nr; | |
d52aa412 KH |
609 | } |
610 | ||
f53d7ce3 JW |
611 | static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg, |
612 | enum mem_cgroup_events_target target) | |
7a159cc9 JW |
613 | { |
614 | unsigned long val, next; | |
615 | ||
a983b5eb JW |
616 | val = __this_cpu_read(memcg->stat_cpu->nr_page_events); |
617 | next = __this_cpu_read(memcg->stat_cpu->targets[target]); | |
7a159cc9 | 618 | /* from time_after() in jiffies.h */ |
6a1a8b80 | 619 | if ((long)(next - val) < 0) { |
f53d7ce3 JW |
620 | switch (target) { |
621 | case MEM_CGROUP_TARGET_THRESH: | |
622 | next = val + THRESHOLDS_EVENTS_TARGET; | |
623 | break; | |
bb4cc1a8 AM |
624 | case MEM_CGROUP_TARGET_SOFTLIMIT: |
625 | next = val + SOFTLIMIT_EVENTS_TARGET; | |
626 | break; | |
f53d7ce3 JW |
627 | case MEM_CGROUP_TARGET_NUMAINFO: |
628 | next = val + NUMAINFO_EVENTS_TARGET; | |
629 | break; | |
630 | default: | |
631 | break; | |
632 | } | |
a983b5eb | 633 | __this_cpu_write(memcg->stat_cpu->targets[target], next); |
f53d7ce3 | 634 | return true; |
7a159cc9 | 635 | } |
f53d7ce3 | 636 | return false; |
d2265e6f KH |
637 | } |
638 | ||
639 | /* | |
640 | * Check events in order. | |
641 | * | |
642 | */ | |
c0ff4b85 | 643 | static void memcg_check_events(struct mem_cgroup *memcg, struct page *page) |
d2265e6f KH |
644 | { |
645 | /* threshold event is triggered in finer grain than soft limit */ | |
f53d7ce3 JW |
646 | if (unlikely(mem_cgroup_event_ratelimit(memcg, |
647 | MEM_CGROUP_TARGET_THRESH))) { | |
bb4cc1a8 | 648 | bool do_softlimit; |
82b3f2a7 | 649 | bool do_numainfo __maybe_unused; |
f53d7ce3 | 650 | |
bb4cc1a8 AM |
651 | do_softlimit = mem_cgroup_event_ratelimit(memcg, |
652 | MEM_CGROUP_TARGET_SOFTLIMIT); | |
f53d7ce3 JW |
653 | #if MAX_NUMNODES > 1 |
654 | do_numainfo = mem_cgroup_event_ratelimit(memcg, | |
655 | MEM_CGROUP_TARGET_NUMAINFO); | |
656 | #endif | |
c0ff4b85 | 657 | mem_cgroup_threshold(memcg); |
bb4cc1a8 AM |
658 | if (unlikely(do_softlimit)) |
659 | mem_cgroup_update_tree(memcg, page); | |
453a9bf3 | 660 | #if MAX_NUMNODES > 1 |
f53d7ce3 | 661 | if (unlikely(do_numainfo)) |
c0ff4b85 | 662 | atomic_inc(&memcg->numainfo_events); |
453a9bf3 | 663 | #endif |
0a31bc97 | 664 | } |
d2265e6f KH |
665 | } |
666 | ||
cf475ad2 | 667 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
78fb7466 | 668 | { |
31a78f23 BS |
669 | /* |
670 | * mm_update_next_owner() may clear mm->owner to NULL | |
671 | * if it races with swapoff, page migration, etc. | |
672 | * So this can be called with p == NULL. | |
673 | */ | |
674 | if (unlikely(!p)) | |
675 | return NULL; | |
676 | ||
073219e9 | 677 | return mem_cgroup_from_css(task_css(p, memory_cgrp_id)); |
78fb7466 | 678 | } |
33398cf2 | 679 | EXPORT_SYMBOL(mem_cgroup_from_task); |
78fb7466 | 680 | |
d46eb14b SB |
681 | /** |
682 | * get_mem_cgroup_from_mm: Obtain a reference on given mm_struct's memcg. | |
683 | * @mm: mm from which memcg should be extracted. It can be NULL. | |
684 | * | |
685 | * Obtain a reference on mm->memcg and returns it if successful. Otherwise | |
686 | * root_mem_cgroup is returned. However if mem_cgroup is disabled, NULL is | |
687 | * returned. | |
688 | */ | |
689 | struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm) | |
54595fe2 | 690 | { |
d46eb14b SB |
691 | struct mem_cgroup *memcg; |
692 | ||
693 | if (mem_cgroup_disabled()) | |
694 | return NULL; | |
0b7f569e | 695 | |
54595fe2 KH |
696 | rcu_read_lock(); |
697 | do { | |
6f6acb00 MH |
698 | /* |
699 | * Page cache insertions can happen withou an | |
700 | * actual mm context, e.g. during disk probing | |
701 | * on boot, loopback IO, acct() writes etc. | |
702 | */ | |
703 | if (unlikely(!mm)) | |
df381975 | 704 | memcg = root_mem_cgroup; |
6f6acb00 MH |
705 | else { |
706 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); | |
707 | if (unlikely(!memcg)) | |
708 | memcg = root_mem_cgroup; | |
709 | } | |
ec903c0c | 710 | } while (!css_tryget_online(&memcg->css)); |
54595fe2 | 711 | rcu_read_unlock(); |
c0ff4b85 | 712 | return memcg; |
54595fe2 | 713 | } |
d46eb14b SB |
714 | EXPORT_SYMBOL(get_mem_cgroup_from_mm); |
715 | ||
716 | /** | |
717 | * If current->active_memcg is non-NULL, do not fallback to current->mm->memcg. | |
718 | */ | |
719 | static __always_inline struct mem_cgroup *get_mem_cgroup_from_current(void) | |
720 | { | |
721 | if (unlikely(current->active_memcg)) { | |
722 | struct mem_cgroup *memcg = root_mem_cgroup; | |
723 | ||
724 | rcu_read_lock(); | |
725 | if (css_tryget_online(¤t->active_memcg->css)) | |
726 | memcg = current->active_memcg; | |
727 | rcu_read_unlock(); | |
728 | return memcg; | |
729 | } | |
730 | return get_mem_cgroup_from_mm(current->mm); | |
731 | } | |
54595fe2 | 732 | |
5660048c JW |
733 | /** |
734 | * mem_cgroup_iter - iterate over memory cgroup hierarchy | |
735 | * @root: hierarchy root | |
736 | * @prev: previously returned memcg, NULL on first invocation | |
737 | * @reclaim: cookie for shared reclaim walks, NULL for full walks | |
738 | * | |
739 | * Returns references to children of the hierarchy below @root, or | |
740 | * @root itself, or %NULL after a full round-trip. | |
741 | * | |
742 | * Caller must pass the return value in @prev on subsequent | |
743 | * invocations for reference counting, or use mem_cgroup_iter_break() | |
744 | * to cancel a hierarchy walk before the round-trip is complete. | |
745 | * | |
b213b54f | 746 | * Reclaimers can specify a node and a priority level in @reclaim to |
5660048c | 747 | * divide up the memcgs in the hierarchy among all concurrent |
b213b54f | 748 | * reclaimers operating on the same node and priority. |
5660048c | 749 | */ |
694fbc0f | 750 | struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, |
5660048c | 751 | struct mem_cgroup *prev, |
694fbc0f | 752 | struct mem_cgroup_reclaim_cookie *reclaim) |
14067bb3 | 753 | { |
33398cf2 | 754 | struct mem_cgroup_reclaim_iter *uninitialized_var(iter); |
5ac8fb31 | 755 | struct cgroup_subsys_state *css = NULL; |
9f3a0d09 | 756 | struct mem_cgroup *memcg = NULL; |
5ac8fb31 | 757 | struct mem_cgroup *pos = NULL; |
711d3d2c | 758 | |
694fbc0f AM |
759 | if (mem_cgroup_disabled()) |
760 | return NULL; | |
5660048c | 761 | |
9f3a0d09 JW |
762 | if (!root) |
763 | root = root_mem_cgroup; | |
7d74b06f | 764 | |
9f3a0d09 | 765 | if (prev && !reclaim) |
5ac8fb31 | 766 | pos = prev; |
14067bb3 | 767 | |
9f3a0d09 JW |
768 | if (!root->use_hierarchy && root != root_mem_cgroup) { |
769 | if (prev) | |
5ac8fb31 | 770 | goto out; |
694fbc0f | 771 | return root; |
9f3a0d09 | 772 | } |
14067bb3 | 773 | |
542f85f9 | 774 | rcu_read_lock(); |
5f578161 | 775 | |
5ac8fb31 | 776 | if (reclaim) { |
ef8f2327 | 777 | struct mem_cgroup_per_node *mz; |
5ac8fb31 | 778 | |
ef8f2327 | 779 | mz = mem_cgroup_nodeinfo(root, reclaim->pgdat->node_id); |
5ac8fb31 JW |
780 | iter = &mz->iter[reclaim->priority]; |
781 | ||
782 | if (prev && reclaim->generation != iter->generation) | |
783 | goto out_unlock; | |
784 | ||
6df38689 | 785 | while (1) { |
4db0c3c2 | 786 | pos = READ_ONCE(iter->position); |
6df38689 VD |
787 | if (!pos || css_tryget(&pos->css)) |
788 | break; | |
5ac8fb31 | 789 | /* |
6df38689 VD |
790 | * css reference reached zero, so iter->position will |
791 | * be cleared by ->css_released. However, we should not | |
792 | * rely on this happening soon, because ->css_released | |
793 | * is called from a work queue, and by busy-waiting we | |
794 | * might block it. So we clear iter->position right | |
795 | * away. | |
5ac8fb31 | 796 | */ |
6df38689 VD |
797 | (void)cmpxchg(&iter->position, pos, NULL); |
798 | } | |
5ac8fb31 JW |
799 | } |
800 | ||
801 | if (pos) | |
802 | css = &pos->css; | |
803 | ||
804 | for (;;) { | |
805 | css = css_next_descendant_pre(css, &root->css); | |
806 | if (!css) { | |
807 | /* | |
808 | * Reclaimers share the hierarchy walk, and a | |
809 | * new one might jump in right at the end of | |
810 | * the hierarchy - make sure they see at least | |
811 | * one group and restart from the beginning. | |
812 | */ | |
813 | if (!prev) | |
814 | continue; | |
815 | break; | |
527a5ec9 | 816 | } |
7d74b06f | 817 | |
5ac8fb31 JW |
818 | /* |
819 | * Verify the css and acquire a reference. The root | |
820 | * is provided by the caller, so we know it's alive | |
821 | * and kicking, and don't take an extra reference. | |
822 | */ | |
823 | memcg = mem_cgroup_from_css(css); | |
14067bb3 | 824 | |
5ac8fb31 JW |
825 | if (css == &root->css) |
826 | break; | |
14067bb3 | 827 | |
0b8f73e1 JW |
828 | if (css_tryget(css)) |
829 | break; | |
9f3a0d09 | 830 | |
5ac8fb31 | 831 | memcg = NULL; |
9f3a0d09 | 832 | } |
5ac8fb31 JW |
833 | |
834 | if (reclaim) { | |
5ac8fb31 | 835 | /* |
6df38689 VD |
836 | * The position could have already been updated by a competing |
837 | * thread, so check that the value hasn't changed since we read | |
838 | * it to avoid reclaiming from the same cgroup twice. | |
5ac8fb31 | 839 | */ |
6df38689 VD |
840 | (void)cmpxchg(&iter->position, pos, memcg); |
841 | ||
5ac8fb31 JW |
842 | if (pos) |
843 | css_put(&pos->css); | |
844 | ||
845 | if (!memcg) | |
846 | iter->generation++; | |
847 | else if (!prev) | |
848 | reclaim->generation = iter->generation; | |
9f3a0d09 | 849 | } |
5ac8fb31 | 850 | |
542f85f9 MH |
851 | out_unlock: |
852 | rcu_read_unlock(); | |
5ac8fb31 | 853 | out: |
c40046f3 MH |
854 | if (prev && prev != root) |
855 | css_put(&prev->css); | |
856 | ||
9f3a0d09 | 857 | return memcg; |
14067bb3 | 858 | } |
7d74b06f | 859 | |
5660048c JW |
860 | /** |
861 | * mem_cgroup_iter_break - abort a hierarchy walk prematurely | |
862 | * @root: hierarchy root | |
863 | * @prev: last visited hierarchy member as returned by mem_cgroup_iter() | |
864 | */ | |
865 | void mem_cgroup_iter_break(struct mem_cgroup *root, | |
866 | struct mem_cgroup *prev) | |
9f3a0d09 JW |
867 | { |
868 | if (!root) | |
869 | root = root_mem_cgroup; | |
870 | if (prev && prev != root) | |
871 | css_put(&prev->css); | |
872 | } | |
7d74b06f | 873 | |
6df38689 VD |
874 | static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg) |
875 | { | |
876 | struct mem_cgroup *memcg = dead_memcg; | |
877 | struct mem_cgroup_reclaim_iter *iter; | |
ef8f2327 MG |
878 | struct mem_cgroup_per_node *mz; |
879 | int nid; | |
6df38689 VD |
880 | int i; |
881 | ||
9f15bde6 | 882 | for (; memcg; memcg = parent_mem_cgroup(memcg)) { |
6df38689 | 883 | for_each_node(nid) { |
ef8f2327 MG |
884 | mz = mem_cgroup_nodeinfo(memcg, nid); |
885 | for (i = 0; i <= DEF_PRIORITY; i++) { | |
886 | iter = &mz->iter[i]; | |
887 | cmpxchg(&iter->position, | |
888 | dead_memcg, NULL); | |
6df38689 VD |
889 | } |
890 | } | |
891 | } | |
892 | } | |
893 | ||
9f3a0d09 JW |
894 | /* |
895 | * Iteration constructs for visiting all cgroups (under a tree). If | |
896 | * loops are exited prematurely (break), mem_cgroup_iter_break() must | |
897 | * be used for reference counting. | |
898 | */ | |
899 | #define for_each_mem_cgroup_tree(iter, root) \ | |
527a5ec9 | 900 | for (iter = mem_cgroup_iter(root, NULL, NULL); \ |
9f3a0d09 | 901 | iter != NULL; \ |
527a5ec9 | 902 | iter = mem_cgroup_iter(root, iter, NULL)) |
711d3d2c | 903 | |
9f3a0d09 | 904 | #define for_each_mem_cgroup(iter) \ |
527a5ec9 | 905 | for (iter = mem_cgroup_iter(NULL, NULL, NULL); \ |
9f3a0d09 | 906 | iter != NULL; \ |
527a5ec9 | 907 | iter = mem_cgroup_iter(NULL, iter, NULL)) |
14067bb3 | 908 | |
7c5f64f8 VD |
909 | /** |
910 | * mem_cgroup_scan_tasks - iterate over tasks of a memory cgroup hierarchy | |
911 | * @memcg: hierarchy root | |
912 | * @fn: function to call for each task | |
913 | * @arg: argument passed to @fn | |
914 | * | |
915 | * This function iterates over tasks attached to @memcg or to any of its | |
916 | * descendants and calls @fn for each task. If @fn returns a non-zero | |
917 | * value, the function breaks the iteration loop and returns the value. | |
918 | * Otherwise, it will iterate over all tasks and return 0. | |
919 | * | |
920 | * This function must not be called for the root memory cgroup. | |
921 | */ | |
922 | int mem_cgroup_scan_tasks(struct mem_cgroup *memcg, | |
923 | int (*fn)(struct task_struct *, void *), void *arg) | |
924 | { | |
925 | struct mem_cgroup *iter; | |
926 | int ret = 0; | |
927 | ||
928 | BUG_ON(memcg == root_mem_cgroup); | |
929 | ||
930 | for_each_mem_cgroup_tree(iter, memcg) { | |
931 | struct css_task_iter it; | |
932 | struct task_struct *task; | |
933 | ||
bc2fb7ed | 934 | css_task_iter_start(&iter->css, 0, &it); |
7c5f64f8 VD |
935 | while (!ret && (task = css_task_iter_next(&it))) |
936 | ret = fn(task, arg); | |
937 | css_task_iter_end(&it); | |
938 | if (ret) { | |
939 | mem_cgroup_iter_break(memcg, iter); | |
940 | break; | |
941 | } | |
942 | } | |
943 | return ret; | |
944 | } | |
945 | ||
925b7673 | 946 | /** |
dfe0e773 | 947 | * mem_cgroup_page_lruvec - return lruvec for isolating/putting an LRU page |
925b7673 | 948 | * @page: the page |
f144c390 | 949 | * @pgdat: pgdat of the page |
dfe0e773 JW |
950 | * |
951 | * This function is only safe when following the LRU page isolation | |
952 | * and putback protocol: the LRU lock must be held, and the page must | |
953 | * either be PageLRU() or the caller must have isolated/allocated it. | |
925b7673 | 954 | */ |
599d0c95 | 955 | struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct pglist_data *pgdat) |
08e552c6 | 956 | { |
ef8f2327 | 957 | struct mem_cgroup_per_node *mz; |
925b7673 | 958 | struct mem_cgroup *memcg; |
bea8c150 | 959 | struct lruvec *lruvec; |
6d12e2d8 | 960 | |
bea8c150 | 961 | if (mem_cgroup_disabled()) { |
599d0c95 | 962 | lruvec = &pgdat->lruvec; |
bea8c150 HD |
963 | goto out; |
964 | } | |
925b7673 | 965 | |
1306a85a | 966 | memcg = page->mem_cgroup; |
7512102c | 967 | /* |
dfe0e773 | 968 | * Swapcache readahead pages are added to the LRU - and |
29833315 | 969 | * possibly migrated - before they are charged. |
7512102c | 970 | */ |
29833315 JW |
971 | if (!memcg) |
972 | memcg = root_mem_cgroup; | |
7512102c | 973 | |
ef8f2327 | 974 | mz = mem_cgroup_page_nodeinfo(memcg, page); |
bea8c150 HD |
975 | lruvec = &mz->lruvec; |
976 | out: | |
977 | /* | |
978 | * Since a node can be onlined after the mem_cgroup was created, | |
979 | * we have to be prepared to initialize lruvec->zone here; | |
980 | * and if offlined then reonlined, we need to reinitialize it. | |
981 | */ | |
599d0c95 MG |
982 | if (unlikely(lruvec->pgdat != pgdat)) |
983 | lruvec->pgdat = pgdat; | |
bea8c150 | 984 | return lruvec; |
08e552c6 | 985 | } |
b69408e8 | 986 | |
925b7673 | 987 | /** |
fa9add64 HD |
988 | * mem_cgroup_update_lru_size - account for adding or removing an lru page |
989 | * @lruvec: mem_cgroup per zone lru vector | |
990 | * @lru: index of lru list the page is sitting on | |
b4536f0c | 991 | * @zid: zone id of the accounted pages |
fa9add64 | 992 | * @nr_pages: positive when adding or negative when removing |
925b7673 | 993 | * |
ca707239 HD |
994 | * This function must be called under lru_lock, just before a page is added |
995 | * to or just after a page is removed from an lru list (that ordering being | |
996 | * so as to allow it to check that lru_size 0 is consistent with list_empty). | |
3f58a829 | 997 | */ |
fa9add64 | 998 | void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru, |
b4536f0c | 999 | int zid, int nr_pages) |
3f58a829 | 1000 | { |
ef8f2327 | 1001 | struct mem_cgroup_per_node *mz; |
fa9add64 | 1002 | unsigned long *lru_size; |
ca707239 | 1003 | long size; |
3f58a829 MK |
1004 | |
1005 | if (mem_cgroup_disabled()) | |
1006 | return; | |
1007 | ||
ef8f2327 | 1008 | mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec); |
b4536f0c | 1009 | lru_size = &mz->lru_zone_size[zid][lru]; |
ca707239 HD |
1010 | |
1011 | if (nr_pages < 0) | |
1012 | *lru_size += nr_pages; | |
1013 | ||
1014 | size = *lru_size; | |
b4536f0c MH |
1015 | if (WARN_ONCE(size < 0, |
1016 | "%s(%p, %d, %d): lru_size %ld\n", | |
1017 | __func__, lruvec, lru, nr_pages, size)) { | |
ca707239 HD |
1018 | VM_BUG_ON(1); |
1019 | *lru_size = 0; | |
1020 | } | |
1021 | ||
1022 | if (nr_pages > 0) | |
1023 | *lru_size += nr_pages; | |
08e552c6 | 1024 | } |
544122e5 | 1025 | |
2314b42d | 1026 | bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg) |
c3ac9a8a | 1027 | { |
2314b42d | 1028 | struct mem_cgroup *task_memcg; |
158e0a2d | 1029 | struct task_struct *p; |
ffbdccf5 | 1030 | bool ret; |
4c4a2214 | 1031 | |
158e0a2d | 1032 | p = find_lock_task_mm(task); |
de077d22 | 1033 | if (p) { |
2314b42d | 1034 | task_memcg = get_mem_cgroup_from_mm(p->mm); |
de077d22 DR |
1035 | task_unlock(p); |
1036 | } else { | |
1037 | /* | |
1038 | * All threads may have already detached their mm's, but the oom | |
1039 | * killer still needs to detect if they have already been oom | |
1040 | * killed to prevent needlessly killing additional tasks. | |
1041 | */ | |
ffbdccf5 | 1042 | rcu_read_lock(); |
2314b42d JW |
1043 | task_memcg = mem_cgroup_from_task(task); |
1044 | css_get(&task_memcg->css); | |
ffbdccf5 | 1045 | rcu_read_unlock(); |
de077d22 | 1046 | } |
2314b42d JW |
1047 | ret = mem_cgroup_is_descendant(task_memcg, memcg); |
1048 | css_put(&task_memcg->css); | |
4c4a2214 DR |
1049 | return ret; |
1050 | } | |
1051 | ||
19942822 | 1052 | /** |
9d11ea9f | 1053 | * mem_cgroup_margin - calculate chargeable space of a memory cgroup |
dad7557e | 1054 | * @memcg: the memory cgroup |
19942822 | 1055 | * |
9d11ea9f | 1056 | * Returns the maximum amount of memory @mem can be charged with, in |
7ec99d62 | 1057 | * pages. |
19942822 | 1058 | */ |
c0ff4b85 | 1059 | static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg) |
19942822 | 1060 | { |
3e32cb2e JW |
1061 | unsigned long margin = 0; |
1062 | unsigned long count; | |
1063 | unsigned long limit; | |
9d11ea9f | 1064 | |
3e32cb2e | 1065 | count = page_counter_read(&memcg->memory); |
bbec2e15 | 1066 | limit = READ_ONCE(memcg->memory.max); |
3e32cb2e JW |
1067 | if (count < limit) |
1068 | margin = limit - count; | |
1069 | ||
7941d214 | 1070 | if (do_memsw_account()) { |
3e32cb2e | 1071 | count = page_counter_read(&memcg->memsw); |
bbec2e15 | 1072 | limit = READ_ONCE(memcg->memsw.max); |
3e32cb2e JW |
1073 | if (count <= limit) |
1074 | margin = min(margin, limit - count); | |
cbedbac3 LR |
1075 | else |
1076 | margin = 0; | |
3e32cb2e JW |
1077 | } |
1078 | ||
1079 | return margin; | |
19942822 JW |
1080 | } |
1081 | ||
32047e2a | 1082 | /* |
bdcbb659 | 1083 | * A routine for checking "mem" is under move_account() or not. |
32047e2a | 1084 | * |
bdcbb659 QH |
1085 | * Checking a cgroup is mc.from or mc.to or under hierarchy of |
1086 | * moving cgroups. This is for waiting at high-memory pressure | |
1087 | * caused by "move". | |
32047e2a | 1088 | */ |
c0ff4b85 | 1089 | static bool mem_cgroup_under_move(struct mem_cgroup *memcg) |
4b534334 | 1090 | { |
2bd9bb20 KH |
1091 | struct mem_cgroup *from; |
1092 | struct mem_cgroup *to; | |
4b534334 | 1093 | bool ret = false; |
2bd9bb20 KH |
1094 | /* |
1095 | * Unlike task_move routines, we access mc.to, mc.from not under | |
1096 | * mutual exclusion by cgroup_mutex. Here, we take spinlock instead. | |
1097 | */ | |
1098 | spin_lock(&mc.lock); | |
1099 | from = mc.from; | |
1100 | to = mc.to; | |
1101 | if (!from) | |
1102 | goto unlock; | |
3e92041d | 1103 | |
2314b42d JW |
1104 | ret = mem_cgroup_is_descendant(from, memcg) || |
1105 | mem_cgroup_is_descendant(to, memcg); | |
2bd9bb20 KH |
1106 | unlock: |
1107 | spin_unlock(&mc.lock); | |
4b534334 KH |
1108 | return ret; |
1109 | } | |
1110 | ||
c0ff4b85 | 1111 | static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg) |
4b534334 KH |
1112 | { |
1113 | if (mc.moving_task && current != mc.moving_task) { | |
c0ff4b85 | 1114 | if (mem_cgroup_under_move(memcg)) { |
4b534334 KH |
1115 | DEFINE_WAIT(wait); |
1116 | prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE); | |
1117 | /* moving charge context might have finished. */ | |
1118 | if (mc.moving_task) | |
1119 | schedule(); | |
1120 | finish_wait(&mc.waitq, &wait); | |
1121 | return true; | |
1122 | } | |
1123 | } | |
1124 | return false; | |
1125 | } | |
1126 | ||
8ad6e404 | 1127 | static const unsigned int memcg1_stats[] = { |
71cd3113 JW |
1128 | MEMCG_CACHE, |
1129 | MEMCG_RSS, | |
1130 | MEMCG_RSS_HUGE, | |
1131 | NR_SHMEM, | |
1132 | NR_FILE_MAPPED, | |
1133 | NR_FILE_DIRTY, | |
1134 | NR_WRITEBACK, | |
1135 | MEMCG_SWAP, | |
1136 | }; | |
1137 | ||
1138 | static const char *const memcg1_stat_names[] = { | |
1139 | "cache", | |
1140 | "rss", | |
1141 | "rss_huge", | |
1142 | "shmem", | |
1143 | "mapped_file", | |
1144 | "dirty", | |
1145 | "writeback", | |
1146 | "swap", | |
1147 | }; | |
1148 | ||
58cf188e | 1149 | #define K(x) ((x) << (PAGE_SHIFT-10)) |
e222432b | 1150 | /** |
58cf188e | 1151 | * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller. |
e222432b BS |
1152 | * @memcg: The memory cgroup that went over limit |
1153 | * @p: Task that is going to be killed | |
1154 | * | |
1155 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is | |
1156 | * enabled | |
1157 | */ | |
1158 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) | |
1159 | { | |
58cf188e SZ |
1160 | struct mem_cgroup *iter; |
1161 | unsigned int i; | |
e222432b | 1162 | |
e222432b BS |
1163 | rcu_read_lock(); |
1164 | ||
2415b9f5 BV |
1165 | if (p) { |
1166 | pr_info("Task in "); | |
1167 | pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id)); | |
1168 | pr_cont(" killed as a result of limit of "); | |
1169 | } else { | |
1170 | pr_info("Memory limit reached of cgroup "); | |
1171 | } | |
1172 | ||
e61734c5 | 1173 | pr_cont_cgroup_path(memcg->css.cgroup); |
0346dadb | 1174 | pr_cont("\n"); |
e222432b | 1175 | |
e222432b BS |
1176 | rcu_read_unlock(); |
1177 | ||
3e32cb2e JW |
1178 | pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n", |
1179 | K((u64)page_counter_read(&memcg->memory)), | |
bbec2e15 | 1180 | K((u64)memcg->memory.max), memcg->memory.failcnt); |
3e32cb2e JW |
1181 | pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n", |
1182 | K((u64)page_counter_read(&memcg->memsw)), | |
bbec2e15 | 1183 | K((u64)memcg->memsw.max), memcg->memsw.failcnt); |
3e32cb2e JW |
1184 | pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n", |
1185 | K((u64)page_counter_read(&memcg->kmem)), | |
bbec2e15 | 1186 | K((u64)memcg->kmem.max), memcg->kmem.failcnt); |
58cf188e SZ |
1187 | |
1188 | for_each_mem_cgroup_tree(iter, memcg) { | |
e61734c5 TH |
1189 | pr_info("Memory cgroup stats for "); |
1190 | pr_cont_cgroup_path(iter->css.cgroup); | |
58cf188e SZ |
1191 | pr_cont(":"); |
1192 | ||
71cd3113 JW |
1193 | for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) { |
1194 | if (memcg1_stats[i] == MEMCG_SWAP && !do_swap_account) | |
58cf188e | 1195 | continue; |
71cd3113 | 1196 | pr_cont(" %s:%luKB", memcg1_stat_names[i], |
ccda7f43 | 1197 | K(memcg_page_state(iter, memcg1_stats[i]))); |
58cf188e SZ |
1198 | } |
1199 | ||
1200 | for (i = 0; i < NR_LRU_LISTS; i++) | |
1201 | pr_cont(" %s:%luKB", mem_cgroup_lru_names[i], | |
1202 | K(mem_cgroup_nr_lru_pages(iter, BIT(i)))); | |
1203 | ||
1204 | pr_cont("\n"); | |
1205 | } | |
e222432b BS |
1206 | } |
1207 | ||
a63d83f4 DR |
1208 | /* |
1209 | * Return the memory (and swap, if configured) limit for a memcg. | |
1210 | */ | |
bbec2e15 | 1211 | unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg) |
a63d83f4 | 1212 | { |
bbec2e15 | 1213 | unsigned long max; |
f3e8eb70 | 1214 | |
bbec2e15 | 1215 | max = memcg->memory.max; |
9a5a8f19 | 1216 | if (mem_cgroup_swappiness(memcg)) { |
bbec2e15 RG |
1217 | unsigned long memsw_max; |
1218 | unsigned long swap_max; | |
9a5a8f19 | 1219 | |
bbec2e15 RG |
1220 | memsw_max = memcg->memsw.max; |
1221 | swap_max = memcg->swap.max; | |
1222 | swap_max = min(swap_max, (unsigned long)total_swap_pages); | |
1223 | max = min(max + swap_max, memsw_max); | |
9a5a8f19 | 1224 | } |
bbec2e15 | 1225 | return max; |
a63d83f4 DR |
1226 | } |
1227 | ||
b6e6edcf | 1228 | static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, |
19965460 | 1229 | int order) |
9cbb78bb | 1230 | { |
6e0fc46d DR |
1231 | struct oom_control oc = { |
1232 | .zonelist = NULL, | |
1233 | .nodemask = NULL, | |
2a966b77 | 1234 | .memcg = memcg, |
6e0fc46d DR |
1235 | .gfp_mask = gfp_mask, |
1236 | .order = order, | |
6e0fc46d | 1237 | }; |
7c5f64f8 | 1238 | bool ret; |
9cbb78bb | 1239 | |
dc56401f | 1240 | mutex_lock(&oom_lock); |
7c5f64f8 | 1241 | ret = out_of_memory(&oc); |
dc56401f | 1242 | mutex_unlock(&oom_lock); |
7c5f64f8 | 1243 | return ret; |
9cbb78bb DR |
1244 | } |
1245 | ||
ae6e71d3 MC |
1246 | #if MAX_NUMNODES > 1 |
1247 | ||
4d0c066d KH |
1248 | /** |
1249 | * test_mem_cgroup_node_reclaimable | |
dad7557e | 1250 | * @memcg: the target memcg |
4d0c066d KH |
1251 | * @nid: the node ID to be checked. |
1252 | * @noswap : specify true here if the user wants flle only information. | |
1253 | * | |
1254 | * This function returns whether the specified memcg contains any | |
1255 | * reclaimable pages on a node. Returns true if there are any reclaimable | |
1256 | * pages in the node. | |
1257 | */ | |
c0ff4b85 | 1258 | static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg, |
4d0c066d KH |
1259 | int nid, bool noswap) |
1260 | { | |
c0ff4b85 | 1261 | if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE)) |
4d0c066d KH |
1262 | return true; |
1263 | if (noswap || !total_swap_pages) | |
1264 | return false; | |
c0ff4b85 | 1265 | if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON)) |
4d0c066d KH |
1266 | return true; |
1267 | return false; | |
1268 | ||
1269 | } | |
889976db YH |
1270 | |
1271 | /* | |
1272 | * Always updating the nodemask is not very good - even if we have an empty | |
1273 | * list or the wrong list here, we can start from some node and traverse all | |
1274 | * nodes based on the zonelist. So update the list loosely once per 10 secs. | |
1275 | * | |
1276 | */ | |
c0ff4b85 | 1277 | static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg) |
889976db YH |
1278 | { |
1279 | int nid; | |
453a9bf3 KH |
1280 | /* |
1281 | * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET | |
1282 | * pagein/pageout changes since the last update. | |
1283 | */ | |
c0ff4b85 | 1284 | if (!atomic_read(&memcg->numainfo_events)) |
453a9bf3 | 1285 | return; |
c0ff4b85 | 1286 | if (atomic_inc_return(&memcg->numainfo_updating) > 1) |
889976db YH |
1287 | return; |
1288 | ||
889976db | 1289 | /* make a nodemask where this memcg uses memory from */ |
31aaea4a | 1290 | memcg->scan_nodes = node_states[N_MEMORY]; |
889976db | 1291 | |
31aaea4a | 1292 | for_each_node_mask(nid, node_states[N_MEMORY]) { |
889976db | 1293 | |
c0ff4b85 R |
1294 | if (!test_mem_cgroup_node_reclaimable(memcg, nid, false)) |
1295 | node_clear(nid, memcg->scan_nodes); | |
889976db | 1296 | } |
453a9bf3 | 1297 | |
c0ff4b85 R |
1298 | atomic_set(&memcg->numainfo_events, 0); |
1299 | atomic_set(&memcg->numainfo_updating, 0); | |
889976db YH |
1300 | } |
1301 | ||
1302 | /* | |
1303 | * Selecting a node where we start reclaim from. Because what we need is just | |
1304 | * reducing usage counter, start from anywhere is O,K. Considering | |
1305 | * memory reclaim from current node, there are pros. and cons. | |
1306 | * | |
1307 | * Freeing memory from current node means freeing memory from a node which | |
1308 | * we'll use or we've used. So, it may make LRU bad. And if several threads | |
1309 | * hit limits, it will see a contention on a node. But freeing from remote | |
1310 | * node means more costs for memory reclaim because of memory latency. | |
1311 | * | |
1312 | * Now, we use round-robin. Better algorithm is welcomed. | |
1313 | */ | |
c0ff4b85 | 1314 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg) |
889976db YH |
1315 | { |
1316 | int node; | |
1317 | ||
c0ff4b85 R |
1318 | mem_cgroup_may_update_nodemask(memcg); |
1319 | node = memcg->last_scanned_node; | |
889976db | 1320 | |
0edaf86c | 1321 | node = next_node_in(node, memcg->scan_nodes); |
889976db | 1322 | /* |
fda3d69b MH |
1323 | * mem_cgroup_may_update_nodemask might have seen no reclaimmable pages |
1324 | * last time it really checked all the LRUs due to rate limiting. | |
1325 | * Fallback to the current node in that case for simplicity. | |
889976db YH |
1326 | */ |
1327 | if (unlikely(node == MAX_NUMNODES)) | |
1328 | node = numa_node_id(); | |
1329 | ||
c0ff4b85 | 1330 | memcg->last_scanned_node = node; |
889976db YH |
1331 | return node; |
1332 | } | |
889976db | 1333 | #else |
c0ff4b85 | 1334 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg) |
889976db YH |
1335 | { |
1336 | return 0; | |
1337 | } | |
1338 | #endif | |
1339 | ||
0608f43d | 1340 | static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg, |
ef8f2327 | 1341 | pg_data_t *pgdat, |
0608f43d AM |
1342 | gfp_t gfp_mask, |
1343 | unsigned long *total_scanned) | |
1344 | { | |
1345 | struct mem_cgroup *victim = NULL; | |
1346 | int total = 0; | |
1347 | int loop = 0; | |
1348 | unsigned long excess; | |
1349 | unsigned long nr_scanned; | |
1350 | struct mem_cgroup_reclaim_cookie reclaim = { | |
ef8f2327 | 1351 | .pgdat = pgdat, |
0608f43d AM |
1352 | .priority = 0, |
1353 | }; | |
1354 | ||
3e32cb2e | 1355 | excess = soft_limit_excess(root_memcg); |
0608f43d AM |
1356 | |
1357 | while (1) { | |
1358 | victim = mem_cgroup_iter(root_memcg, victim, &reclaim); | |
1359 | if (!victim) { | |
1360 | loop++; | |
1361 | if (loop >= 2) { | |
1362 | /* | |
1363 | * If we have not been able to reclaim | |
1364 | * anything, it might because there are | |
1365 | * no reclaimable pages under this hierarchy | |
1366 | */ | |
1367 | if (!total) | |
1368 | break; | |
1369 | /* | |
1370 | * We want to do more targeted reclaim. | |
1371 | * excess >> 2 is not to excessive so as to | |
1372 | * reclaim too much, nor too less that we keep | |
1373 | * coming back to reclaim from this cgroup | |
1374 | */ | |
1375 | if (total >= (excess >> 2) || | |
1376 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) | |
1377 | break; | |
1378 | } | |
1379 | continue; | |
1380 | } | |
a9dd0a83 | 1381 | total += mem_cgroup_shrink_node(victim, gfp_mask, false, |
ef8f2327 | 1382 | pgdat, &nr_scanned); |
0608f43d | 1383 | *total_scanned += nr_scanned; |
3e32cb2e | 1384 | if (!soft_limit_excess(root_memcg)) |
0608f43d | 1385 | break; |
6d61ef40 | 1386 | } |
0608f43d AM |
1387 | mem_cgroup_iter_break(root_memcg, victim); |
1388 | return total; | |
6d61ef40 BS |
1389 | } |
1390 | ||
0056f4e6 JW |
1391 | #ifdef CONFIG_LOCKDEP |
1392 | static struct lockdep_map memcg_oom_lock_dep_map = { | |
1393 | .name = "memcg_oom_lock", | |
1394 | }; | |
1395 | #endif | |
1396 | ||
fb2a6fc5 JW |
1397 | static DEFINE_SPINLOCK(memcg_oom_lock); |
1398 | ||
867578cb KH |
1399 | /* |
1400 | * Check OOM-Killer is already running under our hierarchy. | |
1401 | * If someone is running, return false. | |
1402 | */ | |
fb2a6fc5 | 1403 | static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg) |
867578cb | 1404 | { |
79dfdacc | 1405 | struct mem_cgroup *iter, *failed = NULL; |
a636b327 | 1406 | |
fb2a6fc5 JW |
1407 | spin_lock(&memcg_oom_lock); |
1408 | ||
9f3a0d09 | 1409 | for_each_mem_cgroup_tree(iter, memcg) { |
23751be0 | 1410 | if (iter->oom_lock) { |
79dfdacc MH |
1411 | /* |
1412 | * this subtree of our hierarchy is already locked | |
1413 | * so we cannot give a lock. | |
1414 | */ | |
79dfdacc | 1415 | failed = iter; |
9f3a0d09 JW |
1416 | mem_cgroup_iter_break(memcg, iter); |
1417 | break; | |
23751be0 JW |
1418 | } else |
1419 | iter->oom_lock = true; | |
7d74b06f | 1420 | } |
867578cb | 1421 | |
fb2a6fc5 JW |
1422 | if (failed) { |
1423 | /* | |
1424 | * OK, we failed to lock the whole subtree so we have | |
1425 | * to clean up what we set up to the failing subtree | |
1426 | */ | |
1427 | for_each_mem_cgroup_tree(iter, memcg) { | |
1428 | if (iter == failed) { | |
1429 | mem_cgroup_iter_break(memcg, iter); | |
1430 | break; | |
1431 | } | |
1432 | iter->oom_lock = false; | |
79dfdacc | 1433 | } |
0056f4e6 JW |
1434 | } else |
1435 | mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_); | |
fb2a6fc5 JW |
1436 | |
1437 | spin_unlock(&memcg_oom_lock); | |
1438 | ||
1439 | return !failed; | |
a636b327 | 1440 | } |
0b7f569e | 1441 | |
fb2a6fc5 | 1442 | static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg) |
0b7f569e | 1443 | { |
7d74b06f KH |
1444 | struct mem_cgroup *iter; |
1445 | ||
fb2a6fc5 | 1446 | spin_lock(&memcg_oom_lock); |
0056f4e6 | 1447 | mutex_release(&memcg_oom_lock_dep_map, 1, _RET_IP_); |
c0ff4b85 | 1448 | for_each_mem_cgroup_tree(iter, memcg) |
79dfdacc | 1449 | iter->oom_lock = false; |
fb2a6fc5 | 1450 | spin_unlock(&memcg_oom_lock); |
79dfdacc MH |
1451 | } |
1452 | ||
c0ff4b85 | 1453 | static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg) |
79dfdacc MH |
1454 | { |
1455 | struct mem_cgroup *iter; | |
1456 | ||
c2b42d3c | 1457 | spin_lock(&memcg_oom_lock); |
c0ff4b85 | 1458 | for_each_mem_cgroup_tree(iter, memcg) |
c2b42d3c TH |
1459 | iter->under_oom++; |
1460 | spin_unlock(&memcg_oom_lock); | |
79dfdacc MH |
1461 | } |
1462 | ||
c0ff4b85 | 1463 | static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg) |
79dfdacc MH |
1464 | { |
1465 | struct mem_cgroup *iter; | |
1466 | ||
867578cb KH |
1467 | /* |
1468 | * When a new child is created while the hierarchy is under oom, | |
c2b42d3c | 1469 | * mem_cgroup_oom_lock() may not be called. Watch for underflow. |
867578cb | 1470 | */ |
c2b42d3c | 1471 | spin_lock(&memcg_oom_lock); |
c0ff4b85 | 1472 | for_each_mem_cgroup_tree(iter, memcg) |
c2b42d3c TH |
1473 | if (iter->under_oom > 0) |
1474 | iter->under_oom--; | |
1475 | spin_unlock(&memcg_oom_lock); | |
0b7f569e KH |
1476 | } |
1477 | ||
867578cb KH |
1478 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
1479 | ||
dc98df5a | 1480 | struct oom_wait_info { |
d79154bb | 1481 | struct mem_cgroup *memcg; |
ac6424b9 | 1482 | wait_queue_entry_t wait; |
dc98df5a KH |
1483 | }; |
1484 | ||
ac6424b9 | 1485 | static int memcg_oom_wake_function(wait_queue_entry_t *wait, |
dc98df5a KH |
1486 | unsigned mode, int sync, void *arg) |
1487 | { | |
d79154bb HD |
1488 | struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg; |
1489 | struct mem_cgroup *oom_wait_memcg; | |
dc98df5a KH |
1490 | struct oom_wait_info *oom_wait_info; |
1491 | ||
1492 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); | |
d79154bb | 1493 | oom_wait_memcg = oom_wait_info->memcg; |
dc98df5a | 1494 | |
2314b42d JW |
1495 | if (!mem_cgroup_is_descendant(wake_memcg, oom_wait_memcg) && |
1496 | !mem_cgroup_is_descendant(oom_wait_memcg, wake_memcg)) | |
dc98df5a | 1497 | return 0; |
dc98df5a KH |
1498 | return autoremove_wake_function(wait, mode, sync, arg); |
1499 | } | |
1500 | ||
c0ff4b85 | 1501 | static void memcg_oom_recover(struct mem_cgroup *memcg) |
3c11ecf4 | 1502 | { |
c2b42d3c TH |
1503 | /* |
1504 | * For the following lockless ->under_oom test, the only required | |
1505 | * guarantee is that it must see the state asserted by an OOM when | |
1506 | * this function is called as a result of userland actions | |
1507 | * triggered by the notification of the OOM. This is trivially | |
1508 | * achieved by invoking mem_cgroup_mark_under_oom() before | |
1509 | * triggering notification. | |
1510 | */ | |
1511 | if (memcg && memcg->under_oom) | |
f4b90b70 | 1512 | __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg); |
3c11ecf4 KH |
1513 | } |
1514 | ||
3812c8c8 | 1515 | static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order) |
0b7f569e | 1516 | { |
2a70f6a7 | 1517 | if (!current->memcg_may_oom || order > PAGE_ALLOC_COSTLY_ORDER) |
3812c8c8 | 1518 | return; |
867578cb | 1519 | /* |
49426420 JW |
1520 | * We are in the middle of the charge context here, so we |
1521 | * don't want to block when potentially sitting on a callstack | |
1522 | * that holds all kinds of filesystem and mm locks. | |
1523 | * | |
1524 | * Also, the caller may handle a failed allocation gracefully | |
1525 | * (like optional page cache readahead) and so an OOM killer | |
1526 | * invocation might not even be necessary. | |
1527 | * | |
1528 | * That's why we don't do anything here except remember the | |
1529 | * OOM context and then deal with it at the end of the page | |
1530 | * fault when the stack is unwound, the locks are released, | |
1531 | * and when we know whether the fault was overall successful. | |
867578cb | 1532 | */ |
49426420 | 1533 | css_get(&memcg->css); |
626ebc41 TH |
1534 | current->memcg_in_oom = memcg; |
1535 | current->memcg_oom_gfp_mask = mask; | |
1536 | current->memcg_oom_order = order; | |
3812c8c8 JW |
1537 | } |
1538 | ||
1539 | /** | |
1540 | * mem_cgroup_oom_synchronize - complete memcg OOM handling | |
49426420 | 1541 | * @handle: actually kill/wait or just clean up the OOM state |
3812c8c8 | 1542 | * |
49426420 JW |
1543 | * This has to be called at the end of a page fault if the memcg OOM |
1544 | * handler was enabled. | |
3812c8c8 | 1545 | * |
49426420 | 1546 | * Memcg supports userspace OOM handling where failed allocations must |
3812c8c8 JW |
1547 | * sleep on a waitqueue until the userspace task resolves the |
1548 | * situation. Sleeping directly in the charge context with all kinds | |
1549 | * of locks held is not a good idea, instead we remember an OOM state | |
1550 | * in the task and mem_cgroup_oom_synchronize() has to be called at | |
49426420 | 1551 | * the end of the page fault to complete the OOM handling. |
3812c8c8 JW |
1552 | * |
1553 | * Returns %true if an ongoing memcg OOM situation was detected and | |
49426420 | 1554 | * completed, %false otherwise. |
3812c8c8 | 1555 | */ |
49426420 | 1556 | bool mem_cgroup_oom_synchronize(bool handle) |
3812c8c8 | 1557 | { |
626ebc41 | 1558 | struct mem_cgroup *memcg = current->memcg_in_oom; |
3812c8c8 | 1559 | struct oom_wait_info owait; |
49426420 | 1560 | bool locked; |
3812c8c8 JW |
1561 | |
1562 | /* OOM is global, do not handle */ | |
3812c8c8 | 1563 | if (!memcg) |
49426420 | 1564 | return false; |
3812c8c8 | 1565 | |
7c5f64f8 | 1566 | if (!handle) |
49426420 | 1567 | goto cleanup; |
3812c8c8 JW |
1568 | |
1569 | owait.memcg = memcg; | |
1570 | owait.wait.flags = 0; | |
1571 | owait.wait.func = memcg_oom_wake_function; | |
1572 | owait.wait.private = current; | |
2055da97 | 1573 | INIT_LIST_HEAD(&owait.wait.entry); |
867578cb | 1574 | |
3812c8c8 | 1575 | prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE); |
49426420 JW |
1576 | mem_cgroup_mark_under_oom(memcg); |
1577 | ||
1578 | locked = mem_cgroup_oom_trylock(memcg); | |
1579 | ||
1580 | if (locked) | |
1581 | mem_cgroup_oom_notify(memcg); | |
1582 | ||
1583 | if (locked && !memcg->oom_kill_disable) { | |
1584 | mem_cgroup_unmark_under_oom(memcg); | |
1585 | finish_wait(&memcg_oom_waitq, &owait.wait); | |
626ebc41 TH |
1586 | mem_cgroup_out_of_memory(memcg, current->memcg_oom_gfp_mask, |
1587 | current->memcg_oom_order); | |
49426420 | 1588 | } else { |
3812c8c8 | 1589 | schedule(); |
49426420 JW |
1590 | mem_cgroup_unmark_under_oom(memcg); |
1591 | finish_wait(&memcg_oom_waitq, &owait.wait); | |
1592 | } | |
1593 | ||
1594 | if (locked) { | |
fb2a6fc5 JW |
1595 | mem_cgroup_oom_unlock(memcg); |
1596 | /* | |
1597 | * There is no guarantee that an OOM-lock contender | |
1598 | * sees the wakeups triggered by the OOM kill | |
1599 | * uncharges. Wake any sleepers explicitely. | |
1600 | */ | |
1601 | memcg_oom_recover(memcg); | |
1602 | } | |
49426420 | 1603 | cleanup: |
626ebc41 | 1604 | current->memcg_in_oom = NULL; |
3812c8c8 | 1605 | css_put(&memcg->css); |
867578cb | 1606 | return true; |
0b7f569e KH |
1607 | } |
1608 | ||
d7365e78 | 1609 | /** |
81f8c3a4 JW |
1610 | * lock_page_memcg - lock a page->mem_cgroup binding |
1611 | * @page: the page | |
32047e2a | 1612 | * |
81f8c3a4 | 1613 | * This function protects unlocked LRU pages from being moved to |
739f79fc JW |
1614 | * another cgroup. |
1615 | * | |
1616 | * It ensures lifetime of the returned memcg. Caller is responsible | |
1617 | * for the lifetime of the page; __unlock_page_memcg() is available | |
1618 | * when @page might get freed inside the locked section. | |
d69b042f | 1619 | */ |
739f79fc | 1620 | struct mem_cgroup *lock_page_memcg(struct page *page) |
89c06bd5 KH |
1621 | { |
1622 | struct mem_cgroup *memcg; | |
6de22619 | 1623 | unsigned long flags; |
89c06bd5 | 1624 | |
6de22619 JW |
1625 | /* |
1626 | * The RCU lock is held throughout the transaction. The fast | |
1627 | * path can get away without acquiring the memcg->move_lock | |
1628 | * because page moving starts with an RCU grace period. | |
739f79fc JW |
1629 | * |
1630 | * The RCU lock also protects the memcg from being freed when | |
1631 | * the page state that is going to change is the only thing | |
1632 | * preventing the page itself from being freed. E.g. writeback | |
1633 | * doesn't hold a page reference and relies on PG_writeback to | |
1634 | * keep off truncation, migration and so forth. | |
1635 | */ | |
d7365e78 JW |
1636 | rcu_read_lock(); |
1637 | ||
1638 | if (mem_cgroup_disabled()) | |
739f79fc | 1639 | return NULL; |
89c06bd5 | 1640 | again: |
1306a85a | 1641 | memcg = page->mem_cgroup; |
29833315 | 1642 | if (unlikely(!memcg)) |
739f79fc | 1643 | return NULL; |
d7365e78 | 1644 | |
bdcbb659 | 1645 | if (atomic_read(&memcg->moving_account) <= 0) |
739f79fc | 1646 | return memcg; |
89c06bd5 | 1647 | |
6de22619 | 1648 | spin_lock_irqsave(&memcg->move_lock, flags); |
1306a85a | 1649 | if (memcg != page->mem_cgroup) { |
6de22619 | 1650 | spin_unlock_irqrestore(&memcg->move_lock, flags); |
89c06bd5 KH |
1651 | goto again; |
1652 | } | |
6de22619 JW |
1653 | |
1654 | /* | |
1655 | * When charge migration first begins, we can have locked and | |
1656 | * unlocked page stat updates happening concurrently. Track | |
81f8c3a4 | 1657 | * the task who has the lock for unlock_page_memcg(). |
6de22619 JW |
1658 | */ |
1659 | memcg->move_lock_task = current; | |
1660 | memcg->move_lock_flags = flags; | |
d7365e78 | 1661 | |
739f79fc | 1662 | return memcg; |
89c06bd5 | 1663 | } |
81f8c3a4 | 1664 | EXPORT_SYMBOL(lock_page_memcg); |
89c06bd5 | 1665 | |
d7365e78 | 1666 | /** |
739f79fc JW |
1667 | * __unlock_page_memcg - unlock and unpin a memcg |
1668 | * @memcg: the memcg | |
1669 | * | |
1670 | * Unlock and unpin a memcg returned by lock_page_memcg(). | |
d7365e78 | 1671 | */ |
739f79fc | 1672 | void __unlock_page_memcg(struct mem_cgroup *memcg) |
89c06bd5 | 1673 | { |
6de22619 JW |
1674 | if (memcg && memcg->move_lock_task == current) { |
1675 | unsigned long flags = memcg->move_lock_flags; | |
1676 | ||
1677 | memcg->move_lock_task = NULL; | |
1678 | memcg->move_lock_flags = 0; | |
1679 | ||
1680 | spin_unlock_irqrestore(&memcg->move_lock, flags); | |
1681 | } | |
89c06bd5 | 1682 | |
d7365e78 | 1683 | rcu_read_unlock(); |
89c06bd5 | 1684 | } |
739f79fc JW |
1685 | |
1686 | /** | |
1687 | * unlock_page_memcg - unlock a page->mem_cgroup binding | |
1688 | * @page: the page | |
1689 | */ | |
1690 | void unlock_page_memcg(struct page *page) | |
1691 | { | |
1692 | __unlock_page_memcg(page->mem_cgroup); | |
1693 | } | |
81f8c3a4 | 1694 | EXPORT_SYMBOL(unlock_page_memcg); |
89c06bd5 | 1695 | |
cdec2e42 KH |
1696 | struct memcg_stock_pcp { |
1697 | struct mem_cgroup *cached; /* this never be root cgroup */ | |
11c9ea4e | 1698 | unsigned int nr_pages; |
cdec2e42 | 1699 | struct work_struct work; |
26fe6168 | 1700 | unsigned long flags; |
a0db00fc | 1701 | #define FLUSHING_CACHED_CHARGE 0 |
cdec2e42 KH |
1702 | }; |
1703 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); | |
9f50fad6 | 1704 | static DEFINE_MUTEX(percpu_charge_mutex); |
cdec2e42 | 1705 | |
a0956d54 SS |
1706 | /** |
1707 | * consume_stock: Try to consume stocked charge on this cpu. | |
1708 | * @memcg: memcg to consume from. | |
1709 | * @nr_pages: how many pages to charge. | |
1710 | * | |
1711 | * The charges will only happen if @memcg matches the current cpu's memcg | |
1712 | * stock, and at least @nr_pages are available in that stock. Failure to | |
1713 | * service an allocation will refill the stock. | |
1714 | * | |
1715 | * returns true if successful, false otherwise. | |
cdec2e42 | 1716 | */ |
a0956d54 | 1717 | static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
cdec2e42 KH |
1718 | { |
1719 | struct memcg_stock_pcp *stock; | |
db2ba40c | 1720 | unsigned long flags; |
3e32cb2e | 1721 | bool ret = false; |
cdec2e42 | 1722 | |
a983b5eb | 1723 | if (nr_pages > MEMCG_CHARGE_BATCH) |
3e32cb2e | 1724 | return ret; |
a0956d54 | 1725 | |
db2ba40c JW |
1726 | local_irq_save(flags); |
1727 | ||
1728 | stock = this_cpu_ptr(&memcg_stock); | |
3e32cb2e | 1729 | if (memcg == stock->cached && stock->nr_pages >= nr_pages) { |
a0956d54 | 1730 | stock->nr_pages -= nr_pages; |
3e32cb2e JW |
1731 | ret = true; |
1732 | } | |
db2ba40c JW |
1733 | |
1734 | local_irq_restore(flags); | |
1735 | ||
cdec2e42 KH |
1736 | return ret; |
1737 | } | |
1738 | ||
1739 | /* | |
3e32cb2e | 1740 | * Returns stocks cached in percpu and reset cached information. |
cdec2e42 KH |
1741 | */ |
1742 | static void drain_stock(struct memcg_stock_pcp *stock) | |
1743 | { | |
1744 | struct mem_cgroup *old = stock->cached; | |
1745 | ||
11c9ea4e | 1746 | if (stock->nr_pages) { |
3e32cb2e | 1747 | page_counter_uncharge(&old->memory, stock->nr_pages); |
7941d214 | 1748 | if (do_memsw_account()) |
3e32cb2e | 1749 | page_counter_uncharge(&old->memsw, stock->nr_pages); |
e8ea14cc | 1750 | css_put_many(&old->css, stock->nr_pages); |
11c9ea4e | 1751 | stock->nr_pages = 0; |
cdec2e42 KH |
1752 | } |
1753 | stock->cached = NULL; | |
cdec2e42 KH |
1754 | } |
1755 | ||
cdec2e42 KH |
1756 | static void drain_local_stock(struct work_struct *dummy) |
1757 | { | |
db2ba40c JW |
1758 | struct memcg_stock_pcp *stock; |
1759 | unsigned long flags; | |
1760 | ||
72f0184c MH |
1761 | /* |
1762 | * The only protection from memory hotplug vs. drain_stock races is | |
1763 | * that we always operate on local CPU stock here with IRQ disabled | |
1764 | */ | |
db2ba40c JW |
1765 | local_irq_save(flags); |
1766 | ||
1767 | stock = this_cpu_ptr(&memcg_stock); | |
cdec2e42 | 1768 | drain_stock(stock); |
26fe6168 | 1769 | clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); |
db2ba40c JW |
1770 | |
1771 | local_irq_restore(flags); | |
cdec2e42 KH |
1772 | } |
1773 | ||
1774 | /* | |
3e32cb2e | 1775 | * Cache charges(val) to local per_cpu area. |
320cc51d | 1776 | * This will be consumed by consume_stock() function, later. |
cdec2e42 | 1777 | */ |
c0ff4b85 | 1778 | static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
cdec2e42 | 1779 | { |
db2ba40c JW |
1780 | struct memcg_stock_pcp *stock; |
1781 | unsigned long flags; | |
1782 | ||
1783 | local_irq_save(flags); | |
cdec2e42 | 1784 | |
db2ba40c | 1785 | stock = this_cpu_ptr(&memcg_stock); |
c0ff4b85 | 1786 | if (stock->cached != memcg) { /* reset if necessary */ |
cdec2e42 | 1787 | drain_stock(stock); |
c0ff4b85 | 1788 | stock->cached = memcg; |
cdec2e42 | 1789 | } |
11c9ea4e | 1790 | stock->nr_pages += nr_pages; |
db2ba40c | 1791 | |
a983b5eb | 1792 | if (stock->nr_pages > MEMCG_CHARGE_BATCH) |
475d0487 RG |
1793 | drain_stock(stock); |
1794 | ||
db2ba40c | 1795 | local_irq_restore(flags); |
cdec2e42 KH |
1796 | } |
1797 | ||
1798 | /* | |
c0ff4b85 | 1799 | * Drains all per-CPU charge caches for given root_memcg resp. subtree |
6d3d6aa2 | 1800 | * of the hierarchy under it. |
cdec2e42 | 1801 | */ |
6d3d6aa2 | 1802 | static void drain_all_stock(struct mem_cgroup *root_memcg) |
cdec2e42 | 1803 | { |
26fe6168 | 1804 | int cpu, curcpu; |
d38144b7 | 1805 | |
6d3d6aa2 JW |
1806 | /* If someone's already draining, avoid adding running more workers. */ |
1807 | if (!mutex_trylock(&percpu_charge_mutex)) | |
1808 | return; | |
72f0184c MH |
1809 | /* |
1810 | * Notify other cpus that system-wide "drain" is running | |
1811 | * We do not care about races with the cpu hotplug because cpu down | |
1812 | * as well as workers from this path always operate on the local | |
1813 | * per-cpu data. CPU up doesn't touch memcg_stock at all. | |
1814 | */ | |
5af12d0e | 1815 | curcpu = get_cpu(); |
cdec2e42 KH |
1816 | for_each_online_cpu(cpu) { |
1817 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); | |
c0ff4b85 | 1818 | struct mem_cgroup *memcg; |
26fe6168 | 1819 | |
c0ff4b85 | 1820 | memcg = stock->cached; |
72f0184c | 1821 | if (!memcg || !stock->nr_pages || !css_tryget(&memcg->css)) |
26fe6168 | 1822 | continue; |
72f0184c MH |
1823 | if (!mem_cgroup_is_descendant(memcg, root_memcg)) { |
1824 | css_put(&memcg->css); | |
3e92041d | 1825 | continue; |
72f0184c | 1826 | } |
d1a05b69 MH |
1827 | if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) { |
1828 | if (cpu == curcpu) | |
1829 | drain_local_stock(&stock->work); | |
1830 | else | |
1831 | schedule_work_on(cpu, &stock->work); | |
1832 | } | |
72f0184c | 1833 | css_put(&memcg->css); |
cdec2e42 | 1834 | } |
5af12d0e | 1835 | put_cpu(); |
9f50fad6 | 1836 | mutex_unlock(&percpu_charge_mutex); |
cdec2e42 KH |
1837 | } |
1838 | ||
308167fc | 1839 | static int memcg_hotplug_cpu_dead(unsigned int cpu) |
cdec2e42 | 1840 | { |
cdec2e42 | 1841 | struct memcg_stock_pcp *stock; |
a983b5eb | 1842 | struct mem_cgroup *memcg; |
cdec2e42 | 1843 | |
cdec2e42 KH |
1844 | stock = &per_cpu(memcg_stock, cpu); |
1845 | drain_stock(stock); | |
a983b5eb JW |
1846 | |
1847 | for_each_mem_cgroup(memcg) { | |
1848 | int i; | |
1849 | ||
1850 | for (i = 0; i < MEMCG_NR_STAT; i++) { | |
1851 | int nid; | |
1852 | long x; | |
1853 | ||
1854 | x = this_cpu_xchg(memcg->stat_cpu->count[i], 0); | |
1855 | if (x) | |
1856 | atomic_long_add(x, &memcg->stat[i]); | |
1857 | ||
1858 | if (i >= NR_VM_NODE_STAT_ITEMS) | |
1859 | continue; | |
1860 | ||
1861 | for_each_node(nid) { | |
1862 | struct mem_cgroup_per_node *pn; | |
1863 | ||
1864 | pn = mem_cgroup_nodeinfo(memcg, nid); | |
1865 | x = this_cpu_xchg(pn->lruvec_stat_cpu->count[i], 0); | |
1866 | if (x) | |
1867 | atomic_long_add(x, &pn->lruvec_stat[i]); | |
1868 | } | |
1869 | } | |
1870 | ||
e27be240 | 1871 | for (i = 0; i < NR_VM_EVENT_ITEMS; i++) { |
a983b5eb JW |
1872 | long x; |
1873 | ||
1874 | x = this_cpu_xchg(memcg->stat_cpu->events[i], 0); | |
1875 | if (x) | |
1876 | atomic_long_add(x, &memcg->events[i]); | |
1877 | } | |
1878 | } | |
1879 | ||
308167fc | 1880 | return 0; |
cdec2e42 KH |
1881 | } |
1882 | ||
f7e1cb6e JW |
1883 | static void reclaim_high(struct mem_cgroup *memcg, |
1884 | unsigned int nr_pages, | |
1885 | gfp_t gfp_mask) | |
1886 | { | |
1887 | do { | |
1888 | if (page_counter_read(&memcg->memory) <= memcg->high) | |
1889 | continue; | |
e27be240 | 1890 | memcg_memory_event(memcg, MEMCG_HIGH); |
f7e1cb6e JW |
1891 | try_to_free_mem_cgroup_pages(memcg, nr_pages, gfp_mask, true); |
1892 | } while ((memcg = parent_mem_cgroup(memcg))); | |
1893 | } | |
1894 | ||
1895 | static void high_work_func(struct work_struct *work) | |
1896 | { | |
1897 | struct mem_cgroup *memcg; | |
1898 | ||
1899 | memcg = container_of(work, struct mem_cgroup, high_work); | |
a983b5eb | 1900 | reclaim_high(memcg, MEMCG_CHARGE_BATCH, GFP_KERNEL); |
f7e1cb6e JW |
1901 | } |
1902 | ||
b23afb93 TH |
1903 | /* |
1904 | * Scheduled by try_charge() to be executed from the userland return path | |
1905 | * and reclaims memory over the high limit. | |
1906 | */ | |
1907 | void mem_cgroup_handle_over_high(void) | |
1908 | { | |
1909 | unsigned int nr_pages = current->memcg_nr_pages_over_high; | |
f7e1cb6e | 1910 | struct mem_cgroup *memcg; |
b23afb93 TH |
1911 | |
1912 | if (likely(!nr_pages)) | |
1913 | return; | |
1914 | ||
f7e1cb6e JW |
1915 | memcg = get_mem_cgroup_from_mm(current->mm); |
1916 | reclaim_high(memcg, nr_pages, GFP_KERNEL); | |
b23afb93 TH |
1917 | css_put(&memcg->css); |
1918 | current->memcg_nr_pages_over_high = 0; | |
1919 | } | |
1920 | ||
00501b53 JW |
1921 | static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, |
1922 | unsigned int nr_pages) | |
8a9f3ccd | 1923 | { |
a983b5eb | 1924 | unsigned int batch = max(MEMCG_CHARGE_BATCH, nr_pages); |
9b130619 | 1925 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
6539cc05 | 1926 | struct mem_cgroup *mem_over_limit; |
3e32cb2e | 1927 | struct page_counter *counter; |
6539cc05 | 1928 | unsigned long nr_reclaimed; |
b70a2a21 JW |
1929 | bool may_swap = true; |
1930 | bool drained = false; | |
a636b327 | 1931 | |
ce00a967 | 1932 | if (mem_cgroup_is_root(memcg)) |
10d53c74 | 1933 | return 0; |
6539cc05 | 1934 | retry: |
b6b6cc72 | 1935 | if (consume_stock(memcg, nr_pages)) |
10d53c74 | 1936 | return 0; |
8a9f3ccd | 1937 | |
7941d214 | 1938 | if (!do_memsw_account() || |
6071ca52 JW |
1939 | page_counter_try_charge(&memcg->memsw, batch, &counter)) { |
1940 | if (page_counter_try_charge(&memcg->memory, batch, &counter)) | |
6539cc05 | 1941 | goto done_restock; |
7941d214 | 1942 | if (do_memsw_account()) |
3e32cb2e JW |
1943 | page_counter_uncharge(&memcg->memsw, batch); |
1944 | mem_over_limit = mem_cgroup_from_counter(counter, memory); | |
3fbe7244 | 1945 | } else { |
3e32cb2e | 1946 | mem_over_limit = mem_cgroup_from_counter(counter, memsw); |
b70a2a21 | 1947 | may_swap = false; |
3fbe7244 | 1948 | } |
7a81b88c | 1949 | |
6539cc05 JW |
1950 | if (batch > nr_pages) { |
1951 | batch = nr_pages; | |
1952 | goto retry; | |
1953 | } | |
6d61ef40 | 1954 | |
06b078fc JW |
1955 | /* |
1956 | * Unlike in global OOM situations, memcg is not in a physical | |
1957 | * memory shortage. Allow dying and OOM-killed tasks to | |
1958 | * bypass the last charges so that they can exit quickly and | |
1959 | * free their memory. | |
1960 | */ | |
da99ecf1 | 1961 | if (unlikely(tsk_is_oom_victim(current) || |
06b078fc JW |
1962 | fatal_signal_pending(current) || |
1963 | current->flags & PF_EXITING)) | |
10d53c74 | 1964 | goto force; |
06b078fc | 1965 | |
89a28483 JW |
1966 | /* |
1967 | * Prevent unbounded recursion when reclaim operations need to | |
1968 | * allocate memory. This might exceed the limits temporarily, | |
1969 | * but we prefer facilitating memory reclaim and getting back | |
1970 | * under the limit over triggering OOM kills in these cases. | |
1971 | */ | |
1972 | if (unlikely(current->flags & PF_MEMALLOC)) | |
1973 | goto force; | |
1974 | ||
06b078fc JW |
1975 | if (unlikely(task_in_memcg_oom(current))) |
1976 | goto nomem; | |
1977 | ||
d0164adc | 1978 | if (!gfpflags_allow_blocking(gfp_mask)) |
6539cc05 | 1979 | goto nomem; |
4b534334 | 1980 | |
e27be240 | 1981 | memcg_memory_event(mem_over_limit, MEMCG_MAX); |
241994ed | 1982 | |
b70a2a21 JW |
1983 | nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages, |
1984 | gfp_mask, may_swap); | |
6539cc05 | 1985 | |
61e02c74 | 1986 | if (mem_cgroup_margin(mem_over_limit) >= nr_pages) |
6539cc05 | 1987 | goto retry; |
28c34c29 | 1988 | |
b70a2a21 | 1989 | if (!drained) { |
6d3d6aa2 | 1990 | drain_all_stock(mem_over_limit); |
b70a2a21 JW |
1991 | drained = true; |
1992 | goto retry; | |
1993 | } | |
1994 | ||
28c34c29 JW |
1995 | if (gfp_mask & __GFP_NORETRY) |
1996 | goto nomem; | |
6539cc05 JW |
1997 | /* |
1998 | * Even though the limit is exceeded at this point, reclaim | |
1999 | * may have been able to free some pages. Retry the charge | |
2000 | * before killing the task. | |
2001 | * | |
2002 | * Only for regular pages, though: huge pages are rather | |
2003 | * unlikely to succeed so close to the limit, and we fall back | |
2004 | * to regular pages anyway in case of failure. | |
2005 | */ | |
61e02c74 | 2006 | if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER)) |
6539cc05 JW |
2007 | goto retry; |
2008 | /* | |
2009 | * At task move, charge accounts can be doubly counted. So, it's | |
2010 | * better to wait until the end of task_move if something is going on. | |
2011 | */ | |
2012 | if (mem_cgroup_wait_acct_move(mem_over_limit)) | |
2013 | goto retry; | |
2014 | ||
9b130619 JW |
2015 | if (nr_retries--) |
2016 | goto retry; | |
2017 | ||
06b078fc | 2018 | if (gfp_mask & __GFP_NOFAIL) |
10d53c74 | 2019 | goto force; |
06b078fc | 2020 | |
6539cc05 | 2021 | if (fatal_signal_pending(current)) |
10d53c74 | 2022 | goto force; |
6539cc05 | 2023 | |
e27be240 | 2024 | memcg_memory_event(mem_over_limit, MEMCG_OOM); |
241994ed | 2025 | |
3608de07 JM |
2026 | mem_cgroup_oom(mem_over_limit, gfp_mask, |
2027 | get_order(nr_pages * PAGE_SIZE)); | |
7a81b88c | 2028 | nomem: |
6d1fdc48 | 2029 | if (!(gfp_mask & __GFP_NOFAIL)) |
3168ecbe | 2030 | return -ENOMEM; |
10d53c74 TH |
2031 | force: |
2032 | /* | |
2033 | * The allocation either can't fail or will lead to more memory | |
2034 | * being freed very soon. Allow memory usage go over the limit | |
2035 | * temporarily by force charging it. | |
2036 | */ | |
2037 | page_counter_charge(&memcg->memory, nr_pages); | |
7941d214 | 2038 | if (do_memsw_account()) |
10d53c74 TH |
2039 | page_counter_charge(&memcg->memsw, nr_pages); |
2040 | css_get_many(&memcg->css, nr_pages); | |
2041 | ||
2042 | return 0; | |
6539cc05 JW |
2043 | |
2044 | done_restock: | |
e8ea14cc | 2045 | css_get_many(&memcg->css, batch); |
6539cc05 JW |
2046 | if (batch > nr_pages) |
2047 | refill_stock(memcg, batch - nr_pages); | |
b23afb93 | 2048 | |
241994ed | 2049 | /* |
b23afb93 TH |
2050 | * If the hierarchy is above the normal consumption range, schedule |
2051 | * reclaim on returning to userland. We can perform reclaim here | |
71baba4b | 2052 | * if __GFP_RECLAIM but let's always punt for simplicity and so that |
b23afb93 TH |
2053 | * GFP_KERNEL can consistently be used during reclaim. @memcg is |
2054 | * not recorded as it most likely matches current's and won't | |
2055 | * change in the meantime. As high limit is checked again before | |
2056 | * reclaim, the cost of mismatch is negligible. | |
241994ed JW |
2057 | */ |
2058 | do { | |
b23afb93 | 2059 | if (page_counter_read(&memcg->memory) > memcg->high) { |
f7e1cb6e JW |
2060 | /* Don't bother a random interrupted task */ |
2061 | if (in_interrupt()) { | |
2062 | schedule_work(&memcg->high_work); | |
2063 | break; | |
2064 | } | |
9516a18a | 2065 | current->memcg_nr_pages_over_high += batch; |
b23afb93 TH |
2066 | set_notify_resume(current); |
2067 | break; | |
2068 | } | |
241994ed | 2069 | } while ((memcg = parent_mem_cgroup(memcg))); |
10d53c74 TH |
2070 | |
2071 | return 0; | |
7a81b88c | 2072 | } |
8a9f3ccd | 2073 | |
00501b53 | 2074 | static void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages) |
a3032a2c | 2075 | { |
ce00a967 JW |
2076 | if (mem_cgroup_is_root(memcg)) |
2077 | return; | |
2078 | ||
3e32cb2e | 2079 | page_counter_uncharge(&memcg->memory, nr_pages); |
7941d214 | 2080 | if (do_memsw_account()) |
3e32cb2e | 2081 | page_counter_uncharge(&memcg->memsw, nr_pages); |
ce00a967 | 2082 | |
e8ea14cc | 2083 | css_put_many(&memcg->css, nr_pages); |
d01dd17f KH |
2084 | } |
2085 | ||
0a31bc97 JW |
2086 | static void lock_page_lru(struct page *page, int *isolated) |
2087 | { | |
2088 | struct zone *zone = page_zone(page); | |
2089 | ||
a52633d8 | 2090 | spin_lock_irq(zone_lru_lock(zone)); |
0a31bc97 JW |
2091 | if (PageLRU(page)) { |
2092 | struct lruvec *lruvec; | |
2093 | ||
599d0c95 | 2094 | lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat); |
0a31bc97 JW |
2095 | ClearPageLRU(page); |
2096 | del_page_from_lru_list(page, lruvec, page_lru(page)); | |
2097 | *isolated = 1; | |
2098 | } else | |
2099 | *isolated = 0; | |
2100 | } | |
2101 | ||
2102 | static void unlock_page_lru(struct page *page, int isolated) | |
2103 | { | |
2104 | struct zone *zone = page_zone(page); | |
2105 | ||
2106 | if (isolated) { | |
2107 | struct lruvec *lruvec; | |
2108 | ||
599d0c95 | 2109 | lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat); |
0a31bc97 JW |
2110 | VM_BUG_ON_PAGE(PageLRU(page), page); |
2111 | SetPageLRU(page); | |
2112 | add_page_to_lru_list(page, lruvec, page_lru(page)); | |
2113 | } | |
a52633d8 | 2114 | spin_unlock_irq(zone_lru_lock(zone)); |
0a31bc97 JW |
2115 | } |
2116 | ||
00501b53 | 2117 | static void commit_charge(struct page *page, struct mem_cgroup *memcg, |
6abb5a86 | 2118 | bool lrucare) |
7a81b88c | 2119 | { |
0a31bc97 | 2120 | int isolated; |
9ce70c02 | 2121 | |
1306a85a | 2122 | VM_BUG_ON_PAGE(page->mem_cgroup, page); |
9ce70c02 HD |
2123 | |
2124 | /* | |
2125 | * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page | |
2126 | * may already be on some other mem_cgroup's LRU. Take care of it. | |
2127 | */ | |
0a31bc97 JW |
2128 | if (lrucare) |
2129 | lock_page_lru(page, &isolated); | |
9ce70c02 | 2130 | |
0a31bc97 JW |
2131 | /* |
2132 | * Nobody should be changing or seriously looking at | |
1306a85a | 2133 | * page->mem_cgroup at this point: |
0a31bc97 JW |
2134 | * |
2135 | * - the page is uncharged | |
2136 | * | |
2137 | * - the page is off-LRU | |
2138 | * | |
2139 | * - an anonymous fault has exclusive page access, except for | |
2140 | * a locked page table | |
2141 | * | |
2142 | * - a page cache insertion, a swapin fault, or a migration | |
2143 | * have the page locked | |
2144 | */ | |
1306a85a | 2145 | page->mem_cgroup = memcg; |
9ce70c02 | 2146 | |
0a31bc97 JW |
2147 | if (lrucare) |
2148 | unlock_page_lru(page, isolated); | |
7a81b88c | 2149 | } |
66e1707b | 2150 | |
127424c8 | 2151 | #ifndef CONFIG_SLOB |
f3bb3043 | 2152 | static int memcg_alloc_cache_id(void) |
55007d84 | 2153 | { |
f3bb3043 VD |
2154 | int id, size; |
2155 | int err; | |
2156 | ||
dbcf73e2 | 2157 | id = ida_simple_get(&memcg_cache_ida, |
f3bb3043 VD |
2158 | 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL); |
2159 | if (id < 0) | |
2160 | return id; | |
55007d84 | 2161 | |
dbcf73e2 | 2162 | if (id < memcg_nr_cache_ids) |
f3bb3043 VD |
2163 | return id; |
2164 | ||
2165 | /* | |
2166 | * There's no space for the new id in memcg_caches arrays, | |
2167 | * so we have to grow them. | |
2168 | */ | |
05257a1a | 2169 | down_write(&memcg_cache_ids_sem); |
f3bb3043 VD |
2170 | |
2171 | size = 2 * (id + 1); | |
55007d84 GC |
2172 | if (size < MEMCG_CACHES_MIN_SIZE) |
2173 | size = MEMCG_CACHES_MIN_SIZE; | |
2174 | else if (size > MEMCG_CACHES_MAX_SIZE) | |
2175 | size = MEMCG_CACHES_MAX_SIZE; | |
2176 | ||
f3bb3043 | 2177 | err = memcg_update_all_caches(size); |
60d3fd32 VD |
2178 | if (!err) |
2179 | err = memcg_update_all_list_lrus(size); | |
05257a1a VD |
2180 | if (!err) |
2181 | memcg_nr_cache_ids = size; | |
2182 | ||
2183 | up_write(&memcg_cache_ids_sem); | |
2184 | ||
f3bb3043 | 2185 | if (err) { |
dbcf73e2 | 2186 | ida_simple_remove(&memcg_cache_ida, id); |
f3bb3043 VD |
2187 | return err; |
2188 | } | |
2189 | return id; | |
2190 | } | |
2191 | ||
2192 | static void memcg_free_cache_id(int id) | |
2193 | { | |
dbcf73e2 | 2194 | ida_simple_remove(&memcg_cache_ida, id); |
55007d84 GC |
2195 | } |
2196 | ||
d5b3cf71 | 2197 | struct memcg_kmem_cache_create_work { |
5722d094 VD |
2198 | struct mem_cgroup *memcg; |
2199 | struct kmem_cache *cachep; | |
2200 | struct work_struct work; | |
2201 | }; | |
2202 | ||
d5b3cf71 | 2203 | static void memcg_kmem_cache_create_func(struct work_struct *w) |
d7f25f8a | 2204 | { |
d5b3cf71 VD |
2205 | struct memcg_kmem_cache_create_work *cw = |
2206 | container_of(w, struct memcg_kmem_cache_create_work, work); | |
5722d094 VD |
2207 | struct mem_cgroup *memcg = cw->memcg; |
2208 | struct kmem_cache *cachep = cw->cachep; | |
d7f25f8a | 2209 | |
d5b3cf71 | 2210 | memcg_create_kmem_cache(memcg, cachep); |
bd673145 | 2211 | |
5722d094 | 2212 | css_put(&memcg->css); |
d7f25f8a GC |
2213 | kfree(cw); |
2214 | } | |
2215 | ||
2216 | /* | |
2217 | * Enqueue the creation of a per-memcg kmem_cache. | |
d7f25f8a | 2218 | */ |
d5b3cf71 VD |
2219 | static void __memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg, |
2220 | struct kmem_cache *cachep) | |
d7f25f8a | 2221 | { |
d5b3cf71 | 2222 | struct memcg_kmem_cache_create_work *cw; |
d7f25f8a | 2223 | |
c892fd82 | 2224 | cw = kmalloc(sizeof(*cw), GFP_NOWAIT | __GFP_NOWARN); |
8135be5a | 2225 | if (!cw) |
d7f25f8a | 2226 | return; |
8135be5a VD |
2227 | |
2228 | css_get(&memcg->css); | |
d7f25f8a GC |
2229 | |
2230 | cw->memcg = memcg; | |
2231 | cw->cachep = cachep; | |
d5b3cf71 | 2232 | INIT_WORK(&cw->work, memcg_kmem_cache_create_func); |
d7f25f8a | 2233 | |
17cc4dfe | 2234 | queue_work(memcg_kmem_cache_wq, &cw->work); |
d7f25f8a GC |
2235 | } |
2236 | ||
d5b3cf71 VD |
2237 | static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg, |
2238 | struct kmem_cache *cachep) | |
0e9d92f2 GC |
2239 | { |
2240 | /* | |
2241 | * We need to stop accounting when we kmalloc, because if the | |
2242 | * corresponding kmalloc cache is not yet created, the first allocation | |
d5b3cf71 | 2243 | * in __memcg_schedule_kmem_cache_create will recurse. |
0e9d92f2 GC |
2244 | * |
2245 | * However, it is better to enclose the whole function. Depending on | |
2246 | * the debugging options enabled, INIT_WORK(), for instance, can | |
2247 | * trigger an allocation. This too, will make us recurse. Because at | |
2248 | * this point we can't allow ourselves back into memcg_kmem_get_cache, | |
2249 | * the safest choice is to do it like this, wrapping the whole function. | |
2250 | */ | |
6f185c29 | 2251 | current->memcg_kmem_skip_account = 1; |
d5b3cf71 | 2252 | __memcg_schedule_kmem_cache_create(memcg, cachep); |
6f185c29 | 2253 | current->memcg_kmem_skip_account = 0; |
0e9d92f2 | 2254 | } |
c67a8a68 | 2255 | |
45264778 VD |
2256 | static inline bool memcg_kmem_bypass(void) |
2257 | { | |
2258 | if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD)) | |
2259 | return true; | |
2260 | return false; | |
2261 | } | |
2262 | ||
2263 | /** | |
2264 | * memcg_kmem_get_cache: select the correct per-memcg cache for allocation | |
2265 | * @cachep: the original global kmem cache | |
2266 | * | |
d7f25f8a GC |
2267 | * Return the kmem_cache we're supposed to use for a slab allocation. |
2268 | * We try to use the current memcg's version of the cache. | |
2269 | * | |
45264778 VD |
2270 | * If the cache does not exist yet, if we are the first user of it, we |
2271 | * create it asynchronously in a workqueue and let the current allocation | |
2272 | * go through with the original cache. | |
d7f25f8a | 2273 | * |
45264778 VD |
2274 | * This function takes a reference to the cache it returns to assure it |
2275 | * won't get destroyed while we are working with it. Once the caller is | |
2276 | * done with it, memcg_kmem_put_cache() must be called to release the | |
2277 | * reference. | |
d7f25f8a | 2278 | */ |
45264778 | 2279 | struct kmem_cache *memcg_kmem_get_cache(struct kmem_cache *cachep) |
d7f25f8a GC |
2280 | { |
2281 | struct mem_cgroup *memcg; | |
959c8963 | 2282 | struct kmem_cache *memcg_cachep; |
2a4db7eb | 2283 | int kmemcg_id; |
d7f25f8a | 2284 | |
f7ce3190 | 2285 | VM_BUG_ON(!is_root_cache(cachep)); |
d7f25f8a | 2286 | |
45264778 | 2287 | if (memcg_kmem_bypass()) |
230e9fc2 VD |
2288 | return cachep; |
2289 | ||
9d100c5e | 2290 | if (current->memcg_kmem_skip_account) |
0e9d92f2 GC |
2291 | return cachep; |
2292 | ||
d46eb14b | 2293 | memcg = get_mem_cgroup_from_current(); |
4db0c3c2 | 2294 | kmemcg_id = READ_ONCE(memcg->kmemcg_id); |
2a4db7eb | 2295 | if (kmemcg_id < 0) |
ca0dde97 | 2296 | goto out; |
d7f25f8a | 2297 | |
2a4db7eb | 2298 | memcg_cachep = cache_from_memcg_idx(cachep, kmemcg_id); |
8135be5a VD |
2299 | if (likely(memcg_cachep)) |
2300 | return memcg_cachep; | |
ca0dde97 LZ |
2301 | |
2302 | /* | |
2303 | * If we are in a safe context (can wait, and not in interrupt | |
2304 | * context), we could be be predictable and return right away. | |
2305 | * This would guarantee that the allocation being performed | |
2306 | * already belongs in the new cache. | |
2307 | * | |
2308 | * However, there are some clashes that can arrive from locking. | |
2309 | * For instance, because we acquire the slab_mutex while doing | |
776ed0f0 VD |
2310 | * memcg_create_kmem_cache, this means no further allocation |
2311 | * could happen with the slab_mutex held. So it's better to | |
2312 | * defer everything. | |
ca0dde97 | 2313 | */ |
d5b3cf71 | 2314 | memcg_schedule_kmem_cache_create(memcg, cachep); |
ca0dde97 | 2315 | out: |
8135be5a | 2316 | css_put(&memcg->css); |
ca0dde97 | 2317 | return cachep; |
d7f25f8a | 2318 | } |
d7f25f8a | 2319 | |
45264778 VD |
2320 | /** |
2321 | * memcg_kmem_put_cache: drop reference taken by memcg_kmem_get_cache | |
2322 | * @cachep: the cache returned by memcg_kmem_get_cache | |
2323 | */ | |
2324 | void memcg_kmem_put_cache(struct kmem_cache *cachep) | |
8135be5a VD |
2325 | { |
2326 | if (!is_root_cache(cachep)) | |
f7ce3190 | 2327 | css_put(&cachep->memcg_params.memcg->css); |
8135be5a VD |
2328 | } |
2329 | ||
45264778 | 2330 | /** |
b213b54f | 2331 | * memcg_kmem_charge_memcg: charge a kmem page |
45264778 VD |
2332 | * @page: page to charge |
2333 | * @gfp: reclaim mode | |
2334 | * @order: allocation order | |
2335 | * @memcg: memory cgroup to charge | |
2336 | * | |
2337 | * Returns 0 on success, an error code on failure. | |
2338 | */ | |
2339 | int memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order, | |
2340 | struct mem_cgroup *memcg) | |
7ae1e1d0 | 2341 | { |
f3ccb2c4 VD |
2342 | unsigned int nr_pages = 1 << order; |
2343 | struct page_counter *counter; | |
7ae1e1d0 GC |
2344 | int ret; |
2345 | ||
f3ccb2c4 | 2346 | ret = try_charge(memcg, gfp, nr_pages); |
52c29b04 | 2347 | if (ret) |
f3ccb2c4 | 2348 | return ret; |
52c29b04 JW |
2349 | |
2350 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && | |
2351 | !page_counter_try_charge(&memcg->kmem, nr_pages, &counter)) { | |
2352 | cancel_charge(memcg, nr_pages); | |
2353 | return -ENOMEM; | |
7ae1e1d0 GC |
2354 | } |
2355 | ||
f3ccb2c4 | 2356 | page->mem_cgroup = memcg; |
7ae1e1d0 | 2357 | |
f3ccb2c4 | 2358 | return 0; |
7ae1e1d0 GC |
2359 | } |
2360 | ||
45264778 VD |
2361 | /** |
2362 | * memcg_kmem_charge: charge a kmem page to the current memory cgroup | |
2363 | * @page: page to charge | |
2364 | * @gfp: reclaim mode | |
2365 | * @order: allocation order | |
2366 | * | |
2367 | * Returns 0 on success, an error code on failure. | |
2368 | */ | |
2369 | int memcg_kmem_charge(struct page *page, gfp_t gfp, int order) | |
7ae1e1d0 | 2370 | { |
f3ccb2c4 | 2371 | struct mem_cgroup *memcg; |
fcff7d7e | 2372 | int ret = 0; |
7ae1e1d0 | 2373 | |
45264778 VD |
2374 | if (memcg_kmem_bypass()) |
2375 | return 0; | |
2376 | ||
d46eb14b | 2377 | memcg = get_mem_cgroup_from_current(); |
c4159a75 | 2378 | if (!mem_cgroup_is_root(memcg)) { |
45264778 | 2379 | ret = memcg_kmem_charge_memcg(page, gfp, order, memcg); |
c4159a75 VD |
2380 | if (!ret) |
2381 | __SetPageKmemcg(page); | |
2382 | } | |
7ae1e1d0 | 2383 | css_put(&memcg->css); |
d05e83a6 | 2384 | return ret; |
7ae1e1d0 | 2385 | } |
45264778 VD |
2386 | /** |
2387 | * memcg_kmem_uncharge: uncharge a kmem page | |
2388 | * @page: page to uncharge | |
2389 | * @order: allocation order | |
2390 | */ | |
2391 | void memcg_kmem_uncharge(struct page *page, int order) | |
7ae1e1d0 | 2392 | { |
1306a85a | 2393 | struct mem_cgroup *memcg = page->mem_cgroup; |
f3ccb2c4 | 2394 | unsigned int nr_pages = 1 << order; |
7ae1e1d0 | 2395 | |
7ae1e1d0 GC |
2396 | if (!memcg) |
2397 | return; | |
2398 | ||
309381fe | 2399 | VM_BUG_ON_PAGE(mem_cgroup_is_root(memcg), page); |
29833315 | 2400 | |
52c29b04 JW |
2401 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
2402 | page_counter_uncharge(&memcg->kmem, nr_pages); | |
2403 | ||
f3ccb2c4 | 2404 | page_counter_uncharge(&memcg->memory, nr_pages); |
7941d214 | 2405 | if (do_memsw_account()) |
f3ccb2c4 | 2406 | page_counter_uncharge(&memcg->memsw, nr_pages); |
60d3fd32 | 2407 | |
1306a85a | 2408 | page->mem_cgroup = NULL; |
c4159a75 VD |
2409 | |
2410 | /* slab pages do not have PageKmemcg flag set */ | |
2411 | if (PageKmemcg(page)) | |
2412 | __ClearPageKmemcg(page); | |
2413 | ||
f3ccb2c4 | 2414 | css_put_many(&memcg->css, nr_pages); |
60d3fd32 | 2415 | } |
127424c8 | 2416 | #endif /* !CONFIG_SLOB */ |
7ae1e1d0 | 2417 | |
ca3e0214 KH |
2418 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
2419 | ||
ca3e0214 KH |
2420 | /* |
2421 | * Because tail pages are not marked as "used", set it. We're under | |
a52633d8 | 2422 | * zone_lru_lock and migration entries setup in all page mappings. |
ca3e0214 | 2423 | */ |
e94c8a9c | 2424 | void mem_cgroup_split_huge_fixup(struct page *head) |
ca3e0214 | 2425 | { |
e94c8a9c | 2426 | int i; |
ca3e0214 | 2427 | |
3d37c4a9 KH |
2428 | if (mem_cgroup_disabled()) |
2429 | return; | |
b070e65c | 2430 | |
29833315 | 2431 | for (i = 1; i < HPAGE_PMD_NR; i++) |
1306a85a | 2432 | head[i].mem_cgroup = head->mem_cgroup; |
b9982f8d | 2433 | |
c9019e9b | 2434 | __mod_memcg_state(head->mem_cgroup, MEMCG_RSS_HUGE, -HPAGE_PMD_NR); |
ca3e0214 | 2435 | } |
12d27107 | 2436 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
ca3e0214 | 2437 | |
c255a458 | 2438 | #ifdef CONFIG_MEMCG_SWAP |
02491447 DN |
2439 | /** |
2440 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. | |
2441 | * @entry: swap entry to be moved | |
2442 | * @from: mem_cgroup which the entry is moved from | |
2443 | * @to: mem_cgroup which the entry is moved to | |
2444 | * | |
2445 | * It succeeds only when the swap_cgroup's record for this entry is the same | |
2446 | * as the mem_cgroup's id of @from. | |
2447 | * | |
2448 | * Returns 0 on success, -EINVAL on failure. | |
2449 | * | |
3e32cb2e | 2450 | * The caller must have charged to @to, IOW, called page_counter_charge() about |
02491447 DN |
2451 | * both res and memsw, and called css_get(). |
2452 | */ | |
2453 | static int mem_cgroup_move_swap_account(swp_entry_t entry, | |
e91cbb42 | 2454 | struct mem_cgroup *from, struct mem_cgroup *to) |
02491447 DN |
2455 | { |
2456 | unsigned short old_id, new_id; | |
2457 | ||
34c00c31 LZ |
2458 | old_id = mem_cgroup_id(from); |
2459 | new_id = mem_cgroup_id(to); | |
02491447 DN |
2460 | |
2461 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { | |
c9019e9b JW |
2462 | mod_memcg_state(from, MEMCG_SWAP, -1); |
2463 | mod_memcg_state(to, MEMCG_SWAP, 1); | |
02491447 DN |
2464 | return 0; |
2465 | } | |
2466 | return -EINVAL; | |
2467 | } | |
2468 | #else | |
2469 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, | |
e91cbb42 | 2470 | struct mem_cgroup *from, struct mem_cgroup *to) |
02491447 DN |
2471 | { |
2472 | return -EINVAL; | |
2473 | } | |
8c7c6e34 | 2474 | #endif |
d13d1443 | 2475 | |
bbec2e15 | 2476 | static DEFINE_MUTEX(memcg_max_mutex); |
f212ad7c | 2477 | |
bbec2e15 RG |
2478 | static int mem_cgroup_resize_max(struct mem_cgroup *memcg, |
2479 | unsigned long max, bool memsw) | |
628f4235 | 2480 | { |
3e32cb2e | 2481 | bool enlarge = false; |
bb4a7ea2 | 2482 | bool drained = false; |
3e32cb2e | 2483 | int ret; |
c054a78c YZ |
2484 | bool limits_invariant; |
2485 | struct page_counter *counter = memsw ? &memcg->memsw : &memcg->memory; | |
81d39c20 | 2486 | |
3e32cb2e | 2487 | do { |
628f4235 KH |
2488 | if (signal_pending(current)) { |
2489 | ret = -EINTR; | |
2490 | break; | |
2491 | } | |
3e32cb2e | 2492 | |
bbec2e15 | 2493 | mutex_lock(&memcg_max_mutex); |
c054a78c YZ |
2494 | /* |
2495 | * Make sure that the new limit (memsw or memory limit) doesn't | |
bbec2e15 | 2496 | * break our basic invariant rule memory.max <= memsw.max. |
c054a78c | 2497 | */ |
bbec2e15 RG |
2498 | limits_invariant = memsw ? max >= memcg->memory.max : |
2499 | max <= memcg->memsw.max; | |
c054a78c | 2500 | if (!limits_invariant) { |
bbec2e15 | 2501 | mutex_unlock(&memcg_max_mutex); |
8c7c6e34 | 2502 | ret = -EINVAL; |
8c7c6e34 KH |
2503 | break; |
2504 | } | |
bbec2e15 | 2505 | if (max > counter->max) |
3e32cb2e | 2506 | enlarge = true; |
bbec2e15 RG |
2507 | ret = page_counter_set_max(counter, max); |
2508 | mutex_unlock(&memcg_max_mutex); | |
8c7c6e34 KH |
2509 | |
2510 | if (!ret) | |
2511 | break; | |
2512 | ||
bb4a7ea2 SB |
2513 | if (!drained) { |
2514 | drain_all_stock(memcg); | |
2515 | drained = true; | |
2516 | continue; | |
2517 | } | |
2518 | ||
1ab5c056 AR |
2519 | if (!try_to_free_mem_cgroup_pages(memcg, 1, |
2520 | GFP_KERNEL, !memsw)) { | |
2521 | ret = -EBUSY; | |
2522 | break; | |
2523 | } | |
2524 | } while (true); | |
3e32cb2e | 2525 | |
3c11ecf4 KH |
2526 | if (!ret && enlarge) |
2527 | memcg_oom_recover(memcg); | |
3e32cb2e | 2528 | |
628f4235 KH |
2529 | return ret; |
2530 | } | |
2531 | ||
ef8f2327 | 2532 | unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order, |
0608f43d AM |
2533 | gfp_t gfp_mask, |
2534 | unsigned long *total_scanned) | |
2535 | { | |
2536 | unsigned long nr_reclaimed = 0; | |
ef8f2327 | 2537 | struct mem_cgroup_per_node *mz, *next_mz = NULL; |
0608f43d AM |
2538 | unsigned long reclaimed; |
2539 | int loop = 0; | |
ef8f2327 | 2540 | struct mem_cgroup_tree_per_node *mctz; |
3e32cb2e | 2541 | unsigned long excess; |
0608f43d AM |
2542 | unsigned long nr_scanned; |
2543 | ||
2544 | if (order > 0) | |
2545 | return 0; | |
2546 | ||
ef8f2327 | 2547 | mctz = soft_limit_tree_node(pgdat->node_id); |
d6507ff5 MH |
2548 | |
2549 | /* | |
2550 | * Do not even bother to check the largest node if the root | |
2551 | * is empty. Do it lockless to prevent lock bouncing. Races | |
2552 | * are acceptable as soft limit is best effort anyway. | |
2553 | */ | |
bfc7228b | 2554 | if (!mctz || RB_EMPTY_ROOT(&mctz->rb_root)) |
d6507ff5 MH |
2555 | return 0; |
2556 | ||
0608f43d AM |
2557 | /* |
2558 | * This loop can run a while, specially if mem_cgroup's continuously | |
2559 | * keep exceeding their soft limit and putting the system under | |
2560 | * pressure | |
2561 | */ | |
2562 | do { | |
2563 | if (next_mz) | |
2564 | mz = next_mz; | |
2565 | else | |
2566 | mz = mem_cgroup_largest_soft_limit_node(mctz); | |
2567 | if (!mz) | |
2568 | break; | |
2569 | ||
2570 | nr_scanned = 0; | |
ef8f2327 | 2571 | reclaimed = mem_cgroup_soft_reclaim(mz->memcg, pgdat, |
0608f43d AM |
2572 | gfp_mask, &nr_scanned); |
2573 | nr_reclaimed += reclaimed; | |
2574 | *total_scanned += nr_scanned; | |
0a31bc97 | 2575 | spin_lock_irq(&mctz->lock); |
bc2f2e7f | 2576 | __mem_cgroup_remove_exceeded(mz, mctz); |
0608f43d AM |
2577 | |
2578 | /* | |
2579 | * If we failed to reclaim anything from this memory cgroup | |
2580 | * it is time to move on to the next cgroup | |
2581 | */ | |
2582 | next_mz = NULL; | |
bc2f2e7f VD |
2583 | if (!reclaimed) |
2584 | next_mz = __mem_cgroup_largest_soft_limit_node(mctz); | |
2585 | ||
3e32cb2e | 2586 | excess = soft_limit_excess(mz->memcg); |
0608f43d AM |
2587 | /* |
2588 | * One school of thought says that we should not add | |
2589 | * back the node to the tree if reclaim returns 0. | |
2590 | * But our reclaim could return 0, simply because due | |
2591 | * to priority we are exposing a smaller subset of | |
2592 | * memory to reclaim from. Consider this as a longer | |
2593 | * term TODO. | |
2594 | */ | |
2595 | /* If excess == 0, no tree ops */ | |
cf2c8127 | 2596 | __mem_cgroup_insert_exceeded(mz, mctz, excess); |
0a31bc97 | 2597 | spin_unlock_irq(&mctz->lock); |
0608f43d AM |
2598 | css_put(&mz->memcg->css); |
2599 | loop++; | |
2600 | /* | |
2601 | * Could not reclaim anything and there are no more | |
2602 | * mem cgroups to try or we seem to be looping without | |
2603 | * reclaiming anything. | |
2604 | */ | |
2605 | if (!nr_reclaimed && | |
2606 | (next_mz == NULL || | |
2607 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) | |
2608 | break; | |
2609 | } while (!nr_reclaimed); | |
2610 | if (next_mz) | |
2611 | css_put(&next_mz->memcg->css); | |
2612 | return nr_reclaimed; | |
2613 | } | |
2614 | ||
ea280e7b TH |
2615 | /* |
2616 | * Test whether @memcg has children, dead or alive. Note that this | |
2617 | * function doesn't care whether @memcg has use_hierarchy enabled and | |
2618 | * returns %true if there are child csses according to the cgroup | |
2619 | * hierarchy. Testing use_hierarchy is the caller's responsiblity. | |
2620 | */ | |
b5f99b53 GC |
2621 | static inline bool memcg_has_children(struct mem_cgroup *memcg) |
2622 | { | |
ea280e7b TH |
2623 | bool ret; |
2624 | ||
ea280e7b TH |
2625 | rcu_read_lock(); |
2626 | ret = css_next_child(NULL, &memcg->css); | |
2627 | rcu_read_unlock(); | |
2628 | return ret; | |
b5f99b53 GC |
2629 | } |
2630 | ||
c26251f9 | 2631 | /* |
51038171 | 2632 | * Reclaims as many pages from the given memcg as possible. |
c26251f9 MH |
2633 | * |
2634 | * Caller is responsible for holding css reference for memcg. | |
2635 | */ | |
2636 | static int mem_cgroup_force_empty(struct mem_cgroup *memcg) | |
2637 | { | |
2638 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; | |
c26251f9 | 2639 | |
c1e862c1 KH |
2640 | /* we call try-to-free pages for make this cgroup empty */ |
2641 | lru_add_drain_all(); | |
d12c60f6 JS |
2642 | |
2643 | drain_all_stock(memcg); | |
2644 | ||
f817ed48 | 2645 | /* try to free all pages in this cgroup */ |
3e32cb2e | 2646 | while (nr_retries && page_counter_read(&memcg->memory)) { |
f817ed48 | 2647 | int progress; |
c1e862c1 | 2648 | |
c26251f9 MH |
2649 | if (signal_pending(current)) |
2650 | return -EINTR; | |
2651 | ||
b70a2a21 JW |
2652 | progress = try_to_free_mem_cgroup_pages(memcg, 1, |
2653 | GFP_KERNEL, true); | |
c1e862c1 | 2654 | if (!progress) { |
f817ed48 | 2655 | nr_retries--; |
c1e862c1 | 2656 | /* maybe some writeback is necessary */ |
8aa7e847 | 2657 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
c1e862c1 | 2658 | } |
f817ed48 KH |
2659 | |
2660 | } | |
ab5196c2 MH |
2661 | |
2662 | return 0; | |
cc847582 KH |
2663 | } |
2664 | ||
6770c64e TH |
2665 | static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of, |
2666 | char *buf, size_t nbytes, | |
2667 | loff_t off) | |
c1e862c1 | 2668 | { |
6770c64e | 2669 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
c26251f9 | 2670 | |
d8423011 MH |
2671 | if (mem_cgroup_is_root(memcg)) |
2672 | return -EINVAL; | |
6770c64e | 2673 | return mem_cgroup_force_empty(memcg) ?: nbytes; |
c1e862c1 KH |
2674 | } |
2675 | ||
182446d0 TH |
2676 | static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css, |
2677 | struct cftype *cft) | |
18f59ea7 | 2678 | { |
182446d0 | 2679 | return mem_cgroup_from_css(css)->use_hierarchy; |
18f59ea7 BS |
2680 | } |
2681 | ||
182446d0 TH |
2682 | static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css, |
2683 | struct cftype *cft, u64 val) | |
18f59ea7 BS |
2684 | { |
2685 | int retval = 0; | |
182446d0 | 2686 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
5c9d535b | 2687 | struct mem_cgroup *parent_memcg = mem_cgroup_from_css(memcg->css.parent); |
18f59ea7 | 2688 | |
567fb435 | 2689 | if (memcg->use_hierarchy == val) |
0b8f73e1 | 2690 | return 0; |
567fb435 | 2691 | |
18f59ea7 | 2692 | /* |
af901ca1 | 2693 | * If parent's use_hierarchy is set, we can't make any modifications |
18f59ea7 BS |
2694 | * in the child subtrees. If it is unset, then the change can |
2695 | * occur, provided the current cgroup has no children. | |
2696 | * | |
2697 | * For the root cgroup, parent_mem is NULL, we allow value to be | |
2698 | * set if there are no children. | |
2699 | */ | |
c0ff4b85 | 2700 | if ((!parent_memcg || !parent_memcg->use_hierarchy) && |
18f59ea7 | 2701 | (val == 1 || val == 0)) { |
ea280e7b | 2702 | if (!memcg_has_children(memcg)) |
c0ff4b85 | 2703 | memcg->use_hierarchy = val; |
18f59ea7 BS |
2704 | else |
2705 | retval = -EBUSY; | |
2706 | } else | |
2707 | retval = -EINVAL; | |
567fb435 | 2708 | |
18f59ea7 BS |
2709 | return retval; |
2710 | } | |
2711 | ||
72b54e73 | 2712 | static void tree_stat(struct mem_cgroup *memcg, unsigned long *stat) |
ce00a967 JW |
2713 | { |
2714 | struct mem_cgroup *iter; | |
72b54e73 | 2715 | int i; |
ce00a967 | 2716 | |
72b54e73 | 2717 | memset(stat, 0, sizeof(*stat) * MEMCG_NR_STAT); |
ce00a967 | 2718 | |
72b54e73 VD |
2719 | for_each_mem_cgroup_tree(iter, memcg) { |
2720 | for (i = 0; i < MEMCG_NR_STAT; i++) | |
ccda7f43 | 2721 | stat[i] += memcg_page_state(iter, i); |
72b54e73 | 2722 | } |
ce00a967 JW |
2723 | } |
2724 | ||
72b54e73 | 2725 | static void tree_events(struct mem_cgroup *memcg, unsigned long *events) |
587d9f72 JW |
2726 | { |
2727 | struct mem_cgroup *iter; | |
72b54e73 | 2728 | int i; |
587d9f72 | 2729 | |
e27be240 | 2730 | memset(events, 0, sizeof(*events) * NR_VM_EVENT_ITEMS); |
587d9f72 | 2731 | |
72b54e73 | 2732 | for_each_mem_cgroup_tree(iter, memcg) { |
e27be240 | 2733 | for (i = 0; i < NR_VM_EVENT_ITEMS; i++) |
ccda7f43 | 2734 | events[i] += memcg_sum_events(iter, i); |
72b54e73 | 2735 | } |
587d9f72 JW |
2736 | } |
2737 | ||
6f646156 | 2738 | static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap) |
ce00a967 | 2739 | { |
72b54e73 | 2740 | unsigned long val = 0; |
ce00a967 | 2741 | |
3e32cb2e | 2742 | if (mem_cgroup_is_root(memcg)) { |
72b54e73 VD |
2743 | struct mem_cgroup *iter; |
2744 | ||
2745 | for_each_mem_cgroup_tree(iter, memcg) { | |
ccda7f43 JW |
2746 | val += memcg_page_state(iter, MEMCG_CACHE); |
2747 | val += memcg_page_state(iter, MEMCG_RSS); | |
72b54e73 | 2748 | if (swap) |
ccda7f43 | 2749 | val += memcg_page_state(iter, MEMCG_SWAP); |
72b54e73 | 2750 | } |
3e32cb2e | 2751 | } else { |
ce00a967 | 2752 | if (!swap) |
3e32cb2e | 2753 | val = page_counter_read(&memcg->memory); |
ce00a967 | 2754 | else |
3e32cb2e | 2755 | val = page_counter_read(&memcg->memsw); |
ce00a967 | 2756 | } |
c12176d3 | 2757 | return val; |
ce00a967 JW |
2758 | } |
2759 | ||
3e32cb2e JW |
2760 | enum { |
2761 | RES_USAGE, | |
2762 | RES_LIMIT, | |
2763 | RES_MAX_USAGE, | |
2764 | RES_FAILCNT, | |
2765 | RES_SOFT_LIMIT, | |
2766 | }; | |
ce00a967 | 2767 | |
791badbd | 2768 | static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css, |
05b84301 | 2769 | struct cftype *cft) |
8cdea7c0 | 2770 | { |
182446d0 | 2771 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
3e32cb2e | 2772 | struct page_counter *counter; |
af36f906 | 2773 | |
3e32cb2e | 2774 | switch (MEMFILE_TYPE(cft->private)) { |
8c7c6e34 | 2775 | case _MEM: |
3e32cb2e JW |
2776 | counter = &memcg->memory; |
2777 | break; | |
8c7c6e34 | 2778 | case _MEMSWAP: |
3e32cb2e JW |
2779 | counter = &memcg->memsw; |
2780 | break; | |
510fc4e1 | 2781 | case _KMEM: |
3e32cb2e | 2782 | counter = &memcg->kmem; |
510fc4e1 | 2783 | break; |
d55f90bf | 2784 | case _TCP: |
0db15298 | 2785 | counter = &memcg->tcpmem; |
d55f90bf | 2786 | break; |
8c7c6e34 KH |
2787 | default: |
2788 | BUG(); | |
8c7c6e34 | 2789 | } |
3e32cb2e JW |
2790 | |
2791 | switch (MEMFILE_ATTR(cft->private)) { | |
2792 | case RES_USAGE: | |
2793 | if (counter == &memcg->memory) | |
c12176d3 | 2794 | return (u64)mem_cgroup_usage(memcg, false) * PAGE_SIZE; |
3e32cb2e | 2795 | if (counter == &memcg->memsw) |
c12176d3 | 2796 | return (u64)mem_cgroup_usage(memcg, true) * PAGE_SIZE; |
3e32cb2e JW |
2797 | return (u64)page_counter_read(counter) * PAGE_SIZE; |
2798 | case RES_LIMIT: | |
bbec2e15 | 2799 | return (u64)counter->max * PAGE_SIZE; |
3e32cb2e JW |
2800 | case RES_MAX_USAGE: |
2801 | return (u64)counter->watermark * PAGE_SIZE; | |
2802 | case RES_FAILCNT: | |
2803 | return counter->failcnt; | |
2804 | case RES_SOFT_LIMIT: | |
2805 | return (u64)memcg->soft_limit * PAGE_SIZE; | |
2806 | default: | |
2807 | BUG(); | |
2808 | } | |
8cdea7c0 | 2809 | } |
510fc4e1 | 2810 | |
127424c8 | 2811 | #ifndef CONFIG_SLOB |
567e9ab2 | 2812 | static int memcg_online_kmem(struct mem_cgroup *memcg) |
d6441637 | 2813 | { |
d6441637 VD |
2814 | int memcg_id; |
2815 | ||
b313aeee VD |
2816 | if (cgroup_memory_nokmem) |
2817 | return 0; | |
2818 | ||
2a4db7eb | 2819 | BUG_ON(memcg->kmemcg_id >= 0); |
567e9ab2 | 2820 | BUG_ON(memcg->kmem_state); |
d6441637 | 2821 | |
f3bb3043 | 2822 | memcg_id = memcg_alloc_cache_id(); |
0b8f73e1 JW |
2823 | if (memcg_id < 0) |
2824 | return memcg_id; | |
d6441637 | 2825 | |
ef12947c | 2826 | static_branch_inc(&memcg_kmem_enabled_key); |
d6441637 | 2827 | /* |
567e9ab2 | 2828 | * A memory cgroup is considered kmem-online as soon as it gets |
900a38f0 | 2829 | * kmemcg_id. Setting the id after enabling static branching will |
d6441637 VD |
2830 | * guarantee no one starts accounting before all call sites are |
2831 | * patched. | |
2832 | */ | |
900a38f0 | 2833 | memcg->kmemcg_id = memcg_id; |
567e9ab2 | 2834 | memcg->kmem_state = KMEM_ONLINE; |
bc2791f8 | 2835 | INIT_LIST_HEAD(&memcg->kmem_caches); |
0b8f73e1 JW |
2836 | |
2837 | return 0; | |
d6441637 VD |
2838 | } |
2839 | ||
8e0a8912 JW |
2840 | static void memcg_offline_kmem(struct mem_cgroup *memcg) |
2841 | { | |
2842 | struct cgroup_subsys_state *css; | |
2843 | struct mem_cgroup *parent, *child; | |
2844 | int kmemcg_id; | |
2845 | ||
2846 | if (memcg->kmem_state != KMEM_ONLINE) | |
2847 | return; | |
2848 | /* | |
2849 | * Clear the online state before clearing memcg_caches array | |
2850 | * entries. The slab_mutex in memcg_deactivate_kmem_caches() | |
2851 | * guarantees that no cache will be created for this cgroup | |
2852 | * after we are done (see memcg_create_kmem_cache()). | |
2853 | */ | |
2854 | memcg->kmem_state = KMEM_ALLOCATED; | |
2855 | ||
2856 | memcg_deactivate_kmem_caches(memcg); | |
2857 | ||
2858 | kmemcg_id = memcg->kmemcg_id; | |
2859 | BUG_ON(kmemcg_id < 0); | |
2860 | ||
2861 | parent = parent_mem_cgroup(memcg); | |
2862 | if (!parent) | |
2863 | parent = root_mem_cgroup; | |
2864 | ||
2865 | /* | |
2866 | * Change kmemcg_id of this cgroup and all its descendants to the | |
2867 | * parent's id, and then move all entries from this cgroup's list_lrus | |
2868 | * to ones of the parent. After we have finished, all list_lrus | |
2869 | * corresponding to this cgroup are guaranteed to remain empty. The | |
2870 | * ordering is imposed by list_lru_node->lock taken by | |
2871 | * memcg_drain_all_list_lrus(). | |
2872 | */ | |
3a06bb78 | 2873 | rcu_read_lock(); /* can be called from css_free w/o cgroup_mutex */ |
8e0a8912 JW |
2874 | css_for_each_descendant_pre(css, &memcg->css) { |
2875 | child = mem_cgroup_from_css(css); | |
2876 | BUG_ON(child->kmemcg_id != kmemcg_id); | |
2877 | child->kmemcg_id = parent->kmemcg_id; | |
2878 | if (!memcg->use_hierarchy) | |
2879 | break; | |
2880 | } | |
3a06bb78 TH |
2881 | rcu_read_unlock(); |
2882 | ||
8e0a8912 JW |
2883 | memcg_drain_all_list_lrus(kmemcg_id, parent->kmemcg_id); |
2884 | ||
2885 | memcg_free_cache_id(kmemcg_id); | |
2886 | } | |
2887 | ||
2888 | static void memcg_free_kmem(struct mem_cgroup *memcg) | |
2889 | { | |
0b8f73e1 JW |
2890 | /* css_alloc() failed, offlining didn't happen */ |
2891 | if (unlikely(memcg->kmem_state == KMEM_ONLINE)) | |
2892 | memcg_offline_kmem(memcg); | |
2893 | ||
8e0a8912 JW |
2894 | if (memcg->kmem_state == KMEM_ALLOCATED) { |
2895 | memcg_destroy_kmem_caches(memcg); | |
2896 | static_branch_dec(&memcg_kmem_enabled_key); | |
2897 | WARN_ON(page_counter_read(&memcg->kmem)); | |
2898 | } | |
8e0a8912 | 2899 | } |
d6441637 | 2900 | #else |
0b8f73e1 | 2901 | static int memcg_online_kmem(struct mem_cgroup *memcg) |
127424c8 JW |
2902 | { |
2903 | return 0; | |
2904 | } | |
2905 | static void memcg_offline_kmem(struct mem_cgroup *memcg) | |
2906 | { | |
2907 | } | |
2908 | static void memcg_free_kmem(struct mem_cgroup *memcg) | |
2909 | { | |
2910 | } | |
2911 | #endif /* !CONFIG_SLOB */ | |
2912 | ||
bbec2e15 RG |
2913 | static int memcg_update_kmem_max(struct mem_cgroup *memcg, |
2914 | unsigned long max) | |
d6441637 | 2915 | { |
b313aeee | 2916 | int ret; |
127424c8 | 2917 | |
bbec2e15 RG |
2918 | mutex_lock(&memcg_max_mutex); |
2919 | ret = page_counter_set_max(&memcg->kmem, max); | |
2920 | mutex_unlock(&memcg_max_mutex); | |
127424c8 | 2921 | return ret; |
d6441637 | 2922 | } |
510fc4e1 | 2923 | |
bbec2e15 | 2924 | static int memcg_update_tcp_max(struct mem_cgroup *memcg, unsigned long max) |
d55f90bf VD |
2925 | { |
2926 | int ret; | |
2927 | ||
bbec2e15 | 2928 | mutex_lock(&memcg_max_mutex); |
d55f90bf | 2929 | |
bbec2e15 | 2930 | ret = page_counter_set_max(&memcg->tcpmem, max); |
d55f90bf VD |
2931 | if (ret) |
2932 | goto out; | |
2933 | ||
0db15298 | 2934 | if (!memcg->tcpmem_active) { |
d55f90bf VD |
2935 | /* |
2936 | * The active flag needs to be written after the static_key | |
2937 | * update. This is what guarantees that the socket activation | |
2d758073 JW |
2938 | * function is the last one to run. See mem_cgroup_sk_alloc() |
2939 | * for details, and note that we don't mark any socket as | |
2940 | * belonging to this memcg until that flag is up. | |
d55f90bf VD |
2941 | * |
2942 | * We need to do this, because static_keys will span multiple | |
2943 | * sites, but we can't control their order. If we mark a socket | |
2944 | * as accounted, but the accounting functions are not patched in | |
2945 | * yet, we'll lose accounting. | |
2946 | * | |
2d758073 | 2947 | * We never race with the readers in mem_cgroup_sk_alloc(), |
d55f90bf VD |
2948 | * because when this value change, the code to process it is not |
2949 | * patched in yet. | |
2950 | */ | |
2951 | static_branch_inc(&memcg_sockets_enabled_key); | |
0db15298 | 2952 | memcg->tcpmem_active = true; |
d55f90bf VD |
2953 | } |
2954 | out: | |
bbec2e15 | 2955 | mutex_unlock(&memcg_max_mutex); |
d55f90bf VD |
2956 | return ret; |
2957 | } | |
d55f90bf | 2958 | |
628f4235 KH |
2959 | /* |
2960 | * The user of this function is... | |
2961 | * RES_LIMIT. | |
2962 | */ | |
451af504 TH |
2963 | static ssize_t mem_cgroup_write(struct kernfs_open_file *of, |
2964 | char *buf, size_t nbytes, loff_t off) | |
8cdea7c0 | 2965 | { |
451af504 | 2966 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
3e32cb2e | 2967 | unsigned long nr_pages; |
628f4235 KH |
2968 | int ret; |
2969 | ||
451af504 | 2970 | buf = strstrip(buf); |
650c5e56 | 2971 | ret = page_counter_memparse(buf, "-1", &nr_pages); |
3e32cb2e JW |
2972 | if (ret) |
2973 | return ret; | |
af36f906 | 2974 | |
3e32cb2e | 2975 | switch (MEMFILE_ATTR(of_cft(of)->private)) { |
628f4235 | 2976 | case RES_LIMIT: |
4b3bde4c BS |
2977 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
2978 | ret = -EINVAL; | |
2979 | break; | |
2980 | } | |
3e32cb2e JW |
2981 | switch (MEMFILE_TYPE(of_cft(of)->private)) { |
2982 | case _MEM: | |
bbec2e15 | 2983 | ret = mem_cgroup_resize_max(memcg, nr_pages, false); |
8c7c6e34 | 2984 | break; |
3e32cb2e | 2985 | case _MEMSWAP: |
bbec2e15 | 2986 | ret = mem_cgroup_resize_max(memcg, nr_pages, true); |
296c81d8 | 2987 | break; |
3e32cb2e | 2988 | case _KMEM: |
bbec2e15 | 2989 | ret = memcg_update_kmem_max(memcg, nr_pages); |
3e32cb2e | 2990 | break; |
d55f90bf | 2991 | case _TCP: |
bbec2e15 | 2992 | ret = memcg_update_tcp_max(memcg, nr_pages); |
d55f90bf | 2993 | break; |
3e32cb2e | 2994 | } |
296c81d8 | 2995 | break; |
3e32cb2e JW |
2996 | case RES_SOFT_LIMIT: |
2997 | memcg->soft_limit = nr_pages; | |
2998 | ret = 0; | |
628f4235 KH |
2999 | break; |
3000 | } | |
451af504 | 3001 | return ret ?: nbytes; |
8cdea7c0 BS |
3002 | } |
3003 | ||
6770c64e TH |
3004 | static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf, |
3005 | size_t nbytes, loff_t off) | |
c84872e1 | 3006 | { |
6770c64e | 3007 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
3e32cb2e | 3008 | struct page_counter *counter; |
c84872e1 | 3009 | |
3e32cb2e JW |
3010 | switch (MEMFILE_TYPE(of_cft(of)->private)) { |
3011 | case _MEM: | |
3012 | counter = &memcg->memory; | |
3013 | break; | |
3014 | case _MEMSWAP: | |
3015 | counter = &memcg->memsw; | |
3016 | break; | |
3017 | case _KMEM: | |
3018 | counter = &memcg->kmem; | |
3019 | break; | |
d55f90bf | 3020 | case _TCP: |
0db15298 | 3021 | counter = &memcg->tcpmem; |
d55f90bf | 3022 | break; |
3e32cb2e JW |
3023 | default: |
3024 | BUG(); | |
3025 | } | |
af36f906 | 3026 | |
3e32cb2e | 3027 | switch (MEMFILE_ATTR(of_cft(of)->private)) { |
29f2a4da | 3028 | case RES_MAX_USAGE: |
3e32cb2e | 3029 | page_counter_reset_watermark(counter); |
29f2a4da PE |
3030 | break; |
3031 | case RES_FAILCNT: | |
3e32cb2e | 3032 | counter->failcnt = 0; |
29f2a4da | 3033 | break; |
3e32cb2e JW |
3034 | default: |
3035 | BUG(); | |
29f2a4da | 3036 | } |
f64c3f54 | 3037 | |
6770c64e | 3038 | return nbytes; |
c84872e1 PE |
3039 | } |
3040 | ||
182446d0 | 3041 | static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css, |
7dc74be0 DN |
3042 | struct cftype *cft) |
3043 | { | |
182446d0 | 3044 | return mem_cgroup_from_css(css)->move_charge_at_immigrate; |
7dc74be0 DN |
3045 | } |
3046 | ||
02491447 | 3047 | #ifdef CONFIG_MMU |
182446d0 | 3048 | static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css, |
7dc74be0 DN |
3049 | struct cftype *cft, u64 val) |
3050 | { | |
182446d0 | 3051 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
7dc74be0 | 3052 | |
1dfab5ab | 3053 | if (val & ~MOVE_MASK) |
7dc74be0 | 3054 | return -EINVAL; |
ee5e8472 | 3055 | |
7dc74be0 | 3056 | /* |
ee5e8472 GC |
3057 | * No kind of locking is needed in here, because ->can_attach() will |
3058 | * check this value once in the beginning of the process, and then carry | |
3059 | * on with stale data. This means that changes to this value will only | |
3060 | * affect task migrations starting after the change. | |
7dc74be0 | 3061 | */ |
c0ff4b85 | 3062 | memcg->move_charge_at_immigrate = val; |
7dc74be0 DN |
3063 | return 0; |
3064 | } | |
02491447 | 3065 | #else |
182446d0 | 3066 | static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css, |
02491447 DN |
3067 | struct cftype *cft, u64 val) |
3068 | { | |
3069 | return -ENOSYS; | |
3070 | } | |
3071 | #endif | |
7dc74be0 | 3072 | |
406eb0c9 | 3073 | #ifdef CONFIG_NUMA |
2da8ca82 | 3074 | static int memcg_numa_stat_show(struct seq_file *m, void *v) |
406eb0c9 | 3075 | { |
25485de6 GT |
3076 | struct numa_stat { |
3077 | const char *name; | |
3078 | unsigned int lru_mask; | |
3079 | }; | |
3080 | ||
3081 | static const struct numa_stat stats[] = { | |
3082 | { "total", LRU_ALL }, | |
3083 | { "file", LRU_ALL_FILE }, | |
3084 | { "anon", LRU_ALL_ANON }, | |
3085 | { "unevictable", BIT(LRU_UNEVICTABLE) }, | |
3086 | }; | |
3087 | const struct numa_stat *stat; | |
406eb0c9 | 3088 | int nid; |
25485de6 | 3089 | unsigned long nr; |
2da8ca82 | 3090 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); |
406eb0c9 | 3091 | |
25485de6 GT |
3092 | for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) { |
3093 | nr = mem_cgroup_nr_lru_pages(memcg, stat->lru_mask); | |
3094 | seq_printf(m, "%s=%lu", stat->name, nr); | |
3095 | for_each_node_state(nid, N_MEMORY) { | |
3096 | nr = mem_cgroup_node_nr_lru_pages(memcg, nid, | |
3097 | stat->lru_mask); | |
3098 | seq_printf(m, " N%d=%lu", nid, nr); | |
3099 | } | |
3100 | seq_putc(m, '\n'); | |
406eb0c9 | 3101 | } |
406eb0c9 | 3102 | |
071aee13 YH |
3103 | for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) { |
3104 | struct mem_cgroup *iter; | |
3105 | ||
3106 | nr = 0; | |
3107 | for_each_mem_cgroup_tree(iter, memcg) | |
3108 | nr += mem_cgroup_nr_lru_pages(iter, stat->lru_mask); | |
3109 | seq_printf(m, "hierarchical_%s=%lu", stat->name, nr); | |
3110 | for_each_node_state(nid, N_MEMORY) { | |
3111 | nr = 0; | |
3112 | for_each_mem_cgroup_tree(iter, memcg) | |
3113 | nr += mem_cgroup_node_nr_lru_pages( | |
3114 | iter, nid, stat->lru_mask); | |
3115 | seq_printf(m, " N%d=%lu", nid, nr); | |
3116 | } | |
3117 | seq_putc(m, '\n'); | |
406eb0c9 | 3118 | } |
406eb0c9 | 3119 | |
406eb0c9 YH |
3120 | return 0; |
3121 | } | |
3122 | #endif /* CONFIG_NUMA */ | |
3123 | ||
df0e53d0 | 3124 | /* Universal VM events cgroup1 shows, original sort order */ |
8dd53fd3 | 3125 | static const unsigned int memcg1_events[] = { |
df0e53d0 JW |
3126 | PGPGIN, |
3127 | PGPGOUT, | |
3128 | PGFAULT, | |
3129 | PGMAJFAULT, | |
3130 | }; | |
3131 | ||
3132 | static const char *const memcg1_event_names[] = { | |
3133 | "pgpgin", | |
3134 | "pgpgout", | |
3135 | "pgfault", | |
3136 | "pgmajfault", | |
3137 | }; | |
3138 | ||
2da8ca82 | 3139 | static int memcg_stat_show(struct seq_file *m, void *v) |
d2ceb9b7 | 3140 | { |
2da8ca82 | 3141 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); |
3e32cb2e | 3142 | unsigned long memory, memsw; |
af7c4b0e JW |
3143 | struct mem_cgroup *mi; |
3144 | unsigned int i; | |
406eb0c9 | 3145 | |
71cd3113 | 3146 | BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) != ARRAY_SIZE(memcg1_stats)); |
70bc068c RS |
3147 | BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS); |
3148 | ||
71cd3113 JW |
3149 | for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) { |
3150 | if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account()) | |
1dd3a273 | 3151 | continue; |
71cd3113 | 3152 | seq_printf(m, "%s %lu\n", memcg1_stat_names[i], |
ccda7f43 | 3153 | memcg_page_state(memcg, memcg1_stats[i]) * |
71cd3113 | 3154 | PAGE_SIZE); |
1dd3a273 | 3155 | } |
7b854121 | 3156 | |
df0e53d0 JW |
3157 | for (i = 0; i < ARRAY_SIZE(memcg1_events); i++) |
3158 | seq_printf(m, "%s %lu\n", memcg1_event_names[i], | |
ccda7f43 | 3159 | memcg_sum_events(memcg, memcg1_events[i])); |
af7c4b0e JW |
3160 | |
3161 | for (i = 0; i < NR_LRU_LISTS; i++) | |
3162 | seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i], | |
3163 | mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE); | |
3164 | ||
14067bb3 | 3165 | /* Hierarchical information */ |
3e32cb2e JW |
3166 | memory = memsw = PAGE_COUNTER_MAX; |
3167 | for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) { | |
bbec2e15 RG |
3168 | memory = min(memory, mi->memory.max); |
3169 | memsw = min(memsw, mi->memsw.max); | |
fee7b548 | 3170 | } |
3e32cb2e JW |
3171 | seq_printf(m, "hierarchical_memory_limit %llu\n", |
3172 | (u64)memory * PAGE_SIZE); | |
7941d214 | 3173 | if (do_memsw_account()) |
3e32cb2e JW |
3174 | seq_printf(m, "hierarchical_memsw_limit %llu\n", |
3175 | (u64)memsw * PAGE_SIZE); | |
7f016ee8 | 3176 | |
71cd3113 | 3177 | for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) { |
484ebb3b | 3178 | unsigned long long val = 0; |
af7c4b0e | 3179 | |
71cd3113 | 3180 | if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account()) |
1dd3a273 | 3181 | continue; |
af7c4b0e | 3182 | for_each_mem_cgroup_tree(mi, memcg) |
ccda7f43 | 3183 | val += memcg_page_state(mi, memcg1_stats[i]) * |
71cd3113 JW |
3184 | PAGE_SIZE; |
3185 | seq_printf(m, "total_%s %llu\n", memcg1_stat_names[i], val); | |
af7c4b0e JW |
3186 | } |
3187 | ||
df0e53d0 | 3188 | for (i = 0; i < ARRAY_SIZE(memcg1_events); i++) { |
af7c4b0e JW |
3189 | unsigned long long val = 0; |
3190 | ||
3191 | for_each_mem_cgroup_tree(mi, memcg) | |
ccda7f43 | 3192 | val += memcg_sum_events(mi, memcg1_events[i]); |
df0e53d0 | 3193 | seq_printf(m, "total_%s %llu\n", memcg1_event_names[i], val); |
af7c4b0e JW |
3194 | } |
3195 | ||
3196 | for (i = 0; i < NR_LRU_LISTS; i++) { | |
3197 | unsigned long long val = 0; | |
3198 | ||
3199 | for_each_mem_cgroup_tree(mi, memcg) | |
3200 | val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE; | |
3201 | seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val); | |
1dd3a273 | 3202 | } |
14067bb3 | 3203 | |
7f016ee8 | 3204 | #ifdef CONFIG_DEBUG_VM |
7f016ee8 | 3205 | { |
ef8f2327 MG |
3206 | pg_data_t *pgdat; |
3207 | struct mem_cgroup_per_node *mz; | |
89abfab1 | 3208 | struct zone_reclaim_stat *rstat; |
7f016ee8 KM |
3209 | unsigned long recent_rotated[2] = {0, 0}; |
3210 | unsigned long recent_scanned[2] = {0, 0}; | |
3211 | ||
ef8f2327 MG |
3212 | for_each_online_pgdat(pgdat) { |
3213 | mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id); | |
3214 | rstat = &mz->lruvec.reclaim_stat; | |
7f016ee8 | 3215 | |
ef8f2327 MG |
3216 | recent_rotated[0] += rstat->recent_rotated[0]; |
3217 | recent_rotated[1] += rstat->recent_rotated[1]; | |
3218 | recent_scanned[0] += rstat->recent_scanned[0]; | |
3219 | recent_scanned[1] += rstat->recent_scanned[1]; | |
3220 | } | |
78ccf5b5 JW |
3221 | seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]); |
3222 | seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]); | |
3223 | seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]); | |
3224 | seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]); | |
7f016ee8 KM |
3225 | } |
3226 | #endif | |
3227 | ||
d2ceb9b7 KH |
3228 | return 0; |
3229 | } | |
3230 | ||
182446d0 TH |
3231 | static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css, |
3232 | struct cftype *cft) | |
a7885eb8 | 3233 | { |
182446d0 | 3234 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
a7885eb8 | 3235 | |
1f4c025b | 3236 | return mem_cgroup_swappiness(memcg); |
a7885eb8 KM |
3237 | } |
3238 | ||
182446d0 TH |
3239 | static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css, |
3240 | struct cftype *cft, u64 val) | |
a7885eb8 | 3241 | { |
182446d0 | 3242 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
a7885eb8 | 3243 | |
3dae7fec | 3244 | if (val > 100) |
a7885eb8 KM |
3245 | return -EINVAL; |
3246 | ||
14208b0e | 3247 | if (css->parent) |
3dae7fec JW |
3248 | memcg->swappiness = val; |
3249 | else | |
3250 | vm_swappiness = val; | |
068b38c1 | 3251 | |
a7885eb8 KM |
3252 | return 0; |
3253 | } | |
3254 | ||
2e72b634 KS |
3255 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
3256 | { | |
3257 | struct mem_cgroup_threshold_ary *t; | |
3e32cb2e | 3258 | unsigned long usage; |
2e72b634 KS |
3259 | int i; |
3260 | ||
3261 | rcu_read_lock(); | |
3262 | if (!swap) | |
2c488db2 | 3263 | t = rcu_dereference(memcg->thresholds.primary); |
2e72b634 | 3264 | else |
2c488db2 | 3265 | t = rcu_dereference(memcg->memsw_thresholds.primary); |
2e72b634 KS |
3266 | |
3267 | if (!t) | |
3268 | goto unlock; | |
3269 | ||
ce00a967 | 3270 | usage = mem_cgroup_usage(memcg, swap); |
2e72b634 KS |
3271 | |
3272 | /* | |
748dad36 | 3273 | * current_threshold points to threshold just below or equal to usage. |
2e72b634 KS |
3274 | * If it's not true, a threshold was crossed after last |
3275 | * call of __mem_cgroup_threshold(). | |
3276 | */ | |
5407a562 | 3277 | i = t->current_threshold; |
2e72b634 KS |
3278 | |
3279 | /* | |
3280 | * Iterate backward over array of thresholds starting from | |
3281 | * current_threshold and check if a threshold is crossed. | |
3282 | * If none of thresholds below usage is crossed, we read | |
3283 | * only one element of the array here. | |
3284 | */ | |
3285 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) | |
3286 | eventfd_signal(t->entries[i].eventfd, 1); | |
3287 | ||
3288 | /* i = current_threshold + 1 */ | |
3289 | i++; | |
3290 | ||
3291 | /* | |
3292 | * Iterate forward over array of thresholds starting from | |
3293 | * current_threshold+1 and check if a threshold is crossed. | |
3294 | * If none of thresholds above usage is crossed, we read | |
3295 | * only one element of the array here. | |
3296 | */ | |
3297 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) | |
3298 | eventfd_signal(t->entries[i].eventfd, 1); | |
3299 | ||
3300 | /* Update current_threshold */ | |
5407a562 | 3301 | t->current_threshold = i - 1; |
2e72b634 KS |
3302 | unlock: |
3303 | rcu_read_unlock(); | |
3304 | } | |
3305 | ||
3306 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) | |
3307 | { | |
ad4ca5f4 KS |
3308 | while (memcg) { |
3309 | __mem_cgroup_threshold(memcg, false); | |
7941d214 | 3310 | if (do_memsw_account()) |
ad4ca5f4 KS |
3311 | __mem_cgroup_threshold(memcg, true); |
3312 | ||
3313 | memcg = parent_mem_cgroup(memcg); | |
3314 | } | |
2e72b634 KS |
3315 | } |
3316 | ||
3317 | static int compare_thresholds(const void *a, const void *b) | |
3318 | { | |
3319 | const struct mem_cgroup_threshold *_a = a; | |
3320 | const struct mem_cgroup_threshold *_b = b; | |
3321 | ||
2bff24a3 GT |
3322 | if (_a->threshold > _b->threshold) |
3323 | return 1; | |
3324 | ||
3325 | if (_a->threshold < _b->threshold) | |
3326 | return -1; | |
3327 | ||
3328 | return 0; | |
2e72b634 KS |
3329 | } |
3330 | ||
c0ff4b85 | 3331 | static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg) |
9490ff27 KH |
3332 | { |
3333 | struct mem_cgroup_eventfd_list *ev; | |
3334 | ||
2bcf2e92 MH |
3335 | spin_lock(&memcg_oom_lock); |
3336 | ||
c0ff4b85 | 3337 | list_for_each_entry(ev, &memcg->oom_notify, list) |
9490ff27 | 3338 | eventfd_signal(ev->eventfd, 1); |
2bcf2e92 MH |
3339 | |
3340 | spin_unlock(&memcg_oom_lock); | |
9490ff27 KH |
3341 | return 0; |
3342 | } | |
3343 | ||
c0ff4b85 | 3344 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg) |
9490ff27 | 3345 | { |
7d74b06f KH |
3346 | struct mem_cgroup *iter; |
3347 | ||
c0ff4b85 | 3348 | for_each_mem_cgroup_tree(iter, memcg) |
7d74b06f | 3349 | mem_cgroup_oom_notify_cb(iter); |
9490ff27 KH |
3350 | } |
3351 | ||
59b6f873 | 3352 | static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg, |
347c4a87 | 3353 | struct eventfd_ctx *eventfd, const char *args, enum res_type type) |
2e72b634 | 3354 | { |
2c488db2 KS |
3355 | struct mem_cgroup_thresholds *thresholds; |
3356 | struct mem_cgroup_threshold_ary *new; | |
3e32cb2e JW |
3357 | unsigned long threshold; |
3358 | unsigned long usage; | |
2c488db2 | 3359 | int i, size, ret; |
2e72b634 | 3360 | |
650c5e56 | 3361 | ret = page_counter_memparse(args, "-1", &threshold); |
2e72b634 KS |
3362 | if (ret) |
3363 | return ret; | |
3364 | ||
3365 | mutex_lock(&memcg->thresholds_lock); | |
2c488db2 | 3366 | |
05b84301 | 3367 | if (type == _MEM) { |
2c488db2 | 3368 | thresholds = &memcg->thresholds; |
ce00a967 | 3369 | usage = mem_cgroup_usage(memcg, false); |
05b84301 | 3370 | } else if (type == _MEMSWAP) { |
2c488db2 | 3371 | thresholds = &memcg->memsw_thresholds; |
ce00a967 | 3372 | usage = mem_cgroup_usage(memcg, true); |
05b84301 | 3373 | } else |
2e72b634 KS |
3374 | BUG(); |
3375 | ||
2e72b634 | 3376 | /* Check if a threshold crossed before adding a new one */ |
2c488db2 | 3377 | if (thresholds->primary) |
2e72b634 KS |
3378 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
3379 | ||
2c488db2 | 3380 | size = thresholds->primary ? thresholds->primary->size + 1 : 1; |
2e72b634 KS |
3381 | |
3382 | /* Allocate memory for new array of thresholds */ | |
2c488db2 | 3383 | new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold), |
2e72b634 | 3384 | GFP_KERNEL); |
2c488db2 | 3385 | if (!new) { |
2e72b634 KS |
3386 | ret = -ENOMEM; |
3387 | goto unlock; | |
3388 | } | |
2c488db2 | 3389 | new->size = size; |
2e72b634 KS |
3390 | |
3391 | /* Copy thresholds (if any) to new array */ | |
2c488db2 KS |
3392 | if (thresholds->primary) { |
3393 | memcpy(new->entries, thresholds->primary->entries, (size - 1) * | |
2e72b634 | 3394 | sizeof(struct mem_cgroup_threshold)); |
2c488db2 KS |
3395 | } |
3396 | ||
2e72b634 | 3397 | /* Add new threshold */ |
2c488db2 KS |
3398 | new->entries[size - 1].eventfd = eventfd; |
3399 | new->entries[size - 1].threshold = threshold; | |
2e72b634 KS |
3400 | |
3401 | /* Sort thresholds. Registering of new threshold isn't time-critical */ | |
2c488db2 | 3402 | sort(new->entries, size, sizeof(struct mem_cgroup_threshold), |
2e72b634 KS |
3403 | compare_thresholds, NULL); |
3404 | ||
3405 | /* Find current threshold */ | |
2c488db2 | 3406 | new->current_threshold = -1; |
2e72b634 | 3407 | for (i = 0; i < size; i++) { |
748dad36 | 3408 | if (new->entries[i].threshold <= usage) { |
2e72b634 | 3409 | /* |
2c488db2 KS |
3410 | * new->current_threshold will not be used until |
3411 | * rcu_assign_pointer(), so it's safe to increment | |
2e72b634 KS |
3412 | * it here. |
3413 | */ | |
2c488db2 | 3414 | ++new->current_threshold; |
748dad36 SZ |
3415 | } else |
3416 | break; | |
2e72b634 KS |
3417 | } |
3418 | ||
2c488db2 KS |
3419 | /* Free old spare buffer and save old primary buffer as spare */ |
3420 | kfree(thresholds->spare); | |
3421 | thresholds->spare = thresholds->primary; | |
3422 | ||
3423 | rcu_assign_pointer(thresholds->primary, new); | |
2e72b634 | 3424 | |
907860ed | 3425 | /* To be sure that nobody uses thresholds */ |
2e72b634 KS |
3426 | synchronize_rcu(); |
3427 | ||
2e72b634 KS |
3428 | unlock: |
3429 | mutex_unlock(&memcg->thresholds_lock); | |
3430 | ||
3431 | return ret; | |
3432 | } | |
3433 | ||
59b6f873 | 3434 | static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg, |
347c4a87 TH |
3435 | struct eventfd_ctx *eventfd, const char *args) |
3436 | { | |
59b6f873 | 3437 | return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM); |
347c4a87 TH |
3438 | } |
3439 | ||
59b6f873 | 3440 | static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg, |
347c4a87 TH |
3441 | struct eventfd_ctx *eventfd, const char *args) |
3442 | { | |
59b6f873 | 3443 | return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP); |
347c4a87 TH |
3444 | } |
3445 | ||
59b6f873 | 3446 | static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg, |
347c4a87 | 3447 | struct eventfd_ctx *eventfd, enum res_type type) |
2e72b634 | 3448 | { |
2c488db2 KS |
3449 | struct mem_cgroup_thresholds *thresholds; |
3450 | struct mem_cgroup_threshold_ary *new; | |
3e32cb2e | 3451 | unsigned long usage; |
2c488db2 | 3452 | int i, j, size; |
2e72b634 KS |
3453 | |
3454 | mutex_lock(&memcg->thresholds_lock); | |
05b84301 JW |
3455 | |
3456 | if (type == _MEM) { | |
2c488db2 | 3457 | thresholds = &memcg->thresholds; |
ce00a967 | 3458 | usage = mem_cgroup_usage(memcg, false); |
05b84301 | 3459 | } else if (type == _MEMSWAP) { |
2c488db2 | 3460 | thresholds = &memcg->memsw_thresholds; |
ce00a967 | 3461 | usage = mem_cgroup_usage(memcg, true); |
05b84301 | 3462 | } else |
2e72b634 KS |
3463 | BUG(); |
3464 | ||
371528ca AV |
3465 | if (!thresholds->primary) |
3466 | goto unlock; | |
3467 | ||
2e72b634 KS |
3468 | /* Check if a threshold crossed before removing */ |
3469 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); | |
3470 | ||
3471 | /* Calculate new number of threshold */ | |
2c488db2 KS |
3472 | size = 0; |
3473 | for (i = 0; i < thresholds->primary->size; i++) { | |
3474 | if (thresholds->primary->entries[i].eventfd != eventfd) | |
2e72b634 KS |
3475 | size++; |
3476 | } | |
3477 | ||
2c488db2 | 3478 | new = thresholds->spare; |
907860ed | 3479 | |
2e72b634 KS |
3480 | /* Set thresholds array to NULL if we don't have thresholds */ |
3481 | if (!size) { | |
2c488db2 KS |
3482 | kfree(new); |
3483 | new = NULL; | |
907860ed | 3484 | goto swap_buffers; |
2e72b634 KS |
3485 | } |
3486 | ||
2c488db2 | 3487 | new->size = size; |
2e72b634 KS |
3488 | |
3489 | /* Copy thresholds and find current threshold */ | |
2c488db2 KS |
3490 | new->current_threshold = -1; |
3491 | for (i = 0, j = 0; i < thresholds->primary->size; i++) { | |
3492 | if (thresholds->primary->entries[i].eventfd == eventfd) | |
2e72b634 KS |
3493 | continue; |
3494 | ||
2c488db2 | 3495 | new->entries[j] = thresholds->primary->entries[i]; |
748dad36 | 3496 | if (new->entries[j].threshold <= usage) { |
2e72b634 | 3497 | /* |
2c488db2 | 3498 | * new->current_threshold will not be used |
2e72b634 KS |
3499 | * until rcu_assign_pointer(), so it's safe to increment |
3500 | * it here. | |
3501 | */ | |
2c488db2 | 3502 | ++new->current_threshold; |
2e72b634 KS |
3503 | } |
3504 | j++; | |
3505 | } | |
3506 | ||
907860ed | 3507 | swap_buffers: |
2c488db2 KS |
3508 | /* Swap primary and spare array */ |
3509 | thresholds->spare = thresholds->primary; | |
8c757763 | 3510 | |
2c488db2 | 3511 | rcu_assign_pointer(thresholds->primary, new); |
2e72b634 | 3512 | |
907860ed | 3513 | /* To be sure that nobody uses thresholds */ |
2e72b634 | 3514 | synchronize_rcu(); |
6611d8d7 MC |
3515 | |
3516 | /* If all events are unregistered, free the spare array */ | |
3517 | if (!new) { | |
3518 | kfree(thresholds->spare); | |
3519 | thresholds->spare = NULL; | |
3520 | } | |
371528ca | 3521 | unlock: |
2e72b634 | 3522 | mutex_unlock(&memcg->thresholds_lock); |
2e72b634 | 3523 | } |
c1e862c1 | 3524 | |
59b6f873 | 3525 | static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg, |
347c4a87 TH |
3526 | struct eventfd_ctx *eventfd) |
3527 | { | |
59b6f873 | 3528 | return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM); |
347c4a87 TH |
3529 | } |
3530 | ||
59b6f873 | 3531 | static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg, |
347c4a87 TH |
3532 | struct eventfd_ctx *eventfd) |
3533 | { | |
59b6f873 | 3534 | return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP); |
347c4a87 TH |
3535 | } |
3536 | ||
59b6f873 | 3537 | static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg, |
347c4a87 | 3538 | struct eventfd_ctx *eventfd, const char *args) |
9490ff27 | 3539 | { |
9490ff27 | 3540 | struct mem_cgroup_eventfd_list *event; |
9490ff27 | 3541 | |
9490ff27 KH |
3542 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
3543 | if (!event) | |
3544 | return -ENOMEM; | |
3545 | ||
1af8efe9 | 3546 | spin_lock(&memcg_oom_lock); |
9490ff27 KH |
3547 | |
3548 | event->eventfd = eventfd; | |
3549 | list_add(&event->list, &memcg->oom_notify); | |
3550 | ||
3551 | /* already in OOM ? */ | |
c2b42d3c | 3552 | if (memcg->under_oom) |
9490ff27 | 3553 | eventfd_signal(eventfd, 1); |
1af8efe9 | 3554 | spin_unlock(&memcg_oom_lock); |
9490ff27 KH |
3555 | |
3556 | return 0; | |
3557 | } | |
3558 | ||
59b6f873 | 3559 | static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg, |
347c4a87 | 3560 | struct eventfd_ctx *eventfd) |
9490ff27 | 3561 | { |
9490ff27 | 3562 | struct mem_cgroup_eventfd_list *ev, *tmp; |
9490ff27 | 3563 | |
1af8efe9 | 3564 | spin_lock(&memcg_oom_lock); |
9490ff27 | 3565 | |
c0ff4b85 | 3566 | list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) { |
9490ff27 KH |
3567 | if (ev->eventfd == eventfd) { |
3568 | list_del(&ev->list); | |
3569 | kfree(ev); | |
3570 | } | |
3571 | } | |
3572 | ||
1af8efe9 | 3573 | spin_unlock(&memcg_oom_lock); |
9490ff27 KH |
3574 | } |
3575 | ||
2da8ca82 | 3576 | static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v) |
3c11ecf4 | 3577 | { |
2da8ca82 | 3578 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf)); |
3c11ecf4 | 3579 | |
791badbd | 3580 | seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable); |
c2b42d3c | 3581 | seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom); |
fe6bdfc8 RG |
3582 | seq_printf(sf, "oom_kill %lu\n", |
3583 | atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL])); | |
3c11ecf4 KH |
3584 | return 0; |
3585 | } | |
3586 | ||
182446d0 | 3587 | static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css, |
3c11ecf4 KH |
3588 | struct cftype *cft, u64 val) |
3589 | { | |
182446d0 | 3590 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
3c11ecf4 KH |
3591 | |
3592 | /* cannot set to root cgroup and only 0 and 1 are allowed */ | |
14208b0e | 3593 | if (!css->parent || !((val == 0) || (val == 1))) |
3c11ecf4 KH |
3594 | return -EINVAL; |
3595 | ||
c0ff4b85 | 3596 | memcg->oom_kill_disable = val; |
4d845ebf | 3597 | if (!val) |
c0ff4b85 | 3598 | memcg_oom_recover(memcg); |
3dae7fec | 3599 | |
3c11ecf4 KH |
3600 | return 0; |
3601 | } | |
3602 | ||
52ebea74 TH |
3603 | #ifdef CONFIG_CGROUP_WRITEBACK |
3604 | ||
841710aa TH |
3605 | static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp) |
3606 | { | |
3607 | return wb_domain_init(&memcg->cgwb_domain, gfp); | |
3608 | } | |
3609 | ||
3610 | static void memcg_wb_domain_exit(struct mem_cgroup *memcg) | |
3611 | { | |
3612 | wb_domain_exit(&memcg->cgwb_domain); | |
3613 | } | |
3614 | ||
2529bb3a TH |
3615 | static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg) |
3616 | { | |
3617 | wb_domain_size_changed(&memcg->cgwb_domain); | |
3618 | } | |
3619 | ||
841710aa TH |
3620 | struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb) |
3621 | { | |
3622 | struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css); | |
3623 | ||
3624 | if (!memcg->css.parent) | |
3625 | return NULL; | |
3626 | ||
3627 | return &memcg->cgwb_domain; | |
3628 | } | |
3629 | ||
c2aa723a TH |
3630 | /** |
3631 | * mem_cgroup_wb_stats - retrieve writeback related stats from its memcg | |
3632 | * @wb: bdi_writeback in question | |
c5edf9cd TH |
3633 | * @pfilepages: out parameter for number of file pages |
3634 | * @pheadroom: out parameter for number of allocatable pages according to memcg | |
c2aa723a TH |
3635 | * @pdirty: out parameter for number of dirty pages |
3636 | * @pwriteback: out parameter for number of pages under writeback | |
3637 | * | |
c5edf9cd TH |
3638 | * Determine the numbers of file, headroom, dirty, and writeback pages in |
3639 | * @wb's memcg. File, dirty and writeback are self-explanatory. Headroom | |
3640 | * is a bit more involved. | |
c2aa723a | 3641 | * |
c5edf9cd TH |
3642 | * A memcg's headroom is "min(max, high) - used". In the hierarchy, the |
3643 | * headroom is calculated as the lowest headroom of itself and the | |
3644 | * ancestors. Note that this doesn't consider the actual amount of | |
3645 | * available memory in the system. The caller should further cap | |
3646 | * *@pheadroom accordingly. | |
c2aa723a | 3647 | */ |
c5edf9cd TH |
3648 | void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages, |
3649 | unsigned long *pheadroom, unsigned long *pdirty, | |
3650 | unsigned long *pwriteback) | |
c2aa723a TH |
3651 | { |
3652 | struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css); | |
3653 | struct mem_cgroup *parent; | |
c2aa723a | 3654 | |
ccda7f43 | 3655 | *pdirty = memcg_page_state(memcg, NR_FILE_DIRTY); |
c2aa723a TH |
3656 | |
3657 | /* this should eventually include NR_UNSTABLE_NFS */ | |
ccda7f43 | 3658 | *pwriteback = memcg_page_state(memcg, NR_WRITEBACK); |
c5edf9cd TH |
3659 | *pfilepages = mem_cgroup_nr_lru_pages(memcg, (1 << LRU_INACTIVE_FILE) | |
3660 | (1 << LRU_ACTIVE_FILE)); | |
3661 | *pheadroom = PAGE_COUNTER_MAX; | |
c2aa723a | 3662 | |
c2aa723a | 3663 | while ((parent = parent_mem_cgroup(memcg))) { |
bbec2e15 | 3664 | unsigned long ceiling = min(memcg->memory.max, memcg->high); |
c2aa723a TH |
3665 | unsigned long used = page_counter_read(&memcg->memory); |
3666 | ||
c5edf9cd | 3667 | *pheadroom = min(*pheadroom, ceiling - min(ceiling, used)); |
c2aa723a TH |
3668 | memcg = parent; |
3669 | } | |
c2aa723a TH |
3670 | } |
3671 | ||
841710aa TH |
3672 | #else /* CONFIG_CGROUP_WRITEBACK */ |
3673 | ||
3674 | static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp) | |
3675 | { | |
3676 | return 0; | |
3677 | } | |
3678 | ||
3679 | static void memcg_wb_domain_exit(struct mem_cgroup *memcg) | |
3680 | { | |
3681 | } | |
3682 | ||
2529bb3a TH |
3683 | static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg) |
3684 | { | |
3685 | } | |
3686 | ||
52ebea74 TH |
3687 | #endif /* CONFIG_CGROUP_WRITEBACK */ |
3688 | ||
3bc942f3 TH |
3689 | /* |
3690 | * DO NOT USE IN NEW FILES. | |
3691 | * | |
3692 | * "cgroup.event_control" implementation. | |
3693 | * | |
3694 | * This is way over-engineered. It tries to support fully configurable | |
3695 | * events for each user. Such level of flexibility is completely | |
3696 | * unnecessary especially in the light of the planned unified hierarchy. | |
3697 | * | |
3698 | * Please deprecate this and replace with something simpler if at all | |
3699 | * possible. | |
3700 | */ | |
3701 | ||
79bd9814 TH |
3702 | /* |
3703 | * Unregister event and free resources. | |
3704 | * | |
3705 | * Gets called from workqueue. | |
3706 | */ | |
3bc942f3 | 3707 | static void memcg_event_remove(struct work_struct *work) |
79bd9814 | 3708 | { |
3bc942f3 TH |
3709 | struct mem_cgroup_event *event = |
3710 | container_of(work, struct mem_cgroup_event, remove); | |
59b6f873 | 3711 | struct mem_cgroup *memcg = event->memcg; |
79bd9814 TH |
3712 | |
3713 | remove_wait_queue(event->wqh, &event->wait); | |
3714 | ||
59b6f873 | 3715 | event->unregister_event(memcg, event->eventfd); |
79bd9814 TH |
3716 | |
3717 | /* Notify userspace the event is going away. */ | |
3718 | eventfd_signal(event->eventfd, 1); | |
3719 | ||
3720 | eventfd_ctx_put(event->eventfd); | |
3721 | kfree(event); | |
59b6f873 | 3722 | css_put(&memcg->css); |
79bd9814 TH |
3723 | } |
3724 | ||
3725 | /* | |
a9a08845 | 3726 | * Gets called on EPOLLHUP on eventfd when user closes it. |
79bd9814 TH |
3727 | * |
3728 | * Called with wqh->lock held and interrupts disabled. | |
3729 | */ | |
ac6424b9 | 3730 | static int memcg_event_wake(wait_queue_entry_t *wait, unsigned mode, |
3bc942f3 | 3731 | int sync, void *key) |
79bd9814 | 3732 | { |
3bc942f3 TH |
3733 | struct mem_cgroup_event *event = |
3734 | container_of(wait, struct mem_cgroup_event, wait); | |
59b6f873 | 3735 | struct mem_cgroup *memcg = event->memcg; |
3ad6f93e | 3736 | __poll_t flags = key_to_poll(key); |
79bd9814 | 3737 | |
a9a08845 | 3738 | if (flags & EPOLLHUP) { |
79bd9814 TH |
3739 | /* |
3740 | * If the event has been detached at cgroup removal, we | |
3741 | * can simply return knowing the other side will cleanup | |
3742 | * for us. | |
3743 | * | |
3744 | * We can't race against event freeing since the other | |
3745 | * side will require wqh->lock via remove_wait_queue(), | |
3746 | * which we hold. | |
3747 | */ | |
fba94807 | 3748 | spin_lock(&memcg->event_list_lock); |
79bd9814 TH |
3749 | if (!list_empty(&event->list)) { |
3750 | list_del_init(&event->list); | |
3751 | /* | |
3752 | * We are in atomic context, but cgroup_event_remove() | |
3753 | * may sleep, so we have to call it in workqueue. | |
3754 | */ | |
3755 | schedule_work(&event->remove); | |
3756 | } | |
fba94807 | 3757 | spin_unlock(&memcg->event_list_lock); |
79bd9814 TH |
3758 | } |
3759 | ||
3760 | return 0; | |
3761 | } | |
3762 | ||
3bc942f3 | 3763 | static void memcg_event_ptable_queue_proc(struct file *file, |
79bd9814 TH |
3764 | wait_queue_head_t *wqh, poll_table *pt) |
3765 | { | |
3bc942f3 TH |
3766 | struct mem_cgroup_event *event = |
3767 | container_of(pt, struct mem_cgroup_event, pt); | |
79bd9814 TH |
3768 | |
3769 | event->wqh = wqh; | |
3770 | add_wait_queue(wqh, &event->wait); | |
3771 | } | |
3772 | ||
3773 | /* | |
3bc942f3 TH |
3774 | * DO NOT USE IN NEW FILES. |
3775 | * | |
79bd9814 TH |
3776 | * Parse input and register new cgroup event handler. |
3777 | * | |
3778 | * Input must be in format '<event_fd> <control_fd> <args>'. | |
3779 | * Interpretation of args is defined by control file implementation. | |
3780 | */ | |
451af504 TH |
3781 | static ssize_t memcg_write_event_control(struct kernfs_open_file *of, |
3782 | char *buf, size_t nbytes, loff_t off) | |
79bd9814 | 3783 | { |
451af504 | 3784 | struct cgroup_subsys_state *css = of_css(of); |
fba94807 | 3785 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
3bc942f3 | 3786 | struct mem_cgroup_event *event; |
79bd9814 TH |
3787 | struct cgroup_subsys_state *cfile_css; |
3788 | unsigned int efd, cfd; | |
3789 | struct fd efile; | |
3790 | struct fd cfile; | |
fba94807 | 3791 | const char *name; |
79bd9814 TH |
3792 | char *endp; |
3793 | int ret; | |
3794 | ||
451af504 TH |
3795 | buf = strstrip(buf); |
3796 | ||
3797 | efd = simple_strtoul(buf, &endp, 10); | |
79bd9814 TH |
3798 | if (*endp != ' ') |
3799 | return -EINVAL; | |
451af504 | 3800 | buf = endp + 1; |
79bd9814 | 3801 | |
451af504 | 3802 | cfd = simple_strtoul(buf, &endp, 10); |
79bd9814 TH |
3803 | if ((*endp != ' ') && (*endp != '\0')) |
3804 | return -EINVAL; | |
451af504 | 3805 | buf = endp + 1; |
79bd9814 TH |
3806 | |
3807 | event = kzalloc(sizeof(*event), GFP_KERNEL); | |
3808 | if (!event) | |
3809 | return -ENOMEM; | |
3810 | ||
59b6f873 | 3811 | event->memcg = memcg; |
79bd9814 | 3812 | INIT_LIST_HEAD(&event->list); |
3bc942f3 TH |
3813 | init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc); |
3814 | init_waitqueue_func_entry(&event->wait, memcg_event_wake); | |
3815 | INIT_WORK(&event->remove, memcg_event_remove); | |
79bd9814 TH |
3816 | |
3817 | efile = fdget(efd); | |
3818 | if (!efile.file) { | |
3819 | ret = -EBADF; | |
3820 | goto out_kfree; | |
3821 | } | |
3822 | ||
3823 | event->eventfd = eventfd_ctx_fileget(efile.file); | |
3824 | if (IS_ERR(event->eventfd)) { | |
3825 | ret = PTR_ERR(event->eventfd); | |
3826 | goto out_put_efile; | |
3827 | } | |
3828 | ||
3829 | cfile = fdget(cfd); | |
3830 | if (!cfile.file) { | |
3831 | ret = -EBADF; | |
3832 | goto out_put_eventfd; | |
3833 | } | |
3834 | ||
3835 | /* the process need read permission on control file */ | |
3836 | /* AV: shouldn't we check that it's been opened for read instead? */ | |
3837 | ret = inode_permission(file_inode(cfile.file), MAY_READ); | |
3838 | if (ret < 0) | |
3839 | goto out_put_cfile; | |
3840 | ||
fba94807 TH |
3841 | /* |
3842 | * Determine the event callbacks and set them in @event. This used | |
3843 | * to be done via struct cftype but cgroup core no longer knows | |
3844 | * about these events. The following is crude but the whole thing | |
3845 | * is for compatibility anyway. | |
3bc942f3 TH |
3846 | * |
3847 | * DO NOT ADD NEW FILES. | |
fba94807 | 3848 | */ |
b583043e | 3849 | name = cfile.file->f_path.dentry->d_name.name; |
fba94807 TH |
3850 | |
3851 | if (!strcmp(name, "memory.usage_in_bytes")) { | |
3852 | event->register_event = mem_cgroup_usage_register_event; | |
3853 | event->unregister_event = mem_cgroup_usage_unregister_event; | |
3854 | } else if (!strcmp(name, "memory.oom_control")) { | |
3855 | event->register_event = mem_cgroup_oom_register_event; | |
3856 | event->unregister_event = mem_cgroup_oom_unregister_event; | |
3857 | } else if (!strcmp(name, "memory.pressure_level")) { | |
3858 | event->register_event = vmpressure_register_event; | |
3859 | event->unregister_event = vmpressure_unregister_event; | |
3860 | } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) { | |
347c4a87 TH |
3861 | event->register_event = memsw_cgroup_usage_register_event; |
3862 | event->unregister_event = memsw_cgroup_usage_unregister_event; | |
fba94807 TH |
3863 | } else { |
3864 | ret = -EINVAL; | |
3865 | goto out_put_cfile; | |
3866 | } | |
3867 | ||
79bd9814 | 3868 | /* |
b5557c4c TH |
3869 | * Verify @cfile should belong to @css. Also, remaining events are |
3870 | * automatically removed on cgroup destruction but the removal is | |
3871 | * asynchronous, so take an extra ref on @css. | |
79bd9814 | 3872 | */ |
b583043e | 3873 | cfile_css = css_tryget_online_from_dir(cfile.file->f_path.dentry->d_parent, |
ec903c0c | 3874 | &memory_cgrp_subsys); |
79bd9814 | 3875 | ret = -EINVAL; |
5a17f543 | 3876 | if (IS_ERR(cfile_css)) |
79bd9814 | 3877 | goto out_put_cfile; |
5a17f543 TH |
3878 | if (cfile_css != css) { |
3879 | css_put(cfile_css); | |
79bd9814 | 3880 | goto out_put_cfile; |
5a17f543 | 3881 | } |
79bd9814 | 3882 | |
451af504 | 3883 | ret = event->register_event(memcg, event->eventfd, buf); |
79bd9814 TH |
3884 | if (ret) |
3885 | goto out_put_css; | |
3886 | ||
9965ed17 | 3887 | vfs_poll(efile.file, &event->pt); |
79bd9814 | 3888 | |
fba94807 TH |
3889 | spin_lock(&memcg->event_list_lock); |
3890 | list_add(&event->list, &memcg->event_list); | |
3891 | spin_unlock(&memcg->event_list_lock); | |
79bd9814 TH |
3892 | |
3893 | fdput(cfile); | |
3894 | fdput(efile); | |
3895 | ||
451af504 | 3896 | return nbytes; |
79bd9814 TH |
3897 | |
3898 | out_put_css: | |
b5557c4c | 3899 | css_put(css); |
79bd9814 TH |
3900 | out_put_cfile: |
3901 | fdput(cfile); | |
3902 | out_put_eventfd: | |
3903 | eventfd_ctx_put(event->eventfd); | |
3904 | out_put_efile: | |
3905 | fdput(efile); | |
3906 | out_kfree: | |
3907 | kfree(event); | |
3908 | ||
3909 | return ret; | |
3910 | } | |
3911 | ||
241994ed | 3912 | static struct cftype mem_cgroup_legacy_files[] = { |
8cdea7c0 | 3913 | { |
0eea1030 | 3914 | .name = "usage_in_bytes", |
8c7c6e34 | 3915 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
791badbd | 3916 | .read_u64 = mem_cgroup_read_u64, |
8cdea7c0 | 3917 | }, |
c84872e1 PE |
3918 | { |
3919 | .name = "max_usage_in_bytes", | |
8c7c6e34 | 3920 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
6770c64e | 3921 | .write = mem_cgroup_reset, |
791badbd | 3922 | .read_u64 = mem_cgroup_read_u64, |
c84872e1 | 3923 | }, |
8cdea7c0 | 3924 | { |
0eea1030 | 3925 | .name = "limit_in_bytes", |
8c7c6e34 | 3926 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
451af504 | 3927 | .write = mem_cgroup_write, |
791badbd | 3928 | .read_u64 = mem_cgroup_read_u64, |
8cdea7c0 | 3929 | }, |
296c81d8 BS |
3930 | { |
3931 | .name = "soft_limit_in_bytes", | |
3932 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), | |
451af504 | 3933 | .write = mem_cgroup_write, |
791badbd | 3934 | .read_u64 = mem_cgroup_read_u64, |
296c81d8 | 3935 | }, |
8cdea7c0 BS |
3936 | { |
3937 | .name = "failcnt", | |
8c7c6e34 | 3938 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
6770c64e | 3939 | .write = mem_cgroup_reset, |
791badbd | 3940 | .read_u64 = mem_cgroup_read_u64, |
8cdea7c0 | 3941 | }, |
d2ceb9b7 KH |
3942 | { |
3943 | .name = "stat", | |
2da8ca82 | 3944 | .seq_show = memcg_stat_show, |
d2ceb9b7 | 3945 | }, |
c1e862c1 KH |
3946 | { |
3947 | .name = "force_empty", | |
6770c64e | 3948 | .write = mem_cgroup_force_empty_write, |
c1e862c1 | 3949 | }, |
18f59ea7 BS |
3950 | { |
3951 | .name = "use_hierarchy", | |
3952 | .write_u64 = mem_cgroup_hierarchy_write, | |
3953 | .read_u64 = mem_cgroup_hierarchy_read, | |
3954 | }, | |
79bd9814 | 3955 | { |
3bc942f3 | 3956 | .name = "cgroup.event_control", /* XXX: for compat */ |
451af504 | 3957 | .write = memcg_write_event_control, |
7dbdb199 | 3958 | .flags = CFTYPE_NO_PREFIX | CFTYPE_WORLD_WRITABLE, |
79bd9814 | 3959 | }, |
a7885eb8 KM |
3960 | { |
3961 | .name = "swappiness", | |
3962 | .read_u64 = mem_cgroup_swappiness_read, | |
3963 | .write_u64 = mem_cgroup_swappiness_write, | |
3964 | }, | |
7dc74be0 DN |
3965 | { |
3966 | .name = "move_charge_at_immigrate", | |
3967 | .read_u64 = mem_cgroup_move_charge_read, | |
3968 | .write_u64 = mem_cgroup_move_charge_write, | |
3969 | }, | |
9490ff27 KH |
3970 | { |
3971 | .name = "oom_control", | |
2da8ca82 | 3972 | .seq_show = mem_cgroup_oom_control_read, |
3c11ecf4 | 3973 | .write_u64 = mem_cgroup_oom_control_write, |
9490ff27 KH |
3974 | .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL), |
3975 | }, | |
70ddf637 AV |
3976 | { |
3977 | .name = "pressure_level", | |
70ddf637 | 3978 | }, |
406eb0c9 YH |
3979 | #ifdef CONFIG_NUMA |
3980 | { | |
3981 | .name = "numa_stat", | |
2da8ca82 | 3982 | .seq_show = memcg_numa_stat_show, |
406eb0c9 YH |
3983 | }, |
3984 | #endif | |
510fc4e1 GC |
3985 | { |
3986 | .name = "kmem.limit_in_bytes", | |
3987 | .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT), | |
451af504 | 3988 | .write = mem_cgroup_write, |
791badbd | 3989 | .read_u64 = mem_cgroup_read_u64, |
510fc4e1 GC |
3990 | }, |
3991 | { | |
3992 | .name = "kmem.usage_in_bytes", | |
3993 | .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE), | |
791badbd | 3994 | .read_u64 = mem_cgroup_read_u64, |
510fc4e1 GC |
3995 | }, |
3996 | { | |
3997 | .name = "kmem.failcnt", | |
3998 | .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT), | |
6770c64e | 3999 | .write = mem_cgroup_reset, |
791badbd | 4000 | .read_u64 = mem_cgroup_read_u64, |
510fc4e1 GC |
4001 | }, |
4002 | { | |
4003 | .name = "kmem.max_usage_in_bytes", | |
4004 | .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE), | |
6770c64e | 4005 | .write = mem_cgroup_reset, |
791badbd | 4006 | .read_u64 = mem_cgroup_read_u64, |
510fc4e1 | 4007 | }, |
5b365771 | 4008 | #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG) |
749c5415 GC |
4009 | { |
4010 | .name = "kmem.slabinfo", | |
bc2791f8 TH |
4011 | .seq_start = memcg_slab_start, |
4012 | .seq_next = memcg_slab_next, | |
4013 | .seq_stop = memcg_slab_stop, | |
b047501c | 4014 | .seq_show = memcg_slab_show, |
749c5415 GC |
4015 | }, |
4016 | #endif | |
d55f90bf VD |
4017 | { |
4018 | .name = "kmem.tcp.limit_in_bytes", | |
4019 | .private = MEMFILE_PRIVATE(_TCP, RES_LIMIT), | |
4020 | .write = mem_cgroup_write, | |
4021 | .read_u64 = mem_cgroup_read_u64, | |
4022 | }, | |
4023 | { | |
4024 | .name = "kmem.tcp.usage_in_bytes", | |
4025 | .private = MEMFILE_PRIVATE(_TCP, RES_USAGE), | |
4026 | .read_u64 = mem_cgroup_read_u64, | |
4027 | }, | |
4028 | { | |
4029 | .name = "kmem.tcp.failcnt", | |
4030 | .private = MEMFILE_PRIVATE(_TCP, RES_FAILCNT), | |
4031 | .write = mem_cgroup_reset, | |
4032 | .read_u64 = mem_cgroup_read_u64, | |
4033 | }, | |
4034 | { | |
4035 | .name = "kmem.tcp.max_usage_in_bytes", | |
4036 | .private = MEMFILE_PRIVATE(_TCP, RES_MAX_USAGE), | |
4037 | .write = mem_cgroup_reset, | |
4038 | .read_u64 = mem_cgroup_read_u64, | |
4039 | }, | |
6bc10349 | 4040 | { }, /* terminate */ |
af36f906 | 4041 | }; |
8c7c6e34 | 4042 | |
73f576c0 JW |
4043 | /* |
4044 | * Private memory cgroup IDR | |
4045 | * | |
4046 | * Swap-out records and page cache shadow entries need to store memcg | |
4047 | * references in constrained space, so we maintain an ID space that is | |
4048 | * limited to 16 bit (MEM_CGROUP_ID_MAX), limiting the total number of | |
4049 | * memory-controlled cgroups to 64k. | |
4050 | * | |
4051 | * However, there usually are many references to the oflline CSS after | |
4052 | * the cgroup has been destroyed, such as page cache or reclaimable | |
4053 | * slab objects, that don't need to hang on to the ID. We want to keep | |
4054 | * those dead CSS from occupying IDs, or we might quickly exhaust the | |
4055 | * relatively small ID space and prevent the creation of new cgroups | |
4056 | * even when there are much fewer than 64k cgroups - possibly none. | |
4057 | * | |
4058 | * Maintain a private 16-bit ID space for memcg, and allow the ID to | |
4059 | * be freed and recycled when it's no longer needed, which is usually | |
4060 | * when the CSS is offlined. | |
4061 | * | |
4062 | * The only exception to that are records of swapped out tmpfs/shmem | |
4063 | * pages that need to be attributed to live ancestors on swapin. But | |
4064 | * those references are manageable from userspace. | |
4065 | */ | |
4066 | ||
4067 | static DEFINE_IDR(mem_cgroup_idr); | |
4068 | ||
7e97de0b KT |
4069 | static void mem_cgroup_id_remove(struct mem_cgroup *memcg) |
4070 | { | |
4071 | if (memcg->id.id > 0) { | |
4072 | idr_remove(&mem_cgroup_idr, memcg->id.id); | |
4073 | memcg->id.id = 0; | |
4074 | } | |
4075 | } | |
4076 | ||
615d66c3 | 4077 | static void mem_cgroup_id_get_many(struct mem_cgroup *memcg, unsigned int n) |
73f576c0 | 4078 | { |
58fa2a55 | 4079 | VM_BUG_ON(atomic_read(&memcg->id.ref) <= 0); |
615d66c3 | 4080 | atomic_add(n, &memcg->id.ref); |
73f576c0 JW |
4081 | } |
4082 | ||
615d66c3 | 4083 | static void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n) |
73f576c0 | 4084 | { |
58fa2a55 | 4085 | VM_BUG_ON(atomic_read(&memcg->id.ref) < n); |
615d66c3 | 4086 | if (atomic_sub_and_test(n, &memcg->id.ref)) { |
7e97de0b | 4087 | mem_cgroup_id_remove(memcg); |
73f576c0 JW |
4088 | |
4089 | /* Memcg ID pins CSS */ | |
4090 | css_put(&memcg->css); | |
4091 | } | |
4092 | } | |
4093 | ||
615d66c3 VD |
4094 | static inline void mem_cgroup_id_get(struct mem_cgroup *memcg) |
4095 | { | |
4096 | mem_cgroup_id_get_many(memcg, 1); | |
4097 | } | |
4098 | ||
4099 | static inline void mem_cgroup_id_put(struct mem_cgroup *memcg) | |
4100 | { | |
4101 | mem_cgroup_id_put_many(memcg, 1); | |
4102 | } | |
4103 | ||
73f576c0 JW |
4104 | /** |
4105 | * mem_cgroup_from_id - look up a memcg from a memcg id | |
4106 | * @id: the memcg id to look up | |
4107 | * | |
4108 | * Caller must hold rcu_read_lock(). | |
4109 | */ | |
4110 | struct mem_cgroup *mem_cgroup_from_id(unsigned short id) | |
4111 | { | |
4112 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
4113 | return idr_find(&mem_cgroup_idr, id); | |
4114 | } | |
4115 | ||
ef8f2327 | 4116 | static int alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node) |
6d12e2d8 KH |
4117 | { |
4118 | struct mem_cgroup_per_node *pn; | |
ef8f2327 | 4119 | int tmp = node; |
1ecaab2b KH |
4120 | /* |
4121 | * This routine is called against possible nodes. | |
4122 | * But it's BUG to call kmalloc() against offline node. | |
4123 | * | |
4124 | * TODO: this routine can waste much memory for nodes which will | |
4125 | * never be onlined. It's better to use memory hotplug callback | |
4126 | * function. | |
4127 | */ | |
41e3355d KH |
4128 | if (!node_state(node, N_NORMAL_MEMORY)) |
4129 | tmp = -1; | |
17295c88 | 4130 | pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
6d12e2d8 KH |
4131 | if (!pn) |
4132 | return 1; | |
1ecaab2b | 4133 | |
a983b5eb JW |
4134 | pn->lruvec_stat_cpu = alloc_percpu(struct lruvec_stat); |
4135 | if (!pn->lruvec_stat_cpu) { | |
00f3ca2c JW |
4136 | kfree(pn); |
4137 | return 1; | |
4138 | } | |
4139 | ||
ef8f2327 MG |
4140 | lruvec_init(&pn->lruvec); |
4141 | pn->usage_in_excess = 0; | |
4142 | pn->on_tree = false; | |
4143 | pn->memcg = memcg; | |
4144 | ||
54f72fe0 | 4145 | memcg->nodeinfo[node] = pn; |
6d12e2d8 KH |
4146 | return 0; |
4147 | } | |
4148 | ||
ef8f2327 | 4149 | static void free_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node) |
1ecaab2b | 4150 | { |
00f3ca2c JW |
4151 | struct mem_cgroup_per_node *pn = memcg->nodeinfo[node]; |
4152 | ||
4eaf431f MH |
4153 | if (!pn) |
4154 | return; | |
4155 | ||
a983b5eb | 4156 | free_percpu(pn->lruvec_stat_cpu); |
00f3ca2c | 4157 | kfree(pn); |
1ecaab2b KH |
4158 | } |
4159 | ||
40e952f9 | 4160 | static void __mem_cgroup_free(struct mem_cgroup *memcg) |
59927fb9 | 4161 | { |
c8b2a36f | 4162 | int node; |
59927fb9 | 4163 | |
c8b2a36f | 4164 | for_each_node(node) |
ef8f2327 | 4165 | free_mem_cgroup_per_node_info(memcg, node); |
a983b5eb | 4166 | free_percpu(memcg->stat_cpu); |
8ff69e2c | 4167 | kfree(memcg); |
59927fb9 | 4168 | } |
3afe36b1 | 4169 | |
40e952f9 TE |
4170 | static void mem_cgroup_free(struct mem_cgroup *memcg) |
4171 | { | |
4172 | memcg_wb_domain_exit(memcg); | |
4173 | __mem_cgroup_free(memcg); | |
4174 | } | |
4175 | ||
0b8f73e1 | 4176 | static struct mem_cgroup *mem_cgroup_alloc(void) |
8cdea7c0 | 4177 | { |
d142e3e6 | 4178 | struct mem_cgroup *memcg; |
0b8f73e1 | 4179 | size_t size; |
6d12e2d8 | 4180 | int node; |
8cdea7c0 | 4181 | |
0b8f73e1 JW |
4182 | size = sizeof(struct mem_cgroup); |
4183 | size += nr_node_ids * sizeof(struct mem_cgroup_per_node *); | |
4184 | ||
4185 | memcg = kzalloc(size, GFP_KERNEL); | |
c0ff4b85 | 4186 | if (!memcg) |
0b8f73e1 JW |
4187 | return NULL; |
4188 | ||
73f576c0 JW |
4189 | memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL, |
4190 | 1, MEM_CGROUP_ID_MAX, | |
4191 | GFP_KERNEL); | |
4192 | if (memcg->id.id < 0) | |
4193 | goto fail; | |
4194 | ||
a983b5eb JW |
4195 | memcg->stat_cpu = alloc_percpu(struct mem_cgroup_stat_cpu); |
4196 | if (!memcg->stat_cpu) | |
0b8f73e1 | 4197 | goto fail; |
78fb7466 | 4198 | |
3ed28fa1 | 4199 | for_each_node(node) |
ef8f2327 | 4200 | if (alloc_mem_cgroup_per_node_info(memcg, node)) |
0b8f73e1 | 4201 | goto fail; |
f64c3f54 | 4202 | |
0b8f73e1 JW |
4203 | if (memcg_wb_domain_init(memcg, GFP_KERNEL)) |
4204 | goto fail; | |
28dbc4b6 | 4205 | |
f7e1cb6e | 4206 | INIT_WORK(&memcg->high_work, high_work_func); |
d142e3e6 GC |
4207 | memcg->last_scanned_node = MAX_NUMNODES; |
4208 | INIT_LIST_HEAD(&memcg->oom_notify); | |
d142e3e6 GC |
4209 | mutex_init(&memcg->thresholds_lock); |
4210 | spin_lock_init(&memcg->move_lock); | |
70ddf637 | 4211 | vmpressure_init(&memcg->vmpressure); |
fba94807 TH |
4212 | INIT_LIST_HEAD(&memcg->event_list); |
4213 | spin_lock_init(&memcg->event_list_lock); | |
d886f4e4 | 4214 | memcg->socket_pressure = jiffies; |
127424c8 | 4215 | #ifndef CONFIG_SLOB |
900a38f0 | 4216 | memcg->kmemcg_id = -1; |
900a38f0 | 4217 | #endif |
52ebea74 TH |
4218 | #ifdef CONFIG_CGROUP_WRITEBACK |
4219 | INIT_LIST_HEAD(&memcg->cgwb_list); | |
4220 | #endif | |
73f576c0 | 4221 | idr_replace(&mem_cgroup_idr, memcg, memcg->id.id); |
0b8f73e1 JW |
4222 | return memcg; |
4223 | fail: | |
7e97de0b | 4224 | mem_cgroup_id_remove(memcg); |
40e952f9 | 4225 | __mem_cgroup_free(memcg); |
0b8f73e1 | 4226 | return NULL; |
d142e3e6 GC |
4227 | } |
4228 | ||
0b8f73e1 JW |
4229 | static struct cgroup_subsys_state * __ref |
4230 | mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
d142e3e6 | 4231 | { |
0b8f73e1 JW |
4232 | struct mem_cgroup *parent = mem_cgroup_from_css(parent_css); |
4233 | struct mem_cgroup *memcg; | |
4234 | long error = -ENOMEM; | |
d142e3e6 | 4235 | |
0b8f73e1 JW |
4236 | memcg = mem_cgroup_alloc(); |
4237 | if (!memcg) | |
4238 | return ERR_PTR(error); | |
d142e3e6 | 4239 | |
0b8f73e1 JW |
4240 | memcg->high = PAGE_COUNTER_MAX; |
4241 | memcg->soft_limit = PAGE_COUNTER_MAX; | |
4242 | if (parent) { | |
4243 | memcg->swappiness = mem_cgroup_swappiness(parent); | |
4244 | memcg->oom_kill_disable = parent->oom_kill_disable; | |
4245 | } | |
4246 | if (parent && parent->use_hierarchy) { | |
4247 | memcg->use_hierarchy = true; | |
3e32cb2e | 4248 | page_counter_init(&memcg->memory, &parent->memory); |
37e84351 | 4249 | page_counter_init(&memcg->swap, &parent->swap); |
3e32cb2e JW |
4250 | page_counter_init(&memcg->memsw, &parent->memsw); |
4251 | page_counter_init(&memcg->kmem, &parent->kmem); | |
0db15298 | 4252 | page_counter_init(&memcg->tcpmem, &parent->tcpmem); |
18f59ea7 | 4253 | } else { |
3e32cb2e | 4254 | page_counter_init(&memcg->memory, NULL); |
37e84351 | 4255 | page_counter_init(&memcg->swap, NULL); |
3e32cb2e JW |
4256 | page_counter_init(&memcg->memsw, NULL); |
4257 | page_counter_init(&memcg->kmem, NULL); | |
0db15298 | 4258 | page_counter_init(&memcg->tcpmem, NULL); |
8c7f6edb TH |
4259 | /* |
4260 | * Deeper hierachy with use_hierarchy == false doesn't make | |
4261 | * much sense so let cgroup subsystem know about this | |
4262 | * unfortunate state in our controller. | |
4263 | */ | |
d142e3e6 | 4264 | if (parent != root_mem_cgroup) |
073219e9 | 4265 | memory_cgrp_subsys.broken_hierarchy = true; |
18f59ea7 | 4266 | } |
d6441637 | 4267 | |
0b8f73e1 JW |
4268 | /* The following stuff does not apply to the root */ |
4269 | if (!parent) { | |
4270 | root_mem_cgroup = memcg; | |
4271 | return &memcg->css; | |
4272 | } | |
4273 | ||
b313aeee | 4274 | error = memcg_online_kmem(memcg); |
0b8f73e1 JW |
4275 | if (error) |
4276 | goto fail; | |
127424c8 | 4277 | |
f7e1cb6e | 4278 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket) |
ef12947c | 4279 | static_branch_inc(&memcg_sockets_enabled_key); |
f7e1cb6e | 4280 | |
0b8f73e1 JW |
4281 | return &memcg->css; |
4282 | fail: | |
7e97de0b | 4283 | mem_cgroup_id_remove(memcg); |
0b8f73e1 | 4284 | mem_cgroup_free(memcg); |
ea3a9645 | 4285 | return ERR_PTR(-ENOMEM); |
0b8f73e1 JW |
4286 | } |
4287 | ||
73f576c0 | 4288 | static int mem_cgroup_css_online(struct cgroup_subsys_state *css) |
0b8f73e1 | 4289 | { |
58fa2a55 VD |
4290 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
4291 | ||
73f576c0 | 4292 | /* Online state pins memcg ID, memcg ID pins CSS */ |
58fa2a55 | 4293 | atomic_set(&memcg->id.ref, 1); |
73f576c0 | 4294 | css_get(css); |
2f7dd7a4 | 4295 | return 0; |
8cdea7c0 BS |
4296 | } |
4297 | ||
eb95419b | 4298 | static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) |
df878fb0 | 4299 | { |
eb95419b | 4300 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
3bc942f3 | 4301 | struct mem_cgroup_event *event, *tmp; |
79bd9814 TH |
4302 | |
4303 | /* | |
4304 | * Unregister events and notify userspace. | |
4305 | * Notify userspace about cgroup removing only after rmdir of cgroup | |
4306 | * directory to avoid race between userspace and kernelspace. | |
4307 | */ | |
fba94807 TH |
4308 | spin_lock(&memcg->event_list_lock); |
4309 | list_for_each_entry_safe(event, tmp, &memcg->event_list, list) { | |
79bd9814 TH |
4310 | list_del_init(&event->list); |
4311 | schedule_work(&event->remove); | |
4312 | } | |
fba94807 | 4313 | spin_unlock(&memcg->event_list_lock); |
ec64f515 | 4314 | |
bf8d5d52 | 4315 | page_counter_set_min(&memcg->memory, 0); |
23067153 | 4316 | page_counter_set_low(&memcg->memory, 0); |
63677c74 | 4317 | |
567e9ab2 | 4318 | memcg_offline_kmem(memcg); |
52ebea74 | 4319 | wb_memcg_offline(memcg); |
73f576c0 JW |
4320 | |
4321 | mem_cgroup_id_put(memcg); | |
df878fb0 KH |
4322 | } |
4323 | ||
6df38689 VD |
4324 | static void mem_cgroup_css_released(struct cgroup_subsys_state *css) |
4325 | { | |
4326 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); | |
4327 | ||
4328 | invalidate_reclaim_iterators(memcg); | |
4329 | } | |
4330 | ||
eb95419b | 4331 | static void mem_cgroup_css_free(struct cgroup_subsys_state *css) |
8cdea7c0 | 4332 | { |
eb95419b | 4333 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
c268e994 | 4334 | |
f7e1cb6e | 4335 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket) |
ef12947c | 4336 | static_branch_dec(&memcg_sockets_enabled_key); |
127424c8 | 4337 | |
0db15298 | 4338 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_active) |
d55f90bf | 4339 | static_branch_dec(&memcg_sockets_enabled_key); |
3893e302 | 4340 | |
0b8f73e1 JW |
4341 | vmpressure_cleanup(&memcg->vmpressure); |
4342 | cancel_work_sync(&memcg->high_work); | |
4343 | mem_cgroup_remove_from_trees(memcg); | |
d886f4e4 | 4344 | memcg_free_kmem(memcg); |
0b8f73e1 | 4345 | mem_cgroup_free(memcg); |
8cdea7c0 BS |
4346 | } |
4347 | ||
1ced953b TH |
4348 | /** |
4349 | * mem_cgroup_css_reset - reset the states of a mem_cgroup | |
4350 | * @css: the target css | |
4351 | * | |
4352 | * Reset the states of the mem_cgroup associated with @css. This is | |
4353 | * invoked when the userland requests disabling on the default hierarchy | |
4354 | * but the memcg is pinned through dependency. The memcg should stop | |
4355 | * applying policies and should revert to the vanilla state as it may be | |
4356 | * made visible again. | |
4357 | * | |
4358 | * The current implementation only resets the essential configurations. | |
4359 | * This needs to be expanded to cover all the visible parts. | |
4360 | */ | |
4361 | static void mem_cgroup_css_reset(struct cgroup_subsys_state *css) | |
4362 | { | |
4363 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); | |
4364 | ||
bbec2e15 RG |
4365 | page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX); |
4366 | page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX); | |
4367 | page_counter_set_max(&memcg->memsw, PAGE_COUNTER_MAX); | |
4368 | page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX); | |
4369 | page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX); | |
bf8d5d52 | 4370 | page_counter_set_min(&memcg->memory, 0); |
23067153 | 4371 | page_counter_set_low(&memcg->memory, 0); |
241994ed | 4372 | memcg->high = PAGE_COUNTER_MAX; |
24d404dc | 4373 | memcg->soft_limit = PAGE_COUNTER_MAX; |
2529bb3a | 4374 | memcg_wb_domain_size_changed(memcg); |
1ced953b TH |
4375 | } |
4376 | ||
02491447 | 4377 | #ifdef CONFIG_MMU |
7dc74be0 | 4378 | /* Handlers for move charge at task migration. */ |
854ffa8d | 4379 | static int mem_cgroup_do_precharge(unsigned long count) |
7dc74be0 | 4380 | { |
05b84301 | 4381 | int ret; |
9476db97 | 4382 | |
d0164adc MG |
4383 | /* Try a single bulk charge without reclaim first, kswapd may wake */ |
4384 | ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_DIRECT_RECLAIM, count); | |
9476db97 | 4385 | if (!ret) { |
854ffa8d | 4386 | mc.precharge += count; |
854ffa8d DN |
4387 | return ret; |
4388 | } | |
9476db97 | 4389 | |
3674534b | 4390 | /* Try charges one by one with reclaim, but do not retry */ |
854ffa8d | 4391 | while (count--) { |
3674534b | 4392 | ret = try_charge(mc.to, GFP_KERNEL | __GFP_NORETRY, 1); |
38c5d72f | 4393 | if (ret) |
38c5d72f | 4394 | return ret; |
854ffa8d | 4395 | mc.precharge++; |
9476db97 | 4396 | cond_resched(); |
854ffa8d | 4397 | } |
9476db97 | 4398 | return 0; |
4ffef5fe DN |
4399 | } |
4400 | ||
4ffef5fe DN |
4401 | union mc_target { |
4402 | struct page *page; | |
02491447 | 4403 | swp_entry_t ent; |
4ffef5fe DN |
4404 | }; |
4405 | ||
4ffef5fe | 4406 | enum mc_target_type { |
8d32ff84 | 4407 | MC_TARGET_NONE = 0, |
4ffef5fe | 4408 | MC_TARGET_PAGE, |
02491447 | 4409 | MC_TARGET_SWAP, |
c733a828 | 4410 | MC_TARGET_DEVICE, |
4ffef5fe DN |
4411 | }; |
4412 | ||
90254a65 DN |
4413 | static struct page *mc_handle_present_pte(struct vm_area_struct *vma, |
4414 | unsigned long addr, pte_t ptent) | |
4ffef5fe | 4415 | { |
c733a828 | 4416 | struct page *page = _vm_normal_page(vma, addr, ptent, true); |
4ffef5fe | 4417 | |
90254a65 DN |
4418 | if (!page || !page_mapped(page)) |
4419 | return NULL; | |
4420 | if (PageAnon(page)) { | |
1dfab5ab | 4421 | if (!(mc.flags & MOVE_ANON)) |
90254a65 | 4422 | return NULL; |
1dfab5ab JW |
4423 | } else { |
4424 | if (!(mc.flags & MOVE_FILE)) | |
4425 | return NULL; | |
4426 | } | |
90254a65 DN |
4427 | if (!get_page_unless_zero(page)) |
4428 | return NULL; | |
4429 | ||
4430 | return page; | |
4431 | } | |
4432 | ||
c733a828 | 4433 | #if defined(CONFIG_SWAP) || defined(CONFIG_DEVICE_PRIVATE) |
90254a65 | 4434 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
48406ef8 | 4435 | pte_t ptent, swp_entry_t *entry) |
90254a65 | 4436 | { |
90254a65 DN |
4437 | struct page *page = NULL; |
4438 | swp_entry_t ent = pte_to_swp_entry(ptent); | |
4439 | ||
1dfab5ab | 4440 | if (!(mc.flags & MOVE_ANON) || non_swap_entry(ent)) |
90254a65 | 4441 | return NULL; |
c733a828 JG |
4442 | |
4443 | /* | |
4444 | * Handle MEMORY_DEVICE_PRIVATE which are ZONE_DEVICE page belonging to | |
4445 | * a device and because they are not accessible by CPU they are store | |
4446 | * as special swap entry in the CPU page table. | |
4447 | */ | |
4448 | if (is_device_private_entry(ent)) { | |
4449 | page = device_private_entry_to_page(ent); | |
4450 | /* | |
4451 | * MEMORY_DEVICE_PRIVATE means ZONE_DEVICE page and which have | |
4452 | * a refcount of 1 when free (unlike normal page) | |
4453 | */ | |
4454 | if (!page_ref_add_unless(page, 1, 1)) | |
4455 | return NULL; | |
4456 | return page; | |
4457 | } | |
4458 | ||
4b91355e KH |
4459 | /* |
4460 | * Because lookup_swap_cache() updates some statistics counter, | |
4461 | * we call find_get_page() with swapper_space directly. | |
4462 | */ | |
f6ab1f7f | 4463 | page = find_get_page(swap_address_space(ent), swp_offset(ent)); |
7941d214 | 4464 | if (do_memsw_account()) |
90254a65 DN |
4465 | entry->val = ent.val; |
4466 | ||
4467 | return page; | |
4468 | } | |
4b91355e KH |
4469 | #else |
4470 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, | |
48406ef8 | 4471 | pte_t ptent, swp_entry_t *entry) |
4b91355e KH |
4472 | { |
4473 | return NULL; | |
4474 | } | |
4475 | #endif | |
90254a65 | 4476 | |
87946a72 DN |
4477 | static struct page *mc_handle_file_pte(struct vm_area_struct *vma, |
4478 | unsigned long addr, pte_t ptent, swp_entry_t *entry) | |
4479 | { | |
4480 | struct page *page = NULL; | |
87946a72 DN |
4481 | struct address_space *mapping; |
4482 | pgoff_t pgoff; | |
4483 | ||
4484 | if (!vma->vm_file) /* anonymous vma */ | |
4485 | return NULL; | |
1dfab5ab | 4486 | if (!(mc.flags & MOVE_FILE)) |
87946a72 DN |
4487 | return NULL; |
4488 | ||
87946a72 | 4489 | mapping = vma->vm_file->f_mapping; |
0661a336 | 4490 | pgoff = linear_page_index(vma, addr); |
87946a72 DN |
4491 | |
4492 | /* page is moved even if it's not RSS of this task(page-faulted). */ | |
aa3b1895 HD |
4493 | #ifdef CONFIG_SWAP |
4494 | /* shmem/tmpfs may report page out on swap: account for that too. */ | |
139b6a6f JW |
4495 | if (shmem_mapping(mapping)) { |
4496 | page = find_get_entry(mapping, pgoff); | |
4497 | if (radix_tree_exceptional_entry(page)) { | |
4498 | swp_entry_t swp = radix_to_swp_entry(page); | |
7941d214 | 4499 | if (do_memsw_account()) |
139b6a6f | 4500 | *entry = swp; |
f6ab1f7f HY |
4501 | page = find_get_page(swap_address_space(swp), |
4502 | swp_offset(swp)); | |
139b6a6f JW |
4503 | } |
4504 | } else | |
4505 | page = find_get_page(mapping, pgoff); | |
4506 | #else | |
4507 | page = find_get_page(mapping, pgoff); | |
aa3b1895 | 4508 | #endif |
87946a72 DN |
4509 | return page; |
4510 | } | |
4511 | ||
b1b0deab CG |
4512 | /** |
4513 | * mem_cgroup_move_account - move account of the page | |
4514 | * @page: the page | |
25843c2b | 4515 | * @compound: charge the page as compound or small page |
b1b0deab CG |
4516 | * @from: mem_cgroup which the page is moved from. |
4517 | * @to: mem_cgroup which the page is moved to. @from != @to. | |
4518 | * | |
3ac808fd | 4519 | * The caller must make sure the page is not on LRU (isolate_page() is useful.) |
b1b0deab CG |
4520 | * |
4521 | * This function doesn't do "charge" to new cgroup and doesn't do "uncharge" | |
4522 | * from old cgroup. | |
4523 | */ | |
4524 | static int mem_cgroup_move_account(struct page *page, | |
f627c2f5 | 4525 | bool compound, |
b1b0deab CG |
4526 | struct mem_cgroup *from, |
4527 | struct mem_cgroup *to) | |
4528 | { | |
4529 | unsigned long flags; | |
f627c2f5 | 4530 | unsigned int nr_pages = compound ? hpage_nr_pages(page) : 1; |
b1b0deab | 4531 | int ret; |
c4843a75 | 4532 | bool anon; |
b1b0deab CG |
4533 | |
4534 | VM_BUG_ON(from == to); | |
4535 | VM_BUG_ON_PAGE(PageLRU(page), page); | |
f627c2f5 | 4536 | VM_BUG_ON(compound && !PageTransHuge(page)); |
b1b0deab CG |
4537 | |
4538 | /* | |
6a93ca8f | 4539 | * Prevent mem_cgroup_migrate() from looking at |
45637bab | 4540 | * page->mem_cgroup of its source page while we change it. |
b1b0deab | 4541 | */ |
f627c2f5 | 4542 | ret = -EBUSY; |
b1b0deab CG |
4543 | if (!trylock_page(page)) |
4544 | goto out; | |
4545 | ||
4546 | ret = -EINVAL; | |
4547 | if (page->mem_cgroup != from) | |
4548 | goto out_unlock; | |
4549 | ||
c4843a75 GT |
4550 | anon = PageAnon(page); |
4551 | ||
b1b0deab CG |
4552 | spin_lock_irqsave(&from->move_lock, flags); |
4553 | ||
c4843a75 | 4554 | if (!anon && page_mapped(page)) { |
c9019e9b JW |
4555 | __mod_memcg_state(from, NR_FILE_MAPPED, -nr_pages); |
4556 | __mod_memcg_state(to, NR_FILE_MAPPED, nr_pages); | |
b1b0deab CG |
4557 | } |
4558 | ||
c4843a75 GT |
4559 | /* |
4560 | * move_lock grabbed above and caller set from->moving_account, so | |
ccda7f43 | 4561 | * mod_memcg_page_state will serialize updates to PageDirty. |
c4843a75 GT |
4562 | * So mapping should be stable for dirty pages. |
4563 | */ | |
4564 | if (!anon && PageDirty(page)) { | |
4565 | struct address_space *mapping = page_mapping(page); | |
4566 | ||
4567 | if (mapping_cap_account_dirty(mapping)) { | |
c9019e9b JW |
4568 | __mod_memcg_state(from, NR_FILE_DIRTY, -nr_pages); |
4569 | __mod_memcg_state(to, NR_FILE_DIRTY, nr_pages); | |
c4843a75 GT |
4570 | } |
4571 | } | |
4572 | ||
b1b0deab | 4573 | if (PageWriteback(page)) { |
c9019e9b JW |
4574 | __mod_memcg_state(from, NR_WRITEBACK, -nr_pages); |
4575 | __mod_memcg_state(to, NR_WRITEBACK, nr_pages); | |
b1b0deab CG |
4576 | } |
4577 | ||
4578 | /* | |
4579 | * It is safe to change page->mem_cgroup here because the page | |
4580 | * is referenced, charged, and isolated - we can't race with | |
4581 | * uncharging, charging, migration, or LRU putback. | |
4582 | */ | |
4583 | ||
4584 | /* caller should have done css_get */ | |
4585 | page->mem_cgroup = to; | |
4586 | spin_unlock_irqrestore(&from->move_lock, flags); | |
4587 | ||
4588 | ret = 0; | |
4589 | ||
4590 | local_irq_disable(); | |
f627c2f5 | 4591 | mem_cgroup_charge_statistics(to, page, compound, nr_pages); |
b1b0deab | 4592 | memcg_check_events(to, page); |
f627c2f5 | 4593 | mem_cgroup_charge_statistics(from, page, compound, -nr_pages); |
b1b0deab CG |
4594 | memcg_check_events(from, page); |
4595 | local_irq_enable(); | |
4596 | out_unlock: | |
4597 | unlock_page(page); | |
4598 | out: | |
4599 | return ret; | |
4600 | } | |
4601 | ||
7cf7806c LR |
4602 | /** |
4603 | * get_mctgt_type - get target type of moving charge | |
4604 | * @vma: the vma the pte to be checked belongs | |
4605 | * @addr: the address corresponding to the pte to be checked | |
4606 | * @ptent: the pte to be checked | |
4607 | * @target: the pointer the target page or swap ent will be stored(can be NULL) | |
4608 | * | |
4609 | * Returns | |
4610 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. | |
4611 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for | |
4612 | * move charge. if @target is not NULL, the page is stored in target->page | |
4613 | * with extra refcnt got(Callers should handle it). | |
4614 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a | |
4615 | * target for charge migration. if @target is not NULL, the entry is stored | |
4616 | * in target->ent. | |
df6ad698 JG |
4617 | * 3(MC_TARGET_DEVICE): like MC_TARGET_PAGE but page is MEMORY_DEVICE_PUBLIC |
4618 | * or MEMORY_DEVICE_PRIVATE (so ZONE_DEVICE page and thus not on the lru). | |
4619 | * For now we such page is charge like a regular page would be as for all | |
4620 | * intent and purposes it is just special memory taking the place of a | |
4621 | * regular page. | |
c733a828 JG |
4622 | * |
4623 | * See Documentations/vm/hmm.txt and include/linux/hmm.h | |
7cf7806c LR |
4624 | * |
4625 | * Called with pte lock held. | |
4626 | */ | |
4627 | ||
8d32ff84 | 4628 | static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma, |
90254a65 DN |
4629 | unsigned long addr, pte_t ptent, union mc_target *target) |
4630 | { | |
4631 | struct page *page = NULL; | |
8d32ff84 | 4632 | enum mc_target_type ret = MC_TARGET_NONE; |
90254a65 DN |
4633 | swp_entry_t ent = { .val = 0 }; |
4634 | ||
4635 | if (pte_present(ptent)) | |
4636 | page = mc_handle_present_pte(vma, addr, ptent); | |
4637 | else if (is_swap_pte(ptent)) | |
48406ef8 | 4638 | page = mc_handle_swap_pte(vma, ptent, &ent); |
0661a336 | 4639 | else if (pte_none(ptent)) |
87946a72 | 4640 | page = mc_handle_file_pte(vma, addr, ptent, &ent); |
90254a65 DN |
4641 | |
4642 | if (!page && !ent.val) | |
8d32ff84 | 4643 | return ret; |
02491447 | 4644 | if (page) { |
02491447 | 4645 | /* |
0a31bc97 | 4646 | * Do only loose check w/o serialization. |
1306a85a | 4647 | * mem_cgroup_move_account() checks the page is valid or |
0a31bc97 | 4648 | * not under LRU exclusion. |
02491447 | 4649 | */ |
1306a85a | 4650 | if (page->mem_cgroup == mc.from) { |
02491447 | 4651 | ret = MC_TARGET_PAGE; |
df6ad698 JG |
4652 | if (is_device_private_page(page) || |
4653 | is_device_public_page(page)) | |
c733a828 | 4654 | ret = MC_TARGET_DEVICE; |
02491447 DN |
4655 | if (target) |
4656 | target->page = page; | |
4657 | } | |
4658 | if (!ret || !target) | |
4659 | put_page(page); | |
4660 | } | |
3e14a57b HY |
4661 | /* |
4662 | * There is a swap entry and a page doesn't exist or isn't charged. | |
4663 | * But we cannot move a tail-page in a THP. | |
4664 | */ | |
4665 | if (ent.val && !ret && (!page || !PageTransCompound(page)) && | |
34c00c31 | 4666 | mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) { |
7f0f1546 KH |
4667 | ret = MC_TARGET_SWAP; |
4668 | if (target) | |
4669 | target->ent = ent; | |
4ffef5fe | 4670 | } |
4ffef5fe DN |
4671 | return ret; |
4672 | } | |
4673 | ||
12724850 NH |
4674 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
4675 | /* | |
d6810d73 HY |
4676 | * We don't consider PMD mapped swapping or file mapped pages because THP does |
4677 | * not support them for now. | |
12724850 NH |
4678 | * Caller should make sure that pmd_trans_huge(pmd) is true. |
4679 | */ | |
4680 | static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma, | |
4681 | unsigned long addr, pmd_t pmd, union mc_target *target) | |
4682 | { | |
4683 | struct page *page = NULL; | |
12724850 NH |
4684 | enum mc_target_type ret = MC_TARGET_NONE; |
4685 | ||
84c3fc4e ZY |
4686 | if (unlikely(is_swap_pmd(pmd))) { |
4687 | VM_BUG_ON(thp_migration_supported() && | |
4688 | !is_pmd_migration_entry(pmd)); | |
4689 | return ret; | |
4690 | } | |
12724850 | 4691 | page = pmd_page(pmd); |
309381fe | 4692 | VM_BUG_ON_PAGE(!page || !PageHead(page), page); |
1dfab5ab | 4693 | if (!(mc.flags & MOVE_ANON)) |
12724850 | 4694 | return ret; |
1306a85a | 4695 | if (page->mem_cgroup == mc.from) { |
12724850 NH |
4696 | ret = MC_TARGET_PAGE; |
4697 | if (target) { | |
4698 | get_page(page); | |
4699 | target->page = page; | |
4700 | } | |
4701 | } | |
4702 | return ret; | |
4703 | } | |
4704 | #else | |
4705 | static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma, | |
4706 | unsigned long addr, pmd_t pmd, union mc_target *target) | |
4707 | { | |
4708 | return MC_TARGET_NONE; | |
4709 | } | |
4710 | #endif | |
4711 | ||
4ffef5fe DN |
4712 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, |
4713 | unsigned long addr, unsigned long end, | |
4714 | struct mm_walk *walk) | |
4715 | { | |
26bcd64a | 4716 | struct vm_area_struct *vma = walk->vma; |
4ffef5fe DN |
4717 | pte_t *pte; |
4718 | spinlock_t *ptl; | |
4719 | ||
b6ec57f4 KS |
4720 | ptl = pmd_trans_huge_lock(pmd, vma); |
4721 | if (ptl) { | |
c733a828 JG |
4722 | /* |
4723 | * Note their can not be MC_TARGET_DEVICE for now as we do not | |
4724 | * support transparent huge page with MEMORY_DEVICE_PUBLIC or | |
4725 | * MEMORY_DEVICE_PRIVATE but this might change. | |
4726 | */ | |
12724850 NH |
4727 | if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE) |
4728 | mc.precharge += HPAGE_PMD_NR; | |
bf929152 | 4729 | spin_unlock(ptl); |
1a5a9906 | 4730 | return 0; |
12724850 | 4731 | } |
03319327 | 4732 | |
45f83cef AA |
4733 | if (pmd_trans_unstable(pmd)) |
4734 | return 0; | |
4ffef5fe DN |
4735 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
4736 | for (; addr != end; pte++, addr += PAGE_SIZE) | |
8d32ff84 | 4737 | if (get_mctgt_type(vma, addr, *pte, NULL)) |
4ffef5fe DN |
4738 | mc.precharge++; /* increment precharge temporarily */ |
4739 | pte_unmap_unlock(pte - 1, ptl); | |
4740 | cond_resched(); | |
4741 | ||
7dc74be0 DN |
4742 | return 0; |
4743 | } | |
4744 | ||
4ffef5fe DN |
4745 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
4746 | { | |
4747 | unsigned long precharge; | |
4ffef5fe | 4748 | |
26bcd64a NH |
4749 | struct mm_walk mem_cgroup_count_precharge_walk = { |
4750 | .pmd_entry = mem_cgroup_count_precharge_pte_range, | |
4751 | .mm = mm, | |
4752 | }; | |
dfe076b0 | 4753 | down_read(&mm->mmap_sem); |
0247f3f4 JM |
4754 | walk_page_range(0, mm->highest_vm_end, |
4755 | &mem_cgroup_count_precharge_walk); | |
dfe076b0 | 4756 | up_read(&mm->mmap_sem); |
4ffef5fe DN |
4757 | |
4758 | precharge = mc.precharge; | |
4759 | mc.precharge = 0; | |
4760 | ||
4761 | return precharge; | |
4762 | } | |
4763 | ||
4ffef5fe DN |
4764 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
4765 | { | |
dfe076b0 DN |
4766 | unsigned long precharge = mem_cgroup_count_precharge(mm); |
4767 | ||
4768 | VM_BUG_ON(mc.moving_task); | |
4769 | mc.moving_task = current; | |
4770 | return mem_cgroup_do_precharge(precharge); | |
4ffef5fe DN |
4771 | } |
4772 | ||
dfe076b0 DN |
4773 | /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */ |
4774 | static void __mem_cgroup_clear_mc(void) | |
4ffef5fe | 4775 | { |
2bd9bb20 KH |
4776 | struct mem_cgroup *from = mc.from; |
4777 | struct mem_cgroup *to = mc.to; | |
4778 | ||
4ffef5fe | 4779 | /* we must uncharge all the leftover precharges from mc.to */ |
854ffa8d | 4780 | if (mc.precharge) { |
00501b53 | 4781 | cancel_charge(mc.to, mc.precharge); |
854ffa8d DN |
4782 | mc.precharge = 0; |
4783 | } | |
4784 | /* | |
4785 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so | |
4786 | * we must uncharge here. | |
4787 | */ | |
4788 | if (mc.moved_charge) { | |
00501b53 | 4789 | cancel_charge(mc.from, mc.moved_charge); |
854ffa8d | 4790 | mc.moved_charge = 0; |
4ffef5fe | 4791 | } |
483c30b5 DN |
4792 | /* we must fixup refcnts and charges */ |
4793 | if (mc.moved_swap) { | |
483c30b5 | 4794 | /* uncharge swap account from the old cgroup */ |
ce00a967 | 4795 | if (!mem_cgroup_is_root(mc.from)) |
3e32cb2e | 4796 | page_counter_uncharge(&mc.from->memsw, mc.moved_swap); |
483c30b5 | 4797 | |
615d66c3 VD |
4798 | mem_cgroup_id_put_many(mc.from, mc.moved_swap); |
4799 | ||
05b84301 | 4800 | /* |
3e32cb2e JW |
4801 | * we charged both to->memory and to->memsw, so we |
4802 | * should uncharge to->memory. | |
05b84301 | 4803 | */ |
ce00a967 | 4804 | if (!mem_cgroup_is_root(mc.to)) |
3e32cb2e JW |
4805 | page_counter_uncharge(&mc.to->memory, mc.moved_swap); |
4806 | ||
615d66c3 VD |
4807 | mem_cgroup_id_get_many(mc.to, mc.moved_swap); |
4808 | css_put_many(&mc.to->css, mc.moved_swap); | |
3e32cb2e | 4809 | |
483c30b5 DN |
4810 | mc.moved_swap = 0; |
4811 | } | |
dfe076b0 DN |
4812 | memcg_oom_recover(from); |
4813 | memcg_oom_recover(to); | |
4814 | wake_up_all(&mc.waitq); | |
4815 | } | |
4816 | ||
4817 | static void mem_cgroup_clear_mc(void) | |
4818 | { | |
264a0ae1 TH |
4819 | struct mm_struct *mm = mc.mm; |
4820 | ||
dfe076b0 DN |
4821 | /* |
4822 | * we must clear moving_task before waking up waiters at the end of | |
4823 | * task migration. | |
4824 | */ | |
4825 | mc.moving_task = NULL; | |
4826 | __mem_cgroup_clear_mc(); | |
2bd9bb20 | 4827 | spin_lock(&mc.lock); |
4ffef5fe DN |
4828 | mc.from = NULL; |
4829 | mc.to = NULL; | |
264a0ae1 | 4830 | mc.mm = NULL; |
2bd9bb20 | 4831 | spin_unlock(&mc.lock); |
264a0ae1 TH |
4832 | |
4833 | mmput(mm); | |
4ffef5fe DN |
4834 | } |
4835 | ||
1f7dd3e5 | 4836 | static int mem_cgroup_can_attach(struct cgroup_taskset *tset) |
7dc74be0 | 4837 | { |
1f7dd3e5 | 4838 | struct cgroup_subsys_state *css; |
eed67d75 | 4839 | struct mem_cgroup *memcg = NULL; /* unneeded init to make gcc happy */ |
9f2115f9 | 4840 | struct mem_cgroup *from; |
4530eddb | 4841 | struct task_struct *leader, *p; |
9f2115f9 | 4842 | struct mm_struct *mm; |
1dfab5ab | 4843 | unsigned long move_flags; |
9f2115f9 | 4844 | int ret = 0; |
7dc74be0 | 4845 | |
1f7dd3e5 TH |
4846 | /* charge immigration isn't supported on the default hierarchy */ |
4847 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) | |
9f2115f9 TH |
4848 | return 0; |
4849 | ||
4530eddb TH |
4850 | /* |
4851 | * Multi-process migrations only happen on the default hierarchy | |
4852 | * where charge immigration is not used. Perform charge | |
4853 | * immigration if @tset contains a leader and whine if there are | |
4854 | * multiple. | |
4855 | */ | |
4856 | p = NULL; | |
1f7dd3e5 | 4857 | cgroup_taskset_for_each_leader(leader, css, tset) { |
4530eddb TH |
4858 | WARN_ON_ONCE(p); |
4859 | p = leader; | |
1f7dd3e5 | 4860 | memcg = mem_cgroup_from_css(css); |
4530eddb TH |
4861 | } |
4862 | if (!p) | |
4863 | return 0; | |
4864 | ||
1f7dd3e5 TH |
4865 | /* |
4866 | * We are now commited to this value whatever it is. Changes in this | |
4867 | * tunable will only affect upcoming migrations, not the current one. | |
4868 | * So we need to save it, and keep it going. | |
4869 | */ | |
4870 | move_flags = READ_ONCE(memcg->move_charge_at_immigrate); | |
4871 | if (!move_flags) | |
4872 | return 0; | |
4873 | ||
9f2115f9 TH |
4874 | from = mem_cgroup_from_task(p); |
4875 | ||
4876 | VM_BUG_ON(from == memcg); | |
4877 | ||
4878 | mm = get_task_mm(p); | |
4879 | if (!mm) | |
4880 | return 0; | |
4881 | /* We move charges only when we move a owner of the mm */ | |
4882 | if (mm->owner == p) { | |
4883 | VM_BUG_ON(mc.from); | |
4884 | VM_BUG_ON(mc.to); | |
4885 | VM_BUG_ON(mc.precharge); | |
4886 | VM_BUG_ON(mc.moved_charge); | |
4887 | VM_BUG_ON(mc.moved_swap); | |
4888 | ||
4889 | spin_lock(&mc.lock); | |
264a0ae1 | 4890 | mc.mm = mm; |
9f2115f9 TH |
4891 | mc.from = from; |
4892 | mc.to = memcg; | |
4893 | mc.flags = move_flags; | |
4894 | spin_unlock(&mc.lock); | |
4895 | /* We set mc.moving_task later */ | |
4896 | ||
4897 | ret = mem_cgroup_precharge_mc(mm); | |
4898 | if (ret) | |
4899 | mem_cgroup_clear_mc(); | |
264a0ae1 TH |
4900 | } else { |
4901 | mmput(mm); | |
7dc74be0 DN |
4902 | } |
4903 | return ret; | |
4904 | } | |
4905 | ||
1f7dd3e5 | 4906 | static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset) |
7dc74be0 | 4907 | { |
4e2f245d JW |
4908 | if (mc.to) |
4909 | mem_cgroup_clear_mc(); | |
7dc74be0 DN |
4910 | } |
4911 | ||
4ffef5fe DN |
4912 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
4913 | unsigned long addr, unsigned long end, | |
4914 | struct mm_walk *walk) | |
7dc74be0 | 4915 | { |
4ffef5fe | 4916 | int ret = 0; |
26bcd64a | 4917 | struct vm_area_struct *vma = walk->vma; |
4ffef5fe DN |
4918 | pte_t *pte; |
4919 | spinlock_t *ptl; | |
12724850 NH |
4920 | enum mc_target_type target_type; |
4921 | union mc_target target; | |
4922 | struct page *page; | |
4ffef5fe | 4923 | |
b6ec57f4 KS |
4924 | ptl = pmd_trans_huge_lock(pmd, vma); |
4925 | if (ptl) { | |
62ade86a | 4926 | if (mc.precharge < HPAGE_PMD_NR) { |
bf929152 | 4927 | spin_unlock(ptl); |
12724850 NH |
4928 | return 0; |
4929 | } | |
4930 | target_type = get_mctgt_type_thp(vma, addr, *pmd, &target); | |
4931 | if (target_type == MC_TARGET_PAGE) { | |
4932 | page = target.page; | |
4933 | if (!isolate_lru_page(page)) { | |
f627c2f5 | 4934 | if (!mem_cgroup_move_account(page, true, |
1306a85a | 4935 | mc.from, mc.to)) { |
12724850 NH |
4936 | mc.precharge -= HPAGE_PMD_NR; |
4937 | mc.moved_charge += HPAGE_PMD_NR; | |
4938 | } | |
4939 | putback_lru_page(page); | |
4940 | } | |
4941 | put_page(page); | |
c733a828 JG |
4942 | } else if (target_type == MC_TARGET_DEVICE) { |
4943 | page = target.page; | |
4944 | if (!mem_cgroup_move_account(page, true, | |
4945 | mc.from, mc.to)) { | |
4946 | mc.precharge -= HPAGE_PMD_NR; | |
4947 | mc.moved_charge += HPAGE_PMD_NR; | |
4948 | } | |
4949 | put_page(page); | |
12724850 | 4950 | } |
bf929152 | 4951 | spin_unlock(ptl); |
1a5a9906 | 4952 | return 0; |
12724850 NH |
4953 | } |
4954 | ||
45f83cef AA |
4955 | if (pmd_trans_unstable(pmd)) |
4956 | return 0; | |
4ffef5fe DN |
4957 | retry: |
4958 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); | |
4959 | for (; addr != end; addr += PAGE_SIZE) { | |
4960 | pte_t ptent = *(pte++); | |
c733a828 | 4961 | bool device = false; |
02491447 | 4962 | swp_entry_t ent; |
4ffef5fe DN |
4963 | |
4964 | if (!mc.precharge) | |
4965 | break; | |
4966 | ||
8d32ff84 | 4967 | switch (get_mctgt_type(vma, addr, ptent, &target)) { |
c733a828 JG |
4968 | case MC_TARGET_DEVICE: |
4969 | device = true; | |
4970 | /* fall through */ | |
4ffef5fe DN |
4971 | case MC_TARGET_PAGE: |
4972 | page = target.page; | |
53f9263b KS |
4973 | /* |
4974 | * We can have a part of the split pmd here. Moving it | |
4975 | * can be done but it would be too convoluted so simply | |
4976 | * ignore such a partial THP and keep it in original | |
4977 | * memcg. There should be somebody mapping the head. | |
4978 | */ | |
4979 | if (PageTransCompound(page)) | |
4980 | goto put; | |
c733a828 | 4981 | if (!device && isolate_lru_page(page)) |
4ffef5fe | 4982 | goto put; |
f627c2f5 KS |
4983 | if (!mem_cgroup_move_account(page, false, |
4984 | mc.from, mc.to)) { | |
4ffef5fe | 4985 | mc.precharge--; |
854ffa8d DN |
4986 | /* we uncharge from mc.from later. */ |
4987 | mc.moved_charge++; | |
4ffef5fe | 4988 | } |
c733a828 JG |
4989 | if (!device) |
4990 | putback_lru_page(page); | |
8d32ff84 | 4991 | put: /* get_mctgt_type() gets the page */ |
4ffef5fe DN |
4992 | put_page(page); |
4993 | break; | |
02491447 DN |
4994 | case MC_TARGET_SWAP: |
4995 | ent = target.ent; | |
e91cbb42 | 4996 | if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) { |
02491447 | 4997 | mc.precharge--; |
483c30b5 DN |
4998 | /* we fixup refcnts and charges later. */ |
4999 | mc.moved_swap++; | |
5000 | } | |
02491447 | 5001 | break; |
4ffef5fe DN |
5002 | default: |
5003 | break; | |
5004 | } | |
5005 | } | |
5006 | pte_unmap_unlock(pte - 1, ptl); | |
5007 | cond_resched(); | |
5008 | ||
5009 | if (addr != end) { | |
5010 | /* | |
5011 | * We have consumed all precharges we got in can_attach(). | |
5012 | * We try charge one by one, but don't do any additional | |
5013 | * charges to mc.to if we have failed in charge once in attach() | |
5014 | * phase. | |
5015 | */ | |
854ffa8d | 5016 | ret = mem_cgroup_do_precharge(1); |
4ffef5fe DN |
5017 | if (!ret) |
5018 | goto retry; | |
5019 | } | |
5020 | ||
5021 | return ret; | |
5022 | } | |
5023 | ||
264a0ae1 | 5024 | static void mem_cgroup_move_charge(void) |
4ffef5fe | 5025 | { |
26bcd64a NH |
5026 | struct mm_walk mem_cgroup_move_charge_walk = { |
5027 | .pmd_entry = mem_cgroup_move_charge_pte_range, | |
264a0ae1 | 5028 | .mm = mc.mm, |
26bcd64a | 5029 | }; |
4ffef5fe DN |
5030 | |
5031 | lru_add_drain_all(); | |
312722cb | 5032 | /* |
81f8c3a4 JW |
5033 | * Signal lock_page_memcg() to take the memcg's move_lock |
5034 | * while we're moving its pages to another memcg. Then wait | |
5035 | * for already started RCU-only updates to finish. | |
312722cb JW |
5036 | */ |
5037 | atomic_inc(&mc.from->moving_account); | |
5038 | synchronize_rcu(); | |
dfe076b0 | 5039 | retry: |
264a0ae1 | 5040 | if (unlikely(!down_read_trylock(&mc.mm->mmap_sem))) { |
dfe076b0 DN |
5041 | /* |
5042 | * Someone who are holding the mmap_sem might be waiting in | |
5043 | * waitq. So we cancel all extra charges, wake up all waiters, | |
5044 | * and retry. Because we cancel precharges, we might not be able | |
5045 | * to move enough charges, but moving charge is a best-effort | |
5046 | * feature anyway, so it wouldn't be a big problem. | |
5047 | */ | |
5048 | __mem_cgroup_clear_mc(); | |
5049 | cond_resched(); | |
5050 | goto retry; | |
5051 | } | |
26bcd64a NH |
5052 | /* |
5053 | * When we have consumed all precharges and failed in doing | |
5054 | * additional charge, the page walk just aborts. | |
5055 | */ | |
0247f3f4 JM |
5056 | walk_page_range(0, mc.mm->highest_vm_end, &mem_cgroup_move_charge_walk); |
5057 | ||
264a0ae1 | 5058 | up_read(&mc.mm->mmap_sem); |
312722cb | 5059 | atomic_dec(&mc.from->moving_account); |
7dc74be0 DN |
5060 | } |
5061 | ||
264a0ae1 | 5062 | static void mem_cgroup_move_task(void) |
67e465a7 | 5063 | { |
264a0ae1 TH |
5064 | if (mc.to) { |
5065 | mem_cgroup_move_charge(); | |
a433658c | 5066 | mem_cgroup_clear_mc(); |
264a0ae1 | 5067 | } |
67e465a7 | 5068 | } |
5cfb80a7 | 5069 | #else /* !CONFIG_MMU */ |
1f7dd3e5 | 5070 | static int mem_cgroup_can_attach(struct cgroup_taskset *tset) |
5cfb80a7 DN |
5071 | { |
5072 | return 0; | |
5073 | } | |
1f7dd3e5 | 5074 | static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset) |
5cfb80a7 DN |
5075 | { |
5076 | } | |
264a0ae1 | 5077 | static void mem_cgroup_move_task(void) |
5cfb80a7 DN |
5078 | { |
5079 | } | |
5080 | #endif | |
67e465a7 | 5081 | |
f00baae7 TH |
5082 | /* |
5083 | * Cgroup retains root cgroups across [un]mount cycles making it necessary | |
aa6ec29b TH |
5084 | * to verify whether we're attached to the default hierarchy on each mount |
5085 | * attempt. | |
f00baae7 | 5086 | */ |
eb95419b | 5087 | static void mem_cgroup_bind(struct cgroup_subsys_state *root_css) |
f00baae7 TH |
5088 | { |
5089 | /* | |
aa6ec29b | 5090 | * use_hierarchy is forced on the default hierarchy. cgroup core |
f00baae7 TH |
5091 | * guarantees that @root doesn't have any children, so turning it |
5092 | * on for the root memcg is enough. | |
5093 | */ | |
9e10a130 | 5094 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
7feee590 VD |
5095 | root_mem_cgroup->use_hierarchy = true; |
5096 | else | |
5097 | root_mem_cgroup->use_hierarchy = false; | |
f00baae7 TH |
5098 | } |
5099 | ||
241994ed JW |
5100 | static u64 memory_current_read(struct cgroup_subsys_state *css, |
5101 | struct cftype *cft) | |
5102 | { | |
f5fc3c5d JW |
5103 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
5104 | ||
5105 | return (u64)page_counter_read(&memcg->memory) * PAGE_SIZE; | |
241994ed JW |
5106 | } |
5107 | ||
bf8d5d52 RG |
5108 | static int memory_min_show(struct seq_file *m, void *v) |
5109 | { | |
5110 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); | |
5111 | unsigned long min = READ_ONCE(memcg->memory.min); | |
5112 | ||
5113 | if (min == PAGE_COUNTER_MAX) | |
5114 | seq_puts(m, "max\n"); | |
5115 | else | |
5116 | seq_printf(m, "%llu\n", (u64)min * PAGE_SIZE); | |
5117 | ||
5118 | return 0; | |
5119 | } | |
5120 | ||
5121 | static ssize_t memory_min_write(struct kernfs_open_file *of, | |
5122 | char *buf, size_t nbytes, loff_t off) | |
5123 | { | |
5124 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); | |
5125 | unsigned long min; | |
5126 | int err; | |
5127 | ||
5128 | buf = strstrip(buf); | |
5129 | err = page_counter_memparse(buf, "max", &min); | |
5130 | if (err) | |
5131 | return err; | |
5132 | ||
5133 | page_counter_set_min(&memcg->memory, min); | |
5134 | ||
5135 | return nbytes; | |
5136 | } | |
5137 | ||
241994ed JW |
5138 | static int memory_low_show(struct seq_file *m, void *v) |
5139 | { | |
5140 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); | |
23067153 | 5141 | unsigned long low = READ_ONCE(memcg->memory.low); |
241994ed JW |
5142 | |
5143 | if (low == PAGE_COUNTER_MAX) | |
d2973697 | 5144 | seq_puts(m, "max\n"); |
241994ed JW |
5145 | else |
5146 | seq_printf(m, "%llu\n", (u64)low * PAGE_SIZE); | |
5147 | ||
5148 | return 0; | |
5149 | } | |
5150 | ||
5151 | static ssize_t memory_low_write(struct kernfs_open_file *of, | |
5152 | char *buf, size_t nbytes, loff_t off) | |
5153 | { | |
5154 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); | |
5155 | unsigned long low; | |
5156 | int err; | |
5157 | ||
5158 | buf = strstrip(buf); | |
d2973697 | 5159 | err = page_counter_memparse(buf, "max", &low); |
241994ed JW |
5160 | if (err) |
5161 | return err; | |
5162 | ||
23067153 | 5163 | page_counter_set_low(&memcg->memory, low); |
241994ed JW |
5164 | |
5165 | return nbytes; | |
5166 | } | |
5167 | ||
5168 | static int memory_high_show(struct seq_file *m, void *v) | |
5169 | { | |
5170 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); | |
4db0c3c2 | 5171 | unsigned long high = READ_ONCE(memcg->high); |
241994ed JW |
5172 | |
5173 | if (high == PAGE_COUNTER_MAX) | |
d2973697 | 5174 | seq_puts(m, "max\n"); |
241994ed JW |
5175 | else |
5176 | seq_printf(m, "%llu\n", (u64)high * PAGE_SIZE); | |
5177 | ||
5178 | return 0; | |
5179 | } | |
5180 | ||
5181 | static ssize_t memory_high_write(struct kernfs_open_file *of, | |
5182 | char *buf, size_t nbytes, loff_t off) | |
5183 | { | |
5184 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); | |
588083bb | 5185 | unsigned long nr_pages; |
241994ed JW |
5186 | unsigned long high; |
5187 | int err; | |
5188 | ||
5189 | buf = strstrip(buf); | |
d2973697 | 5190 | err = page_counter_memparse(buf, "max", &high); |
241994ed JW |
5191 | if (err) |
5192 | return err; | |
5193 | ||
5194 | memcg->high = high; | |
5195 | ||
588083bb JW |
5196 | nr_pages = page_counter_read(&memcg->memory); |
5197 | if (nr_pages > high) | |
5198 | try_to_free_mem_cgroup_pages(memcg, nr_pages - high, | |
5199 | GFP_KERNEL, true); | |
5200 | ||
2529bb3a | 5201 | memcg_wb_domain_size_changed(memcg); |
241994ed JW |
5202 | return nbytes; |
5203 | } | |
5204 | ||
5205 | static int memory_max_show(struct seq_file *m, void *v) | |
5206 | { | |
5207 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); | |
bbec2e15 | 5208 | unsigned long max = READ_ONCE(memcg->memory.max); |
241994ed JW |
5209 | |
5210 | if (max == PAGE_COUNTER_MAX) | |
d2973697 | 5211 | seq_puts(m, "max\n"); |
241994ed JW |
5212 | else |
5213 | seq_printf(m, "%llu\n", (u64)max * PAGE_SIZE); | |
5214 | ||
5215 | return 0; | |
5216 | } | |
5217 | ||
5218 | static ssize_t memory_max_write(struct kernfs_open_file *of, | |
5219 | char *buf, size_t nbytes, loff_t off) | |
5220 | { | |
5221 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); | |
b6e6edcf JW |
5222 | unsigned int nr_reclaims = MEM_CGROUP_RECLAIM_RETRIES; |
5223 | bool drained = false; | |
241994ed JW |
5224 | unsigned long max; |
5225 | int err; | |
5226 | ||
5227 | buf = strstrip(buf); | |
d2973697 | 5228 | err = page_counter_memparse(buf, "max", &max); |
241994ed JW |
5229 | if (err) |
5230 | return err; | |
5231 | ||
bbec2e15 | 5232 | xchg(&memcg->memory.max, max); |
b6e6edcf JW |
5233 | |
5234 | for (;;) { | |
5235 | unsigned long nr_pages = page_counter_read(&memcg->memory); | |
5236 | ||
5237 | if (nr_pages <= max) | |
5238 | break; | |
5239 | ||
5240 | if (signal_pending(current)) { | |
5241 | err = -EINTR; | |
5242 | break; | |
5243 | } | |
5244 | ||
5245 | if (!drained) { | |
5246 | drain_all_stock(memcg); | |
5247 | drained = true; | |
5248 | continue; | |
5249 | } | |
5250 | ||
5251 | if (nr_reclaims) { | |
5252 | if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max, | |
5253 | GFP_KERNEL, true)) | |
5254 | nr_reclaims--; | |
5255 | continue; | |
5256 | } | |
5257 | ||
e27be240 | 5258 | memcg_memory_event(memcg, MEMCG_OOM); |
b6e6edcf JW |
5259 | if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0)) |
5260 | break; | |
5261 | } | |
241994ed | 5262 | |
2529bb3a | 5263 | memcg_wb_domain_size_changed(memcg); |
241994ed JW |
5264 | return nbytes; |
5265 | } | |
5266 | ||
5267 | static int memory_events_show(struct seq_file *m, void *v) | |
5268 | { | |
5269 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); | |
5270 | ||
e27be240 JW |
5271 | seq_printf(m, "low %lu\n", |
5272 | atomic_long_read(&memcg->memory_events[MEMCG_LOW])); | |
5273 | seq_printf(m, "high %lu\n", | |
5274 | atomic_long_read(&memcg->memory_events[MEMCG_HIGH])); | |
5275 | seq_printf(m, "max %lu\n", | |
5276 | atomic_long_read(&memcg->memory_events[MEMCG_MAX])); | |
5277 | seq_printf(m, "oom %lu\n", | |
5278 | atomic_long_read(&memcg->memory_events[MEMCG_OOM])); | |
fe6bdfc8 RG |
5279 | seq_printf(m, "oom_kill %lu\n", |
5280 | atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL])); | |
241994ed JW |
5281 | |
5282 | return 0; | |
5283 | } | |
5284 | ||
587d9f72 JW |
5285 | static int memory_stat_show(struct seq_file *m, void *v) |
5286 | { | |
5287 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); | |
72b54e73 | 5288 | unsigned long stat[MEMCG_NR_STAT]; |
e27be240 | 5289 | unsigned long events[NR_VM_EVENT_ITEMS]; |
587d9f72 JW |
5290 | int i; |
5291 | ||
5292 | /* | |
5293 | * Provide statistics on the state of the memory subsystem as | |
5294 | * well as cumulative event counters that show past behavior. | |
5295 | * | |
5296 | * This list is ordered following a combination of these gradients: | |
5297 | * 1) generic big picture -> specifics and details | |
5298 | * 2) reflecting userspace activity -> reflecting kernel heuristics | |
5299 | * | |
5300 | * Current memory state: | |
5301 | */ | |
5302 | ||
72b54e73 VD |
5303 | tree_stat(memcg, stat); |
5304 | tree_events(memcg, events); | |
5305 | ||
587d9f72 | 5306 | seq_printf(m, "anon %llu\n", |
71cd3113 | 5307 | (u64)stat[MEMCG_RSS] * PAGE_SIZE); |
587d9f72 | 5308 | seq_printf(m, "file %llu\n", |
71cd3113 | 5309 | (u64)stat[MEMCG_CACHE] * PAGE_SIZE); |
12580e4b | 5310 | seq_printf(m, "kernel_stack %llu\n", |
efdc9490 | 5311 | (u64)stat[MEMCG_KERNEL_STACK_KB] * 1024); |
27ee57c9 | 5312 | seq_printf(m, "slab %llu\n", |
32049296 JW |
5313 | (u64)(stat[NR_SLAB_RECLAIMABLE] + |
5314 | stat[NR_SLAB_UNRECLAIMABLE]) * PAGE_SIZE); | |
b2807f07 | 5315 | seq_printf(m, "sock %llu\n", |
72b54e73 | 5316 | (u64)stat[MEMCG_SOCK] * PAGE_SIZE); |
587d9f72 | 5317 | |
9a4caf1e | 5318 | seq_printf(m, "shmem %llu\n", |
71cd3113 | 5319 | (u64)stat[NR_SHMEM] * PAGE_SIZE); |
587d9f72 | 5320 | seq_printf(m, "file_mapped %llu\n", |
71cd3113 | 5321 | (u64)stat[NR_FILE_MAPPED] * PAGE_SIZE); |
587d9f72 | 5322 | seq_printf(m, "file_dirty %llu\n", |
71cd3113 | 5323 | (u64)stat[NR_FILE_DIRTY] * PAGE_SIZE); |
587d9f72 | 5324 | seq_printf(m, "file_writeback %llu\n", |
71cd3113 | 5325 | (u64)stat[NR_WRITEBACK] * PAGE_SIZE); |
587d9f72 JW |
5326 | |
5327 | for (i = 0; i < NR_LRU_LISTS; i++) { | |
5328 | struct mem_cgroup *mi; | |
5329 | unsigned long val = 0; | |
5330 | ||
5331 | for_each_mem_cgroup_tree(mi, memcg) | |
5332 | val += mem_cgroup_nr_lru_pages(mi, BIT(i)); | |
5333 | seq_printf(m, "%s %llu\n", | |
5334 | mem_cgroup_lru_names[i], (u64)val * PAGE_SIZE); | |
5335 | } | |
5336 | ||
27ee57c9 | 5337 | seq_printf(m, "slab_reclaimable %llu\n", |
32049296 | 5338 | (u64)stat[NR_SLAB_RECLAIMABLE] * PAGE_SIZE); |
27ee57c9 | 5339 | seq_printf(m, "slab_unreclaimable %llu\n", |
32049296 | 5340 | (u64)stat[NR_SLAB_UNRECLAIMABLE] * PAGE_SIZE); |
27ee57c9 | 5341 | |
587d9f72 JW |
5342 | /* Accumulated memory events */ |
5343 | ||
df0e53d0 JW |
5344 | seq_printf(m, "pgfault %lu\n", events[PGFAULT]); |
5345 | seq_printf(m, "pgmajfault %lu\n", events[PGMAJFAULT]); | |
587d9f72 | 5346 | |
2262185c RG |
5347 | seq_printf(m, "pgrefill %lu\n", events[PGREFILL]); |
5348 | seq_printf(m, "pgscan %lu\n", events[PGSCAN_KSWAPD] + | |
5349 | events[PGSCAN_DIRECT]); | |
5350 | seq_printf(m, "pgsteal %lu\n", events[PGSTEAL_KSWAPD] + | |
5351 | events[PGSTEAL_DIRECT]); | |
5352 | seq_printf(m, "pgactivate %lu\n", events[PGACTIVATE]); | |
5353 | seq_printf(m, "pgdeactivate %lu\n", events[PGDEACTIVATE]); | |
5354 | seq_printf(m, "pglazyfree %lu\n", events[PGLAZYFREE]); | |
5355 | seq_printf(m, "pglazyfreed %lu\n", events[PGLAZYFREED]); | |
5356 | ||
2a2e4885 | 5357 | seq_printf(m, "workingset_refault %lu\n", |
71cd3113 | 5358 | stat[WORKINGSET_REFAULT]); |
2a2e4885 | 5359 | seq_printf(m, "workingset_activate %lu\n", |
71cd3113 | 5360 | stat[WORKINGSET_ACTIVATE]); |
2a2e4885 | 5361 | seq_printf(m, "workingset_nodereclaim %lu\n", |
71cd3113 | 5362 | stat[WORKINGSET_NODERECLAIM]); |
2a2e4885 | 5363 | |
587d9f72 JW |
5364 | return 0; |
5365 | } | |
5366 | ||
241994ed JW |
5367 | static struct cftype memory_files[] = { |
5368 | { | |
5369 | .name = "current", | |
f5fc3c5d | 5370 | .flags = CFTYPE_NOT_ON_ROOT, |
241994ed JW |
5371 | .read_u64 = memory_current_read, |
5372 | }, | |
bf8d5d52 RG |
5373 | { |
5374 | .name = "min", | |
5375 | .flags = CFTYPE_NOT_ON_ROOT, | |
5376 | .seq_show = memory_min_show, | |
5377 | .write = memory_min_write, | |
5378 | }, | |
241994ed JW |
5379 | { |
5380 | .name = "low", | |
5381 | .flags = CFTYPE_NOT_ON_ROOT, | |
5382 | .seq_show = memory_low_show, | |
5383 | .write = memory_low_write, | |
5384 | }, | |
5385 | { | |
5386 | .name = "high", | |
5387 | .flags = CFTYPE_NOT_ON_ROOT, | |
5388 | .seq_show = memory_high_show, | |
5389 | .write = memory_high_write, | |
5390 | }, | |
5391 | { | |
5392 | .name = "max", | |
5393 | .flags = CFTYPE_NOT_ON_ROOT, | |
5394 | .seq_show = memory_max_show, | |
5395 | .write = memory_max_write, | |
5396 | }, | |
5397 | { | |
5398 | .name = "events", | |
5399 | .flags = CFTYPE_NOT_ON_ROOT, | |
472912a2 | 5400 | .file_offset = offsetof(struct mem_cgroup, events_file), |
241994ed JW |
5401 | .seq_show = memory_events_show, |
5402 | }, | |
587d9f72 JW |
5403 | { |
5404 | .name = "stat", | |
5405 | .flags = CFTYPE_NOT_ON_ROOT, | |
5406 | .seq_show = memory_stat_show, | |
5407 | }, | |
241994ed JW |
5408 | { } /* terminate */ |
5409 | }; | |
5410 | ||
073219e9 | 5411 | struct cgroup_subsys memory_cgrp_subsys = { |
92fb9748 | 5412 | .css_alloc = mem_cgroup_css_alloc, |
d142e3e6 | 5413 | .css_online = mem_cgroup_css_online, |
92fb9748 | 5414 | .css_offline = mem_cgroup_css_offline, |
6df38689 | 5415 | .css_released = mem_cgroup_css_released, |
92fb9748 | 5416 | .css_free = mem_cgroup_css_free, |
1ced953b | 5417 | .css_reset = mem_cgroup_css_reset, |
7dc74be0 DN |
5418 | .can_attach = mem_cgroup_can_attach, |
5419 | .cancel_attach = mem_cgroup_cancel_attach, | |
264a0ae1 | 5420 | .post_attach = mem_cgroup_move_task, |
f00baae7 | 5421 | .bind = mem_cgroup_bind, |
241994ed JW |
5422 | .dfl_cftypes = memory_files, |
5423 | .legacy_cftypes = mem_cgroup_legacy_files, | |
6d12e2d8 | 5424 | .early_init = 0, |
8cdea7c0 | 5425 | }; |
c077719b | 5426 | |
241994ed | 5427 | /** |
bf8d5d52 | 5428 | * mem_cgroup_protected - check if memory consumption is in the normal range |
34c81057 | 5429 | * @root: the top ancestor of the sub-tree being checked |
241994ed JW |
5430 | * @memcg: the memory cgroup to check |
5431 | * | |
23067153 RG |
5432 | * WARNING: This function is not stateless! It can only be used as part |
5433 | * of a top-down tree iteration, not for isolated queries. | |
34c81057 | 5434 | * |
bf8d5d52 RG |
5435 | * Returns one of the following: |
5436 | * MEMCG_PROT_NONE: cgroup memory is not protected | |
5437 | * MEMCG_PROT_LOW: cgroup memory is protected as long there is | |
5438 | * an unprotected supply of reclaimable memory from other cgroups. | |
5439 | * MEMCG_PROT_MIN: cgroup memory is protected | |
34c81057 | 5440 | * |
bf8d5d52 | 5441 | * @root is exclusive; it is never protected when looked at directly |
34c81057 | 5442 | * |
bf8d5d52 RG |
5443 | * To provide a proper hierarchical behavior, effective memory.min/low values |
5444 | * are used. Below is the description of how effective memory.low is calculated. | |
5445 | * Effective memory.min values is calculated in the same way. | |
34c81057 | 5446 | * |
23067153 RG |
5447 | * Effective memory.low is always equal or less than the original memory.low. |
5448 | * If there is no memory.low overcommittment (which is always true for | |
5449 | * top-level memory cgroups), these two values are equal. | |
5450 | * Otherwise, it's a part of parent's effective memory.low, | |
5451 | * calculated as a cgroup's memory.low usage divided by sum of sibling's | |
5452 | * memory.low usages, where memory.low usage is the size of actually | |
5453 | * protected memory. | |
34c81057 | 5454 | * |
23067153 RG |
5455 | * low_usage |
5456 | * elow = min( memory.low, parent->elow * ------------------ ), | |
5457 | * siblings_low_usage | |
34c81057 | 5458 | * |
23067153 RG |
5459 | * | memory.current, if memory.current < memory.low |
5460 | * low_usage = | | |
5461 | | 0, otherwise. | |
34c81057 | 5462 | * |
23067153 RG |
5463 | * |
5464 | * Such definition of the effective memory.low provides the expected | |
5465 | * hierarchical behavior: parent's memory.low value is limiting | |
5466 | * children, unprotected memory is reclaimed first and cgroups, | |
5467 | * which are not using their guarantee do not affect actual memory | |
5468 | * distribution. | |
5469 | * | |
5470 | * For example, if there are memcgs A, A/B, A/C, A/D and A/E: | |
5471 | * | |
5472 | * A A/memory.low = 2G, A/memory.current = 6G | |
5473 | * //\\ | |
5474 | * BC DE B/memory.low = 3G B/memory.current = 2G | |
5475 | * C/memory.low = 1G C/memory.current = 2G | |
5476 | * D/memory.low = 0 D/memory.current = 2G | |
5477 | * E/memory.low = 10G E/memory.current = 0 | |
5478 | * | |
5479 | * and the memory pressure is applied, the following memory distribution | |
5480 | * is expected (approximately): | |
5481 | * | |
5482 | * A/memory.current = 2G | |
5483 | * | |
5484 | * B/memory.current = 1.3G | |
5485 | * C/memory.current = 0.6G | |
5486 | * D/memory.current = 0 | |
5487 | * E/memory.current = 0 | |
5488 | * | |
5489 | * These calculations require constant tracking of the actual low usages | |
bf8d5d52 RG |
5490 | * (see propagate_protected_usage()), as well as recursive calculation of |
5491 | * effective memory.low values. But as we do call mem_cgroup_protected() | |
23067153 RG |
5492 | * path for each memory cgroup top-down from the reclaim, |
5493 | * it's possible to optimize this part, and save calculated elow | |
5494 | * for next usage. This part is intentionally racy, but it's ok, | |
5495 | * as memory.low is a best-effort mechanism. | |
241994ed | 5496 | */ |
bf8d5d52 RG |
5497 | enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root, |
5498 | struct mem_cgroup *memcg) | |
241994ed | 5499 | { |
23067153 | 5500 | struct mem_cgroup *parent; |
bf8d5d52 RG |
5501 | unsigned long emin, parent_emin; |
5502 | unsigned long elow, parent_elow; | |
5503 | unsigned long usage; | |
23067153 | 5504 | |
241994ed | 5505 | if (mem_cgroup_disabled()) |
bf8d5d52 | 5506 | return MEMCG_PROT_NONE; |
241994ed | 5507 | |
34c81057 SC |
5508 | if (!root) |
5509 | root = root_mem_cgroup; | |
5510 | if (memcg == root) | |
bf8d5d52 | 5511 | return MEMCG_PROT_NONE; |
241994ed | 5512 | |
23067153 | 5513 | usage = page_counter_read(&memcg->memory); |
bf8d5d52 RG |
5514 | if (!usage) |
5515 | return MEMCG_PROT_NONE; | |
5516 | ||
5517 | emin = memcg->memory.min; | |
5518 | elow = memcg->memory.low; | |
34c81057 | 5519 | |
bf8d5d52 | 5520 | parent = parent_mem_cgroup(memcg); |
df2a4196 RG |
5521 | /* No parent means a non-hierarchical mode on v1 memcg */ |
5522 | if (!parent) | |
5523 | return MEMCG_PROT_NONE; | |
5524 | ||
23067153 RG |
5525 | if (parent == root) |
5526 | goto exit; | |
5527 | ||
bf8d5d52 RG |
5528 | parent_emin = READ_ONCE(parent->memory.emin); |
5529 | emin = min(emin, parent_emin); | |
5530 | if (emin && parent_emin) { | |
5531 | unsigned long min_usage, siblings_min_usage; | |
5532 | ||
5533 | min_usage = min(usage, memcg->memory.min); | |
5534 | siblings_min_usage = atomic_long_read( | |
5535 | &parent->memory.children_min_usage); | |
5536 | ||
5537 | if (min_usage && siblings_min_usage) | |
5538 | emin = min(emin, parent_emin * min_usage / | |
5539 | siblings_min_usage); | |
5540 | } | |
5541 | ||
23067153 RG |
5542 | parent_elow = READ_ONCE(parent->memory.elow); |
5543 | elow = min(elow, parent_elow); | |
bf8d5d52 RG |
5544 | if (elow && parent_elow) { |
5545 | unsigned long low_usage, siblings_low_usage; | |
23067153 | 5546 | |
bf8d5d52 RG |
5547 | low_usage = min(usage, memcg->memory.low); |
5548 | siblings_low_usage = atomic_long_read( | |
5549 | &parent->memory.children_low_usage); | |
23067153 | 5550 | |
bf8d5d52 RG |
5551 | if (low_usage && siblings_low_usage) |
5552 | elow = min(elow, parent_elow * low_usage / | |
5553 | siblings_low_usage); | |
5554 | } | |
23067153 | 5555 | |
23067153 | 5556 | exit: |
bf8d5d52 | 5557 | memcg->memory.emin = emin; |
23067153 | 5558 | memcg->memory.elow = elow; |
bf8d5d52 RG |
5559 | |
5560 | if (usage <= emin) | |
5561 | return MEMCG_PROT_MIN; | |
5562 | else if (usage <= elow) | |
5563 | return MEMCG_PROT_LOW; | |
5564 | else | |
5565 | return MEMCG_PROT_NONE; | |
241994ed JW |
5566 | } |
5567 | ||
00501b53 JW |
5568 | /** |
5569 | * mem_cgroup_try_charge - try charging a page | |
5570 | * @page: page to charge | |
5571 | * @mm: mm context of the victim | |
5572 | * @gfp_mask: reclaim mode | |
5573 | * @memcgp: charged memcg return | |
25843c2b | 5574 | * @compound: charge the page as compound or small page |
00501b53 JW |
5575 | * |
5576 | * Try to charge @page to the memcg that @mm belongs to, reclaiming | |
5577 | * pages according to @gfp_mask if necessary. | |
5578 | * | |
5579 | * Returns 0 on success, with *@memcgp pointing to the charged memcg. | |
5580 | * Otherwise, an error code is returned. | |
5581 | * | |
5582 | * After page->mapping has been set up, the caller must finalize the | |
5583 | * charge with mem_cgroup_commit_charge(). Or abort the transaction | |
5584 | * with mem_cgroup_cancel_charge() in case page instantiation fails. | |
5585 | */ | |
5586 | int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm, | |
f627c2f5 KS |
5587 | gfp_t gfp_mask, struct mem_cgroup **memcgp, |
5588 | bool compound) | |
00501b53 JW |
5589 | { |
5590 | struct mem_cgroup *memcg = NULL; | |
f627c2f5 | 5591 | unsigned int nr_pages = compound ? hpage_nr_pages(page) : 1; |
00501b53 JW |
5592 | int ret = 0; |
5593 | ||
5594 | if (mem_cgroup_disabled()) | |
5595 | goto out; | |
5596 | ||
5597 | if (PageSwapCache(page)) { | |
00501b53 JW |
5598 | /* |
5599 | * Every swap fault against a single page tries to charge the | |
5600 | * page, bail as early as possible. shmem_unuse() encounters | |
5601 | * already charged pages, too. The USED bit is protected by | |
5602 | * the page lock, which serializes swap cache removal, which | |
5603 | * in turn serializes uncharging. | |
5604 | */ | |
e993d905 | 5605 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
abe2895b | 5606 | if (compound_head(page)->mem_cgroup) |
00501b53 | 5607 | goto out; |
e993d905 | 5608 | |
37e84351 | 5609 | if (do_swap_account) { |
e993d905 VD |
5610 | swp_entry_t ent = { .val = page_private(page), }; |
5611 | unsigned short id = lookup_swap_cgroup_id(ent); | |
5612 | ||
5613 | rcu_read_lock(); | |
5614 | memcg = mem_cgroup_from_id(id); | |
5615 | if (memcg && !css_tryget_online(&memcg->css)) | |
5616 | memcg = NULL; | |
5617 | rcu_read_unlock(); | |
5618 | } | |
00501b53 JW |
5619 | } |
5620 | ||
00501b53 JW |
5621 | if (!memcg) |
5622 | memcg = get_mem_cgroup_from_mm(mm); | |
5623 | ||
5624 | ret = try_charge(memcg, gfp_mask, nr_pages); | |
5625 | ||
5626 | css_put(&memcg->css); | |
00501b53 JW |
5627 | out: |
5628 | *memcgp = memcg; | |
5629 | return ret; | |
5630 | } | |
5631 | ||
2cf85583 TH |
5632 | int mem_cgroup_try_charge_delay(struct page *page, struct mm_struct *mm, |
5633 | gfp_t gfp_mask, struct mem_cgroup **memcgp, | |
5634 | bool compound) | |
5635 | { | |
5636 | struct mem_cgroup *memcg; | |
5637 | int ret; | |
5638 | ||
5639 | ret = mem_cgroup_try_charge(page, mm, gfp_mask, memcgp, compound); | |
5640 | memcg = *memcgp; | |
5641 | mem_cgroup_throttle_swaprate(memcg, page_to_nid(page), gfp_mask); | |
5642 | return ret; | |
5643 | } | |
5644 | ||
00501b53 JW |
5645 | /** |
5646 | * mem_cgroup_commit_charge - commit a page charge | |
5647 | * @page: page to charge | |
5648 | * @memcg: memcg to charge the page to | |
5649 | * @lrucare: page might be on LRU already | |
25843c2b | 5650 | * @compound: charge the page as compound or small page |
00501b53 JW |
5651 | * |
5652 | * Finalize a charge transaction started by mem_cgroup_try_charge(), | |
5653 | * after page->mapping has been set up. This must happen atomically | |
5654 | * as part of the page instantiation, i.e. under the page table lock | |
5655 | * for anonymous pages, under the page lock for page and swap cache. | |
5656 | * | |
5657 | * In addition, the page must not be on the LRU during the commit, to | |
5658 | * prevent racing with task migration. If it might be, use @lrucare. | |
5659 | * | |
5660 | * Use mem_cgroup_cancel_charge() to cancel the transaction instead. | |
5661 | */ | |
5662 | void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg, | |
f627c2f5 | 5663 | bool lrucare, bool compound) |
00501b53 | 5664 | { |
f627c2f5 | 5665 | unsigned int nr_pages = compound ? hpage_nr_pages(page) : 1; |
00501b53 JW |
5666 | |
5667 | VM_BUG_ON_PAGE(!page->mapping, page); | |
5668 | VM_BUG_ON_PAGE(PageLRU(page) && !lrucare, page); | |
5669 | ||
5670 | if (mem_cgroup_disabled()) | |
5671 | return; | |
5672 | /* | |
5673 | * Swap faults will attempt to charge the same page multiple | |
5674 | * times. But reuse_swap_page() might have removed the page | |
5675 | * from swapcache already, so we can't check PageSwapCache(). | |
5676 | */ | |
5677 | if (!memcg) | |
5678 | return; | |
5679 | ||
6abb5a86 JW |
5680 | commit_charge(page, memcg, lrucare); |
5681 | ||
6abb5a86 | 5682 | local_irq_disable(); |
f627c2f5 | 5683 | mem_cgroup_charge_statistics(memcg, page, compound, nr_pages); |
6abb5a86 JW |
5684 | memcg_check_events(memcg, page); |
5685 | local_irq_enable(); | |
00501b53 | 5686 | |
7941d214 | 5687 | if (do_memsw_account() && PageSwapCache(page)) { |
00501b53 JW |
5688 | swp_entry_t entry = { .val = page_private(page) }; |
5689 | /* | |
5690 | * The swap entry might not get freed for a long time, | |
5691 | * let's not wait for it. The page already received a | |
5692 | * memory+swap charge, drop the swap entry duplicate. | |
5693 | */ | |
38d8b4e6 | 5694 | mem_cgroup_uncharge_swap(entry, nr_pages); |
00501b53 JW |
5695 | } |
5696 | } | |
5697 | ||
5698 | /** | |
5699 | * mem_cgroup_cancel_charge - cancel a page charge | |
5700 | * @page: page to charge | |
5701 | * @memcg: memcg to charge the page to | |
25843c2b | 5702 | * @compound: charge the page as compound or small page |
00501b53 JW |
5703 | * |
5704 | * Cancel a charge transaction started by mem_cgroup_try_charge(). | |
5705 | */ | |
f627c2f5 KS |
5706 | void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg, |
5707 | bool compound) | |
00501b53 | 5708 | { |
f627c2f5 | 5709 | unsigned int nr_pages = compound ? hpage_nr_pages(page) : 1; |
00501b53 JW |
5710 | |
5711 | if (mem_cgroup_disabled()) | |
5712 | return; | |
5713 | /* | |
5714 | * Swap faults will attempt to charge the same page multiple | |
5715 | * times. But reuse_swap_page() might have removed the page | |
5716 | * from swapcache already, so we can't check PageSwapCache(). | |
5717 | */ | |
5718 | if (!memcg) | |
5719 | return; | |
5720 | ||
00501b53 JW |
5721 | cancel_charge(memcg, nr_pages); |
5722 | } | |
5723 | ||
a9d5adee JG |
5724 | struct uncharge_gather { |
5725 | struct mem_cgroup *memcg; | |
5726 | unsigned long pgpgout; | |
5727 | unsigned long nr_anon; | |
5728 | unsigned long nr_file; | |
5729 | unsigned long nr_kmem; | |
5730 | unsigned long nr_huge; | |
5731 | unsigned long nr_shmem; | |
5732 | struct page *dummy_page; | |
5733 | }; | |
5734 | ||
5735 | static inline void uncharge_gather_clear(struct uncharge_gather *ug) | |
747db954 | 5736 | { |
a9d5adee JG |
5737 | memset(ug, 0, sizeof(*ug)); |
5738 | } | |
5739 | ||
5740 | static void uncharge_batch(const struct uncharge_gather *ug) | |
5741 | { | |
5742 | unsigned long nr_pages = ug->nr_anon + ug->nr_file + ug->nr_kmem; | |
747db954 JW |
5743 | unsigned long flags; |
5744 | ||
a9d5adee JG |
5745 | if (!mem_cgroup_is_root(ug->memcg)) { |
5746 | page_counter_uncharge(&ug->memcg->memory, nr_pages); | |
7941d214 | 5747 | if (do_memsw_account()) |
a9d5adee JG |
5748 | page_counter_uncharge(&ug->memcg->memsw, nr_pages); |
5749 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && ug->nr_kmem) | |
5750 | page_counter_uncharge(&ug->memcg->kmem, ug->nr_kmem); | |
5751 | memcg_oom_recover(ug->memcg); | |
ce00a967 | 5752 | } |
747db954 JW |
5753 | |
5754 | local_irq_save(flags); | |
c9019e9b JW |
5755 | __mod_memcg_state(ug->memcg, MEMCG_RSS, -ug->nr_anon); |
5756 | __mod_memcg_state(ug->memcg, MEMCG_CACHE, -ug->nr_file); | |
5757 | __mod_memcg_state(ug->memcg, MEMCG_RSS_HUGE, -ug->nr_huge); | |
5758 | __mod_memcg_state(ug->memcg, NR_SHMEM, -ug->nr_shmem); | |
5759 | __count_memcg_events(ug->memcg, PGPGOUT, ug->pgpgout); | |
a983b5eb | 5760 | __this_cpu_add(ug->memcg->stat_cpu->nr_page_events, nr_pages); |
a9d5adee | 5761 | memcg_check_events(ug->memcg, ug->dummy_page); |
747db954 | 5762 | local_irq_restore(flags); |
e8ea14cc | 5763 | |
a9d5adee JG |
5764 | if (!mem_cgroup_is_root(ug->memcg)) |
5765 | css_put_many(&ug->memcg->css, nr_pages); | |
5766 | } | |
5767 | ||
5768 | static void uncharge_page(struct page *page, struct uncharge_gather *ug) | |
5769 | { | |
5770 | VM_BUG_ON_PAGE(PageLRU(page), page); | |
3f2eb028 JG |
5771 | VM_BUG_ON_PAGE(page_count(page) && !is_zone_device_page(page) && |
5772 | !PageHWPoison(page) , page); | |
a9d5adee JG |
5773 | |
5774 | if (!page->mem_cgroup) | |
5775 | return; | |
5776 | ||
5777 | /* | |
5778 | * Nobody should be changing or seriously looking at | |
5779 | * page->mem_cgroup at this point, we have fully | |
5780 | * exclusive access to the page. | |
5781 | */ | |
5782 | ||
5783 | if (ug->memcg != page->mem_cgroup) { | |
5784 | if (ug->memcg) { | |
5785 | uncharge_batch(ug); | |
5786 | uncharge_gather_clear(ug); | |
5787 | } | |
5788 | ug->memcg = page->mem_cgroup; | |
5789 | } | |
5790 | ||
5791 | if (!PageKmemcg(page)) { | |
5792 | unsigned int nr_pages = 1; | |
5793 | ||
5794 | if (PageTransHuge(page)) { | |
5795 | nr_pages <<= compound_order(page); | |
5796 | ug->nr_huge += nr_pages; | |
5797 | } | |
5798 | if (PageAnon(page)) | |
5799 | ug->nr_anon += nr_pages; | |
5800 | else { | |
5801 | ug->nr_file += nr_pages; | |
5802 | if (PageSwapBacked(page)) | |
5803 | ug->nr_shmem += nr_pages; | |
5804 | } | |
5805 | ug->pgpgout++; | |
5806 | } else { | |
5807 | ug->nr_kmem += 1 << compound_order(page); | |
5808 | __ClearPageKmemcg(page); | |
5809 | } | |
5810 | ||
5811 | ug->dummy_page = page; | |
5812 | page->mem_cgroup = NULL; | |
747db954 JW |
5813 | } |
5814 | ||
5815 | static void uncharge_list(struct list_head *page_list) | |
5816 | { | |
a9d5adee | 5817 | struct uncharge_gather ug; |
747db954 | 5818 | struct list_head *next; |
a9d5adee JG |
5819 | |
5820 | uncharge_gather_clear(&ug); | |
747db954 | 5821 | |
8b592656 JW |
5822 | /* |
5823 | * Note that the list can be a single page->lru; hence the | |
5824 | * do-while loop instead of a simple list_for_each_entry(). | |
5825 | */ | |
747db954 JW |
5826 | next = page_list->next; |
5827 | do { | |
a9d5adee JG |
5828 | struct page *page; |
5829 | ||
747db954 JW |
5830 | page = list_entry(next, struct page, lru); |
5831 | next = page->lru.next; | |
5832 | ||
a9d5adee | 5833 | uncharge_page(page, &ug); |
747db954 JW |
5834 | } while (next != page_list); |
5835 | ||
a9d5adee JG |
5836 | if (ug.memcg) |
5837 | uncharge_batch(&ug); | |
747db954 JW |
5838 | } |
5839 | ||
0a31bc97 JW |
5840 | /** |
5841 | * mem_cgroup_uncharge - uncharge a page | |
5842 | * @page: page to uncharge | |
5843 | * | |
5844 | * Uncharge a page previously charged with mem_cgroup_try_charge() and | |
5845 | * mem_cgroup_commit_charge(). | |
5846 | */ | |
5847 | void mem_cgroup_uncharge(struct page *page) | |
5848 | { | |
a9d5adee JG |
5849 | struct uncharge_gather ug; |
5850 | ||
0a31bc97 JW |
5851 | if (mem_cgroup_disabled()) |
5852 | return; | |
5853 | ||
747db954 | 5854 | /* Don't touch page->lru of any random page, pre-check: */ |
1306a85a | 5855 | if (!page->mem_cgroup) |
0a31bc97 JW |
5856 | return; |
5857 | ||
a9d5adee JG |
5858 | uncharge_gather_clear(&ug); |
5859 | uncharge_page(page, &ug); | |
5860 | uncharge_batch(&ug); | |
747db954 | 5861 | } |
0a31bc97 | 5862 | |
747db954 JW |
5863 | /** |
5864 | * mem_cgroup_uncharge_list - uncharge a list of page | |
5865 | * @page_list: list of pages to uncharge | |
5866 | * | |
5867 | * Uncharge a list of pages previously charged with | |
5868 | * mem_cgroup_try_charge() and mem_cgroup_commit_charge(). | |
5869 | */ | |
5870 | void mem_cgroup_uncharge_list(struct list_head *page_list) | |
5871 | { | |
5872 | if (mem_cgroup_disabled()) | |
5873 | return; | |
0a31bc97 | 5874 | |
747db954 JW |
5875 | if (!list_empty(page_list)) |
5876 | uncharge_list(page_list); | |
0a31bc97 JW |
5877 | } |
5878 | ||
5879 | /** | |
6a93ca8f JW |
5880 | * mem_cgroup_migrate - charge a page's replacement |
5881 | * @oldpage: currently circulating page | |
5882 | * @newpage: replacement page | |
0a31bc97 | 5883 | * |
6a93ca8f JW |
5884 | * Charge @newpage as a replacement page for @oldpage. @oldpage will |
5885 | * be uncharged upon free. | |
0a31bc97 JW |
5886 | * |
5887 | * Both pages must be locked, @newpage->mapping must be set up. | |
5888 | */ | |
6a93ca8f | 5889 | void mem_cgroup_migrate(struct page *oldpage, struct page *newpage) |
0a31bc97 | 5890 | { |
29833315 | 5891 | struct mem_cgroup *memcg; |
44b7a8d3 JW |
5892 | unsigned int nr_pages; |
5893 | bool compound; | |
d93c4130 | 5894 | unsigned long flags; |
0a31bc97 JW |
5895 | |
5896 | VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage); | |
5897 | VM_BUG_ON_PAGE(!PageLocked(newpage), newpage); | |
0a31bc97 | 5898 | VM_BUG_ON_PAGE(PageAnon(oldpage) != PageAnon(newpage), newpage); |
6abb5a86 JW |
5899 | VM_BUG_ON_PAGE(PageTransHuge(oldpage) != PageTransHuge(newpage), |
5900 | newpage); | |
0a31bc97 JW |
5901 | |
5902 | if (mem_cgroup_disabled()) | |
5903 | return; | |
5904 | ||
5905 | /* Page cache replacement: new page already charged? */ | |
1306a85a | 5906 | if (newpage->mem_cgroup) |
0a31bc97 JW |
5907 | return; |
5908 | ||
45637bab | 5909 | /* Swapcache readahead pages can get replaced before being charged */ |
1306a85a | 5910 | memcg = oldpage->mem_cgroup; |
29833315 | 5911 | if (!memcg) |
0a31bc97 JW |
5912 | return; |
5913 | ||
44b7a8d3 JW |
5914 | /* Force-charge the new page. The old one will be freed soon */ |
5915 | compound = PageTransHuge(newpage); | |
5916 | nr_pages = compound ? hpage_nr_pages(newpage) : 1; | |
5917 | ||
5918 | page_counter_charge(&memcg->memory, nr_pages); | |
5919 | if (do_memsw_account()) | |
5920 | page_counter_charge(&memcg->memsw, nr_pages); | |
5921 | css_get_many(&memcg->css, nr_pages); | |
0a31bc97 | 5922 | |
9cf7666a | 5923 | commit_charge(newpage, memcg, false); |
44b7a8d3 | 5924 | |
d93c4130 | 5925 | local_irq_save(flags); |
44b7a8d3 JW |
5926 | mem_cgroup_charge_statistics(memcg, newpage, compound, nr_pages); |
5927 | memcg_check_events(memcg, newpage); | |
d93c4130 | 5928 | local_irq_restore(flags); |
0a31bc97 JW |
5929 | } |
5930 | ||
ef12947c | 5931 | DEFINE_STATIC_KEY_FALSE(memcg_sockets_enabled_key); |
11092087 JW |
5932 | EXPORT_SYMBOL(memcg_sockets_enabled_key); |
5933 | ||
2d758073 | 5934 | void mem_cgroup_sk_alloc(struct sock *sk) |
11092087 JW |
5935 | { |
5936 | struct mem_cgroup *memcg; | |
5937 | ||
2d758073 JW |
5938 | if (!mem_cgroup_sockets_enabled) |
5939 | return; | |
5940 | ||
edbe69ef RG |
5941 | /* |
5942 | * Socket cloning can throw us here with sk_memcg already | |
5943 | * filled. It won't however, necessarily happen from | |
5944 | * process context. So the test for root memcg given | |
5945 | * the current task's memcg won't help us in this case. | |
5946 | * | |
5947 | * Respecting the original socket's memcg is a better | |
5948 | * decision in this case. | |
5949 | */ | |
5950 | if (sk->sk_memcg) { | |
5951 | css_get(&sk->sk_memcg->css); | |
5952 | return; | |
5953 | } | |
5954 | ||
11092087 JW |
5955 | rcu_read_lock(); |
5956 | memcg = mem_cgroup_from_task(current); | |
f7e1cb6e JW |
5957 | if (memcg == root_mem_cgroup) |
5958 | goto out; | |
0db15298 | 5959 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !memcg->tcpmem_active) |
f7e1cb6e | 5960 | goto out; |
f7e1cb6e | 5961 | if (css_tryget_online(&memcg->css)) |
11092087 | 5962 | sk->sk_memcg = memcg; |
f7e1cb6e | 5963 | out: |
11092087 JW |
5964 | rcu_read_unlock(); |
5965 | } | |
11092087 | 5966 | |
2d758073 | 5967 | void mem_cgroup_sk_free(struct sock *sk) |
11092087 | 5968 | { |
2d758073 JW |
5969 | if (sk->sk_memcg) |
5970 | css_put(&sk->sk_memcg->css); | |
11092087 JW |
5971 | } |
5972 | ||
5973 | /** | |
5974 | * mem_cgroup_charge_skmem - charge socket memory | |
5975 | * @memcg: memcg to charge | |
5976 | * @nr_pages: number of pages to charge | |
5977 | * | |
5978 | * Charges @nr_pages to @memcg. Returns %true if the charge fit within | |
5979 | * @memcg's configured limit, %false if the charge had to be forced. | |
5980 | */ | |
5981 | bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) | |
5982 | { | |
f7e1cb6e | 5983 | gfp_t gfp_mask = GFP_KERNEL; |
11092087 | 5984 | |
f7e1cb6e | 5985 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) { |
0db15298 | 5986 | struct page_counter *fail; |
f7e1cb6e | 5987 | |
0db15298 JW |
5988 | if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) { |
5989 | memcg->tcpmem_pressure = 0; | |
f7e1cb6e JW |
5990 | return true; |
5991 | } | |
0db15298 JW |
5992 | page_counter_charge(&memcg->tcpmem, nr_pages); |
5993 | memcg->tcpmem_pressure = 1; | |
f7e1cb6e | 5994 | return false; |
11092087 | 5995 | } |
d886f4e4 | 5996 | |
f7e1cb6e JW |
5997 | /* Don't block in the packet receive path */ |
5998 | if (in_softirq()) | |
5999 | gfp_mask = GFP_NOWAIT; | |
6000 | ||
c9019e9b | 6001 | mod_memcg_state(memcg, MEMCG_SOCK, nr_pages); |
b2807f07 | 6002 | |
f7e1cb6e JW |
6003 | if (try_charge(memcg, gfp_mask, nr_pages) == 0) |
6004 | return true; | |
6005 | ||
6006 | try_charge(memcg, gfp_mask|__GFP_NOFAIL, nr_pages); | |
11092087 JW |
6007 | return false; |
6008 | } | |
6009 | ||
6010 | /** | |
6011 | * mem_cgroup_uncharge_skmem - uncharge socket memory | |
b7701a5f MR |
6012 | * @memcg: memcg to uncharge |
6013 | * @nr_pages: number of pages to uncharge | |
11092087 JW |
6014 | */ |
6015 | void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) | |
6016 | { | |
f7e1cb6e | 6017 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) { |
0db15298 | 6018 | page_counter_uncharge(&memcg->tcpmem, nr_pages); |
f7e1cb6e JW |
6019 | return; |
6020 | } | |
d886f4e4 | 6021 | |
c9019e9b | 6022 | mod_memcg_state(memcg, MEMCG_SOCK, -nr_pages); |
b2807f07 | 6023 | |
475d0487 | 6024 | refill_stock(memcg, nr_pages); |
11092087 JW |
6025 | } |
6026 | ||
f7e1cb6e JW |
6027 | static int __init cgroup_memory(char *s) |
6028 | { | |
6029 | char *token; | |
6030 | ||
6031 | while ((token = strsep(&s, ",")) != NULL) { | |
6032 | if (!*token) | |
6033 | continue; | |
6034 | if (!strcmp(token, "nosocket")) | |
6035 | cgroup_memory_nosocket = true; | |
04823c83 VD |
6036 | if (!strcmp(token, "nokmem")) |
6037 | cgroup_memory_nokmem = true; | |
f7e1cb6e JW |
6038 | } |
6039 | return 0; | |
6040 | } | |
6041 | __setup("cgroup.memory=", cgroup_memory); | |
11092087 | 6042 | |
2d11085e | 6043 | /* |
1081312f MH |
6044 | * subsys_initcall() for memory controller. |
6045 | * | |
308167fc SAS |
6046 | * Some parts like memcg_hotplug_cpu_dead() have to be initialized from this |
6047 | * context because of lock dependencies (cgroup_lock -> cpu hotplug) but | |
6048 | * basically everything that doesn't depend on a specific mem_cgroup structure | |
6049 | * should be initialized from here. | |
2d11085e MH |
6050 | */ |
6051 | static int __init mem_cgroup_init(void) | |
6052 | { | |
95a045f6 JW |
6053 | int cpu, node; |
6054 | ||
13583c3d VD |
6055 | #ifndef CONFIG_SLOB |
6056 | /* | |
6057 | * Kmem cache creation is mostly done with the slab_mutex held, | |
17cc4dfe TH |
6058 | * so use a workqueue with limited concurrency to avoid stalling |
6059 | * all worker threads in case lots of cgroups are created and | |
6060 | * destroyed simultaneously. | |
13583c3d | 6061 | */ |
17cc4dfe TH |
6062 | memcg_kmem_cache_wq = alloc_workqueue("memcg_kmem_cache", 0, 1); |
6063 | BUG_ON(!memcg_kmem_cache_wq); | |
13583c3d VD |
6064 | #endif |
6065 | ||
308167fc SAS |
6066 | cpuhp_setup_state_nocalls(CPUHP_MM_MEMCQ_DEAD, "mm/memctrl:dead", NULL, |
6067 | memcg_hotplug_cpu_dead); | |
95a045f6 JW |
6068 | |
6069 | for_each_possible_cpu(cpu) | |
6070 | INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work, | |
6071 | drain_local_stock); | |
6072 | ||
6073 | for_each_node(node) { | |
6074 | struct mem_cgroup_tree_per_node *rtpn; | |
95a045f6 JW |
6075 | |
6076 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, | |
6077 | node_online(node) ? node : NUMA_NO_NODE); | |
6078 | ||
ef8f2327 | 6079 | rtpn->rb_root = RB_ROOT; |
fa90b2fd | 6080 | rtpn->rb_rightmost = NULL; |
ef8f2327 | 6081 | spin_lock_init(&rtpn->lock); |
95a045f6 JW |
6082 | soft_limit_tree.rb_tree_per_node[node] = rtpn; |
6083 | } | |
6084 | ||
2d11085e MH |
6085 | return 0; |
6086 | } | |
6087 | subsys_initcall(mem_cgroup_init); | |
21afa38e JW |
6088 | |
6089 | #ifdef CONFIG_MEMCG_SWAP | |
358c07fc AB |
6090 | static struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg) |
6091 | { | |
6092 | while (!atomic_inc_not_zero(&memcg->id.ref)) { | |
6093 | /* | |
6094 | * The root cgroup cannot be destroyed, so it's refcount must | |
6095 | * always be >= 1. | |
6096 | */ | |
6097 | if (WARN_ON_ONCE(memcg == root_mem_cgroup)) { | |
6098 | VM_BUG_ON(1); | |
6099 | break; | |
6100 | } | |
6101 | memcg = parent_mem_cgroup(memcg); | |
6102 | if (!memcg) | |
6103 | memcg = root_mem_cgroup; | |
6104 | } | |
6105 | return memcg; | |
6106 | } | |
6107 | ||
21afa38e JW |
6108 | /** |
6109 | * mem_cgroup_swapout - transfer a memsw charge to swap | |
6110 | * @page: page whose memsw charge to transfer | |
6111 | * @entry: swap entry to move the charge to | |
6112 | * | |
6113 | * Transfer the memsw charge of @page to @entry. | |
6114 | */ | |
6115 | void mem_cgroup_swapout(struct page *page, swp_entry_t entry) | |
6116 | { | |
1f47b61f | 6117 | struct mem_cgroup *memcg, *swap_memcg; |
d6810d73 | 6118 | unsigned int nr_entries; |
21afa38e JW |
6119 | unsigned short oldid; |
6120 | ||
6121 | VM_BUG_ON_PAGE(PageLRU(page), page); | |
6122 | VM_BUG_ON_PAGE(page_count(page), page); | |
6123 | ||
7941d214 | 6124 | if (!do_memsw_account()) |
21afa38e JW |
6125 | return; |
6126 | ||
6127 | memcg = page->mem_cgroup; | |
6128 | ||
6129 | /* Readahead page, never charged */ | |
6130 | if (!memcg) | |
6131 | return; | |
6132 | ||
1f47b61f VD |
6133 | /* |
6134 | * In case the memcg owning these pages has been offlined and doesn't | |
6135 | * have an ID allocated to it anymore, charge the closest online | |
6136 | * ancestor for the swap instead and transfer the memory+swap charge. | |
6137 | */ | |
6138 | swap_memcg = mem_cgroup_id_get_online(memcg); | |
d6810d73 HY |
6139 | nr_entries = hpage_nr_pages(page); |
6140 | /* Get references for the tail pages, too */ | |
6141 | if (nr_entries > 1) | |
6142 | mem_cgroup_id_get_many(swap_memcg, nr_entries - 1); | |
6143 | oldid = swap_cgroup_record(entry, mem_cgroup_id(swap_memcg), | |
6144 | nr_entries); | |
21afa38e | 6145 | VM_BUG_ON_PAGE(oldid, page); |
c9019e9b | 6146 | mod_memcg_state(swap_memcg, MEMCG_SWAP, nr_entries); |
21afa38e JW |
6147 | |
6148 | page->mem_cgroup = NULL; | |
6149 | ||
6150 | if (!mem_cgroup_is_root(memcg)) | |
d6810d73 | 6151 | page_counter_uncharge(&memcg->memory, nr_entries); |
21afa38e | 6152 | |
1f47b61f VD |
6153 | if (memcg != swap_memcg) { |
6154 | if (!mem_cgroup_is_root(swap_memcg)) | |
d6810d73 HY |
6155 | page_counter_charge(&swap_memcg->memsw, nr_entries); |
6156 | page_counter_uncharge(&memcg->memsw, nr_entries); | |
1f47b61f VD |
6157 | } |
6158 | ||
ce9ce665 SAS |
6159 | /* |
6160 | * Interrupts should be disabled here because the caller holds the | |
b93b0163 | 6161 | * i_pages lock which is taken with interrupts-off. It is |
ce9ce665 | 6162 | * important here to have the interrupts disabled because it is the |
b93b0163 | 6163 | * only synchronisation we have for updating the per-CPU variables. |
ce9ce665 SAS |
6164 | */ |
6165 | VM_BUG_ON(!irqs_disabled()); | |
d6810d73 HY |
6166 | mem_cgroup_charge_statistics(memcg, page, PageTransHuge(page), |
6167 | -nr_entries); | |
21afa38e | 6168 | memcg_check_events(memcg, page); |
73f576c0 JW |
6169 | |
6170 | if (!mem_cgroup_is_root(memcg)) | |
d08afa14 | 6171 | css_put_many(&memcg->css, nr_entries); |
21afa38e JW |
6172 | } |
6173 | ||
38d8b4e6 HY |
6174 | /** |
6175 | * mem_cgroup_try_charge_swap - try charging swap space for a page | |
37e84351 VD |
6176 | * @page: page being added to swap |
6177 | * @entry: swap entry to charge | |
6178 | * | |
38d8b4e6 | 6179 | * Try to charge @page's memcg for the swap space at @entry. |
37e84351 VD |
6180 | * |
6181 | * Returns 0 on success, -ENOMEM on failure. | |
6182 | */ | |
6183 | int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry) | |
6184 | { | |
38d8b4e6 | 6185 | unsigned int nr_pages = hpage_nr_pages(page); |
37e84351 | 6186 | struct page_counter *counter; |
38d8b4e6 | 6187 | struct mem_cgroup *memcg; |
37e84351 VD |
6188 | unsigned short oldid; |
6189 | ||
6190 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) || !do_swap_account) | |
6191 | return 0; | |
6192 | ||
6193 | memcg = page->mem_cgroup; | |
6194 | ||
6195 | /* Readahead page, never charged */ | |
6196 | if (!memcg) | |
6197 | return 0; | |
6198 | ||
f3a53a3a TH |
6199 | if (!entry.val) { |
6200 | memcg_memory_event(memcg, MEMCG_SWAP_FAIL); | |
bb98f2c5 | 6201 | return 0; |
f3a53a3a | 6202 | } |
bb98f2c5 | 6203 | |
1f47b61f VD |
6204 | memcg = mem_cgroup_id_get_online(memcg); |
6205 | ||
37e84351 | 6206 | if (!mem_cgroup_is_root(memcg) && |
38d8b4e6 | 6207 | !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) { |
f3a53a3a TH |
6208 | memcg_memory_event(memcg, MEMCG_SWAP_MAX); |
6209 | memcg_memory_event(memcg, MEMCG_SWAP_FAIL); | |
1f47b61f | 6210 | mem_cgroup_id_put(memcg); |
37e84351 | 6211 | return -ENOMEM; |
1f47b61f | 6212 | } |
37e84351 | 6213 | |
38d8b4e6 HY |
6214 | /* Get references for the tail pages, too */ |
6215 | if (nr_pages > 1) | |
6216 | mem_cgroup_id_get_many(memcg, nr_pages - 1); | |
6217 | oldid = swap_cgroup_record(entry, mem_cgroup_id(memcg), nr_pages); | |
37e84351 | 6218 | VM_BUG_ON_PAGE(oldid, page); |
c9019e9b | 6219 | mod_memcg_state(memcg, MEMCG_SWAP, nr_pages); |
37e84351 | 6220 | |
37e84351 VD |
6221 | return 0; |
6222 | } | |
6223 | ||
21afa38e | 6224 | /** |
38d8b4e6 | 6225 | * mem_cgroup_uncharge_swap - uncharge swap space |
21afa38e | 6226 | * @entry: swap entry to uncharge |
38d8b4e6 | 6227 | * @nr_pages: the amount of swap space to uncharge |
21afa38e | 6228 | */ |
38d8b4e6 | 6229 | void mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_pages) |
21afa38e JW |
6230 | { |
6231 | struct mem_cgroup *memcg; | |
6232 | unsigned short id; | |
6233 | ||
37e84351 | 6234 | if (!do_swap_account) |
21afa38e JW |
6235 | return; |
6236 | ||
38d8b4e6 | 6237 | id = swap_cgroup_record(entry, 0, nr_pages); |
21afa38e | 6238 | rcu_read_lock(); |
adbe427b | 6239 | memcg = mem_cgroup_from_id(id); |
21afa38e | 6240 | if (memcg) { |
37e84351 VD |
6241 | if (!mem_cgroup_is_root(memcg)) { |
6242 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) | |
38d8b4e6 | 6243 | page_counter_uncharge(&memcg->swap, nr_pages); |
37e84351 | 6244 | else |
38d8b4e6 | 6245 | page_counter_uncharge(&memcg->memsw, nr_pages); |
37e84351 | 6246 | } |
c9019e9b | 6247 | mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages); |
38d8b4e6 | 6248 | mem_cgroup_id_put_many(memcg, nr_pages); |
21afa38e JW |
6249 | } |
6250 | rcu_read_unlock(); | |
6251 | } | |
6252 | ||
d8b38438 VD |
6253 | long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg) |
6254 | { | |
6255 | long nr_swap_pages = get_nr_swap_pages(); | |
6256 | ||
6257 | if (!do_swap_account || !cgroup_subsys_on_dfl(memory_cgrp_subsys)) | |
6258 | return nr_swap_pages; | |
6259 | for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) | |
6260 | nr_swap_pages = min_t(long, nr_swap_pages, | |
bbec2e15 | 6261 | READ_ONCE(memcg->swap.max) - |
d8b38438 VD |
6262 | page_counter_read(&memcg->swap)); |
6263 | return nr_swap_pages; | |
6264 | } | |
6265 | ||
5ccc5aba VD |
6266 | bool mem_cgroup_swap_full(struct page *page) |
6267 | { | |
6268 | struct mem_cgroup *memcg; | |
6269 | ||
6270 | VM_BUG_ON_PAGE(!PageLocked(page), page); | |
6271 | ||
6272 | if (vm_swap_full()) | |
6273 | return true; | |
6274 | if (!do_swap_account || !cgroup_subsys_on_dfl(memory_cgrp_subsys)) | |
6275 | return false; | |
6276 | ||
6277 | memcg = page->mem_cgroup; | |
6278 | if (!memcg) | |
6279 | return false; | |
6280 | ||
6281 | for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) | |
bbec2e15 | 6282 | if (page_counter_read(&memcg->swap) * 2 >= memcg->swap.max) |
5ccc5aba VD |
6283 | return true; |
6284 | ||
6285 | return false; | |
6286 | } | |
6287 | ||
21afa38e JW |
6288 | /* for remember boot option*/ |
6289 | #ifdef CONFIG_MEMCG_SWAP_ENABLED | |
6290 | static int really_do_swap_account __initdata = 1; | |
6291 | #else | |
6292 | static int really_do_swap_account __initdata; | |
6293 | #endif | |
6294 | ||
6295 | static int __init enable_swap_account(char *s) | |
6296 | { | |
6297 | if (!strcmp(s, "1")) | |
6298 | really_do_swap_account = 1; | |
6299 | else if (!strcmp(s, "0")) | |
6300 | really_do_swap_account = 0; | |
6301 | return 1; | |
6302 | } | |
6303 | __setup("swapaccount=", enable_swap_account); | |
6304 | ||
37e84351 VD |
6305 | static u64 swap_current_read(struct cgroup_subsys_state *css, |
6306 | struct cftype *cft) | |
6307 | { | |
6308 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); | |
6309 | ||
6310 | return (u64)page_counter_read(&memcg->swap) * PAGE_SIZE; | |
6311 | } | |
6312 | ||
6313 | static int swap_max_show(struct seq_file *m, void *v) | |
6314 | { | |
6315 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); | |
bbec2e15 | 6316 | unsigned long max = READ_ONCE(memcg->swap.max); |
37e84351 VD |
6317 | |
6318 | if (max == PAGE_COUNTER_MAX) | |
6319 | seq_puts(m, "max\n"); | |
6320 | else | |
6321 | seq_printf(m, "%llu\n", (u64)max * PAGE_SIZE); | |
6322 | ||
6323 | return 0; | |
6324 | } | |
6325 | ||
6326 | static ssize_t swap_max_write(struct kernfs_open_file *of, | |
6327 | char *buf, size_t nbytes, loff_t off) | |
6328 | { | |
6329 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); | |
6330 | unsigned long max; | |
6331 | int err; | |
6332 | ||
6333 | buf = strstrip(buf); | |
6334 | err = page_counter_memparse(buf, "max", &max); | |
6335 | if (err) | |
6336 | return err; | |
6337 | ||
be09102b | 6338 | xchg(&memcg->swap.max, max); |
37e84351 VD |
6339 | |
6340 | return nbytes; | |
6341 | } | |
6342 | ||
f3a53a3a TH |
6343 | static int swap_events_show(struct seq_file *m, void *v) |
6344 | { | |
6345 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); | |
6346 | ||
6347 | seq_printf(m, "max %lu\n", | |
6348 | atomic_long_read(&memcg->memory_events[MEMCG_SWAP_MAX])); | |
6349 | seq_printf(m, "fail %lu\n", | |
6350 | atomic_long_read(&memcg->memory_events[MEMCG_SWAP_FAIL])); | |
6351 | ||
6352 | return 0; | |
6353 | } | |
6354 | ||
37e84351 VD |
6355 | static struct cftype swap_files[] = { |
6356 | { | |
6357 | .name = "swap.current", | |
6358 | .flags = CFTYPE_NOT_ON_ROOT, | |
6359 | .read_u64 = swap_current_read, | |
6360 | }, | |
6361 | { | |
6362 | .name = "swap.max", | |
6363 | .flags = CFTYPE_NOT_ON_ROOT, | |
6364 | .seq_show = swap_max_show, | |
6365 | .write = swap_max_write, | |
6366 | }, | |
f3a53a3a TH |
6367 | { |
6368 | .name = "swap.events", | |
6369 | .flags = CFTYPE_NOT_ON_ROOT, | |
6370 | .file_offset = offsetof(struct mem_cgroup, swap_events_file), | |
6371 | .seq_show = swap_events_show, | |
6372 | }, | |
37e84351 VD |
6373 | { } /* terminate */ |
6374 | }; | |
6375 | ||
21afa38e JW |
6376 | static struct cftype memsw_cgroup_files[] = { |
6377 | { | |
6378 | .name = "memsw.usage_in_bytes", | |
6379 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), | |
6380 | .read_u64 = mem_cgroup_read_u64, | |
6381 | }, | |
6382 | { | |
6383 | .name = "memsw.max_usage_in_bytes", | |
6384 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), | |
6385 | .write = mem_cgroup_reset, | |
6386 | .read_u64 = mem_cgroup_read_u64, | |
6387 | }, | |
6388 | { | |
6389 | .name = "memsw.limit_in_bytes", | |
6390 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), | |
6391 | .write = mem_cgroup_write, | |
6392 | .read_u64 = mem_cgroup_read_u64, | |
6393 | }, | |
6394 | { | |
6395 | .name = "memsw.failcnt", | |
6396 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), | |
6397 | .write = mem_cgroup_reset, | |
6398 | .read_u64 = mem_cgroup_read_u64, | |
6399 | }, | |
6400 | { }, /* terminate */ | |
6401 | }; | |
6402 | ||
6403 | static int __init mem_cgroup_swap_init(void) | |
6404 | { | |
6405 | if (!mem_cgroup_disabled() && really_do_swap_account) { | |
6406 | do_swap_account = 1; | |
37e84351 VD |
6407 | WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, |
6408 | swap_files)); | |
21afa38e JW |
6409 | WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, |
6410 | memsw_cgroup_files)); | |
6411 | } | |
6412 | return 0; | |
6413 | } | |
6414 | subsys_initcall(mem_cgroup_swap_init); | |
6415 | ||
6416 | #endif /* CONFIG_MEMCG_SWAP */ |