blob: 4b78661c68d0f56a2a09cc0849a763b3379356b5 [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
Andrew Mortonc255a452012-07-31 16:43:02 -070044#ifdef CONFIG_MEMCG
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -080045/*
46 * All "charge" functions with gfp_mask should use GFP_KERNEL or
47 * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
48 * alloc memory but reclaims memory from all available zones. So, "where I want
49 * memory from" bits of gfp_mask has no meaning. So any bits of that field is
50 * available but adding a rule is better. charge functions' gfp_mask should
51 * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
52 * codes.
53 * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
54 */
Pavel Emelianov78fb7462008-02-07 00:13:51 -080055
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080056extern int mem_cgroup_newpage_charge(struct page *page, struct mm_struct *mm,
Balbir Singhe1a1cd52008-02-07 00:14:02 -080057 gfp_t gfp_mask);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080058/* for swap handling */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080059extern int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
Johannes Weiner72835c82012-01-12 17:18:32 -080060 struct page *page, gfp_t mask, struct mem_cgroup **memcgp);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080061extern void mem_cgroup_commit_charge_swapin(struct page *page,
Johannes Weiner72835c82012-01-12 17:18:32 -080062 struct mem_cgroup *memcg);
63extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -080064
Hugh Dickins82895462008-03-04 14:29:08 -080065extern int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
66 gfp_t gfp_mask);
Johannes Weiner925b7672012-01-12 17:18:15 -080067
68struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
Hugh Dickinsfa9add62012-05-29 15:07:09 -070069struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -080070
71/* For coalescing uncharge for reducing memcg' overhead*/
72extern void mem_cgroup_uncharge_start(void);
73extern void mem_cgroup_uncharge_end(void);
74
Balbir Singh3c541e12008-02-07 00:14:41 -080075extern void mem_cgroup_uncharge_page(struct page *page);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -070076extern void mem_cgroup_uncharge_cache_page(struct page *page);
KAMEZAWA Hiroyukic9b0ed512008-07-25 01:47:15 -070077
Johannes Weinerc3ac9a82012-05-29 15:06:25 -070078bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
79 struct mem_cgroup *memcg);
David Rientjesffbdccf2013-07-03 15:01:23 -070080bool task_in_mem_cgroup(struct task_struct *task,
81 const struct mem_cgroup *memcg);
David Rientjes3062fc62008-02-07 00:14:03 -080082
Wu Fengguange42d9d52009-12-16 12:19:59 +010083extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
Balbir Singhcf475ad22008-04-29 01:00:16 -070084extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -070085extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
Balbir Singhcf475ad22008-04-29 01:00:16 -070086
Glauber Costae1aab162011-12-11 21:47:03 +000087extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
Tejun Heo182446d2013-08-08 20:11:24 -040088extern struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css);
Glauber Costae1aab162011-12-11 21:47:03 +000089
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080090static inline
Johannes Weiner587af302012-10-08 16:34:12 -070091bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg)
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080092{
Johannes Weiner587af302012-10-08 16:34:12 -070093 struct mem_cgroup *task_memcg;
94 bool match;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -070095
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080096 rcu_read_lock();
Johannes Weiner587af302012-10-08 16:34:12 -070097 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
98 match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080099 rcu_read_unlock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700100 return match;
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800101}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800102
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700103extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
Wu Fengguangd3242362009-12-16 12:19:59 +0100104
Johannes Weiner0030f532012-07-31 16:45:25 -0700105extern void
106mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
107 struct mem_cgroup **memcgp);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700108extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800109 struct page *oldpage, struct page *newpage, bool migration_ok);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800110
Johannes Weiner56600482012-01-12 17:17:59 -0800111struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
112 struct mem_cgroup *,
113 struct mem_cgroup_reclaim_cookie *);
114void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
115
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800116/*
117 * For memory reclaim.
118 */
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700119int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
Ying Han889976d2011-05-26 16:25:33 -0700120int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700121unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700122void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
Balbir Singhe2224322009-04-02 16:57:39 -0700123extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
124 struct task_struct *p);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800125extern void mem_cgroup_replace_page_cache(struct page *oldpage,
126 struct page *newpage);
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800127
Andrew Mortonc255a452012-07-31 16:43:02 -0700128#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -0800129extern int do_swap_account;
130#endif
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800131
132static inline bool mem_cgroup_disabled(void)
133{
134 if (mem_cgroup_subsys.disabled)
135 return true;
136 return false;
137}
138
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700139void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
140 unsigned long *flags);
141
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700142extern atomic_t memcg_moving;
143
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700144static inline void mem_cgroup_begin_update_page_stat(struct page *page,
145 bool *locked, unsigned long *flags)
146{
147 if (mem_cgroup_disabled())
148 return;
149 rcu_read_lock();
150 *locked = false;
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700151 if (atomic_read(&memcg_moving))
152 __mem_cgroup_begin_update_page_stat(page, locked, flags);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700153}
154
155void __mem_cgroup_end_update_page_stat(struct page *page,
156 unsigned long *flags);
157static inline void mem_cgroup_end_update_page_stat(struct page *page,
158 bool *locked, unsigned long *flags)
159{
160 if (mem_cgroup_disabled())
161 return;
162 if (*locked)
163 __mem_cgroup_end_update_page_stat(page, flags);
164 rcu_read_unlock();
165}
166
Greg Thelen2a7106f2011-01-13 15:47:37 -0800167void mem_cgroup_update_page_stat(struct page *page,
168 enum mem_cgroup_page_stat_item idx,
169 int val);
170
171static inline void mem_cgroup_inc_page_stat(struct page *page,
172 enum mem_cgroup_page_stat_item idx)
173{
174 mem_cgroup_update_page_stat(page, idx, 1);
175}
176
177static inline void mem_cgroup_dec_page_stat(struct page *page,
178 enum mem_cgroup_page_stat_item idx)
179{
180 mem_cgroup_update_page_stat(page, idx, -1);
181}
182
Michal Hocko3b387222013-09-12 15:13:21 -0700183bool mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg);
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 Rientjesffbdccf2013-07-03 15:01:23 -0700275static inline bool task_in_mem_cgroup(struct task_struct *task,
276 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -0800277{
David Rientjesffbdccf2013-07-03 15:01:23 -0700278 return true;
David Rientjes4c4a2212008-02-07 00:14:06 -0800279}
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
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800322static inline unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700323mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800324{
325 return 0;
326}
327
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700328static inline void
329mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
330 int increment)
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800331{
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800332}
333
Balbir Singhe2224322009-04-02 16:57:39 -0700334static inline void
335mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
336{
337}
338
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700339static inline void mem_cgroup_begin_update_page_stat(struct page *page,
340 bool *locked, unsigned long *flags)
341{
342}
343
344static inline void mem_cgroup_end_update_page_stat(struct page *page,
345 bool *locked, unsigned long *flags)
346{
347}
348
Greg Thelen2a7106f2011-01-13 15:47:37 -0800349static inline void mem_cgroup_inc_page_stat(struct page *page,
350 enum mem_cgroup_page_stat_item idx)
351{
352}
353
354static inline void mem_cgroup_dec_page_stat(struct page *page,
355 enum mem_cgroup_page_stat_item idx)
Balbir Singhd69b0422009-06-17 16:26:34 -0700356{
357}
358
Balbir Singh4e416952009-09-23 15:56:39 -0700359static inline
Michal Hocko3b387222013-09-12 15:13:21 -0700360bool mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg)
Balbir Singh4e416952009-09-23 15:56:39 -0700361{
Michal Hocko3b387222013-09-12 15:13:21 -0700362 return false;
Balbir Singh4e416952009-09-23 15:56:39 -0700363}
364
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800365static inline void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800366{
367}
368
Ying Han456f9982011-05-26 16:25:38 -0700369static inline
370void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
371{
372}
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800373static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
374 struct page *newpage)
375{
376}
Andrew Mortonc255a452012-07-31 16:43:02 -0700377#endif /* CONFIG_MEMCG */
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800378
Andrew Mortonc255a452012-07-31 16:43:02 -0700379#if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700380static inline bool
381mem_cgroup_bad_page_check(struct page *page)
382{
383 return false;
384}
385
386static inline void
387mem_cgroup_print_bad_page(struct page *page)
388{
389}
390#endif
391
Glauber Costae1aab162011-12-11 21:47:03 +0000392enum {
393 UNDER_LIMIT,
394 SOFT_LIMIT,
395 OVER_LIMIT,
396};
397
398struct sock;
David Rientjescd590852012-10-10 15:54:08 -0700399#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000400void sock_update_memcg(struct sock *sk);
401void sock_release_memcg(struct sock *sk);
402#else
403static inline void sock_update_memcg(struct sock *sk)
404{
405}
406static inline void sock_release_memcg(struct sock *sk)
407{
408}
David Rientjescd590852012-10-10 15:54:08 -0700409#endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800410
411#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -0800412extern struct static_key memcg_kmem_enabled_key;
Glauber Costa749c5412012-12-18 14:23:01 -0800413
414extern int memcg_limited_groups_array_size;
Glauber Costaebe945c2012-12-18 14:23:10 -0800415
416/*
417 * Helper macro to loop through all memcg-specific caches. Callers must still
418 * check if the cache is valid (it is either valid or NULL).
419 * the slab_mutex must be held when looping through those caches
420 */
Glauber Costa749c5412012-12-18 14:23:01 -0800421#define for_each_memcg_cache_index(_idx) \
Glauber Costa91c777d2013-02-04 14:28:49 -0800422 for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++)
Glauber Costa749c5412012-12-18 14:23:01 -0800423
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800424static inline bool memcg_kmem_enabled(void)
425{
Glauber Costaa8964b92012-12-18 14:22:09 -0800426 return static_key_false(&memcg_kmem_enabled_key);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800427}
428
429/*
430 * In general, we'll do everything in our power to not incur in any overhead
431 * for non-memcg users for the kmem functions. Not even a function call, if we
432 * can avoid it.
433 *
434 * Therefore, we'll inline all those functions so that in the best case, we'll
435 * see that kmemcg is off for everybody and proceed quickly. If it is on,
436 * we'll still do most of the flag checking inline. We check a lot of
437 * conditions, but because they are pretty simple, they are expected to be
438 * fast.
439 */
440bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
441 int order);
442void __memcg_kmem_commit_charge(struct page *page,
443 struct mem_cgroup *memcg, int order);
444void __memcg_kmem_uncharge_pages(struct page *page, int order);
445
Glauber Costa2633d7a2012-12-18 14:22:34 -0800446int memcg_cache_id(struct mem_cgroup *memcg);
Glauber Costa943a4512012-12-18 14:23:03 -0800447int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
448 struct kmem_cache *root_cache);
Glauber Costa2633d7a2012-12-18 14:22:34 -0800449void memcg_release_cache(struct kmem_cache *cachep);
450void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep);
451
Glauber Costa55007d82012-12-18 14:22:38 -0800452int memcg_update_cache_size(struct kmem_cache *s, int num_groups);
453void memcg_update_array_size(int num_groups);
Glauber Costad7f25f82012-12-18 14:22:40 -0800454
455struct kmem_cache *
456__memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
457
Glauber Costa1f458cb2012-12-18 14:22:50 -0800458void mem_cgroup_destroy_cache(struct kmem_cache *cachep);
Glauber Costa7cf27982012-12-18 14:22:55 -0800459void kmem_cache_destroy_memcg_children(struct kmem_cache *s);
Glauber Costa1f458cb2012-12-18 14:22:50 -0800460
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800461/**
462 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
463 * @gfp: the gfp allocation flags.
464 * @memcg: a pointer to the memcg this was charged against.
465 * @order: allocation order.
466 *
467 * returns true if the memcg where the current task belongs can hold this
468 * allocation.
469 *
470 * We return true automatically if this allocation is not to be accounted to
471 * any memcg.
472 */
473static inline bool
474memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
475{
476 if (!memcg_kmem_enabled())
477 return true;
478
479 /*
480 * __GFP_NOFAIL allocations will move on even if charging is not
481 * possible. Therefore we don't even try, and have this allocation
482 * unaccounted. We could in theory charge it with
483 * res_counter_charge_nofail, but we hope those allocations are rare,
484 * and won't be worth the trouble.
485 */
486 if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL))
487 return true;
488 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
489 return true;
490
491 /* If the test is dying, just let it go. */
492 if (unlikely(fatal_signal_pending(current)))
493 return true;
494
495 return __memcg_kmem_newpage_charge(gfp, memcg, order);
496}
497
498/**
499 * memcg_kmem_uncharge_pages: uncharge pages from memcg
500 * @page: pointer to struct page being freed
501 * @order: allocation order.
502 *
503 * there is no need to specify memcg here, since it is embedded in page_cgroup
504 */
505static inline void
506memcg_kmem_uncharge_pages(struct page *page, int order)
507{
508 if (memcg_kmem_enabled())
509 __memcg_kmem_uncharge_pages(page, order);
510}
511
512/**
513 * memcg_kmem_commit_charge: embeds correct memcg in a page
514 * @page: pointer to struct page recently allocated
515 * @memcg: the memcg structure we charged against
516 * @order: allocation order.
517 *
518 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
519 * failure of the allocation. if @page is NULL, this function will revert the
520 * charges. Otherwise, it will commit the memcg given by @memcg to the
521 * corresponding page_cgroup.
522 */
523static inline void
524memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
525{
526 if (memcg_kmem_enabled() && memcg)
527 __memcg_kmem_commit_charge(page, memcg, order);
528}
529
Glauber Costad7f25f82012-12-18 14:22:40 -0800530/**
531 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
532 * @cachep: the original global kmem cache
533 * @gfp: allocation flags.
534 *
535 * This function assumes that the task allocating, which determines the memcg
536 * in the page allocator, belongs to the same cgroup throughout the whole
537 * process. Misacounting can happen if the task calls memcg_kmem_get_cache()
538 * while belonging to a cgroup, and later on changes. This is considered
539 * acceptable, and should only happen upon task migration.
540 *
541 * Before the cache is created by the memcg core, there is also a possible
542 * imbalance: the task belongs to a memcg, but the cache being allocated from
543 * is the global cache, since the child cache is not yet guaranteed to be
544 * ready. This case is also fine, since in this case the GFP_KMEMCG will not be
545 * passed and the page allocator will not attempt any cgroup accounting.
546 */
547static __always_inline struct kmem_cache *
548memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
549{
550 if (!memcg_kmem_enabled())
551 return cachep;
552 if (gfp & __GFP_NOFAIL)
553 return cachep;
554 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
555 return cachep;
556 if (unlikely(fatal_signal_pending(current)))
557 return cachep;
558
559 return __memcg_kmem_get_cache(cachep, gfp);
560}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800561#else
Glauber Costa749c5412012-12-18 14:23:01 -0800562#define for_each_memcg_cache_index(_idx) \
563 for (; NULL; )
564
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800565static inline bool memcg_kmem_enabled(void)
566{
567 return false;
568}
569
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800570static inline bool
571memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
572{
573 return true;
574}
575
576static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
577{
578}
579
580static inline void
581memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
582{
583}
Glauber Costa2633d7a2012-12-18 14:22:34 -0800584
585static inline int memcg_cache_id(struct mem_cgroup *memcg)
586{
587 return -1;
588}
589
Glauber Costa943a4512012-12-18 14:23:03 -0800590static inline int
591memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
592 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -0800593{
594 return 0;
595}
596
597static inline void memcg_release_cache(struct kmem_cache *cachep)
598{
599}
600
601static inline void memcg_cache_list_add(struct mem_cgroup *memcg,
602 struct kmem_cache *s)
603{
604}
Glauber Costad7f25f82012-12-18 14:22:40 -0800605
606static inline struct kmem_cache *
607memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
608{
609 return cachep;
610}
Glauber Costa7cf27982012-12-18 14:22:55 -0800611
612static inline void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
613{
614}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800615#endif /* CONFIG_MEMCG_KMEM */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800616#endif /* _LINUX_MEMCONTROL_H */
617