blob: ef2b9bd7fafa7af1b6e9d94d5ba148283782a505 [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
Andrew Mortonc255a452012-07-31 16:43:02 -070056#ifdef CONFIG_MEMCG
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -080057/*
58 * All "charge" functions with gfp_mask should use GFP_KERNEL or
59 * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
60 * alloc memory but reclaims memory from all available zones. So, "where I want
61 * memory from" bits of gfp_mask has no meaning. So any bits of that field is
62 * available but adding a rule is better. charge functions' gfp_mask should
63 * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
64 * codes.
65 * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
66 */
Pavel Emelianov78fb7462008-02-07 00:13:51 -080067
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080068extern int mem_cgroup_newpage_charge(struct page *page, struct mm_struct *mm,
Balbir Singhe1a1cd52008-02-07 00:14:02 -080069 gfp_t gfp_mask);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080070/* for swap handling */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080071extern int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
Johannes Weiner72835c82012-01-12 17:18:32 -080072 struct page *page, gfp_t mask, struct mem_cgroup **memcgp);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080073extern void mem_cgroup_commit_charge_swapin(struct page *page,
Johannes Weiner72835c82012-01-12 17:18:32 -080074 struct mem_cgroup *memcg);
75extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080076
Hugh Dickins82895462008-03-04 14:29:08 -080077extern int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
78 gfp_t gfp_mask);
Johannes Weiner925b7672012-01-12 17:18:15 -080079
80struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
Hugh Dickinsfa9add62012-05-29 15:07:09 -070081struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -080082
83/* For coalescing uncharge for reducing memcg' overhead*/
84extern void mem_cgroup_uncharge_start(void);
85extern void mem_cgroup_uncharge_end(void);
86
Balbir Singh3c541e12008-02-07 00:14:41 -080087extern void mem_cgroup_uncharge_page(struct page *page);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -070088extern void mem_cgroup_uncharge_cache_page(struct page *page);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -070089
Johannes Weinerc3ac9a82012-05-29 15:06:25 -070090bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
91 struct mem_cgroup *memcg);
David Rientjesffbdccf2013-07-03 15:01:23 -070092bool task_in_mem_cgroup(struct task_struct *task,
93 const struct mem_cgroup *memcg);
David Rientjes3062fc62008-02-07 00:14:03 -080094
Wu Fengguange42d9d52009-12-16 12:19:59 +010095extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
Balbir Singhcf475ad2008-04-29 01:00:16 -070096extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -070097extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
Balbir Singhcf475ad2008-04-29 01:00:16 -070098
Glauber Costae1aab162011-12-11 21:47:03 +000099extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
Tejun Heo182446d2013-08-08 20:11:24 -0400100extern struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css);
Glauber Costae1aab162011-12-11 21:47:03 +0000101
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800102static inline
Johannes Weiner587af302012-10-08 16:34:12 -0700103bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg)
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800104{
Johannes Weiner587af302012-10-08 16:34:12 -0700105 struct mem_cgroup *task_memcg;
106 bool match;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700107
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800108 rcu_read_lock();
Johannes Weiner587af302012-10-08 16:34:12 -0700109 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
110 match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800111 rcu_read_unlock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700112 return match;
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800113}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800114
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700115extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
Wu Fengguangd3242362009-12-16 12:19:59 +0100116
Johannes Weiner0030f532012-07-31 16:45:25 -0700117extern void
118mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
119 struct mem_cgroup **memcgp);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700120extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800121 struct page *oldpage, struct page *newpage, bool migration_ok);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800122
Andrew Morton694fbc02013-09-24 15:27:37 -0700123struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
124 struct mem_cgroup *,
125 struct mem_cgroup_reclaim_cookie *);
Johannes Weiner56600482012-01-12 17:17:59 -0800126void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
127
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800128/*
129 * For memory reclaim.
130 */
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700131int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
Ying Han889976d2011-05-26 16:25:33 -0700132int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700133unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700134void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
Balbir Singhe2224322009-04-02 16:57:39 -0700135extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
136 struct task_struct *p);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800137extern void mem_cgroup_replace_page_cache(struct page *oldpage,
138 struct page *newpage);
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800139
Johannes Weiner519e5242013-09-12 15:13:42 -0700140/**
141 * mem_cgroup_toggle_oom - toggle the memcg OOM killer for the current task
142 * @new: true to enable, false to disable
143 *
144 * Toggle whether a failed memcg charge should invoke the OOM killer
145 * or just return -ENOMEM. Returns the previous toggle state.
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700146 *
147 * NOTE: Any path that enables the OOM killer before charging must
148 * call mem_cgroup_oom_synchronize() afterward to finalize the
149 * OOM handling and clean up.
Johannes Weiner519e5242013-09-12 15:13:42 -0700150 */
151static inline bool mem_cgroup_toggle_oom(bool new)
152{
153 bool old;
154
155 old = current->memcg_oom.may_oom;
156 current->memcg_oom.may_oom = new;
157
158 return old;
159}
160
161static inline void mem_cgroup_enable_oom(void)
162{
163 bool old = mem_cgroup_toggle_oom(true);
164
165 WARN_ON(old == true);
166}
167
168static inline void mem_cgroup_disable_oom(void)
169{
170 bool old = mem_cgroup_toggle_oom(false);
171
172 WARN_ON(old == false);
173}
174
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700175static inline bool task_in_memcg_oom(struct task_struct *p)
176{
177 return p->memcg_oom.in_memcg_oom;
178}
179
180bool mem_cgroup_oom_synchronize(void);
181
Andrew Mortonc255a452012-07-31 16:43:02 -0700182#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -0800183extern int do_swap_account;
184#endif
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800185
186static inline bool mem_cgroup_disabled(void)
187{
188 if (mem_cgroup_subsys.disabled)
189 return true;
190 return false;
191}
192
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700193void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
194 unsigned long *flags);
195
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700196extern atomic_t memcg_moving;
197
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700198static inline void mem_cgroup_begin_update_page_stat(struct page *page,
199 bool *locked, unsigned long *flags)
200{
201 if (mem_cgroup_disabled())
202 return;
203 rcu_read_lock();
204 *locked = false;
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700205 if (atomic_read(&memcg_moving))
206 __mem_cgroup_begin_update_page_stat(page, locked, flags);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700207}
208
209void __mem_cgroup_end_update_page_stat(struct page *page,
210 unsigned long *flags);
211static inline void mem_cgroup_end_update_page_stat(struct page *page,
212 bool *locked, unsigned long *flags)
213{
214 if (mem_cgroup_disabled())
215 return;
216 if (*locked)
217 __mem_cgroup_end_update_page_stat(page, flags);
218 rcu_read_unlock();
219}
220
Greg Thelen2a7106f2011-01-13 15:47:37 -0800221void mem_cgroup_update_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700222 enum mem_cgroup_stat_index idx,
Greg Thelen2a7106f2011-01-13 15:47:37 -0800223 int val);
224
225static inline void mem_cgroup_inc_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700226 enum mem_cgroup_stat_index idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -0800227{
228 mem_cgroup_update_page_stat(page, idx, 1);
229}
230
231static inline void mem_cgroup_dec_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700232 enum mem_cgroup_stat_index idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -0800233{
234 mem_cgroup_update_page_stat(page, idx, -1);
235}
236
Andrew Morton694fbc02013-09-24 15:27:37 -0700237bool mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
Michal Hockoa5b7c872013-09-12 15:13:25 -0700238 struct mem_cgroup *root);
David Rientjesa63d83f2010-08-09 17:19:46 -0700239
David Rientjes68ae5642012-12-12 13:51:57 -0800240void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
241static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
242 enum vm_event_item idx)
243{
244 if (mem_cgroup_disabled())
245 return;
246 __mem_cgroup_count_vm_event(mm, idx);
247}
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800248#ifdef CONFIG_TRANSPARENT_HUGEPAGE
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800249void mem_cgroup_split_huge_fixup(struct page *head);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800250#endif
251
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700252#ifdef CONFIG_DEBUG_VM
253bool mem_cgroup_bad_page_check(struct page *page);
254void mem_cgroup_print_bad_page(struct page *page);
255#endif
Andrew Mortonc255a452012-07-31 16:43:02 -0700256#else /* CONFIG_MEMCG */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800257struct mem_cgroup;
258
259static inline int mem_cgroup_newpage_charge(struct page *page,
Hugh Dickins82895462008-03-04 14:29:08 -0800260 struct mm_struct *mm, gfp_t gfp_mask)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800261{
262 return 0;
263}
264
Hugh Dickins82895462008-03-04 14:29:08 -0800265static inline int mem_cgroup_cache_charge(struct page *page,
266 struct mm_struct *mm, gfp_t gfp_mask)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800267{
Hugh Dickins82895462008-03-04 14:29:08 -0800268 return 0;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800269}
270
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800271static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
Johannes Weiner72835c82012-01-12 17:18:32 -0800272 struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800273{
274 return 0;
275}
276
277static inline void mem_cgroup_commit_charge_swapin(struct page *page,
Johannes Weiner72835c82012-01-12 17:18:32 -0800278 struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800279{
280}
281
Johannes Weiner72835c82012-01-12 17:18:32 -0800282static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800283{
284}
285
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -0800286static inline void mem_cgroup_uncharge_start(void)
287{
288}
289
290static inline void mem_cgroup_uncharge_end(void)
291{
292}
293
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800294static inline void mem_cgroup_uncharge_page(struct page *page)
295{
296}
297
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700298static inline void mem_cgroup_uncharge_cache_page(struct page *page)
299{
300}
301
Johannes Weiner925b7672012-01-12 17:18:15 -0800302static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
303 struct mem_cgroup *memcg)
304{
305 return &zone->lruvec;
306}
307
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700308static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
309 struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -0700310{
Johannes Weiner925b7672012-01-12 17:18:15 -0800311 return &zone->lruvec;
Balbir Singh66e17072008-02-07 00:13:56 -0800312}
313
Wu Fengguange42d9d52009-12-16 12:19:59 +0100314static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
315{
316 return NULL;
317}
318
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700319static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
320{
321 return NULL;
322}
323
Johannes Weiner587af302012-10-08 16:34:12 -0700324static inline bool mm_match_cgroup(struct mm_struct *mm,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700325 struct mem_cgroup *memcg)
Balbir Singhbed71612008-02-07 00:14:01 -0800326{
Johannes Weiner587af302012-10-08 16:34:12 -0700327 return true;
Balbir Singhbed71612008-02-07 00:14:01 -0800328}
329
David Rientjesffbdccf2013-07-03 15:01:23 -0700330static inline bool task_in_mem_cgroup(struct task_struct *task,
331 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -0800332{
David Rientjesffbdccf2013-07-03 15:01:23 -0700333 return true;
David Rientjes4c4a2212008-02-07 00:14:06 -0800334}
335
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700336static inline struct cgroup_subsys_state
337 *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100338{
339 return NULL;
340}
341
Johannes Weiner0030f532012-07-31 16:45:25 -0700342static inline void
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -0700343mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
Johannes Weiner0030f532012-07-31 16:45:25 -0700344 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800345{
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800346}
347
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700348static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800349 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800350{
351}
352
Johannes Weiner56600482012-01-12 17:17:59 -0800353static inline struct mem_cgroup *
354mem_cgroup_iter(struct mem_cgroup *root,
355 struct mem_cgroup *prev,
356 struct mem_cgroup_reclaim_cookie *reclaim)
357{
358 return NULL;
359}
360
361static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
362 struct mem_cgroup *prev)
363{
364}
365
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800366static inline bool mem_cgroup_disabled(void)
367{
368 return true;
369}
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800370
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800371static inline int
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700372mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800373{
374 return 1;
375}
376
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800377static inline unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700378mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800379{
380 return 0;
381}
382
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700383static inline void
384mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
385 int increment)
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800386{
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800387}
388
Balbir Singhe2224322009-04-02 16:57:39 -0700389static inline void
390mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
391{
392}
393
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700394static inline void mem_cgroup_begin_update_page_stat(struct page *page,
395 bool *locked, unsigned long *flags)
396{
397}
398
399static inline void mem_cgroup_end_update_page_stat(struct page *page,
400 bool *locked, unsigned long *flags)
401{
402}
403
Johannes Weiner519e5242013-09-12 15:13:42 -0700404static inline bool mem_cgroup_toggle_oom(bool new)
405{
406 return false;
407}
408
409static inline void mem_cgroup_enable_oom(void)
410{
411}
412
413static inline void mem_cgroup_disable_oom(void)
414{
415}
416
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700417static inline bool task_in_memcg_oom(struct task_struct *p)
418{
419 return false;
420}
421
422static inline bool mem_cgroup_oom_synchronize(void)
423{
424 return false;
425}
426
Greg Thelen2a7106f2011-01-13 15:47:37 -0800427static inline void mem_cgroup_inc_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700428 enum mem_cgroup_stat_index idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -0800429{
430}
431
432static inline void mem_cgroup_dec_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700433 enum mem_cgroup_stat_index idx)
Balbir Singhd69b0422009-06-17 16:26:34 -0700434{
435}
436
Balbir Singh4e416952009-09-23 15:56:39 -0700437static inline
Andrew Morton694fbc02013-09-24 15:27:37 -0700438bool mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
Michal Hockoa5b7c872013-09-12 15:13:25 -0700439 struct mem_cgroup *root)
Balbir Singh4e416952009-09-23 15:56:39 -0700440{
Andrew Morton694fbc02013-09-24 15:27:37 -0700441 return false;
Balbir Singh4e416952009-09-23 15:56:39 -0700442}
443
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800444static inline void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800445{
446}
447
Ying Han456f9982011-05-26 16:25:38 -0700448static inline
449void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
450{
451}
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800452static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
453 struct page *newpage)
454{
455}
Andrew Mortonc255a452012-07-31 16:43:02 -0700456#endif /* CONFIG_MEMCG */
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800457
Andrew Mortonc255a452012-07-31 16:43:02 -0700458#if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700459static inline bool
460mem_cgroup_bad_page_check(struct page *page)
461{
462 return false;
463}
464
465static inline void
466mem_cgroup_print_bad_page(struct page *page)
467{
468}
469#endif
470
Glauber Costae1aab162011-12-11 21:47:03 +0000471enum {
472 UNDER_LIMIT,
473 SOFT_LIMIT,
474 OVER_LIMIT,
475};
476
477struct sock;
David Rientjescd590852012-10-10 15:54:08 -0700478#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000479void sock_update_memcg(struct sock *sk);
480void sock_release_memcg(struct sock *sk);
481#else
482static inline void sock_update_memcg(struct sock *sk)
483{
484}
485static inline void sock_release_memcg(struct sock *sk)
486{
487}
David Rientjescd590852012-10-10 15:54:08 -0700488#endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800489
490#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -0800491extern struct static_key memcg_kmem_enabled_key;
Glauber Costa749c5412012-12-18 14:23:01 -0800492
493extern int memcg_limited_groups_array_size;
Glauber Costaebe945c2012-12-18 14:23:10 -0800494
495/*
496 * Helper macro to loop through all memcg-specific caches. Callers must still
497 * check if the cache is valid (it is either valid or NULL).
498 * the slab_mutex must be held when looping through those caches
499 */
Glauber Costa749c5412012-12-18 14:23:01 -0800500#define for_each_memcg_cache_index(_idx) \
Glauber Costa91c777d2013-02-04 14:28:49 -0800501 for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++)
Glauber Costa749c5412012-12-18 14:23:01 -0800502
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800503static inline bool memcg_kmem_enabled(void)
504{
Glauber Costaa8964b92012-12-18 14:22:09 -0800505 return static_key_false(&memcg_kmem_enabled_key);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800506}
507
508/*
509 * In general, we'll do everything in our power to not incur in any overhead
510 * for non-memcg users for the kmem functions. Not even a function call, if we
511 * can avoid it.
512 *
513 * Therefore, we'll inline all those functions so that in the best case, we'll
514 * see that kmemcg is off for everybody and proceed quickly. If it is on,
515 * we'll still do most of the flag checking inline. We check a lot of
516 * conditions, but because they are pretty simple, they are expected to be
517 * fast.
518 */
519bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
520 int order);
521void __memcg_kmem_commit_charge(struct page *page,
522 struct mem_cgroup *memcg, int order);
523void __memcg_kmem_uncharge_pages(struct page *page, int order);
524
Glauber Costa2633d7a2012-12-18 14:22:34 -0800525int memcg_cache_id(struct mem_cgroup *memcg);
Glauber Costa943a4512012-12-18 14:23:03 -0800526int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
527 struct kmem_cache *root_cache);
Glauber Costa2633d7a2012-12-18 14:22:34 -0800528void memcg_release_cache(struct kmem_cache *cachep);
529void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep);
530
Glauber Costa55007d82012-12-18 14:22:38 -0800531int memcg_update_cache_size(struct kmem_cache *s, int num_groups);
532void memcg_update_array_size(int num_groups);
Glauber Costad7f25f82012-12-18 14:22:40 -0800533
534struct kmem_cache *
535__memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
536
Glauber Costa1f458cb2012-12-18 14:22:50 -0800537void mem_cgroup_destroy_cache(struct kmem_cache *cachep);
Glauber Costa7cf27982012-12-18 14:22:55 -0800538void kmem_cache_destroy_memcg_children(struct kmem_cache *s);
Glauber Costa1f458cb2012-12-18 14:22:50 -0800539
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800540/**
541 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
542 * @gfp: the gfp allocation flags.
543 * @memcg: a pointer to the memcg this was charged against.
544 * @order: allocation order.
545 *
546 * returns true if the memcg where the current task belongs can hold this
547 * allocation.
548 *
549 * We return true automatically if this allocation is not to be accounted to
550 * any memcg.
551 */
552static inline bool
553memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
554{
555 if (!memcg_kmem_enabled())
556 return true;
557
558 /*
559 * __GFP_NOFAIL allocations will move on even if charging is not
560 * possible. Therefore we don't even try, and have this allocation
561 * unaccounted. We could in theory charge it with
562 * res_counter_charge_nofail, but we hope those allocations are rare,
563 * and won't be worth the trouble.
564 */
565 if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL))
566 return true;
567 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
568 return true;
569
570 /* If the test is dying, just let it go. */
571 if (unlikely(fatal_signal_pending(current)))
572 return true;
573
574 return __memcg_kmem_newpage_charge(gfp, memcg, order);
575}
576
577/**
578 * memcg_kmem_uncharge_pages: uncharge pages from memcg
579 * @page: pointer to struct page being freed
580 * @order: allocation order.
581 *
582 * there is no need to specify memcg here, since it is embedded in page_cgroup
583 */
584static inline void
585memcg_kmem_uncharge_pages(struct page *page, int order)
586{
587 if (memcg_kmem_enabled())
588 __memcg_kmem_uncharge_pages(page, order);
589}
590
591/**
592 * memcg_kmem_commit_charge: embeds correct memcg in a page
593 * @page: pointer to struct page recently allocated
594 * @memcg: the memcg structure we charged against
595 * @order: allocation order.
596 *
597 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
598 * failure of the allocation. if @page is NULL, this function will revert the
599 * charges. Otherwise, it will commit the memcg given by @memcg to the
600 * corresponding page_cgroup.
601 */
602static inline void
603memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
604{
605 if (memcg_kmem_enabled() && memcg)
606 __memcg_kmem_commit_charge(page, memcg, order);
607}
608
Glauber Costad7f25f82012-12-18 14:22:40 -0800609/**
610 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
611 * @cachep: the original global kmem cache
612 * @gfp: allocation flags.
613 *
614 * This function assumes that the task allocating, which determines the memcg
615 * in the page allocator, belongs to the same cgroup throughout the whole
616 * process. Misacounting can happen if the task calls memcg_kmem_get_cache()
617 * while belonging to a cgroup, and later on changes. This is considered
618 * acceptable, and should only happen upon task migration.
619 *
620 * Before the cache is created by the memcg core, there is also a possible
621 * imbalance: the task belongs to a memcg, but the cache being allocated from
622 * is the global cache, since the child cache is not yet guaranteed to be
623 * ready. This case is also fine, since in this case the GFP_KMEMCG will not be
624 * passed and the page allocator will not attempt any cgroup accounting.
625 */
626static __always_inline struct kmem_cache *
627memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
628{
629 if (!memcg_kmem_enabled())
630 return cachep;
631 if (gfp & __GFP_NOFAIL)
632 return cachep;
633 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
634 return cachep;
635 if (unlikely(fatal_signal_pending(current)))
636 return cachep;
637
638 return __memcg_kmem_get_cache(cachep, gfp);
639}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800640#else
Glauber Costa749c5412012-12-18 14:23:01 -0800641#define for_each_memcg_cache_index(_idx) \
642 for (; NULL; )
643
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800644static inline bool memcg_kmem_enabled(void)
645{
646 return false;
647}
648
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800649static inline bool
650memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
651{
652 return true;
653}
654
655static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
656{
657}
658
659static inline void
660memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
661{
662}
Glauber Costa2633d7a2012-12-18 14:22:34 -0800663
664static inline int memcg_cache_id(struct mem_cgroup *memcg)
665{
666 return -1;
667}
668
Glauber Costa943a4512012-12-18 14:23:03 -0800669static inline int
670memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
671 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -0800672{
673 return 0;
674}
675
676static inline void memcg_release_cache(struct kmem_cache *cachep)
677{
678}
679
680static inline void memcg_cache_list_add(struct mem_cgroup *memcg,
681 struct kmem_cache *s)
682{
683}
Glauber Costad7f25f82012-12-18 14:22:40 -0800684
685static inline struct kmem_cache *
686memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
687{
688 return cachep;
689}
Glauber Costa7cf27982012-12-18 14:22:55 -0800690
691static inline void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
692{
693}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800694#endif /* CONFIG_MEMCG_KMEM */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800695#endif /* _LINUX_MEMCONTROL_H */
696