blob: 34ac6497d01a43a59ebc76027904591cd194d222 [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
Greg Thelen2a7106f2011-01-13 15:47:37 -080033/* Stats that can be updated by kernel. */
34enum mem_cgroup_page_stat_item {
35 MEMCG_NR_FILE_MAPPED, /* # of pages charged as file rss */
36};
37
Johannes Weiner56600482012-01-12 17:17:59 -080038struct mem_cgroup_reclaim_cookie {
39 struct zone *zone;
40 int priority;
41 unsigned int generation;
42};
43
Michal Hockode577802013-09-12 15:13:26 -070044enum mem_cgroup_filter_t {
45 VISIT, /* visit current node */
46 SKIP, /* skip the current node and continue traversal */
47 SKIP_TREE, /* skip the whole subtree and continue traversal */
48};
49
50/*
51 * mem_cgroup_filter_t predicate might instruct mem_cgroup_iter_cond how to
52 * iterate through the hierarchy tree. Each tree element is checked by the
53 * predicate before it is returned by the iterator. If a filter returns
54 * SKIP or SKIP_TREE then the iterator code continues traversal (with the
55 * next node down the hierarchy or the next node that doesn't belong under the
56 * memcg's subtree).
57 */
58typedef enum mem_cgroup_filter_t
59(*mem_cgroup_iter_filter)(struct mem_cgroup *memcg, struct mem_cgroup *root);
60
Andrew Mortonc255a452012-07-31 16:43:02 -070061#ifdef CONFIG_MEMCG
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -080062/*
63 * All "charge" functions with gfp_mask should use GFP_KERNEL or
64 * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
65 * alloc memory but reclaims memory from all available zones. So, "where I want
66 * memory from" bits of gfp_mask has no meaning. So any bits of that field is
67 * available but adding a rule is better. charge functions' gfp_mask should
68 * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
69 * codes.
70 * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
71 */
Pavel Emelianov78fb7462008-02-07 00:13:51 -080072
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080073extern int mem_cgroup_newpage_charge(struct page *page, struct mm_struct *mm,
Balbir Singhe1a1cd52008-02-07 00:14:02 -080074 gfp_t gfp_mask);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080075/* for swap handling */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080076extern int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
Johannes Weiner72835c82012-01-12 17:18:32 -080077 struct page *page, gfp_t mask, struct mem_cgroup **memcgp);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080078extern void mem_cgroup_commit_charge_swapin(struct page *page,
Johannes Weiner72835c82012-01-12 17:18:32 -080079 struct mem_cgroup *memcg);
80extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080081
Hugh Dickins82895462008-03-04 14:29:08 -080082extern int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
83 gfp_t gfp_mask);
Johannes Weiner925b7672012-01-12 17:18:15 -080084
85struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
Hugh Dickinsfa9add62012-05-29 15:07:09 -070086struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -080087
88/* For coalescing uncharge for reducing memcg' overhead*/
89extern void mem_cgroup_uncharge_start(void);
90extern void mem_cgroup_uncharge_end(void);
91
Balbir Singh3c541e12008-02-07 00:14:41 -080092extern void mem_cgroup_uncharge_page(struct page *page);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -070093extern void mem_cgroup_uncharge_cache_page(struct page *page);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -070094
Johannes Weinerc3ac9a82012-05-29 15:06:25 -070095bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
96 struct mem_cgroup *memcg);
David Rientjesffbdccf2013-07-03 15:01:23 -070097bool task_in_mem_cgroup(struct task_struct *task,
98 const struct mem_cgroup *memcg);
David Rientjes3062fc62008-02-07 00:14:03 -080099
Wu Fengguange42d9d52009-12-16 12:19:59 +0100100extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
Balbir Singhcf475ad2008-04-29 01:00:16 -0700101extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700102extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
Balbir Singhcf475ad2008-04-29 01:00:16 -0700103
Glauber Costae1aab162011-12-11 21:47:03 +0000104extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
Tejun Heo182446d2013-08-08 20:11:24 -0400105extern struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css);
Glauber Costae1aab162011-12-11 21:47:03 +0000106
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800107static inline
Johannes Weiner587af302012-10-08 16:34:12 -0700108bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg)
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800109{
Johannes Weiner587af302012-10-08 16:34:12 -0700110 struct mem_cgroup *task_memcg;
111 bool match;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700112
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800113 rcu_read_lock();
Johannes Weiner587af302012-10-08 16:34:12 -0700114 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
115 match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800116 rcu_read_unlock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700117 return match;
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800118}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800119
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700120extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
Wu Fengguangd3242362009-12-16 12:19:59 +0100121
Johannes Weiner0030f532012-07-31 16:45:25 -0700122extern void
123mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
124 struct mem_cgroup **memcgp);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700125extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800126 struct page *oldpage, struct page *newpage, bool migration_ok);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800127
Michal Hockode577802013-09-12 15:13:26 -0700128struct mem_cgroup *mem_cgroup_iter_cond(struct mem_cgroup *root,
129 struct mem_cgroup *prev,
130 struct mem_cgroup_reclaim_cookie *reclaim,
131 mem_cgroup_iter_filter cond);
132
133static inline struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
134 struct mem_cgroup *prev,
135 struct mem_cgroup_reclaim_cookie *reclaim)
136{
137 return mem_cgroup_iter_cond(root, prev, reclaim, NULL);
138}
139
Johannes Weiner56600482012-01-12 17:17:59 -0800140void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
141
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800142/*
143 * For memory reclaim.
144 */
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700145int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
Ying Han889976d2011-05-26 16:25:33 -0700146int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700147unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700148void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
Balbir Singhe2224322009-04-02 16:57:39 -0700149extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
150 struct task_struct *p);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800151extern void mem_cgroup_replace_page_cache(struct page *oldpage,
152 struct page *newpage);
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800153
Johannes Weiner519e5242013-09-12 15:13:42 -0700154/**
155 * mem_cgroup_toggle_oom - toggle the memcg OOM killer for the current task
156 * @new: true to enable, false to disable
157 *
158 * Toggle whether a failed memcg charge should invoke the OOM killer
159 * or just return -ENOMEM. Returns the previous toggle state.
160 */
161static inline bool mem_cgroup_toggle_oom(bool new)
162{
163 bool old;
164
165 old = current->memcg_oom.may_oom;
166 current->memcg_oom.may_oom = new;
167
168 return old;
169}
170
171static inline void mem_cgroup_enable_oom(void)
172{
173 bool old = mem_cgroup_toggle_oom(true);
174
175 WARN_ON(old == true);
176}
177
178static inline void mem_cgroup_disable_oom(void)
179{
180 bool old = mem_cgroup_toggle_oom(false);
181
182 WARN_ON(old == false);
183}
184
Andrew Mortonc255a452012-07-31 16:43:02 -0700185#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -0800186extern int do_swap_account;
187#endif
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800188
189static inline bool mem_cgroup_disabled(void)
190{
191 if (mem_cgroup_subsys.disabled)
192 return true;
193 return false;
194}
195
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700196void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
197 unsigned long *flags);
198
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700199extern atomic_t memcg_moving;
200
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700201static inline void mem_cgroup_begin_update_page_stat(struct page *page,
202 bool *locked, unsigned long *flags)
203{
204 if (mem_cgroup_disabled())
205 return;
206 rcu_read_lock();
207 *locked = false;
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700208 if (atomic_read(&memcg_moving))
209 __mem_cgroup_begin_update_page_stat(page, locked, flags);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700210}
211
212void __mem_cgroup_end_update_page_stat(struct page *page,
213 unsigned long *flags);
214static inline void mem_cgroup_end_update_page_stat(struct page *page,
215 bool *locked, unsigned long *flags)
216{
217 if (mem_cgroup_disabled())
218 return;
219 if (*locked)
220 __mem_cgroup_end_update_page_stat(page, flags);
221 rcu_read_unlock();
222}
223
Greg Thelen2a7106f2011-01-13 15:47:37 -0800224void mem_cgroup_update_page_stat(struct page *page,
225 enum mem_cgroup_page_stat_item idx,
226 int val);
227
228static inline void mem_cgroup_inc_page_stat(struct page *page,
229 enum mem_cgroup_page_stat_item idx)
230{
231 mem_cgroup_update_page_stat(page, idx, 1);
232}
233
234static inline void mem_cgroup_dec_page_stat(struct page *page,
235 enum mem_cgroup_page_stat_item idx)
236{
237 mem_cgroup_update_page_stat(page, idx, -1);
238}
239
Michal Hockode577802013-09-12 15:13:26 -0700240enum mem_cgroup_filter_t
241mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
Michal Hockoa5b7c872013-09-12 15:13:25 -0700242 struct mem_cgroup *root);
David Rientjesa63d83f2010-08-09 17:19:46 -0700243
David Rientjes68ae5642012-12-12 13:51:57 -0800244void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
245static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
246 enum vm_event_item idx)
247{
248 if (mem_cgroup_disabled())
249 return;
250 __mem_cgroup_count_vm_event(mm, idx);
251}
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800252#ifdef CONFIG_TRANSPARENT_HUGEPAGE
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800253void mem_cgroup_split_huge_fixup(struct page *head);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800254#endif
255
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700256#ifdef CONFIG_DEBUG_VM
257bool mem_cgroup_bad_page_check(struct page *page);
258void mem_cgroup_print_bad_page(struct page *page);
259#endif
Andrew Mortonc255a452012-07-31 16:43:02 -0700260#else /* CONFIG_MEMCG */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800261struct mem_cgroup;
262
263static inline int mem_cgroup_newpage_charge(struct page *page,
Hugh Dickins82895462008-03-04 14:29:08 -0800264 struct mm_struct *mm, gfp_t gfp_mask)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800265{
266 return 0;
267}
268
Hugh Dickins82895462008-03-04 14:29:08 -0800269static inline int mem_cgroup_cache_charge(struct page *page,
270 struct mm_struct *mm, gfp_t gfp_mask)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800271{
Hugh Dickins82895462008-03-04 14:29:08 -0800272 return 0;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800273}
274
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800275static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
Johannes Weiner72835c82012-01-12 17:18:32 -0800276 struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800277{
278 return 0;
279}
280
281static inline void mem_cgroup_commit_charge_swapin(struct page *page,
Johannes Weiner72835c82012-01-12 17:18:32 -0800282 struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800283{
284}
285
Johannes Weiner72835c82012-01-12 17:18:32 -0800286static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800287{
288}
289
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -0800290static inline void mem_cgroup_uncharge_start(void)
291{
292}
293
294static inline void mem_cgroup_uncharge_end(void)
295{
296}
297
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800298static inline void mem_cgroup_uncharge_page(struct page *page)
299{
300}
301
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700302static inline void mem_cgroup_uncharge_cache_page(struct page *page)
303{
304}
305
Johannes Weiner925b7672012-01-12 17:18:15 -0800306static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
307 struct mem_cgroup *memcg)
308{
309 return &zone->lruvec;
310}
311
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700312static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
313 struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -0700314{
Johannes Weiner925b7672012-01-12 17:18:15 -0800315 return &zone->lruvec;
Balbir Singh66e17072008-02-07 00:13:56 -0800316}
317
Wu Fengguange42d9d52009-12-16 12:19:59 +0100318static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
319{
320 return NULL;
321}
322
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700323static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
324{
325 return NULL;
326}
327
Johannes Weiner587af302012-10-08 16:34:12 -0700328static inline bool mm_match_cgroup(struct mm_struct *mm,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700329 struct mem_cgroup *memcg)
Balbir Singhbed71612008-02-07 00:14:01 -0800330{
Johannes Weiner587af302012-10-08 16:34:12 -0700331 return true;
Balbir Singhbed71612008-02-07 00:14:01 -0800332}
333
David Rientjesffbdccf2013-07-03 15:01:23 -0700334static inline bool task_in_mem_cgroup(struct task_struct *task,
335 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -0800336{
David Rientjesffbdccf2013-07-03 15:01:23 -0700337 return true;
David Rientjes4c4a2212008-02-07 00:14:06 -0800338}
339
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700340static inline struct cgroup_subsys_state
341 *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100342{
343 return NULL;
344}
345
Johannes Weiner0030f532012-07-31 16:45:25 -0700346static inline void
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -0700347mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
Johannes Weiner0030f532012-07-31 16:45:25 -0700348 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800349{
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800350}
351
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700352static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800353 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800354{
355}
Michal Hockode577802013-09-12 15:13:26 -0700356static inline struct mem_cgroup *
357mem_cgroup_iter_cond(struct mem_cgroup *root,
358 struct mem_cgroup *prev,
359 struct mem_cgroup_reclaim_cookie *reclaim,
360 mem_cgroup_iter_filter cond)
361{
362 /* first call must return non-NULL, second return NULL */
363 return (struct mem_cgroup *)(unsigned long)!prev;
364}
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800365
Johannes Weiner56600482012-01-12 17:17:59 -0800366static inline struct mem_cgroup *
367mem_cgroup_iter(struct mem_cgroup *root,
368 struct mem_cgroup *prev,
369 struct mem_cgroup_reclaim_cookie *reclaim)
370{
371 return NULL;
372}
373
374static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
375 struct mem_cgroup *prev)
376{
377}
378
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800379static inline bool mem_cgroup_disabled(void)
380{
381 return true;
382}
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800383
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800384static inline int
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700385mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800386{
387 return 1;
388}
389
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800390static inline unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700391mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800392{
393 return 0;
394}
395
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700396static inline void
397mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
398 int increment)
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800399{
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800400}
401
Balbir Singhe2224322009-04-02 16:57:39 -0700402static inline void
403mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
404{
405}
406
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700407static inline void mem_cgroup_begin_update_page_stat(struct page *page,
408 bool *locked, unsigned long *flags)
409{
410}
411
412static inline void mem_cgroup_end_update_page_stat(struct page *page,
413 bool *locked, unsigned long *flags)
414{
415}
416
Johannes Weiner519e5242013-09-12 15:13:42 -0700417static inline bool mem_cgroup_toggle_oom(bool new)
418{
419 return false;
420}
421
422static inline void mem_cgroup_enable_oom(void)
423{
424}
425
426static inline void mem_cgroup_disable_oom(void)
427{
428}
429
Greg Thelen2a7106f2011-01-13 15:47:37 -0800430static inline void mem_cgroup_inc_page_stat(struct page *page,
431 enum mem_cgroup_page_stat_item idx)
432{
433}
434
435static inline void mem_cgroup_dec_page_stat(struct page *page,
436 enum mem_cgroup_page_stat_item idx)
Balbir Singhd69b0422009-06-17 16:26:34 -0700437{
438}
439
Balbir Singh4e416952009-09-23 15:56:39 -0700440static inline
Michal Hockode577802013-09-12 15:13:26 -0700441enum mem_cgroup_filter_t
442mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
Michal Hockoa5b7c872013-09-12 15:13:25 -0700443 struct mem_cgroup *root)
Balbir Singh4e416952009-09-23 15:56:39 -0700444{
Michal Hockode577802013-09-12 15:13:26 -0700445 return VISIT;
Balbir Singh4e416952009-09-23 15:56:39 -0700446}
447
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800448static inline void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800449{
450}
451
Ying Han456f9982011-05-26 16:25:38 -0700452static inline
453void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
454{
455}
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800456static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
457 struct page *newpage)
458{
459}
Andrew Mortonc255a452012-07-31 16:43:02 -0700460#endif /* CONFIG_MEMCG */
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800461
Andrew Mortonc255a452012-07-31 16:43:02 -0700462#if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700463static inline bool
464mem_cgroup_bad_page_check(struct page *page)
465{
466 return false;
467}
468
469static inline void
470mem_cgroup_print_bad_page(struct page *page)
471{
472}
473#endif
474
Glauber Costae1aab162011-12-11 21:47:03 +0000475enum {
476 UNDER_LIMIT,
477 SOFT_LIMIT,
478 OVER_LIMIT,
479};
480
481struct sock;
David Rientjescd590852012-10-10 15:54:08 -0700482#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000483void sock_update_memcg(struct sock *sk);
484void sock_release_memcg(struct sock *sk);
485#else
486static inline void sock_update_memcg(struct sock *sk)
487{
488}
489static inline void sock_release_memcg(struct sock *sk)
490{
491}
David Rientjescd590852012-10-10 15:54:08 -0700492#endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800493
494#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -0800495extern struct static_key memcg_kmem_enabled_key;
Glauber Costa749c5412012-12-18 14:23:01 -0800496
497extern int memcg_limited_groups_array_size;
Glauber Costaebe945c2012-12-18 14:23:10 -0800498
499/*
500 * Helper macro to loop through all memcg-specific caches. Callers must still
501 * check if the cache is valid (it is either valid or NULL).
502 * the slab_mutex must be held when looping through those caches
503 */
Glauber Costa749c5412012-12-18 14:23:01 -0800504#define for_each_memcg_cache_index(_idx) \
Glauber Costa91c777d2013-02-04 14:28:49 -0800505 for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++)
Glauber Costa749c5412012-12-18 14:23:01 -0800506
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800507static inline bool memcg_kmem_enabled(void)
508{
Glauber Costaa8964b92012-12-18 14:22:09 -0800509 return static_key_false(&memcg_kmem_enabled_key);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800510}
511
512/*
513 * In general, we'll do everything in our power to not incur in any overhead
514 * for non-memcg users for the kmem functions. Not even a function call, if we
515 * can avoid it.
516 *
517 * Therefore, we'll inline all those functions so that in the best case, we'll
518 * see that kmemcg is off for everybody and proceed quickly. If it is on,
519 * we'll still do most of the flag checking inline. We check a lot of
520 * conditions, but because they are pretty simple, they are expected to be
521 * fast.
522 */
523bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
524 int order);
525void __memcg_kmem_commit_charge(struct page *page,
526 struct mem_cgroup *memcg, int order);
527void __memcg_kmem_uncharge_pages(struct page *page, int order);
528
Glauber Costa2633d7a2012-12-18 14:22:34 -0800529int memcg_cache_id(struct mem_cgroup *memcg);
Glauber Costa943a4512012-12-18 14:23:03 -0800530int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
531 struct kmem_cache *root_cache);
Glauber Costa2633d7a2012-12-18 14:22:34 -0800532void memcg_release_cache(struct kmem_cache *cachep);
533void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep);
534
Glauber Costa55007d82012-12-18 14:22:38 -0800535int memcg_update_cache_size(struct kmem_cache *s, int num_groups);
536void memcg_update_array_size(int num_groups);
Glauber Costad7f25f82012-12-18 14:22:40 -0800537
538struct kmem_cache *
539__memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
540
Glauber Costa1f458cb2012-12-18 14:22:50 -0800541void mem_cgroup_destroy_cache(struct kmem_cache *cachep);
Glauber Costa7cf27982012-12-18 14:22:55 -0800542void kmem_cache_destroy_memcg_children(struct kmem_cache *s);
Glauber Costa1f458cb2012-12-18 14:22:50 -0800543
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800544/**
545 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
546 * @gfp: the gfp allocation flags.
547 * @memcg: a pointer to the memcg this was charged against.
548 * @order: allocation order.
549 *
550 * returns true if the memcg where the current task belongs can hold this
551 * allocation.
552 *
553 * We return true automatically if this allocation is not to be accounted to
554 * any memcg.
555 */
556static inline bool
557memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
558{
559 if (!memcg_kmem_enabled())
560 return true;
561
562 /*
563 * __GFP_NOFAIL allocations will move on even if charging is not
564 * possible. Therefore we don't even try, and have this allocation
565 * unaccounted. We could in theory charge it with
566 * res_counter_charge_nofail, but we hope those allocations are rare,
567 * and won't be worth the trouble.
568 */
569 if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL))
570 return true;
571 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
572 return true;
573
574 /* If the test is dying, just let it go. */
575 if (unlikely(fatal_signal_pending(current)))
576 return true;
577
578 return __memcg_kmem_newpage_charge(gfp, memcg, order);
579}
580
581/**
582 * memcg_kmem_uncharge_pages: uncharge pages from memcg
583 * @page: pointer to struct page being freed
584 * @order: allocation order.
585 *
586 * there is no need to specify memcg here, since it is embedded in page_cgroup
587 */
588static inline void
589memcg_kmem_uncharge_pages(struct page *page, int order)
590{
591 if (memcg_kmem_enabled())
592 __memcg_kmem_uncharge_pages(page, order);
593}
594
595/**
596 * memcg_kmem_commit_charge: embeds correct memcg in a page
597 * @page: pointer to struct page recently allocated
598 * @memcg: the memcg structure we charged against
599 * @order: allocation order.
600 *
601 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
602 * failure of the allocation. if @page is NULL, this function will revert the
603 * charges. Otherwise, it will commit the memcg given by @memcg to the
604 * corresponding page_cgroup.
605 */
606static inline void
607memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
608{
609 if (memcg_kmem_enabled() && memcg)
610 __memcg_kmem_commit_charge(page, memcg, order);
611}
612
Glauber Costad7f25f82012-12-18 14:22:40 -0800613/**
614 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
615 * @cachep: the original global kmem cache
616 * @gfp: allocation flags.
617 *
618 * This function assumes that the task allocating, which determines the memcg
619 * in the page allocator, belongs to the same cgroup throughout the whole
620 * process. Misacounting can happen if the task calls memcg_kmem_get_cache()
621 * while belonging to a cgroup, and later on changes. This is considered
622 * acceptable, and should only happen upon task migration.
623 *
624 * Before the cache is created by the memcg core, there is also a possible
625 * imbalance: the task belongs to a memcg, but the cache being allocated from
626 * is the global cache, since the child cache is not yet guaranteed to be
627 * ready. This case is also fine, since in this case the GFP_KMEMCG will not be
628 * passed and the page allocator will not attempt any cgroup accounting.
629 */
630static __always_inline struct kmem_cache *
631memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
632{
633 if (!memcg_kmem_enabled())
634 return cachep;
635 if (gfp & __GFP_NOFAIL)
636 return cachep;
637 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
638 return cachep;
639 if (unlikely(fatal_signal_pending(current)))
640 return cachep;
641
642 return __memcg_kmem_get_cache(cachep, gfp);
643}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800644#else
Glauber Costa749c5412012-12-18 14:23:01 -0800645#define for_each_memcg_cache_index(_idx) \
646 for (; NULL; )
647
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800648static inline bool memcg_kmem_enabled(void)
649{
650 return false;
651}
652
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800653static inline bool
654memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
655{
656 return true;
657}
658
659static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
660{
661}
662
663static inline void
664memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
665{
666}
Glauber Costa2633d7a2012-12-18 14:22:34 -0800667
668static inline int memcg_cache_id(struct mem_cgroup *memcg)
669{
670 return -1;
671}
672
Glauber Costa943a4512012-12-18 14:23:03 -0800673static inline int
674memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
675 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -0800676{
677 return 0;
678}
679
680static inline void memcg_release_cache(struct kmem_cache *cachep)
681{
682}
683
684static inline void memcg_cache_list_add(struct mem_cgroup *memcg,
685 struct kmem_cache *s)
686{
687}
Glauber Costad7f25f82012-12-18 14:22:40 -0800688
689static inline struct kmem_cache *
690memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
691{
692 return cachep;
693}
Glauber Costa7cf27982012-12-18 14:22:55 -0800694
695static inline void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
696{
697}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800698#endif /* CONFIG_MEMCG_KMEM */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800699#endif /* _LINUX_MEMCONTROL_H */
700