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 = false;
  76. rcu_read_lock();
  77. task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
  78. if (task_memcg)
  79. match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
  80. rcu_read_unlock();
  81. return match;
  82. }
  83. extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
  84. struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
  85. struct mem_cgroup *,
  86. struct mem_cgroup_reclaim_cookie *);
  87. void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
  88. /*
  89. * For memory reclaim.
  90. */
  91. int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
  92. int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
  93. unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
  94. void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
  95. extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
  96. struct task_struct *p);
  97. static inline void mem_cgroup_oom_enable(void)
  98. {
  99. WARN_ON(current->memcg_oom.may_oom);
  100. current->memcg_oom.may_oom = 1;
  101. }
  102. static inline void mem_cgroup_oom_disable(void)
  103. {
  104. WARN_ON(!current->memcg_oom.may_oom);
  105. current->memcg_oom.may_oom = 0;
  106. }
  107. static inline bool task_in_memcg_oom(struct task_struct *p)
  108. {
  109. return p->memcg_oom.memcg;
  110. }
  111. bool mem_cgroup_oom_synchronize(bool wait);
  112. #ifdef CONFIG_MEMCG_SWAP
  113. extern int do_swap_account;
  114. #endif
  115. static inline bool mem_cgroup_disabled(void)
  116. {
  117. if (memory_cgrp_subsys.disabled)
  118. return true;
  119. return false;
  120. }
  121. struct mem_cgroup *mem_cgroup_begin_page_stat(struct page *page, bool *locked,
  122. unsigned long *flags);
  123. void mem_cgroup_end_page_stat(struct mem_cgroup *memcg, bool locked,
  124. unsigned long flags);
  125. void mem_cgroup_update_page_stat(struct mem_cgroup *memcg,
  126. enum mem_cgroup_stat_index idx, int val);
  127. static inline void mem_cgroup_inc_page_stat(struct mem_cgroup *memcg,
  128. enum mem_cgroup_stat_index idx)
  129. {
  130. mem_cgroup_update_page_stat(memcg, idx, 1);
  131. }
  132. static inline void mem_cgroup_dec_page_stat(struct mem_cgroup *memcg,
  133. enum mem_cgroup_stat_index idx)
  134. {
  135. mem_cgroup_update_page_stat(memcg, idx, -1);
  136. }
  137. unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
  138. gfp_t gfp_mask,
  139. unsigned long *total_scanned);
  140. void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
  141. static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
  142. enum vm_event_item idx)
  143. {
  144. if (mem_cgroup_disabled())
  145. return;
  146. __mem_cgroup_count_vm_event(mm, idx);
  147. }
  148. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  149. void mem_cgroup_split_huge_fixup(struct page *head);
  150. #endif
  151. #ifdef CONFIG_DEBUG_VM
  152. bool mem_cgroup_bad_page_check(struct page *page);
  153. void mem_cgroup_print_bad_page(struct page *page);
  154. #endif
  155. #else /* CONFIG_MEMCG */
  156. struct mem_cgroup;
  157. static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
  158. gfp_t gfp_mask,
  159. struct mem_cgroup **memcgp)
  160. {
  161. *memcgp = NULL;
  162. return 0;
  163. }
  164. static inline void mem_cgroup_commit_charge(struct page *page,
  165. struct mem_cgroup *memcg,
  166. bool lrucare)
  167. {
  168. }
  169. static inline void mem_cgroup_cancel_charge(struct page *page,
  170. struct mem_cgroup *memcg)
  171. {
  172. }
  173. static inline void mem_cgroup_uncharge(struct page *page)
  174. {
  175. }
  176. static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
  177. {
  178. }
  179. static inline void mem_cgroup_migrate(struct page *oldpage,
  180. struct page *newpage,
  181. bool lrucare)
  182. {
  183. }
  184. static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
  185. struct mem_cgroup *memcg)
  186. {
  187. return &zone->lruvec;
  188. }
  189. static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
  190. struct zone *zone)
  191. {
  192. return &zone->lruvec;
  193. }
  194. static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
  195. {
  196. return NULL;
  197. }
  198. static inline bool mm_match_cgroup(struct mm_struct *mm,
  199. struct mem_cgroup *memcg)
  200. {
  201. return true;
  202. }
  203. static inline bool task_in_mem_cgroup(struct task_struct *task,
  204. const struct mem_cgroup *memcg)
  205. {
  206. return true;
  207. }
  208. static inline struct cgroup_subsys_state
  209. *mem_cgroup_css(struct mem_cgroup *memcg)
  210. {
  211. return NULL;
  212. }
  213. static inline struct mem_cgroup *
  214. mem_cgroup_iter(struct mem_cgroup *root,
  215. struct mem_cgroup *prev,
  216. struct mem_cgroup_reclaim_cookie *reclaim)
  217. {
  218. return NULL;
  219. }
  220. static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
  221. struct mem_cgroup *prev)
  222. {
  223. }
  224. static inline bool mem_cgroup_disabled(void)
  225. {
  226. return true;
  227. }
  228. static inline int
  229. mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
  230. {
  231. return 1;
  232. }
  233. static inline unsigned long
  234. mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
  235. {
  236. return 0;
  237. }
  238. static inline void
  239. mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
  240. int increment)
  241. {
  242. }
  243. static inline void
  244. mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
  245. {
  246. }
  247. static inline struct mem_cgroup *mem_cgroup_begin_page_stat(struct page *page,
  248. bool *locked, unsigned long *flags)
  249. {
  250. return NULL;
  251. }
  252. static inline void mem_cgroup_end_page_stat(struct mem_cgroup *memcg,
  253. bool locked, unsigned long flags)
  254. {
  255. }
  256. static inline void mem_cgroup_oom_enable(void)
  257. {
  258. }
  259. static inline void mem_cgroup_oom_disable(void)
  260. {
  261. }
  262. static inline bool task_in_memcg_oom(struct task_struct *p)
  263. {
  264. return false;
  265. }
  266. static inline bool mem_cgroup_oom_synchronize(bool wait)
  267. {
  268. return false;
  269. }
  270. static inline void mem_cgroup_inc_page_stat(struct mem_cgroup *memcg,
  271. enum mem_cgroup_stat_index idx)
  272. {
  273. }
  274. static inline void mem_cgroup_dec_page_stat(struct mem_cgroup *memcg,
  275. enum mem_cgroup_stat_index idx)
  276. {
  277. }
  278. static inline
  279. unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
  280. gfp_t gfp_mask,
  281. unsigned long *total_scanned)
  282. {
  283. return 0;
  284. }
  285. static inline void mem_cgroup_split_huge_fixup(struct page *head)
  286. {
  287. }
  288. static inline
  289. void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
  290. {
  291. }
  292. #endif /* CONFIG_MEMCG */
  293. #if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
  294. static inline bool
  295. mem_cgroup_bad_page_check(struct page *page)
  296. {
  297. return false;
  298. }
  299. static inline void
  300. mem_cgroup_print_bad_page(struct page *page)
  301. {
  302. }
  303. #endif
  304. enum {
  305. UNDER_LIMIT,
  306. SOFT_LIMIT,
  307. OVER_LIMIT,
  308. };
  309. struct sock;
  310. #if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
  311. void sock_update_memcg(struct sock *sk);
  312. void sock_release_memcg(struct sock *sk);
  313. #else
  314. static inline void sock_update_memcg(struct sock *sk)
  315. {
  316. }
  317. static inline void sock_release_memcg(struct sock *sk)
  318. {
  319. }
  320. #endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
  321. #ifdef CONFIG_MEMCG_KMEM
  322. extern struct static_key memcg_kmem_enabled_key;
  323. extern int memcg_limited_groups_array_size;
  324. /*
  325. * Helper macro to loop through all memcg-specific caches. Callers must still
  326. * check if the cache is valid (it is either valid or NULL).
  327. * the slab_mutex must be held when looping through those caches
  328. */
  329. #define for_each_memcg_cache_index(_idx) \
  330. for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++)
  331. static inline bool memcg_kmem_enabled(void)
  332. {
  333. return static_key_false(&memcg_kmem_enabled_key);
  334. }
  335. /*
  336. * In general, we'll do everything in our power to not incur in any overhead
  337. * for non-memcg users for the kmem functions. Not even a function call, if we
  338. * can avoid it.
  339. *
  340. * Therefore, we'll inline all those functions so that in the best case, we'll
  341. * see that kmemcg is off for everybody and proceed quickly. If it is on,
  342. * we'll still do most of the flag checking inline. We check a lot of
  343. * conditions, but because they are pretty simple, they are expected to be
  344. * fast.
  345. */
  346. bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
  347. int order);
  348. void __memcg_kmem_commit_charge(struct page *page,
  349. struct mem_cgroup *memcg, int order);
  350. void __memcg_kmem_uncharge_pages(struct page *page, int order);
  351. int memcg_cache_id(struct mem_cgroup *memcg);
  352. void memcg_update_array_size(int num_groups);
  353. struct kmem_cache *
  354. __memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
  355. int __memcg_charge_slab(struct kmem_cache *cachep, gfp_t gfp, int order);
  356. void __memcg_uncharge_slab(struct kmem_cache *cachep, int order);
  357. int __memcg_cleanup_cache_params(struct kmem_cache *s);
  358. /**
  359. * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
  360. * @gfp: the gfp allocation flags.
  361. * @memcg: a pointer to the memcg this was charged against.
  362. * @order: allocation order.
  363. *
  364. * returns true if the memcg where the current task belongs can hold this
  365. * allocation.
  366. *
  367. * We return true automatically if this allocation is not to be accounted to
  368. * any memcg.
  369. */
  370. static inline bool
  371. memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
  372. {
  373. if (!memcg_kmem_enabled())
  374. return true;
  375. /*
  376. * __GFP_NOFAIL allocations will move on even if charging is not
  377. * possible. Therefore we don't even try, and have this allocation
  378. * unaccounted. We could in theory charge it forcibly, but we hope
  379. * those allocations are rare, 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 */