memcontrol.h 15 KB

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