blob: 60e95872da2983365300f1abb193b6e81974b5de [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.h - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -08009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#ifndef _LINUX_MEMCONTROL_H
21#define _LINUX_MEMCONTROL_H
Hirokazu Takahashif8d665422009-01-07 18:08:02 -080022#include <linux/cgroup.h>
Ying Han456f9982011-05-26 16:25:38 -070023#include <linux/vm_event_item.h>
Glauber Costa7ae1e1d2012-12-18 14:21:56 -080024#include <linux/hardirq.h>
Glauber Costaa8964b92012-12-18 14:22:09 -080025#include <linux/jump_label.h>
Ying Han456f9982011-05-26 16:25:38 -070026
Pavel Emelianov78fb7462008-02-07 00:13:51 -080027struct mem_cgroup;
28struct page_cgroup;
Balbir Singh8697d332008-02-07 00:13:59 -080029struct page;
30struct mm_struct;
Glauber Costa2633d7a2012-12-18 14:22:34 -080031struct kmem_cache;
Pavel Emelianov78fb7462008-02-07 00:13:51 -080032
Sha Zhengju68b48762013-09-12 15:13:50 -070033/*
34 * The corresponding mem_cgroup_stat_names is defined in mm/memcontrol.c,
35 * These two lists should keep in accord with each other.
36 */
37enum mem_cgroup_stat_index {
38 /*
39 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
40 */
41 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
42 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
43 MEM_CGROUP_STAT_RSS_HUGE, /* # of pages charged as anon huge */
44 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Sha Zhengju3ea67d02013-09-12 15:13:53 -070045 MEM_CGROUP_STAT_WRITEBACK, /* # of pages under writeback */
Sha Zhengju68b48762013-09-12 15:13:50 -070046 MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */
47 MEM_CGROUP_STAT_NSTATS,
Greg Thelen2a7106f2011-01-13 15:47:37 -080048};
49
Johannes Weiner56600482012-01-12 17:17:59 -080050struct mem_cgroup_reclaim_cookie {
51 struct zone *zone;
52 int priority;
53 unsigned int generation;
54};
55
Michal Hockode577802013-09-12 15:13:26 -070056enum mem_cgroup_filter_t {
57 VISIT, /* visit current node */
58 SKIP, /* skip the current node and continue traversal */
59 SKIP_TREE, /* skip the whole subtree and continue traversal */
60};
61
62/*
63 * mem_cgroup_filter_t predicate might instruct mem_cgroup_iter_cond how to
64 * iterate through the hierarchy tree. Each tree element is checked by the
65 * predicate before it is returned by the iterator. If a filter returns
66 * SKIP or SKIP_TREE then the iterator code continues traversal (with the
67 * next node down the hierarchy or the next node that doesn't belong under the
68 * memcg's subtree).
69 */
70typedef enum mem_cgroup_filter_t
71(*mem_cgroup_iter_filter)(struct mem_cgroup *memcg, struct mem_cgroup *root);
72
Andrew Mortonc255a452012-07-31 16:43:02 -070073#ifdef CONFIG_MEMCG
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -080074/*
75 * All "charge" functions with gfp_mask should use GFP_KERNEL or
76 * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
77 * alloc memory but reclaims memory from all available zones. So, "where I want
78 * memory from" bits of gfp_mask has no meaning. So any bits of that field is
79 * available but adding a rule is better. charge functions' gfp_mask should
80 * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
81 * codes.
82 * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
83 */
Pavel Emelianov78fb7462008-02-07 00:13:51 -080084
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080085extern int mem_cgroup_newpage_charge(struct page *page, struct mm_struct *mm,
Balbir Singhe1a1cd52008-02-07 00:14:02 -080086 gfp_t gfp_mask);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080087/* for swap handling */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080088extern int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
Johannes Weiner72835c82012-01-12 17:18:32 -080089 struct page *page, gfp_t mask, struct mem_cgroup **memcgp);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080090extern void mem_cgroup_commit_charge_swapin(struct page *page,
Johannes Weiner72835c82012-01-12 17:18:32 -080091 struct mem_cgroup *memcg);
92extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080093
Hugh Dickins82895462008-03-04 14:29:08 -080094extern int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
95 gfp_t gfp_mask);
Johannes Weiner925b7672012-01-12 17:18:15 -080096
97struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
Hugh Dickinsfa9add62012-05-29 15:07:09 -070098struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -080099
100/* For coalescing uncharge for reducing memcg' overhead*/
101extern void mem_cgroup_uncharge_start(void);
102extern void mem_cgroup_uncharge_end(void);
103
Balbir Singh3c541e12008-02-07 00:14:41 -0800104extern void mem_cgroup_uncharge_page(struct page *page);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700105extern void mem_cgroup_uncharge_cache_page(struct page *page);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700106
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700107bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
108 struct mem_cgroup *memcg);
David Rientjesffbdccf2013-07-03 15:01:23 -0700109bool task_in_mem_cgroup(struct task_struct *task,
110 const struct mem_cgroup *memcg);
David Rientjes3062fc62008-02-07 00:14:03 -0800111
Wu Fengguange42d9d52009-12-16 12:19:59 +0100112extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
Balbir Singhcf475ad2008-04-29 01:00:16 -0700113extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700114extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
Balbir Singhcf475ad2008-04-29 01:00:16 -0700115
Glauber Costae1aab162011-12-11 21:47:03 +0000116extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
Tejun Heo182446d2013-08-08 20:11:24 -0400117extern struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css);
Glauber Costae1aab162011-12-11 21:47:03 +0000118
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800119static inline
Johannes Weiner587af302012-10-08 16:34:12 -0700120bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg)
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800121{
Johannes Weiner587af302012-10-08 16:34:12 -0700122 struct mem_cgroup *task_memcg;
123 bool match;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700124
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800125 rcu_read_lock();
Johannes Weiner587af302012-10-08 16:34:12 -0700126 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
127 match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800128 rcu_read_unlock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700129 return match;
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800130}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800131
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700132extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
Wu Fengguangd3242362009-12-16 12:19:59 +0100133
Johannes Weiner0030f532012-07-31 16:45:25 -0700134extern void
135mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
136 struct mem_cgroup **memcgp);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700137extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800138 struct page *oldpage, struct page *newpage, bool migration_ok);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800139
Michal Hockode577802013-09-12 15:13:26 -0700140struct mem_cgroup *mem_cgroup_iter_cond(struct mem_cgroup *root,
141 struct mem_cgroup *prev,
142 struct mem_cgroup_reclaim_cookie *reclaim,
143 mem_cgroup_iter_filter cond);
144
145static inline struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
146 struct mem_cgroup *prev,
147 struct mem_cgroup_reclaim_cookie *reclaim)
148{
149 return mem_cgroup_iter_cond(root, prev, reclaim, NULL);
150}
151
Johannes Weiner56600482012-01-12 17:17:59 -0800152void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
153
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800154/*
155 * For memory reclaim.
156 */
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700157int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
Ying Han889976d2011-05-26 16:25:33 -0700158int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700159unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700160void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
Balbir Singhe2224322009-04-02 16:57:39 -0700161extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
162 struct task_struct *p);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800163extern void mem_cgroup_replace_page_cache(struct page *oldpage,
164 struct page *newpage);
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800165
Johannes Weiner519e5242013-09-12 15:13:42 -0700166/**
167 * mem_cgroup_toggle_oom - toggle the memcg OOM killer for the current task
168 * @new: true to enable, false to disable
169 *
170 * Toggle whether a failed memcg charge should invoke the OOM killer
171 * or just return -ENOMEM. Returns the previous toggle state.
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700172 *
173 * NOTE: Any path that enables the OOM killer before charging must
174 * call mem_cgroup_oom_synchronize() afterward to finalize the
175 * OOM handling and clean up.
Johannes Weiner519e5242013-09-12 15:13:42 -0700176 */
177static inline bool mem_cgroup_toggle_oom(bool new)
178{
179 bool old;
180
181 old = current->memcg_oom.may_oom;
182 current->memcg_oom.may_oom = new;
183
184 return old;
185}
186
187static inline void mem_cgroup_enable_oom(void)
188{
189 bool old = mem_cgroup_toggle_oom(true);
190
191 WARN_ON(old == true);
192}
193
194static inline void mem_cgroup_disable_oom(void)
195{
196 bool old = mem_cgroup_toggle_oom(false);
197
198 WARN_ON(old == false);
199}
200
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700201static inline bool task_in_memcg_oom(struct task_struct *p)
202{
203 return p->memcg_oom.in_memcg_oom;
204}
205
206bool mem_cgroup_oom_synchronize(void);
207
Andrew Mortonc255a452012-07-31 16:43:02 -0700208#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -0800209extern int do_swap_account;
210#endif
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800211
212static inline bool mem_cgroup_disabled(void)
213{
214 if (mem_cgroup_subsys.disabled)
215 return true;
216 return false;
217}
218
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700219void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
220 unsigned long *flags);
221
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700222extern atomic_t memcg_moving;
223
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700224static inline void mem_cgroup_begin_update_page_stat(struct page *page,
225 bool *locked, unsigned long *flags)
226{
227 if (mem_cgroup_disabled())
228 return;
229 rcu_read_lock();
230 *locked = false;
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700231 if (atomic_read(&memcg_moving))
232 __mem_cgroup_begin_update_page_stat(page, locked, flags);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700233}
234
235void __mem_cgroup_end_update_page_stat(struct page *page,
236 unsigned long *flags);
237static inline void mem_cgroup_end_update_page_stat(struct page *page,
238 bool *locked, unsigned long *flags)
239{
240 if (mem_cgroup_disabled())
241 return;
242 if (*locked)
243 __mem_cgroup_end_update_page_stat(page, flags);
244 rcu_read_unlock();
245}
246
Greg Thelen2a7106f2011-01-13 15:47:37 -0800247void mem_cgroup_update_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700248 enum mem_cgroup_stat_index idx,
Greg Thelen2a7106f2011-01-13 15:47:37 -0800249 int val);
250
251static inline void mem_cgroup_inc_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700252 enum mem_cgroup_stat_index idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -0800253{
254 mem_cgroup_update_page_stat(page, idx, 1);
255}
256
257static inline void mem_cgroup_dec_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700258 enum mem_cgroup_stat_index idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -0800259{
260 mem_cgroup_update_page_stat(page, idx, -1);
261}
262
Michal Hockode577802013-09-12 15:13:26 -0700263enum mem_cgroup_filter_t
264mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
Michal Hockoa5b7c872013-09-12 15:13:25 -0700265 struct mem_cgroup *root);
David Rientjesa63d83f2010-08-09 17:19:46 -0700266
David Rientjes68ae5642012-12-12 13:51:57 -0800267void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
268static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
269 enum vm_event_item idx)
270{
271 if (mem_cgroup_disabled())
272 return;
273 __mem_cgroup_count_vm_event(mm, idx);
274}
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800275#ifdef CONFIG_TRANSPARENT_HUGEPAGE
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800276void mem_cgroup_split_huge_fixup(struct page *head);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800277#endif
278
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700279#ifdef CONFIG_DEBUG_VM
280bool mem_cgroup_bad_page_check(struct page *page);
281void mem_cgroup_print_bad_page(struct page *page);
282#endif
Andrew Mortonc255a452012-07-31 16:43:02 -0700283#else /* CONFIG_MEMCG */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800284struct mem_cgroup;
285
286static inline int mem_cgroup_newpage_charge(struct page *page,
Hugh Dickins82895462008-03-04 14:29:08 -0800287 struct mm_struct *mm, gfp_t gfp_mask)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800288{
289 return 0;
290}
291
Hugh Dickins82895462008-03-04 14:29:08 -0800292static inline int mem_cgroup_cache_charge(struct page *page,
293 struct mm_struct *mm, gfp_t gfp_mask)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800294{
Hugh Dickins82895462008-03-04 14:29:08 -0800295 return 0;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800296}
297
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800298static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
Johannes Weiner72835c82012-01-12 17:18:32 -0800299 struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800300{
301 return 0;
302}
303
304static inline void mem_cgroup_commit_charge_swapin(struct page *page,
Johannes Weiner72835c82012-01-12 17:18:32 -0800305 struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800306{
307}
308
Johannes Weiner72835c82012-01-12 17:18:32 -0800309static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800310{
311}
312
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -0800313static inline void mem_cgroup_uncharge_start(void)
314{
315}
316
317static inline void mem_cgroup_uncharge_end(void)
318{
319}
320
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800321static inline void mem_cgroup_uncharge_page(struct page *page)
322{
323}
324
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700325static inline void mem_cgroup_uncharge_cache_page(struct page *page)
326{
327}
328
Johannes Weiner925b7672012-01-12 17:18:15 -0800329static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
330 struct mem_cgroup *memcg)
331{
332 return &zone->lruvec;
333}
334
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700335static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
336 struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -0700337{
Johannes Weiner925b7672012-01-12 17:18:15 -0800338 return &zone->lruvec;
Balbir Singh66e17072008-02-07 00:13:56 -0800339}
340
Wu Fengguange42d9d52009-12-16 12:19:59 +0100341static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
342{
343 return NULL;
344}
345
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700346static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
347{
348 return NULL;
349}
350
Johannes Weiner587af302012-10-08 16:34:12 -0700351static inline bool mm_match_cgroup(struct mm_struct *mm,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700352 struct mem_cgroup *memcg)
Balbir Singhbed71612008-02-07 00:14:01 -0800353{
Johannes Weiner587af302012-10-08 16:34:12 -0700354 return true;
Balbir Singhbed71612008-02-07 00:14:01 -0800355}
356
David Rientjesffbdccf2013-07-03 15:01:23 -0700357static inline bool task_in_mem_cgroup(struct task_struct *task,
358 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -0800359{
David Rientjesffbdccf2013-07-03 15:01:23 -0700360 return true;
David Rientjes4c4a2212008-02-07 00:14:06 -0800361}
362
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700363static inline struct cgroup_subsys_state
364 *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100365{
366 return NULL;
367}
368
Johannes Weiner0030f532012-07-31 16:45:25 -0700369static inline void
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -0700370mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
Johannes Weiner0030f532012-07-31 16:45:25 -0700371 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800372{
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800373}
374
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700375static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800376 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800377{
378}
Michal Hockode577802013-09-12 15:13:26 -0700379static inline struct mem_cgroup *
380mem_cgroup_iter_cond(struct mem_cgroup *root,
381 struct mem_cgroup *prev,
382 struct mem_cgroup_reclaim_cookie *reclaim,
383 mem_cgroup_iter_filter cond)
384{
385 /* first call must return non-NULL, second return NULL */
386 return (struct mem_cgroup *)(unsigned long)!prev;
387}
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800388
Johannes Weiner56600482012-01-12 17:17:59 -0800389static inline struct mem_cgroup *
390mem_cgroup_iter(struct mem_cgroup *root,
391 struct mem_cgroup *prev,
392 struct mem_cgroup_reclaim_cookie *reclaim)
393{
394 return NULL;
395}
396
397static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
398 struct mem_cgroup *prev)
399{
400}
401
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800402static inline bool mem_cgroup_disabled(void)
403{
404 return true;
405}
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800406
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800407static inline int
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700408mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800409{
410 return 1;
411}
412
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800413static inline unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700414mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800415{
416 return 0;
417}
418
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700419static inline void
420mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
421 int increment)
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800422{
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800423}
424
Balbir Singhe2224322009-04-02 16:57:39 -0700425static inline void
426mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
427{
428}
429
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700430static inline void mem_cgroup_begin_update_page_stat(struct page *page,
431 bool *locked, unsigned long *flags)
432{
433}
434
435static inline void mem_cgroup_end_update_page_stat(struct page *page,
436 bool *locked, unsigned long *flags)
437{
438}
439
Johannes Weiner519e5242013-09-12 15:13:42 -0700440static inline bool mem_cgroup_toggle_oom(bool new)
441{
442 return false;
443}
444
445static inline void mem_cgroup_enable_oom(void)
446{
447}
448
449static inline void mem_cgroup_disable_oom(void)
450{
451}
452
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700453static inline bool task_in_memcg_oom(struct task_struct *p)
454{
455 return false;
456}
457
458static inline bool mem_cgroup_oom_synchronize(void)
459{
460 return false;
461}
462
Greg Thelen2a7106f2011-01-13 15:47:37 -0800463static inline void mem_cgroup_inc_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700464 enum mem_cgroup_stat_index idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -0800465{
466}
467
468static inline void mem_cgroup_dec_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700469 enum mem_cgroup_stat_index idx)
Balbir Singhd69b0422009-06-17 16:26:34 -0700470{
471}
472
Balbir Singh4e416952009-09-23 15:56:39 -0700473static inline
Michal Hockode577802013-09-12 15:13:26 -0700474enum mem_cgroup_filter_t
475mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
Michal Hockoa5b7c872013-09-12 15:13:25 -0700476 struct mem_cgroup *root)
Balbir Singh4e416952009-09-23 15:56:39 -0700477{
Michal Hockode577802013-09-12 15:13:26 -0700478 return VISIT;
Balbir Singh4e416952009-09-23 15:56:39 -0700479}
480
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800481static inline void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800482{
483}
484
Ying Han456f9982011-05-26 16:25:38 -0700485static inline
486void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
487{
488}
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800489static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
490 struct page *newpage)
491{
492}
Andrew Mortonc255a452012-07-31 16:43:02 -0700493#endif /* CONFIG_MEMCG */
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800494
Andrew Mortonc255a452012-07-31 16:43:02 -0700495#if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700496static inline bool
497mem_cgroup_bad_page_check(struct page *page)
498{
499 return false;
500}
501
502static inline void
503mem_cgroup_print_bad_page(struct page *page)
504{
505}
506#endif
507
Glauber Costae1aab162011-12-11 21:47:03 +0000508enum {
509 UNDER_LIMIT,
510 SOFT_LIMIT,
511 OVER_LIMIT,
512};
513
514struct sock;
David Rientjescd590852012-10-10 15:54:08 -0700515#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000516void sock_update_memcg(struct sock *sk);
517void sock_release_memcg(struct sock *sk);
518#else
519static inline void sock_update_memcg(struct sock *sk)
520{
521}
522static inline void sock_release_memcg(struct sock *sk)
523{
524}
David Rientjescd590852012-10-10 15:54:08 -0700525#endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800526
527#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -0800528extern struct static_key memcg_kmem_enabled_key;
Glauber Costa749c5412012-12-18 14:23:01 -0800529
530extern int memcg_limited_groups_array_size;
Glauber Costaebe945c2012-12-18 14:23:10 -0800531
532/*
533 * Helper macro to loop through all memcg-specific caches. Callers must still
534 * check if the cache is valid (it is either valid or NULL).
535 * the slab_mutex must be held when looping through those caches
536 */
Glauber Costa749c5412012-12-18 14:23:01 -0800537#define for_each_memcg_cache_index(_idx) \
Glauber Costa91c777d2013-02-04 14:28:49 -0800538 for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++)
Glauber Costa749c5412012-12-18 14:23:01 -0800539
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800540static inline bool memcg_kmem_enabled(void)
541{
Glauber Costaa8964b92012-12-18 14:22:09 -0800542 return static_key_false(&memcg_kmem_enabled_key);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800543}
544
545/*
546 * In general, we'll do everything in our power to not incur in any overhead
547 * for non-memcg users for the kmem functions. Not even a function call, if we
548 * can avoid it.
549 *
550 * Therefore, we'll inline all those functions so that in the best case, we'll
551 * see that kmemcg is off for everybody and proceed quickly. If it is on,
552 * we'll still do most of the flag checking inline. We check a lot of
553 * conditions, but because they are pretty simple, they are expected to be
554 * fast.
555 */
556bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
557 int order);
558void __memcg_kmem_commit_charge(struct page *page,
559 struct mem_cgroup *memcg, int order);
560void __memcg_kmem_uncharge_pages(struct page *page, int order);
561
Glauber Costa2633d7a2012-12-18 14:22:34 -0800562int memcg_cache_id(struct mem_cgroup *memcg);
Glauber Costa943a4512012-12-18 14:23:03 -0800563int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
564 struct kmem_cache *root_cache);
Glauber Costa2633d7a2012-12-18 14:22:34 -0800565void memcg_release_cache(struct kmem_cache *cachep);
566void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep);
567
Glauber Costa55007d82012-12-18 14:22:38 -0800568int memcg_update_cache_size(struct kmem_cache *s, int num_groups);
569void memcg_update_array_size(int num_groups);
Glauber Costad7f25f82012-12-18 14:22:40 -0800570
571struct kmem_cache *
572__memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
573
Glauber Costa1f458cb2012-12-18 14:22:50 -0800574void mem_cgroup_destroy_cache(struct kmem_cache *cachep);
Glauber Costa7cf27982012-12-18 14:22:55 -0800575void kmem_cache_destroy_memcg_children(struct kmem_cache *s);
Glauber Costa1f458cb2012-12-18 14:22:50 -0800576
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800577/**
578 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
579 * @gfp: the gfp allocation flags.
580 * @memcg: a pointer to the memcg this was charged against.
581 * @order: allocation order.
582 *
583 * returns true if the memcg where the current task belongs can hold this
584 * allocation.
585 *
586 * We return true automatically if this allocation is not to be accounted to
587 * any memcg.
588 */
589static inline bool
590memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
591{
592 if (!memcg_kmem_enabled())
593 return true;
594
595 /*
596 * __GFP_NOFAIL allocations will move on even if charging is not
597 * possible. Therefore we don't even try, and have this allocation
598 * unaccounted. We could in theory charge it with
599 * res_counter_charge_nofail, but we hope those allocations are rare,
600 * and won't be worth the trouble.
601 */
602 if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL))
603 return true;
604 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
605 return true;
606
607 /* If the test is dying, just let it go. */
608 if (unlikely(fatal_signal_pending(current)))
609 return true;
610
611 return __memcg_kmem_newpage_charge(gfp, memcg, order);
612}
613
614/**
615 * memcg_kmem_uncharge_pages: uncharge pages from memcg
616 * @page: pointer to struct page being freed
617 * @order: allocation order.
618 *
619 * there is no need to specify memcg here, since it is embedded in page_cgroup
620 */
621static inline void
622memcg_kmem_uncharge_pages(struct page *page, int order)
623{
624 if (memcg_kmem_enabled())
625 __memcg_kmem_uncharge_pages(page, order);
626}
627
628/**
629 * memcg_kmem_commit_charge: embeds correct memcg in a page
630 * @page: pointer to struct page recently allocated
631 * @memcg: the memcg structure we charged against
632 * @order: allocation order.
633 *
634 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
635 * failure of the allocation. if @page is NULL, this function will revert the
636 * charges. Otherwise, it will commit the memcg given by @memcg to the
637 * corresponding page_cgroup.
638 */
639static inline void
640memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
641{
642 if (memcg_kmem_enabled() && memcg)
643 __memcg_kmem_commit_charge(page, memcg, order);
644}
645
Glauber Costad7f25f82012-12-18 14:22:40 -0800646/**
647 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
648 * @cachep: the original global kmem cache
649 * @gfp: allocation flags.
650 *
651 * This function assumes that the task allocating, which determines the memcg
652 * in the page allocator, belongs to the same cgroup throughout the whole
653 * process. Misacounting can happen if the task calls memcg_kmem_get_cache()
654 * while belonging to a cgroup, and later on changes. This is considered
655 * acceptable, and should only happen upon task migration.
656 *
657 * Before the cache is created by the memcg core, there is also a possible
658 * imbalance: the task belongs to a memcg, but the cache being allocated from
659 * is the global cache, since the child cache is not yet guaranteed to be
660 * ready. This case is also fine, since in this case the GFP_KMEMCG will not be
661 * passed and the page allocator will not attempt any cgroup accounting.
662 */
663static __always_inline struct kmem_cache *
664memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
665{
666 if (!memcg_kmem_enabled())
667 return cachep;
668 if (gfp & __GFP_NOFAIL)
669 return cachep;
670 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
671 return cachep;
672 if (unlikely(fatal_signal_pending(current)))
673 return cachep;
674
675 return __memcg_kmem_get_cache(cachep, gfp);
676}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800677#else
Glauber Costa749c5412012-12-18 14:23:01 -0800678#define for_each_memcg_cache_index(_idx) \
679 for (; NULL; )
680
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800681static inline bool memcg_kmem_enabled(void)
682{
683 return false;
684}
685
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800686static inline bool
687memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
688{
689 return true;
690}
691
692static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
693{
694}
695
696static inline void
697memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
698{
699}
Glauber Costa2633d7a2012-12-18 14:22:34 -0800700
701static inline int memcg_cache_id(struct mem_cgroup *memcg)
702{
703 return -1;
704}
705
Glauber Costa943a4512012-12-18 14:23:03 -0800706static inline int
707memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
708 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -0800709{
710 return 0;
711}
712
713static inline void memcg_release_cache(struct kmem_cache *cachep)
714{
715}
716
717static inline void memcg_cache_list_add(struct mem_cgroup *memcg,
718 struct kmem_cache *s)
719{
720}
Glauber Costad7f25f82012-12-18 14:22:40 -0800721
722static inline struct kmem_cache *
723memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
724{
725 return cachep;
726}
Glauber Costa7cf27982012-12-18 14:22:55 -0800727
728static inline void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
729{
730}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800731#endif /* CONFIG_MEMCG_KMEM */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800732#endif /* _LINUX_MEMCONTROL_H */
733