memcontrol.h 15 KB

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