memcontrol.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* memcontrol.h - Memory Controller
  2. *
  3. * Copyright IBM Corporation, 2007
  4. * Author Balbir Singh <balbir@linux.vnet.ibm.com>
  5. *
  6. * Copyright 2007 OpenVZ SWsoft Inc
  7. * Author: Pavel Emelianov <xemul@openvz.org>
  8. *
  9. * 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. #ifndef _LINUX_MEMCONTROL_H
  20. #define _LINUX_MEMCONTROL_H
  21. #include <linux/cgroup.h>
  22. #include <linux/vm_event_item.h>
  23. struct mem_cgroup;
  24. struct page_cgroup;
  25. struct page;
  26. struct mm_struct;
  27. /* Stats that can be updated by kernel. */
  28. enum mem_cgroup_page_stat_item {
  29. MEMCG_NR_FILE_MAPPED, /* # of pages charged as file rss */
  30. };
  31. struct mem_cgroup_reclaim_cookie {
  32. struct zone *zone;
  33. int priority;
  34. unsigned int generation;
  35. };
  36. #ifdef CONFIG_MEMCG
  37. /*
  38. * All "charge" functions with gfp_mask should use GFP_KERNEL or
  39. * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
  40. * alloc memory but reclaims memory from all available zones. So, "where I want
  41. * memory from" bits of gfp_mask has no meaning. So any bits of that field is
  42. * available but adding a rule is better. charge functions' gfp_mask should
  43. * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
  44. * codes.
  45. * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
  46. */
  47. extern int mem_cgroup_newpage_charge(struct page *page, struct mm_struct *mm,
  48. gfp_t gfp_mask);
  49. /* for swap handling */
  50. extern int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
  51. struct page *page, gfp_t mask, struct mem_cgroup **memcgp);
  52. extern void mem_cgroup_commit_charge_swapin(struct page *page,
  53. struct mem_cgroup *memcg);
  54. extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg);
  55. extern int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  56. gfp_t gfp_mask);
  57. struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
  58. struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
  59. /* For coalescing uncharge for reducing memcg' overhead*/
  60. extern void mem_cgroup_uncharge_start(void);
  61. extern void mem_cgroup_uncharge_end(void);
  62. extern void mem_cgroup_uncharge_page(struct page *page);
  63. extern void mem_cgroup_uncharge_cache_page(struct page *page);
  64. bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
  65. struct mem_cgroup *memcg);
  66. int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg);
  67. extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
  68. extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
  69. extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
  70. extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
  71. extern struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont);
  72. static inline
  73. int mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *cgroup)
  74. {
  75. struct mem_cgroup *memcg;
  76. int match;
  77. rcu_read_lock();
  78. memcg = mem_cgroup_from_task(rcu_dereference((mm)->owner));
  79. match = __mem_cgroup_same_or_subtree(cgroup, memcg);
  80. rcu_read_unlock();
  81. return match;
  82. }
  83. extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
  84. extern int
  85. mem_cgroup_prepare_migration(struct page *page,
  86. struct page *newpage, struct mem_cgroup **memcgp, gfp_t gfp_mask);
  87. extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
  88. struct page *oldpage, struct page *newpage, bool migration_ok);
  89. struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
  90. struct mem_cgroup *,
  91. struct mem_cgroup_reclaim_cookie *);
  92. void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
  93. /*
  94. * For memory reclaim.
  95. */
  96. int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
  97. int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec);
  98. int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
  99. unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
  100. void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
  101. extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
  102. struct task_struct *p);
  103. extern void mem_cgroup_replace_page_cache(struct page *oldpage,
  104. struct page *newpage);
  105. #ifdef CONFIG_MEMCG_SWAP
  106. extern int do_swap_account;
  107. #endif
  108. static inline bool mem_cgroup_disabled(void)
  109. {
  110. if (mem_cgroup_subsys.disabled)
  111. return true;
  112. return false;
  113. }
  114. void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
  115. unsigned long *flags);
  116. extern atomic_t memcg_moving;
  117. static inline void mem_cgroup_begin_update_page_stat(struct page *page,
  118. bool *locked, unsigned long *flags)
  119. {
  120. if (mem_cgroup_disabled())
  121. return;
  122. rcu_read_lock();
  123. *locked = false;
  124. if (atomic_read(&memcg_moving))
  125. __mem_cgroup_begin_update_page_stat(page, locked, flags);
  126. }
  127. void __mem_cgroup_end_update_page_stat(struct page *page,
  128. unsigned long *flags);
  129. static inline void mem_cgroup_end_update_page_stat(struct page *page,
  130. bool *locked, unsigned long *flags)
  131. {
  132. if (mem_cgroup_disabled())
  133. return;
  134. if (*locked)
  135. __mem_cgroup_end_update_page_stat(page, flags);
  136. rcu_read_unlock();
  137. }
  138. void mem_cgroup_update_page_stat(struct page *page,
  139. enum mem_cgroup_page_stat_item idx,
  140. int val);
  141. static inline void mem_cgroup_inc_page_stat(struct page *page,
  142. enum mem_cgroup_page_stat_item idx)
  143. {
  144. mem_cgroup_update_page_stat(page, idx, 1);
  145. }
  146. static inline void mem_cgroup_dec_page_stat(struct page *page,
  147. enum mem_cgroup_page_stat_item idx)
  148. {
  149. mem_cgroup_update_page_stat(page, idx, -1);
  150. }
  151. unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
  152. gfp_t gfp_mask,
  153. unsigned long *total_scanned);
  154. u64 mem_cgroup_get_limit(struct mem_cgroup *memcg);
  155. void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
  156. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  157. void mem_cgroup_split_huge_fixup(struct page *head);
  158. #endif
  159. #ifdef CONFIG_DEBUG_VM
  160. bool mem_cgroup_bad_page_check(struct page *page);
  161. void mem_cgroup_print_bad_page(struct page *page);
  162. #endif
  163. #else /* CONFIG_MEMCG */
  164. struct mem_cgroup;
  165. static inline int mem_cgroup_newpage_charge(struct page *page,
  166. struct mm_struct *mm, gfp_t gfp_mask)
  167. {
  168. return 0;
  169. }
  170. static inline int mem_cgroup_cache_charge(struct page *page,
  171. struct mm_struct *mm, gfp_t gfp_mask)
  172. {
  173. return 0;
  174. }
  175. static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
  176. struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp)
  177. {
  178. return 0;
  179. }
  180. static inline void mem_cgroup_commit_charge_swapin(struct page *page,
  181. struct mem_cgroup *memcg)
  182. {
  183. }
  184. static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
  185. {
  186. }
  187. static inline void mem_cgroup_uncharge_start(void)
  188. {
  189. }
  190. static inline void mem_cgroup_uncharge_end(void)
  191. {
  192. }
  193. static inline void mem_cgroup_uncharge_page(struct page *page)
  194. {
  195. }
  196. static inline void mem_cgroup_uncharge_cache_page(struct page *page)
  197. {
  198. }
  199. static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
  200. struct mem_cgroup *memcg)
  201. {
  202. return &zone->lruvec;
  203. }
  204. static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
  205. struct zone *zone)
  206. {
  207. return &zone->lruvec;
  208. }
  209. static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
  210. {
  211. return NULL;
  212. }
  213. static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
  214. {
  215. return NULL;
  216. }
  217. static inline int mm_match_cgroup(struct mm_struct *mm,
  218. struct mem_cgroup *memcg)
  219. {
  220. return 1;
  221. }
  222. static inline int task_in_mem_cgroup(struct task_struct *task,
  223. const struct mem_cgroup *memcg)
  224. {
  225. return 1;
  226. }
  227. static inline struct cgroup_subsys_state
  228. *mem_cgroup_css(struct mem_cgroup *memcg)
  229. {
  230. return NULL;
  231. }
  232. static inline int
  233. mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
  234. struct mem_cgroup **memcgp, gfp_t gfp_mask)
  235. {
  236. return 0;
  237. }
  238. static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
  239. struct page *oldpage, struct page *newpage, bool migration_ok)
  240. {
  241. }
  242. static inline struct mem_cgroup *
  243. mem_cgroup_iter(struct mem_cgroup *root,
  244. struct mem_cgroup *prev,
  245. struct mem_cgroup_reclaim_cookie *reclaim)
  246. {
  247. return NULL;
  248. }
  249. static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
  250. struct mem_cgroup *prev)
  251. {
  252. }
  253. static inline bool mem_cgroup_disabled(void)
  254. {
  255. return true;
  256. }
  257. static inline int
  258. mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
  259. {
  260. return 1;
  261. }
  262. static inline int
  263. mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
  264. {
  265. return 1;
  266. }
  267. static inline unsigned long
  268. mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
  269. {
  270. return 0;
  271. }
  272. static inline void
  273. mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
  274. int increment)
  275. {
  276. }
  277. static inline void
  278. mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
  279. {
  280. }
  281. static inline void mem_cgroup_begin_update_page_stat(struct page *page,
  282. bool *locked, unsigned long *flags)
  283. {
  284. }
  285. static inline void mem_cgroup_end_update_page_stat(struct page *page,
  286. bool *locked, unsigned long *flags)
  287. {
  288. }
  289. static inline void mem_cgroup_inc_page_stat(struct page *page,
  290. enum mem_cgroup_page_stat_item idx)
  291. {
  292. }
  293. static inline void mem_cgroup_dec_page_stat(struct page *page,
  294. enum mem_cgroup_page_stat_item idx)
  295. {
  296. }
  297. static inline
  298. unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
  299. gfp_t gfp_mask,
  300. unsigned long *total_scanned)
  301. {
  302. return 0;
  303. }
  304. static inline
  305. u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
  306. {
  307. return 0;
  308. }
  309. static inline void mem_cgroup_split_huge_fixup(struct page *head)
  310. {
  311. }
  312. static inline
  313. void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
  314. {
  315. }
  316. static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
  317. struct page *newpage)
  318. {
  319. }
  320. #endif /* CONFIG_MEMCG */
  321. #if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
  322. static inline bool
  323. mem_cgroup_bad_page_check(struct page *page)
  324. {
  325. return false;
  326. }
  327. static inline void
  328. mem_cgroup_print_bad_page(struct page *page)
  329. {
  330. }
  331. #endif
  332. enum {
  333. UNDER_LIMIT,
  334. SOFT_LIMIT,
  335. OVER_LIMIT,
  336. };
  337. struct sock;
  338. #ifdef CONFIG_MEMCG_KMEM
  339. void sock_update_memcg(struct sock *sk);
  340. void sock_release_memcg(struct sock *sk);
  341. #else
  342. static inline void sock_update_memcg(struct sock *sk)
  343. {
  344. }
  345. static inline void sock_release_memcg(struct sock *sk)
  346. {
  347. }
  348. #endif /* CONFIG_MEMCG_KMEM */
  349. #endif /* _LINUX_MEMCONTROL_H */