160 lines · c
1/* SPDX-License-Identifier: GPL-2.0-or-later */2 3#ifndef __MM_MEMCONTROL_V1_H4#define __MM_MEMCONTROL_V1_H5 6#include <linux/cgroup-defs.h>7 8/* Cgroup v1 and v2 common declarations */9 10int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,11 unsigned int nr_pages);12 13static inline int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,14 unsigned int nr_pages)15{16 if (mem_cgroup_is_root(memcg))17 return 0;18 19 return try_charge_memcg(memcg, gfp_mask, nr_pages);20}21 22void mem_cgroup_id_get_many(struct mem_cgroup *memcg, unsigned int n);23void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n);24 25/*26 * Iteration constructs for visiting all cgroups (under a tree). If27 * loops are exited prematurely (break), mem_cgroup_iter_break() must28 * be used for reference counting.29 */30#define for_each_mem_cgroup_tree(iter, root) \31 for (iter = mem_cgroup_iter(root, NULL, NULL); \32 iter != NULL; \33 iter = mem_cgroup_iter(root, iter, NULL))34 35#define for_each_mem_cgroup(iter) \36 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \37 iter != NULL; \38 iter = mem_cgroup_iter(NULL, iter, NULL))39 40/* Whether legacy memory+swap accounting is active */41static bool do_memsw_account(void)42{43 return !cgroup_subsys_on_dfl(memory_cgrp_subsys);44}45 46/*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 used49 * to trigger some periodic events. This is straightforward and better50 * than using jiffies etc. to handle periodic memcg event.51 */52enum mem_cgroup_events_target {53 MEM_CGROUP_TARGET_THRESH,54 MEM_CGROUP_TARGET_SOFTLIMIT,55 MEM_CGROUP_NTARGETS,56};57 58unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap);59 60void drain_all_stock(struct mem_cgroup *root_memcg);61 62unsigned long memcg_events(struct mem_cgroup *memcg, int event);63unsigned long memcg_events_local(struct mem_cgroup *memcg, int event);64unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx);65unsigned long memcg_page_state_output(struct mem_cgroup *memcg, int item);66unsigned long memcg_page_state_local_output(struct mem_cgroup *memcg, int item);67int memory_stat_show(struct seq_file *m, void *v);68 69/* Cgroup v1-specific declarations */70#ifdef CONFIG_MEMCG_V171 72bool memcg1_alloc_events(struct mem_cgroup *memcg);73void memcg1_free_events(struct mem_cgroup *memcg);74 75void memcg1_memcg_init(struct mem_cgroup *memcg);76void memcg1_remove_from_trees(struct mem_cgroup *memcg);77 78static inline void memcg1_soft_limit_reset(struct mem_cgroup *memcg)79{80 WRITE_ONCE(memcg->soft_limit, PAGE_COUNTER_MAX);81}82 83bool memcg1_wait_acct_move(struct mem_cgroup *memcg);84 85struct cgroup_taskset;86int memcg1_can_attach(struct cgroup_taskset *tset);87void memcg1_cancel_attach(struct cgroup_taskset *tset);88void memcg1_move_task(void);89void memcg1_css_offline(struct mem_cgroup *memcg);90 91/* for encoding cft->private value on file */92enum res_type {93 _MEM,94 _MEMSWAP,95 _KMEM,96 _TCP,97};98 99bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked);100void memcg1_oom_finish(struct mem_cgroup *memcg, bool locked);101void memcg1_oom_recover(struct mem_cgroup *memcg);102 103void memcg1_commit_charge(struct folio *folio, struct mem_cgroup *memcg);104void memcg1_swapout(struct folio *folio, struct mem_cgroup *memcg);105void memcg1_uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,106 unsigned long nr_memory, int nid);107 108void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s);109 110void memcg1_account_kmem(struct mem_cgroup *memcg, int nr_pages);111static inline bool memcg1_tcpmem_active(struct mem_cgroup *memcg)112{113 return memcg->tcpmem_active;114}115bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,116 gfp_t gfp_mask);117static inline void memcg1_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)118{119 page_counter_uncharge(&memcg->tcpmem, nr_pages);120}121 122extern struct cftype memsw_files[];123extern struct cftype mem_cgroup_legacy_files[];124 125#else /* CONFIG_MEMCG_V1 */126 127static inline bool memcg1_alloc_events(struct mem_cgroup *memcg) { return true; }128static inline void memcg1_free_events(struct mem_cgroup *memcg) {}129 130static inline void memcg1_memcg_init(struct mem_cgroup *memcg) {}131static inline void memcg1_remove_from_trees(struct mem_cgroup *memcg) {}132static inline void memcg1_soft_limit_reset(struct mem_cgroup *memcg) {}133static inline bool memcg1_wait_acct_move(struct mem_cgroup *memcg) { return false; }134static inline void memcg1_css_offline(struct mem_cgroup *memcg) {}135 136static inline bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked) { return true; }137static inline void memcg1_oom_finish(struct mem_cgroup *memcg, bool locked) {}138static inline void memcg1_oom_recover(struct mem_cgroup *memcg) {}139 140static inline void memcg1_commit_charge(struct folio *folio,141 struct mem_cgroup *memcg) {}142 143static inline void memcg1_swapout(struct folio *folio, struct mem_cgroup *memcg) {}144 145static inline void memcg1_uncharge_batch(struct mem_cgroup *memcg,146 unsigned long pgpgout,147 unsigned long nr_memory, int nid) {}148 149static inline void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) {}150 151static inline void memcg1_account_kmem(struct mem_cgroup *memcg, int nr_pages) {}152static inline bool memcg1_tcpmem_active(struct mem_cgroup *memcg) { return false; }153static inline bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,154 gfp_t gfp_mask) { return true; }155static inline void memcg1_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) {}156 157#endif /* CONFIG_MEMCG_V1 */158 159#endif /* __MM_MEMCONTROL_V1_H */160