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