memcontrol.h 15 KB

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