xref: /linux/mm/memcontrol-v1.c (revision 2f0f6b0773be0a1ec475097ae54848eea42adc7d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <linux/memcontrol.h>
4 #include <linux/swap.h>
5 #include <linux/mm_inline.h>
6 #include <linux/pagewalk.h>
7 #include <linux/backing-dev.h>
8 #include <linux/eventfd.h>
9 #include <linux/log2.h>
10 #include <linux/poll.h>
11 #include <linux/sort.h>
12 #include <linux/file.h>
13 #include <linux/seq_buf.h>
14 
15 #include "internal.h"
16 #include "swap.h"
17 #include "swap_table.h"
18 #include "memcontrol-v1.h"
19 
20 /*
21  * Cgroups above their limits are maintained in a RB-Tree, independent of
22  * their hierarchy representation
23  */
24 
25 struct mem_cgroup_tree_per_node {
26 	struct rb_root rb_root;
27 	struct rb_node *rb_rightmost;
28 	spinlock_t lock;
29 };
30 
31 struct mem_cgroup_tree {
32 	struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
33 };
34 
35 static struct mem_cgroup_tree soft_limit_tree __read_mostly;
36 
37 /*
38  * Maximum loops in mem_cgroup_soft_reclaim(), used for soft
39  * limit reclaim to prevent infinite loops, if they ever occur.
40  */
41 #define	MEM_CGROUP_MAX_RECLAIM_LOOPS		100
42 #define	MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS	2
43 
44 /* for OOM */
45 struct mem_cgroup_eventfd_list {
46 	struct list_head list;
47 	struct eventfd_ctx *eventfd;
48 };
49 
50 /*
51  * cgroup_event represents events which userspace want to receive.
52  */
53 struct mem_cgroup_event {
54 	/*
55 	 * memcg which the event belongs to.
56 	 */
57 	struct mem_cgroup *memcg;
58 	/*
59 	 * eventfd to signal userspace about the event.
60 	 */
61 	struct eventfd_ctx *eventfd;
62 	/*
63 	 * Each of these stored in a list by the cgroup.
64 	 */
65 	struct list_head list;
66 	/*
67 	 * register_event() callback will be used to add new userspace
68 	 * waiter for changes related to this event.  Use eventfd_signal()
69 	 * on eventfd to send notification to userspace.
70 	 */
71 	int (*register_event)(struct mem_cgroup *memcg,
72 			      struct eventfd_ctx *eventfd, const char *args);
73 	/*
74 	 * unregister_event() callback will be called when userspace closes
75 	 * the eventfd or on cgroup removing.  This callback must be set,
76 	 * if you want provide notification functionality.
77 	 */
78 	void (*unregister_event)(struct mem_cgroup *memcg,
79 				 struct eventfd_ctx *eventfd);
80 	/*
81 	 * All fields below needed to unregister event when
82 	 * userspace closes eventfd.
83 	 */
84 	poll_table pt;
85 	wait_queue_head_t *wqh;
86 	wait_queue_entry_t wait;
87 	struct work_struct remove;
88 };
89 
90 #define MEMFILE_PRIVATE(x, val)	((x) << 16 | (val))
91 #define MEMFILE_TYPE(val)	((val) >> 16 & 0xffff)
92 #define MEMFILE_ATTR(val)	((val) & 0xffff)
93 
94 enum {
95 	RES_USAGE,
96 	RES_LIMIT,
97 	RES_MAX_USAGE,
98 	RES_FAILCNT,
99 	RES_SOFT_LIMIT,
100 };
101 
102 #ifdef CONFIG_LOCKDEP
103 static struct lockdep_map memcg_oom_lock_dep_map = {
104 	.name = "memcg_oom_lock",
105 };
106 #endif
107 
108 DEFINE_SPINLOCK(memcg_oom_lock);
109 
110 static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_node *mz,
111 					 struct mem_cgroup_tree_per_node *mctz,
112 					 unsigned long new_usage_in_excess)
113 {
114 	struct rb_node **p = &mctz->rb_root.rb_node;
115 	struct rb_node *parent = NULL;
116 	struct mem_cgroup_per_node *mz_node;
117 	bool rightmost = true;
118 
119 	if (mz->on_tree)
120 		return;
121 
122 	mz->usage_in_excess = new_usage_in_excess;
123 	if (!mz->usage_in_excess)
124 		return;
125 	while (*p) {
126 		parent = *p;
127 		mz_node = rb_entry(parent, struct mem_cgroup_per_node,
128 					tree_node);
129 		if (mz->usage_in_excess < mz_node->usage_in_excess) {
130 			p = &(*p)->rb_left;
131 			rightmost = false;
132 		} else {
133 			p = &(*p)->rb_right;
134 		}
135 	}
136 
137 	if (rightmost)
138 		mctz->rb_rightmost = &mz->tree_node;
139 
140 	rb_link_node(&mz->tree_node, parent, p);
141 	rb_insert_color(&mz->tree_node, &mctz->rb_root);
142 	mz->on_tree = true;
143 }
144 
145 static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz,
146 					 struct mem_cgroup_tree_per_node *mctz)
147 {
148 	if (!mz->on_tree)
149 		return;
150 
151 	if (&mz->tree_node == mctz->rb_rightmost)
152 		mctz->rb_rightmost = rb_prev(&mz->tree_node);
153 
154 	rb_erase(&mz->tree_node, &mctz->rb_root);
155 	mz->on_tree = false;
156 }
157 
158 static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz,
159 				       struct mem_cgroup_tree_per_node *mctz)
160 {
161 	unsigned long flags;
162 
163 	spin_lock_irqsave(&mctz->lock, flags);
164 	__mem_cgroup_remove_exceeded(mz, mctz);
165 	spin_unlock_irqrestore(&mctz->lock, flags);
166 }
167 
168 static unsigned long soft_limit_excess(struct mem_cgroup *memcg)
169 {
170 	unsigned long nr_pages = page_counter_read(&memcg->memory);
171 	unsigned long soft_limit = READ_ONCE(memcg->soft_limit);
172 	unsigned long excess = 0;
173 
174 	if (nr_pages > soft_limit)
175 		excess = nr_pages - soft_limit;
176 
177 	return excess;
178 }
179 
180 static void memcg1_update_tree(struct mem_cgroup *memcg, int nid)
181 {
182 	unsigned long excess;
183 	struct mem_cgroup_per_node *mz;
184 	struct mem_cgroup_tree_per_node *mctz;
185 
186 	if (lru_gen_enabled()) {
187 		if (soft_limit_excess(memcg))
188 			lru_gen_soft_reclaim(memcg, nid);
189 		return;
190 	}
191 
192 	mctz = soft_limit_tree.rb_tree_per_node[nid];
193 	if (!mctz)
194 		return;
195 	/*
196 	 * Necessary to update all ancestors when hierarchy is used.
197 	 * because their event counter is not touched.
198 	 */
199 	for (; memcg; memcg = parent_mem_cgroup(memcg)) {
200 		mz = memcg->nodeinfo[nid];
201 		excess = soft_limit_excess(memcg);
202 		/*
203 		 * We have to update the tree if mz is on RB-tree or
204 		 * mem is over its softlimit.
205 		 */
206 		if (excess || mz->on_tree) {
207 			unsigned long flags;
208 
209 			spin_lock_irqsave(&mctz->lock, flags);
210 			/* if on-tree, remove it */
211 			if (mz->on_tree)
212 				__mem_cgroup_remove_exceeded(mz, mctz);
213 			/*
214 			 * Insert again. mz->usage_in_excess will be updated.
215 			 * If excess is 0, no tree ops.
216 			 */
217 			__mem_cgroup_insert_exceeded(mz, mctz, excess);
218 			spin_unlock_irqrestore(&mctz->lock, flags);
219 		}
220 	}
221 }
222 
223 void memcg1_remove_from_trees(struct mem_cgroup *memcg)
224 {
225 	struct mem_cgroup_tree_per_node *mctz;
226 	struct mem_cgroup_per_node *mz;
227 	int nid;
228 
229 	for_each_node(nid) {
230 		mz = memcg->nodeinfo[nid];
231 		mctz = soft_limit_tree.rb_tree_per_node[nid];
232 		if (mctz)
233 			mem_cgroup_remove_exceeded(mz, mctz);
234 	}
235 }
236 
237 static struct mem_cgroup_per_node *
238 __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
239 {
240 	struct mem_cgroup_per_node *mz;
241 
242 retry:
243 	mz = NULL;
244 	if (!mctz->rb_rightmost)
245 		goto done;		/* Nothing to reclaim from */
246 
247 	mz = rb_entry(mctz->rb_rightmost,
248 		      struct mem_cgroup_per_node, tree_node);
249 	/*
250 	 * Remove the node now but someone else can add it back,
251 	 * we will to add it back at the end of reclaim to its correct
252 	 * position in the tree.
253 	 */
254 	__mem_cgroup_remove_exceeded(mz, mctz);
255 	if (!soft_limit_excess(mz->memcg) ||
256 	    !css_tryget(&mz->memcg->css))
257 		goto retry;
258 done:
259 	return mz;
260 }
261 
262 static struct mem_cgroup_per_node *
263 mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
264 {
265 	struct mem_cgroup_per_node *mz;
266 
267 	spin_lock_irq(&mctz->lock);
268 	mz = __mem_cgroup_largest_soft_limit_node(mctz);
269 	spin_unlock_irq(&mctz->lock);
270 	return mz;
271 }
272 
273 static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
274 				   pg_data_t *pgdat,
275 				   gfp_t gfp_mask,
276 				   unsigned long *total_scanned)
277 {
278 	struct mem_cgroup *victim = NULL;
279 	int total = 0;
280 	int loop = 0;
281 	unsigned long excess;
282 	unsigned long nr_scanned;
283 	struct mem_cgroup_reclaim_cookie reclaim = {
284 		.pgdat = pgdat,
285 	};
286 
287 	excess = soft_limit_excess(root_memcg);
288 
289 	while (1) {
290 		victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
291 		if (!victim) {
292 			loop++;
293 			if (loop >= 2) {
294 				/*
295 				 * If we have not been able to reclaim
296 				 * anything, it might because there are
297 				 * no reclaimable pages under this hierarchy
298 				 */
299 				if (!total)
300 					break;
301 				/*
302 				 * We want to do more targeted reclaim.
303 				 * excess >> 2 is not to excessive so as to
304 				 * reclaim too much, nor too less that we keep
305 				 * coming back to reclaim from this cgroup
306 				 */
307 				if (total >= (excess >> 2) ||
308 					(loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
309 					break;
310 			}
311 			continue;
312 		}
313 		total += mem_cgroup_shrink_node(victim, gfp_mask, false,
314 					pgdat, &nr_scanned);
315 		*total_scanned += nr_scanned;
316 		if (!soft_limit_excess(root_memcg))
317 			break;
318 	}
319 	mem_cgroup_iter_break(root_memcg, victim);
320 	return total;
321 }
322 
323 unsigned long memcg1_soft_limit_reclaim(pg_data_t *pgdat, int order,
324 					    gfp_t gfp_mask,
325 					    unsigned long *total_scanned)
326 {
327 	unsigned long nr_reclaimed = 0;
328 	struct mem_cgroup_per_node *mz, *next_mz = NULL;
329 	unsigned long reclaimed;
330 	int loop = 0;
331 	struct mem_cgroup_tree_per_node *mctz;
332 	unsigned long excess;
333 
334 	if (lru_gen_enabled())
335 		return 0;
336 
337 	if (order > 0)
338 		return 0;
339 
340 	mctz = soft_limit_tree.rb_tree_per_node[pgdat->node_id];
341 
342 	/*
343 	 * Do not even bother to check the largest node if the root
344 	 * is empty. Do it lockless to prevent lock bouncing. Races
345 	 * are acceptable as soft limit is best effort anyway.
346 	 */
347 	if (!mctz || RB_EMPTY_ROOT(&mctz->rb_root))
348 		return 0;
349 
350 	/*
351 	 * This loop can run a while, specially if mem_cgroup's continuously
352 	 * keep exceeding their soft limit and putting the system under
353 	 * pressure
354 	 */
355 	do {
356 		if (next_mz)
357 			mz = next_mz;
358 		else
359 			mz = mem_cgroup_largest_soft_limit_node(mctz);
360 		if (!mz)
361 			break;
362 
363 		reclaimed = mem_cgroup_soft_reclaim(mz->memcg, pgdat,
364 						    gfp_mask, total_scanned);
365 		nr_reclaimed += reclaimed;
366 		spin_lock_irq(&mctz->lock);
367 
368 		/*
369 		 * If we failed to reclaim anything from this memory cgroup
370 		 * it is time to move on to the next cgroup
371 		 */
372 		next_mz = NULL;
373 		if (!reclaimed)
374 			next_mz = __mem_cgroup_largest_soft_limit_node(mctz);
375 
376 		excess = soft_limit_excess(mz->memcg);
377 		/*
378 		 * One school of thought says that we should not add
379 		 * back the node to the tree if reclaim returns 0.
380 		 * But our reclaim could return 0, simply because due
381 		 * to priority we are exposing a smaller subset of
382 		 * memory to reclaim from. Consider this as a longer
383 		 * term TODO.
384 		 */
385 		/* If excess == 0, no tree ops */
386 		__mem_cgroup_insert_exceeded(mz, mctz, excess);
387 		spin_unlock_irq(&mctz->lock);
388 		css_put(&mz->memcg->css);
389 		loop++;
390 		/*
391 		 * Could not reclaim anything and there are no more
392 		 * mem cgroups to try or we seem to be looping without
393 		 * reclaiming anything.
394 		 */
395 		if (!nr_reclaimed &&
396 			(next_mz == NULL ||
397 			loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
398 			break;
399 	} while (!nr_reclaimed);
400 	if (next_mz)
401 		css_put(&next_mz->memcg->css);
402 	return nr_reclaimed;
403 }
404 
405 static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
406 				struct cftype *cft)
407 {
408 	return 0;
409 }
410 
411 #ifdef CONFIG_MMU
412 static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
413 				 struct cftype *cft, u64 val)
414 {
415 	pr_warn_once("Cgroup memory moving (move_charge_at_immigrate) is deprecated. "
416 		     "Please report your usecase to linux-mm@kvack.org if you "
417 		     "depend on this functionality.\n");
418 
419 	if (val != 0)
420 		return -EINVAL;
421 	return 0;
422 }
423 #else
424 static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
425 				 struct cftype *cft, u64 val)
426 {
427 	return -ENOSYS;
428 }
429 #endif
430 
431 static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
432 {
433 	unsigned long val;
434 
435 	if (mem_cgroup_is_root(memcg)) {
436 		/*
437 		 * Approximate root's usage from global state. This isn't
438 		 * perfect, but the root usage was always an approximation.
439 		 */
440 		val = global_node_page_state(NR_FILE_PAGES) +
441 			global_node_page_state(NR_ANON_MAPPED);
442 		if (swap)
443 			val += total_swap_pages - get_nr_swap_pages();
444 	} else {
445 		if (!swap)
446 			val = page_counter_read(&memcg->memory);
447 		else
448 			val = page_counter_read(&memcg->memsw);
449 	}
450 	return val;
451 }
452 
453 static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
454 {
455 	struct mem_cgroup_threshold_ary *t;
456 	unsigned long usage;
457 	int i;
458 
459 	rcu_read_lock();
460 	if (!swap)
461 		t = rcu_dereference(memcg->thresholds.primary);
462 	else
463 		t = rcu_dereference(memcg->memsw_thresholds.primary);
464 
465 	if (!t)
466 		goto unlock;
467 
468 	usage = mem_cgroup_usage(memcg, swap);
469 
470 	/*
471 	 * current_threshold points to threshold just below or equal to usage.
472 	 * If it's not true, a threshold was crossed after last
473 	 * call of __mem_cgroup_threshold().
474 	 */
475 	i = t->current_threshold;
476 
477 	/*
478 	 * Iterate backward over array of thresholds starting from
479 	 * current_threshold and check if a threshold is crossed.
480 	 * If none of thresholds below usage is crossed, we read
481 	 * only one element of the array here.
482 	 */
483 	for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
484 		eventfd_signal(t->entries[i].eventfd);
485 
486 	/* i = current_threshold + 1 */
487 	i++;
488 
489 	/*
490 	 * Iterate forward over array of thresholds starting from
491 	 * current_threshold+1 and check if a threshold is crossed.
492 	 * If none of thresholds above usage is crossed, we read
493 	 * only one element of the array here.
494 	 */
495 	for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
496 		eventfd_signal(t->entries[i].eventfd);
497 
498 	/* Update current_threshold */
499 	t->current_threshold = i - 1;
500 unlock:
501 	rcu_read_unlock();
502 }
503 
504 static void mem_cgroup_threshold(struct mem_cgroup *memcg)
505 {
506 	while (memcg) {
507 		__mem_cgroup_threshold(memcg, false);
508 		if (do_memsw_account())
509 			__mem_cgroup_threshold(memcg, true);
510 
511 		memcg = parent_mem_cgroup(memcg);
512 	}
513 }
514 
515 /* Cgroup1: threshold notifications & softlimit tree updates */
516 
517 /*
518  * Per memcg event counter is incremented at every pagein/pageout. With THP,
519  * it will be incremented by the number of pages. This counter is used
520  * to trigger some periodic events. This is straightforward and better
521  * than using jiffies etc. to handle periodic memcg event.
522  */
523 enum mem_cgroup_events_target {
524 	MEM_CGROUP_TARGET_THRESH,
525 	MEM_CGROUP_TARGET_SOFTLIMIT,
526 	MEM_CGROUP_NTARGETS,
527 };
528 
529 struct memcg1_events_percpu {
530 	unsigned long nr_page_events;
531 	unsigned long targets[MEM_CGROUP_NTARGETS];
532 };
533 
534 static void memcg1_charge_statistics(struct mem_cgroup *memcg, int nr_pages)
535 {
536 	/* pagein of a big page is an event. So, ignore page size */
537 	if (nr_pages > 0)
538 		count_memcg_events(memcg, PGPGIN, 1);
539 	else {
540 		count_memcg_events(memcg, PGPGOUT, 1);
541 		nr_pages = -nr_pages; /* for event */
542 	}
543 
544 	__this_cpu_add(memcg->events_percpu->nr_page_events, nr_pages);
545 }
546 
547 #define THRESHOLDS_EVENTS_TARGET 128
548 #define SOFTLIMIT_EVENTS_TARGET 1024
549 
550 static bool memcg1_event_ratelimit(struct mem_cgroup *memcg,
551 				enum mem_cgroup_events_target target)
552 {
553 	unsigned long val, next;
554 
555 	val = __this_cpu_read(memcg->events_percpu->nr_page_events);
556 	next = __this_cpu_read(memcg->events_percpu->targets[target]);
557 	/* from time_after() in jiffies.h */
558 	if ((long)(next - val) < 0) {
559 		switch (target) {
560 		case MEM_CGROUP_TARGET_THRESH:
561 			next = val + THRESHOLDS_EVENTS_TARGET;
562 			break;
563 		case MEM_CGROUP_TARGET_SOFTLIMIT:
564 			next = val + SOFTLIMIT_EVENTS_TARGET;
565 			break;
566 		default:
567 			break;
568 		}
569 		__this_cpu_write(memcg->events_percpu->targets[target], next);
570 		return true;
571 	}
572 	return false;
573 }
574 
575 /*
576  * Check events in order.
577  *
578  */
579 static void memcg1_check_events(struct mem_cgroup *memcg, int nid)
580 {
581 	if (IS_ENABLED(CONFIG_PREEMPT_RT))
582 		return;
583 
584 	/* threshold event is triggered in finer grain than soft limit */
585 	if (unlikely(memcg1_event_ratelimit(memcg,
586 						MEM_CGROUP_TARGET_THRESH))) {
587 		bool do_softlimit;
588 
589 		do_softlimit = memcg1_event_ratelimit(memcg,
590 						MEM_CGROUP_TARGET_SOFTLIMIT);
591 		mem_cgroup_threshold(memcg);
592 		if (unlikely(do_softlimit))
593 			memcg1_update_tree(memcg, nid);
594 	}
595 }
596 
597 void memcg1_commit_charge(struct folio *folio, struct mem_cgroup *memcg)
598 {
599 	unsigned long flags;
600 
601 	local_irq_save(flags);
602 	memcg1_charge_statistics(memcg, folio_nr_pages(folio));
603 	memcg1_check_events(memcg, folio_nid(folio));
604 	local_irq_restore(flags);
605 }
606 
607 #ifdef CONFIG_SWAP
608 /**
609  * __memcg1_swapout - transfer a memsw charge to swap
610  * @folio: folio whose memsw charge to transfer
611  * @ci: the locked swap cluster holding the swap entries
612  *
613  * Transfer the memsw charge of @folio to the swap entry stored in
614  * folio->swap.
615  *
616  * Context: folio must be isolated, unmapped, locked and is just about to
617  * be freed, and caller must disable IRQs and hold the swap cluster lock.
618  */
619 void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci)
620 {
621 	struct mem_cgroup *memcg, *swap_memcg;
622 	struct obj_cgroup *objcg;
623 	unsigned int nr_entries;
624 
625 	VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio);
626 	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
627 	VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
628 	VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
629 
630 	if (mem_cgroup_disabled())
631 		return;
632 
633 	if (!do_memsw_account())
634 		return;
635 
636 	objcg = folio_objcg(folio);
637 	VM_WARN_ON_ONCE_FOLIO(!objcg, folio);
638 	if (!objcg)
639 		return;
640 
641 	rcu_read_lock();
642 	memcg = obj_cgroup_memcg(objcg);
643 	/*
644 	 * In case the memcg owning these pages has been offlined and doesn't
645 	 * have an ID allocated to it anymore, charge the closest online
646 	 * ancestor for the swap instead and transfer the memory+swap charge.
647 	 */
648 	nr_entries = folio_nr_pages(folio);
649 	swap_memcg = mem_cgroup_private_id_get_online(memcg, nr_entries);
650 	mod_memcg_state(swap_memcg, MEMCG_SWAP, nr_entries);
651 
652 	__swap_cgroup_set(ci, swp_cluster_offset(folio->swap), nr_entries,
653 			  mem_cgroup_private_id(swap_memcg));
654 
655 	folio_unqueue_deferred_split(folio);
656 	folio->memcg_data = 0;
657 
658 	if (!obj_cgroup_is_root(objcg))
659 		page_counter_uncharge(&memcg->memory, nr_entries);
660 
661 	if (memcg != swap_memcg) {
662 		if (!mem_cgroup_is_root(swap_memcg))
663 			page_counter_charge(&swap_memcg->memsw, nr_entries);
664 		page_counter_uncharge(&memcg->memsw, nr_entries);
665 	}
666 
667 	/*
668 	 * The caller must hold the swap cluster lock with IRQ off. It is
669 	 * important here to have the interrupts disabled because it is the
670 	 * only synchronisation we have for updating the per-CPU variables.
671 	 */
672 	preempt_disable_nested();
673 	VM_WARN_ON_IRQS_ENABLED();
674 	memcg1_charge_statistics(memcg, -folio_nr_pages(folio));
675 	preempt_enable_nested();
676 	memcg1_check_events(memcg, folio_nid(folio));
677 
678 	rcu_read_unlock();
679 	obj_cgroup_put(objcg);
680 }
681 
682 /**
683  * memcg1_swapin - uncharge swap slot on swapin
684  * @folio: folio being swapped in
685  *
686  * Call this function after successfully adding the charged
687  * folio to swapcache.
688  *
689  * Context: The folio has to be in swap cache and locked.
690  */
691 void memcg1_swapin(struct folio *folio)
692 {
693 	struct swap_cluster_info *ci;
694 	unsigned long nr_pages;
695 	unsigned short id;
696 
697 	VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio);
698 	VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
699 
700 	/*
701 	 * Cgroup1's unified memory+swap counter has been charged with the
702 	 * new swapcache page, finish the transfer by uncharging the swap
703 	 * slot. The swap slot would also get uncharged when it dies, but
704 	 * it can stick around indefinitely and we'd count the page twice
705 	 * the entire time.
706 	 *
707 	 * Cgroup2 has separate resource counters for memory and swap,
708 	 * so this is a non-issue here. Memory and swap charge lifetimes
709 	 * correspond 1:1 to page and swap slot lifetimes: we charge the
710 	 * page to memory here, and uncharge swap when the slot is freed.
711 	 */
712 	if (!do_memsw_account())
713 		return;
714 
715 	/*
716 	 * The swap entry might not get freed for a long time,
717 	 * let's not wait for it.  The page already received a
718 	 * memory+swap charge, drop the swap entry duplicate.
719 	 */
720 	nr_pages = folio_nr_pages(folio);
721 	ci = swap_cluster_get_and_lock(folio);
722 	id = __swap_cgroup_clear(ci, swp_cluster_offset(folio->swap),
723 				 nr_pages);
724 	swap_cluster_unlock(ci);
725 	mem_cgroup_uncharge_swap(id, nr_pages);
726 }
727 #endif
728 
729 void memcg1_uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
730 			   unsigned long nr_memory, int nid)
731 {
732 	unsigned long flags;
733 
734 	local_irq_save(flags);
735 	count_memcg_events(memcg, PGPGOUT, pgpgout);
736 	__this_cpu_add(memcg->events_percpu->nr_page_events, nr_memory);
737 	memcg1_check_events(memcg, nid);
738 	local_irq_restore(flags);
739 }
740 
741 static int compare_thresholds(const void *a, const void *b)
742 {
743 	const struct mem_cgroup_threshold *_a = a;
744 	const struct mem_cgroup_threshold *_b = b;
745 
746 	if (_a->threshold > _b->threshold)
747 		return 1;
748 
749 	if (_a->threshold < _b->threshold)
750 		return -1;
751 
752 	return 0;
753 }
754 
755 static void mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
756 {
757 	struct mem_cgroup_eventfd_list *ev;
758 
759 	spin_lock(&memcg_oom_lock);
760 
761 	list_for_each_entry(ev, &memcg->oom_notify, list)
762 		eventfd_signal(ev->eventfd);
763 
764 	spin_unlock(&memcg_oom_lock);
765 }
766 
767 static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
768 {
769 	struct mem_cgroup *iter;
770 
771 	for_each_mem_cgroup_tree(iter, memcg)
772 		mem_cgroup_oom_notify_cb(iter);
773 }
774 
775 static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
776 	struct eventfd_ctx *eventfd, const char *args, enum res_type type)
777 {
778 	struct mem_cgroup_thresholds *thresholds;
779 	struct mem_cgroup_threshold_ary *new;
780 	unsigned long threshold;
781 	unsigned long usage;
782 	int i, size, ret;
783 
784 	ret = page_counter_memparse(args, "-1", &threshold);
785 	if (ret)
786 		return ret;
787 
788 	mutex_lock(&memcg->thresholds_lock);
789 
790 	if (type == _MEM) {
791 		thresholds = &memcg->thresholds;
792 		usage = mem_cgroup_usage(memcg, false);
793 	} else if (type == _MEMSWAP) {
794 		thresholds = &memcg->memsw_thresholds;
795 		usage = mem_cgroup_usage(memcg, true);
796 	} else
797 		BUG();
798 
799 	/* Check if a threshold crossed before adding a new one */
800 	if (thresholds->primary)
801 		__mem_cgroup_threshold(memcg, type == _MEMSWAP);
802 
803 	size = thresholds->primary ? thresholds->primary->size + 1 : 1;
804 
805 	/* Allocate memory for new array of thresholds */
806 	new = kmalloc_flex(*new, entries, size, GFP_KERNEL_ACCOUNT);
807 	if (!new) {
808 		ret = -ENOMEM;
809 		goto unlock;
810 	}
811 	new->size = size;
812 
813 	/* Copy thresholds (if any) to new array */
814 	if (thresholds->primary)
815 		memcpy(new->entries, thresholds->primary->entries,
816 		       flex_array_size(new, entries, size - 1));
817 
818 	/* Add new threshold */
819 	new->entries[size - 1].eventfd = eventfd;
820 	new->entries[size - 1].threshold = threshold;
821 
822 	/* Sort thresholds. Registering of new threshold isn't time-critical */
823 	sort(new->entries, size, sizeof(*new->entries),
824 			compare_thresholds, NULL);
825 
826 	/* Find current threshold */
827 	new->current_threshold = -1;
828 	for (i = 0; i < size; i++) {
829 		if (new->entries[i].threshold <= usage) {
830 			/*
831 			 * new->current_threshold will not be used until
832 			 * rcu_assign_pointer(), so it's safe to increment
833 			 * it here.
834 			 */
835 			++new->current_threshold;
836 		} else
837 			break;
838 	}
839 
840 	/* Free old spare buffer and save old primary buffer as spare */
841 	kfree(thresholds->spare);
842 	thresholds->spare = thresholds->primary;
843 
844 	rcu_assign_pointer(thresholds->primary, new);
845 
846 	/* To be sure that nobody uses thresholds */
847 	synchronize_rcu();
848 
849 unlock:
850 	mutex_unlock(&memcg->thresholds_lock);
851 
852 	return ret;
853 }
854 
855 static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
856 	struct eventfd_ctx *eventfd, const char *args)
857 {
858 	return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
859 }
860 
861 static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
862 	struct eventfd_ctx *eventfd, const char *args)
863 {
864 	return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
865 }
866 
867 static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
868 	struct eventfd_ctx *eventfd, enum res_type type)
869 {
870 	struct mem_cgroup_thresholds *thresholds;
871 	struct mem_cgroup_threshold_ary *new;
872 	unsigned long usage;
873 	int i, j, size, entries;
874 
875 	mutex_lock(&memcg->thresholds_lock);
876 
877 	if (type == _MEM) {
878 		thresholds = &memcg->thresholds;
879 		usage = mem_cgroup_usage(memcg, false);
880 	} else if (type == _MEMSWAP) {
881 		thresholds = &memcg->memsw_thresholds;
882 		usage = mem_cgroup_usage(memcg, true);
883 	} else
884 		BUG();
885 
886 	if (!thresholds->primary)
887 		goto unlock;
888 
889 	/* Check if a threshold crossed before removing */
890 	__mem_cgroup_threshold(memcg, type == _MEMSWAP);
891 
892 	/* Calculate new number of threshold */
893 	size = entries = 0;
894 	for (i = 0; i < thresholds->primary->size; i++) {
895 		if (thresholds->primary->entries[i].eventfd != eventfd)
896 			size++;
897 		else
898 			entries++;
899 	}
900 
901 	new = thresholds->spare;
902 
903 	/* If no items related to eventfd have been cleared, nothing to do */
904 	if (!entries)
905 		goto unlock;
906 
907 	/* Set thresholds array to NULL if we don't have thresholds */
908 	if (!size) {
909 		kfree(new);
910 		new = NULL;
911 		goto swap_buffers;
912 	}
913 
914 	new->size = size;
915 
916 	/* Copy thresholds and find current threshold */
917 	new->current_threshold = -1;
918 	for (i = 0, j = 0; i < thresholds->primary->size; i++) {
919 		if (thresholds->primary->entries[i].eventfd == eventfd)
920 			continue;
921 
922 		new->entries[j] = thresholds->primary->entries[i];
923 		if (new->entries[j].threshold <= usage) {
924 			/*
925 			 * new->current_threshold will not be used
926 			 * until rcu_assign_pointer(), so it's safe to increment
927 			 * it here.
928 			 */
929 			++new->current_threshold;
930 		}
931 		j++;
932 	}
933 
934 swap_buffers:
935 	/* Swap primary and spare array */
936 	thresholds->spare = thresholds->primary;
937 
938 	rcu_assign_pointer(thresholds->primary, new);
939 
940 	/* To be sure that nobody uses thresholds */
941 	synchronize_rcu();
942 
943 	/* If all events are unregistered, free the spare array */
944 	if (!new) {
945 		kfree(thresholds->spare);
946 		thresholds->spare = NULL;
947 	}
948 unlock:
949 	mutex_unlock(&memcg->thresholds_lock);
950 }
951 
952 static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
953 	struct eventfd_ctx *eventfd)
954 {
955 	return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
956 }
957 
958 static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
959 	struct eventfd_ctx *eventfd)
960 {
961 	return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
962 }
963 
964 static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
965 	struct eventfd_ctx *eventfd, const char *args)
966 {
967 	struct mem_cgroup_eventfd_list *event;
968 
969 	event = kmalloc_obj(*event, GFP_KERNEL_ACCOUNT);
970 	if (!event)
971 		return -ENOMEM;
972 
973 	spin_lock(&memcg_oom_lock);
974 
975 	event->eventfd = eventfd;
976 	list_add(&event->list, &memcg->oom_notify);
977 
978 	/* already in OOM ? */
979 	if (memcg->under_oom)
980 		eventfd_signal(eventfd);
981 	spin_unlock(&memcg_oom_lock);
982 
983 	return 0;
984 }
985 
986 static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg,
987 	struct eventfd_ctx *eventfd)
988 {
989 	struct mem_cgroup_eventfd_list *ev, *tmp;
990 
991 	spin_lock(&memcg_oom_lock);
992 
993 	list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
994 		if (ev->eventfd == eventfd) {
995 			list_del(&ev->list);
996 			kfree(ev);
997 		}
998 	}
999 
1000 	spin_unlock(&memcg_oom_lock);
1001 }
1002 
1003 /*
1004  * DO NOT USE IN NEW FILES.
1005  *
1006  * "cgroup.event_control" implementation.
1007  *
1008  * This is way over-engineered.  It tries to support fully configurable
1009  * events for each user.  Such level of flexibility is completely
1010  * unnecessary especially in the light of the planned unified hierarchy.
1011  *
1012  * Please deprecate this and replace with something simpler if at all
1013  * possible.
1014  */
1015 
1016 /*
1017  * Unregister event and free resources.
1018  *
1019  * Gets called from workqueue.
1020  */
1021 static void memcg_event_remove(struct work_struct *work)
1022 {
1023 	struct mem_cgroup_event *event =
1024 		container_of(work, struct mem_cgroup_event, remove);
1025 	struct mem_cgroup *memcg = event->memcg;
1026 
1027 	remove_wait_queue(event->wqh, &event->wait);
1028 
1029 	event->unregister_event(memcg, event->eventfd);
1030 
1031 	/* Notify userspace the event is going away. */
1032 	eventfd_signal(event->eventfd);
1033 
1034 	eventfd_ctx_put(event->eventfd);
1035 	kfree(event);
1036 	css_put(&memcg->css);
1037 }
1038 
1039 /*
1040  * Gets called on EPOLLHUP on eventfd when user closes it.
1041  *
1042  * Called with wqh->lock held and interrupts disabled.
1043  */
1044 static int memcg_event_wake(wait_queue_entry_t *wait, unsigned int mode,
1045 			    int sync, void *key)
1046 {
1047 	struct mem_cgroup_event *event =
1048 		container_of(wait, struct mem_cgroup_event, wait);
1049 	struct mem_cgroup *memcg = event->memcg;
1050 	__poll_t flags = key_to_poll(key);
1051 
1052 	if (flags & EPOLLHUP) {
1053 		/*
1054 		 * If the event has been detached at cgroup removal, we
1055 		 * can simply return knowing the other side will cleanup
1056 		 * for us.
1057 		 *
1058 		 * We can't race against event freeing since the other
1059 		 * side will require wqh->lock via remove_wait_queue(),
1060 		 * which we hold.
1061 		 */
1062 		spin_lock(&memcg->event_list_lock);
1063 		if (!list_empty(&event->list)) {
1064 			list_del_init(&event->list);
1065 			/*
1066 			 * We are in atomic context, but cgroup_event_remove()
1067 			 * may sleep, so we have to call it in workqueue.
1068 			 */
1069 			schedule_work(&event->remove);
1070 		}
1071 		spin_unlock(&memcg->event_list_lock);
1072 	}
1073 
1074 	return 0;
1075 }
1076 
1077 static void memcg_event_ptable_queue_proc(struct file *file,
1078 		wait_queue_head_t *wqh, poll_table *pt)
1079 {
1080 	struct mem_cgroup_event *event =
1081 		container_of(pt, struct mem_cgroup_event, pt);
1082 
1083 	event->wqh = wqh;
1084 	add_wait_queue(wqh, &event->wait);
1085 }
1086 
1087 /*
1088  * DO NOT USE IN NEW FILES.
1089  *
1090  * Parse input and register new cgroup event handler.
1091  *
1092  * Input must be in format '<event_fd> <control_fd> <args>'.
1093  * Interpretation of args is defined by control file implementation.
1094  */
1095 static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
1096 					 char *buf, size_t nbytes, loff_t off)
1097 {
1098 	struct cgroup_subsys_state *css = of_css(of);
1099 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
1100 	struct mem_cgroup_event *event;
1101 	struct cgroup_subsys_state *cfile_css;
1102 	unsigned int efd, cfd;
1103 	struct dentry *cdentry;
1104 	const char *name;
1105 	char *endp;
1106 	int ret;
1107 
1108 	if (IS_ENABLED(CONFIG_PREEMPT_RT))
1109 		return -EOPNOTSUPP;
1110 
1111 	buf = strstrip(buf);
1112 
1113 	efd = simple_strtoul(buf, &endp, 10);
1114 	if (*endp != ' ')
1115 		return -EINVAL;
1116 	buf = endp + 1;
1117 
1118 	cfd = simple_strtoul(buf, &endp, 10);
1119 	if (*endp == '\0')
1120 		buf = endp;
1121 	else if (*endp == ' ')
1122 		buf = endp + 1;
1123 	else
1124 		return -EINVAL;
1125 
1126 	CLASS(fd, efile)(efd);
1127 	if (fd_empty(efile))
1128 		return -EBADF;
1129 
1130 	CLASS(fd, cfile)(cfd);
1131 
1132 	event = kzalloc_obj(*event, GFP_KERNEL_ACCOUNT);
1133 	if (!event)
1134 		return -ENOMEM;
1135 
1136 	event->memcg = memcg;
1137 	INIT_LIST_HEAD(&event->list);
1138 	init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc);
1139 	init_waitqueue_func_entry(&event->wait, memcg_event_wake);
1140 	INIT_WORK(&event->remove, memcg_event_remove);
1141 
1142 	event->eventfd = eventfd_ctx_fileget(fd_file(efile));
1143 	if (IS_ERR(event->eventfd)) {
1144 		ret = PTR_ERR(event->eventfd);
1145 		goto out_kfree;
1146 	}
1147 
1148 	if (fd_empty(cfile)) {
1149 		ret = -EBADF;
1150 		goto out_put_eventfd;
1151 	}
1152 
1153 	/* the process need read permission on control file */
1154 	/* AV: shouldn't we check that it's been opened for read instead? */
1155 	ret = file_permission(fd_file(cfile), MAY_READ);
1156 	if (ret < 0)
1157 		goto out_put_eventfd;
1158 
1159 	/*
1160 	 * The control file must be a regular cgroup1 file. As a regular cgroup
1161 	 * file can't be renamed, it's safe to access its name afterwards.
1162 	 */
1163 	cdentry = fd_file(cfile)->f_path.dentry;
1164 	if (cdentry->d_sb->s_type != &cgroup_fs_type || !d_is_reg(cdentry)) {
1165 		ret = -EINVAL;
1166 		goto out_put_eventfd;
1167 	}
1168 
1169 	/*
1170 	 * Determine the event callbacks and set them in @event.  This used
1171 	 * to be done via struct cftype but cgroup core no longer knows
1172 	 * about these events.  The following is crude but the whole thing
1173 	 * is for compatibility anyway.
1174 	 *
1175 	 * DO NOT ADD NEW FILES.
1176 	 */
1177 	name = cdentry->d_name.name;
1178 
1179 	if (!strcmp(name, "memory.usage_in_bytes")) {
1180 		event->register_event = mem_cgroup_usage_register_event;
1181 		event->unregister_event = mem_cgroup_usage_unregister_event;
1182 	} else if (!strcmp(name, "memory.oom_control")) {
1183 		pr_warn_once("oom_control is deprecated and will be removed. "
1184 			     "Please report your usecase to linux-mm@kvack.org"
1185 			     " if you depend on this functionality.\n");
1186 		event->register_event = mem_cgroup_oom_register_event;
1187 		event->unregister_event = mem_cgroup_oom_unregister_event;
1188 	} else if (!strcmp(name, "memory.pressure_level")) {
1189 		pr_warn_once("pressure_level is deprecated and will be removed. "
1190 			     "Please report your usecase to linux-mm@kvack.org "
1191 			     "if you depend on this functionality.\n");
1192 		event->register_event = vmpressure_register_event;
1193 		event->unregister_event = vmpressure_unregister_event;
1194 	} else if (!strcmp(name, "memory.memsw.usage_in_bytes")) {
1195 		event->register_event = memsw_cgroup_usage_register_event;
1196 		event->unregister_event = memsw_cgroup_usage_unregister_event;
1197 	} else {
1198 		ret = -EINVAL;
1199 		goto out_put_eventfd;
1200 	}
1201 
1202 	/*
1203 	 * Verify @cfile should belong to @css.  Also, remaining events are
1204 	 * automatically removed on cgroup destruction but the removal is
1205 	 * asynchronous, so take an extra ref on @css.
1206 	 */
1207 	cfile_css = css_tryget_online_from_dir(cdentry->d_parent,
1208 					       &memory_cgrp_subsys);
1209 	ret = -EINVAL;
1210 	if (IS_ERR(cfile_css))
1211 		goto out_put_eventfd;
1212 	if (cfile_css != css)
1213 		goto out_put_css;
1214 
1215 	ret = event->register_event(memcg, event->eventfd, buf);
1216 	if (ret)
1217 		goto out_put_css;
1218 
1219 	vfs_poll(fd_file(efile), &event->pt);
1220 
1221 	spin_lock_irq(&memcg->event_list_lock);
1222 	list_add(&event->list, &memcg->event_list);
1223 	spin_unlock_irq(&memcg->event_list_lock);
1224 	return nbytes;
1225 
1226 out_put_css:
1227 	css_put(cfile_css);
1228 out_put_eventfd:
1229 	eventfd_ctx_put(event->eventfd);
1230 out_kfree:
1231 	kfree(event);
1232 	return ret;
1233 }
1234 
1235 void memcg1_memcg_init(struct mem_cgroup *memcg)
1236 {
1237 	INIT_LIST_HEAD(&memcg->oom_notify);
1238 	mutex_init(&memcg->thresholds_lock);
1239 	INIT_LIST_HEAD(&memcg->event_list);
1240 	spin_lock_init(&memcg->event_list_lock);
1241 }
1242 
1243 void memcg1_css_offline(struct mem_cgroup *memcg)
1244 {
1245 	struct mem_cgroup_event *event, *tmp;
1246 
1247 	/*
1248 	 * Unregister events and notify userspace.
1249 	 * Notify userspace about cgroup removing only after rmdir of cgroup
1250 	 * directory to avoid race between userspace and kernelspace.
1251 	 */
1252 	spin_lock_irq(&memcg->event_list_lock);
1253 	list_for_each_entry_safe(event, tmp, &memcg->event_list, list) {
1254 		list_del_init(&event->list);
1255 		schedule_work(&event->remove);
1256 	}
1257 	spin_unlock_irq(&memcg->event_list_lock);
1258 }
1259 
1260 /*
1261  * Check OOM-Killer is already running under our hierarchy.
1262  * If someone is running, return false.
1263  */
1264 static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
1265 {
1266 	struct mem_cgroup *iter, *failed = NULL;
1267 
1268 	spin_lock(&memcg_oom_lock);
1269 
1270 	for_each_mem_cgroup_tree(iter, memcg) {
1271 		if (iter->oom_lock) {
1272 			/*
1273 			 * this subtree of our hierarchy is already locked
1274 			 * so we cannot give a lock.
1275 			 */
1276 			failed = iter;
1277 			mem_cgroup_iter_break(memcg, iter);
1278 			break;
1279 		}
1280 		iter->oom_lock = true;
1281 	}
1282 
1283 	if (failed) {
1284 		/*
1285 		 * OK, we failed to lock the whole subtree so we have
1286 		 * to clean up what we set up to the failing subtree
1287 		 */
1288 		for_each_mem_cgroup_tree(iter, memcg) {
1289 			if (iter == failed) {
1290 				mem_cgroup_iter_break(memcg, iter);
1291 				break;
1292 			}
1293 			iter->oom_lock = false;
1294 		}
1295 	} else
1296 		mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_);
1297 
1298 	spin_unlock(&memcg_oom_lock);
1299 
1300 	return !failed;
1301 }
1302 
1303 static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
1304 {
1305 	struct mem_cgroup *iter;
1306 
1307 	spin_lock(&memcg_oom_lock);
1308 	mutex_release(&memcg_oom_lock_dep_map, _RET_IP_);
1309 	for_each_mem_cgroup_tree(iter, memcg)
1310 		iter->oom_lock = false;
1311 	spin_unlock(&memcg_oom_lock);
1312 }
1313 
1314 static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
1315 {
1316 	struct mem_cgroup *iter;
1317 
1318 	spin_lock(&memcg_oom_lock);
1319 	for_each_mem_cgroup_tree(iter, memcg)
1320 		iter->under_oom++;
1321 	spin_unlock(&memcg_oom_lock);
1322 }
1323 
1324 static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
1325 {
1326 	struct mem_cgroup *iter;
1327 
1328 	/*
1329 	 * Be careful about under_oom underflows because a child memcg
1330 	 * could have been added after mem_cgroup_mark_under_oom.
1331 	 */
1332 	spin_lock(&memcg_oom_lock);
1333 	for_each_mem_cgroup_tree(iter, memcg)
1334 		if (iter->under_oom > 0)
1335 			iter->under_oom--;
1336 	spin_unlock(&memcg_oom_lock);
1337 }
1338 
1339 static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1340 
1341 struct oom_wait_info {
1342 	struct mem_cgroup *memcg;
1343 	wait_queue_entry_t	wait;
1344 };
1345 
1346 static int memcg_oom_wake_function(wait_queue_entry_t *wait,
1347 	unsigned int mode, int sync, void *arg)
1348 {
1349 	struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1350 	struct mem_cgroup *oom_wait_memcg;
1351 	struct oom_wait_info *oom_wait_info;
1352 
1353 	oom_wait_info = container_of(wait, struct oom_wait_info, wait);
1354 	oom_wait_memcg = oom_wait_info->memcg;
1355 
1356 	if (!mem_cgroup_is_descendant(wake_memcg, oom_wait_memcg) &&
1357 	    !mem_cgroup_is_descendant(oom_wait_memcg, wake_memcg))
1358 		return 0;
1359 	return autoremove_wake_function(wait, mode, sync, arg);
1360 }
1361 
1362 void memcg1_oom_recover(struct mem_cgroup *memcg)
1363 {
1364 	/*
1365 	 * For the following lockless ->under_oom test, the only required
1366 	 * guarantee is that it must see the state asserted by an OOM when
1367 	 * this function is called as a result of userland actions
1368 	 * triggered by the notification of the OOM.  This is trivially
1369 	 * achieved by invoking mem_cgroup_mark_under_oom() before
1370 	 * triggering notification.
1371 	 */
1372 	if (memcg && memcg->under_oom)
1373 		__wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
1374 }
1375 
1376 /**
1377  * mem_cgroup_oom_synchronize - complete memcg OOM handling
1378  * @handle: actually kill/wait or just clean up the OOM state
1379  *
1380  * This has to be called at the end of a page fault if the memcg OOM
1381  * handler was enabled.
1382  *
1383  * Memcg supports userspace OOM handling where failed allocations must
1384  * sleep on a waitqueue until the userspace task resolves the
1385  * situation.  Sleeping directly in the charge context with all kinds
1386  * of locks held is not a good idea, instead we remember an OOM state
1387  * in the task and mem_cgroup_oom_synchronize() has to be called at
1388  * the end of the page fault to complete the OOM handling.
1389  *
1390  * Returns %true if an ongoing memcg OOM situation was detected and
1391  * completed, %false otherwise.
1392  */
1393 bool mem_cgroup_oom_synchronize(bool handle)
1394 {
1395 	struct mem_cgroup *memcg = current->memcg_in_oom;
1396 	struct oom_wait_info owait;
1397 	bool locked;
1398 
1399 	/* OOM is global, do not handle */
1400 	if (!memcg)
1401 		return false;
1402 
1403 	if (!handle)
1404 		goto cleanup;
1405 
1406 	owait.memcg = memcg;
1407 	owait.wait.flags = 0;
1408 	owait.wait.func = memcg_oom_wake_function;
1409 	owait.wait.private = current;
1410 	INIT_LIST_HEAD(&owait.wait.entry);
1411 
1412 	prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1413 	mem_cgroup_mark_under_oom(memcg);
1414 
1415 	locked = mem_cgroup_oom_trylock(memcg);
1416 
1417 	if (locked)
1418 		mem_cgroup_oom_notify(memcg);
1419 
1420 	schedule();
1421 	mem_cgroup_unmark_under_oom(memcg);
1422 	finish_wait(&memcg_oom_waitq, &owait.wait);
1423 
1424 	if (locked)
1425 		mem_cgroup_oom_unlock(memcg);
1426 cleanup:
1427 	current->memcg_in_oom = NULL;
1428 	css_put(&memcg->css);
1429 	return true;
1430 }
1431 
1432 
1433 bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked)
1434 {
1435 	/*
1436 	 * We are in the middle of the charge context here, so we
1437 	 * don't want to block when potentially sitting on a callstack
1438 	 * that holds all kinds of filesystem and mm locks.
1439 	 *
1440 	 * cgroup1 allows disabling the OOM killer and waiting for outside
1441 	 * handling until the charge can succeed; remember the context and put
1442 	 * the task to sleep at the end of the page fault when all locks are
1443 	 * released.
1444 	 *
1445 	 * On the other hand, in-kernel OOM killer allows for an async victim
1446 	 * memory reclaim (oom_reaper) and that means that we are not solely
1447 	 * relying on the oom victim to make a forward progress and we can
1448 	 * invoke the oom killer here.
1449 	 *
1450 	 * Please note that mem_cgroup_out_of_memory might fail to find a
1451 	 * victim and then we have to bail out from the charge path.
1452 	 */
1453 	if (READ_ONCE(memcg->oom_kill_disable)) {
1454 		if (current->in_user_fault) {
1455 			css_get(&memcg->css);
1456 			current->memcg_in_oom = memcg;
1457 		}
1458 		return false;
1459 	}
1460 
1461 	mem_cgroup_mark_under_oom(memcg);
1462 
1463 	*locked = mem_cgroup_oom_trylock(memcg);
1464 
1465 	if (*locked)
1466 		mem_cgroup_oom_notify(memcg);
1467 
1468 	mem_cgroup_unmark_under_oom(memcg);
1469 
1470 	return true;
1471 }
1472 
1473 void memcg1_oom_finish(struct mem_cgroup *memcg, bool locked)
1474 {
1475 	if (locked)
1476 		mem_cgroup_oom_unlock(memcg);
1477 }
1478 
1479 /*
1480  * cgroup v1 userspace vmpressure interface (memory.pressure_level /
1481  * cgroup.event_control). Kept here so v2-only kernels (CONFIG_MEMCG_V1=n)
1482  * drop the whole eventfd accumulator, its work item, and the per-memcg
1483  * state it requires.
1484  *
1485  * When there are too little pages left to scan, vmpressure() may miss the
1486  * critical pressure as number of pages will be less than "window size".
1487  * However, in that case the vmscan priority will raise fast as the
1488  * reclaimer will try to scan LRUs more deeply.
1489  *
1490  * The vmscan logic considers these special priorities:
1491  *
1492  * prio == DEF_PRIORITY (12): reclaimer starts with that value
1493  * prio <= DEF_PRIORITY - 2 : kswapd becomes somewhat overwhelmed
1494  * prio == 0                : close to OOM, kernel scans every page in an lru
1495  *
1496  * Any value in this range is acceptable for this tunable (i.e. from 12 to
1497  * 0). Current value for the vmpressure_level_critical_prio is chosen
1498  * empirically, but the number, in essence, means that we consider
1499  * critical level when scanning depth is ~10% of the lru size (vmscan
1500  * scans 'lru_size >> prio' pages, so it is actually 12.5%, or one
1501  * eights).
1502  */
1503 static const unsigned int vmpressure_level_critical_prio = ilog2(100 / 10);
1504 
1505 enum vmpressure_modes {
1506 	VMPRESSURE_NO_PASSTHROUGH = 0,
1507 	VMPRESSURE_HIERARCHY,
1508 	VMPRESSURE_LOCAL,
1509 	VMPRESSURE_NUM_MODES,
1510 };
1511 
1512 static const char * const vmpressure_str_levels[] = {
1513 	[VMPRESSURE_LOW] = "low",
1514 	[VMPRESSURE_MEDIUM] = "medium",
1515 	[VMPRESSURE_CRITICAL] = "critical",
1516 };
1517 
1518 static const char * const vmpressure_str_modes[] = {
1519 	[VMPRESSURE_NO_PASSTHROUGH] = "default",
1520 	[VMPRESSURE_HIERARCHY] = "hierarchy",
1521 	[VMPRESSURE_LOCAL] = "local",
1522 };
1523 
1524 struct vmpressure_event {
1525 	struct eventfd_ctx *efd;
1526 	enum vmpressure_levels level;
1527 	enum vmpressure_modes mode;
1528 	struct list_head node;
1529 };
1530 
1531 static struct vmpressure *work_to_vmpressure(struct work_struct *work)
1532 {
1533 	return container_of(work, struct vmpressure, work);
1534 }
1535 
1536 static struct vmpressure *vmpressure_parent(struct vmpressure *vmpr)
1537 {
1538 	struct mem_cgroup *memcg = vmpressure_to_memcg(vmpr);
1539 
1540 	memcg = parent_mem_cgroup(memcg);
1541 	if (!memcg)
1542 		return NULL;
1543 	return memcg_to_vmpressure(memcg);
1544 }
1545 
1546 static bool vmpressure_event(struct vmpressure *vmpr,
1547 			     const enum vmpressure_levels level,
1548 			     bool ancestor, bool signalled)
1549 {
1550 	struct vmpressure_event *ev;
1551 	bool ret = false;
1552 
1553 	mutex_lock(&vmpr->events_lock);
1554 	list_for_each_entry(ev, &vmpr->events, node) {
1555 		if (ancestor && ev->mode == VMPRESSURE_LOCAL)
1556 			continue;
1557 		if (signalled && ev->mode == VMPRESSURE_NO_PASSTHROUGH)
1558 			continue;
1559 		if (level < ev->level)
1560 			continue;
1561 		eventfd_signal(ev->efd);
1562 		ret = true;
1563 	}
1564 	mutex_unlock(&vmpr->events_lock);
1565 
1566 	return ret;
1567 }
1568 
1569 static void vmpressure_work_fn(struct work_struct *work)
1570 {
1571 	struct vmpressure *vmpr = work_to_vmpressure(work);
1572 	unsigned long scanned;
1573 	unsigned long reclaimed;
1574 	enum vmpressure_levels level;
1575 	bool ancestor = false;
1576 	bool signalled = false;
1577 
1578 	spin_lock(&vmpr->sr_lock);
1579 	/*
1580 	 * Several contexts might be calling vmpressure(), so it is
1581 	 * possible that the work was rescheduled again before the old
1582 	 * work context cleared the counters. In that case we will run
1583 	 * just after the old work returns, but then scanned might be zero
1584 	 * here. No need for any locks here since we don't care if
1585 	 * vmpr->reclaimed is in sync.
1586 	 */
1587 	scanned = vmpr->tree_scanned;
1588 	if (!scanned) {
1589 		spin_unlock(&vmpr->sr_lock);
1590 		return;
1591 	}
1592 
1593 	reclaimed = vmpr->tree_reclaimed;
1594 	vmpr->tree_scanned = 0;
1595 	vmpr->tree_reclaimed = 0;
1596 	spin_unlock(&vmpr->sr_lock);
1597 
1598 	level = vmpressure_calc_level(scanned, reclaimed);
1599 
1600 	do {
1601 		if (vmpressure_event(vmpr, level, ancestor, signalled))
1602 			signalled = true;
1603 		ancestor = true;
1604 	} while ((vmpr = vmpressure_parent(vmpr)));
1605 }
1606 
1607 /*
1608  * Tree-mode accumulator: accumulate per-memcg scanned/reclaimed and
1609  * schedule the work that walks the parent chain and signals registered
1610  * eventfd listeners once we cross the window threshold.
1611  */
1612 void vmpressure_v1_account_tree(struct vmpressure *vmpr,
1613 				unsigned long scanned,
1614 				unsigned long reclaimed)
1615 {
1616 	spin_lock(&vmpr->sr_lock);
1617 	scanned = vmpr->tree_scanned += scanned;
1618 	vmpr->tree_reclaimed += reclaimed;
1619 	spin_unlock(&vmpr->sr_lock);
1620 
1621 	if (scanned < vmpressure_win)
1622 		return;
1623 	schedule_work(&vmpr->work);
1624 }
1625 
1626 void vmpressure_v1_init(struct vmpressure *vmpr)
1627 {
1628 	mutex_init(&vmpr->events_lock);
1629 	INIT_LIST_HEAD(&vmpr->events);
1630 	INIT_WORK(&vmpr->work, vmpressure_work_fn);
1631 }
1632 
1633 void vmpressure_v1_cleanup(struct vmpressure *vmpr)
1634 {
1635 	/*
1636 	 * Make sure there is no pending work before eventfd infrastructure
1637 	 * goes away.
1638 	 */
1639 	flush_work(&vmpr->work);
1640 }
1641 
1642 /**
1643  * vmpressure_prio() - Account memory pressure through reclaimer priority level
1644  * @gfp:	reclaimer's gfp mask
1645  * @memcg:	cgroup memory controller handle
1646  * @prio:	reclaimer's priority
1647  *
1648  * This function should be called from the reclaim path every time when
1649  * the vmscan's reclaiming priority (scanning depth) changes.
1650  *
1651  * This function does not return any value.
1652  */
1653 void vmpressure_prio(gfp_t gfp, struct mem_cgroup *memcg, int prio)
1654 {
1655 	/*
1656 	 * We only use prio for accounting critical level. For more info
1657 	 * see comment for vmpressure_level_critical_prio variable above.
1658 	 */
1659 	if (prio > vmpressure_level_critical_prio)
1660 		return;
1661 
1662 	/*
1663 	 * OK, the prio is below the threshold, updating vmpressure
1664 	 * information before shrinker dives into long shrinking of long
1665 	 * range vmscan. Passing scanned = vmpressure_win, reclaimed = 0
1666 	 * to the vmpressure() basically means that we signal 'critical'
1667 	 * level.
1668 	 */
1669 	vmpressure(gfp, 0, memcg, true, vmpressure_win, 0);
1670 }
1671 
1672 #define MAX_VMPRESSURE_ARGS_LEN	(strlen("critical") + strlen("hierarchy") + 2)
1673 
1674 /**
1675  * vmpressure_register_event() - Bind vmpressure notifications to an eventfd
1676  * @memcg:	memcg that is interested in vmpressure notifications
1677  * @eventfd:	eventfd context to link notifications with
1678  * @args:	event arguments (pressure level threshold, optional mode)
1679  *
1680  * This function associates eventfd context with the vmpressure
1681  * infrastructure, so that the notifications will be delivered to the
1682  * @eventfd. The @args parameter is a comma-delimited string that denotes a
1683  * pressure level threshold (one of vmpressure_str_levels, i.e. "low", "medium",
1684  * or "critical") and an optional mode (one of vmpressure_str_modes, i.e.
1685  * "hierarchy" or "local").
1686  *
1687  * To be used as memcg event method.
1688  *
1689  * Return: 0 on success, -ENOMEM on memory failure or -EINVAL if @args could
1690  * not be parsed.
1691  */
1692 int vmpressure_register_event(struct mem_cgroup *memcg,
1693 			      struct eventfd_ctx *eventfd, const char *args)
1694 {
1695 	struct vmpressure *vmpr = memcg_to_vmpressure(memcg);
1696 	struct vmpressure_event *ev;
1697 	enum vmpressure_modes mode = VMPRESSURE_NO_PASSTHROUGH;
1698 	enum vmpressure_levels level;
1699 	char *spec, *spec_orig;
1700 	char *token;
1701 	int ret = 0;
1702 
1703 	spec_orig = spec = kstrndup(args, MAX_VMPRESSURE_ARGS_LEN, GFP_KERNEL);
1704 	if (!spec)
1705 		return -ENOMEM;
1706 
1707 	/* Find required level */
1708 	token = strsep(&spec, ",");
1709 	ret = match_string(vmpressure_str_levels, VMPRESSURE_NUM_LEVELS, token);
1710 	if (ret < 0)
1711 		goto out;
1712 	level = ret;
1713 
1714 	/* Find optional mode */
1715 	token = strsep(&spec, ",");
1716 	if (token) {
1717 		ret = match_string(vmpressure_str_modes, VMPRESSURE_NUM_MODES, token);
1718 		if (ret < 0)
1719 			goto out;
1720 		mode = ret;
1721 	}
1722 
1723 	ev = kzalloc_obj(*ev, GFP_KERNEL_ACCOUNT);
1724 	if (!ev) {
1725 		ret = -ENOMEM;
1726 		goto out;
1727 	}
1728 
1729 	ev->efd = eventfd;
1730 	ev->level = level;
1731 	ev->mode = mode;
1732 
1733 	mutex_lock(&vmpr->events_lock);
1734 	list_add(&ev->node, &vmpr->events);
1735 	mutex_unlock(&vmpr->events_lock);
1736 	ret = 0;
1737 out:
1738 	kfree(spec_orig);
1739 	return ret;
1740 }
1741 
1742 /**
1743  * vmpressure_unregister_event() - Unbind eventfd from vmpressure
1744  * @memcg:	memcg handle
1745  * @eventfd:	eventfd context that was used to link vmpressure with the @cg
1746  *
1747  * This function does internal manipulations to detach the @eventfd from
1748  * the vmpressure notifications, and then frees internal resources
1749  * associated with the @eventfd (but the @eventfd itself is not freed).
1750  *
1751  * To be used as memcg event method.
1752  */
1753 void vmpressure_unregister_event(struct mem_cgroup *memcg,
1754 				 struct eventfd_ctx *eventfd)
1755 {
1756 	struct vmpressure *vmpr = memcg_to_vmpressure(memcg);
1757 	struct vmpressure_event *ev;
1758 
1759 	mutex_lock(&vmpr->events_lock);
1760 	list_for_each_entry(ev, &vmpr->events, node) {
1761 		if (ev->efd != eventfd)
1762 			continue;
1763 		list_del(&ev->node);
1764 		kfree(ev);
1765 		break;
1766 	}
1767 	mutex_unlock(&vmpr->events_lock);
1768 }
1769 
1770 static DEFINE_MUTEX(memcg_max_mutex);
1771 
1772 static int mem_cgroup_resize_max(struct mem_cgroup *memcg,
1773 				 unsigned long max, bool memsw)
1774 {
1775 	bool enlarge = false;
1776 	bool drained = false;
1777 	int ret;
1778 	bool limits_invariant;
1779 	struct page_counter *counter = memsw ? &memcg->memsw : &memcg->memory;
1780 
1781 	do {
1782 		if (signal_pending(current)) {
1783 			ret = -EINTR;
1784 			break;
1785 		}
1786 
1787 		mutex_lock(&memcg_max_mutex);
1788 		/*
1789 		 * Make sure that the new limit (memsw or memory limit) doesn't
1790 		 * break our basic invariant rule memory.max <= memsw.max.
1791 		 */
1792 		limits_invariant = memsw ? max >= READ_ONCE(memcg->memory.max) :
1793 					   max <= memcg->memsw.max;
1794 		if (!limits_invariant) {
1795 			mutex_unlock(&memcg_max_mutex);
1796 			ret = -EINVAL;
1797 			break;
1798 		}
1799 		if (max > counter->max)
1800 			enlarge = true;
1801 		ret = page_counter_set_max(counter, max);
1802 		mutex_unlock(&memcg_max_mutex);
1803 
1804 		if (!ret)
1805 			break;
1806 
1807 		/* cgroup_rmdir() waits for us with cgroup_mutex held. */
1808 		if (memcg_is_dying(memcg))
1809 			break;
1810 
1811 		if (!drained) {
1812 			drain_all_stock(memcg);
1813 			drained = true;
1814 			continue;
1815 		}
1816 
1817 		if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
1818 				memsw ? 0 : MEMCG_RECLAIM_MAY_SWAP, NULL)) {
1819 			ret = -EBUSY;
1820 			break;
1821 		}
1822 	} while (true);
1823 
1824 	if (!ret && enlarge)
1825 		memcg1_oom_recover(memcg);
1826 
1827 	return ret;
1828 }
1829 
1830 /*
1831  * Reclaims as many pages from the given memcg as possible.
1832  *
1833  * Caller is responsible for holding css reference for memcg.
1834  */
1835 static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
1836 {
1837 	int nr_retries = MAX_RECLAIM_RETRIES;
1838 
1839 	/* we call try-to-free pages for make this cgroup empty */
1840 	lru_add_drain_all();
1841 
1842 	drain_all_stock(memcg);
1843 
1844 	/* try to free all pages in this cgroup */
1845 	while (nr_retries && page_counter_read(&memcg->memory)) {
1846 		if (signal_pending(current))
1847 			return -EINTR;
1848 
1849 		/* cgroup_rmdir() waits for us with cgroup_mutex held. */
1850 		if (memcg_is_dying(memcg))
1851 			break;
1852 
1853 		if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
1854 						  MEMCG_RECLAIM_MAY_SWAP, NULL))
1855 			nr_retries--;
1856 	}
1857 
1858 	return 0;
1859 }
1860 
1861 static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of,
1862 					    char *buf, size_t nbytes,
1863 					    loff_t off)
1864 {
1865 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
1866 
1867 	if (mem_cgroup_is_root(memcg))
1868 		return -EINVAL;
1869 	return mem_cgroup_force_empty(memcg) ?: nbytes;
1870 }
1871 
1872 static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
1873 				     struct cftype *cft)
1874 {
1875 	return 1;
1876 }
1877 
1878 static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
1879 				      struct cftype *cft, u64 val)
1880 {
1881 	if (val == 1)
1882 		return 0;
1883 
1884 	pr_warn_once("Non-hierarchical mode is deprecated. "
1885 		     "Please report your usecase to linux-mm@kvack.org if you "
1886 		     "depend on this functionality.\n");
1887 
1888 	return -EINVAL;
1889 }
1890 
1891 static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
1892 			       struct cftype *cft)
1893 {
1894 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
1895 	struct page_counter *counter;
1896 
1897 	switch (MEMFILE_TYPE(cft->private)) {
1898 	case _MEM:
1899 		counter = &memcg->memory;
1900 		break;
1901 	case _MEMSWAP:
1902 		counter = &memcg->memsw;
1903 		break;
1904 	case _KMEM:
1905 		counter = &memcg->kmem;
1906 		break;
1907 	case _TCP:
1908 		counter = &memcg->tcpmem;
1909 		break;
1910 	default:
1911 		BUG();
1912 	}
1913 
1914 	switch (MEMFILE_ATTR(cft->private)) {
1915 	case RES_USAGE:
1916 		if (counter == &memcg->memory)
1917 			return (u64)mem_cgroup_usage(memcg, false) * PAGE_SIZE;
1918 		if (counter == &memcg->memsw)
1919 			return (u64)mem_cgroup_usage(memcg, true) * PAGE_SIZE;
1920 		return (u64)page_counter_read(counter) * PAGE_SIZE;
1921 	case RES_LIMIT:
1922 		return (u64)counter->max * PAGE_SIZE;
1923 	case RES_MAX_USAGE:
1924 		return (u64)counter->watermark * PAGE_SIZE;
1925 	case RES_FAILCNT:
1926 		return counter->failcnt;
1927 	case RES_SOFT_LIMIT:
1928 		return (u64)READ_ONCE(memcg->soft_limit) * PAGE_SIZE;
1929 	default:
1930 		BUG();
1931 	}
1932 }
1933 
1934 /*
1935  * This function doesn't do anything useful. Its only job is to provide a read
1936  * handler for a file so that cgroup_file_mode() will add read permissions.
1937  */
1938 static int mem_cgroup_dummy_seq_show(__always_unused struct seq_file *m,
1939 				     __always_unused void *v)
1940 {
1941 	return -EINVAL;
1942 }
1943 
1944 static int memcg_update_tcp_max(struct mem_cgroup *memcg, unsigned long max)
1945 {
1946 	int ret;
1947 
1948 	mutex_lock(&memcg_max_mutex);
1949 
1950 	ret = page_counter_set_max(&memcg->tcpmem, max);
1951 	if (ret)
1952 		goto out;
1953 
1954 	if (!memcg->tcpmem_active) {
1955 		/*
1956 		 * The active flag needs to be written after the static_key
1957 		 * update. This is what guarantees that the socket activation
1958 		 * function is the last one to run. See mem_cgroup_sk_alloc()
1959 		 * for details, and note that we don't mark any socket as
1960 		 * belonging to this memcg until that flag is up.
1961 		 *
1962 		 * We need to do this, because static_keys will span multiple
1963 		 * sites, but we can't control their order. If we mark a socket
1964 		 * as accounted, but the accounting functions are not patched in
1965 		 * yet, we'll lose accounting.
1966 		 *
1967 		 * We never race with the readers in mem_cgroup_sk_alloc(),
1968 		 * because when this value change, the code to process it is not
1969 		 * patched in yet.
1970 		 */
1971 		static_branch_inc(&memcg_sockets_enabled_key);
1972 		memcg->tcpmem_active = true;
1973 	}
1974 out:
1975 	mutex_unlock(&memcg_max_mutex);
1976 	return ret;
1977 }
1978 
1979 /*
1980  * The user of this function is...
1981  * RES_LIMIT.
1982  */
1983 static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
1984 				char *buf, size_t nbytes, loff_t off)
1985 {
1986 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
1987 	unsigned long nr_pages;
1988 	int ret;
1989 
1990 	buf = strstrip(buf);
1991 	ret = page_counter_memparse(buf, "-1", &nr_pages);
1992 	if (ret)
1993 		return ret;
1994 
1995 	switch (MEMFILE_ATTR(of_cft(of)->private)) {
1996 	case RES_LIMIT:
1997 		if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
1998 			ret = -EINVAL;
1999 			break;
2000 		}
2001 		switch (MEMFILE_TYPE(of_cft(of)->private)) {
2002 		case _MEM:
2003 			ret = mem_cgroup_resize_max(memcg, nr_pages, false);
2004 			break;
2005 		case _MEMSWAP:
2006 			ret = mem_cgroup_resize_max(memcg, nr_pages, true);
2007 			break;
2008 		case _KMEM:
2009 			pr_warn_once("kmem.limit_in_bytes is deprecated and will be removed. "
2010 				     "Writing any value to this file has no effect. "
2011 				     "Please report your usecase to linux-mm@kvack.org if you "
2012 				     "depend on this functionality.\n");
2013 			ret = 0;
2014 			break;
2015 		case _TCP:
2016 			pr_warn_once("kmem.tcp.limit_in_bytes is deprecated and will be removed. "
2017 				     "Please report your usecase to linux-mm@kvack.org if you "
2018 				     "depend on this functionality.\n");
2019 			ret = memcg_update_tcp_max(memcg, nr_pages);
2020 			break;
2021 		}
2022 		break;
2023 	case RES_SOFT_LIMIT:
2024 		if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
2025 			ret = -EOPNOTSUPP;
2026 		} else {
2027 			pr_warn_once("soft_limit_in_bytes is deprecated and will be removed. "
2028 				     "Please report your usecase to linux-mm@kvack.org if you "
2029 				     "depend on this functionality.\n");
2030 			WRITE_ONCE(memcg->soft_limit, nr_pages);
2031 			ret = 0;
2032 		}
2033 		break;
2034 	}
2035 	return ret ?: nbytes;
2036 }
2037 
2038 static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf,
2039 				size_t nbytes, loff_t off)
2040 {
2041 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
2042 	struct page_counter *counter;
2043 
2044 	switch (MEMFILE_TYPE(of_cft(of)->private)) {
2045 	case _MEM:
2046 		counter = &memcg->memory;
2047 		break;
2048 	case _MEMSWAP:
2049 		counter = &memcg->memsw;
2050 		break;
2051 	case _KMEM:
2052 		counter = &memcg->kmem;
2053 		break;
2054 	case _TCP:
2055 		counter = &memcg->tcpmem;
2056 		break;
2057 	default:
2058 		BUG();
2059 	}
2060 
2061 	switch (MEMFILE_ATTR(of_cft(of)->private)) {
2062 	case RES_MAX_USAGE:
2063 		page_counter_reset_watermark(counter);
2064 		break;
2065 	case RES_FAILCNT:
2066 		counter->failcnt = 0;
2067 		break;
2068 	default:
2069 		BUG();
2070 	}
2071 
2072 	return nbytes;
2073 }
2074 
2075 #ifdef CONFIG_NUMA
2076 
2077 #define LRU_ALL_FILE (BIT(LRU_INACTIVE_FILE) | BIT(LRU_ACTIVE_FILE))
2078 #define LRU_ALL_ANON (BIT(LRU_INACTIVE_ANON) | BIT(LRU_ACTIVE_ANON))
2079 #define LRU_ALL	     ((1 << NR_LRU_LISTS) - 1)
2080 
2081 static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
2082 				int nid, unsigned int lru_mask, bool tree)
2083 {
2084 	struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid));
2085 	unsigned long nr = 0;
2086 	enum lru_list lru;
2087 
2088 	VM_BUG_ON((unsigned int)nid >= nr_node_ids);
2089 
2090 	for_each_lru(lru) {
2091 		if (!(BIT(lru) & lru_mask))
2092 			continue;
2093 		if (tree)
2094 			nr += lruvec_page_state(lruvec, NR_LRU_BASE + lru);
2095 		else
2096 			nr += lruvec_page_state_local(lruvec, NR_LRU_BASE + lru);
2097 	}
2098 	return nr;
2099 }
2100 
2101 static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
2102 					     unsigned int lru_mask,
2103 					     bool tree)
2104 {
2105 	unsigned long nr = 0;
2106 	enum lru_list lru;
2107 
2108 	for_each_lru(lru) {
2109 		if (!(BIT(lru) & lru_mask))
2110 			continue;
2111 		if (tree)
2112 			nr += memcg_page_state(memcg, NR_LRU_BASE + lru);
2113 		else
2114 			nr += memcg_page_state_local(memcg, NR_LRU_BASE + lru);
2115 	}
2116 	return nr;
2117 }
2118 
2119 static int memcg_numa_stat_show(struct seq_file *m, void *v)
2120 {
2121 	struct numa_stat {
2122 		const char *name;
2123 		unsigned int lru_mask;
2124 	};
2125 
2126 	static const struct numa_stat stats[] = {
2127 		{ "total", LRU_ALL },
2128 		{ "file", LRU_ALL_FILE },
2129 		{ "anon", LRU_ALL_ANON },
2130 		{ "unevictable", BIT(LRU_UNEVICTABLE) },
2131 	};
2132 	const struct numa_stat *stat;
2133 	int nid;
2134 	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
2135 
2136 	mem_cgroup_flush_stats(memcg);
2137 
2138 	for (stat = stats; stat < ARRAY_END(stats); stat++) {
2139 		seq_printf(m, "%s=%lu", stat->name,
2140 			   mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
2141 						   false));
2142 		for_each_node_state(nid, N_MEMORY)
2143 			seq_printf(m, " N%d=%lu", nid,
2144 				   mem_cgroup_node_nr_lru_pages(memcg, nid,
2145 							stat->lru_mask, false));
2146 		seq_putc(m, '\n');
2147 	}
2148 
2149 	for (stat = stats; stat < ARRAY_END(stats); stat++) {
2150 
2151 		seq_printf(m, "hierarchical_%s=%lu", stat->name,
2152 			   mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
2153 						   true));
2154 		for_each_node_state(nid, N_MEMORY)
2155 			seq_printf(m, " N%d=%lu", nid,
2156 				   mem_cgroup_node_nr_lru_pages(memcg, nid,
2157 							stat->lru_mask, true));
2158 		seq_putc(m, '\n');
2159 	}
2160 
2161 	return 0;
2162 }
2163 #endif /* CONFIG_NUMA */
2164 
2165 static const unsigned int memcg1_stats[] = {
2166 	NR_FILE_PAGES,
2167 	NR_ANON_MAPPED,
2168 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
2169 	NR_ANON_THPS,
2170 #endif
2171 	NR_SHMEM,
2172 	NR_FILE_MAPPED,
2173 	NR_FILE_DIRTY,
2174 	NR_WRITEBACK,
2175 	WORKINGSET_REFAULT_ANON,
2176 	WORKINGSET_REFAULT_FILE,
2177 #ifdef CONFIG_SWAP
2178 	MEMCG_SWAP,
2179 	NR_SWAPCACHE,
2180 #endif
2181 };
2182 
2183 static const char *const memcg1_stat_names[] = {
2184 	"cache",
2185 	"rss",
2186 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
2187 	"rss_huge",
2188 #endif
2189 	"shmem",
2190 	"mapped_file",
2191 	"dirty",
2192 	"writeback",
2193 	"workingset_refault_anon",
2194 	"workingset_refault_file",
2195 #ifdef CONFIG_SWAP
2196 	"swap",
2197 	"swapcached",
2198 #endif
2199 };
2200 
2201 /* Universal VM events cgroup1 shows, original sort order */
2202 static const unsigned int memcg1_events[] = {
2203 	PGPGIN,
2204 	PGPGOUT,
2205 	PGFAULT,
2206 	PGMAJFAULT,
2207 };
2208 
2209 void reparent_memcg1_state_local(struct mem_cgroup *memcg, struct mem_cgroup *parent)
2210 {
2211 	int i;
2212 
2213 	for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++)
2214 		reparent_memcg_state_local(memcg, parent, memcg1_stats[i]);
2215 }
2216 
2217 void reparent_memcg1_lruvec_state_local(struct mem_cgroup *memcg, struct mem_cgroup *parent)
2218 {
2219 	int i;
2220 
2221 	for (i = 0; i < NR_LRU_LISTS; i++)
2222 		reparent_memcg_lruvec_state_local(memcg, parent, i);
2223 }
2224 
2225 void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s)
2226 {
2227 	unsigned long memory, memsw;
2228 	struct mem_cgroup *mi;
2229 	unsigned int i;
2230 
2231 	BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) != ARRAY_SIZE(memcg1_stats));
2232 
2233 	mem_cgroup_flush_stats(memcg);
2234 
2235 	for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
2236 		unsigned long nr;
2237 
2238 		nr = memcg_page_state_local_output(memcg, memcg1_stats[i]);
2239 		seq_buf_printf(s, "%s %lu\n", memcg1_stat_names[i], nr);
2240 	}
2241 
2242 	for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
2243 		seq_buf_printf(s, "%s %lu\n", vm_event_name(memcg1_events[i]),
2244 			       memcg_events_local(memcg, memcg1_events[i]));
2245 
2246 	for (i = 0; i < NR_LRU_LISTS; i++)
2247 		seq_buf_printf(s, "%s %lu\n", lru_list_name(i),
2248 			       memcg_page_state_local(memcg, NR_LRU_BASE + i) *
2249 			       PAGE_SIZE);
2250 
2251 	/* Hierarchical information */
2252 	memory = memsw = PAGE_COUNTER_MAX;
2253 	for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) {
2254 		memory = min(memory, READ_ONCE(mi->memory.max));
2255 		memsw = min(memsw, READ_ONCE(mi->memsw.max));
2256 	}
2257 	seq_buf_printf(s, "hierarchical_memory_limit %llu\n",
2258 		       (u64)memory * PAGE_SIZE);
2259 	seq_buf_printf(s, "hierarchical_memsw_limit %llu\n",
2260 		       (u64)memsw * PAGE_SIZE);
2261 
2262 	for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
2263 		unsigned long nr;
2264 
2265 		nr = memcg_page_state_output(memcg, memcg1_stats[i]);
2266 		seq_buf_printf(s, "total_%s %llu\n", memcg1_stat_names[i],
2267 			       (u64)nr);
2268 	}
2269 
2270 	for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
2271 		seq_buf_printf(s, "total_%s %llu\n",
2272 			       vm_event_name(memcg1_events[i]),
2273 			       (u64)memcg_events(memcg, memcg1_events[i]));
2274 
2275 	for (i = 0; i < NR_LRU_LISTS; i++)
2276 		seq_buf_printf(s, "total_%s %llu\n", lru_list_name(i),
2277 			       (u64)memcg_page_state(memcg, NR_LRU_BASE + i) *
2278 			       PAGE_SIZE);
2279 
2280 #ifdef CONFIG_DEBUG_VM
2281 	{
2282 		pg_data_t *pgdat;
2283 		struct mem_cgroup_per_node *mz;
2284 		unsigned long anon_cost = 0;
2285 		unsigned long file_cost = 0;
2286 
2287 		for_each_online_pgdat(pgdat) {
2288 			mz = memcg->nodeinfo[pgdat->node_id];
2289 
2290 			anon_cost += mz->lruvec.anon_cost;
2291 			file_cost += mz->lruvec.file_cost;
2292 		}
2293 		seq_buf_printf(s, "anon_cost %lu\n", anon_cost);
2294 		seq_buf_printf(s, "file_cost %lu\n", file_cost);
2295 	}
2296 #endif
2297 }
2298 
2299 static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
2300 				      struct cftype *cft)
2301 {
2302 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
2303 
2304 	return mem_cgroup_swappiness(memcg);
2305 }
2306 
2307 static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
2308 				       struct cftype *cft, u64 val)
2309 {
2310 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
2311 
2312 	if (val > MAX_SWAPPINESS)
2313 		return -EINVAL;
2314 
2315 	if (!mem_cgroup_is_root(memcg)) {
2316 		pr_info_once("Per memcg swappiness does not exist in cgroup v2. "
2317 			     "See memory.reclaim or memory.swap.max there\n ");
2318 		WRITE_ONCE(memcg->swappiness, val);
2319 	} else
2320 		WRITE_ONCE(vm_swappiness, val);
2321 
2322 	return 0;
2323 }
2324 
2325 static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
2326 {
2327 	struct mem_cgroup *memcg = mem_cgroup_from_seq(sf);
2328 
2329 	seq_printf(sf, "oom_kill_disable %d\n", READ_ONCE(memcg->oom_kill_disable));
2330 	seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom);
2331 	seq_printf(sf, "oom_kill %lu\n",
2332 		   atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL]));
2333 	return 0;
2334 }
2335 
2336 static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
2337 	struct cftype *cft, u64 val)
2338 {
2339 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
2340 
2341 	pr_warn_once("oom_control is deprecated and will be removed. "
2342 		     "Please report your usecase to linux-mm@kvack.org if you "
2343 		     "depend on this functionality.\n");
2344 
2345 	/* cannot set to root cgroup and only 0 and 1 are allowed */
2346 	if (mem_cgroup_is_root(memcg) || !((val == 0) || (val == 1)))
2347 		return -EINVAL;
2348 
2349 	WRITE_ONCE(memcg->oom_kill_disable, val);
2350 	if (!val)
2351 		memcg1_oom_recover(memcg);
2352 
2353 	return 0;
2354 }
2355 
2356 #ifdef CONFIG_SLUB_DEBUG
2357 static int mem_cgroup_slab_show(struct seq_file *m, void *p)
2358 {
2359 	/*
2360 	 * Deprecated.
2361 	 * Please, take a look at tools/cgroup/memcg_slabinfo.py .
2362 	 */
2363 	return 0;
2364 }
2365 #endif
2366 
2367 struct cftype mem_cgroup_legacy_files[] = {
2368 	{
2369 		.name = "usage_in_bytes",
2370 		.private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
2371 		.read_u64 = mem_cgroup_read_u64,
2372 	},
2373 	{
2374 		.name = "max_usage_in_bytes",
2375 		.private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
2376 		.write = mem_cgroup_reset,
2377 		.read_u64 = mem_cgroup_read_u64,
2378 	},
2379 	{
2380 		.name = "limit_in_bytes",
2381 		.private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
2382 		.write = mem_cgroup_write,
2383 		.read_u64 = mem_cgroup_read_u64,
2384 	},
2385 	{
2386 		.name = "soft_limit_in_bytes",
2387 		.private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
2388 		.write = mem_cgroup_write,
2389 		.read_u64 = mem_cgroup_read_u64,
2390 	},
2391 	{
2392 		.name = "failcnt",
2393 		.private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
2394 		.write = mem_cgroup_reset,
2395 		.read_u64 = mem_cgroup_read_u64,
2396 	},
2397 	{
2398 		.name = "stat",
2399 		.seq_show = memory_stat_show,
2400 	},
2401 	{
2402 		.name = "force_empty",
2403 		.write = mem_cgroup_force_empty_write,
2404 	},
2405 	{
2406 		.name = "use_hierarchy",
2407 		.write_u64 = mem_cgroup_hierarchy_write,
2408 		.read_u64 = mem_cgroup_hierarchy_read,
2409 	},
2410 	{
2411 		.name = "cgroup.event_control",		/* XXX: for compat */
2412 		.write = memcg_write_event_control,
2413 		.flags = CFTYPE_NO_PREFIX,
2414 	},
2415 	{
2416 		.name = "swappiness",
2417 		.read_u64 = mem_cgroup_swappiness_read,
2418 		.write_u64 = mem_cgroup_swappiness_write,
2419 	},
2420 	{
2421 		.name = "move_charge_at_immigrate",
2422 		.read_u64 = mem_cgroup_move_charge_read,
2423 		.write_u64 = mem_cgroup_move_charge_write,
2424 	},
2425 	{
2426 		.name = "oom_control",
2427 		.seq_show = mem_cgroup_oom_control_read,
2428 		.write_u64 = mem_cgroup_oom_control_write,
2429 	},
2430 	{
2431 		.name = "pressure_level",
2432 		.seq_show = mem_cgroup_dummy_seq_show,
2433 	},
2434 #ifdef CONFIG_NUMA
2435 	{
2436 		.name = "numa_stat",
2437 		.seq_show = memcg_numa_stat_show,
2438 	},
2439 #endif
2440 	{
2441 		.name = "kmem.limit_in_bytes",
2442 		.private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
2443 		.write = mem_cgroup_write,
2444 		.read_u64 = mem_cgroup_read_u64,
2445 	},
2446 	{
2447 		.name = "kmem.usage_in_bytes",
2448 		.private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
2449 		.read_u64 = mem_cgroup_read_u64,
2450 	},
2451 	{
2452 		.name = "kmem.failcnt",
2453 		.private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
2454 		.write = mem_cgroup_reset,
2455 		.read_u64 = mem_cgroup_read_u64,
2456 	},
2457 	{
2458 		.name = "kmem.max_usage_in_bytes",
2459 		.private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
2460 		.write = mem_cgroup_reset,
2461 		.read_u64 = mem_cgroup_read_u64,
2462 	},
2463 #ifdef CONFIG_SLUB_DEBUG
2464 	{
2465 		.name = "kmem.slabinfo",
2466 		.seq_show = mem_cgroup_slab_show,
2467 	},
2468 #endif
2469 	{
2470 		.name = "kmem.tcp.limit_in_bytes",
2471 		.private = MEMFILE_PRIVATE(_TCP, RES_LIMIT),
2472 		.write = mem_cgroup_write,
2473 		.read_u64 = mem_cgroup_read_u64,
2474 	},
2475 	{
2476 		.name = "kmem.tcp.usage_in_bytes",
2477 		.private = MEMFILE_PRIVATE(_TCP, RES_USAGE),
2478 		.read_u64 = mem_cgroup_read_u64,
2479 	},
2480 	{
2481 		.name = "kmem.tcp.failcnt",
2482 		.private = MEMFILE_PRIVATE(_TCP, RES_FAILCNT),
2483 		.write = mem_cgroup_reset,
2484 		.read_u64 = mem_cgroup_read_u64,
2485 	},
2486 	{
2487 		.name = "kmem.tcp.max_usage_in_bytes",
2488 		.private = MEMFILE_PRIVATE(_TCP, RES_MAX_USAGE),
2489 		.write = mem_cgroup_reset,
2490 		.read_u64 = mem_cgroup_read_u64,
2491 	},
2492 	{ },	/* terminate */
2493 };
2494 
2495 struct cftype memsw_files[] = {
2496 	{
2497 		.name = "memsw.usage_in_bytes",
2498 		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2499 		.read_u64 = mem_cgroup_read_u64,
2500 	},
2501 	{
2502 		.name = "memsw.max_usage_in_bytes",
2503 		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2504 		.write = mem_cgroup_reset,
2505 		.read_u64 = mem_cgroup_read_u64,
2506 	},
2507 	{
2508 		.name = "memsw.limit_in_bytes",
2509 		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2510 		.write = mem_cgroup_write,
2511 		.read_u64 = mem_cgroup_read_u64,
2512 	},
2513 	{
2514 		.name = "memsw.failcnt",
2515 		.private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2516 		.write = mem_cgroup_reset,
2517 		.read_u64 = mem_cgroup_read_u64,
2518 	},
2519 	{ },	/* terminate */
2520 };
2521 
2522 void memcg1_account_kmem(struct mem_cgroup *memcg, int nr_pages)
2523 {
2524 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
2525 		if (nr_pages > 0)
2526 			page_counter_charge(&memcg->kmem, nr_pages);
2527 		else
2528 			page_counter_uncharge(&memcg->kmem, -nr_pages);
2529 	}
2530 }
2531 
2532 bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
2533 			 gfp_t gfp_mask)
2534 {
2535 	struct page_counter *fail;
2536 
2537 	if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) {
2538 		memcg->tcpmem_pressure = 0;
2539 		return true;
2540 	}
2541 	memcg->tcpmem_pressure = 1;
2542 	if (gfp_mask & __GFP_NOFAIL) {
2543 		page_counter_charge(&memcg->tcpmem, nr_pages);
2544 		return true;
2545 	}
2546 	return false;
2547 }
2548 
2549 bool memcg1_alloc_events(struct mem_cgroup *memcg)
2550 {
2551 	memcg->events_percpu = alloc_percpu_gfp(struct memcg1_events_percpu,
2552 						GFP_KERNEL_ACCOUNT);
2553 	return !!memcg->events_percpu;
2554 }
2555 
2556 void memcg1_free_events(struct mem_cgroup *memcg)
2557 {
2558 	free_percpu(memcg->events_percpu);
2559 }
2560 
2561 static int __init memcg1_init(void)
2562 {
2563 	int node;
2564 
2565 	for_each_node(node) {
2566 		struct mem_cgroup_tree_per_node *rtpn;
2567 
2568 		rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, node);
2569 
2570 		rtpn->rb_root = RB_ROOT;
2571 		rtpn->rb_rightmost = NULL;
2572 		spin_lock_init(&rtpn->lock);
2573 		soft_limit_tree.rb_tree_per_node[node] = rtpn;
2574 	}
2575 
2576 	return 0;
2577 }
2578 subsys_initcall(memcg1_init);
2579