blob: 1a9a096858e0d253cb13037fa4f0c1e9ef91eafd [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
Johannes Weiner00501b52014-08-08 14:19:20 -070057int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
58 gfp_t gfp_mask, struct mem_cgroup **memcgp);
59void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
60 bool lrucare);
61void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg);
Johannes Weiner925b7672012-01-12 17:18:15 -080062
63struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
Hugh Dickinsfa9add62012-05-29 15:07:09 -070064struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -080065
66/* For coalescing uncharge for reducing memcg' overhead*/
67extern void mem_cgroup_uncharge_start(void);
68extern void mem_cgroup_uncharge_end(void);
69
Balbir Singh3c541e12008-02-07 00:14:41 -080070extern void mem_cgroup_uncharge_page(struct page *page);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -070071extern void mem_cgroup_uncharge_cache_page(struct page *page);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -070072
Johannes Weinerc3ac9a82012-05-29 15:06:25 -070073bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
74 struct mem_cgroup *memcg);
David Rientjesffbdccf2013-07-03 15:01:23 -070075bool task_in_mem_cgroup(struct task_struct *task,
76 const struct mem_cgroup *memcg);
David Rientjes3062fc62008-02-07 00:14:03 -080077
Wu Fengguange42d9d52009-12-16 12:19:59 +010078extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
Balbir Singhcf475ad2008-04-29 01:00:16 -070079extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
80
Glauber Costae1aab162011-12-11 21:47:03 +000081extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
Tejun Heo182446d2013-08-08 20:11:24 -040082extern struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css);
Glauber Costae1aab162011-12-11 21:47:03 +000083
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080084static inline
Johannes Weiner587af302012-10-08 16:34:12 -070085bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg)
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080086{
Johannes Weiner587af302012-10-08 16:34:12 -070087 struct mem_cgroup *task_memcg;
88 bool match;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -070089
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080090 rcu_read_lock();
Johannes Weiner587af302012-10-08 16:34:12 -070091 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
92 match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080093 rcu_read_unlock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -070094 return match;
Lai Jiangshan2e4d4092009-01-07 18:08:07 -080095}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080096
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -070097extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
Wu Fengguangd3242362009-12-16 12:19:59 +010098
Johannes Weiner0030f532012-07-31 16:45:25 -070099extern void
100mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
101 struct mem_cgroup **memcgp);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700102extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800103 struct page *oldpage, struct page *newpage, bool migration_ok);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800104
Andrew Morton694fbc02013-09-24 15:27:37 -0700105struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
106 struct mem_cgroup *,
107 struct mem_cgroup_reclaim_cookie *);
Johannes Weiner56600482012-01-12 17:17:59 -0800108void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
109
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800110/*
111 * For memory reclaim.
112 */
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700113int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
Ying Han889976d2011-05-26 16:25:33 -0700114int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700115unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700116void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
Balbir Singhe2224322009-04-02 16:57:39 -0700117extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
118 struct task_struct *p);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800119extern void mem_cgroup_replace_page_cache(struct page *oldpage,
120 struct page *newpage);
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800121
Johannes Weiner49426422013-10-16 13:46:59 -0700122static inline void mem_cgroup_oom_enable(void)
Johannes Weiner519e5242013-09-12 15:13:42 -0700123{
Johannes Weiner49426422013-10-16 13:46:59 -0700124 WARN_ON(current->memcg_oom.may_oom);
125 current->memcg_oom.may_oom = 1;
Johannes Weiner519e5242013-09-12 15:13:42 -0700126}
127
Johannes Weiner49426422013-10-16 13:46:59 -0700128static inline void mem_cgroup_oom_disable(void)
Johannes Weiner519e5242013-09-12 15:13:42 -0700129{
Johannes Weiner49426422013-10-16 13:46:59 -0700130 WARN_ON(!current->memcg_oom.may_oom);
131 current->memcg_oom.may_oom = 0;
Johannes Weiner519e5242013-09-12 15:13:42 -0700132}
133
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700134static inline bool task_in_memcg_oom(struct task_struct *p)
135{
Johannes Weiner49426422013-10-16 13:46:59 -0700136 return p->memcg_oom.memcg;
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700137}
138
Johannes Weiner49426422013-10-16 13:46:59 -0700139bool mem_cgroup_oom_synchronize(bool wait);
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700140
Andrew Mortonc255a452012-07-31 16:43:02 -0700141#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -0800142extern int do_swap_account;
143#endif
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800144
145static inline bool mem_cgroup_disabled(void)
146{
Tejun Heo073219e2014-02-08 10:36:58 -0500147 if (memory_cgrp_subsys.disabled)
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800148 return true;
149 return false;
150}
151
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700152void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
153 unsigned long *flags);
154
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700155extern atomic_t memcg_moving;
156
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700157static inline void mem_cgroup_begin_update_page_stat(struct page *page,
158 bool *locked, unsigned long *flags)
159{
160 if (mem_cgroup_disabled())
161 return;
162 rcu_read_lock();
163 *locked = false;
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -0700164 if (atomic_read(&memcg_moving))
165 __mem_cgroup_begin_update_page_stat(page, locked, flags);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700166}
167
168void __mem_cgroup_end_update_page_stat(struct page *page,
169 unsigned long *flags);
170static inline void mem_cgroup_end_update_page_stat(struct page *page,
171 bool *locked, unsigned long *flags)
172{
173 if (mem_cgroup_disabled())
174 return;
175 if (*locked)
176 __mem_cgroup_end_update_page_stat(page, flags);
177 rcu_read_unlock();
178}
179
Greg Thelen2a7106f2011-01-13 15:47:37 -0800180void mem_cgroup_update_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700181 enum mem_cgroup_stat_index idx,
Greg Thelen2a7106f2011-01-13 15:47:37 -0800182 int val);
183
184static inline void mem_cgroup_inc_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700185 enum mem_cgroup_stat_index idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -0800186{
187 mem_cgroup_update_page_stat(page, idx, 1);
188}
189
190static inline void mem_cgroup_dec_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700191 enum mem_cgroup_stat_index idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -0800192{
193 mem_cgroup_update_page_stat(page, idx, -1);
194}
195
Andrew Morton0608f432013-09-24 15:27:41 -0700196unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
197 gfp_t gfp_mask,
198 unsigned long *total_scanned);
David Rientjesa63d83f2010-08-09 17:19:46 -0700199
David Rientjes68ae5642012-12-12 13:51:57 -0800200void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
201static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
202 enum vm_event_item idx)
203{
204 if (mem_cgroup_disabled())
205 return;
206 __mem_cgroup_count_vm_event(mm, idx);
207}
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800208#ifdef CONFIG_TRANSPARENT_HUGEPAGE
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800209void mem_cgroup_split_huge_fixup(struct page *head);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800210#endif
211
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700212#ifdef CONFIG_DEBUG_VM
213bool mem_cgroup_bad_page_check(struct page *page);
214void mem_cgroup_print_bad_page(struct page *page);
215#endif
Andrew Mortonc255a452012-07-31 16:43:02 -0700216#else /* CONFIG_MEMCG */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800217struct mem_cgroup;
218
Johannes Weiner00501b52014-08-08 14:19:20 -0700219static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
220 gfp_t gfp_mask,
221 struct mem_cgroup **memcgp)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800222{
Johannes Weiner00501b52014-08-08 14:19:20 -0700223 *memcgp = NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800224 return 0;
225}
226
Johannes Weiner00501b52014-08-08 14:19:20 -0700227static inline void mem_cgroup_commit_charge(struct page *page,
228 struct mem_cgroup *memcg,
229 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800230{
231}
232
Johannes Weiner00501b52014-08-08 14:19:20 -0700233static inline void mem_cgroup_cancel_charge(struct page *page,
234 struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800235{
236}
237
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -0800238static inline void mem_cgroup_uncharge_start(void)
239{
240}
241
242static inline void mem_cgroup_uncharge_end(void)
243{
244}
245
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800246static inline void mem_cgroup_uncharge_page(struct page *page)
247{
248}
249
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700250static inline void mem_cgroup_uncharge_cache_page(struct page *page)
251{
252}
253
Johannes Weiner925b7672012-01-12 17:18:15 -0800254static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
255 struct mem_cgroup *memcg)
256{
257 return &zone->lruvec;
258}
259
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700260static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
261 struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -0700262{
Johannes Weiner925b7672012-01-12 17:18:15 -0800263 return &zone->lruvec;
Balbir Singh66e17072008-02-07 00:13:56 -0800264}
265
Wu Fengguange42d9d52009-12-16 12:19:59 +0100266static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
267{
268 return NULL;
269}
270
Johannes Weiner587af302012-10-08 16:34:12 -0700271static inline bool mm_match_cgroup(struct mm_struct *mm,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700272 struct mem_cgroup *memcg)
Balbir Singhbed71612008-02-07 00:14:01 -0800273{
Johannes Weiner587af302012-10-08 16:34:12 -0700274 return true;
Balbir Singhbed71612008-02-07 00:14:01 -0800275}
276
David Rientjesffbdccf2013-07-03 15:01:23 -0700277static inline bool task_in_mem_cgroup(struct task_struct *task,
278 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -0800279{
David Rientjesffbdccf2013-07-03 15:01:23 -0700280 return true;
David Rientjes4c4a2212008-02-07 00:14:06 -0800281}
282
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700283static inline struct cgroup_subsys_state
284 *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100285{
286 return NULL;
287}
288
Johannes Weiner0030f532012-07-31 16:45:25 -0700289static inline void
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -0700290mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
Johannes Weiner0030f532012-07-31 16:45:25 -0700291 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800292{
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800293}
294
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700295static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800296 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800297{
298}
299
Johannes Weiner56600482012-01-12 17:17:59 -0800300static inline struct mem_cgroup *
301mem_cgroup_iter(struct mem_cgroup *root,
302 struct mem_cgroup *prev,
303 struct mem_cgroup_reclaim_cookie *reclaim)
304{
305 return NULL;
306}
307
308static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
309 struct mem_cgroup *prev)
310{
311}
312
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800313static inline bool mem_cgroup_disabled(void)
314{
315 return true;
316}
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800317
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800318static inline int
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -0700319mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800320{
321 return 1;
322}
323
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800324static inline unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700325mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800326{
327 return 0;
328}
329
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700330static inline void
331mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
332 int increment)
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800333{
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800334}
335
Balbir Singhe2224322009-04-02 16:57:39 -0700336static inline void
337mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
338{
339}
340
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700341static inline void mem_cgroup_begin_update_page_stat(struct page *page,
342 bool *locked, unsigned long *flags)
343{
344}
345
346static inline void mem_cgroup_end_update_page_stat(struct page *page,
347 bool *locked, unsigned long *flags)
348{
349}
350
Johannes Weiner49426422013-10-16 13:46:59 -0700351static inline void mem_cgroup_oom_enable(void)
Johannes Weiner519e5242013-09-12 15:13:42 -0700352{
353}
354
Johannes Weiner49426422013-10-16 13:46:59 -0700355static inline void mem_cgroup_oom_disable(void)
Johannes Weiner519e5242013-09-12 15:13:42 -0700356{
357}
358
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700359static inline bool task_in_memcg_oom(struct task_struct *p)
360{
361 return false;
362}
363
Johannes Weiner49426422013-10-16 13:46:59 -0700364static inline bool mem_cgroup_oom_synchronize(bool wait)
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700365{
366 return false;
367}
368
Greg Thelen2a7106f2011-01-13 15:47:37 -0800369static inline void mem_cgroup_inc_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700370 enum mem_cgroup_stat_index idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -0800371{
372}
373
374static inline void mem_cgroup_dec_page_stat(struct page *page,
Sha Zhengju68b48762013-09-12 15:13:50 -0700375 enum mem_cgroup_stat_index idx)
Balbir Singhd69b0422009-06-17 16:26:34 -0700376{
377}
378
Balbir Singh4e416952009-09-23 15:56:39 -0700379static inline
Andrew Morton0608f432013-09-24 15:27:41 -0700380unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
381 gfp_t gfp_mask,
382 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -0700383{
Andrew Morton0608f432013-09-24 15:27:41 -0700384 return 0;
Balbir Singh4e416952009-09-23 15:56:39 -0700385}
386
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800387static inline void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800388{
389}
390
Ying Han456f9982011-05-26 16:25:38 -0700391static inline
392void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
393{
394}
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -0800395static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
396 struct page *newpage)
397{
398}
Andrew Mortonc255a452012-07-31 16:43:02 -0700399#endif /* CONFIG_MEMCG */
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800400
Andrew Mortonc255a452012-07-31 16:43:02 -0700401#if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -0700402static inline bool
403mem_cgroup_bad_page_check(struct page *page)
404{
405 return false;
406}
407
408static inline void
409mem_cgroup_print_bad_page(struct page *page)
410{
411}
412#endif
413
Glauber Costae1aab162011-12-11 21:47:03 +0000414enum {
415 UNDER_LIMIT,
416 SOFT_LIMIT,
417 OVER_LIMIT,
418};
419
420struct sock;
David Rientjescd590852012-10-10 15:54:08 -0700421#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000422void sock_update_memcg(struct sock *sk);
423void sock_release_memcg(struct sock *sk);
424#else
425static inline void sock_update_memcg(struct sock *sk)
426{
427}
428static inline void sock_release_memcg(struct sock *sk)
429{
430}
David Rientjescd590852012-10-10 15:54:08 -0700431#endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800432
433#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -0800434extern struct static_key memcg_kmem_enabled_key;
Glauber Costa749c5412012-12-18 14:23:01 -0800435
436extern int memcg_limited_groups_array_size;
Glauber Costaebe945c2012-12-18 14:23:10 -0800437
438/*
439 * Helper macro to loop through all memcg-specific caches. Callers must still
440 * check if the cache is valid (it is either valid or NULL).
441 * the slab_mutex must be held when looping through those caches
442 */
Glauber Costa749c5412012-12-18 14:23:01 -0800443#define for_each_memcg_cache_index(_idx) \
Glauber Costa91c777d2013-02-04 14:28:49 -0800444 for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++)
Glauber Costa749c5412012-12-18 14:23:01 -0800445
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800446static inline bool memcg_kmem_enabled(void)
447{
Glauber Costaa8964b92012-12-18 14:22:09 -0800448 return static_key_false(&memcg_kmem_enabled_key);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800449}
450
451/*
452 * In general, we'll do everything in our power to not incur in any overhead
453 * for non-memcg users for the kmem functions. Not even a function call, if we
454 * can avoid it.
455 *
456 * Therefore, we'll inline all those functions so that in the best case, we'll
457 * see that kmemcg is off for everybody and proceed quickly. If it is on,
458 * we'll still do most of the flag checking inline. We check a lot of
459 * conditions, but because they are pretty simple, they are expected to be
460 * fast.
461 */
462bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
463 int order);
464void __memcg_kmem_commit_charge(struct page *page,
465 struct mem_cgroup *memcg, int order);
466void __memcg_kmem_uncharge_pages(struct page *page, int order);
467
Glauber Costa2633d7a2012-12-18 14:22:34 -0800468int memcg_cache_id(struct mem_cgroup *memcg);
Vladimir Davydov5722d092014-04-07 15:39:24 -0700469
Vladimir Davydov363a0442014-01-23 15:52:56 -0800470int memcg_alloc_cache_params(struct mem_cgroup *memcg, struct kmem_cache *s,
471 struct kmem_cache *root_cache);
472void memcg_free_cache_params(struct kmem_cache *s);
Glauber Costa2633d7a2012-12-18 14:22:34 -0800473
Glauber Costa55007d82012-12-18 14:22:38 -0800474int memcg_update_cache_size(struct kmem_cache *s, int num_groups);
475void memcg_update_array_size(int num_groups);
Glauber Costad7f25f82012-12-18 14:22:40 -0800476
477struct kmem_cache *
478__memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
479
Vladimir Davydovc67a8a62014-06-04 16:07:39 -0700480int __memcg_charge_slab(struct kmem_cache *cachep, gfp_t gfp, int order);
481void __memcg_uncharge_slab(struct kmem_cache *cachep, int order);
Vladimir Davydov5dfb4172014-06-04 16:06:38 -0700482
Vladimir Davydov776ed0f2014-06-04 16:10:02 -0700483int __memcg_cleanup_cache_params(struct kmem_cache *s);
Glauber Costa1f458cb2012-12-18 14:22:50 -0800484
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800485/**
486 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
487 * @gfp: the gfp allocation flags.
488 * @memcg: a pointer to the memcg this was charged against.
489 * @order: allocation order.
490 *
491 * returns true if the memcg where the current task belongs can hold this
492 * allocation.
493 *
494 * We return true automatically if this allocation is not to be accounted to
495 * any memcg.
496 */
497static inline bool
498memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
499{
500 if (!memcg_kmem_enabled())
501 return true;
502
503 /*
504 * __GFP_NOFAIL allocations will move on even if charging is not
505 * possible. Therefore we don't even try, and have this allocation
506 * unaccounted. We could in theory charge it with
507 * res_counter_charge_nofail, but we hope those allocations are rare,
508 * and won't be worth the trouble.
509 */
Vladimir Davydov52383432014-06-04 16:06:39 -0700510 if (gfp & __GFP_NOFAIL)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800511 return true;
512 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
513 return true;
514
515 /* If the test is dying, just let it go. */
516 if (unlikely(fatal_signal_pending(current)))
517 return true;
518
519 return __memcg_kmem_newpage_charge(gfp, memcg, order);
520}
521
522/**
523 * memcg_kmem_uncharge_pages: uncharge pages from memcg
524 * @page: pointer to struct page being freed
525 * @order: allocation order.
526 *
527 * there is no need to specify memcg here, since it is embedded in page_cgroup
528 */
529static inline void
530memcg_kmem_uncharge_pages(struct page *page, int order)
531{
532 if (memcg_kmem_enabled())
533 __memcg_kmem_uncharge_pages(page, order);
534}
535
536/**
537 * memcg_kmem_commit_charge: embeds correct memcg in a page
538 * @page: pointer to struct page recently allocated
539 * @memcg: the memcg structure we charged against
540 * @order: allocation order.
541 *
542 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
543 * failure of the allocation. if @page is NULL, this function will revert the
544 * charges. Otherwise, it will commit the memcg given by @memcg to the
545 * corresponding page_cgroup.
546 */
547static inline void
548memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
549{
550 if (memcg_kmem_enabled() && memcg)
551 __memcg_kmem_commit_charge(page, memcg, order);
552}
553
Glauber Costad7f25f82012-12-18 14:22:40 -0800554/**
555 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
556 * @cachep: the original global kmem cache
557 * @gfp: allocation flags.
558 *
Vladimir Davydov5dfb4172014-06-04 16:06:38 -0700559 * All memory allocated from a per-memcg cache is charged to the owner memcg.
Glauber Costad7f25f82012-12-18 14:22:40 -0800560 */
561static __always_inline struct kmem_cache *
562memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
563{
564 if (!memcg_kmem_enabled())
565 return cachep;
566 if (gfp & __GFP_NOFAIL)
567 return cachep;
568 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
569 return cachep;
570 if (unlikely(fatal_signal_pending(current)))
571 return cachep;
572
573 return __memcg_kmem_get_cache(cachep, gfp);
574}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800575#else
Glauber Costa749c5412012-12-18 14:23:01 -0800576#define for_each_memcg_cache_index(_idx) \
577 for (; NULL; )
578
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800579static inline bool memcg_kmem_enabled(void)
580{
581 return false;
582}
583
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800584static inline bool
585memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
586{
587 return true;
588}
589
590static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
591{
592}
593
594static inline void
595memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
596{
597}
Glauber Costa2633d7a2012-12-18 14:22:34 -0800598
599static inline int memcg_cache_id(struct mem_cgroup *memcg)
600{
601 return -1;
602}
603
Vladimir Davydov363a0442014-01-23 15:52:56 -0800604static inline int memcg_alloc_cache_params(struct mem_cgroup *memcg,
605 struct kmem_cache *s, struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -0800606{
607 return 0;
608}
609
Vladimir Davydov363a0442014-01-23 15:52:56 -0800610static inline void memcg_free_cache_params(struct kmem_cache *s)
611{
612}
613
Glauber Costad7f25f82012-12-18 14:22:40 -0800614static inline struct kmem_cache *
615memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
616{
617 return cachep;
618}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -0800619#endif /* CONFIG_MEMCG_KMEM */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800620#endif /* _LINUX_MEMCONTROL_H */
621