memcontrol.h 14 KB

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