memcontrol.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. enum mem_cgroup_filter_t {
  52. VISIT, /* visit current node */
  53. SKIP, /* skip the current node and continue traversal */
  54. SKIP_TREE, /* skip the whole subtree and continue traversal */
  55. };
  56. /*
  57. * mem_cgroup_filter_t predicate might instruct mem_cgroup_iter_cond how to
  58. * iterate through the hierarchy tree. Each tree element is checked by the
  59. * predicate before it is returned by the iterator. If a filter returns
  60. * SKIP or SKIP_TREE then the iterator code continues traversal (with the
  61. * next node down the hierarchy or the next node that doesn't belong under the
  62. * memcg's subtree).
  63. */
  64. typedef enum mem_cgroup_filter_t
  65. (*mem_cgroup_iter_filter)(struct mem_cgroup *memcg, struct mem_cgroup *root);
  66. #ifdef CONFIG_MEMCG
  67. /*
  68. * All "charge" functions with gfp_mask should use GFP_KERNEL or
  69. * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
  70. * alloc memory but reclaims memory from all available zones. So, "where I want
  71. * memory from" bits of gfp_mask has no meaning. So any bits of that field is
  72. * available but adding a rule is better. charge functions' gfp_mask should
  73. * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
  74. * codes.
  75. * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
  76. */
  77. extern int mem_cgroup_newpage_charge(struct page *page, struct mm_struct *mm,
  78. gfp_t gfp_mask);
  79. /* for swap handling */
  80. extern int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
  81. struct page *page, gfp_t mask, struct mem_cgroup **memcgp);
  82. extern void mem_cgroup_commit_charge_swapin(struct page *page,
  83. struct mem_cgroup *memcg);
  84. extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg);
  85. extern int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  86. gfp_t gfp_mask);
  87. struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
  88. struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
  89. /* For coalescing uncharge for reducing memcg' overhead*/
  90. extern void mem_cgroup_uncharge_start(void);
  91. extern void mem_cgroup_uncharge_end(void);
  92. extern void mem_cgroup_uncharge_page(struct page *page);
  93. extern void mem_cgroup_uncharge_cache_page(struct page *page);
  94. bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
  95. struct mem_cgroup *memcg);
  96. bool task_in_mem_cgroup(struct task_struct *task,
  97. const struct mem_cgroup *memcg);
  98. extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
  99. extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
  100. extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
  101. extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
  102. extern struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css);
  103. static inline
  104. bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg)
  105. {
  106. struct mem_cgroup *task_memcg;
  107. bool match;
  108. rcu_read_lock();
  109. task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
  110. match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
  111. rcu_read_unlock();
  112. return match;
  113. }
  114. extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
  115. extern void
  116. mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
  117. struct mem_cgroup **memcgp);
  118. extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
  119. struct page *oldpage, struct page *newpage, bool migration_ok);
  120. struct mem_cgroup *mem_cgroup_iter_cond(struct mem_cgroup *root,
  121. struct mem_cgroup *prev,
  122. struct mem_cgroup_reclaim_cookie *reclaim,
  123. mem_cgroup_iter_filter cond);
  124. static inline struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
  125. struct mem_cgroup *prev,
  126. struct mem_cgroup_reclaim_cookie *reclaim)
  127. {
  128. return mem_cgroup_iter_cond(root, prev, reclaim, NULL);
  129. }
  130. void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
  131. /*
  132. * For memory reclaim.
  133. */
  134. int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
  135. int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
  136. unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
  137. void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
  138. extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
  139. struct task_struct *p);
  140. extern void mem_cgroup_replace_page_cache(struct page *oldpage,
  141. struct page *newpage);
  142. /**
  143. * mem_cgroup_toggle_oom - toggle the memcg OOM killer for the current task
  144. * @new: true to enable, false to disable
  145. *
  146. * Toggle whether a failed memcg charge should invoke the OOM killer
  147. * or just return -ENOMEM. Returns the previous toggle state.
  148. *
  149. * NOTE: Any path that enables the OOM killer before charging must
  150. * call mem_cgroup_oom_synchronize() afterward to finalize the
  151. * OOM handling and clean up.
  152. */
  153. static inline bool mem_cgroup_toggle_oom(bool new)
  154. {
  155. bool old;
  156. old = current->memcg_oom.may_oom;
  157. current->memcg_oom.may_oom = new;
  158. return old;
  159. }
  160. static inline void mem_cgroup_enable_oom(void)
  161. {
  162. bool old = mem_cgroup_toggle_oom(true);
  163. WARN_ON(old == true);
  164. }
  165. static inline void mem_cgroup_disable_oom(void)
  166. {
  167. bool old = mem_cgroup_toggle_oom(false);
  168. WARN_ON(old == false);
  169. }
  170. static inline bool task_in_memcg_oom(struct task_struct *p)
  171. {
  172. return p->memcg_oom.in_memcg_oom;
  173. }
  174. bool mem_cgroup_oom_synchronize(void);
  175. #ifdef CONFIG_MEMCG_SWAP
  176. extern int do_swap_account;
  177. #endif
  178. static inline bool mem_cgroup_disabled(void)
  179. {
  180. if (mem_cgroup_subsys.disabled)
  181. return true;
  182. return false;
  183. }
  184. void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
  185. unsigned long *flags);
  186. extern atomic_t memcg_moving;
  187. static inline void mem_cgroup_begin_update_page_stat(struct page *page,
  188. bool *locked, unsigned long *flags)
  189. {
  190. if (mem_cgroup_disabled())
  191. return;
  192. rcu_read_lock();
  193. *locked = false;
  194. if (atomic_read(&memcg_moving))
  195. __mem_cgroup_begin_update_page_stat(page, locked, flags);
  196. }
  197. void __mem_cgroup_end_update_page_stat(struct page *page,
  198. unsigned long *flags);
  199. static inline void mem_cgroup_end_update_page_stat(struct page *page,
  200. bool *locked, unsigned long *flags)
  201. {
  202. if (mem_cgroup_disabled())
  203. return;
  204. if (*locked)
  205. __mem_cgroup_end_update_page_stat(page, flags);
  206. rcu_read_unlock();
  207. }
  208. void mem_cgroup_update_page_stat(struct page *page,
  209. enum mem_cgroup_stat_index idx,
  210. int val);
  211. static inline void mem_cgroup_inc_page_stat(struct page *page,
  212. enum mem_cgroup_stat_index idx)
  213. {
  214. mem_cgroup_update_page_stat(page, idx, 1);
  215. }
  216. static inline void mem_cgroup_dec_page_stat(struct page *page,
  217. enum mem_cgroup_stat_index idx)
  218. {
  219. mem_cgroup_update_page_stat(page, idx, -1);
  220. }
  221. enum mem_cgroup_filter_t
  222. mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
  223. struct mem_cgroup *root);
  224. void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
  225. static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
  226. enum vm_event_item idx)
  227. {
  228. if (mem_cgroup_disabled())
  229. return;
  230. __mem_cgroup_count_vm_event(mm, idx);
  231. }
  232. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  233. void mem_cgroup_split_huge_fixup(struct page *head);
  234. #endif
  235. #ifdef CONFIG_DEBUG_VM
  236. bool mem_cgroup_bad_page_check(struct page *page);
  237. void mem_cgroup_print_bad_page(struct page *page);
  238. #endif
  239. #else /* CONFIG_MEMCG */
  240. struct mem_cgroup;
  241. static inline int mem_cgroup_newpage_charge(struct page *page,
  242. struct mm_struct *mm, gfp_t gfp_mask)
  243. {
  244. return 0;
  245. }
  246. static inline int mem_cgroup_cache_charge(struct page *page,
  247. struct mm_struct *mm, gfp_t gfp_mask)
  248. {
  249. return 0;
  250. }
  251. static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
  252. struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp)
  253. {
  254. return 0;
  255. }
  256. static inline void mem_cgroup_commit_charge_swapin(struct page *page,
  257. struct mem_cgroup *memcg)
  258. {
  259. }
  260. static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
  261. {
  262. }
  263. static inline void mem_cgroup_uncharge_start(void)
  264. {
  265. }
  266. static inline void mem_cgroup_uncharge_end(void)
  267. {
  268. }
  269. static inline void mem_cgroup_uncharge_page(struct page *page)
  270. {
  271. }
  272. static inline void mem_cgroup_uncharge_cache_page(struct page *page)
  273. {
  274. }
  275. static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
  276. struct mem_cgroup *memcg)
  277. {
  278. return &zone->lruvec;
  279. }
  280. static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
  281. struct zone *zone)
  282. {
  283. return &zone->lruvec;
  284. }
  285. static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
  286. {
  287. return NULL;
  288. }
  289. static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
  290. {
  291. return NULL;
  292. }
  293. static inline bool mm_match_cgroup(struct mm_struct *mm,
  294. struct mem_cgroup *memcg)
  295. {
  296. return true;
  297. }
  298. static inline bool task_in_mem_cgroup(struct task_struct *task,
  299. const struct mem_cgroup *memcg)
  300. {
  301. return true;
  302. }
  303. static inline struct cgroup_subsys_state
  304. *mem_cgroup_css(struct mem_cgroup *memcg)
  305. {
  306. return NULL;
  307. }
  308. static inline void
  309. mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
  310. struct mem_cgroup **memcgp)
  311. {
  312. }
  313. static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
  314. struct page *oldpage, struct page *newpage, bool migration_ok)
  315. {
  316. }
  317. static inline struct mem_cgroup *
  318. mem_cgroup_iter_cond(struct mem_cgroup *root,
  319. struct mem_cgroup *prev,
  320. struct mem_cgroup_reclaim_cookie *reclaim,
  321. mem_cgroup_iter_filter cond)
  322. {
  323. /* first call must return non-NULL, second return NULL */
  324. return (struct mem_cgroup *)(unsigned long)!prev;
  325. }
  326. static inline struct mem_cgroup *
  327. mem_cgroup_iter(struct mem_cgroup *root,
  328. struct mem_cgroup *prev,
  329. struct mem_cgroup_reclaim_cookie *reclaim)
  330. {
  331. return NULL;
  332. }
  333. static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
  334. struct mem_cgroup *prev)
  335. {
  336. }
  337. static inline bool mem_cgroup_disabled(void)
  338. {
  339. return true;
  340. }
  341. static inline int
  342. mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
  343. {
  344. return 1;
  345. }
  346. static inline unsigned long
  347. mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
  348. {
  349. return 0;
  350. }
  351. static inline void
  352. mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
  353. int increment)
  354. {
  355. }
  356. static inline void
  357. mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
  358. {
  359. }
  360. static inline void mem_cgroup_begin_update_page_stat(struct page *page,
  361. bool *locked, unsigned long *flags)
  362. {
  363. }
  364. static inline void mem_cgroup_end_update_page_stat(struct page *page,
  365. bool *locked, unsigned long *flags)
  366. {
  367. }
  368. static inline bool mem_cgroup_toggle_oom(bool new)
  369. {
  370. return false;
  371. }
  372. static inline void mem_cgroup_enable_oom(void)
  373. {
  374. }
  375. static inline void mem_cgroup_disable_oom(void)
  376. {
  377. }
  378. static inline bool task_in_memcg_oom(struct task_struct *p)
  379. {
  380. return false;
  381. }
  382. static inline bool mem_cgroup_oom_synchronize(void)
  383. {
  384. return false;
  385. }
  386. static inline void mem_cgroup_inc_page_stat(struct page *page,
  387. enum mem_cgroup_stat_index idx)
  388. {
  389. }
  390. static inline void mem_cgroup_dec_page_stat(struct page *page,
  391. enum mem_cgroup_stat_index idx)
  392. {
  393. }
  394. static inline
  395. enum mem_cgroup_filter_t
  396. mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
  397. struct mem_cgroup *root)
  398. {
  399. return VISIT;
  400. }
  401. static inline void mem_cgroup_split_huge_fixup(struct page *head)
  402. {
  403. }
  404. static inline
  405. void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
  406. {
  407. }
  408. static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
  409. struct page *newpage)
  410. {
  411. }
  412. #endif /* CONFIG_MEMCG */
  413. #if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
  414. static inline bool
  415. mem_cgroup_bad_page_check(struct page *page)
  416. {
  417. return false;
  418. }
  419. static inline void
  420. mem_cgroup_print_bad_page(struct page *page)
  421. {
  422. }
  423. #endif
  424. enum {
  425. UNDER_LIMIT,
  426. SOFT_LIMIT,
  427. OVER_LIMIT,
  428. };
  429. struct sock;
  430. #if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
  431. void sock_update_memcg(struct sock *sk);
  432. void sock_release_memcg(struct sock *sk);
  433. #else
  434. static inline void sock_update_memcg(struct sock *sk)
  435. {
  436. }
  437. static inline void sock_release_memcg(struct sock *sk)
  438. {
  439. }
  440. #endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
  441. #ifdef CONFIG_MEMCG_KMEM
  442. extern struct static_key memcg_kmem_enabled_key;
  443. extern int memcg_limited_groups_array_size;
  444. /*
  445. * Helper macro to loop through all memcg-specific caches. Callers must still
  446. * check if the cache is valid (it is either valid or NULL).
  447. * the slab_mutex must be held when looping through those caches
  448. */
  449. #define for_each_memcg_cache_index(_idx) \
  450. for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++)
  451. static inline bool memcg_kmem_enabled(void)
  452. {
  453. return static_key_false(&memcg_kmem_enabled_key);
  454. }
  455. /*
  456. * In general, we'll do everything in our power to not incur in any overhead
  457. * for non-memcg users for the kmem functions. Not even a function call, if we
  458. * can avoid it.
  459. *
  460. * Therefore, we'll inline all those functions so that in the best case, we'll
  461. * see that kmemcg is off for everybody and proceed quickly. If it is on,
  462. * we'll still do most of the flag checking inline. We check a lot of
  463. * conditions, but because they are pretty simple, they are expected to be
  464. * fast.
  465. */
  466. bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
  467. int order);
  468. void __memcg_kmem_commit_charge(struct page *page,
  469. struct mem_cgroup *memcg, int order);
  470. void __memcg_kmem_uncharge_pages(struct page *page, int order);
  471. int memcg_cache_id(struct mem_cgroup *memcg);
  472. int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
  473. struct kmem_cache *root_cache);
  474. void memcg_release_cache(struct kmem_cache *cachep);
  475. void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep);
  476. int memcg_update_cache_size(struct kmem_cache *s, int num_groups);
  477. void memcg_update_array_size(int num_groups);
  478. struct kmem_cache *
  479. __memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
  480. void mem_cgroup_destroy_cache(struct kmem_cache *cachep);
  481. void kmem_cache_destroy_memcg_children(struct kmem_cache *s);
  482. /**
  483. * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
  484. * @gfp: the gfp allocation flags.
  485. * @memcg: a pointer to the memcg this was charged against.
  486. * @order: allocation order.
  487. *
  488. * returns true if the memcg where the current task belongs can hold this
  489. * allocation.
  490. *
  491. * We return true automatically if this allocation is not to be accounted to
  492. * any memcg.
  493. */
  494. static inline bool
  495. memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
  496. {
  497. if (!memcg_kmem_enabled())
  498. return true;
  499. /*
  500. * __GFP_NOFAIL allocations will move on even if charging is not
  501. * possible. Therefore we don't even try, and have this allocation
  502. * unaccounted. We could in theory charge it with
  503. * res_counter_charge_nofail, but we hope those allocations are rare,
  504. * and won't be worth the trouble.
  505. */
  506. if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL))
  507. return true;
  508. if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
  509. return true;
  510. /* If the test is dying, just let it go. */
  511. if (unlikely(fatal_signal_pending(current)))
  512. return true;
  513. return __memcg_kmem_newpage_charge(gfp, memcg, order);
  514. }
  515. /**
  516. * memcg_kmem_uncharge_pages: uncharge pages from memcg
  517. * @page: pointer to struct page being freed
  518. * @order: allocation order.
  519. *
  520. * there is no need to specify memcg here, since it is embedded in page_cgroup
  521. */
  522. static inline void
  523. memcg_kmem_uncharge_pages(struct page *page, int order)
  524. {
  525. if (memcg_kmem_enabled())
  526. __memcg_kmem_uncharge_pages(page, order);
  527. }
  528. /**
  529. * memcg_kmem_commit_charge: embeds correct memcg in a page
  530. * @page: pointer to struct page recently allocated
  531. * @memcg: the memcg structure we charged against
  532. * @order: allocation order.
  533. *
  534. * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
  535. * failure of the allocation. if @page is NULL, this function will revert the
  536. * charges. Otherwise, it will commit the memcg given by @memcg to the
  537. * corresponding page_cgroup.
  538. */
  539. static inline void
  540. memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
  541. {
  542. if (memcg_kmem_enabled() && memcg)
  543. __memcg_kmem_commit_charge(page, memcg, order);
  544. }
  545. /**
  546. * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
  547. * @cachep: the original global kmem cache
  548. * @gfp: allocation flags.
  549. *
  550. * This function assumes that the task allocating, which determines the memcg
  551. * in the page allocator, belongs to the same cgroup throughout the whole
  552. * process. Misacounting can happen if the task calls memcg_kmem_get_cache()
  553. * while belonging to a cgroup, and later on changes. This is considered
  554. * acceptable, and should only happen upon task migration.
  555. *
  556. * Before the cache is created by the memcg core, there is also a possible
  557. * imbalance: the task belongs to a memcg, but the cache being allocated from
  558. * is the global cache, since the child cache is not yet guaranteed to be
  559. * ready. This case is also fine, since in this case the GFP_KMEMCG will not be
  560. * passed and the page allocator will not attempt any cgroup accounting.
  561. */
  562. static __always_inline struct kmem_cache *
  563. memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
  564. {
  565. if (!memcg_kmem_enabled())
  566. return cachep;
  567. if (gfp & __GFP_NOFAIL)
  568. return cachep;
  569. if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
  570. return cachep;
  571. if (unlikely(fatal_signal_pending(current)))
  572. return cachep;
  573. return __memcg_kmem_get_cache(cachep, gfp);
  574. }
  575. #else
  576. #define for_each_memcg_cache_index(_idx) \
  577. for (; NULL; )
  578. static inline bool memcg_kmem_enabled(void)
  579. {
  580. return false;
  581. }
  582. static inline bool
  583. memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
  584. {
  585. return true;
  586. }
  587. static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
  588. {
  589. }
  590. static inline void
  591. memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
  592. {
  593. }
  594. static inline int memcg_cache_id(struct mem_cgroup *memcg)
  595. {
  596. return -1;
  597. }
  598. static inline int
  599. memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
  600. struct kmem_cache *root_cache)
  601. {
  602. return 0;
  603. }
  604. static inline void memcg_release_cache(struct kmem_cache *cachep)
  605. {
  606. }
  607. static inline void memcg_cache_list_add(struct mem_cgroup *memcg,
  608. struct kmem_cache *s)
  609. {
  610. }
  611. static inline struct kmem_cache *
  612. memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
  613. {
  614. return cachep;
  615. }
  616. static inline void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
  617. {
  618. }
  619. #endif /* CONFIG_MEMCG_KMEM */
  620. #endif /* _LINUX_MEMCONTROL_H */