blob: a3b4812f494f0c74a093fa5693f5e8b07fe6c40f [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 Hiroyukic9b0ed52008-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);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -070080int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg);
David Rientjes3062fc62008-02-07 00:14:03 -080081
Wu Fengguange42d9d52009-12-16 12:19:59 +010082extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
Balbir Singhcf475ad2008-04-29 01:00:16 -070083extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -070084extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
Balbir Singhcf475ad2008-04-29 01:00:16 -070085
Glauber Costae1aab162011-12-11 21:47:03 +000086extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +000087extern struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont);
Glauber Costae1aab162011-12-11 21:47:03 +000088
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080089static inline
Johannes Weiner587af302012-10-08 16:34:12 -070090bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg)
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080091{
Johannes Weiner587af302012-10-08 16:34:12 -070092 struct mem_cgroup *task_memcg;
93 bool match;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -070094
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080095 rcu_read_lock();
Johannes Weiner587af302012-10-08 16:34:12 -070096 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
97 match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080098 rcu_read_unlock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -070099 return match;
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800100}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800101
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700102extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
Wu Fengguangd3242362009-12-16 12:19:59 +0100103
Johannes Weiner0030f532012-07-31 16:45:25 -0700104extern void
105mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
106 struct mem_cgroup **memcgp);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700107extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800108 struct page *oldpage, struct page *newpage, bool migration_ok);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800109
Johannes Weiner56600482012-01-12 17:17:59 -0800110struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
111 struct mem_cgroup *,
112 struct mem_cgroup_reclaim_cookie *);
113void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
114
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800115/*
116 * For memory reclaim.
117 */
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700118int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
Ying Han889976d2011-05-26 16:25:33 -0700119int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700120unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700121void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
Balbir Singhe2224322009-04-02 16:57:39 -0700122extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
123 struct task_struct *p);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800124extern void mem_cgroup_replace_page_cache(struct page *oldpage,
125 struct page *newpage);
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800126
Johannes Weinerf8a51172013-10-16 13:46:59 -0700127static inline void mem_cgroup_oom_enable(void)
Johannes Weiner11f34782013-09-12 15:13:42 -0700128{
Johannes Weinerf8a51172013-10-16 13:46:59 -0700129 WARN_ON(current->memcg_oom.may_oom);
130 current->memcg_oom.may_oom = 1;
Johannes Weiner11f34782013-09-12 15:13:42 -0700131}
132
Johannes Weinerf8a51172013-10-16 13:46:59 -0700133static inline void mem_cgroup_oom_disable(void)
Johannes Weiner11f34782013-09-12 15:13:42 -0700134{
Johannes Weinerf8a51172013-10-16 13:46:59 -0700135 WARN_ON(!current->memcg_oom.may_oom);
136 current->memcg_oom.may_oom = 0;
Johannes Weiner11f34782013-09-12 15:13:42 -0700137}
138
Johannes Weinerf79d6a42013-09-12 15:13:44 -0700139static inline bool task_in_memcg_oom(struct task_struct *p)
140{
Johannes Weinerf8a51172013-10-16 13:46:59 -0700141 return p->memcg_oom.memcg;
Johannes Weinerf79d6a42013-09-12 15:13:44 -0700142}
143
Johannes Weinerf8a51172013-10-16 13:46:59 -0700144bool mem_cgroup_oom_synchronize(bool wait);
Johannes Weinerf79d6a42013-09-12 15:13:44 -0700145
Andrew Mortonc255a452012-07-31 16:43:02 -0700146#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -0800147extern int do_swap_account;
148#endif
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800149
150static inline bool mem_cgroup_disabled(void)
151{
152 if (mem_cgroup_subsys.disabled)
153 return true;
154 return false;
155}
156
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700157void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
158 unsigned long *flags);
159
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700160extern atomic_t memcg_moving;
161
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700162static inline void mem_cgroup_begin_update_page_stat(struct page *page,
163 bool *locked, unsigned long *flags)
164{
165 if (mem_cgroup_disabled())
166 return;
167 rcu_read_lock();
168 *locked = false;
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700169 if (atomic_read(&memcg_moving))
170 __mem_cgroup_begin_update_page_stat(page, locked, flags);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700171}
172
173void __mem_cgroup_end_update_page_stat(struct page *page,
174 unsigned long *flags);
175static inline void mem_cgroup_end_update_page_stat(struct page *page,
176 bool *locked, unsigned long *flags)
177{
178 if (mem_cgroup_disabled())
179 return;
180 if (*locked)
181 __mem_cgroup_end_update_page_stat(page, flags);
182 rcu_read_unlock();
183}
184
Greg Thelen2a7106f2011-01-13 15:47:37 -0800185void mem_cgroup_update_page_stat(struct page *page,
186 enum mem_cgroup_page_stat_item idx,
187 int val);
188
189static inline void mem_cgroup_inc_page_stat(struct page *page,
190 enum mem_cgroup_page_stat_item idx)
191{
192 mem_cgroup_update_page_stat(page, idx, 1);
193}
194
195static inline void mem_cgroup_dec_page_stat(struct page *page,
196 enum mem_cgroup_page_stat_item idx)
197{
198 mem_cgroup_update_page_stat(page, idx, -1);
199}
200
Balbir Singh4e416952009-09-23 15:56:39 -0700201unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -0700202 gfp_t gfp_mask,
203 unsigned long *total_scanned);
David Rientjesa63d83f2010-08-09 17:19:46 -0700204
David Rientjes68ae5642012-12-12 13:51:57 -0800205void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
206static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
207 enum vm_event_item idx)
208{
209 if (mem_cgroup_disabled())
210 return;
211 __mem_cgroup_count_vm_event(mm, idx);
212}
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800213#ifdef CONFIG_TRANSPARENT_HUGEPAGE
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800214void mem_cgroup_split_huge_fixup(struct page *head);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800215#endif
216
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700217#ifdef CONFIG_DEBUG_VM
218bool mem_cgroup_bad_page_check(struct page *page);
219void mem_cgroup_print_bad_page(struct page *page);
220#endif
Andrew Mortonc255a452012-07-31 16:43:02 -0700221#else /* CONFIG_MEMCG */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800222struct mem_cgroup;
223
224static inline int mem_cgroup_newpage_charge(struct page *page,
Hugh Dickins82895462008-03-04 14:29:08 -0800225 struct mm_struct *mm, gfp_t gfp_mask)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800226{
227 return 0;
228}
229
Hugh Dickins82895462008-03-04 14:29:08 -0800230static inline int mem_cgroup_cache_charge(struct page *page,
231 struct mm_struct *mm, gfp_t gfp_mask)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800232{
Hugh Dickins82895462008-03-04 14:29:08 -0800233 return 0;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800234}
235
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800236static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
Johannes Weiner72835c82012-01-12 17:18:32 -0800237 struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800238{
239 return 0;
240}
241
242static inline void mem_cgroup_commit_charge_swapin(struct page *page,
Johannes Weiner72835c82012-01-12 17:18:32 -0800243 struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800244{
245}
246
Johannes Weiner72835c82012-01-12 17:18:32 -0800247static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800248{
249}
250
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -0800251static inline void mem_cgroup_uncharge_start(void)
252{
253}
254
255static inline void mem_cgroup_uncharge_end(void)
256{
257}
258
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800259static inline void mem_cgroup_uncharge_page(struct page *page)
260{
261}
262
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700263static inline void mem_cgroup_uncharge_cache_page(struct page *page)
264{
265}
266
Johannes Weiner925b7672012-01-12 17:18:15 -0800267static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
268 struct mem_cgroup *memcg)
269{
270 return &zone->lruvec;
271}
272
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700273static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
274 struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -0700275{
Johannes Weiner925b7672012-01-12 17:18:15 -0800276 return &zone->lruvec;
Balbir Singh66e17072008-02-07 00:13:56 -0800277}
278
Wu Fengguange42d9d52009-12-16 12:19:59 +0100279static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
280{
281 return NULL;
282}
283
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700284static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
285{
286 return NULL;
287}
288
Johannes Weiner587af302012-10-08 16:34:12 -0700289static inline bool mm_match_cgroup(struct mm_struct *mm,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700290 struct mem_cgroup *memcg)
Balbir Singhbed71612008-02-07 00:14:01 -0800291{
Johannes Weiner587af302012-10-08 16:34:12 -0700292 return true;
Balbir Singhbed71612008-02-07 00:14:01 -0800293}
294
David Rientjes4c4a2212008-02-07 00:14:06 -0800295static inline int task_in_mem_cgroup(struct task_struct *task,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700296 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -0800297{
298 return 1;
299}
300
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700301static inline struct cgroup_subsys_state
302 *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100303{
304 return NULL;
305}
306
Johannes Weiner0030f532012-07-31 16:45:25 -0700307static inline void
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -0700308mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
Johannes Weiner0030f532012-07-31 16:45:25 -0700309 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800310{
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800311}
312
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700313static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800314 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800315{
316}
317
Johannes Weiner56600482012-01-12 17:17:59 -0800318static inline struct mem_cgroup *
319mem_cgroup_iter(struct mem_cgroup *root,
320 struct mem_cgroup *prev,
321 struct mem_cgroup_reclaim_cookie *reclaim)
322{
323 return NULL;
324}
325
326static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
327 struct mem_cgroup *prev)
328{
329}
330
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800331static inline bool mem_cgroup_disabled(void)
332{
333 return true;
334}
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800335
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800336static inline int
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700337mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800338{
339 return 1;
340}
341
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800342static inline unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700343mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800344{
345 return 0;
346}
347
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700348static inline void
349mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
350 int increment)
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800351{
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800352}
353
Balbir Singhe2224322009-04-02 16:57:39 -0700354static inline void
355mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
356{
357}
358
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700359static inline void mem_cgroup_begin_update_page_stat(struct page *page,
360 bool *locked, unsigned long *flags)
361{
362}
363
364static inline void mem_cgroup_end_update_page_stat(struct page *page,
365 bool *locked, unsigned long *flags)
366{
367}
368
Johannes Weinerf8a51172013-10-16 13:46:59 -0700369static inline void mem_cgroup_oom_enable(void)
Johannes Weiner11f34782013-09-12 15:13:42 -0700370{
371}
372
Johannes Weinerf8a51172013-10-16 13:46:59 -0700373static inline void mem_cgroup_oom_disable(void)
Johannes Weiner11f34782013-09-12 15:13:42 -0700374{
375}
376
Johannes Weinerf79d6a42013-09-12 15:13:44 -0700377static inline bool task_in_memcg_oom(struct task_struct *p)
378{
379 return false;
380}
381
Johannes Weinerf8a51172013-10-16 13:46:59 -0700382static inline bool mem_cgroup_oom_synchronize(bool wait)
Johannes Weinerf79d6a42013-09-12 15:13:44 -0700383{
384 return false;
385}
386
Greg Thelen2a7106f2011-01-13 15:47:37 -0800387static inline void mem_cgroup_inc_page_stat(struct page *page,
388 enum mem_cgroup_page_stat_item idx)
389{
390}
391
392static inline void mem_cgroup_dec_page_stat(struct page *page,
393 enum mem_cgroup_page_stat_item idx)
Balbir Singhd69b0422009-06-17 16:26:34 -0700394{
395}
396
Balbir Singh4e416952009-09-23 15:56:39 -0700397static inline
398unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -0700399 gfp_t gfp_mask,
400 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -0700401{
402 return 0;
403}
404
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800405static inline void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800406{
407}
408
Ying Han456f9982011-05-26 16:25:38 -0700409static inline
410void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
411{
412}
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800413static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
414 struct page *newpage)
415{
416}
Andrew Mortonc255a452012-07-31 16:43:02 -0700417#endif /* CONFIG_MEMCG */
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800418
Andrew Mortonc255a452012-07-31 16:43:02 -0700419#if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700420static inline bool
421mem_cgroup_bad_page_check(struct page *page)
422{
423 return false;
424}
425
426static inline void
427mem_cgroup_print_bad_page(struct page *page)
428{
429}
430#endif
431
Glauber Costae1aab162011-12-11 21:47:03 +0000432enum {
433 UNDER_LIMIT,
434 SOFT_LIMIT,
435 OVER_LIMIT,
436};
437
438struct sock;
David Rientjescd590852012-10-10 15:54:08 -0700439#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000440void sock_update_memcg(struct sock *sk);
441void sock_release_memcg(struct sock *sk);
442#else
443static inline void sock_update_memcg(struct sock *sk)
444{
445}
446static inline void sock_release_memcg(struct sock *sk)
447{
448}
David Rientjescd590852012-10-10 15:54:08 -0700449#endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800450
451#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -0800452extern struct static_key memcg_kmem_enabled_key;
Glauber Costa749c5412012-12-18 14:23:01 -0800453
454extern int memcg_limited_groups_array_size;
Glauber Costaebe945c2012-12-18 14:23:10 -0800455
456/*
457 * Helper macro to loop through all memcg-specific caches. Callers must still
458 * check if the cache is valid (it is either valid or NULL).
459 * the slab_mutex must be held when looping through those caches
460 */
Glauber Costa749c5412012-12-18 14:23:01 -0800461#define for_each_memcg_cache_index(_idx) \
Glauber Costa91c777d2013-02-04 14:28:49 -0800462 for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++)
Glauber Costa749c5412012-12-18 14:23:01 -0800463
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800464static inline bool memcg_kmem_enabled(void)
465{
Glauber Costaa8964b92012-12-18 14:22:09 -0800466 return static_key_false(&memcg_kmem_enabled_key);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800467}
468
469/*
470 * In general, we'll do everything in our power to not incur in any overhead
471 * for non-memcg users for the kmem functions. Not even a function call, if we
472 * can avoid it.
473 *
474 * Therefore, we'll inline all those functions so that in the best case, we'll
475 * see that kmemcg is off for everybody and proceed quickly. If it is on,
476 * we'll still do most of the flag checking inline. We check a lot of
477 * conditions, but because they are pretty simple, they are expected to be
478 * fast.
479 */
480bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
481 int order);
482void __memcg_kmem_commit_charge(struct page *page,
483 struct mem_cgroup *memcg, int order);
484void __memcg_kmem_uncharge_pages(struct page *page, int order);
485
Glauber Costa2633d7a2012-12-18 14:22:34 -0800486int memcg_cache_id(struct mem_cgroup *memcg);
Glauber Costa943a4512012-12-18 14:23:03 -0800487int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
488 struct kmem_cache *root_cache);
Glauber Costa2633d7a2012-12-18 14:22:34 -0800489void memcg_release_cache(struct kmem_cache *cachep);
490void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep);
491
Glauber Costa55007d82012-12-18 14:22:38 -0800492int memcg_update_cache_size(struct kmem_cache *s, int num_groups);
493void memcg_update_array_size(int num_groups);
Glauber Costad7f25f82012-12-18 14:22:40 -0800494
495struct kmem_cache *
496__memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
497
Glauber Costa1f458cb2012-12-18 14:22:50 -0800498void mem_cgroup_destroy_cache(struct kmem_cache *cachep);
Glauber Costa7cf27982012-12-18 14:22:55 -0800499void kmem_cache_destroy_memcg_children(struct kmem_cache *s);
Glauber Costa1f458cb2012-12-18 14:22:50 -0800500
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800501/**
502 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
503 * @gfp: the gfp allocation flags.
504 * @memcg: a pointer to the memcg this was charged against.
505 * @order: allocation order.
506 *
507 * returns true if the memcg where the current task belongs can hold this
508 * allocation.
509 *
510 * We return true automatically if this allocation is not to be accounted to
511 * any memcg.
512 */
513static inline bool
514memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
515{
516 if (!memcg_kmem_enabled())
517 return true;
518
519 /*
520 * __GFP_NOFAIL allocations will move on even if charging is not
521 * possible. Therefore we don't even try, and have this allocation
522 * unaccounted. We could in theory charge it with
523 * res_counter_charge_nofail, but we hope those allocations are rare,
524 * and won't be worth the trouble.
525 */
526 if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL))
527 return true;
528 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
529 return true;
530
531 /* If the test is dying, just let it go. */
532 if (unlikely(fatal_signal_pending(current)))
533 return true;
534
535 return __memcg_kmem_newpage_charge(gfp, memcg, order);
536}
537
538/**
539 * memcg_kmem_uncharge_pages: uncharge pages from memcg
540 * @page: pointer to struct page being freed
541 * @order: allocation order.
542 *
543 * there is no need to specify memcg here, since it is embedded in page_cgroup
544 */
545static inline void
546memcg_kmem_uncharge_pages(struct page *page, int order)
547{
548 if (memcg_kmem_enabled())
549 __memcg_kmem_uncharge_pages(page, order);
550}
551
552/**
553 * memcg_kmem_commit_charge: embeds correct memcg in a page
554 * @page: pointer to struct page recently allocated
555 * @memcg: the memcg structure we charged against
556 * @order: allocation order.
557 *
558 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
559 * failure of the allocation. if @page is NULL, this function will revert the
560 * charges. Otherwise, it will commit the memcg given by @memcg to the
561 * corresponding page_cgroup.
562 */
563static inline void
564memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
565{
566 if (memcg_kmem_enabled() && memcg)
567 __memcg_kmem_commit_charge(page, memcg, order);
568}
569
Glauber Costad7f25f82012-12-18 14:22:40 -0800570/**
571 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
572 * @cachep: the original global kmem cache
573 * @gfp: allocation flags.
574 *
575 * This function assumes that the task allocating, which determines the memcg
576 * in the page allocator, belongs to the same cgroup throughout the whole
577 * process. Misacounting can happen if the task calls memcg_kmem_get_cache()
578 * while belonging to a cgroup, and later on changes. This is considered
579 * acceptable, and should only happen upon task migration.
580 *
581 * Before the cache is created by the memcg core, there is also a possible
582 * imbalance: the task belongs to a memcg, but the cache being allocated from
583 * is the global cache, since the child cache is not yet guaranteed to be
584 * ready. This case is also fine, since in this case the GFP_KMEMCG will not be
585 * passed and the page allocator will not attempt any cgroup accounting.
586 */
587static __always_inline struct kmem_cache *
588memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
589{
590 if (!memcg_kmem_enabled())
591 return cachep;
592 if (gfp & __GFP_NOFAIL)
593 return cachep;
594 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
595 return cachep;
596 if (unlikely(fatal_signal_pending(current)))
597 return cachep;
598
599 return __memcg_kmem_get_cache(cachep, gfp);
600}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800601#else
Glauber Costa749c5412012-12-18 14:23:01 -0800602#define for_each_memcg_cache_index(_idx) \
603 for (; NULL; )
604
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800605static inline bool memcg_kmem_enabled(void)
606{
607 return false;
608}
609
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800610static inline bool
611memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
612{
613 return true;
614}
615
616static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
617{
618}
619
620static inline void
621memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
622{
623}
Glauber Costa2633d7a2012-12-18 14:22:34 -0800624
625static inline int memcg_cache_id(struct mem_cgroup *memcg)
626{
627 return -1;
628}
629
Glauber Costa943a4512012-12-18 14:23:03 -0800630static inline int
631memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
632 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -0800633{
634 return 0;
635}
636
637static inline void memcg_release_cache(struct kmem_cache *cachep)
638{
639}
640
641static inline void memcg_cache_list_add(struct mem_cgroup *memcg,
642 struct kmem_cache *s)
643{
644}
Glauber Costad7f25f82012-12-18 14:22:40 -0800645
646static inline struct kmem_cache *
647memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
648{
649 return cachep;
650}
Glauber Costa7cf27982012-12-18 14:22:55 -0800651
652static inline void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
653{
654}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800655#endif /* CONFIG_MEMCG_KMEM */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800656#endif /* _LINUX_MEMCONTROL_H */
657