blob: f952a07ce7a9d08903e1c57cc575c5655b9bdc05 [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 */
45 MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */
46 MEM_CGROUP_STAT_NSTATS,
Greg Thelen2a7106f2011-01-13 15:47:37 -080047};
48
Johannes Weiner56600482012-01-12 17:17:59 -080049struct mem_cgroup_reclaim_cookie {
50 struct zone *zone;
51 int priority;
52 unsigned int generation;
53};
54
Michal Hockode577802013-09-12 15:13:26 -070055enum mem_cgroup_filter_t {
56 VISIT, /* visit current node */
57 SKIP, /* skip the current node and continue traversal */
58 SKIP_TREE, /* skip the whole subtree and continue traversal */
59};
60
61/*
62 * mem_cgroup_filter_t predicate might instruct mem_cgroup_iter_cond how to
63 * iterate through the hierarchy tree. Each tree element is checked by the
64 * predicate before it is returned by the iterator. If a filter returns
65 * SKIP or SKIP_TREE then the iterator code continues traversal (with the
66 * next node down the hierarchy or the next node that doesn't belong under the
67 * memcg's subtree).
68 */
69typedef enum mem_cgroup_filter_t
70(*mem_cgroup_iter_filter)(struct mem_cgroup *memcg, struct mem_cgroup *root);
71
Andrew Mortonc255a452012-07-31 16:43:02 -070072#ifdef CONFIG_MEMCG
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -080073/*
74 * All "charge" functions with gfp_mask should use GFP_KERNEL or
75 * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
76 * alloc memory but reclaims memory from all available zones. So, "where I want
77 * memory from" bits of gfp_mask has no meaning. So any bits of that field is
78 * available but adding a rule is better. charge functions' gfp_mask should
79 * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
80 * codes.
81 * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
82 */
Pavel Emelianov78fb7462008-02-07 00:13:51 -080083
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080084extern int mem_cgroup_newpage_charge(struct page *page, struct mm_struct *mm,
Balbir Singhe1a1cd52008-02-07 00:14:02 -080085 gfp_t gfp_mask);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080086/* for swap handling */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080087extern int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
Johannes Weiner72835c82012-01-12 17:18:32 -080088 struct page *page, gfp_t mask, struct mem_cgroup **memcgp);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080089extern void mem_cgroup_commit_charge_swapin(struct page *page,
Johannes Weiner72835c82012-01-12 17:18:32 -080090 struct mem_cgroup *memcg);
91extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080092
Hugh Dickins82895462008-03-04 14:29:08 -080093extern int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
94 gfp_t gfp_mask);
Johannes Weiner925b7672012-01-12 17:18:15 -080095
96struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
Hugh Dickinsfa9add62012-05-29 15:07:09 -070097struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -080098
99/* For coalescing uncharge for reducing memcg' overhead*/
100extern void mem_cgroup_uncharge_start(void);
101extern void mem_cgroup_uncharge_end(void);
102
Balbir Singh3c541e12008-02-07 00:14:41 -0800103extern void mem_cgroup_uncharge_page(struct page *page);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700104extern void mem_cgroup_uncharge_cache_page(struct page *page);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700105
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700106bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
107 struct mem_cgroup *memcg);
David Rientjesffbdccf2013-07-03 15:01:23 -0700108bool task_in_mem_cgroup(struct task_struct *task,
109 const struct mem_cgroup *memcg);
David Rientjes3062fc62008-02-07 00:14:03 -0800110
Wu Fengguange42d9d52009-12-16 12:19:59 +0100111extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
Balbir Singhcf475ad2008-04-29 01:00:16 -0700112extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700113extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
Balbir Singhcf475ad2008-04-29 01:00:16 -0700114
Glauber Costae1aab162011-12-11 21:47:03 +0000115extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
Tejun Heo182446d2013-08-08 20:11:24 -0400116extern struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css);
Glauber Costae1aab162011-12-11 21:47:03 +0000117
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800118static inline
Johannes Weiner587af302012-10-08 16:34:12 -0700119bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg)
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800120{
Johannes Weiner587af302012-10-08 16:34:12 -0700121 struct mem_cgroup *task_memcg;
122 bool match;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700123
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800124 rcu_read_lock();
Johannes Weiner587af302012-10-08 16:34:12 -0700125 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
126 match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800127 rcu_read_unlock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700128 return match;
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800129}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800130
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700131extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
Wu Fengguangd3242362009-12-16 12:19:59 +0100132
Johannes Weiner0030f532012-07-31 16:45:25 -0700133extern void
134mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
135 struct mem_cgroup **memcgp);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700136extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800137 struct page *oldpage, struct page *newpage, bool migration_ok);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800138
Michal Hockode577802013-09-12 15:13:26 -0700139struct mem_cgroup *mem_cgroup_iter_cond(struct mem_cgroup *root,
140 struct mem_cgroup *prev,
141 struct mem_cgroup_reclaim_cookie *reclaim,
142 mem_cgroup_iter_filter cond);
143
144static inline struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
145 struct mem_cgroup *prev,
146 struct mem_cgroup_reclaim_cookie *reclaim)
147{
148 return mem_cgroup_iter_cond(root, prev, reclaim, NULL);
149}
150
Johannes Weiner56600482012-01-12 17:17:59 -0800151void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
152
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800153/*
154 * For memory reclaim.
155 */
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700156int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
Ying Han889976d2011-05-26 16:25:33 -0700157int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700158unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700159void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
Balbir Singhe2224322009-04-02 16:57:39 -0700160extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
161 struct task_struct *p);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800162extern void mem_cgroup_replace_page_cache(struct page *oldpage,
163 struct page *newpage);
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800164
Johannes Weiner519e5242013-09-12 15:13:42 -0700165/**
166 * mem_cgroup_toggle_oom - toggle the memcg OOM killer for the current task
167 * @new: true to enable, false to disable
168 *
169 * Toggle whether a failed memcg charge should invoke the OOM killer
170 * or just return -ENOMEM. Returns the previous toggle state.
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700171 *
172 * NOTE: Any path that enables the OOM killer before charging must
173 * call mem_cgroup_oom_synchronize() afterward to finalize the
174 * OOM handling and clean up.
Johannes Weiner519e5242013-09-12 15:13:42 -0700175 */
176static inline bool mem_cgroup_toggle_oom(bool new)
177{
178 bool old;
179
180 old = current->memcg_oom.may_oom;
181 current->memcg_oom.may_oom = new;
182
183 return old;
184}
185
186static inline void mem_cgroup_enable_oom(void)
187{
188 bool old = mem_cgroup_toggle_oom(true);
189
190 WARN_ON(old == true);
191}
192
193static inline void mem_cgroup_disable_oom(void)
194{
195 bool old = mem_cgroup_toggle_oom(false);
196
197 WARN_ON(old == false);
198}
199
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700200static inline bool task_in_memcg_oom(struct task_struct *p)
201{
202 return p->memcg_oom.in_memcg_oom;
203}
204
205bool mem_cgroup_oom_synchronize(void);
206
Andrew Mortonc255a452012-07-31 16:43:02 -0700207#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -0800208extern int do_swap_account;
209#endif
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800210
211static inline bool mem_cgroup_disabled(void)
212{
213 if (mem_cgroup_subsys.disabled)
214 return true;
215 return false;
216}
217
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700218void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
219 unsigned long *flags);
220
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700221extern atomic_t memcg_moving;
222
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700223static inline void mem_cgroup_begin_update_page_stat(struct page *page,
224 bool *locked, unsigned long *flags)
225{
226 if (mem_cgroup_disabled())
227 return;
228 rcu_read_lock();
229 *locked = false;
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700230 if (atomic_read(&memcg_moving))
231 __mem_cgroup_begin_update_page_stat(page, locked, flags);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700232}
233
234void __mem_cgroup_end_update_page_stat(struct page *page,
235 unsigned long *flags);
236static inline void mem_cgroup_end_update_page_stat(struct page *page,
237 bool *locked, unsigned long *flags)
238{
239 if (mem_cgroup_disabled())
240 return;
241 if (*locked)
242 __mem_cgroup_end_update_page_stat(page, flags);
243 rcu_read_unlock();
244}
245
Greg Thelen2a7106f2011-01-13 15:47:37 -0800246void mem_cgroup_update_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700247 enum mem_cgroup_stat_index idx,
Greg Thelen2a7106f2011-01-13 15:47:37 -0800248 int val);
249
250static inline void mem_cgroup_inc_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700251 enum mem_cgroup_stat_index idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -0800252{
253 mem_cgroup_update_page_stat(page, idx, 1);
254}
255
256static inline void mem_cgroup_dec_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700257 enum mem_cgroup_stat_index idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -0800258{
259 mem_cgroup_update_page_stat(page, idx, -1);
260}
261
Michal Hockode577802013-09-12 15:13:26 -0700262enum mem_cgroup_filter_t
263mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
Michal Hockoa5b7c872013-09-12 15:13:25 -0700264 struct mem_cgroup *root);
David Rientjesa63d83f2010-08-09 17:19:46 -0700265
David Rientjes68ae5642012-12-12 13:51:57 -0800266void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
267static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
268 enum vm_event_item idx)
269{
270 if (mem_cgroup_disabled())
271 return;
272 __mem_cgroup_count_vm_event(mm, idx);
273}
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800274#ifdef CONFIG_TRANSPARENT_HUGEPAGE
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800275void mem_cgroup_split_huge_fixup(struct page *head);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800276#endif
277
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700278#ifdef CONFIG_DEBUG_VM
279bool mem_cgroup_bad_page_check(struct page *page);
280void mem_cgroup_print_bad_page(struct page *page);
281#endif
Andrew Mortonc255a452012-07-31 16:43:02 -0700282#else /* CONFIG_MEMCG */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800283struct mem_cgroup;
284
285static inline int mem_cgroup_newpage_charge(struct page *page,
Hugh Dickins82895462008-03-04 14:29:08 -0800286 struct mm_struct *mm, gfp_t gfp_mask)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800287{
288 return 0;
289}
290
Hugh Dickins82895462008-03-04 14:29:08 -0800291static inline int mem_cgroup_cache_charge(struct page *page,
292 struct mm_struct *mm, gfp_t gfp_mask)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800293{
Hugh Dickins82895462008-03-04 14:29:08 -0800294 return 0;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800295}
296
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800297static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
Johannes Weiner72835c82012-01-12 17:18:32 -0800298 struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800299{
300 return 0;
301}
302
303static inline void mem_cgroup_commit_charge_swapin(struct page *page,
Johannes Weiner72835c82012-01-12 17:18:32 -0800304 struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800305{
306}
307
Johannes Weiner72835c82012-01-12 17:18:32 -0800308static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800309{
310}
311
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -0800312static inline void mem_cgroup_uncharge_start(void)
313{
314}
315
316static inline void mem_cgroup_uncharge_end(void)
317{
318}
319
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800320static inline void mem_cgroup_uncharge_page(struct page *page)
321{
322}
323
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700324static inline void mem_cgroup_uncharge_cache_page(struct page *page)
325{
326}
327
Johannes Weiner925b7672012-01-12 17:18:15 -0800328static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
329 struct mem_cgroup *memcg)
330{
331 return &zone->lruvec;
332}
333
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700334static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
335 struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -0700336{
Johannes Weiner925b7672012-01-12 17:18:15 -0800337 return &zone->lruvec;
Balbir Singh66e17072008-02-07 00:13:56 -0800338}
339
Wu Fengguange42d9d52009-12-16 12:19:59 +0100340static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
341{
342 return NULL;
343}
344
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700345static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
346{
347 return NULL;
348}
349
Johannes Weiner587af302012-10-08 16:34:12 -0700350static inline bool mm_match_cgroup(struct mm_struct *mm,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700351 struct mem_cgroup *memcg)
Balbir Singhbed71612008-02-07 00:14:01 -0800352{
Johannes Weiner587af302012-10-08 16:34:12 -0700353 return true;
Balbir Singhbed71612008-02-07 00:14:01 -0800354}
355
David Rientjesffbdccf2013-07-03 15:01:23 -0700356static inline bool task_in_mem_cgroup(struct task_struct *task,
357 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -0800358{
David Rientjesffbdccf2013-07-03 15:01:23 -0700359 return true;
David Rientjes4c4a2212008-02-07 00:14:06 -0800360}
361
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700362static inline struct cgroup_subsys_state
363 *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100364{
365 return NULL;
366}
367
Johannes Weiner0030f532012-07-31 16:45:25 -0700368static inline void
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -0700369mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
Johannes Weiner0030f532012-07-31 16:45:25 -0700370 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800371{
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800372}
373
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700374static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800375 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800376{
377}
Michal Hockode577802013-09-12 15:13:26 -0700378static inline struct mem_cgroup *
379mem_cgroup_iter_cond(struct mem_cgroup *root,
380 struct mem_cgroup *prev,
381 struct mem_cgroup_reclaim_cookie *reclaim,
382 mem_cgroup_iter_filter cond)
383{
384 /* first call must return non-NULL, second return NULL */
385 return (struct mem_cgroup *)(unsigned long)!prev;
386}
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800387
Johannes Weiner56600482012-01-12 17:17:59 -0800388static inline struct mem_cgroup *
389mem_cgroup_iter(struct mem_cgroup *root,
390 struct mem_cgroup *prev,
391 struct mem_cgroup_reclaim_cookie *reclaim)
392{
393 return NULL;
394}
395
396static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
397 struct mem_cgroup *prev)
398{
399}
400
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800401static inline bool mem_cgroup_disabled(void)
402{
403 return true;
404}
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800405
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800406static inline int
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700407mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800408{
409 return 1;
410}
411
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800412static inline unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700413mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800414{
415 return 0;
416}
417
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700418static inline void
419mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
420 int increment)
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800421{
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800422}
423
Balbir Singhe2224322009-04-02 16:57:39 -0700424static inline void
425mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
426{
427}
428
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700429static inline void mem_cgroup_begin_update_page_stat(struct page *page,
430 bool *locked, unsigned long *flags)
431{
432}
433
434static inline void mem_cgroup_end_update_page_stat(struct page *page,
435 bool *locked, unsigned long *flags)
436{
437}
438
Johannes Weiner519e5242013-09-12 15:13:42 -0700439static inline bool mem_cgroup_toggle_oom(bool new)
440{
441 return false;
442}
443
444static inline void mem_cgroup_enable_oom(void)
445{
446}
447
448static inline void mem_cgroup_disable_oom(void)
449{
450}
451
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700452static inline bool task_in_memcg_oom(struct task_struct *p)
453{
454 return false;
455}
456
457static inline bool mem_cgroup_oom_synchronize(void)
458{
459 return false;
460}
461
Greg Thelen2a7106f2011-01-13 15:47:37 -0800462static inline void mem_cgroup_inc_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700463 enum mem_cgroup_stat_index idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -0800464{
465}
466
467static inline void mem_cgroup_dec_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700468 enum mem_cgroup_stat_index idx)
Balbir Singhd69b0422009-06-17 16:26:34 -0700469{
470}
471
Balbir Singh4e416952009-09-23 15:56:39 -0700472static inline
Michal Hockode577802013-09-12 15:13:26 -0700473enum mem_cgroup_filter_t
474mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
Michal Hockoa5b7c872013-09-12 15:13:25 -0700475 struct mem_cgroup *root)
Balbir Singh4e416952009-09-23 15:56:39 -0700476{
Michal Hockode577802013-09-12 15:13:26 -0700477 return VISIT;
Balbir Singh4e416952009-09-23 15:56:39 -0700478}
479
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800480static inline void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800481{
482}
483
Ying Han456f9982011-05-26 16:25:38 -0700484static inline
485void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
486{
487}
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800488static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
489 struct page *newpage)
490{
491}
Andrew Mortonc255a452012-07-31 16:43:02 -0700492#endif /* CONFIG_MEMCG */
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800493
Andrew Mortonc255a452012-07-31 16:43:02 -0700494#if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700495static inline bool
496mem_cgroup_bad_page_check(struct page *page)
497{
498 return false;
499}
500
501static inline void
502mem_cgroup_print_bad_page(struct page *page)
503{
504}
505#endif
506
Glauber Costae1aab162011-12-11 21:47:03 +0000507enum {
508 UNDER_LIMIT,
509 SOFT_LIMIT,
510 OVER_LIMIT,
511};
512
513struct sock;
David Rientjescd590852012-10-10 15:54:08 -0700514#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000515void sock_update_memcg(struct sock *sk);
516void sock_release_memcg(struct sock *sk);
517#else
518static inline void sock_update_memcg(struct sock *sk)
519{
520}
521static inline void sock_release_memcg(struct sock *sk)
522{
523}
David Rientjescd590852012-10-10 15:54:08 -0700524#endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800525
526#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -0800527extern struct static_key memcg_kmem_enabled_key;
Glauber Costa749c5412012-12-18 14:23:01 -0800528
529extern int memcg_limited_groups_array_size;
Glauber Costaebe945c2012-12-18 14:23:10 -0800530
531/*
532 * Helper macro to loop through all memcg-specific caches. Callers must still
533 * check if the cache is valid (it is either valid or NULL).
534 * the slab_mutex must be held when looping through those caches
535 */
Glauber Costa749c5412012-12-18 14:23:01 -0800536#define for_each_memcg_cache_index(_idx) \
Glauber Costa91c777d2013-02-04 14:28:49 -0800537 for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++)
Glauber Costa749c5412012-12-18 14:23:01 -0800538
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800539static inline bool memcg_kmem_enabled(void)
540{
Glauber Costaa8964b92012-12-18 14:22:09 -0800541 return static_key_false(&memcg_kmem_enabled_key);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800542}
543
544/*
545 * In general, we'll do everything in our power to not incur in any overhead
546 * for non-memcg users for the kmem functions. Not even a function call, if we
547 * can avoid it.
548 *
549 * Therefore, we'll inline all those functions so that in the best case, we'll
550 * see that kmemcg is off for everybody and proceed quickly. If it is on,
551 * we'll still do most of the flag checking inline. We check a lot of
552 * conditions, but because they are pretty simple, they are expected to be
553 * fast.
554 */
555bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
556 int order);
557void __memcg_kmem_commit_charge(struct page *page,
558 struct mem_cgroup *memcg, int order);
559void __memcg_kmem_uncharge_pages(struct page *page, int order);
560
Glauber Costa2633d7a2012-12-18 14:22:34 -0800561int memcg_cache_id(struct mem_cgroup *memcg);
Glauber Costa943a4512012-12-18 14:23:03 -0800562int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
563 struct kmem_cache *root_cache);
Glauber Costa2633d7a2012-12-18 14:22:34 -0800564void memcg_release_cache(struct kmem_cache *cachep);
565void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep);
566
Glauber Costa55007d82012-12-18 14:22:38 -0800567int memcg_update_cache_size(struct kmem_cache *s, int num_groups);
568void memcg_update_array_size(int num_groups);
Glauber Costad7f25f82012-12-18 14:22:40 -0800569
570struct kmem_cache *
571__memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
572
Glauber Costa1f458cb2012-12-18 14:22:50 -0800573void mem_cgroup_destroy_cache(struct kmem_cache *cachep);
Glauber Costa7cf27982012-12-18 14:22:55 -0800574void kmem_cache_destroy_memcg_children(struct kmem_cache *s);
Glauber Costa1f458cb2012-12-18 14:22:50 -0800575
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800576/**
577 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
578 * @gfp: the gfp allocation flags.
579 * @memcg: a pointer to the memcg this was charged against.
580 * @order: allocation order.
581 *
582 * returns true if the memcg where the current task belongs can hold this
583 * allocation.
584 *
585 * We return true automatically if this allocation is not to be accounted to
586 * any memcg.
587 */
588static inline bool
589memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
590{
591 if (!memcg_kmem_enabled())
592 return true;
593
594 /*
595 * __GFP_NOFAIL allocations will move on even if charging is not
596 * possible. Therefore we don't even try, and have this allocation
597 * unaccounted. We could in theory charge it with
598 * res_counter_charge_nofail, but we hope those allocations are rare,
599 * and won't be worth the trouble.
600 */
601 if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL))
602 return true;
603 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
604 return true;
605
606 /* If the test is dying, just let it go. */
607 if (unlikely(fatal_signal_pending(current)))
608 return true;
609
610 return __memcg_kmem_newpage_charge(gfp, memcg, order);
611}
612
613/**
614 * memcg_kmem_uncharge_pages: uncharge pages from memcg
615 * @page: pointer to struct page being freed
616 * @order: allocation order.
617 *
618 * there is no need to specify memcg here, since it is embedded in page_cgroup
619 */
620static inline void
621memcg_kmem_uncharge_pages(struct page *page, int order)
622{
623 if (memcg_kmem_enabled())
624 __memcg_kmem_uncharge_pages(page, order);
625}
626
627/**
628 * memcg_kmem_commit_charge: embeds correct memcg in a page
629 * @page: pointer to struct page recently allocated
630 * @memcg: the memcg structure we charged against
631 * @order: allocation order.
632 *
633 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
634 * failure of the allocation. if @page is NULL, this function will revert the
635 * charges. Otherwise, it will commit the memcg given by @memcg to the
636 * corresponding page_cgroup.
637 */
638static inline void
639memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
640{
641 if (memcg_kmem_enabled() && memcg)
642 __memcg_kmem_commit_charge(page, memcg, order);
643}
644
Glauber Costad7f25f82012-12-18 14:22:40 -0800645/**
646 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
647 * @cachep: the original global kmem cache
648 * @gfp: allocation flags.
649 *
650 * This function assumes that the task allocating, which determines the memcg
651 * in the page allocator, belongs to the same cgroup throughout the whole
652 * process. Misacounting can happen if the task calls memcg_kmem_get_cache()
653 * while belonging to a cgroup, and later on changes. This is considered
654 * acceptable, and should only happen upon task migration.
655 *
656 * Before the cache is created by the memcg core, there is also a possible
657 * imbalance: the task belongs to a memcg, but the cache being allocated from
658 * is the global cache, since the child cache is not yet guaranteed to be
659 * ready. This case is also fine, since in this case the GFP_KMEMCG will not be
660 * passed and the page allocator will not attempt any cgroup accounting.
661 */
662static __always_inline struct kmem_cache *
663memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
664{
665 if (!memcg_kmem_enabled())
666 return cachep;
667 if (gfp & __GFP_NOFAIL)
668 return cachep;
669 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
670 return cachep;
671 if (unlikely(fatal_signal_pending(current)))
672 return cachep;
673
674 return __memcg_kmem_get_cache(cachep, gfp);
675}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800676#else
Glauber Costa749c5412012-12-18 14:23:01 -0800677#define for_each_memcg_cache_index(_idx) \
678 for (; NULL; )
679
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800680static inline bool memcg_kmem_enabled(void)
681{
682 return false;
683}
684
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800685static inline bool
686memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
687{
688 return true;
689}
690
691static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
692{
693}
694
695static inline void
696memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
697{
698}
Glauber Costa2633d7a2012-12-18 14:22:34 -0800699
700static inline int memcg_cache_id(struct mem_cgroup *memcg)
701{
702 return -1;
703}
704
Glauber Costa943a4512012-12-18 14:23:03 -0800705static inline int
706memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
707 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -0800708{
709 return 0;
710}
711
712static inline void memcg_release_cache(struct kmem_cache *cachep)
713{
714}
715
716static inline void memcg_cache_list_add(struct mem_cgroup *memcg,
717 struct kmem_cache *s)
718{
719}
Glauber Costad7f25f82012-12-18 14:22:40 -0800720
721static inline struct kmem_cache *
722memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
723{
724 return cachep;
725}
Glauber Costa7cf27982012-12-18 14:22:55 -0800726
727static inline void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
728{
729}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800730#endif /* CONFIG_MEMCG_KMEM */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800731#endif /* _LINUX_MEMCONTROL_H */
732