memcontrol.h 14 KB

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