blob: afa2ad40457e906782ee15fdf6a155e6cd4293fe [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>
Ying Han456f9982011-05-26 16:25:38 -070025
Pavel Emelianov78fb7462008-02-07 00:13:51 -080026struct mem_cgroup;
27struct page_cgroup;
Balbir Singh8697d332008-02-07 00:13:59 -080028struct page;
29struct mm_struct;
Pavel Emelianov78fb7462008-02-07 00:13:51 -080030
Greg Thelen2a7106f2011-01-13 15:47:37 -080031/* Stats that can be updated by kernel. */
32enum mem_cgroup_page_stat_item {
33 MEMCG_NR_FILE_MAPPED, /* # of pages charged as file rss */
34};
35
Johannes Weiner56600482012-01-12 17:17:59 -080036struct mem_cgroup_reclaim_cookie {
37 struct zone *zone;
38 int priority;
39 unsigned int generation;
40};
41
Andrew Mortonc255a452012-07-31 16:43:02 -070042#ifdef CONFIG_MEMCG
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -080043/*
44 * All "charge" functions with gfp_mask should use GFP_KERNEL or
45 * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
46 * alloc memory but reclaims memory from all available zones. So, "where I want
47 * memory from" bits of gfp_mask has no meaning. So any bits of that field is
48 * available but adding a rule is better. charge functions' gfp_mask should
49 * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
50 * codes.
51 * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
52 */
Pavel Emelianov78fb7462008-02-07 00:13:51 -080053
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080054extern int mem_cgroup_newpage_charge(struct page *page, struct mm_struct *mm,
Balbir Singhe1a1cd52008-02-07 00:14:02 -080055 gfp_t gfp_mask);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080056/* for swap handling */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080057extern int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
Johannes Weiner72835c82012-01-12 17:18:32 -080058 struct page *page, gfp_t mask, struct mem_cgroup **memcgp);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080059extern void mem_cgroup_commit_charge_swapin(struct page *page,
Johannes Weiner72835c82012-01-12 17:18:32 -080060 struct mem_cgroup *memcg);
61extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080062
Hugh Dickins82895462008-03-04 14:29:08 -080063extern int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
64 gfp_t gfp_mask);
Johannes Weiner925b7672012-01-12 17:18:15 -080065
66struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
Hugh Dickinsfa9add62012-05-29 15:07:09 -070067struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -080068
69/* For coalescing uncharge for reducing memcg' overhead*/
70extern void mem_cgroup_uncharge_start(void);
71extern void mem_cgroup_uncharge_end(void);
72
Balbir Singh3c541e12008-02-07 00:14:41 -080073extern void mem_cgroup_uncharge_page(struct page *page);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -070074extern void mem_cgroup_uncharge_cache_page(struct page *page);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -070075
Johannes Weinerc3ac9a82012-05-29 15:06:25 -070076bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
77 struct mem_cgroup *memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -070078int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg);
David Rientjes3062fc62008-02-07 00:14:03 -080079
Wu Fengguange42d9d52009-12-16 12:19:59 +010080extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
Balbir Singhcf475ad2008-04-29 01:00:16 -070081extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -070082extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
Balbir Singhcf475ad2008-04-29 01:00:16 -070083
Glauber Costae1aab162011-12-11 21:47:03 +000084extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +000085extern struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont);
Glauber Costae1aab162011-12-11 21:47:03 +000086
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080087static inline
Johannes Weiner587af302012-10-08 16:34:12 -070088bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg)
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080089{
Johannes Weiner587af302012-10-08 16:34:12 -070090 struct mem_cgroup *task_memcg;
91 bool match;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -070092
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080093 rcu_read_lock();
Johannes Weiner587af302012-10-08 16:34:12 -070094 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
95 match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080096 rcu_read_unlock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -070097 return match;
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080098}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080099
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700100extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
Wu Fengguangd3242362009-12-16 12:19:59 +0100101
Johannes Weiner0030f532012-07-31 16:45:25 -0700102extern void
103mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
104 struct mem_cgroup **memcgp);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700105extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800106 struct page *oldpage, struct page *newpage, bool migration_ok);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800107
Johannes Weiner56600482012-01-12 17:17:59 -0800108struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
109 struct mem_cgroup *,
110 struct mem_cgroup_reclaim_cookie *);
111void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
112
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800113/*
114 * For memory reclaim.
115 */
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700116int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
117int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec);
Ying Han889976d2011-05-26 16:25:33 -0700118int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700119unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700120void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
Balbir Singhe2224322009-04-02 16:57:39 -0700121extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
122 struct task_struct *p);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800123extern void mem_cgroup_replace_page_cache(struct page *oldpage,
124 struct page *newpage);
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800125
Andrew Mortonc255a452012-07-31 16:43:02 -0700126#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -0800127extern int do_swap_account;
128#endif
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800129
130static inline bool mem_cgroup_disabled(void)
131{
132 if (mem_cgroup_subsys.disabled)
133 return true;
134 return false;
135}
136
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700137void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
138 unsigned long *flags);
139
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700140extern atomic_t memcg_moving;
141
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700142static inline void mem_cgroup_begin_update_page_stat(struct page *page,
143 bool *locked, unsigned long *flags)
144{
145 if (mem_cgroup_disabled())
146 return;
147 rcu_read_lock();
148 *locked = false;
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700149 if (atomic_read(&memcg_moving))
150 __mem_cgroup_begin_update_page_stat(page, locked, flags);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700151}
152
153void __mem_cgroup_end_update_page_stat(struct page *page,
154 unsigned long *flags);
155static inline void mem_cgroup_end_update_page_stat(struct page *page,
156 bool *locked, unsigned long *flags)
157{
158 if (mem_cgroup_disabled())
159 return;
160 if (*locked)
161 __mem_cgroup_end_update_page_stat(page, flags);
162 rcu_read_unlock();
163}
164
Greg Thelen2a7106f2011-01-13 15:47:37 -0800165void mem_cgroup_update_page_stat(struct page *page,
166 enum mem_cgroup_page_stat_item idx,
167 int val);
168
169static inline void mem_cgroup_inc_page_stat(struct page *page,
170 enum mem_cgroup_page_stat_item idx)
171{
172 mem_cgroup_update_page_stat(page, idx, 1);
173}
174
175static inline void mem_cgroup_dec_page_stat(struct page *page,
176 enum mem_cgroup_page_stat_item idx)
177{
178 mem_cgroup_update_page_stat(page, idx, -1);
179}
180
Balbir Singh4e416952009-09-23 15:56:39 -0700181unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -0700182 gfp_t gfp_mask,
183 unsigned long *total_scanned);
David Rientjesa63d83f2010-08-09 17:19:46 -0700184
David Rientjes68ae5642012-12-12 13:51:57 -0800185void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
186static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
187 enum vm_event_item idx)
188{
189 if (mem_cgroup_disabled())
190 return;
191 __mem_cgroup_count_vm_event(mm, idx);
192}
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800193#ifdef CONFIG_TRANSPARENT_HUGEPAGE
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800194void mem_cgroup_split_huge_fixup(struct page *head);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800195#endif
196
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700197#ifdef CONFIG_DEBUG_VM
198bool mem_cgroup_bad_page_check(struct page *page);
199void mem_cgroup_print_bad_page(struct page *page);
200#endif
Andrew Mortonc255a452012-07-31 16:43:02 -0700201#else /* CONFIG_MEMCG */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800202struct mem_cgroup;
203
204static inline int mem_cgroup_newpage_charge(struct page *page,
Hugh Dickins82895462008-03-04 14:29:08 -0800205 struct mm_struct *mm, gfp_t gfp_mask)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800206{
207 return 0;
208}
209
Hugh Dickins82895462008-03-04 14:29:08 -0800210static inline int mem_cgroup_cache_charge(struct page *page,
211 struct mm_struct *mm, gfp_t gfp_mask)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800212{
Hugh Dickins82895462008-03-04 14:29:08 -0800213 return 0;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800214}
215
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800216static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
Johannes Weiner72835c82012-01-12 17:18:32 -0800217 struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800218{
219 return 0;
220}
221
222static inline void mem_cgroup_commit_charge_swapin(struct page *page,
Johannes Weiner72835c82012-01-12 17:18:32 -0800223 struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800224{
225}
226
Johannes Weiner72835c82012-01-12 17:18:32 -0800227static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800228{
229}
230
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -0800231static inline void mem_cgroup_uncharge_start(void)
232{
233}
234
235static inline void mem_cgroup_uncharge_end(void)
236{
237}
238
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800239static inline void mem_cgroup_uncharge_page(struct page *page)
240{
241}
242
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700243static inline void mem_cgroup_uncharge_cache_page(struct page *page)
244{
245}
246
Johannes Weiner925b7672012-01-12 17:18:15 -0800247static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
248 struct mem_cgroup *memcg)
249{
250 return &zone->lruvec;
251}
252
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700253static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
254 struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -0700255{
Johannes Weiner925b7672012-01-12 17:18:15 -0800256 return &zone->lruvec;
Balbir Singh66e17072008-02-07 00:13:56 -0800257}
258
Wu Fengguange42d9d52009-12-16 12:19:59 +0100259static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
260{
261 return NULL;
262}
263
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700264static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
265{
266 return NULL;
267}
268
Johannes Weiner587af302012-10-08 16:34:12 -0700269static inline bool mm_match_cgroup(struct mm_struct *mm,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700270 struct mem_cgroup *memcg)
Balbir Singhbed71612008-02-07 00:14:01 -0800271{
Johannes Weiner587af302012-10-08 16:34:12 -0700272 return true;
Balbir Singhbed71612008-02-07 00:14:01 -0800273}
274
David Rientjes4c4a2212008-02-07 00:14:06 -0800275static inline int task_in_mem_cgroup(struct task_struct *task,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700276 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -0800277{
278 return 1;
279}
280
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700281static inline struct cgroup_subsys_state
282 *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100283{
284 return NULL;
285}
286
Johannes Weiner0030f532012-07-31 16:45:25 -0700287static inline void
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -0700288mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
Johannes Weiner0030f532012-07-31 16:45:25 -0700289 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800290{
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800291}
292
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700293static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800294 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800295{
296}
297
Johannes Weiner56600482012-01-12 17:17:59 -0800298static inline struct mem_cgroup *
299mem_cgroup_iter(struct mem_cgroup *root,
300 struct mem_cgroup *prev,
301 struct mem_cgroup_reclaim_cookie *reclaim)
302{
303 return NULL;
304}
305
306static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
307 struct mem_cgroup *prev)
308{
309}
310
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800311static inline bool mem_cgroup_disabled(void)
312{
313 return true;
314}
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800315
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800316static inline int
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700317mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800318{
319 return 1;
320}
321
Rik van Riel56e49d22009-06-16 15:32:28 -0700322static inline int
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700323mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
Rik van Riel56e49d22009-06-16 15:32:28 -0700324{
325 return 1;
326}
327
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800328static inline unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700329mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800330{
331 return 0;
332}
333
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700334static inline void
335mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
336 int increment)
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800337{
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800338}
339
Balbir Singhe2224322009-04-02 16:57:39 -0700340static inline void
341mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
342{
343}
344
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700345static inline void mem_cgroup_begin_update_page_stat(struct page *page,
346 bool *locked, unsigned long *flags)
347{
348}
349
350static inline void mem_cgroup_end_update_page_stat(struct page *page,
351 bool *locked, unsigned long *flags)
352{
353}
354
Greg Thelen2a7106f2011-01-13 15:47:37 -0800355static inline void mem_cgroup_inc_page_stat(struct page *page,
356 enum mem_cgroup_page_stat_item idx)
357{
358}
359
360static inline void mem_cgroup_dec_page_stat(struct page *page,
361 enum mem_cgroup_page_stat_item idx)
Balbir Singhd69b0422009-06-17 16:26:34 -0700362{
363}
364
Balbir Singh4e416952009-09-23 15:56:39 -0700365static inline
366unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -0700367 gfp_t gfp_mask,
368 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -0700369{
370 return 0;
371}
372
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800373static inline void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800374{
375}
376
Ying Han456f9982011-05-26 16:25:38 -0700377static inline
378void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
379{
380}
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800381static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
382 struct page *newpage)
383{
384}
Andrew Mortonc255a452012-07-31 16:43:02 -0700385#endif /* CONFIG_MEMCG */
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800386
Andrew Mortonc255a452012-07-31 16:43:02 -0700387#if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700388static inline bool
389mem_cgroup_bad_page_check(struct page *page)
390{
391 return false;
392}
393
394static inline void
395mem_cgroup_print_bad_page(struct page *page)
396{
397}
398#endif
399
Glauber Costae1aab162011-12-11 21:47:03 +0000400enum {
401 UNDER_LIMIT,
402 SOFT_LIMIT,
403 OVER_LIMIT,
404};
405
406struct sock;
David Rientjescd590852012-10-10 15:54:08 -0700407#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000408void sock_update_memcg(struct sock *sk);
409void sock_release_memcg(struct sock *sk);
410#else
411static inline void sock_update_memcg(struct sock *sk)
412{
413}
414static inline void sock_release_memcg(struct sock *sk)
415{
416}
David Rientjescd590852012-10-10 15:54:08 -0700417#endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800418
419#ifdef CONFIG_MEMCG_KMEM
420static inline bool memcg_kmem_enabled(void)
421{
422 return true;
423}
424
425/*
426 * In general, we'll do everything in our power to not incur in any overhead
427 * for non-memcg users for the kmem functions. Not even a function call, if we
428 * can avoid it.
429 *
430 * Therefore, we'll inline all those functions so that in the best case, we'll
431 * see that kmemcg is off for everybody and proceed quickly. If it is on,
432 * we'll still do most of the flag checking inline. We check a lot of
433 * conditions, but because they are pretty simple, they are expected to be
434 * fast.
435 */
436bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
437 int order);
438void __memcg_kmem_commit_charge(struct page *page,
439 struct mem_cgroup *memcg, int order);
440void __memcg_kmem_uncharge_pages(struct page *page, int order);
441
442/**
443 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
444 * @gfp: the gfp allocation flags.
445 * @memcg: a pointer to the memcg this was charged against.
446 * @order: allocation order.
447 *
448 * returns true if the memcg where the current task belongs can hold this
449 * allocation.
450 *
451 * We return true automatically if this allocation is not to be accounted to
452 * any memcg.
453 */
454static inline bool
455memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
456{
457 if (!memcg_kmem_enabled())
458 return true;
459
460 /*
461 * __GFP_NOFAIL allocations will move on even if charging is not
462 * possible. Therefore we don't even try, and have this allocation
463 * unaccounted. We could in theory charge it with
464 * res_counter_charge_nofail, but we hope those allocations are rare,
465 * and won't be worth the trouble.
466 */
467 if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL))
468 return true;
469 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
470 return true;
471
472 /* If the test is dying, just let it go. */
473 if (unlikely(fatal_signal_pending(current)))
474 return true;
475
476 return __memcg_kmem_newpage_charge(gfp, memcg, order);
477}
478
479/**
480 * memcg_kmem_uncharge_pages: uncharge pages from memcg
481 * @page: pointer to struct page being freed
482 * @order: allocation order.
483 *
484 * there is no need to specify memcg here, since it is embedded in page_cgroup
485 */
486static inline void
487memcg_kmem_uncharge_pages(struct page *page, int order)
488{
489 if (memcg_kmem_enabled())
490 __memcg_kmem_uncharge_pages(page, order);
491}
492
493/**
494 * memcg_kmem_commit_charge: embeds correct memcg in a page
495 * @page: pointer to struct page recently allocated
496 * @memcg: the memcg structure we charged against
497 * @order: allocation order.
498 *
499 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
500 * failure of the allocation. if @page is NULL, this function will revert the
501 * charges. Otherwise, it will commit the memcg given by @memcg to the
502 * corresponding page_cgroup.
503 */
504static inline void
505memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
506{
507 if (memcg_kmem_enabled() && memcg)
508 __memcg_kmem_commit_charge(page, memcg, order);
509}
510
511#else
512static inline bool
513memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
514{
515 return true;
516}
517
518static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
519{
520}
521
522static inline void
523memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
524{
525}
526#endif /* CONFIG_MEMCG_KMEM */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800527#endif /* _LINUX_MEMCONTROL_H */
528