1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <linux/page_counter.h>
29#include <linux/memcontrol.h>
30#include <linux/cgroup.h>
31#include <linux/mm.h>
32#include <linux/hugetlb.h>
33#include <linux/pagemap.h>
34#include <linux/smp.h>
35#include <linux/page-flags.h>
36#include <linux/backing-dev.h>
37#include <linux/bit_spinlock.h>
38#include <linux/rcupdate.h>
39#include <linux/limits.h>
40#include <linux/export.h>
41#include <linux/mutex.h>
42#include <linux/rbtree.h>
43#include <linux/slab.h>
44#include <linux/swap.h>
45#include <linux/swapops.h>
46#include <linux/spinlock.h>
47#include <linux/eventfd.h>
48#include <linux/sort.h>
49#include <linux/fs.h>
50#include <linux/seq_file.h>
51#include <linux/vmalloc.h>
52#include <linux/vmpressure.h>
53#include <linux/mm_inline.h>
54#include <linux/page_cgroup.h>
55#include <linux/cpu.h>
56#include <linux/oom.h>
57#include "internal.h"
58#include <net/sock.h>
59#include <net/ip.h>
60#include <net/tcp_memcontrol.h>
61#include "slab.h"
62
63#include <asm/uaccess.h>
64
65#include <trace/events/vmscan.h>
66
67struct cgroup_subsys mem_cgroup_subsys __read_mostly;
68EXPORT_SYMBOL(mem_cgroup_subsys);
69
70#define MEM_CGROUP_RECLAIM_RETRIES 5
71static struct mem_cgroup *root_mem_cgroup __read_mostly;
72
73
74static bool cgroup_memory_nokmem;
75
76#ifdef CONFIG_MEMCG_SWAP
77
78int do_swap_account __read_mostly;
79
80
81#ifdef CONFIG_MEMCG_SWAP_ENABLED
82static int really_do_swap_account __initdata = 1;
83#else
84static int really_do_swap_account __initdata = 0;
85#endif
86
87#else
88#define do_swap_account 0
89#endif
90
91
92
93
94
95enum mem_cgroup_stat_index {
96
97
98
99 MEM_CGROUP_STAT_CACHE,
100 MEM_CGROUP_STAT_RSS,
101 MEM_CGROUP_STAT_RSS_HUGE,
102 MEM_CGROUP_STAT_FILE_MAPPED,
103 MEM_CGROUP_STAT_SWAP,
104 MEM_CGROUP_STAT_NSTATS,
105};
106
107static const char * const mem_cgroup_stat_names[] = {
108 "cache",
109 "rss",
110 "rss_huge",
111 "mapped_file",
112 "swap",
113};
114
115enum mem_cgroup_events_index {
116 MEM_CGROUP_EVENTS_PGPGIN,
117 MEM_CGROUP_EVENTS_PGPGOUT,
118 MEM_CGROUP_EVENTS_PGFAULT,
119 MEM_CGROUP_EVENTS_PGMAJFAULT,
120 MEM_CGROUP_EVENTS_NSTATS,
121};
122
123static const char * const mem_cgroup_events_names[] = {
124 "pgpgin",
125 "pgpgout",
126 "pgfault",
127 "pgmajfault",
128};
129
130static const char * const mem_cgroup_lru_names[] = {
131 "inactive_anon",
132 "active_anon",
133 "inactive_file",
134 "active_file",
135 "unevictable",
136};
137
138
139
140
141
142
143
144enum mem_cgroup_events_target {
145 MEM_CGROUP_TARGET_THRESH,
146 MEM_CGROUP_TARGET_SOFTLIMIT,
147 MEM_CGROUP_TARGET_NUMAINFO,
148 MEM_CGROUP_NTARGETS,
149};
150#define THRESHOLDS_EVENTS_TARGET 128
151#define SOFTLIMIT_EVENTS_TARGET 1024
152#define NUMAINFO_EVENTS_TARGET 1024
153
154#define MEM_CGROUP_ID_MAX USHRT_MAX
155
156static void mem_cgroup_id_put(struct mem_cgroup *memcg);
157static unsigned short mem_cgroup_id(struct mem_cgroup *memcg);
158
159struct mem_cgroup_stat_cpu {
160 long count[MEM_CGROUP_STAT_NSTATS];
161 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
162 unsigned long nr_page_events;
163 unsigned long targets[MEM_CGROUP_NTARGETS];
164};
165
166struct mem_cgroup_reclaim_iter {
167
168
169
170
171 struct mem_cgroup *last_visited;
172 unsigned long last_dead_count;
173
174
175 unsigned int generation;
176};
177
178
179
180
181struct mem_cgroup_per_zone {
182 struct lruvec lruvec;
183 unsigned long lru_size[NR_LRU_LISTS];
184
185 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
186
187 struct rb_node tree_node;
188 unsigned long usage_in_excess;
189
190 bool on_tree;
191 struct mem_cgroup *memcg;
192
193};
194
195struct mem_cgroup_per_node {
196 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
197};
198
199struct mem_cgroup_lru_info {
200 struct mem_cgroup_per_node *nodeinfo[0];
201};
202
203
204
205
206
207
208struct mem_cgroup_tree_per_zone {
209 struct rb_root rb_root;
210 spinlock_t lock;
211};
212
213struct mem_cgroup_tree_per_node {
214 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
215};
216
217struct mem_cgroup_tree {
218 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
219};
220
221static struct mem_cgroup_tree soft_limit_tree __read_mostly;
222
223struct mem_cgroup_threshold {
224 struct eventfd_ctx *eventfd;
225 unsigned long threshold;
226};
227
228
229struct mem_cgroup_threshold_ary {
230
231 int current_threshold;
232
233 unsigned int size;
234
235 struct mem_cgroup_threshold entries[0];
236};
237
238struct mem_cgroup_thresholds {
239
240 struct mem_cgroup_threshold_ary *primary;
241
242
243
244
245
246 struct mem_cgroup_threshold_ary *spare;
247};
248
249
250struct mem_cgroup_eventfd_list {
251 struct list_head list;
252 struct eventfd_ctx *eventfd;
253};
254
255static void mem_cgroup_threshold(struct mem_cgroup *memcg);
256static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
257
258
259
260
261
262
263
264
265
266
267
268
269struct mem_cgroup {
270 struct cgroup_subsys_state css;
271
272
273 unsigned short id;
274
275
276
277
278 struct page_counter memory;
279
280 unsigned long soft_limit;
281
282
283 struct vmpressure vmpressure;
284
285 union {
286
287
288
289 struct page_counter memsw;
290
291
292
293
294
295
296
297
298
299 struct rcu_head rcu_freeing;
300
301
302
303
304 struct work_struct work_freeing;
305 };
306
307
308
309 struct page_counter kmem;
310
311
312
313 bool use_hierarchy;
314 unsigned long kmem_account_flags;
315
316 bool oom_lock;
317 atomic_t under_oom;
318 atomic_t oom_wakeups;
319
320 atomic_t refcnt;
321
322 int swappiness;
323
324 int oom_kill_disable;
325
326
327 bool memsw_is_minimum;
328
329
330 struct mutex thresholds_lock;
331
332
333 struct mem_cgroup_thresholds thresholds;
334
335
336 struct mem_cgroup_thresholds memsw_thresholds;
337
338
339 struct list_head oom_notify;
340
341
342
343
344
345 unsigned long move_charge_at_immigrate;
346
347
348
349 atomic_t moving_account;
350
351 spinlock_t move_lock;
352
353
354
355 struct mem_cgroup_stat_cpu __percpu *stat;
356
357
358
359
360 struct mem_cgroup_stat_cpu nocpu_base;
361 spinlock_t pcp_counter_lock;
362
363 atomic_t dead_count;
364#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
365 struct tcp_memcontrol tcp_mem;
366#endif
367#if defined(CONFIG_MEMCG_KMEM)
368
369
370 struct list_head memcg_slab_caches;
371 RH_KABI_DEPRECATE(struct mutex, slab_caches_mutex)
372
373 int kmemcg_id;
374#endif
375
376 int last_scanned_node;
377#if MAX_NUMNODES > 1
378 nodemask_t scan_nodes;
379 atomic_t numainfo_events;
380 atomic_t numainfo_updating;
381#endif
382
383
384
385
386
387
388
389
390 struct mem_cgroup_lru_info info;
391};
392
393static size_t memcg_size(void)
394{
395 return sizeof(struct mem_cgroup) +
396 nr_node_ids * sizeof(struct mem_cgroup_per_node *);
397}
398
399
400enum {
401 KMEM_ACCOUNTED_ACTIVE,
402 KMEM_ACCOUNTED_DEAD,
403};
404
405#ifdef CONFIG_MEMCG_KMEM
406static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
407{
408 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
409}
410
411static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
412{
413 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
414}
415
416static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
417{
418 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
419 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
420}
421
422static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
423{
424 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
425 &memcg->kmem_account_flags);
426}
427#endif
428
429
430
431
432
433
434enum move_type {
435 MOVE_CHARGE_TYPE_ANON,
436 MOVE_CHARGE_TYPE_FILE,
437 NR_MOVE_TYPE,
438};
439
440
441static struct move_charge_struct {
442 spinlock_t lock;
443 struct mem_cgroup *from;
444 struct mem_cgroup *to;
445 unsigned long immigrate_flags;
446 unsigned long precharge;
447 unsigned long moved_charge;
448 unsigned long moved_swap;
449 struct task_struct *moving_task;
450 wait_queue_head_t waitq;
451} mc = {
452 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
453 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
454};
455
456static bool move_anon(void)
457{
458 return test_bit(MOVE_CHARGE_TYPE_ANON, &mc.immigrate_flags);
459}
460
461static bool move_file(void)
462{
463 return test_bit(MOVE_CHARGE_TYPE_FILE, &mc.immigrate_flags);
464}
465
466
467
468
469
470#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
471#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
472
473enum charge_type {
474 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
475 MEM_CGROUP_CHARGE_TYPE_ANON,
476 MEM_CGROUP_CHARGE_TYPE_SWAPOUT,
477 MEM_CGROUP_CHARGE_TYPE_DROP,
478 NR_CHARGE_TYPE,
479};
480
481
482enum res_type {
483 _MEM,
484 _MEMSWAP,
485 _OOM_TYPE,
486 _KMEM,
487};
488
489#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
490#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
491#define MEMFILE_ATTR(val) ((val) & 0xffff)
492
493#define OOM_CONTROL (0)
494
495
496
497
498#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
499#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
500#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
501#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
502
503
504
505
506
507
508static DEFINE_MUTEX(memcg_create_mutex);
509
510static void mem_cgroup_get(struct mem_cgroup *memcg);
511static void mem_cgroup_put(struct mem_cgroup *memcg);
512
513static inline
514struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
515{
516 return container_of(s, struct mem_cgroup, css);
517}
518
519
520struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
521{
522 if (!memcg)
523 memcg = root_mem_cgroup;
524 return &memcg->vmpressure;
525}
526
527struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr)
528{
529 return &container_of(vmpr, struct mem_cgroup, vmpressure)->css;
530}
531
532struct vmpressure *css_to_vmpressure(struct cgroup_subsys_state *css)
533{
534 return &mem_cgroup_from_css(css)->vmpressure;
535}
536
537static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
538{
539 return (memcg == root_mem_cgroup);
540}
541
542
543#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
544
545void sock_update_memcg(struct sock *sk)
546{
547 if (mem_cgroup_sockets_enabled) {
548 struct mem_cgroup *memcg;
549 struct cg_proto *cg_proto;
550
551 BUG_ON(!sk->sk_prot->proto_cgroup);
552
553
554
555
556
557
558
559
560
561 if (sk->sk_cgrp) {
562 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
563 mem_cgroup_get(sk->sk_cgrp->memcg);
564 return;
565 }
566
567 rcu_read_lock();
568 memcg = mem_cgroup_from_task(current);
569 cg_proto = sk->sk_prot->proto_cgroup(memcg);
570 if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) {
571 mem_cgroup_get(memcg);
572 sk->sk_cgrp = cg_proto;
573 }
574 rcu_read_unlock();
575 }
576}
577EXPORT_SYMBOL(sock_update_memcg);
578
579void sock_release_memcg(struct sock *sk)
580{
581 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
582 struct mem_cgroup *memcg;
583 WARN_ON(!sk->sk_cgrp->memcg);
584 memcg = sk->sk_cgrp->memcg;
585 mem_cgroup_put(memcg);
586 }
587}
588
589struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
590{
591 if (!memcg || mem_cgroup_is_root(memcg))
592 return NULL;
593
594 return &memcg->tcp_mem.cg_proto;
595}
596EXPORT_SYMBOL(tcp_proto_cgroup);
597
598static void disarm_sock_keys(struct mem_cgroup *memcg)
599{
600 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
601 return;
602 static_key_slow_dec(&memcg_socket_limit_enabled);
603}
604#else
605static void disarm_sock_keys(struct mem_cgroup *memcg)
606{
607}
608#endif
609
610#ifdef CONFIG_MEMCG_KMEM
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628static DEFINE_IDA(kmem_limited_groups);
629int memcg_limited_groups_array_size;
630
631
632
633
634
635
636
637
638
639
640
641
642
643#define MEMCG_CACHES_MIN_SIZE 4
644#define MEMCG_CACHES_MAX_SIZE 65535
645
646
647
648
649
650
651
652struct static_key memcg_kmem_enabled_key;
653EXPORT_SYMBOL(memcg_kmem_enabled_key);
654
655static void disarm_kmem_keys(struct mem_cgroup *memcg)
656{
657 if (memcg_kmem_is_active(memcg)) {
658 static_key_slow_dec(&memcg_kmem_enabled_key);
659 if (memcg->kmemcg_id >= 0)
660 ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id);
661 }
662
663
664
665
666 WARN_ON(page_counter_read(&memcg->kmem));
667}
668#else
669static void disarm_kmem_keys(struct mem_cgroup *memcg)
670{
671}
672#endif
673
674static void disarm_static_keys(struct mem_cgroup *memcg)
675{
676 disarm_sock_keys(memcg);
677 disarm_kmem_keys(memcg);
678}
679
680static void drain_all_stock_async(struct mem_cgroup *memcg);
681
682static struct mem_cgroup_per_zone *
683mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
684{
685 VM_BUG_ON((unsigned)nid >= nr_node_ids);
686 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
687}
688
689struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
690{
691 return &memcg->css;
692}
693
694static struct mem_cgroup_per_zone *
695page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
696{
697 int nid = page_to_nid(page);
698 int zid = page_zonenum(page);
699
700 return mem_cgroup_zoneinfo(memcg, nid, zid);
701}
702
703static struct mem_cgroup_tree_per_zone *
704soft_limit_tree_node_zone(int nid, int zid)
705{
706 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
707}
708
709static struct mem_cgroup_tree_per_zone *
710soft_limit_tree_from_page(struct page *page)
711{
712 int nid = page_to_nid(page);
713 int zid = page_zonenum(page);
714
715 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
716}
717
718static void
719__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
720 struct mem_cgroup_per_zone *mz,
721 struct mem_cgroup_tree_per_zone *mctz,
722 unsigned long new_usage_in_excess)
723{
724 struct rb_node **p = &mctz->rb_root.rb_node;
725 struct rb_node *parent = NULL;
726 struct mem_cgroup_per_zone *mz_node;
727
728 if (mz->on_tree)
729 return;
730
731 mz->usage_in_excess = new_usage_in_excess;
732 if (!mz->usage_in_excess)
733 return;
734 while (*p) {
735 parent = *p;
736 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
737 tree_node);
738 if (mz->usage_in_excess < mz_node->usage_in_excess)
739 p = &(*p)->rb_left;
740
741
742
743
744 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
745 p = &(*p)->rb_right;
746 }
747 rb_link_node(&mz->tree_node, parent, p);
748 rb_insert_color(&mz->tree_node, &mctz->rb_root);
749 mz->on_tree = true;
750}
751
752static void
753__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
754 struct mem_cgroup_per_zone *mz,
755 struct mem_cgroup_tree_per_zone *mctz)
756{
757 if (!mz->on_tree)
758 return;
759 rb_erase(&mz->tree_node, &mctz->rb_root);
760 mz->on_tree = false;
761}
762
763static void
764mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
765 struct mem_cgroup_per_zone *mz,
766 struct mem_cgroup_tree_per_zone *mctz)
767{
768 spin_lock(&mctz->lock);
769 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
770 spin_unlock(&mctz->lock);
771}
772
773static unsigned long soft_limit_excess(struct mem_cgroup *memcg)
774{
775 unsigned long nr_pages = page_counter_read(&memcg->memory);
776 unsigned long soft_limit = ACCESS_ONCE(memcg->soft_limit);
777 unsigned long excess = 0;
778
779 if (nr_pages > soft_limit)
780 excess = nr_pages - soft_limit;
781
782 return excess;
783}
784
785static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
786{
787 unsigned long excess;
788 struct mem_cgroup_per_zone *mz;
789 struct mem_cgroup_tree_per_zone *mctz;
790 int nid = page_to_nid(page);
791 int zid = page_zonenum(page);
792 mctz = soft_limit_tree_from_page(page);
793
794
795
796
797
798 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
799 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
800 excess = soft_limit_excess(memcg);
801
802
803
804
805 if (excess || mz->on_tree) {
806 spin_lock(&mctz->lock);
807
808 if (mz->on_tree)
809 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
810
811
812
813
814 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
815 spin_unlock(&mctz->lock);
816 }
817 }
818}
819
820static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
821{
822 int node, zone;
823 struct mem_cgroup_per_zone *mz;
824 struct mem_cgroup_tree_per_zone *mctz;
825
826 for_each_node(node) {
827 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
828 mz = mem_cgroup_zoneinfo(memcg, node, zone);
829 mctz = soft_limit_tree_node_zone(node, zone);
830 mem_cgroup_remove_exceeded(memcg, mz, mctz);
831 }
832 }
833}
834
835static struct mem_cgroup_per_zone *
836__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
837{
838 struct rb_node *rightmost = NULL;
839 struct mem_cgroup_per_zone *mz;
840
841retry:
842 mz = NULL;
843 rightmost = rb_last(&mctz->rb_root);
844 if (!rightmost)
845 goto done;
846
847 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
848
849
850
851
852
853 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
854 if (!soft_limit_excess(mz->memcg) ||
855 !css_tryget(&mz->memcg->css))
856 goto retry;
857done:
858 return mz;
859}
860
861static struct mem_cgroup_per_zone *
862mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
863{
864 struct mem_cgroup_per_zone *mz;
865
866 spin_lock(&mctz->lock);
867 mz = __mem_cgroup_largest_soft_limit_node(mctz);
868 spin_unlock(&mctz->lock);
869 return mz;
870}
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
892 enum mem_cgroup_stat_index idx)
893{
894 long val = 0;
895 int cpu;
896
897 get_online_cpus();
898 for_each_online_cpu(cpu)
899 val += per_cpu(memcg->stat->count[idx], cpu);
900#ifdef CONFIG_HOTPLUG_CPU
901 spin_lock(&memcg->pcp_counter_lock);
902 val += memcg->nocpu_base.count[idx];
903 spin_unlock(&memcg->pcp_counter_lock);
904#endif
905 put_online_cpus();
906 return val;
907}
908
909static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
910 bool charge)
911{
912 int val = (charge) ? 1 : -1;
913 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
914}
915
916static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
917 enum mem_cgroup_events_index idx)
918{
919 unsigned long val = 0;
920 int cpu;
921
922 for_each_online_cpu(cpu)
923 val += per_cpu(memcg->stat->events[idx], cpu);
924#ifdef CONFIG_HOTPLUG_CPU
925 spin_lock(&memcg->pcp_counter_lock);
926 val += memcg->nocpu_base.events[idx];
927 spin_unlock(&memcg->pcp_counter_lock);
928#endif
929 return val;
930}
931
932
933
934
935
936
937static void mem_cgroup_sum_all_stat_events(struct mem_cgroup *memcg,
938 unsigned long *stats,
939 unsigned long *events)
940{
941 int i;
942 int cpu;
943
944 get_online_cpus();
945 for_each_online_cpu(cpu) {
946 unsigned long *pcpu_stats = per_cpu(memcg->stat->count, cpu);
947 unsigned long *pcpu_events = per_cpu(memcg->stat->events, cpu);
948
949 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++)
950 stats[i] += pcpu_stats[i];
951
952 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
953 events[i] = pcpu_events[i];
954 }
955
956#ifdef CONFIG_HOTPLUG_CPU
957 spin_lock(&memcg->pcp_counter_lock);
958
959 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++)
960 stats[i] += memcg->nocpu_base.count[i];
961
962 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
963 events[i] = memcg->nocpu_base.events[i];
964
965 spin_unlock(&memcg->pcp_counter_lock);
966#endif
967 put_online_cpus();
968 return;
969}
970
971static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
972 struct page *page,
973 bool anon, int nr_pages)
974{
975 preempt_disable();
976
977
978
979
980
981 if (anon)
982 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
983 nr_pages);
984 else
985 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
986 nr_pages);
987
988 if (PageTransHuge(page))
989 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
990 nr_pages);
991
992
993 if (nr_pages > 0)
994 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
995 else {
996 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
997 nr_pages = -nr_pages;
998 }
999
1000 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
1001
1002 preempt_enable();
1003}
1004
1005unsigned long
1006mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
1007{
1008 struct mem_cgroup_per_zone *mz;
1009
1010 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1011 return mz->lru_size[lru];
1012}
1013
1014static unsigned long
1015mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
1016 unsigned int lru_mask)
1017{
1018 struct mem_cgroup_per_zone *mz;
1019 enum lru_list lru;
1020 unsigned long ret = 0;
1021
1022 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
1023
1024 for_each_lru(lru) {
1025 if (BIT(lru) & lru_mask)
1026 ret += mz->lru_size[lru];
1027 }
1028 return ret;
1029}
1030
1031static unsigned long
1032mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
1033 int nid, unsigned int lru_mask)
1034{
1035 u64 total = 0;
1036 int zid;
1037
1038 for (zid = 0; zid < MAX_NR_ZONES; zid++)
1039 total += mem_cgroup_zone_nr_lru_pages(memcg,
1040 nid, zid, lru_mask);
1041
1042 return total;
1043}
1044
1045static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
1046 unsigned int lru_mask)
1047{
1048 int nid;
1049 u64 total = 0;
1050
1051 for_each_node_state(nid, N_MEMORY)
1052 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
1053 return total;
1054}
1055
1056static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
1057 enum mem_cgroup_events_target target)
1058{
1059 unsigned long val, next;
1060
1061 val = __this_cpu_read(memcg->stat->nr_page_events);
1062 next = __this_cpu_read(memcg->stat->targets[target]);
1063
1064 if ((long)next - (long)val < 0) {
1065 switch (target) {
1066 case MEM_CGROUP_TARGET_THRESH:
1067 next = val + THRESHOLDS_EVENTS_TARGET;
1068 break;
1069 case MEM_CGROUP_TARGET_SOFTLIMIT:
1070 next = val + SOFTLIMIT_EVENTS_TARGET;
1071 break;
1072 case MEM_CGROUP_TARGET_NUMAINFO:
1073 next = val + NUMAINFO_EVENTS_TARGET;
1074 break;
1075 default:
1076 break;
1077 }
1078 __this_cpu_write(memcg->stat->targets[target], next);
1079 return true;
1080 }
1081 return false;
1082}
1083
1084
1085
1086
1087
1088static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
1089{
1090 preempt_disable();
1091
1092 if (unlikely(mem_cgroup_event_ratelimit(memcg,
1093 MEM_CGROUP_TARGET_THRESH))) {
1094 bool do_softlimit;
1095 bool do_numainfo __maybe_unused;
1096
1097 do_softlimit = mem_cgroup_event_ratelimit(memcg,
1098 MEM_CGROUP_TARGET_SOFTLIMIT);
1099#if MAX_NUMNODES > 1
1100 do_numainfo = mem_cgroup_event_ratelimit(memcg,
1101 MEM_CGROUP_TARGET_NUMAINFO);
1102#endif
1103 preempt_enable();
1104
1105 mem_cgroup_threshold(memcg);
1106 if (unlikely(do_softlimit))
1107 mem_cgroup_update_tree(memcg, page);
1108#if MAX_NUMNODES > 1
1109 if (unlikely(do_numainfo))
1110 atomic_inc(&memcg->numainfo_events);
1111#endif
1112 } else
1113 preempt_enable();
1114}
1115
1116struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
1117{
1118 return mem_cgroup_from_css(
1119 cgroup_subsys_state(cont, mem_cgroup_subsys_id));
1120}
1121
1122struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
1123{
1124
1125
1126
1127
1128
1129 if (unlikely(!p))
1130 return NULL;
1131
1132 return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
1133}
1134
1135struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
1136{
1137 struct mem_cgroup *memcg = NULL;
1138
1139 if (!mm)
1140 return NULL;
1141
1142
1143
1144
1145
1146 rcu_read_lock();
1147 do {
1148 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1149 if (unlikely(!memcg))
1150 break;
1151 } while (!css_tryget(&memcg->css));
1152 rcu_read_unlock();
1153 return memcg;
1154}
1155
1156
1157
1158
1159
1160
1161
1162static struct mem_cgroup *__mem_cgroup_iter_next(struct mem_cgroup *root,
1163 struct mem_cgroup *last_visited)
1164{
1165 struct cgroup *prev_cgroup, *next_cgroup;
1166
1167
1168
1169
1170
1171 if (!last_visited)
1172 return root;
1173
1174 prev_cgroup = (last_visited == root) ? NULL
1175 : last_visited->css.cgroup;
1176skip_node:
1177 next_cgroup = cgroup_next_descendant_pre(
1178 prev_cgroup, root->css.cgroup);
1179
1180
1181
1182
1183
1184
1185
1186
1187 if (next_cgroup) {
1188 struct mem_cgroup *mem = mem_cgroup_from_cont(
1189 next_cgroup);
1190 if (css_tryget(&mem->css))
1191 return mem;
1192 else {
1193 prev_cgroup = next_cgroup;
1194 goto skip_node;
1195 }
1196 }
1197
1198 return NULL;
1199}
1200
1201static void mem_cgroup_iter_invalidate(struct mem_cgroup *root)
1202{
1203
1204
1205
1206
1207
1208 atomic_inc(&root->dead_count);
1209}
1210
1211static struct mem_cgroup *
1212mem_cgroup_iter_load(struct mem_cgroup_reclaim_iter *iter,
1213 struct mem_cgroup *root,
1214 int *sequence)
1215{
1216 struct mem_cgroup *position = NULL;
1217
1218
1219
1220
1221
1222
1223
1224
1225 *sequence = atomic_read(&root->dead_count);
1226 if (iter->last_dead_count == *sequence) {
1227 smp_rmb();
1228 position = iter->last_visited;
1229
1230
1231
1232
1233
1234
1235
1236 if (position && position != root &&
1237 !css_tryget(&position->css))
1238
1239 position = NULL;
1240 }
1241 return position;
1242}
1243
1244static void mem_cgroup_iter_update(struct mem_cgroup_reclaim_iter *iter,
1245 struct mem_cgroup *last_visited,
1246 struct mem_cgroup *new_position,
1247 struct mem_cgroup *root,
1248 int sequence)
1249{
1250
1251 if (last_visited && last_visited != root)
1252 css_put(&last_visited->css);
1253
1254
1255
1256
1257
1258
1259 iter->last_visited = new_position;
1260 smp_wmb();
1261 iter->last_dead_count = sequence;
1262}
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
1282 struct mem_cgroup *prev,
1283 struct mem_cgroup_reclaim_cookie *reclaim)
1284{
1285 struct mem_cgroup *memcg = NULL;
1286 struct mem_cgroup *last_visited = NULL;
1287
1288 if (mem_cgroup_disabled())
1289 return NULL;
1290
1291 if (!root)
1292 root = root_mem_cgroup;
1293
1294 if (prev && !reclaim)
1295 last_visited = prev;
1296
1297 if (!root->use_hierarchy && root != root_mem_cgroup) {
1298 if (prev)
1299 goto out_css_put;
1300 return root;
1301 }
1302
1303 rcu_read_lock();
1304 while (!memcg) {
1305 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
1306 int uninitialized_var(seq);
1307
1308 if (reclaim) {
1309 int nid = zone_to_nid(reclaim->zone);
1310 int zid = zone_idx(reclaim->zone);
1311 struct mem_cgroup_per_zone *mz;
1312
1313 mz = mem_cgroup_zoneinfo(root, nid, zid);
1314 iter = &mz->reclaim_iter[reclaim->priority];
1315 if (prev && reclaim->generation != iter->generation) {
1316 iter->last_visited = NULL;
1317 goto out_unlock;
1318 }
1319
1320 last_visited = mem_cgroup_iter_load(iter, root, &seq);
1321 }
1322
1323 memcg = __mem_cgroup_iter_next(root, last_visited);
1324
1325 if (reclaim) {
1326 mem_cgroup_iter_update(iter, last_visited, memcg, root,
1327 seq);
1328
1329 if (!memcg)
1330 iter->generation++;
1331 else if (!prev && memcg)
1332 reclaim->generation = iter->generation;
1333 }
1334
1335 if (prev && !memcg)
1336 goto out_unlock;
1337 }
1338out_unlock:
1339 rcu_read_unlock();
1340out_css_put:
1341 if (prev && prev != root)
1342 css_put(&prev->css);
1343
1344 return memcg;
1345}
1346
1347
1348
1349
1350
1351
1352void mem_cgroup_iter_break(struct mem_cgroup *root,
1353 struct mem_cgroup *prev)
1354{
1355 if (!root)
1356 root = root_mem_cgroup;
1357 if (prev && prev != root)
1358 css_put(&prev->css);
1359}
1360
1361
1362
1363
1364
1365
1366#define for_each_mem_cgroup_tree(iter, root) \
1367 for (iter = mem_cgroup_iter(root, NULL, NULL); \
1368 iter != NULL; \
1369 iter = mem_cgroup_iter(root, iter, NULL))
1370
1371#define for_each_mem_cgroup(iter) \
1372 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
1373 iter != NULL; \
1374 iter = mem_cgroup_iter(NULL, iter, NULL))
1375
1376void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
1377{
1378 struct mem_cgroup *memcg;
1379
1380 rcu_read_lock();
1381 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1382 if (unlikely(!memcg))
1383 goto out;
1384
1385 switch (idx) {
1386 case PGFAULT:
1387 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1388 break;
1389 case PGMAJFAULT:
1390 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
1391 break;
1392 default:
1393 BUG();
1394 }
1395out:
1396 rcu_read_unlock();
1397}
1398EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1410 struct mem_cgroup *memcg)
1411{
1412 struct mem_cgroup_per_zone *mz;
1413 struct lruvec *lruvec;
1414
1415 if (mem_cgroup_disabled()) {
1416 lruvec = &zone->lruvec;
1417 goto out;
1418 }
1419
1420 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
1421 lruvec = &mz->lruvec;
1422out:
1423
1424
1425
1426
1427
1428 if (unlikely(lruvec->zone != zone))
1429 lruvec->zone = zone;
1430 return lruvec;
1431}
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
1453{
1454 struct mem_cgroup_per_zone *mz;
1455 struct mem_cgroup *memcg;
1456 struct page_cgroup *pc;
1457 struct lruvec *lruvec;
1458
1459 if (mem_cgroup_disabled()) {
1460 lruvec = &zone->lruvec;
1461 goto out;
1462 }
1463
1464 pc = lookup_page_cgroup(page);
1465 memcg = pc->mem_cgroup;
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
1477 pc->mem_cgroup = memcg = root_mem_cgroup;
1478
1479 mz = page_cgroup_zoneinfo(memcg, page);
1480 lruvec = &mz->lruvec;
1481out:
1482
1483
1484
1485
1486
1487 if (unlikely(lruvec->zone != zone))
1488 lruvec->zone = zone;
1489 return lruvec;
1490}
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1502 int nr_pages)
1503{
1504 struct mem_cgroup_per_zone *mz;
1505 unsigned long *lru_size;
1506
1507 if (mem_cgroup_disabled())
1508 return;
1509
1510 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1511 lru_size = mz->lru_size + lru;
1512 *lru_size += nr_pages;
1513 VM_BUG_ON((long)(*lru_size) < 0);
1514}
1515
1516
1517
1518
1519
1520bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1521 struct mem_cgroup *memcg)
1522{
1523 if (root_memcg == memcg)
1524 return true;
1525 if (!root_memcg->use_hierarchy || !memcg)
1526 return false;
1527 return cgroup_is_descendant(memcg->css.cgroup, root_memcg->css.cgroup);
1528}
1529
1530static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1531 struct mem_cgroup *memcg)
1532{
1533 bool ret;
1534
1535 rcu_read_lock();
1536 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
1537 rcu_read_unlock();
1538 return ret;
1539}
1540
1541int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
1542{
1543 int ret;
1544 struct mem_cgroup *curr = NULL;
1545 struct task_struct *p;
1546
1547 p = find_lock_task_mm(task);
1548 if (p) {
1549 curr = try_get_mem_cgroup_from_mm(p->mm);
1550 task_unlock(p);
1551 } else {
1552
1553
1554
1555
1556
1557 task_lock(task);
1558 curr = mem_cgroup_from_task(task);
1559 if (curr)
1560 css_get(&curr->css);
1561 task_unlock(task);
1562 }
1563 if (!curr)
1564 return 0;
1565
1566
1567
1568
1569
1570
1571 ret = mem_cgroup_same_or_subtree(memcg, curr);
1572 css_put(&curr->css);
1573 return ret;
1574}
1575
1576int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
1577{
1578 unsigned long inactive_ratio;
1579 unsigned long inactive;
1580 unsigned long active;
1581 unsigned long gb;
1582
1583 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1584 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
1585
1586 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1587 if (gb)
1588 inactive_ratio = int_sqrt(10 * gb);
1589 else
1590 inactive_ratio = 1;
1591
1592 return inactive * inactive_ratio < active;
1593}
1594
1595#define mem_cgroup_from_counter(counter, member) \
1596 container_of(counter, struct mem_cgroup, member)
1597
1598
1599
1600
1601
1602
1603
1604
1605static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
1606{
1607 unsigned long margin = 0;
1608 unsigned long count;
1609 unsigned long limit;
1610
1611 count = page_counter_read(&memcg->memory);
1612 limit = ACCESS_ONCE(memcg->memory.limit);
1613 if (count < limit)
1614 margin = limit - count;
1615
1616 if (do_swap_account) {
1617 count = page_counter_read(&memcg->memsw);
1618 limit = ACCESS_ONCE(memcg->memsw.limit);
1619 if (count <= limit)
1620 margin = min(margin, limit - count);
1621 }
1622
1623 return margin;
1624}
1625
1626int mem_cgroup_swappiness(struct mem_cgroup *memcg)
1627{
1628 struct cgroup *cgrp = memcg->css.cgroup;
1629
1630
1631 if (cgrp->parent == NULL)
1632 return vm_swappiness;
1633
1634 return memcg->swappiness;
1635}
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653atomic_t memcg_moving __read_mostly;
1654
1655static void mem_cgroup_start_move(struct mem_cgroup *memcg)
1656{
1657 atomic_inc(&memcg_moving);
1658 atomic_inc(&memcg->moving_account);
1659 synchronize_rcu();
1660}
1661
1662static void mem_cgroup_end_move(struct mem_cgroup *memcg)
1663{
1664
1665
1666
1667
1668 if (memcg) {
1669 atomic_dec(&memcg_moving);
1670 atomic_dec(&memcg->moving_account);
1671 }
1672}
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
1687{
1688 VM_BUG_ON(!rcu_read_lock_held());
1689 return atomic_read(&memcg->moving_account) > 0;
1690}
1691
1692static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
1693{
1694 struct mem_cgroup *from;
1695 struct mem_cgroup *to;
1696 bool ret = false;
1697
1698
1699
1700
1701 spin_lock(&mc.lock);
1702 from = mc.from;
1703 to = mc.to;
1704 if (!from)
1705 goto unlock;
1706
1707 ret = mem_cgroup_same_or_subtree(memcg, from)
1708 || mem_cgroup_same_or_subtree(memcg, to);
1709unlock:
1710 spin_unlock(&mc.lock);
1711 return ret;
1712}
1713
1714static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
1715{
1716 if (mc.moving_task && current != mc.moving_task) {
1717 if (mem_cgroup_under_move(memcg)) {
1718 DEFINE_WAIT(wait);
1719 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1720
1721 if (mc.moving_task)
1722 schedule();
1723 finish_wait(&mc.waitq, &wait);
1724 return true;
1725 }
1726 }
1727 return false;
1728}
1729
1730
1731
1732
1733
1734
1735
1736static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1737 unsigned long *flags)
1738{
1739 spin_lock_irqsave(&memcg->move_lock, *flags);
1740}
1741
1742static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1743 unsigned long *flags)
1744{
1745 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1746}
1747
1748#define K(x) ((x) << (PAGE_SHIFT-10))
1749
1750
1751
1752
1753
1754
1755
1756
1757void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1758{
1759 struct cgroup *task_cgrp;
1760 struct cgroup *mem_cgrp;
1761
1762
1763
1764
1765
1766 static char memcg_name[PATH_MAX];
1767 int ret;
1768 struct mem_cgroup *iter;
1769 unsigned int i;
1770
1771 if (!p)
1772 return;
1773
1774 rcu_read_lock();
1775
1776 mem_cgrp = memcg->css.cgroup;
1777 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1778
1779 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1780 if (ret < 0) {
1781
1782
1783
1784
1785 rcu_read_unlock();
1786 goto done;
1787 }
1788 rcu_read_unlock();
1789
1790 pr_info("Task in %s killed", memcg_name);
1791
1792 rcu_read_lock();
1793 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1794 if (ret < 0) {
1795 rcu_read_unlock();
1796 goto done;
1797 }
1798 rcu_read_unlock();
1799
1800
1801
1802
1803 pr_cont(" as a result of limit of %s\n", memcg_name);
1804done:
1805
1806 pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n",
1807 K((u64)page_counter_read(&memcg->memory)),
1808 K((u64)memcg->memory.limit), memcg->memory.failcnt);
1809 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n",
1810 K((u64)page_counter_read(&memcg->memsw)),
1811 K((u64)memcg->memsw.limit), memcg->memsw.failcnt);
1812 pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n",
1813 K((u64)page_counter_read(&memcg->kmem)),
1814 K((u64)memcg->kmem.limit), memcg->kmem.failcnt);
1815
1816 for_each_mem_cgroup_tree(iter, memcg) {
1817 pr_info("Memory cgroup stats");
1818
1819 rcu_read_lock();
1820 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1821 if (!ret)
1822 pr_cont(" for %s", memcg_name);
1823 rcu_read_unlock();
1824 pr_cont(":");
1825
1826 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1827 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1828 continue;
1829 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1830 K(mem_cgroup_read_stat(iter, i)));
1831 }
1832
1833 for (i = 0; i < NR_LRU_LISTS; i++)
1834 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1835 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1836
1837 pr_cont("\n");
1838 }
1839}
1840
1841
1842
1843
1844
1845static int mem_cgroup_count_children(struct mem_cgroup *memcg)
1846{
1847 int num = 0;
1848 struct mem_cgroup *iter;
1849
1850 for_each_mem_cgroup_tree(iter, memcg)
1851 num++;
1852 return num;
1853}
1854
1855
1856
1857
1858static unsigned long mem_cgroup_get_limit(struct mem_cgroup *memcg)
1859{
1860 unsigned long limit;
1861
1862 limit = memcg->memory.limit;
1863 if (mem_cgroup_swappiness(memcg)) {
1864 unsigned long memsw_limit;
1865
1866 memsw_limit = memcg->memsw.limit;
1867 limit = min(limit + total_swap_pages, memsw_limit);
1868 }
1869 return limit;
1870}
1871
1872static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1873 int order)
1874{
1875 struct mem_cgroup *iter;
1876 unsigned long chosen_points = 0;
1877 unsigned long totalpages;
1878 unsigned int points = 0;
1879 struct task_struct *chosen = NULL;
1880
1881
1882
1883
1884
1885
1886 if (fatal_signal_pending(current) || task_will_free_mem(current)) {
1887 set_thread_flag(TIF_MEMDIE);
1888 return;
1889 }
1890
1891 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
1892 totalpages = mem_cgroup_get_limit(memcg) ? : 1;
1893 for_each_mem_cgroup_tree(iter, memcg) {
1894 struct cgroup *cgroup = iter->css.cgroup;
1895 struct cgroup_iter it;
1896 struct task_struct *task;
1897
1898 cgroup_iter_start(cgroup, &it);
1899 while ((task = cgroup_iter_next(cgroup, &it))) {
1900 switch (oom_scan_process_thread(task, totalpages, NULL,
1901 false)) {
1902 case OOM_SCAN_SELECT:
1903 if (chosen)
1904 put_task_struct(chosen);
1905 chosen = task;
1906 chosen_points = ULONG_MAX;
1907 get_task_struct(chosen);
1908
1909 case OOM_SCAN_CONTINUE:
1910 continue;
1911 case OOM_SCAN_ABORT:
1912 cgroup_iter_end(cgroup, &it);
1913 mem_cgroup_iter_break(memcg, iter);
1914 if (chosen)
1915 put_task_struct(chosen);
1916 return;
1917 case OOM_SCAN_OK:
1918 break;
1919 };
1920 points = oom_badness(task, memcg, NULL, totalpages);
1921 if (points > chosen_points) {
1922 if (chosen)
1923 put_task_struct(chosen);
1924 chosen = task;
1925 chosen_points = points;
1926 get_task_struct(chosen);
1927 }
1928 }
1929 cgroup_iter_end(cgroup, &it);
1930 }
1931
1932 if (!chosen)
1933 return;
1934 points = chosen_points * 1000 / totalpages;
1935 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1936 NULL, "Memory cgroup out of memory");
1937}
1938
1939static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1940 gfp_t gfp_mask,
1941 unsigned long flags)
1942{
1943 unsigned long total = 0;
1944 bool noswap = false;
1945 int loop;
1946
1947 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1948 noswap = true;
1949 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1950 noswap = true;
1951
1952 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1953 if (loop)
1954 drain_all_stock_async(memcg);
1955 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1956
1957
1958
1959
1960
1961 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1962 break;
1963 if (mem_cgroup_margin(memcg))
1964 break;
1965
1966
1967
1968
1969 if (loop && !total)
1970 break;
1971 }
1972 return total;
1973}
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
1986 int nid, bool noswap)
1987{
1988 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
1989 return true;
1990 if (noswap || !total_swap_pages)
1991 return false;
1992 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
1993 return true;
1994 return false;
1995
1996}
1997#if MAX_NUMNODES > 1
1998
1999
2000
2001
2002
2003
2004
2005static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
2006{
2007 int nid;
2008
2009
2010
2011
2012 if (!atomic_read(&memcg->numainfo_events))
2013 return;
2014 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
2015 return;
2016
2017
2018 memcg->scan_nodes = node_states[N_MEMORY];
2019
2020 for_each_node_mask(nid, node_states[N_MEMORY]) {
2021
2022 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
2023 node_clear(nid, memcg->scan_nodes);
2024 }
2025
2026 atomic_set(&memcg->numainfo_events, 0);
2027 atomic_set(&memcg->numainfo_updating, 0);
2028}
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
2043{
2044 int node;
2045
2046 mem_cgroup_may_update_nodemask(memcg);
2047 node = memcg->last_scanned_node;
2048
2049 node = next_node(node, memcg->scan_nodes);
2050 if (node == MAX_NUMNODES)
2051 node = first_node(memcg->scan_nodes);
2052
2053
2054
2055
2056
2057
2058 if (unlikely(node == MAX_NUMNODES))
2059 node = numa_node_id();
2060
2061 memcg->last_scanned_node = node;
2062 return node;
2063}
2064
2065
2066
2067
2068
2069
2070
2071static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
2072{
2073 int nid;
2074
2075
2076
2077
2078
2079 if (!nodes_empty(memcg->scan_nodes)) {
2080 for (nid = first_node(memcg->scan_nodes);
2081 nid < MAX_NUMNODES;
2082 nid = next_node(nid, memcg->scan_nodes)) {
2083
2084 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
2085 return true;
2086 }
2087 }
2088
2089
2090
2091 for_each_node_state(nid, N_MEMORY) {
2092 if (node_isset(nid, memcg->scan_nodes))
2093 continue;
2094 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
2095 return true;
2096 }
2097 return false;
2098}
2099
2100#else
2101int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
2102{
2103 return 0;
2104}
2105
2106static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
2107{
2108 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
2109}
2110#endif
2111
2112static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
2113 struct zone *zone,
2114 gfp_t gfp_mask,
2115 unsigned long *total_scanned)
2116{
2117 struct mem_cgroup *victim = NULL;
2118 int total = 0;
2119 int loop = 0;
2120 unsigned long excess;
2121 unsigned long nr_scanned;
2122 struct mem_cgroup_reclaim_cookie reclaim = {
2123 .zone = zone,
2124 .priority = 0,
2125 };
2126
2127 excess = soft_limit_excess(root_memcg);
2128
2129 while (1) {
2130 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
2131 if (!victim) {
2132 loop++;
2133 if (loop >= 2) {
2134
2135
2136
2137
2138
2139 if (!total)
2140 break;
2141
2142
2143
2144
2145
2146
2147 if (total >= (excess >> 2) ||
2148 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
2149 break;
2150 }
2151 continue;
2152 }
2153 if (!mem_cgroup_reclaimable(victim, false))
2154 continue;
2155 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
2156 zone, &nr_scanned);
2157 *total_scanned += nr_scanned;
2158 if (!soft_limit_excess(root_memcg))
2159 break;
2160 }
2161 mem_cgroup_iter_break(root_memcg, victim);
2162 return total;
2163}
2164
2165static DEFINE_SPINLOCK(memcg_oom_lock);
2166
2167
2168
2169
2170
2171static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
2172{
2173 struct mem_cgroup *iter, *failed = NULL;
2174
2175 spin_lock(&memcg_oom_lock);
2176
2177 for_each_mem_cgroup_tree(iter, memcg) {
2178 if (iter->oom_lock) {
2179
2180
2181
2182
2183 failed = iter;
2184 mem_cgroup_iter_break(memcg, iter);
2185 break;
2186 } else
2187 iter->oom_lock = true;
2188 }
2189
2190 if (failed) {
2191
2192
2193
2194
2195 for_each_mem_cgroup_tree(iter, memcg) {
2196 if (iter == failed) {
2197 mem_cgroup_iter_break(memcg, iter);
2198 break;
2199 }
2200 iter->oom_lock = false;
2201 }
2202 }
2203
2204 spin_unlock(&memcg_oom_lock);
2205
2206 return !failed;
2207}
2208
2209static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
2210{
2211 struct mem_cgroup *iter;
2212
2213 spin_lock(&memcg_oom_lock);
2214 for_each_mem_cgroup_tree(iter, memcg)
2215 iter->oom_lock = false;
2216 spin_unlock(&memcg_oom_lock);
2217}
2218
2219static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
2220{
2221 struct mem_cgroup *iter;
2222
2223 for_each_mem_cgroup_tree(iter, memcg)
2224 atomic_inc(&iter->under_oom);
2225}
2226
2227static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
2228{
2229 struct mem_cgroup *iter;
2230
2231
2232
2233
2234
2235
2236 for_each_mem_cgroup_tree(iter, memcg)
2237 atomic_add_unless(&iter->under_oom, -1, 0);
2238}
2239
2240static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
2241
2242struct oom_wait_info {
2243 struct mem_cgroup *memcg;
2244 wait_queue_t wait;
2245};
2246
2247static int memcg_oom_wake_function(wait_queue_t *wait,
2248 unsigned mode, int sync, void *arg)
2249{
2250 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2251 struct mem_cgroup *oom_wait_memcg;
2252 struct oom_wait_info *oom_wait_info;
2253
2254 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
2255 oom_wait_memcg = oom_wait_info->memcg;
2256
2257
2258
2259
2260
2261 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2262 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
2263 return 0;
2264 return autoremove_wake_function(wait, mode, sync, arg);
2265}
2266
2267static void memcg_wakeup_oom(struct mem_cgroup *memcg)
2268{
2269 atomic_inc(&memcg->oom_wakeups);
2270
2271 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
2272}
2273
2274static void memcg_oom_recover(struct mem_cgroup *memcg)
2275{
2276 if (memcg && atomic_read(&memcg->under_oom))
2277 memcg_wakeup_oom(memcg);
2278}
2279
2280static void mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
2281{
2282 if (!current->memcg_oom.may_oom)
2283 return;
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298 css_get(&memcg->css);
2299 current->memcg_oom.memcg = memcg;
2300 current->memcg_oom.gfp_mask = mask;
2301 current->memcg_oom.order = order;
2302}
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321bool mem_cgroup_oom_synchronize(bool handle)
2322{
2323 struct mem_cgroup *memcg = current->memcg_oom.memcg;
2324 struct oom_wait_info owait;
2325 bool locked;
2326
2327
2328 if (!memcg)
2329 return false;
2330
2331 if (!handle)
2332 goto cleanup;
2333
2334 owait.memcg = memcg;
2335 owait.wait.flags = 0;
2336 owait.wait.func = memcg_oom_wake_function;
2337 owait.wait.private = current;
2338 INIT_LIST_HEAD(&owait.wait.task_list);
2339
2340 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
2341 mem_cgroup_mark_under_oom(memcg);
2342
2343 locked = mem_cgroup_oom_trylock(memcg);
2344
2345 if (locked)
2346 mem_cgroup_oom_notify(memcg);
2347
2348 if (locked && !memcg->oom_kill_disable) {
2349 mem_cgroup_unmark_under_oom(memcg);
2350 finish_wait(&memcg_oom_waitq, &owait.wait);
2351 mem_cgroup_out_of_memory(memcg, current->memcg_oom.gfp_mask,
2352 current->memcg_oom.order);
2353 } else {
2354 schedule();
2355 mem_cgroup_unmark_under_oom(memcg);
2356 finish_wait(&memcg_oom_waitq, &owait.wait);
2357 }
2358
2359 if (locked) {
2360 mem_cgroup_oom_unlock(memcg);
2361
2362
2363
2364
2365
2366 memcg_oom_recover(memcg);
2367 }
2368cleanup:
2369 current->memcg_oom.memcg = NULL;
2370 css_put(&memcg->css);
2371 return true;
2372}
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398void __mem_cgroup_begin_update_page_stat(struct page *page,
2399 bool *locked, unsigned long *flags)
2400{
2401 struct mem_cgroup *memcg;
2402 struct page_cgroup *pc;
2403
2404 pc = lookup_page_cgroup(page);
2405again:
2406 memcg = pc->mem_cgroup;
2407 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2408 return;
2409
2410
2411
2412
2413
2414
2415 if (!mem_cgroup_stolen(memcg))
2416 return;
2417
2418 move_lock_mem_cgroup(memcg, flags);
2419 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2420 move_unlock_mem_cgroup(memcg, flags);
2421 goto again;
2422 }
2423 *locked = true;
2424}
2425
2426void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2427{
2428 struct page_cgroup *pc = lookup_page_cgroup(page);
2429
2430
2431
2432
2433
2434
2435 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2436}
2437
2438void mem_cgroup_update_page_stat(struct page *page,
2439 enum mem_cgroup_page_stat_item idx, int val)
2440{
2441 struct mem_cgroup *memcg;
2442 struct page_cgroup *pc = lookup_page_cgroup(page);
2443 unsigned long uninitialized_var(flags);
2444
2445 if (mem_cgroup_disabled())
2446 return;
2447
2448 memcg = pc->mem_cgroup;
2449 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2450 return;
2451
2452 switch (idx) {
2453 case MEMCG_NR_FILE_MAPPED:
2454 idx = MEM_CGROUP_STAT_FILE_MAPPED;
2455 break;
2456 default:
2457 BUG();
2458 }
2459
2460 this_cpu_add(memcg->stat->count[idx], val);
2461}
2462
2463
2464
2465
2466
2467#define CHARGE_BATCH 32U
2468struct memcg_stock_pcp {
2469 struct mem_cgroup *cached;
2470 unsigned int nr_pages;
2471 struct work_struct work;
2472 unsigned long flags;
2473#define FLUSHING_CACHED_CHARGE 0
2474};
2475static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
2476static DEFINE_MUTEX(percpu_charge_mutex);
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
2490{
2491 struct memcg_stock_pcp *stock;
2492 bool ret = false;
2493
2494 if (nr_pages > CHARGE_BATCH)
2495 return ret;
2496
2497 stock = &get_cpu_var(memcg_stock);
2498 if (memcg == stock->cached && stock->nr_pages >= nr_pages) {
2499 stock->nr_pages -= nr_pages;
2500 ret = true;
2501 }
2502 put_cpu_var(memcg_stock);
2503 return ret;
2504}
2505
2506
2507
2508
2509static void drain_stock(struct memcg_stock_pcp *stock)
2510{
2511 struct mem_cgroup *old = stock->cached;
2512
2513 if (stock->nr_pages) {
2514 page_counter_uncharge(&old->memory, stock->nr_pages);
2515 if (do_swap_account)
2516 page_counter_uncharge(&old->memsw, stock->nr_pages);
2517 stock->nr_pages = 0;
2518 }
2519 stock->cached = NULL;
2520}
2521
2522
2523
2524
2525
2526static void drain_local_stock(struct work_struct *dummy)
2527{
2528 struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock);
2529 drain_stock(stock);
2530 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
2531}
2532
2533static void __init memcg_stock_init(void)
2534{
2535 int cpu;
2536
2537 for_each_possible_cpu(cpu) {
2538 struct memcg_stock_pcp *stock =
2539 &per_cpu(memcg_stock, cpu);
2540 INIT_WORK(&stock->work, drain_local_stock);
2541 }
2542}
2543
2544
2545
2546
2547
2548static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
2549{
2550 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2551
2552 if (stock->cached != memcg) {
2553 drain_stock(stock);
2554 stock->cached = memcg;
2555 }
2556 stock->nr_pages += nr_pages;
2557 put_cpu_var(memcg_stock);
2558}
2559
2560
2561
2562
2563
2564
2565static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
2566{
2567 int cpu, curcpu;
2568
2569
2570 get_online_cpus();
2571 curcpu = get_cpu();
2572 for_each_online_cpu(cpu) {
2573 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
2574 struct mem_cgroup *memcg;
2575
2576 memcg = stock->cached;
2577 if (!memcg || !stock->nr_pages)
2578 continue;
2579 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
2580 continue;
2581 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2582 if (cpu == curcpu)
2583 drain_local_stock(&stock->work);
2584 else
2585 schedule_work_on(cpu, &stock->work);
2586 }
2587 }
2588 put_cpu();
2589
2590 if (!sync)
2591 goto out;
2592
2593 for_each_online_cpu(cpu) {
2594 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
2595 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
2596 flush_work(&stock->work);
2597 }
2598out:
2599 put_online_cpus();
2600}
2601
2602
2603
2604
2605
2606
2607static void drain_all_stock_async(struct mem_cgroup *root_memcg)
2608{
2609
2610
2611
2612 if (!mutex_trylock(&percpu_charge_mutex))
2613 return;
2614 drain_all_stock(root_memcg, false);
2615 mutex_unlock(&percpu_charge_mutex);
2616}
2617
2618
2619static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
2620{
2621
2622 mutex_lock(&percpu_charge_mutex);
2623 drain_all_stock(root_memcg, true);
2624 mutex_unlock(&percpu_charge_mutex);
2625}
2626
2627
2628
2629
2630
2631static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
2632{
2633 int i;
2634
2635 spin_lock(&memcg->pcp_counter_lock);
2636 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
2637 long x = per_cpu(memcg->stat->count[i], cpu);
2638
2639 per_cpu(memcg->stat->count[i], cpu) = 0;
2640 memcg->nocpu_base.count[i] += x;
2641 }
2642 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
2643 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
2644
2645 per_cpu(memcg->stat->events[i], cpu) = 0;
2646 memcg->nocpu_base.events[i] += x;
2647 }
2648 spin_unlock(&memcg->pcp_counter_lock);
2649}
2650
2651static int memcg_cpu_hotplug_callback(struct notifier_block *nb,
2652 unsigned long action,
2653 void *hcpu)
2654{
2655 int cpu = (unsigned long)hcpu;
2656 struct memcg_stock_pcp *stock;
2657 struct mem_cgroup *iter;
2658
2659 if (action == CPU_ONLINE)
2660 return NOTIFY_OK;
2661
2662 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
2663 return NOTIFY_OK;
2664
2665 for_each_mem_cgroup(iter)
2666 mem_cgroup_drain_pcp_counter(iter, cpu);
2667
2668 stock = &per_cpu(memcg_stock, cpu);
2669 drain_stock(stock);
2670 return NOTIFY_OK;
2671}
2672
2673
2674
2675enum {
2676 CHARGE_OK,
2677 CHARGE_RETRY,
2678 CHARGE_NOMEM,
2679 CHARGE_WOULDBLOCK,
2680};
2681
2682static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
2683 unsigned int nr_pages, unsigned int min_pages,
2684 bool invoke_oom)
2685{
2686 struct mem_cgroup *mem_over_limit;
2687 struct page_counter *counter;
2688 unsigned long flags = 0;
2689 int ret = -ENOMEM;
2690
2691 if (likely(page_counter_try_charge(&memcg->memory, nr_pages,
2692 &counter))) {
2693 if (!do_swap_account)
2694 return CHARGE_OK;
2695 if (likely(page_counter_try_charge(&memcg->memsw, nr_pages,
2696 &counter)))
2697 return CHARGE_OK;
2698
2699 page_counter_uncharge(&memcg->memory, nr_pages);
2700 mem_over_limit = mem_cgroup_from_counter(counter, memsw);
2701 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2702 } else
2703 mem_over_limit = mem_cgroup_from_counter(counter, memory);
2704
2705
2706
2707
2708 if (nr_pages > min_pages)
2709 return CHARGE_RETRY;
2710
2711 if (!(gfp_mask & __GFP_WAIT))
2712 return CHARGE_WOULDBLOCK;
2713
2714 if (gfp_mask & __GFP_NORETRY)
2715 return CHARGE_NOMEM;
2716
2717 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
2718 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
2719 return CHARGE_RETRY;
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
2730 return CHARGE_RETRY;
2731
2732
2733
2734
2735
2736 if (mem_cgroup_wait_acct_move(mem_over_limit))
2737 return CHARGE_RETRY;
2738
2739 if (invoke_oom)
2740 mem_cgroup_oom(mem_over_limit, gfp_mask,
2741 get_order(nr_pages * PAGE_SIZE));
2742
2743 return CHARGE_NOMEM;
2744}
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767static int __mem_cgroup_try_charge(struct mm_struct *mm,
2768 gfp_t gfp_mask,
2769 unsigned int nr_pages,
2770 struct mem_cgroup **ptr,
2771 bool oom)
2772{
2773 unsigned int batch = max(CHARGE_BATCH, nr_pages);
2774 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2775 struct mem_cgroup *memcg = NULL;
2776 int ret;
2777
2778
2779
2780
2781
2782
2783 if (unlikely(test_thread_flag(TIF_MEMDIE)
2784 || fatal_signal_pending(current)))
2785 goto bypass;
2786
2787
2788
2789
2790
2791
2792
2793 if (unlikely(current->flags & PF_MEMALLOC))
2794 goto bypass;
2795
2796 if (unlikely(task_in_memcg_oom(current)))
2797 goto nomem;
2798
2799 if (gfp_mask & __GFP_NOFAIL)
2800 oom = false;
2801
2802
2803
2804
2805
2806
2807
2808 if (!*ptr && !mm)
2809 *ptr = root_mem_cgroup;
2810again:
2811 if (*ptr) {
2812 memcg = *ptr;
2813 if (mem_cgroup_is_root(memcg))
2814 goto done;
2815 if (consume_stock(memcg, nr_pages))
2816 goto done;
2817 css_get(&memcg->css);
2818 } else {
2819 struct task_struct *p;
2820
2821 rcu_read_lock();
2822 p = rcu_dereference(mm->owner);
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833 memcg = mem_cgroup_from_task(p);
2834 if (!memcg)
2835 memcg = root_mem_cgroup;
2836 if (mem_cgroup_is_root(memcg)) {
2837 rcu_read_unlock();
2838 goto done;
2839 }
2840 if (consume_stock(memcg, nr_pages)) {
2841
2842
2843
2844
2845
2846
2847
2848
2849 rcu_read_unlock();
2850 goto done;
2851 }
2852
2853 if (!css_tryget(&memcg->css)) {
2854 rcu_read_unlock();
2855 goto again;
2856 }
2857 rcu_read_unlock();
2858 }
2859
2860 do {
2861 bool invoke_oom = oom && !nr_oom_retries;
2862
2863
2864 if (fatal_signal_pending(current)) {
2865 css_put(&memcg->css);
2866 goto bypass;
2867 }
2868
2869 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch,
2870 nr_pages, invoke_oom);
2871 switch (ret) {
2872 case CHARGE_OK:
2873 break;
2874 case CHARGE_RETRY:
2875 batch = nr_pages;
2876 css_put(&memcg->css);
2877 memcg = NULL;
2878 goto again;
2879 case CHARGE_WOULDBLOCK:
2880 css_put(&memcg->css);
2881 goto nomem;
2882 case CHARGE_NOMEM:
2883 if (!oom || invoke_oom) {
2884 css_put(&memcg->css);
2885 goto nomem;
2886 }
2887 nr_oom_retries--;
2888 break;
2889 }
2890 } while (ret != CHARGE_OK);
2891
2892 if (batch > nr_pages)
2893 refill_stock(memcg, batch - nr_pages);
2894 css_put(&memcg->css);
2895done:
2896 *ptr = memcg;
2897 return 0;
2898nomem:
2899 if (!(gfp_mask & __GFP_NOFAIL)) {
2900 *ptr = NULL;
2901 return -ENOMEM;
2902 }
2903bypass:
2904 *ptr = root_mem_cgroup;
2905 return -EINTR;
2906}
2907
2908
2909
2910
2911
2912
2913static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
2914 unsigned int nr_pages)
2915{
2916 if (!mem_cgroup_is_root(memcg)) {
2917 page_counter_uncharge(&memcg->memory, nr_pages);
2918 if (do_swap_account)
2919 page_counter_uncharge(&memcg->memsw, nr_pages);
2920 }
2921}
2922
2923struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
2924
2925
2926
2927
2928
2929
2930static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2931{
2932
2933 if (!id)
2934 return NULL;
2935 return mem_cgroup_from_id(id);
2936}
2937
2938struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
2939{
2940 struct mem_cgroup *memcg = NULL;
2941 struct page_cgroup *pc;
2942 unsigned short id;
2943 swp_entry_t ent;
2944
2945 VM_BUG_ON_PAGE(!PageLocked(page), page);
2946
2947 pc = lookup_page_cgroup(page);
2948 lock_page_cgroup(pc);
2949 if (PageCgroupUsed(pc)) {
2950 memcg = pc->mem_cgroup;
2951 if (memcg && !css_tryget(&memcg->css))
2952 memcg = NULL;
2953 } else if (PageSwapCache(page)) {
2954 ent.val = page_private(page);
2955 id = lookup_swap_cgroup_id(ent);
2956 rcu_read_lock();
2957 memcg = mem_cgroup_lookup(id);
2958 if (memcg && !css_tryget(&memcg->css))
2959 memcg = NULL;
2960 rcu_read_unlock();
2961 }
2962 unlock_page_cgroup(pc);
2963 return memcg;
2964}
2965
2966static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
2967 struct page *page,
2968 unsigned int nr_pages,
2969 enum charge_type ctype,
2970 bool lrucare)
2971{
2972 struct page_cgroup *pc = lookup_page_cgroup(page);
2973 struct zone *uninitialized_var(zone);
2974 struct lruvec *lruvec;
2975 bool was_on_lru = false;
2976 bool anon;
2977
2978 lock_page_cgroup(pc);
2979 VM_BUG_ON_PAGE(PageCgroupUsed(pc), page);
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989 if (lrucare) {
2990 zone = page_zone(page);
2991 spin_lock_irq(&zone->lru_lock);
2992 if (PageLRU(page)) {
2993 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
2994 ClearPageLRU(page);
2995 del_page_from_lru_list(page, lruvec, page_lru(page));
2996 was_on_lru = true;
2997 }
2998 }
2999
3000 pc->mem_cgroup = memcg;
3001
3002
3003
3004
3005
3006
3007
3008 smp_wmb();
3009 SetPageCgroupUsed(pc);
3010
3011 if (lrucare) {
3012 if (was_on_lru) {
3013 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
3014 VM_BUG_ON_PAGE(PageLRU(page), page);
3015 SetPageLRU(page);
3016 add_page_to_lru_list(page, lruvec, page_lru(page));
3017 }
3018 spin_unlock_irq(&zone->lru_lock);
3019 }
3020
3021 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
3022 anon = true;
3023 else
3024 anon = false;
3025
3026 mem_cgroup_charge_statistics(memcg, page, anon, nr_pages);
3027 unlock_page_cgroup(pc);
3028
3029
3030
3031
3032
3033
3034 memcg_check_events(memcg, page);
3035}
3036
3037#ifdef CONFIG_MEMCG_KMEM
3038
3039
3040
3041
3042static DEFINE_MUTEX(memcg_slab_mutex);
3043
3044static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
3045{
3046 return !cgroup_memory_nokmem && !mem_cgroup_disabled() &&
3047 !mem_cgroup_is_root(memcg) &&
3048 memcg_kmem_is_active(memcg);
3049}
3050
3051
3052
3053
3054
3055static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
3056{
3057 struct kmem_cache *cachep;
3058
3059 VM_BUG_ON(p->is_root_cache);
3060 cachep = p->root_cache;
3061 return cache_from_memcg_idx(cachep, memcg_cache_id(p->memcg));
3062}
3063
3064#ifdef CONFIG_SLABINFO
3065static int mem_cgroup_slabinfo_read(struct cgroup *cont, struct cftype *cft,
3066 struct seq_file *m)
3067{
3068 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
3069 struct memcg_cache_params *params;
3070
3071 if (!memcg_can_account_kmem(memcg))
3072 return -EIO;
3073
3074 print_slabinfo_header(m);
3075
3076 mutex_lock(&memcg_slab_mutex);
3077 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
3078 cache_show(memcg_params_to_cache(params), m);
3079 mutex_unlock(&memcg_slab_mutex);
3080
3081 return 0;
3082}
3083#endif
3084
3085int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp,
3086 unsigned long nr_pages)
3087{
3088 struct page_counter *counter;
3089 struct mem_cgroup *_memcg;
3090 int ret = 0;
3091 bool may_oom;
3092
3093 if (!page_counter_try_charge(&memcg->kmem, nr_pages, &counter))
3094 return -ENOMEM;
3095
3096
3097
3098
3099
3100 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
3101
3102 _memcg = memcg;
3103 ret = __mem_cgroup_try_charge(NULL, gfp, nr_pages, &_memcg, may_oom);
3104
3105 if (ret == -EINTR) {
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121 page_counter_charge(&memcg->memory, nr_pages);
3122 if (do_swap_account)
3123 page_counter_charge(&memcg->memsw, nr_pages);
3124 ret = 0;
3125 } else if (ret)
3126 page_counter_uncharge(&memcg->kmem, nr_pages);
3127
3128 return ret;
3129}
3130
3131void memcg_uncharge_kmem(struct mem_cgroup *memcg,
3132 unsigned long nr_pages)
3133{
3134 page_counter_uncharge(&memcg->memory, nr_pages);
3135 if (do_swap_account)
3136 page_counter_uncharge(&memcg->memsw, nr_pages);
3137
3138
3139 if (page_counter_uncharge(&memcg->kmem, nr_pages))
3140 return;
3141
3142 if (memcg_kmem_test_and_clear_dead(memcg))
3143 mem_cgroup_put(memcg);
3144}
3145
3146
3147
3148
3149
3150
3151int memcg_cache_id(struct mem_cgroup *memcg)
3152{
3153 return memcg ? memcg->kmemcg_id : -1;
3154}
3155
3156
3157
3158
3159
3160
3161
3162
3163static int memcg_update_cache_sizes(struct mem_cgroup *memcg)
3164{
3165 int num, ret;
3166
3167 num = ida_simple_get(&kmem_limited_groups,
3168 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
3169 if (num < 0)
3170 return num;
3171
3172 mutex_lock(&memcg_slab_mutex);
3173 ret = memcg_update_all_caches(num+1);
3174 mutex_unlock(&memcg_slab_mutex);
3175 if (ret) {
3176 ida_simple_remove(&kmem_limited_groups, num);
3177 return ret;
3178 }
3179
3180 memcg->kmemcg_id = num;
3181 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
3182 return 0;
3183}
3184
3185static size_t memcg_caches_array_size(int num_groups)
3186{
3187 ssize_t size;
3188 if (num_groups <= 0)
3189 return 0;
3190
3191 size = 2 * num_groups;
3192 if (size < MEMCG_CACHES_MIN_SIZE)
3193 size = MEMCG_CACHES_MIN_SIZE;
3194 else if (size > MEMCG_CACHES_MAX_SIZE)
3195 size = MEMCG_CACHES_MAX_SIZE;
3196
3197 return size;
3198}
3199
3200
3201
3202
3203
3204
3205void memcg_update_array_size(int num)
3206{
3207 if (num > memcg_limited_groups_array_size)
3208 memcg_limited_groups_array_size = memcg_caches_array_size(num);
3209}
3210
3211int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
3212{
3213 struct memcg_cache_params *cur_params = s->memcg_params;
3214
3215 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
3216
3217
3218
3219
3220
3221 if (num_groups > memcg_limited_groups_array_size || !cur_params) {
3222 int i;
3223 struct memcg_cache_params *new_params;
3224 ssize_t size = memcg_caches_array_size(num_groups);
3225
3226 size *= sizeof(void *);
3227 size += sizeof(struct memcg_cache_params);
3228
3229 new_params = kzalloc(size, GFP_KERNEL);
3230 if (!new_params)
3231 return -ENOMEM;
3232
3233 new_params->is_root_cache = true;
3234
3235
3236 if (!cur_params)
3237 goto out;
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3249 if (!cur_params->memcg_caches[i])
3250 continue;
3251 new_params->memcg_caches[i] =
3252 cur_params->memcg_caches[i];
3253 }
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264out:
3265 rcu_assign_pointer(s->memcg_params, new_params);
3266 if (cur_params)
3267 kfree_rcu(cur_params, rcu_head);
3268 }
3269 return 0;
3270}
3271
3272char *memcg_create_cache_name(struct mem_cgroup *memcg,
3273 struct kmem_cache *root_cache)
3274{
3275 const char *name;
3276
3277
3278
3279
3280
3281
3282 lockdep_assert_held(&slab_mutex);
3283
3284 rcu_read_lock();
3285 name = cgroup_name(memcg->css.cgroup);
3286 rcu_read_unlock();
3287 return kasprintf(GFP_KERNEL, "%s(%d:%s)", root_cache->name,
3288 memcg_cache_id(memcg), name);
3289}
3290
3291int memcg_alloc_cache_params(struct mem_cgroup *memcg, struct kmem_cache *s,
3292 struct kmem_cache *root_cache)
3293{
3294 size_t size = sizeof(struct memcg_cache_params);
3295
3296 if (!memcg_kmem_enabled())
3297 return 0;
3298
3299 if (!memcg)
3300 size += memcg_limited_groups_array_size * sizeof(void *);
3301
3302 s->memcg_params = kzalloc(size, GFP_KERNEL);
3303 if (!s->memcg_params)
3304 return -ENOMEM;
3305
3306 if (memcg) {
3307 s->memcg_params->memcg = memcg;
3308 s->memcg_params->root_cache = root_cache;
3309 } else
3310 s->memcg_params->is_root_cache = true;
3311
3312 return 0;
3313}
3314
3315void memcg_free_cache_params(struct kmem_cache *s)
3316{
3317 kfree(s->memcg_params);
3318}
3319
3320static void memcg_kmem_create_cache(struct mem_cgroup *memcg,
3321 struct kmem_cache *root_cache)
3322{
3323 struct kmem_cache *cachep;
3324 int id;
3325
3326 lockdep_assert_held(&memcg_slab_mutex);
3327
3328 id = memcg_cache_id(memcg);
3329
3330
3331
3332
3333
3334
3335 if (cache_from_memcg_idx(root_cache, id))
3336 return;
3337
3338 cachep = kmem_cache_create_memcg(memcg, root_cache);
3339
3340
3341
3342
3343
3344 if (!cachep)
3345 return;
3346
3347 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
3348
3349
3350
3351
3352
3353
3354 smp_wmb();
3355
3356 BUG_ON(root_cache->memcg_params->memcg_caches[id]);
3357 root_cache->memcg_params->memcg_caches[id] = cachep;
3358
3359 mem_cgroup_get(memcg);
3360}
3361
3362static void memcg_kmem_destroy_cache(struct kmem_cache *cachep)
3363{
3364 struct kmem_cache *root_cache;
3365 struct mem_cgroup *memcg;
3366 int id;
3367
3368 lockdep_assert_held(&memcg_slab_mutex);
3369
3370 BUG_ON(is_root_cache(cachep));
3371
3372 root_cache = cachep->memcg_params->root_cache;
3373 memcg = cachep->memcg_params->memcg;
3374 id = memcg_cache_id(memcg);
3375
3376 BUG_ON(root_cache->memcg_params->memcg_caches[id] != cachep);
3377 root_cache->memcg_params->memcg_caches[id] = NULL;
3378
3379 list_del(&cachep->memcg_params->list);
3380
3381 kmem_cache_destroy(cachep);
3382
3383 mem_cgroup_put(memcg);
3384}
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405static inline void memcg_stop_kmem_account(void)
3406{
3407 VM_BUG_ON(!current->mm);
3408 current->memcg_kmem_skip_account++;
3409}
3410
3411static inline void memcg_resume_kmem_account(void)
3412{
3413 VM_BUG_ON(!current->mm);
3414 current->memcg_kmem_skip_account--;
3415}
3416
3417static DEFINE_MUTEX(memcg_limit_mutex);
3418
3419int __kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3420{
3421 struct kmem_cache *c;
3422 int i, failed = 0;
3423
3424 mutex_lock(&memcg_slab_mutex);
3425 for_each_memcg_cache_index(i) {
3426 c = cache_from_memcg_idx(s, i);
3427 if (!c)
3428 continue;
3429
3430 memcg_kmem_destroy_cache(c);
3431
3432 if (cache_from_memcg_idx(s, i))
3433 failed++;
3434 }
3435 mutex_unlock(&memcg_slab_mutex);
3436 return failed;
3437}
3438
3439static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3440{
3441 struct kmem_cache *cachep;
3442 struct memcg_cache_params *params, *tmp;
3443
3444 if (!memcg_kmem_is_active(memcg))
3445 return;
3446
3447 mutex_lock(&memcg_slab_mutex);
3448 list_for_each_entry_safe(params, tmp, &memcg->memcg_slab_caches, list) {
3449 cachep = memcg_params_to_cache(params);
3450 __kmemcg_cache_deactivate(cachep);
3451 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3452 memcg_kmem_destroy_cache(cachep);
3453 }
3454 mutex_unlock(&memcg_slab_mutex);
3455}
3456
3457struct create_work {
3458 struct mem_cgroup *memcg;
3459 struct kmem_cache *cachep;
3460 struct work_struct work;
3461};
3462
3463static void memcg_create_cache_work_func(struct work_struct *w)
3464{
3465 struct create_work *cw = container_of(w, struct create_work, work);
3466 struct mem_cgroup *memcg = cw->memcg;
3467 struct kmem_cache *cachep = cw->cachep;
3468
3469 mutex_lock(&memcg_slab_mutex);
3470 memcg_kmem_create_cache(memcg, cachep);
3471 mutex_unlock(&memcg_slab_mutex);
3472
3473 css_put(&memcg->css);
3474 kfree(cw);
3475}
3476
3477
3478
3479
3480static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3481 struct kmem_cache *cachep)
3482{
3483 struct create_work *cw;
3484
3485 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
3486 if (cw == NULL) {
3487 css_put(&memcg->css);
3488 return;
3489 }
3490
3491 cw->memcg = memcg;
3492 cw->cachep = cachep;
3493
3494 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3495 schedule_work(&cw->work);
3496}
3497
3498static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3499 struct kmem_cache *cachep)
3500{
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512 memcg_stop_kmem_account();
3513 __memcg_create_cache_enqueue(memcg, cachep);
3514 memcg_resume_kmem_account();
3515}
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3530 gfp_t gfp)
3531{
3532 struct mem_cgroup *memcg;
3533 struct kmem_cache *memcg_cachep;
3534
3535 VM_BUG_ON(!cachep->memcg_params);
3536 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3537
3538 if (cachep->flags & SLAB_ACCOUNT)
3539 gfp |= __GFP_ACCOUNT;
3540
3541 if (!(gfp & __GFP_ACCOUNT))
3542 return cachep;
3543
3544 if (!current->mm || current->memcg_kmem_skip_account)
3545 return cachep;
3546
3547 rcu_read_lock();
3548 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
3549
3550 if (!memcg_can_account_kmem(memcg))
3551 goto out;
3552
3553 memcg_cachep = cache_from_memcg_idx(cachep, memcg_cache_id(memcg));
3554 if (likely(memcg_cachep)) {
3555 cachep = memcg_cachep;
3556 goto out;
3557 }
3558
3559
3560 if (!css_tryget(&memcg->css))
3561 goto out;
3562 rcu_read_unlock();
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581 memcg_create_cache_enqueue(memcg, cachep);
3582 return cachep;
3583out:
3584 rcu_read_unlock();
3585 return cachep;
3586}
3587EXPORT_SYMBOL(__memcg_kmem_get_cache);
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603bool
3604__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3605{
3606 struct mem_cgroup *memcg;
3607 int ret;
3608
3609 *_memcg = NULL;
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635 if (!current->mm || current->memcg_kmem_skip_account)
3636 return true;
3637
3638 memcg = try_get_mem_cgroup_from_mm(current->mm);
3639
3640
3641
3642
3643
3644
3645 if (unlikely(!memcg))
3646 return true;
3647
3648 if (!memcg_can_account_kmem(memcg)) {
3649 css_put(&memcg->css);
3650 return true;
3651 }
3652
3653 ret = memcg_charge_kmem(memcg, gfp, 1 << order);
3654 if (!ret)
3655 *_memcg = memcg;
3656
3657 css_put(&memcg->css);
3658 return (ret == 0);
3659}
3660
3661void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3662 int order)
3663{
3664 struct page_cgroup *pc;
3665
3666 VM_BUG_ON(mem_cgroup_is_root(memcg));
3667
3668
3669 if (!page) {
3670 memcg_uncharge_kmem(memcg, 1 << order);
3671 return;
3672 }
3673
3674 pc = lookup_page_cgroup(page);
3675 lock_page_cgroup(pc);
3676 pc->mem_cgroup = memcg;
3677 SetPageCgroupUsed(pc);
3678 unlock_page_cgroup(pc);
3679}
3680
3681void __memcg_kmem_uncharge_pages(struct page *page, int order)
3682{
3683 struct mem_cgroup *memcg = NULL;
3684 struct page_cgroup *pc;
3685
3686
3687 pc = lookup_page_cgroup(page);
3688
3689
3690
3691
3692 if (!PageCgroupUsed(pc))
3693 return;
3694
3695 lock_page_cgroup(pc);
3696 if (PageCgroupUsed(pc)) {
3697 memcg = pc->mem_cgroup;
3698 ClearPageCgroupUsed(pc);
3699 }
3700 unlock_page_cgroup(pc);
3701
3702
3703
3704
3705
3706 if (!memcg)
3707 return;
3708
3709 VM_BUG_ON_PAGE(mem_cgroup_is_root(memcg), page);
3710 memcg_uncharge_kmem(memcg, 1 << order);
3711}
3712#else
3713static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3714{
3715}
3716#endif
3717
3718#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3719
3720#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
3721
3722
3723
3724
3725
3726
3727void mem_cgroup_split_huge_fixup(struct page *head)
3728{
3729 struct page_cgroup *head_pc = lookup_page_cgroup(head);
3730 struct page_cgroup *pc;
3731 struct mem_cgroup *memcg;
3732 int i;
3733
3734 if (mem_cgroup_disabled())
3735 return;
3736
3737 memcg = head_pc->mem_cgroup;
3738 for (i = 1; i < HPAGE_PMD_NR; i++) {
3739 pc = head_pc + i;
3740 pc->mem_cgroup = memcg;
3741 smp_wmb();
3742 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3743 }
3744 __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE],
3745 HPAGE_PMD_NR);
3746}
3747#endif
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764static int mem_cgroup_move_account(struct page *page,
3765 unsigned int nr_pages,
3766 struct page_cgroup *pc,
3767 struct mem_cgroup *from,
3768 struct mem_cgroup *to)
3769{
3770 unsigned long flags;
3771 int ret;
3772 bool anon = PageAnon(page);
3773
3774 VM_BUG_ON(from == to);
3775 VM_BUG_ON_PAGE(PageLRU(page), page);
3776
3777
3778
3779
3780
3781
3782 ret = -EBUSY;
3783 if (nr_pages > 1 && !PageTransHuge(page))
3784 goto out;
3785
3786 lock_page_cgroup(pc);
3787
3788 ret = -EINVAL;
3789 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3790 goto unlock;
3791
3792 move_lock_mem_cgroup(from, &flags);
3793
3794 if (!anon && page_mapped(page)) {
3795
3796 preempt_disable();
3797 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3798 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3799 preempt_enable();
3800 }
3801 mem_cgroup_charge_statistics(from, page, anon, -nr_pages);
3802
3803
3804 pc->mem_cgroup = to;
3805 mem_cgroup_charge_statistics(to, page, anon, nr_pages);
3806 move_unlock_mem_cgroup(from, &flags);
3807 ret = 0;
3808unlock:
3809 unlock_page_cgroup(pc);
3810
3811
3812
3813 memcg_check_events(to, page);
3814 memcg_check_events(from, page);
3815out:
3816 return ret;
3817}
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840static int mem_cgroup_move_parent(struct page *page,
3841 struct page_cgroup *pc,
3842 struct mem_cgroup *child)
3843{
3844 struct mem_cgroup *parent;
3845 unsigned int nr_pages;
3846 unsigned long uninitialized_var(flags);
3847 int ret;
3848
3849 VM_BUG_ON(mem_cgroup_is_root(child));
3850
3851 ret = -EBUSY;
3852 if (!get_page_unless_zero(page))
3853 goto out;
3854 if (isolate_lru_page(page))
3855 goto put;
3856
3857 nr_pages = hpage_nr_pages(page);
3858
3859 parent = parent_mem_cgroup(child);
3860
3861
3862
3863 if (!parent)
3864 parent = root_mem_cgroup;
3865
3866 if (nr_pages > 1) {
3867 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
3868 flags = compound_lock_irqsave(page);
3869 }
3870
3871 ret = mem_cgroup_move_account(page, nr_pages,
3872 pc, child, parent);
3873 if (!ret) {
3874
3875 page_counter_cancel(&child->memory, nr_pages);
3876 if (do_swap_account)
3877 page_counter_cancel(&child->memsw, nr_pages);
3878 }
3879
3880 if (nr_pages > 1)
3881 compound_unlock_irqrestore(page, flags);
3882 putback_lru_page(page);
3883put:
3884 put_page(page);
3885out:
3886 return ret;
3887}
3888
3889
3890
3891
3892
3893
3894
3895static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
3896 gfp_t gfp_mask, enum charge_type ctype)
3897{
3898 struct mem_cgroup *memcg = NULL;
3899 unsigned int nr_pages = 1;
3900 bool oom = true;
3901 int ret;
3902
3903 if (PageTransHuge(page)) {
3904 nr_pages <<= compound_order(page);
3905 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
3906
3907
3908
3909
3910 oom = false;
3911 }
3912
3913 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
3914 if (ret == -ENOMEM)
3915 return ret;
3916 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
3917 return 0;
3918}
3919
3920int mem_cgroup_newpage_charge(struct page *page,
3921 struct mm_struct *mm, gfp_t gfp_mask)
3922{
3923 if (mem_cgroup_disabled())
3924 return 0;
3925 VM_BUG_ON_PAGE(page_mapped(page), page);
3926 VM_BUG_ON_PAGE(page->mapping && !PageAnon(page), page);
3927 VM_BUG_ON(!mm);
3928 return mem_cgroup_charge_common(page, mm, gfp_mask,
3929 MEM_CGROUP_CHARGE_TYPE_ANON);
3930}
3931
3932
3933
3934
3935
3936
3937
3938static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3939 struct page *page,
3940 gfp_t mask,
3941 struct mem_cgroup **memcgp)
3942{
3943 struct mem_cgroup *memcg;
3944 struct page_cgroup *pc;
3945 int ret;
3946
3947 pc = lookup_page_cgroup(page);
3948
3949
3950
3951
3952
3953
3954
3955 if (PageCgroupUsed(pc))
3956 return 0;
3957 if (!do_swap_account)
3958 goto charge_cur_mm;
3959 memcg = try_get_mem_cgroup_from_page(page);
3960 if (!memcg)
3961 goto charge_cur_mm;
3962 *memcgp = memcg;
3963 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
3964 css_put(&memcg->css);
3965 if (ret == -EINTR)
3966 ret = 0;
3967 return ret;
3968charge_cur_mm:
3969 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3970 if (ret == -EINTR)
3971 ret = 0;
3972 return ret;
3973}
3974
3975int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3976 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3977{
3978 *memcgp = NULL;
3979 if (mem_cgroup_disabled())
3980 return 0;
3981
3982
3983
3984
3985
3986
3987 if (!PageSwapCache(page)) {
3988 int ret;
3989
3990 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3991 if (ret == -EINTR)
3992 ret = 0;
3993 return ret;
3994 }
3995 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3996}
3997
3998void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3999{
4000 if (mem_cgroup_disabled())
4001 return;
4002 if (!memcg)
4003 return;
4004 __mem_cgroup_cancel_charge(memcg, 1);
4005}
4006
4007static void
4008__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
4009 enum charge_type ctype)
4010{
4011 if (mem_cgroup_disabled())
4012 return;
4013 if (!memcg)
4014 return;
4015
4016 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
4017
4018
4019
4020
4021
4022
4023
4024 if (do_swap_account && PageSwapCache(page)) {
4025 swp_entry_t ent = {.val = page_private(page)};
4026 mem_cgroup_uncharge_swap(ent);
4027 }
4028}
4029
4030void mem_cgroup_commit_charge_swapin(struct page *page,
4031 struct mem_cgroup *memcg)
4032{
4033 __mem_cgroup_commit_charge_swapin(page, memcg,
4034 MEM_CGROUP_CHARGE_TYPE_ANON);
4035}
4036
4037int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
4038 gfp_t gfp_mask)
4039{
4040 struct mem_cgroup *memcg = NULL;
4041 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
4042 int ret;
4043
4044 if (mem_cgroup_disabled())
4045 return 0;
4046 if (PageCompound(page))
4047 return 0;
4048
4049 if (!PageSwapCache(page))
4050 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
4051 else {
4052 ret = __mem_cgroup_try_charge_swapin(mm, page,
4053 gfp_mask, &memcg);
4054 if (!ret)
4055 __mem_cgroup_commit_charge_swapin(page, memcg, type);
4056 }
4057 return ret;
4058}
4059
4060static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
4061 unsigned int nr_pages,
4062 const enum charge_type ctype)
4063{
4064 struct memcg_batch_info *batch = NULL;
4065 bool uncharge_memsw = true;
4066
4067
4068 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
4069 uncharge_memsw = false;
4070
4071 batch = ¤t->memcg_batch;
4072
4073
4074
4075
4076
4077 if (!batch->memcg)
4078 batch->memcg = memcg;
4079
4080
4081
4082
4083
4084
4085
4086
4087 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
4088 goto direct_uncharge;
4089
4090 if (nr_pages > 1)
4091 goto direct_uncharge;
4092
4093
4094
4095
4096
4097
4098 if (batch->memcg != memcg)
4099 goto direct_uncharge;
4100
4101 batch->nr_pages++;
4102 if (uncharge_memsw)
4103 batch->memsw_nr_pages++;
4104 return;
4105direct_uncharge:
4106 page_counter_uncharge(&memcg->memory, nr_pages);
4107 if (uncharge_memsw)
4108 page_counter_uncharge(&memcg->memsw, nr_pages);
4109 if (unlikely(batch->memcg != memcg))
4110 memcg_oom_recover(memcg);
4111}
4112
4113
4114
4115
4116static struct mem_cgroup *
4117__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
4118 bool end_migration)
4119{
4120 struct mem_cgroup *memcg = NULL;
4121 unsigned int nr_pages = 1;
4122 struct page_cgroup *pc;
4123 bool anon;
4124
4125 if (mem_cgroup_disabled())
4126 return NULL;
4127
4128 if (PageTransHuge(page)) {
4129 nr_pages <<= compound_order(page);
4130 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
4131 }
4132
4133
4134
4135 pc = lookup_page_cgroup(page);
4136 if (unlikely(!PageCgroupUsed(pc)))
4137 return NULL;
4138
4139 lock_page_cgroup(pc);
4140
4141 memcg = pc->mem_cgroup;
4142
4143 if (!PageCgroupUsed(pc))
4144 goto unlock_out;
4145
4146 anon = PageAnon(page);
4147
4148 switch (ctype) {
4149 case MEM_CGROUP_CHARGE_TYPE_ANON:
4150
4151
4152
4153
4154
4155 anon = true;
4156
4157 case MEM_CGROUP_CHARGE_TYPE_DROP:
4158
4159 if (page_mapped(page))
4160 goto unlock_out;
4161
4162
4163
4164
4165
4166
4167
4168 if (!end_migration && PageCgroupMigration(pc))
4169 goto unlock_out;
4170 break;
4171 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4172 if (!PageAnon(page)) {
4173 if (page->mapping && !page_is_file_cache(page))
4174 goto unlock_out;
4175 } else if (page_mapped(page))
4176 goto unlock_out;
4177 break;
4178 default:
4179 break;
4180 }
4181
4182 mem_cgroup_charge_statistics(memcg, page, anon, -nr_pages);
4183
4184 ClearPageCgroupUsed(pc);
4185
4186
4187
4188
4189
4190
4191
4192 unlock_page_cgroup(pc);
4193
4194
4195
4196
4197 memcg_check_events(memcg, page);
4198 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
4199 mem_cgroup_swap_statistics(memcg, true);
4200 mem_cgroup_get(memcg);
4201 }
4202
4203
4204
4205
4206
4207 if (!end_migration && !mem_cgroup_is_root(memcg))
4208 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
4209
4210 return memcg;
4211
4212unlock_out:
4213 unlock_page_cgroup(pc);
4214 return NULL;
4215}
4216
4217void mem_cgroup_uncharge_page(struct page *page)
4218{
4219
4220 if (page_mapped(page))
4221 return;
4222 VM_BUG_ON_PAGE(page->mapping && !PageAnon(page), page);
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235 if (PageSwapCache(page))
4236 return;
4237 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
4238}
4239
4240void mem_cgroup_uncharge_cache_page(struct page *page)
4241{
4242 VM_BUG_ON_PAGE(page_mapped(page), page);
4243 VM_BUG_ON_PAGE(page->mapping, page);
4244 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
4245}
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255void mem_cgroup_uncharge_start(void)
4256{
4257 current->memcg_batch.do_batch++;
4258
4259 if (current->memcg_batch.do_batch == 1) {
4260 current->memcg_batch.memcg = NULL;
4261 current->memcg_batch.nr_pages = 0;
4262 current->memcg_batch.memsw_nr_pages = 0;
4263 }
4264}
4265
4266void mem_cgroup_uncharge_end(void)
4267{
4268 struct memcg_batch_info *batch = ¤t->memcg_batch;
4269
4270 if (!batch->do_batch)
4271 return;
4272
4273 batch->do_batch--;
4274 if (batch->do_batch)
4275 return;
4276
4277 if (!batch->memcg)
4278 return;
4279
4280
4281
4282
4283 if (batch->nr_pages)
4284 page_counter_uncharge(&batch->memcg->memory, batch->nr_pages);
4285 if (batch->memsw_nr_pages)
4286 page_counter_uncharge(&batch->memcg->memsw, batch->memsw_nr_pages);
4287 memcg_oom_recover(batch->memcg);
4288
4289 batch->memcg = NULL;
4290}
4291
4292#ifdef CONFIG_SWAP
4293
4294
4295
4296
4297void
4298mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
4299{
4300 struct mem_cgroup *memcg;
4301 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
4302
4303 if (!swapout)
4304 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4305
4306 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
4307
4308
4309
4310
4311
4312 if (do_swap_account && swapout && memcg)
4313 swap_cgroup_record(ent, mem_cgroup_id(memcg));
4314}
4315#endif
4316
4317#ifdef CONFIG_MEMCG_SWAP
4318
4319
4320
4321
4322void mem_cgroup_uncharge_swap(swp_entry_t ent)
4323{
4324 struct mem_cgroup *memcg;
4325 unsigned short id;
4326
4327 if (!do_swap_account)
4328 return;
4329
4330 id = swap_cgroup_record(ent, 0);
4331 rcu_read_lock();
4332 memcg = mem_cgroup_lookup(id);
4333 if (memcg) {
4334
4335
4336
4337
4338 if (!mem_cgroup_is_root(memcg))
4339 page_counter_uncharge(&memcg->memsw, 1);
4340 mem_cgroup_swap_statistics(memcg, false);
4341 mem_cgroup_put(memcg);
4342 }
4343 rcu_read_unlock();
4344}
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360static int mem_cgroup_move_swap_account(swp_entry_t entry,
4361 struct mem_cgroup *from, struct mem_cgroup *to)
4362{
4363 unsigned short old_id, new_id;
4364
4365 old_id = mem_cgroup_id(from);
4366 new_id = mem_cgroup_id(to);
4367
4368 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
4369 mem_cgroup_swap_statistics(from, false);
4370 mem_cgroup_swap_statistics(to, true);
4371
4372
4373
4374
4375
4376
4377
4378
4379 mem_cgroup_get(to);
4380 return 0;
4381 }
4382 return -EINVAL;
4383}
4384#else
4385static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
4386 struct mem_cgroup *from, struct mem_cgroup *to)
4387{
4388 return -EINVAL;
4389}
4390#endif
4391
4392
4393
4394
4395
4396void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4397 struct mem_cgroup **memcgp)
4398{
4399 struct mem_cgroup *memcg = NULL;
4400 unsigned int nr_pages = 1;
4401 struct page_cgroup *pc;
4402 enum charge_type ctype;
4403
4404 *memcgp = NULL;
4405
4406 if (mem_cgroup_disabled())
4407 return;
4408
4409 if (PageTransHuge(page))
4410 nr_pages <<= compound_order(page);
4411
4412 pc = lookup_page_cgroup(page);
4413 lock_page_cgroup(pc);
4414 if (PageCgroupUsed(pc)) {
4415 memcg = pc->mem_cgroup;
4416 css_get(&memcg->css);
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446 if (PageAnon(page))
4447 SetPageCgroupMigration(pc);
4448 }
4449 unlock_page_cgroup(pc);
4450
4451
4452
4453
4454 if (!memcg)
4455 return;
4456
4457 *memcgp = memcg;
4458
4459
4460
4461
4462
4463
4464 if (PageAnon(page))
4465 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
4466 else
4467 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
4468
4469
4470
4471
4472
4473 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
4474}
4475
4476
4477void mem_cgroup_end_migration(struct mem_cgroup *memcg,
4478 struct page *oldpage, struct page *newpage, bool migration_ok)
4479{
4480 struct page *used, *unused;
4481 struct page_cgroup *pc;
4482 bool anon;
4483
4484 if (!memcg)
4485 return;
4486
4487 if (!migration_ok) {
4488 used = oldpage;
4489 unused = newpage;
4490 } else {
4491 used = newpage;
4492 unused = oldpage;
4493 }
4494 anon = PageAnon(used);
4495 __mem_cgroup_uncharge_common(unused,
4496 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4497 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4498 true);
4499 css_put(&memcg->css);
4500
4501
4502
4503
4504
4505 pc = lookup_page_cgroup(oldpage);
4506 lock_page_cgroup(pc);
4507 ClearPageCgroupMigration(pc);
4508 unlock_page_cgroup(pc);
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518 if (anon)
4519 mem_cgroup_uncharge_page(used);
4520}
4521
4522
4523
4524
4525
4526
4527void mem_cgroup_replace_page_cache(struct page *oldpage,
4528 struct page *newpage)
4529{
4530 struct mem_cgroup *memcg = NULL;
4531 struct page_cgroup *pc;
4532 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
4533
4534 if (mem_cgroup_disabled())
4535 return;
4536
4537 pc = lookup_page_cgroup(oldpage);
4538
4539 lock_page_cgroup(pc);
4540 if (PageCgroupUsed(pc)) {
4541 memcg = pc->mem_cgroup;
4542 mem_cgroup_charge_statistics(memcg, oldpage, false, -1);
4543 ClearPageCgroupUsed(pc);
4544 }
4545 unlock_page_cgroup(pc);
4546
4547
4548
4549
4550
4551 if (!memcg)
4552 return;
4553
4554
4555
4556
4557
4558 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
4559}
4560
4561#ifdef CONFIG_DEBUG_VM
4562static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4563{
4564 struct page_cgroup *pc;
4565
4566 pc = lookup_page_cgroup(page);
4567
4568
4569
4570
4571
4572 if (likely(pc) && PageCgroupUsed(pc))
4573 return pc;
4574 return NULL;
4575}
4576
4577bool mem_cgroup_bad_page_check(struct page *page)
4578{
4579 if (mem_cgroup_disabled())
4580 return false;
4581
4582 return lookup_page_cgroup_used(page) != NULL;
4583}
4584
4585void mem_cgroup_print_bad_page(struct page *page)
4586{
4587 struct page_cgroup *pc;
4588
4589 pc = lookup_page_cgroup_used(page);
4590 if (pc) {
4591 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4592 pc, pc->flags, pc->mem_cgroup);
4593 }
4594}
4595#endif
4596
4597static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
4598 unsigned long limit)
4599{
4600 unsigned long curusage;
4601 unsigned long oldusage;
4602 unsigned long memswlimit;
4603 bool enlarge = false;
4604 int retry_count;
4605 int ret;
4606
4607
4608
4609
4610
4611
4612 retry_count = MEM_CGROUP_RECLAIM_RETRIES *
4613 mem_cgroup_count_children(memcg);
4614
4615 oldusage = page_counter_read(&memcg->memory);
4616
4617 do {
4618 if (signal_pending(current)) {
4619 ret = -EINTR;
4620 break;
4621 }
4622 mutex_lock(&memcg_limit_mutex);
4623 memswlimit = memcg->memsw.limit;
4624 if (limit > memswlimit) {
4625 mutex_unlock(&memcg_limit_mutex);
4626 ret = -EINVAL;
4627 break;
4628 }
4629
4630 if (limit > memcg->memory.limit)
4631 enlarge = true;
4632
4633 ret = page_counter_limit(&memcg->memory, limit);
4634 if (!ret) {
4635 if (memswlimit == limit)
4636 memcg->memsw_is_minimum = true;
4637 else
4638 memcg->memsw_is_minimum = false;
4639 }
4640 mutex_unlock(&memcg_limit_mutex);
4641
4642 if (!ret)
4643 break;
4644
4645 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4646 MEM_CGROUP_RECLAIM_SHRINK);
4647 curusage = page_counter_read(&memcg->memory);
4648
4649 if (curusage >= oldusage)
4650 retry_count--;
4651 else
4652 oldusage = curusage;
4653 } while (retry_count);
4654
4655 if (!ret && enlarge)
4656 memcg_oom_recover(memcg);
4657
4658 return ret;
4659}
4660
4661static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4662 unsigned long limit)
4663{
4664 unsigned long curusage;
4665 unsigned long oldusage;
4666 unsigned long memlimit, memswlimit;
4667 bool enlarge = false;
4668 int retry_count;
4669 int ret;
4670
4671
4672 retry_count = MEM_CGROUP_RECLAIM_RETRIES *
4673 mem_cgroup_count_children(memcg);
4674
4675 oldusage = page_counter_read(&memcg->memsw);
4676
4677 do {
4678 if (signal_pending(current)) {
4679 ret = -EINTR;
4680 break;
4681 }
4682 mutex_lock(&memcg_limit_mutex);
4683 memlimit = memcg->memory.limit;
4684 if (limit < memlimit) {
4685 mutex_unlock(&memcg_limit_mutex);
4686 ret = -EINVAL;
4687 break;
4688 }
4689 memswlimit = memcg->memsw.limit;
4690 if (limit > memswlimit)
4691 enlarge = true;
4692 ret = page_counter_limit(&memcg->memsw, limit);
4693 if (!ret) {
4694 if (memlimit == limit)
4695 memcg->memsw_is_minimum = true;
4696 else
4697 memcg->memsw_is_minimum = false;
4698 }
4699 mutex_unlock(&memcg_limit_mutex);
4700
4701 if (!ret)
4702 break;
4703
4704 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4705 MEM_CGROUP_RECLAIM_NOSWAP |
4706 MEM_CGROUP_RECLAIM_SHRINK);
4707 curusage = page_counter_read(&memcg->memsw);
4708
4709 if (curusage >= oldusage)
4710 retry_count--;
4711 else
4712 oldusage = curusage;
4713 } while (retry_count);
4714
4715 if (!ret && enlarge)
4716 memcg_oom_recover(memcg);
4717 return ret;
4718}
4719
4720unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
4721 gfp_t gfp_mask,
4722 unsigned long *total_scanned)
4723{
4724 unsigned long nr_reclaimed = 0;
4725 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
4726 unsigned long reclaimed;
4727 int loop = 0;
4728 struct mem_cgroup_tree_per_zone *mctz;
4729 unsigned long excess;
4730 unsigned long nr_scanned;
4731
4732 if (order > 0)
4733 return 0;
4734
4735 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
4736
4737
4738
4739
4740
4741 do {
4742 if (next_mz)
4743 mz = next_mz;
4744 else
4745 mz = mem_cgroup_largest_soft_limit_node(mctz);
4746 if (!mz)
4747 break;
4748
4749 nr_scanned = 0;
4750 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
4751 gfp_mask, &nr_scanned);
4752 nr_reclaimed += reclaimed;
4753 *total_scanned += nr_scanned;
4754 spin_lock(&mctz->lock);
4755
4756
4757
4758
4759
4760 next_mz = NULL;
4761 if (!reclaimed) {
4762 do {
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774 next_mz =
4775 __mem_cgroup_largest_soft_limit_node(mctz);
4776 if (next_mz == mz)
4777 css_put(&next_mz->memcg->css);
4778 else
4779 break;
4780 } while (1);
4781 }
4782 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
4783 excess = soft_limit_excess(mz->memcg);
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
4794 spin_unlock(&mctz->lock);
4795 css_put(&mz->memcg->css);
4796 loop++;
4797
4798
4799
4800
4801
4802 if (!nr_reclaimed &&
4803 (next_mz == NULL ||
4804 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
4805 break;
4806 } while (!nr_reclaimed);
4807 if (next_mz)
4808 css_put(&next_mz->memcg->css);
4809 return nr_reclaimed;
4810}
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
4824 int node, int zid, enum lru_list lru)
4825{
4826 struct lruvec *lruvec;
4827 unsigned long flags;
4828 struct list_head *list;
4829 struct page *busy;
4830 struct zone *zone;
4831
4832 zone = &NODE_DATA(node)->node_zones[zid];
4833 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4834 list = &lruvec->lists[lru];
4835
4836 busy = NULL;
4837 do {
4838 struct page_cgroup *pc;
4839 struct page *page;
4840
4841 cond_resched();
4842 spin_lock_irqsave(&zone->lru_lock, flags);
4843 if (list_empty(list)) {
4844 spin_unlock_irqrestore(&zone->lru_lock, flags);
4845 break;
4846 }
4847 page = list_entry(list->prev, struct page, lru);
4848 if (busy == page) {
4849 list_move(&page->lru, list);
4850 busy = NULL;
4851 spin_unlock_irqrestore(&zone->lru_lock, flags);
4852 continue;
4853 }
4854 spin_unlock_irqrestore(&zone->lru_lock, flags);
4855
4856 pc = lookup_page_cgroup(page);
4857
4858 if (mem_cgroup_move_parent(page, pc, memcg)) {
4859
4860 busy = page;
4861 } else
4862 busy = NULL;
4863 } while (!list_empty(list));
4864}
4865
4866
4867
4868
4869
4870
4871
4872
4873static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
4874{
4875 int node, zid;
4876
4877 do {
4878
4879 lru_add_drain_all();
4880 drain_all_stock_sync(memcg);
4881 mem_cgroup_start_move(memcg);
4882 for_each_node_state(node, N_MEMORY) {
4883 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
4884 enum lru_list lru;
4885 for_each_lru(lru) {
4886 mem_cgroup_force_empty_list(memcg,
4887 node, zid, lru);
4888 }
4889 }
4890 }
4891 mem_cgroup_end_move(memcg);
4892 memcg_oom_recover(memcg);
4893 cond_resched();
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907 } while (page_counter_read(&memcg->memory) -
4908 page_counter_read(&memcg->kmem) > 0);
4909}
4910
4911
4912
4913
4914
4915
4916static inline bool __memcg_has_children(struct mem_cgroup *memcg)
4917{
4918 struct cgroup *pos;
4919
4920
4921 cgroup_for_each_child(pos, memcg->css.cgroup)
4922 return true;
4923 return false;
4924}
4925
4926
4927
4928
4929
4930
4931
4932
4933static inline bool memcg_has_children(struct mem_cgroup *memcg)
4934{
4935 return memcg->use_hierarchy && __memcg_has_children(memcg);
4936}
4937
4938
4939
4940
4941
4942
4943
4944static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4945{
4946 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4947 struct cgroup *cgrp = memcg->css.cgroup;
4948
4949
4950 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4951 return -EBUSY;
4952
4953
4954 lru_add_drain_all();
4955
4956 while (nr_retries && page_counter_read(&memcg->memory)) {
4957 int progress;
4958
4959 if (signal_pending(current))
4960 return -EINTR;
4961
4962 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
4963 false);
4964 if (!progress) {
4965 nr_retries--;
4966
4967 congestion_wait(BLK_RW_ASYNC, HZ/10);
4968 }
4969
4970 }
4971 lru_add_drain();
4972 mem_cgroup_reparent_charges(memcg);
4973
4974 return 0;
4975}
4976
4977static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
4978{
4979 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4980 int ret;
4981
4982 if (mem_cgroup_is_root(memcg))
4983 return -EINVAL;
4984 css_get(&memcg->css);
4985 ret = mem_cgroup_force_empty(memcg);
4986 css_put(&memcg->css);
4987
4988 return ret;
4989}
4990
4991
4992static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
4993{
4994 return mem_cgroup_from_cont(cont)->use_hierarchy;
4995}
4996
4997static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
4998 u64 val)
4999{
5000 int retval = 0;
5001 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
5002 struct cgroup *parent = cont->parent;
5003 struct mem_cgroup *parent_memcg = NULL;
5004
5005 if (parent)
5006 parent_memcg = mem_cgroup_from_cont(parent);
5007
5008 mutex_lock(&memcg_create_mutex);
5009
5010 if (memcg->use_hierarchy == val)
5011 goto out;
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
5022 (val == 1 || val == 0)) {
5023 if (!__memcg_has_children(memcg))
5024 memcg->use_hierarchy = val;
5025 else
5026 retval = -EBUSY;
5027 } else
5028 retval = -EINVAL;
5029
5030out:
5031 mutex_unlock(&memcg_create_mutex);
5032
5033 return retval;
5034}
5035
5036
5037static unsigned long tree_stat(struct mem_cgroup *memcg,
5038 enum mem_cgroup_stat_index idx)
5039{
5040 struct mem_cgroup *iter;
5041 long val = 0;
5042
5043
5044 for_each_mem_cgroup_tree(iter, memcg)
5045 val += mem_cgroup_read_stat(iter, idx);
5046
5047 if (val < 0)
5048 val = 0;
5049 return val;
5050}
5051
5052static inline unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
5053{
5054 unsigned long val;
5055
5056 if (mem_cgroup_is_root(memcg)) {
5057 val = tree_stat(memcg, MEM_CGROUP_STAT_CACHE);
5058 val += tree_stat(memcg, MEM_CGROUP_STAT_RSS);
5059 if (swap)
5060 val += tree_stat(memcg, MEM_CGROUP_STAT_SWAP);
5061 } else {
5062 if (!swap)
5063 val = page_counter_read(&memcg->memory);
5064 else
5065 val = page_counter_read(&memcg->memsw);
5066 }
5067 return val;
5068}
5069
5070struct accumulated_stats {
5071 unsigned long stat[MEM_CGROUP_STAT_NSTATS];
5072 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
5073 unsigned long lru_pages[NR_LRU_LISTS];
5074};
5075
5076static void accumulate_memcg_tree(struct mem_cgroup *memcg,
5077 struct accumulated_stats *acc)
5078{
5079 struct mem_cgroup *mi;
5080 int i;
5081
5082 for_each_mem_cgroup_tree(mi, memcg) {
5083 mem_cgroup_sum_all_stat_events(mi, acc->stat, acc->events);
5084
5085 for (i = 0; i < NR_LRU_LISTS; i++)
5086 acc->lru_pages[i] += mem_cgroup_nr_lru_pages(mi, BIT(i));
5087
5088 cond_resched();
5089 }
5090}
5091
5092enum {
5093 RES_USAGE,
5094 RES_LIMIT,
5095 RES_MAX_USAGE,
5096 RES_FAILCNT,
5097 RES_SOFT_LIMIT,
5098};
5099
5100static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
5101 struct file *file, char __user *buf,
5102 size_t nbytes, loff_t *ppos)
5103{
5104 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
5105 char str[64];
5106 u64 val;
5107 int len;
5108 struct page_counter *counter;
5109
5110 switch (MEMFILE_TYPE(cft->private)) {
5111 case _MEM:
5112 counter = &memcg->memory;
5113 break;
5114 case _MEMSWAP:
5115 counter = &memcg->memsw;
5116 break;
5117 case _KMEM:
5118 counter = &memcg->kmem;
5119 break;
5120 default:
5121 BUG();
5122 }
5123
5124 switch (MEMFILE_ATTR(cft->private)) {
5125 case RES_USAGE:
5126 if (counter == &memcg->memory)
5127 val = (u64)mem_cgroup_usage(memcg, false) * PAGE_SIZE;
5128 else if (counter == &memcg->memsw)
5129 val = (u64)mem_cgroup_usage(memcg, true) * PAGE_SIZE;
5130 else
5131 val = (u64)page_counter_read(counter) * PAGE_SIZE;
5132 break;
5133 case RES_LIMIT:
5134 val = (u64)counter->limit * PAGE_SIZE;
5135 break;
5136 case RES_MAX_USAGE:
5137 val = (u64)counter->watermark * PAGE_SIZE;
5138 break;
5139 case RES_FAILCNT:
5140 val = (u64)counter->failcnt;
5141 break;
5142 case RES_SOFT_LIMIT:
5143 val = (u64)memcg->soft_limit * PAGE_SIZE;
5144 break;
5145 default:
5146 BUG();
5147 }
5148
5149 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
5150 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
5151}
5152
5153static int memcg_update_kmem_limit(struct cgroup *cont, unsigned long limit)
5154{
5155 int ret = -EINVAL;
5156#ifdef CONFIG_MEMCG_KMEM
5157 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
5158
5159
5160
5161
5162
5163
5164 if (cgroup_memory_nokmem)
5165 return 0;
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179 mutex_lock(&memcg_create_mutex);
5180 mutex_lock(&memcg_limit_mutex);
5181 if (!memcg->kmem_account_flags && limit != PAGE_COUNTER_MAX) {
5182 if (cgroup_task_count(cont) || memcg_has_children(memcg)) {
5183 ret = -EBUSY;
5184 goto out;
5185 }
5186 ret = page_counter_limit(&memcg->kmem, limit);
5187 VM_BUG_ON(ret);
5188
5189 ret = memcg_update_cache_sizes(memcg);
5190 if (ret) {
5191 page_counter_limit(&memcg->kmem, PAGE_COUNTER_MAX);
5192 goto out;
5193 }
5194 static_key_slow_inc(&memcg_kmem_enabled_key);
5195
5196
5197
5198
5199 memcg_kmem_set_active(memcg);
5200
5201
5202
5203
5204
5205
5206
5207 mem_cgroup_get(memcg);
5208 } else
5209 ret = page_counter_limit(&memcg->kmem, limit);
5210out:
5211 mutex_unlock(&memcg_limit_mutex);
5212 mutex_unlock(&memcg_create_mutex);
5213#endif
5214 return ret;
5215}
5216
5217#ifdef CONFIG_MEMCG_KMEM
5218static int memcg_propagate_kmem(struct mem_cgroup *memcg)
5219{
5220 int ret = 0;
5221 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5222
5223 if (!parent || cgroup_memory_nokmem)
5224 goto out;
5225
5226 memcg->kmem_account_flags = parent->kmem_account_flags;
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237 if (!memcg_kmem_is_active(memcg))
5238 goto out;
5239
5240
5241
5242
5243
5244
5245
5246 mem_cgroup_get(memcg);
5247 static_key_slow_inc(&memcg_kmem_enabled_key);
5248
5249 mutex_lock(&memcg_limit_mutex);
5250 memcg_stop_kmem_account();
5251 ret = memcg_update_cache_sizes(memcg);
5252 memcg_resume_kmem_account();
5253 mutex_unlock(&memcg_limit_mutex);
5254out:
5255 return ret;
5256}
5257#endif
5258
5259
5260
5261
5262
5263static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
5264 const char *buffer)
5265{
5266 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
5267 unsigned long nr_pages;
5268 int ret;
5269
5270 ret = page_counter_memparse(buffer, &nr_pages);
5271 if (ret)
5272 return ret;
5273
5274 switch (MEMFILE_ATTR(cft->private)) {
5275 case RES_LIMIT:
5276 if (mem_cgroup_is_root(memcg)) {
5277 ret = -EINVAL;
5278 break;
5279 }
5280 switch (MEMFILE_TYPE(cft->private)) {
5281 case _MEM:
5282 ret = mem_cgroup_resize_limit(memcg, nr_pages);
5283 break;
5284 case _MEMSWAP:
5285 ret = mem_cgroup_resize_memsw_limit(memcg, nr_pages);
5286 break;
5287 case _KMEM:
5288 ret = memcg_update_kmem_limit(cont, nr_pages);
5289 break;
5290 }
5291 break;
5292 case RES_SOFT_LIMIT:
5293 memcg->soft_limit = nr_pages;
5294 ret = 0;
5295 break;
5296 }
5297 return ret;
5298}
5299
5300static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
5301{
5302 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
5303 struct page_counter *counter;
5304
5305 switch (MEMFILE_TYPE(event)) {
5306 case _MEM:
5307 counter = &memcg->memory;
5308 break;
5309 case _MEMSWAP:
5310 counter = &memcg->memsw;
5311 break;
5312 case _KMEM:
5313 counter = &memcg->kmem;
5314 break;
5315 default:
5316 BUG();
5317 }
5318
5319 switch (MEMFILE_ATTR(event)) {
5320 case RES_MAX_USAGE:
5321 page_counter_reset_watermark(counter);
5322 break;
5323 case RES_FAILCNT:
5324 counter->failcnt = 0;
5325 break;
5326 default:
5327 BUG();
5328 }
5329
5330 return 0;
5331}
5332
5333static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
5334 struct cftype *cft)
5335{
5336 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
5337}
5338
5339#ifdef CONFIG_MMU
5340static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5341 struct cftype *cft, u64 val)
5342{
5343 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5344
5345 if (val >= (1 << NR_MOVE_TYPE))
5346 return -EINVAL;
5347
5348
5349
5350
5351
5352
5353
5354 memcg->move_charge_at_immigrate = val;
5355 return 0;
5356}
5357#else
5358static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5359 struct cftype *cft, u64 val)
5360{
5361 return -ENOSYS;
5362}
5363#endif
5364
5365#ifdef CONFIG_NUMA
5366static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
5367 struct seq_file *m)
5368{
5369 int nid;
5370 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5371 unsigned long node_nr;
5372 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
5373
5374 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
5375 seq_printf(m, "total=%lu", total_nr);
5376 for_each_node_state(nid, N_MEMORY) {
5377 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
5378 seq_printf(m, " N%d=%lu", nid, node_nr);
5379 }
5380 seq_putc(m, '\n');
5381
5382 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
5383 seq_printf(m, "file=%lu", file_nr);
5384 for_each_node_state(nid, N_MEMORY) {
5385 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
5386 LRU_ALL_FILE);
5387 seq_printf(m, " N%d=%lu", nid, node_nr);
5388 }
5389 seq_putc(m, '\n');
5390
5391 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
5392 seq_printf(m, "anon=%lu", anon_nr);
5393 for_each_node_state(nid, N_MEMORY) {
5394 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
5395 LRU_ALL_ANON);
5396 seq_printf(m, " N%d=%lu", nid, node_nr);
5397 }
5398 seq_putc(m, '\n');
5399
5400 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
5401 seq_printf(m, "unevictable=%lu", unevictable_nr);
5402 for_each_node_state(nid, N_MEMORY) {
5403 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
5404 BIT(LRU_UNEVICTABLE));
5405 seq_printf(m, " N%d=%lu", nid, node_nr);
5406 }
5407 seq_putc(m, '\n');
5408 return 0;
5409}
5410#endif
5411
5412static inline void mem_cgroup_lru_names_not_uptodate(void)
5413{
5414 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5415}
5416
5417static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
5418 struct seq_file *m)
5419{
5420 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
5421 unsigned long memory, memsw;
5422 struct mem_cgroup *mi;
5423 unsigned int i;
5424 struct accumulated_stats acc;
5425
5426 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5427 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
5428 continue;
5429 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5430 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
5431 }
5432
5433 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5434 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5435 mem_cgroup_read_events(memcg, i));
5436
5437 for (i = 0; i < NR_LRU_LISTS; i++)
5438 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5439 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5440
5441
5442 memory = memsw = PAGE_COUNTER_MAX;
5443 for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) {
5444 memory = min(memory, mi->memory.limit);
5445 memsw = min(memsw, mi->memsw.limit);
5446 }
5447 seq_printf(m, "hierarchical_memory_limit %llu\n",
5448 (u64)memory * PAGE_SIZE);
5449 if (do_swap_account)
5450 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5451 (u64)memsw * PAGE_SIZE);
5452
5453 memset(&acc, 0, sizeof(acc));
5454 accumulate_memcg_tree(memcg, &acc);
5455
5456 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5457 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
5458 continue;
5459 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i],
5460 (u64)acc.stat[i] * PAGE_SIZE);
5461 }
5462
5463 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5464 seq_printf(m, "total_%s %llu\n", mem_cgroup_events_names[i],
5465 (u64)acc.events[i]);
5466
5467 for (i = 0; i < NR_LRU_LISTS; i++)
5468 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i],
5469 (u64)acc.lru_pages[i] * PAGE_SIZE);
5470
5471#ifdef CONFIG_DEBUG_VM
5472 {
5473 int nid, zid;
5474 struct mem_cgroup_per_zone *mz;
5475 struct zone_reclaim_stat *rstat;
5476 unsigned long recent_rotated[2] = {0, 0};
5477 unsigned long recent_scanned[2] = {0, 0};
5478
5479 for_each_online_node(nid)
5480 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
5481 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
5482 rstat = &mz->lruvec.reclaim_stat;
5483
5484 recent_rotated[0] += rstat->recent_rotated[0];
5485 recent_rotated[1] += rstat->recent_rotated[1];
5486 recent_scanned[0] += rstat->recent_scanned[0];
5487 recent_scanned[1] += rstat->recent_scanned[1];
5488 }
5489 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5490 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5491 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5492 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
5493 }
5494#endif
5495
5496 return 0;
5497}
5498
5499static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
5500{
5501 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5502
5503 return mem_cgroup_swappiness(memcg);
5504}
5505
5506static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
5507 u64 val)
5508{
5509 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5510
5511 if (val > 100)
5512 return -EINVAL;
5513
5514 if (cgrp->parent)
5515 memcg->swappiness = val;
5516 else
5517 vm_swappiness = val;
5518
5519 return 0;
5520}
5521
5522static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5523{
5524 struct mem_cgroup_threshold_ary *t;
5525 unsigned long usage;
5526 int i;
5527
5528 rcu_read_lock();
5529 if (!swap)
5530 t = rcu_dereference(memcg->thresholds.primary);
5531 else
5532 t = rcu_dereference(memcg->memsw_thresholds.primary);
5533
5534 if (!t)
5535 goto unlock;
5536
5537 usage = mem_cgroup_usage(memcg, swap);
5538
5539
5540
5541
5542
5543
5544 i = t->current_threshold;
5545
5546
5547
5548
5549
5550
5551
5552 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5553 eventfd_signal(t->entries[i].eventfd, 1);
5554
5555
5556 i++;
5557
5558
5559
5560
5561
5562
5563
5564 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5565 eventfd_signal(t->entries[i].eventfd, 1);
5566
5567
5568 t->current_threshold = i - 1;
5569unlock:
5570 rcu_read_unlock();
5571}
5572
5573static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5574{
5575 while (memcg) {
5576 __mem_cgroup_threshold(memcg, false);
5577 if (do_swap_account)
5578 __mem_cgroup_threshold(memcg, true);
5579
5580 memcg = parent_mem_cgroup(memcg);
5581 }
5582}
5583
5584static int compare_thresholds(const void *a, const void *b)
5585{
5586 const struct mem_cgroup_threshold *_a = a;
5587 const struct mem_cgroup_threshold *_b = b;
5588
5589 if (_a->threshold > _b->threshold)
5590 return 1;
5591
5592 if (_a->threshold < _b->threshold)
5593 return -1;
5594
5595 return 0;
5596}
5597
5598static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
5599{
5600 struct mem_cgroup_eventfd_list *ev;
5601
5602 spin_lock(&memcg_oom_lock);
5603
5604 list_for_each_entry(ev, &memcg->oom_notify, list)
5605 eventfd_signal(ev->eventfd, 1);
5606
5607 spin_unlock(&memcg_oom_lock);
5608 return 0;
5609}
5610
5611static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
5612{
5613 struct mem_cgroup *iter;
5614
5615 for_each_mem_cgroup_tree(iter, memcg)
5616 mem_cgroup_oom_notify_cb(iter);
5617}
5618
5619static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
5620 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5621{
5622 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5623 struct mem_cgroup_thresholds *thresholds;
5624 struct mem_cgroup_threshold_ary *new;
5625 enum res_type type = MEMFILE_TYPE(cft->private);
5626 unsigned long threshold;
5627 unsigned long usage;
5628 int i, size, ret;
5629
5630 ret = page_counter_memparse(args, &threshold);
5631 if (ret)
5632 return ret;
5633
5634 mutex_lock(&memcg->thresholds_lock);
5635
5636 if (type == _MEM)
5637 thresholds = &memcg->thresholds;
5638 else if (type == _MEMSWAP)
5639 thresholds = &memcg->memsw_thresholds;
5640 else
5641 BUG();
5642
5643 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5644
5645
5646 if (thresholds->primary)
5647 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5648
5649 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
5650
5651
5652 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
5653 GFP_KERNEL);
5654 if (!new) {
5655 ret = -ENOMEM;
5656 goto unlock;
5657 }
5658 new->size = size;
5659
5660
5661 if (thresholds->primary) {
5662 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
5663 sizeof(struct mem_cgroup_threshold));
5664 }
5665
5666
5667 new->entries[size - 1].eventfd = eventfd;
5668 new->entries[size - 1].threshold = threshold;
5669
5670
5671 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
5672 compare_thresholds, NULL);
5673
5674
5675 new->current_threshold = -1;
5676 for (i = 0; i < size; i++) {
5677 if (new->entries[i].threshold <= usage) {
5678
5679
5680
5681
5682
5683 ++new->current_threshold;
5684 } else
5685 break;
5686 }
5687
5688
5689 kfree(thresholds->spare);
5690 thresholds->spare = thresholds->primary;
5691
5692 rcu_assign_pointer(thresholds->primary, new);
5693
5694
5695 synchronize_rcu();
5696
5697unlock:
5698 mutex_unlock(&memcg->thresholds_lock);
5699
5700 return ret;
5701}
5702
5703static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
5704 struct cftype *cft, struct eventfd_ctx *eventfd)
5705{
5706 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5707 struct mem_cgroup_thresholds *thresholds;
5708 struct mem_cgroup_threshold_ary *new;
5709 enum res_type type = MEMFILE_TYPE(cft->private);
5710 unsigned long usage;
5711 int i, j, size, entries;
5712
5713 mutex_lock(&memcg->thresholds_lock);
5714 if (type == _MEM)
5715 thresholds = &memcg->thresholds;
5716 else if (type == _MEMSWAP)
5717 thresholds = &memcg->memsw_thresholds;
5718 else
5719 BUG();
5720
5721 if (!thresholds->primary)
5722 goto unlock;
5723
5724 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5725
5726
5727 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5728
5729
5730 size = entries = 0;
5731 for (i = 0; i < thresholds->primary->size; i++) {
5732 if (thresholds->primary->entries[i].eventfd != eventfd)
5733 size++;
5734 else
5735 entries++;
5736 }
5737
5738 new = thresholds->spare;
5739
5740
5741 if (!entries)
5742 goto unlock;
5743
5744
5745 if (!size) {
5746 kfree(new);
5747 new = NULL;
5748 goto swap_buffers;
5749 }
5750
5751 new->size = size;
5752
5753
5754 new->current_threshold = -1;
5755 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5756 if (thresholds->primary->entries[i].eventfd == eventfd)
5757 continue;
5758
5759 new->entries[j] = thresholds->primary->entries[i];
5760 if (new->entries[j].threshold <= usage) {
5761
5762
5763
5764
5765
5766 ++new->current_threshold;
5767 }
5768 j++;
5769 }
5770
5771swap_buffers:
5772
5773 thresholds->spare = thresholds->primary;
5774
5775 rcu_assign_pointer(thresholds->primary, new);
5776
5777
5778 synchronize_rcu();
5779
5780
5781 if (!new) {
5782 kfree(thresholds->spare);
5783 thresholds->spare = NULL;
5784 }
5785unlock:
5786 mutex_unlock(&memcg->thresholds_lock);
5787}
5788
5789static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
5790 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5791{
5792 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5793 struct mem_cgroup_eventfd_list *event;
5794 enum res_type type = MEMFILE_TYPE(cft->private);
5795
5796 BUG_ON(type != _OOM_TYPE);
5797 event = kmalloc(sizeof(*event), GFP_KERNEL);
5798 if (!event)
5799 return -ENOMEM;
5800
5801 spin_lock(&memcg_oom_lock);
5802
5803 event->eventfd = eventfd;
5804 list_add(&event->list, &memcg->oom_notify);
5805
5806
5807 if (atomic_read(&memcg->under_oom))
5808 eventfd_signal(eventfd, 1);
5809 spin_unlock(&memcg_oom_lock);
5810
5811 return 0;
5812}
5813
5814static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
5815 struct cftype *cft, struct eventfd_ctx *eventfd)
5816{
5817 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5818 struct mem_cgroup_eventfd_list *ev, *tmp;
5819 enum res_type type = MEMFILE_TYPE(cft->private);
5820
5821 BUG_ON(type != _OOM_TYPE);
5822
5823 spin_lock(&memcg_oom_lock);
5824
5825 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
5826 if (ev->eventfd == eventfd) {
5827 list_del(&ev->list);
5828 kfree(ev);
5829 }
5830 }
5831
5832 spin_unlock(&memcg_oom_lock);
5833}
5834
5835static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
5836 struct cftype *cft, struct cgroup_map_cb *cb)
5837{
5838 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5839
5840 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
5841
5842 if (atomic_read(&memcg->under_oom))
5843 cb->fill(cb, "under_oom", 1);
5844 else
5845 cb->fill(cb, "under_oom", 0);
5846 return 0;
5847}
5848
5849static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
5850 struct cftype *cft, u64 val)
5851{
5852 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5853
5854
5855 if (!cgrp->parent || !((val == 0) || (val == 1)))
5856 return -EINVAL;
5857
5858 memcg->oom_kill_disable = val;
5859 if (!val)
5860 memcg_oom_recover(memcg);
5861
5862 return 0;
5863}
5864
5865#ifdef CONFIG_MEMCG_KMEM
5866static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
5867{
5868 int ret;
5869
5870 memcg->kmemcg_id = -1;
5871 ret = memcg_propagate_kmem(memcg);
5872 if (ret)
5873 return ret;
5874
5875 return mem_cgroup_sockets_init(memcg, ss);
5876}
5877
5878static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
5879{
5880 mem_cgroup_sockets_destroy(memcg);
5881
5882 memcg_kmem_mark_dead(memcg);
5883
5884 if (page_counter_read(&memcg->kmem))
5885 return;
5886
5887
5888
5889
5890
5891
5892
5893 if (memcg_kmem_test_and_clear_dead(memcg))
5894 mem_cgroup_put(memcg);
5895}
5896#else
5897static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
5898{
5899 return 0;
5900}
5901
5902static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
5903{
5904}
5905#endif
5906
5907static struct cftype mem_cgroup_files[] = {
5908 {
5909 .name = "usage_in_bytes",
5910 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
5911 .read = mem_cgroup_read,
5912 .register_event = mem_cgroup_usage_register_event,
5913 .unregister_event = mem_cgroup_usage_unregister_event,
5914 },
5915 {
5916 .name = "max_usage_in_bytes",
5917 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
5918 .trigger = mem_cgroup_reset,
5919 .read = mem_cgroup_read,
5920 },
5921 {
5922 .name = "limit_in_bytes",
5923 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
5924 .write_string = mem_cgroup_write,
5925 .read = mem_cgroup_read,
5926 },
5927 {
5928 .name = "soft_limit_in_bytes",
5929 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5930 .write_string = mem_cgroup_write,
5931 .read = mem_cgroup_read,
5932 },
5933 {
5934 .name = "failcnt",
5935 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
5936 .trigger = mem_cgroup_reset,
5937 .read = mem_cgroup_read,
5938 },
5939 {
5940 .name = "stat",
5941 .read_seq_string = memcg_stat_show,
5942 },
5943 {
5944 .name = "force_empty",
5945 .trigger = mem_cgroup_force_empty_write,
5946 },
5947 {
5948 .name = "use_hierarchy",
5949 .flags = CFTYPE_INSANE,
5950 .write_u64 = mem_cgroup_hierarchy_write,
5951 .read_u64 = mem_cgroup_hierarchy_read,
5952 },
5953 {
5954 .name = "swappiness",
5955 .read_u64 = mem_cgroup_swappiness_read,
5956 .write_u64 = mem_cgroup_swappiness_write,
5957 },
5958 {
5959 .name = "move_charge_at_immigrate",
5960 .read_u64 = mem_cgroup_move_charge_read,
5961 .write_u64 = mem_cgroup_move_charge_write,
5962 },
5963 {
5964 .name = "oom_control",
5965 .read_map = mem_cgroup_oom_control_read,
5966 .write_u64 = mem_cgroup_oom_control_write,
5967 .register_event = mem_cgroup_oom_register_event,
5968 .unregister_event = mem_cgroup_oom_unregister_event,
5969 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5970 },
5971 {
5972 .name = "pressure_level",
5973 .register_event = vmpressure_register_event,
5974 .unregister_event = vmpressure_unregister_event,
5975 },
5976#ifdef CONFIG_NUMA
5977 {
5978 .name = "numa_stat",
5979 .read_seq_string = memcg_numa_stat_show,
5980 },
5981#endif
5982#ifdef CONFIG_MEMCG_KMEM
5983 {
5984 .name = "kmem.limit_in_bytes",
5985 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5986 .write_string = mem_cgroup_write,
5987 .read = mem_cgroup_read,
5988 },
5989 {
5990 .name = "kmem.usage_in_bytes",
5991 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5992 .read = mem_cgroup_read,
5993 },
5994 {
5995 .name = "kmem.failcnt",
5996 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5997 .trigger = mem_cgroup_reset,
5998 .read = mem_cgroup_read,
5999 },
6000 {
6001 .name = "kmem.max_usage_in_bytes",
6002 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
6003 .trigger = mem_cgroup_reset,
6004 .read = mem_cgroup_read,
6005 },
6006#ifdef CONFIG_SLABINFO
6007 {
6008 .name = "kmem.slabinfo",
6009 .read_seq_string = mem_cgroup_slabinfo_read,
6010 },
6011#endif
6012#endif
6013 { },
6014};
6015
6016#ifdef CONFIG_MEMCG_SWAP
6017static struct cftype memsw_cgroup_files[] = {
6018 {
6019 .name = "memsw.usage_in_bytes",
6020 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
6021 .read = mem_cgroup_read,
6022 .register_event = mem_cgroup_usage_register_event,
6023 .unregister_event = mem_cgroup_usage_unregister_event,
6024 },
6025 {
6026 .name = "memsw.max_usage_in_bytes",
6027 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
6028 .trigger = mem_cgroup_reset,
6029 .read = mem_cgroup_read,
6030 },
6031 {
6032 .name = "memsw.limit_in_bytes",
6033 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
6034 .write_string = mem_cgroup_write,
6035 .read = mem_cgroup_read,
6036 },
6037 {
6038 .name = "memsw.failcnt",
6039 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
6040 .trigger = mem_cgroup_reset,
6041 .read = mem_cgroup_read,
6042 },
6043 { },
6044};
6045#endif
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071static DEFINE_IDR(mem_cgroup_idr);
6072
6073static DEFINE_MUTEX(mem_cgroup_idr_lock);
6074
6075static unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
6076{
6077 return memcg->id;
6078}
6079
6080static void mem_cgroup_id_put(struct mem_cgroup *memcg)
6081{
6082 mutex_lock(&mem_cgroup_idr_lock);
6083 idr_remove(&mem_cgroup_idr, memcg->id);
6084 mutex_unlock(&mem_cgroup_idr_lock);
6085 memcg->id = 0;
6086 synchronize_rcu();
6087}
6088
6089
6090
6091
6092
6093
6094
6095struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
6096{
6097 WARN_ON_ONCE(!rcu_read_lock_held());
6098 return idr_find(&mem_cgroup_idr, id);
6099}
6100
6101static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
6102{
6103 struct mem_cgroup_per_node *pn;
6104 struct mem_cgroup_per_zone *mz;
6105 int zone, tmp = node;
6106
6107
6108
6109
6110
6111
6112
6113
6114 if (!node_state(node, N_NORMAL_MEMORY))
6115 tmp = -1;
6116 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
6117 if (!pn)
6118 return 1;
6119
6120 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6121 mz = &pn->zoneinfo[zone];
6122 lruvec_init(&mz->lruvec);
6123 mz->usage_in_excess = 0;
6124 mz->on_tree = false;
6125 mz->memcg = memcg;
6126 }
6127 memcg->info.nodeinfo[node] = pn;
6128 return 0;
6129}
6130
6131static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
6132{
6133 kfree(memcg->info.nodeinfo[node]);
6134}
6135
6136static struct mem_cgroup *mem_cgroup_alloc(void)
6137{
6138 struct mem_cgroup *memcg;
6139 size_t size = memcg_size();
6140 int id;
6141
6142
6143 if (size < PAGE_SIZE)
6144 memcg = kzalloc(size, GFP_KERNEL);
6145 else
6146 memcg = vzalloc(size);
6147
6148 if (!memcg)
6149 return NULL;
6150
6151 mutex_lock(&mem_cgroup_idr_lock);
6152 id = idr_alloc(&mem_cgroup_idr, NULL,
6153 1, MEM_CGROUP_ID_MAX,
6154 GFP_KERNEL);
6155 mutex_unlock(&mem_cgroup_idr_lock);
6156 if (id < 0)
6157 goto fail;
6158
6159 memcg->id = id;
6160
6161 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
6162 if (!memcg->stat)
6163 goto out_free;
6164 spin_lock_init(&memcg->pcp_counter_lock);
6165 mutex_lock(&mem_cgroup_idr_lock);
6166 idr_replace(&mem_cgroup_idr, memcg, memcg->id);
6167 mutex_unlock(&mem_cgroup_idr_lock);
6168 synchronize_rcu();
6169 return memcg;
6170
6171out_free:
6172 if (memcg->id > 0) {
6173 mutex_lock(&mem_cgroup_idr_lock);
6174 idr_remove(&mem_cgroup_idr, memcg->id);
6175 mutex_unlock(&mem_cgroup_idr_lock);
6176 synchronize_rcu();
6177 }
6178fail:
6179 if (size < PAGE_SIZE)
6180 kfree(memcg);
6181 else
6182 vfree(memcg);
6183 return NULL;
6184}
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197static void __mem_cgroup_free(struct mem_cgroup *memcg)
6198{
6199 int node;
6200 size_t size = memcg_size();
6201
6202 mem_cgroup_remove_from_trees(memcg);
6203
6204 mem_cgroup_id_put(memcg);
6205
6206 for_each_node(node)
6207 free_mem_cgroup_per_zone_info(memcg, node);
6208
6209 free_percpu(memcg->stat);
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222 disarm_static_keys(memcg);
6223 if (size < PAGE_SIZE)
6224 kfree(memcg);
6225 else
6226 vfree(memcg);
6227}
6228
6229
6230
6231
6232
6233
6234
6235static void free_work(struct work_struct *work)
6236{
6237 struct mem_cgroup *memcg;
6238
6239 memcg = container_of(work, struct mem_cgroup, work_freeing);
6240 __mem_cgroup_free(memcg);
6241}
6242
6243static void free_rcu(struct rcu_head *rcu_head)
6244{
6245 struct mem_cgroup *memcg;
6246
6247 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
6248 INIT_WORK(&memcg->work_freeing, free_work);
6249 schedule_work(&memcg->work_freeing);
6250}
6251
6252static void mem_cgroup_get(struct mem_cgroup *memcg)
6253{
6254 atomic_inc(&memcg->refcnt);
6255}
6256
6257static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
6258{
6259 if (atomic_sub_and_test(count, &memcg->refcnt)) {
6260 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
6261 call_rcu(&memcg->rcu_freeing, free_rcu);
6262 if (parent)
6263 mem_cgroup_put(parent);
6264 }
6265}
6266
6267static void mem_cgroup_put(struct mem_cgroup *memcg)
6268{
6269 __mem_cgroup_put(memcg, 1);
6270}
6271
6272
6273
6274
6275struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
6276{
6277 if (!memcg->memory.parent)
6278 return NULL;
6279 return mem_cgroup_from_counter(memcg->memory.parent, memory);
6280}
6281EXPORT_SYMBOL(parent_mem_cgroup);
6282
6283static void __init mem_cgroup_soft_limit_tree_init(void)
6284{
6285 struct mem_cgroup_tree_per_node *rtpn;
6286 struct mem_cgroup_tree_per_zone *rtpz;
6287 int tmp, node, zone;
6288
6289 for_each_node(node) {
6290 tmp = node;
6291 if (!node_state(node, N_NORMAL_MEMORY))
6292 tmp = -1;
6293 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
6294 BUG_ON(!rtpn);
6295
6296 soft_limit_tree.rb_tree_per_node[node] = rtpn;
6297
6298 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6299 rtpz = &rtpn->rb_tree_per_zone[zone];
6300 rtpz->rb_root = RB_ROOT;
6301 spin_lock_init(&rtpz->lock);
6302 }
6303 }
6304}
6305
6306static struct cgroup_subsys_state * __ref
6307mem_cgroup_css_alloc(struct cgroup *cont)
6308{
6309 struct mem_cgroup *memcg;
6310 long error = -ENOMEM;
6311 int node;
6312
6313 memcg = mem_cgroup_alloc();
6314 if (!memcg)
6315 return ERR_PTR(error);
6316
6317 for_each_node(node)
6318 if (alloc_mem_cgroup_per_zone_info(memcg, node))
6319 goto free_out;
6320
6321
6322 if (cont->parent == NULL) {
6323 root_mem_cgroup = memcg;
6324 page_counter_init(&memcg->memory, NULL);
6325 memcg->soft_limit = PAGE_COUNTER_MAX;
6326 page_counter_init(&memcg->memsw, NULL);
6327 page_counter_init(&memcg->kmem, NULL);
6328 }
6329
6330 memcg->last_scanned_node = MAX_NUMNODES;
6331 INIT_LIST_HEAD(&memcg->oom_notify);
6332 atomic_set(&memcg->refcnt, 1);
6333 memcg->move_charge_at_immigrate = 0;
6334 mutex_init(&memcg->thresholds_lock);
6335 spin_lock_init(&memcg->move_lock);
6336 vmpressure_init(&memcg->vmpressure);
6337
6338 return &memcg->css;
6339
6340free_out:
6341 __mem_cgroup_free(memcg);
6342 return ERR_PTR(error);
6343}
6344
6345static int
6346mem_cgroup_css_online(struct cgroup *cont)
6347{
6348 struct mem_cgroup *memcg, *parent;
6349 int error = 0;
6350
6351 if (!cont->parent)
6352 return 0;
6353
6354 mutex_lock(&memcg_create_mutex);
6355 memcg = mem_cgroup_from_cont(cont);
6356 parent = mem_cgroup_from_cont(cont->parent);
6357
6358 memcg->use_hierarchy = parent->use_hierarchy;
6359 memcg->oom_kill_disable = parent->oom_kill_disable;
6360 memcg->swappiness = mem_cgroup_swappiness(parent);
6361
6362 if (parent->use_hierarchy) {
6363 page_counter_init(&memcg->memory, &parent->memory);
6364 memcg->soft_limit = PAGE_COUNTER_MAX;
6365 page_counter_init(&memcg->memsw, &parent->memsw);
6366 page_counter_init(&memcg->kmem, &parent->kmem);
6367
6368
6369
6370
6371
6372
6373
6374 mem_cgroup_get(parent);
6375 } else {
6376 page_counter_init(&memcg->memory, NULL);
6377 memcg->soft_limit = PAGE_COUNTER_MAX;
6378 page_counter_init(&memcg->memsw, NULL);
6379 page_counter_init(&memcg->kmem, NULL);
6380
6381
6382
6383
6384
6385 if (parent != root_mem_cgroup)
6386 mem_cgroup_subsys.broken_hierarchy = true;
6387 }
6388
6389 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
6390 mutex_unlock(&memcg_create_mutex);
6391 return error;
6392}
6393
6394
6395
6396
6397static void mem_cgroup_invalidate_reclaim_iterators(struct mem_cgroup *memcg)
6398{
6399 struct mem_cgroup *parent = memcg;
6400
6401 while ((parent = parent_mem_cgroup(parent)))
6402 mem_cgroup_iter_invalidate(parent);
6403
6404
6405
6406
6407
6408 if (!root_mem_cgroup->use_hierarchy)
6409 mem_cgroup_iter_invalidate(root_mem_cgroup);
6410}
6411
6412static void mem_cgroup_css_offline(struct cgroup *cont)
6413{
6414 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
6415 struct cgroup *iter;
6416
6417 mem_cgroup_invalidate_reclaim_iterators(memcg);
6418
6419
6420
6421
6422
6423 rcu_read_lock();
6424 cgroup_for_each_descendant_post(iter, cont) {
6425 rcu_read_unlock();
6426 mem_cgroup_reparent_charges(mem_cgroup_from_cont(iter));
6427 rcu_read_lock();
6428 }
6429 rcu_read_unlock();
6430 mem_cgroup_reparent_charges(memcg);
6431
6432 mem_cgroup_destroy_all_caches(memcg);
6433 vmpressure_cleanup(&memcg->vmpressure);
6434}
6435
6436static void mem_cgroup_css_free(struct cgroup *cont)
6437{
6438 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
6439
6440 kmem_cgroup_destroy(memcg);
6441
6442 mem_cgroup_put(memcg);
6443}
6444
6445#ifdef CONFIG_MMU
6446
6447#define PRECHARGE_COUNT_AT_ONCE 256
6448static int mem_cgroup_do_precharge(unsigned long count)
6449{
6450 int ret = 0;
6451 int batch_count = PRECHARGE_COUNT_AT_ONCE;
6452 struct mem_cgroup *memcg = mc.to;
6453
6454 if (mem_cgroup_is_root(memcg)) {
6455 mc.precharge += count;
6456
6457 return ret;
6458 }
6459
6460 if (count > 1) {
6461 struct page_counter *dummy;
6462
6463
6464
6465
6466
6467
6468 if (!page_counter_try_charge(&memcg->memory, count, &dummy))
6469 goto one_by_one;
6470 if (do_swap_account &&
6471 !page_counter_try_charge(&memcg->memsw, count, &dummy)) {
6472 page_counter_uncharge(&memcg->memory, count);
6473 goto one_by_one;
6474 }
6475 mc.precharge += count;
6476 return ret;
6477 }
6478one_by_one:
6479
6480 while (count--) {
6481 if (signal_pending(current)) {
6482 ret = -EINTR;
6483 break;
6484 }
6485 if (!batch_count--) {
6486 batch_count = PRECHARGE_COUNT_AT_ONCE;
6487 cond_resched();
6488 }
6489 ret = __mem_cgroup_try_charge(NULL,
6490 GFP_KERNEL, 1, &memcg, false);
6491 if (ret)
6492
6493 return ret;
6494 mc.precharge++;
6495 }
6496 return ret;
6497}
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517union mc_target {
6518 struct page *page;
6519 swp_entry_t ent;
6520};
6521
6522enum mc_target_type {
6523 MC_TARGET_NONE = 0,
6524 MC_TARGET_PAGE,
6525 MC_TARGET_SWAP,
6526};
6527
6528static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6529 unsigned long addr, pte_t ptent)
6530{
6531 struct page *page = vm_normal_page(vma, addr, ptent);
6532
6533 if (!page || !page_mapped(page))
6534 return NULL;
6535 if (PageAnon(page)) {
6536
6537 if (!move_anon())
6538 return NULL;
6539 } else if (!move_file())
6540
6541 return NULL;
6542 if (!get_page_unless_zero(page))
6543 return NULL;
6544
6545 return page;
6546}
6547
6548#ifdef CONFIG_SWAP
6549static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6550 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6551{
6552 struct page *page = NULL;
6553 swp_entry_t ent = pte_to_swp_entry(ptent);
6554
6555 if (!move_anon() || non_swap_entry(ent))
6556 return NULL;
6557
6558
6559
6560
6561 page = find_get_page(swap_address_space(ent), ent.val);
6562 if (do_swap_account)
6563 entry->val = ent.val;
6564
6565 return page;
6566}
6567#else
6568static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6569 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6570{
6571 return NULL;
6572}
6573#endif
6574
6575static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6576 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6577{
6578 struct page *page = NULL;
6579 struct address_space *mapping;
6580 pgoff_t pgoff;
6581
6582 if (!vma->vm_file)
6583 return NULL;
6584 if (!move_file())
6585 return NULL;
6586
6587 mapping = vma->vm_file->f_mapping;
6588 if (pte_none(ptent))
6589 pgoff = linear_page_index(vma, addr);
6590 else
6591 pgoff = pte_to_pgoff(ptent);
6592
6593
6594#ifdef CONFIG_SWAP
6595
6596 if (shmem_mapping(mapping)) {
6597 page = __find_get_page(mapping, pgoff);
6598 if (radix_tree_exceptional_entry(page)) {
6599 swp_entry_t swp = radix_to_swp_entry(page);
6600 if (do_swap_account)
6601 *entry = swp;
6602 page = find_get_page(swap_address_space(swp), swp.val);
6603 }
6604 } else
6605 page = find_get_page(mapping, pgoff);
6606#else
6607 page = find_get_page(mapping, pgoff);
6608#endif
6609 return page;
6610}
6611
6612static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
6613 unsigned long addr, pte_t ptent, union mc_target *target)
6614{
6615 struct page *page = NULL;
6616 struct page_cgroup *pc;
6617 enum mc_target_type ret = MC_TARGET_NONE;
6618 swp_entry_t ent = { .val = 0 };
6619
6620 if (pte_present(ptent))
6621 page = mc_handle_present_pte(vma, addr, ptent);
6622 else if (is_swap_pte(ptent))
6623 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
6624 else if (pte_none(ptent) || pte_file(ptent))
6625 page = mc_handle_file_pte(vma, addr, ptent, &ent);
6626
6627 if (!page && !ent.val)
6628 return ret;
6629 if (page) {
6630 pc = lookup_page_cgroup(page);
6631
6632
6633
6634
6635
6636 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6637 ret = MC_TARGET_PAGE;
6638 if (target)
6639 target->page = page;
6640 }
6641 if (!ret || !target)
6642 put_page(page);
6643 }
6644
6645 if (ent.val && !ret &&
6646 mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
6647 ret = MC_TARGET_SWAP;
6648 if (target)
6649 target->ent = ent;
6650 }
6651 return ret;
6652}
6653
6654#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6655
6656
6657
6658
6659
6660static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6661 unsigned long addr, pmd_t pmd, union mc_target *target)
6662{
6663 struct page *page = NULL;
6664 struct page_cgroup *pc;
6665 enum mc_target_type ret = MC_TARGET_NONE;
6666
6667 page = pmd_page(pmd);
6668 VM_BUG_ON_PAGE(!page || !PageHead(page), page);
6669 if (!move_anon())
6670 return ret;
6671 pc = lookup_page_cgroup(page);
6672 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6673 ret = MC_TARGET_PAGE;
6674 if (target) {
6675 get_page(page);
6676 target->page = page;
6677 }
6678 }
6679 return ret;
6680}
6681#else
6682static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6683 unsigned long addr, pmd_t pmd, union mc_target *target)
6684{
6685 return MC_TARGET_NONE;
6686}
6687#endif
6688
6689static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6690 unsigned long addr, unsigned long end,
6691 struct mm_walk *walk)
6692{
6693 struct vm_area_struct *vma = walk->private;
6694 pte_t *pte;
6695 spinlock_t *ptl;
6696
6697 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
6698 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6699 mc.precharge += HPAGE_PMD_NR;
6700 spin_unlock(ptl);
6701 return 0;
6702 }
6703
6704 if (pmd_trans_unstable(pmd))
6705 return 0;
6706 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6707 for (; addr != end; pte++, addr += PAGE_SIZE)
6708 if (get_mctgt_type(vma, addr, *pte, NULL))
6709 mc.precharge++;
6710 pte_unmap_unlock(pte - 1, ptl);
6711 cond_resched();
6712
6713 return 0;
6714}
6715
6716static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6717{
6718 unsigned long precharge;
6719 struct vm_area_struct *vma;
6720
6721 down_read(&mm->mmap_sem);
6722 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6723 struct mm_walk mem_cgroup_count_precharge_walk = {
6724 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6725 .mm = mm,
6726 .private = vma,
6727 };
6728 if (is_vm_hugetlb_page(vma))
6729 continue;
6730 walk_page_range(vma->vm_start, vma->vm_end,
6731 &mem_cgroup_count_precharge_walk);
6732 }
6733 up_read(&mm->mmap_sem);
6734
6735 precharge = mc.precharge;
6736 mc.precharge = 0;
6737
6738 return precharge;
6739}
6740
6741static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6742{
6743 unsigned long precharge = mem_cgroup_count_precharge(mm);
6744
6745 VM_BUG_ON(mc.moving_task);
6746 mc.moving_task = current;
6747 return mem_cgroup_do_precharge(precharge);
6748}
6749
6750
6751static void __mem_cgroup_clear_mc(void)
6752{
6753 struct mem_cgroup *from = mc.from;
6754 struct mem_cgroup *to = mc.to;
6755
6756
6757 if (mc.precharge) {
6758 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6759 mc.precharge = 0;
6760 }
6761
6762
6763
6764
6765 if (mc.moved_charge) {
6766 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6767 mc.moved_charge = 0;
6768 }
6769
6770 if (mc.moved_swap) {
6771
6772 if (!mem_cgroup_is_root(mc.from))
6773 page_counter_uncharge(&mc.from->memsw, mc.moved_swap);
6774
6775 if (!mem_cgroup_is_root(mc.to)) {
6776
6777
6778
6779
6780 page_counter_uncharge(&mc.to->memory, mc.moved_swap);
6781 }
6782 __mem_cgroup_put(mc.from, mc.moved_swap);
6783
6784
6785 mc.moved_swap = 0;
6786 }
6787 memcg_oom_recover(from);
6788 memcg_oom_recover(to);
6789 wake_up_all(&mc.waitq);
6790}
6791
6792static void mem_cgroup_clear_mc(void)
6793{
6794 struct mem_cgroup *from = mc.from;
6795
6796
6797
6798
6799
6800 mc.moving_task = NULL;
6801 __mem_cgroup_clear_mc();
6802 spin_lock(&mc.lock);
6803 mc.from = NULL;
6804 mc.to = NULL;
6805 spin_unlock(&mc.lock);
6806 mem_cgroup_end_move(from);
6807}
6808
6809static int mem_cgroup_can_attach(struct cgroup *cgroup,
6810 struct cgroup_taskset *tset)
6811{
6812 struct task_struct *p = cgroup_taskset_first(tset);
6813 int ret = 0;
6814 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
6815 unsigned long move_charge_at_immigrate;
6816
6817
6818
6819
6820
6821
6822 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6823 if (move_charge_at_immigrate) {
6824 struct mm_struct *mm;
6825 struct mem_cgroup *from = mem_cgroup_from_task(p);
6826
6827 VM_BUG_ON(from == memcg);
6828
6829 mm = get_task_mm(p);
6830 if (!mm)
6831 return 0;
6832
6833 if (mm->owner == p) {
6834 VM_BUG_ON(mc.from);
6835 VM_BUG_ON(mc.to);
6836 VM_BUG_ON(mc.precharge);
6837 VM_BUG_ON(mc.moved_charge);
6838 VM_BUG_ON(mc.moved_swap);
6839 mem_cgroup_start_move(from);
6840 spin_lock(&mc.lock);
6841 mc.from = from;
6842 mc.to = memcg;
6843 mc.immigrate_flags = move_charge_at_immigrate;
6844 spin_unlock(&mc.lock);
6845
6846
6847 ret = mem_cgroup_precharge_mc(mm);
6848 if (ret)
6849 mem_cgroup_clear_mc();
6850 }
6851 mmput(mm);
6852 }
6853 return ret;
6854}
6855
6856static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6857 struct cgroup_taskset *tset)
6858{
6859 mem_cgroup_clear_mc();
6860}
6861
6862static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6863 unsigned long addr, unsigned long end,
6864 struct mm_walk *walk)
6865{
6866 int ret = 0;
6867 struct vm_area_struct *vma = walk->private;
6868 pte_t *pte;
6869 spinlock_t *ptl;
6870 enum mc_target_type target_type;
6871 union mc_target target;
6872 struct page *page;
6873 struct page_cgroup *pc;
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885 if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
6886 if (mc.precharge < HPAGE_PMD_NR) {
6887 spin_unlock(ptl);
6888 return 0;
6889 }
6890 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6891 if (target_type == MC_TARGET_PAGE) {
6892 page = target.page;
6893 if (!isolate_lru_page(page)) {
6894 pc = lookup_page_cgroup(page);
6895 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
6896 pc, mc.from, mc.to)) {
6897 mc.precharge -= HPAGE_PMD_NR;
6898 mc.moved_charge += HPAGE_PMD_NR;
6899 }
6900 putback_lru_page(page);
6901 }
6902 put_page(page);
6903 }
6904 spin_unlock(ptl);
6905 return 0;
6906 }
6907
6908 if (pmd_trans_unstable(pmd))
6909 return 0;
6910retry:
6911 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6912 for (; addr != end; addr += PAGE_SIZE) {
6913 pte_t ptent = *(pte++);
6914 swp_entry_t ent;
6915
6916 if (!mc.precharge)
6917 break;
6918
6919 switch (get_mctgt_type(vma, addr, ptent, &target)) {
6920 case MC_TARGET_PAGE:
6921 page = target.page;
6922 if (isolate_lru_page(page))
6923 goto put;
6924 pc = lookup_page_cgroup(page);
6925 if (!mem_cgroup_move_account(page, 1, pc,
6926 mc.from, mc.to)) {
6927 mc.precharge--;
6928
6929 mc.moved_charge++;
6930 }
6931 putback_lru_page(page);
6932put:
6933 put_page(page);
6934 break;
6935 case MC_TARGET_SWAP:
6936 ent = target.ent;
6937 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
6938 mc.precharge--;
6939
6940 mc.moved_swap++;
6941 }
6942 break;
6943 default:
6944 break;
6945 }
6946 }
6947 pte_unmap_unlock(pte - 1, ptl);
6948 cond_resched();
6949
6950 if (addr != end) {
6951
6952
6953
6954
6955
6956
6957 ret = mem_cgroup_do_precharge(1);
6958 if (!ret)
6959 goto retry;
6960 }
6961
6962 return ret;
6963}
6964
6965static void mem_cgroup_move_charge(struct mm_struct *mm)
6966{
6967 struct vm_area_struct *vma;
6968
6969 lru_add_drain_all();
6970retry:
6971 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6972
6973
6974
6975
6976
6977
6978
6979 __mem_cgroup_clear_mc();
6980 cond_resched();
6981 goto retry;
6982 }
6983 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6984 int ret;
6985 struct mm_walk mem_cgroup_move_charge_walk = {
6986 .pmd_entry = mem_cgroup_move_charge_pte_range,
6987 .mm = mm,
6988 .private = vma,
6989 };
6990 if (is_vm_hugetlb_page(vma))
6991 continue;
6992 ret = walk_page_range(vma->vm_start, vma->vm_end,
6993 &mem_cgroup_move_charge_walk);
6994 if (ret)
6995
6996
6997
6998
6999 break;
7000 }
7001 up_read(&mm->mmap_sem);
7002}
7003
7004static void mem_cgroup_move_task(struct cgroup *cont,
7005 struct cgroup_taskset *tset)
7006{
7007 struct task_struct *p = cgroup_taskset_first(tset);
7008 struct mm_struct *mm = get_task_mm(p);
7009
7010 if (mm) {
7011 if (mc.to)
7012 mem_cgroup_move_charge(mm);
7013 mmput(mm);
7014 }
7015 if (mc.to)
7016 mem_cgroup_clear_mc();
7017}
7018#else
7019static int mem_cgroup_can_attach(struct cgroup *cgroup,
7020 struct cgroup_taskset *tset)
7021{
7022 return 0;
7023}
7024static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
7025 struct cgroup_taskset *tset)
7026{
7027}
7028static void mem_cgroup_move_task(struct cgroup *cont,
7029 struct cgroup_taskset *tset)
7030{
7031}
7032#endif
7033
7034
7035
7036
7037
7038static void mem_cgroup_bind(struct cgroup *root)
7039{
7040
7041
7042
7043
7044
7045 if (cgroup_sane_behavior(root))
7046 mem_cgroup_from_cont(root)->use_hierarchy = true;
7047}
7048
7049struct cgroup_subsys mem_cgroup_subsys = {
7050 .name = "memory",
7051 .subsys_id = mem_cgroup_subsys_id,
7052 .css_alloc = mem_cgroup_css_alloc,
7053 .css_online = mem_cgroup_css_online,
7054 .css_offline = mem_cgroup_css_offline,
7055 .css_free = mem_cgroup_css_free,
7056 .can_attach = mem_cgroup_can_attach,
7057 .cancel_attach = mem_cgroup_cancel_attach,
7058 .attach = mem_cgroup_move_task,
7059 .bind = mem_cgroup_bind,
7060 .base_cftypes = mem_cgroup_files,
7061 .early_init = 0,
7062};
7063
7064#ifdef CONFIG_MEMCG_SWAP
7065static int __init enable_swap_account(char *s)
7066{
7067
7068 if (!strcmp(s, "1"))
7069 really_do_swap_account = 1;
7070 else if (!strcmp(s, "0"))
7071 really_do_swap_account = 0;
7072 return 1;
7073}
7074__setup("swapaccount=", enable_swap_account);
7075
7076static void __init memsw_file_init(void)
7077{
7078 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
7079}
7080
7081static void __init enable_swap_cgroup(void)
7082{
7083 if (!mem_cgroup_disabled() && really_do_swap_account) {
7084 do_swap_account = 1;
7085 memsw_file_init();
7086 }
7087}
7088
7089#else
7090static void __init enable_swap_cgroup(void)
7091{
7092}
7093#endif
7094
7095static int __init cgroup_memory(char *s)
7096{
7097 char *token;
7098
7099 while ((token = strsep(&s, ",")) != NULL) {
7100 if (!*token)
7101 continue;
7102 if (!strcmp(token, "nokmem"))
7103 cgroup_memory_nokmem = true;
7104 }
7105 return 0;
7106}
7107__setup("cgroup.memory=", cgroup_memory);
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117static int __init mem_cgroup_init(void)
7118{
7119 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
7120 enable_swap_cgroup();
7121 mem_cgroup_soft_limit_tree_init();
7122 memcg_stock_init();
7123 return 0;
7124}
7125subsys_initcall(mem_cgroup_init);
7126