memcontrol.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. #include <linux/hardirq.h>
  24. #include <linux/jump_label.h>
  25. struct mem_cgroup;
  26. struct page_cgroup;
  27. struct page;
  28. struct mm_struct;
  29. /* Stats that can be updated by kernel. */
  30. enum mem_cgroup_page_stat_item {
  31. MEMCG_NR_FILE_MAPPED, /* # of pages charged as file rss */
  32. };
  33. struct mem_cgroup_reclaim_cookie {
  34. struct zone *zone;
  35. int priority;
  36. unsigned int generation;
  37. };
  38. #ifdef CONFIG_MEMCG
  39. /*
  40. * All "charge" functions with gfp_mask should use GFP_KERNEL or
  41. * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
  42. * alloc memory but reclaims memory from all available zones. So, "where I want
  43. * memory from" bits of gfp_mask has no meaning. So any bits of that field is
  44. * available but adding a rule is better. charge functions' gfp_mask should
  45. * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
  46. * codes.
  47. * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
  48. */
  49. extern int mem_cgroup_newpage_charge(struct page *page, struct mm_struct *mm,
  50. gfp_t gfp_mask);
  51. /* for swap handling */
  52. extern int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
  53. struct page *page, gfp_t mask, struct mem_cgroup **memcgp);
  54. extern void mem_cgroup_commit_charge_swapin(struct page *page,
  55. struct mem_cgroup *memcg);
  56. extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg);
  57. extern int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  58. gfp_t gfp_mask);
  59. struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
  60. struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
  61. /* For coalescing uncharge for reducing memcg' overhead*/
  62. extern void mem_cgroup_uncharge_start(void);
  63. extern void mem_cgroup_uncharge_end(void);
  64. extern void mem_cgroup_uncharge_page(struct page *page);
  65. extern void mem_cgroup_uncharge_cache_page(struct page *page);
  66. bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
  67. struct mem_cgroup *memcg);
  68. int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg);
  69. extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
  70. extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
  71. extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
  72. extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
  73. extern struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont);
  74. static inline
  75. bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg)
  76. {
  77. struct mem_cgroup *task_memcg;
  78. bool match;
  79. rcu_read_lock();
  80. task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
  81. match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
  82. rcu_read_unlock();
  83. return match;
  84. }
  85. extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
  86. extern void
  87. mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
  88. struct mem_cgroup **memcgp);
  89. extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
  90. struct page *oldpage, struct page *newpage, bool migration_ok);
  91. struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
  92. struct mem_cgroup *,
  93. struct mem_cgroup_reclaim_cookie *);
  94. void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
  95. /*
  96. * For memory reclaim.
  97. */
  98. int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
  99. int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec);
  100. int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
  101. unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
  102. void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
  103. extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
  104. struct task_struct *p);
  105. extern void mem_cgroup_replace_page_cache(struct page *oldpage,
  106. struct page *newpage);
  107. #ifdef CONFIG_MEMCG_SWAP
  108. extern int do_swap_account;
  109. #endif
  110. static inline bool mem_cgroup_disabled(void)
  111. {
  112. if (mem_cgroup_subsys.disabled)
  113. return true;
  114. return false;
  115. }
  116. void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
  117. unsigned long *flags);
  118. extern atomic_t memcg_moving;
  119. static inline void mem_cgroup_begin_update_page_stat(struct page *page,
  120. bool *locked, unsigned long *flags)
  121. {
  122. if (mem_cgroup_disabled())
  123. return;
  124. rcu_read_lock();
  125. *locked = false;
  126. if (atomic_read(&memcg_moving))
  127. __mem_cgroup_begin_update_page_stat(page, locked, flags);
  128. }
  129. void __mem_cgroup_end_update_page_stat(struct page *page,
  130. unsigned long *flags);
  131. static inline void mem_cgroup_end_update_page_stat(struct page *page,
  132. bool *locked, unsigned long *flags)
  133. {
  134. if (mem_cgroup_disabled())
  135. return;
  136. if (*locked)
  137. __mem_cgroup_end_update_page_stat(page, flags);
  138. rcu_read_unlock();
  139. }
  140. void mem_cgroup_update_page_stat(struct page *page,
  141. enum mem_cgroup_page_stat_item idx,
  142. int val);
  143. static inline void mem_cgroup_inc_page_stat(struct page *page,
  144. enum mem_cgroup_page_stat_item idx)
  145. {
  146. mem_cgroup_update_page_stat(page, idx, 1);
  147. }
  148. static inline void mem_cgroup_dec_page_stat(struct page *page,
  149. enum mem_cgroup_page_stat_item idx)
  150. {
  151. mem_cgroup_update_page_stat(page, idx, -1);
  152. }
  153. unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
  154. gfp_t gfp_mask,
  155. unsigned long *total_scanned);
  156. void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
  157. static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
  158. enum vm_event_item idx)
  159. {
  160. if (mem_cgroup_disabled())
  161. return;
  162. __mem_cgroup_count_vm_event(mm, idx);
  163. }
  164. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  165. void mem_cgroup_split_huge_fixup(struct page *head);
  166. #endif
  167. #ifdef CONFIG_DEBUG_VM
  168. bool mem_cgroup_bad_page_check(struct page *page);
  169. void mem_cgroup_print_bad_page(struct page *page);
  170. #endif
  171. #else /* CONFIG_MEMCG */
  172. struct mem_cgroup;
  173. static inline int mem_cgroup_newpage_charge(struct page *page,
  174. struct mm_struct *mm, gfp_t gfp_mask)
  175. {
  176. return 0;
  177. }
  178. static inline int mem_cgroup_cache_charge(struct page *page,
  179. struct mm_struct *mm, gfp_t gfp_mask)
  180. {
  181. return 0;
  182. }
  183. static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
  184. struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp)
  185. {
  186. return 0;
  187. }
  188. static inline void mem_cgroup_commit_charge_swapin(struct page *page,
  189. struct mem_cgroup *memcg)
  190. {
  191. }
  192. static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
  193. {
  194. }
  195. static inline void mem_cgroup_uncharge_start(void)
  196. {
  197. }
  198. static inline void mem_cgroup_uncharge_end(void)
  199. {
  200. }
  201. static inline void mem_cgroup_uncharge_page(struct page *page)
  202. {
  203. }
  204. static inline void mem_cgroup_uncharge_cache_page(struct page *page)
  205. {
  206. }
  207. static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
  208. struct mem_cgroup *memcg)
  209. {
  210. return &zone->lruvec;
  211. }
  212. static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
  213. struct zone *zone)
  214. {
  215. return &zone->lruvec;
  216. }
  217. static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
  218. {
  219. return NULL;
  220. }
  221. static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
  222. {
  223. return NULL;
  224. }
  225. static inline bool mm_match_cgroup(struct mm_struct *mm,
  226. struct mem_cgroup *memcg)
  227. {
  228. return true;
  229. }
  230. static inline int task_in_mem_cgroup(struct task_struct *task,
  231. const struct mem_cgroup *memcg)
  232. {
  233. return 1;
  234. }
  235. static inline struct cgroup_subsys_state
  236. *mem_cgroup_css(struct mem_cgroup *memcg)
  237. {
  238. return NULL;
  239. }
  240. static inline void
  241. mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
  242. struct mem_cgroup **memcgp)
  243. {
  244. }
  245. static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
  246. struct page *oldpage, struct page *newpage, bool migration_ok)
  247. {
  248. }
  249. static inline struct mem_cgroup *
  250. mem_cgroup_iter(struct mem_cgroup *root,
  251. struct mem_cgroup *prev,
  252. struct mem_cgroup_reclaim_cookie *reclaim)
  253. {
  254. return NULL;
  255. }
  256. static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
  257. struct mem_cgroup *prev)
  258. {
  259. }
  260. static inline bool mem_cgroup_disabled(void)
  261. {
  262. return true;
  263. }
  264. static inline int
  265. mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
  266. {
  267. return 1;
  268. }
  269. static inline int
  270. mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
  271. {
  272. return 1;
  273. }
  274. static inline unsigned long
  275. mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
  276. {
  277. return 0;
  278. }
  279. static inline void
  280. mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
  281. int increment)
  282. {
  283. }
  284. static inline void
  285. mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
  286. {
  287. }
  288. static inline void mem_cgroup_begin_update_page_stat(struct page *page,
  289. bool *locked, unsigned long *flags)
  290. {
  291. }
  292. static inline void mem_cgroup_end_update_page_stat(struct page *page,
  293. bool *locked, unsigned long *flags)
  294. {
  295. }
  296. static inline void mem_cgroup_inc_page_stat(struct page *page,
  297. enum mem_cgroup_page_stat_item idx)
  298. {
  299. }
  300. static inline void mem_cgroup_dec_page_stat(struct page *page,
  301. enum mem_cgroup_page_stat_item idx)
  302. {
  303. }
  304. static inline
  305. unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
  306. gfp_t gfp_mask,
  307. unsigned long *total_scanned)
  308. {
  309. return 0;
  310. }
  311. static inline void mem_cgroup_split_huge_fixup(struct page *head)
  312. {
  313. }
  314. static inline
  315. void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
  316. {
  317. }
  318. static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
  319. struct page *newpage)
  320. {
  321. }
  322. #endif /* CONFIG_MEMCG */
  323. #if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
  324. static inline bool
  325. mem_cgroup_bad_page_check(struct page *page)
  326. {
  327. return false;
  328. }
  329. static inline void
  330. mem_cgroup_print_bad_page(struct page *page)
  331. {
  332. }
  333. #endif
  334. enum {
  335. UNDER_LIMIT,
  336. SOFT_LIMIT,
  337. OVER_LIMIT,
  338. };
  339. struct sock;
  340. #if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
  341. void sock_update_memcg(struct sock *sk);
  342. void sock_release_memcg(struct sock *sk);
  343. #else
  344. static inline void sock_update_memcg(struct sock *sk)
  345. {
  346. }
  347. static inline void sock_release_memcg(struct sock *sk)
  348. {
  349. }
  350. #endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
  351. #ifdef CONFIG_MEMCG_KMEM
  352. extern struct static_key memcg_kmem_enabled_key;
  353. static inline bool memcg_kmem_enabled(void)
  354. {
  355. return static_key_false(&memcg_kmem_enabled_key);
  356. }
  357. /*
  358. * In general, we'll do everything in our power to not incur in any overhead
  359. * for non-memcg users for the kmem functions. Not even a function call, if we
  360. * can avoid it.
  361. *
  362. * Therefore, we'll inline all those functions so that in the best case, we'll
  363. * see that kmemcg is off for everybody and proceed quickly. If it is on,
  364. * we'll still do most of the flag checking inline. We check a lot of
  365. * conditions, but because they are pretty simple, they are expected to be
  366. * fast.
  367. */
  368. bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
  369. int order);
  370. void __memcg_kmem_commit_charge(struct page *page,
  371. struct mem_cgroup *memcg, int order);
  372. void __memcg_kmem_uncharge_pages(struct page *page, int order);
  373. /**
  374. * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
  375. * @gfp: the gfp allocation flags.
  376. * @memcg: a pointer to the memcg this was charged against.
  377. * @order: allocation order.
  378. *
  379. * returns true if the memcg where the current task belongs can hold this
  380. * allocation.
  381. *
  382. * We return true automatically if this allocation is not to be accounted to
  383. * any memcg.
  384. */
  385. static inline bool
  386. memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
  387. {
  388. if (!memcg_kmem_enabled())
  389. return true;
  390. /*
  391. * __GFP_NOFAIL allocations will move on even if charging is not
  392. * possible. Therefore we don't even try, and have this allocation
  393. * unaccounted. We could in theory charge it with
  394. * res_counter_charge_nofail, but we hope those allocations are rare,
  395. * and won't be worth the trouble.
  396. */
  397. if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL))
  398. return true;
  399. if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
  400. return true;
  401. /* If the test is dying, just let it go. */
  402. if (unlikely(fatal_signal_pending(current)))
  403. return true;
  404. return __memcg_kmem_newpage_charge(gfp, memcg, order);
  405. }
  406. /**
  407. * memcg_kmem_uncharge_pages: uncharge pages from memcg
  408. * @page: pointer to struct page being freed
  409. * @order: allocation order.
  410. *
  411. * there is no need to specify memcg here, since it is embedded in page_cgroup
  412. */
  413. static inline void
  414. memcg_kmem_uncharge_pages(struct page *page, int order)
  415. {
  416. if (memcg_kmem_enabled())
  417. __memcg_kmem_uncharge_pages(page, order);
  418. }
  419. /**
  420. * memcg_kmem_commit_charge: embeds correct memcg in a page
  421. * @page: pointer to struct page recently allocated
  422. * @memcg: the memcg structure we charged against
  423. * @order: allocation order.
  424. *
  425. * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
  426. * failure of the allocation. if @page is NULL, this function will revert the
  427. * charges. Otherwise, it will commit the memcg given by @memcg to the
  428. * corresponding page_cgroup.
  429. */
  430. static inline void
  431. memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
  432. {
  433. if (memcg_kmem_enabled() && memcg)
  434. __memcg_kmem_commit_charge(page, memcg, order);
  435. }
  436. #else
  437. static inline bool
  438. memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
  439. {
  440. return true;
  441. }
  442. static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
  443. {
  444. }
  445. static inline void
  446. memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
  447. {
  448. }
  449. #endif /* CONFIG_MEMCG_KMEM */
  450. #endif /* _LINUX_MEMCONTROL_H */