1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #ifndef __MM_MEMCONTROL_V1_H
4 #define __MM_MEMCONTROL_V1_H
6 #include <linux/cgroup-defs.h>
8 /* Cgroup v1 and v2 common declarations */
10 int try_charge_memcg(struct mem_cgroup
*memcg
, gfp_t gfp_mask
,
11 unsigned int nr_pages
);
13 static inline int try_charge(struct mem_cgroup
*memcg
, gfp_t gfp_mask
,
14 unsigned int nr_pages
)
16 if (mem_cgroup_is_root(memcg
))
19 return try_charge_memcg(memcg
, gfp_mask
, nr_pages
);
22 void mem_cgroup_id_get_many(struct mem_cgroup
*memcg
, unsigned int n
);
23 void mem_cgroup_id_put_many(struct mem_cgroup
*memcg
, unsigned int n
);
26 * Iteration constructs for visiting all cgroups (under a tree). If
27 * loops are exited prematurely (break), mem_cgroup_iter_break() must
28 * be used for reference counting.
30 #define for_each_mem_cgroup_tree(iter, root) \
31 for (iter = mem_cgroup_iter(root, NULL, NULL); \
33 iter = mem_cgroup_iter(root, iter, NULL))
35 #define for_each_mem_cgroup(iter) \
36 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
38 iter = mem_cgroup_iter(NULL, iter, NULL))
40 /* Whether legacy memory+swap accounting is active */
41 static inline bool do_memsw_account(void)
43 return !cgroup_subsys_on_dfl(memory_cgrp_subsys
);
47 * Per memcg event counter is incremented at every pagein/pageout. With THP,
48 * it will be incremented by the number of pages. This counter is used
49 * to trigger some periodic events. This is straightforward and better
50 * than using jiffies etc. to handle periodic memcg event.
52 enum mem_cgroup_events_target
{
53 MEM_CGROUP_TARGET_THRESH
,
54 MEM_CGROUP_TARGET_SOFTLIMIT
,
58 unsigned long mem_cgroup_usage(struct mem_cgroup
*memcg
, bool swap
);
60 void drain_all_stock(struct mem_cgroup
*root_memcg
);
62 unsigned long memcg_events(struct mem_cgroup
*memcg
, int event
);
63 unsigned long memcg_events_local(struct mem_cgroup
*memcg
, int event
);
64 unsigned long memcg_page_state_local(struct mem_cgroup
*memcg
, int idx
);
65 unsigned long memcg_page_state_output(struct mem_cgroup
*memcg
, int item
);
66 unsigned long memcg_page_state_local_output(struct mem_cgroup
*memcg
, int item
);
67 int memory_stat_show(struct seq_file
*m
, void *v
);
69 /* Cgroup v1-specific declarations */
70 #ifdef CONFIG_MEMCG_V1
72 bool memcg1_alloc_events(struct mem_cgroup
*memcg
);
73 void memcg1_free_events(struct mem_cgroup
*memcg
);
75 void memcg1_memcg_init(struct mem_cgroup
*memcg
);
76 void memcg1_remove_from_trees(struct mem_cgroup
*memcg
);
78 static inline void memcg1_soft_limit_reset(struct mem_cgroup
*memcg
)
80 WRITE_ONCE(memcg
->soft_limit
, PAGE_COUNTER_MAX
);
83 struct cgroup_taskset
;
84 void memcg1_css_offline(struct mem_cgroup
*memcg
);
86 /* for encoding cft->private value on file */
94 bool memcg1_oom_prepare(struct mem_cgroup
*memcg
, bool *locked
);
95 void memcg1_oom_finish(struct mem_cgroup
*memcg
, bool locked
);
96 void memcg1_oom_recover(struct mem_cgroup
*memcg
);
98 void memcg1_commit_charge(struct folio
*folio
, struct mem_cgroup
*memcg
);
99 void memcg1_swapout(struct folio
*folio
, struct mem_cgroup
*memcg
);
100 void memcg1_uncharge_batch(struct mem_cgroup
*memcg
, unsigned long pgpgout
,
101 unsigned long nr_memory
, int nid
);
103 void memcg1_stat_format(struct mem_cgroup
*memcg
, struct seq_buf
*s
);
105 void memcg1_account_kmem(struct mem_cgroup
*memcg
, int nr_pages
);
106 static inline bool memcg1_tcpmem_active(struct mem_cgroup
*memcg
)
108 return memcg
->tcpmem_active
;
110 bool memcg1_charge_skmem(struct mem_cgroup
*memcg
, unsigned int nr_pages
,
112 static inline void memcg1_uncharge_skmem(struct mem_cgroup
*memcg
, unsigned int nr_pages
)
114 page_counter_uncharge(&memcg
->tcpmem
, nr_pages
);
117 extern struct cftype memsw_files
[];
118 extern struct cftype mem_cgroup_legacy_files
[];
120 #else /* CONFIG_MEMCG_V1 */
122 static inline bool memcg1_alloc_events(struct mem_cgroup
*memcg
) { return true; }
123 static inline void memcg1_free_events(struct mem_cgroup
*memcg
) {}
125 static inline void memcg1_memcg_init(struct mem_cgroup
*memcg
) {}
126 static inline void memcg1_remove_from_trees(struct mem_cgroup
*memcg
) {}
127 static inline void memcg1_soft_limit_reset(struct mem_cgroup
*memcg
) {}
128 static inline void memcg1_css_offline(struct mem_cgroup
*memcg
) {}
130 static inline bool memcg1_oom_prepare(struct mem_cgroup
*memcg
, bool *locked
) { return true; }
131 static inline void memcg1_oom_finish(struct mem_cgroup
*memcg
, bool locked
) {}
132 static inline void memcg1_oom_recover(struct mem_cgroup
*memcg
) {}
134 static inline void memcg1_commit_charge(struct folio
*folio
,
135 struct mem_cgroup
*memcg
) {}
137 static inline void memcg1_swapout(struct folio
*folio
, struct mem_cgroup
*memcg
) {}
139 static inline void memcg1_uncharge_batch(struct mem_cgroup
*memcg
,
140 unsigned long pgpgout
,
141 unsigned long nr_memory
, int nid
) {}
143 static inline void memcg1_stat_format(struct mem_cgroup
*memcg
, struct seq_buf
*s
) {}
145 static inline void memcg1_account_kmem(struct mem_cgroup
*memcg
, int nr_pages
) {}
146 static inline bool memcg1_tcpmem_active(struct mem_cgroup
*memcg
) { return false; }
147 static inline bool memcg1_charge_skmem(struct mem_cgroup
*memcg
, unsigned int nr_pages
,
148 gfp_t gfp_mask
) { return true; }
149 static inline void memcg1_uncharge_skmem(struct mem_cgroup
*memcg
, unsigned int nr_pages
) {}
151 #endif /* CONFIG_MEMCG_V1 */
153 #endif /* __MM_MEMCONTROL_V1_H */