memcontrol.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. #include <linux/page_counter.h>
  26. #include <linux/vmpressure.h>
  27. #include <linux/eventfd.h>
  28. #include <linux/mmzone.h>
  29. #include <linux/writeback.h>
  30. #include <linux/page-flags.h>
  31. struct mem_cgroup;
  32. struct page;
  33. struct mm_struct;
  34. struct kmem_cache;
  35. /*
  36. * The corresponding mem_cgroup_stat_names is defined in mm/memcontrol.c,
  37. * These two lists should keep in accord with each other.
  38. */
  39. enum mem_cgroup_stat_index {
  40. /*
  41. * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
  42. */
  43. MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
  44. MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
  45. MEM_CGROUP_STAT_RSS_HUGE, /* # of pages charged as anon huge */
  46. MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
  47. MEM_CGROUP_STAT_DIRTY, /* # of dirty pages in page cache */
  48. MEM_CGROUP_STAT_WRITEBACK, /* # of pages under writeback */
  49. MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */
  50. MEM_CGROUP_STAT_NSTATS,
  51. /* default hierarchy stats */
  52. MEMCG_SOCK = MEM_CGROUP_STAT_NSTATS,
  53. MEMCG_NR_STAT,
  54. };
  55. struct mem_cgroup_reclaim_cookie {
  56. struct zone *zone;
  57. int priority;
  58. unsigned int generation;
  59. };
  60. enum mem_cgroup_events_index {
  61. MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
  62. MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
  63. MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
  64. MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
  65. MEM_CGROUP_EVENTS_NSTATS,
  66. /* default hierarchy events */
  67. MEMCG_LOW = MEM_CGROUP_EVENTS_NSTATS,
  68. MEMCG_HIGH,
  69. MEMCG_MAX,
  70. MEMCG_OOM,
  71. MEMCG_NR_EVENTS,
  72. };
  73. /*
  74. * Per memcg event counter is incremented at every pagein/pageout. With THP,
  75. * it will be incremated by the number of pages. This counter is used for
  76. * for trigger some periodic events. This is straightforward and better
  77. * than using jiffies etc. to handle periodic memcg event.
  78. */
  79. enum mem_cgroup_events_target {
  80. MEM_CGROUP_TARGET_THRESH,
  81. MEM_CGROUP_TARGET_SOFTLIMIT,
  82. MEM_CGROUP_TARGET_NUMAINFO,
  83. MEM_CGROUP_NTARGETS,
  84. };
  85. #ifdef CONFIG_MEMCG
  86. #define MEM_CGROUP_ID_SHIFT 16
  87. #define MEM_CGROUP_ID_MAX USHRT_MAX
  88. struct mem_cgroup_stat_cpu {
  89. long count[MEMCG_NR_STAT];
  90. unsigned long events[MEMCG_NR_EVENTS];
  91. unsigned long nr_page_events;
  92. unsigned long targets[MEM_CGROUP_NTARGETS];
  93. };
  94. struct mem_cgroup_reclaim_iter {
  95. struct mem_cgroup *position;
  96. /* scan generation, increased every round-trip */
  97. unsigned int generation;
  98. };
  99. /*
  100. * per-zone information in memory controller.
  101. */
  102. struct mem_cgroup_per_zone {
  103. struct lruvec lruvec;
  104. unsigned long lru_size[NR_LRU_LISTS];
  105. struct mem_cgroup_reclaim_iter iter[DEF_PRIORITY + 1];
  106. struct rb_node tree_node; /* RB tree node */
  107. unsigned long usage_in_excess;/* Set to the value by which */
  108. /* the soft limit is exceeded*/
  109. bool on_tree;
  110. struct mem_cgroup *memcg; /* Back pointer, we cannot */
  111. /* use container_of */
  112. };
  113. struct mem_cgroup_per_node {
  114. struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
  115. };
  116. struct mem_cgroup_threshold {
  117. struct eventfd_ctx *eventfd;
  118. unsigned long threshold;
  119. };
  120. /* For threshold */
  121. struct mem_cgroup_threshold_ary {
  122. /* An array index points to threshold just below or equal to usage. */
  123. int current_threshold;
  124. /* Size of entries[] */
  125. unsigned int size;
  126. /* Array of thresholds */
  127. struct mem_cgroup_threshold entries[0];
  128. };
  129. struct mem_cgroup_thresholds {
  130. /* Primary thresholds array */
  131. struct mem_cgroup_threshold_ary *primary;
  132. /*
  133. * Spare threshold array.
  134. * This is needed to make mem_cgroup_unregister_event() "never fail".
  135. * It must be able to store at least primary->size - 1 entries.
  136. */
  137. struct mem_cgroup_threshold_ary *spare;
  138. };
  139. enum memcg_kmem_state {
  140. KMEM_NONE,
  141. KMEM_ALLOCATED,
  142. KMEM_ONLINE,
  143. };
  144. /*
  145. * The memory controller data structure. The memory controller controls both
  146. * page cache and RSS per cgroup. We would eventually like to provide
  147. * statistics based on the statistics developed by Rik Van Riel for clock-pro,
  148. * to help the administrator determine what knobs to tune.
  149. */
  150. struct mem_cgroup {
  151. struct cgroup_subsys_state css;
  152. /* Accounted resources */
  153. struct page_counter memory;
  154. struct page_counter swap;
  155. /* Legacy consumer-oriented counters */
  156. struct page_counter memsw;
  157. struct page_counter kmem;
  158. struct page_counter tcpmem;
  159. /* Normal memory consumption range */
  160. unsigned long low;
  161. unsigned long high;
  162. /* Range enforcement for interrupt charges */
  163. struct work_struct high_work;
  164. unsigned long soft_limit;
  165. /* vmpressure notifications */
  166. struct vmpressure vmpressure;
  167. /*
  168. * Should the accounting and control be hierarchical, per subtree?
  169. */
  170. bool use_hierarchy;
  171. /* protected by memcg_oom_lock */
  172. bool oom_lock;
  173. int under_oom;
  174. int swappiness;
  175. /* OOM-Killer disable */
  176. int oom_kill_disable;
  177. /* handle for "memory.events" */
  178. struct cgroup_file events_file;
  179. /* protect arrays of thresholds */
  180. struct mutex thresholds_lock;
  181. /* thresholds for memory usage. RCU-protected */
  182. struct mem_cgroup_thresholds thresholds;
  183. /* thresholds for mem+swap usage. RCU-protected */
  184. struct mem_cgroup_thresholds memsw_thresholds;
  185. /* For oom notifier event fd */
  186. struct list_head oom_notify;
  187. /*
  188. * Should we move charges of a task when a task is moved into this
  189. * mem_cgroup ? And what type of charges should we move ?
  190. */
  191. unsigned long move_charge_at_immigrate;
  192. /*
  193. * set > 0 if pages under this cgroup are moving to other cgroup.
  194. */
  195. atomic_t moving_account;
  196. /* taken only while moving_account > 0 */
  197. spinlock_t move_lock;
  198. struct task_struct *move_lock_task;
  199. unsigned long move_lock_flags;
  200. /*
  201. * percpu counter.
  202. */
  203. struct mem_cgroup_stat_cpu __percpu *stat;
  204. unsigned long socket_pressure;
  205. /* Legacy tcp memory accounting */
  206. bool tcpmem_active;
  207. int tcpmem_pressure;
  208. #ifndef CONFIG_SLOB
  209. /* Index in the kmem_cache->memcg_params.memcg_caches array */
  210. int kmemcg_id;
  211. enum memcg_kmem_state kmem_state;
  212. #endif
  213. int last_scanned_node;
  214. #if MAX_NUMNODES > 1
  215. nodemask_t scan_nodes;
  216. atomic_t numainfo_events;
  217. atomic_t numainfo_updating;
  218. #endif
  219. #ifdef CONFIG_CGROUP_WRITEBACK
  220. struct list_head cgwb_list;
  221. struct wb_domain cgwb_domain;
  222. #endif
  223. /* List of events which userspace want to receive */
  224. struct list_head event_list;
  225. spinlock_t event_list_lock;
  226. struct mem_cgroup_per_node *nodeinfo[0];
  227. /* WARNING: nodeinfo must be the last member here */
  228. };
  229. extern struct mem_cgroup *root_mem_cgroup;
  230. static inline bool mem_cgroup_disabled(void)
  231. {
  232. return !cgroup_subsys_enabled(memory_cgrp_subsys);
  233. }
  234. /**
  235. * mem_cgroup_events - count memory events against a cgroup
  236. * @memcg: the memory cgroup
  237. * @idx: the event index
  238. * @nr: the number of events to account for
  239. */
  240. static inline void mem_cgroup_events(struct mem_cgroup *memcg,
  241. enum mem_cgroup_events_index idx,
  242. unsigned int nr)
  243. {
  244. this_cpu_add(memcg->stat->events[idx], nr);
  245. cgroup_file_notify(&memcg->events_file);
  246. }
  247. bool mem_cgroup_low(struct mem_cgroup *root, struct mem_cgroup *memcg);
  248. int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
  249. gfp_t gfp_mask, struct mem_cgroup **memcgp,
  250. bool compound);
  251. void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
  252. bool lrucare, bool compound);
  253. void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg,
  254. bool compound);
  255. void mem_cgroup_uncharge(struct page *page);
  256. void mem_cgroup_uncharge_list(struct list_head *page_list);
  257. void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
  258. struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
  259. struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
  260. bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg);
  261. struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
  262. static inline
  263. struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
  264. return css ? container_of(css, struct mem_cgroup, css) : NULL;
  265. }
  266. #define mem_cgroup_from_counter(counter, member) \
  267. container_of(counter, struct mem_cgroup, member)
  268. struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
  269. struct mem_cgroup *,
  270. struct mem_cgroup_reclaim_cookie *);
  271. void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
  272. static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
  273. {
  274. if (mem_cgroup_disabled())
  275. return 0;
  276. return memcg->css.id;
  277. }
  278. /**
  279. * mem_cgroup_from_id - look up a memcg from an id
  280. * @id: the id to look up
  281. *
  282. * Caller must hold rcu_read_lock() and use css_tryget() as necessary.
  283. */
  284. static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
  285. {
  286. struct cgroup_subsys_state *css;
  287. css = css_from_id(id, &memory_cgrp_subsys);
  288. return mem_cgroup_from_css(css);
  289. }
  290. /**
  291. * parent_mem_cgroup - find the accounting parent of a memcg
  292. * @memcg: memcg whose parent to find
  293. *
  294. * Returns the parent memcg, or NULL if this is the root or the memory
  295. * controller is in legacy no-hierarchy mode.
  296. */
  297. static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
  298. {
  299. if (!memcg->memory.parent)
  300. return NULL;
  301. return mem_cgroup_from_counter(memcg->memory.parent, memory);
  302. }
  303. static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
  304. struct mem_cgroup *root)
  305. {
  306. if (root == memcg)
  307. return true;
  308. if (!root->use_hierarchy)
  309. return false;
  310. return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
  311. }
  312. static inline bool mm_match_cgroup(struct mm_struct *mm,
  313. struct mem_cgroup *memcg)
  314. {
  315. struct mem_cgroup *task_memcg;
  316. bool match = false;
  317. rcu_read_lock();
  318. task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
  319. if (task_memcg)
  320. match = mem_cgroup_is_descendant(task_memcg, memcg);
  321. rcu_read_unlock();
  322. return match;
  323. }
  324. struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
  325. ino_t page_cgroup_ino(struct page *page);
  326. static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
  327. {
  328. if (mem_cgroup_disabled())
  329. return true;
  330. return !!(memcg->css.flags & CSS_ONLINE);
  331. }
  332. /*
  333. * For memory reclaim.
  334. */
  335. int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
  336. void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
  337. int nr_pages);
  338. static inline
  339. unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
  340. {
  341. struct mem_cgroup_per_zone *mz;
  342. mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
  343. return mz->lru_size[lru];
  344. }
  345. static inline bool mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
  346. {
  347. unsigned long inactive_ratio;
  348. unsigned long inactive;
  349. unsigned long active;
  350. unsigned long gb;
  351. inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
  352. active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
  353. gb = (inactive + active) >> (30 - PAGE_SHIFT);
  354. if (gb)
  355. inactive_ratio = int_sqrt(10 * gb);
  356. else
  357. inactive_ratio = 1;
  358. return inactive * inactive_ratio < active;
  359. }
  360. void mem_cgroup_handle_over_high(void);
  361. void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
  362. struct task_struct *p);
  363. static inline void mem_cgroup_oom_enable(void)
  364. {
  365. WARN_ON(current->memcg_may_oom);
  366. current->memcg_may_oom = 1;
  367. }
  368. static inline void mem_cgroup_oom_disable(void)
  369. {
  370. WARN_ON(!current->memcg_may_oom);
  371. current->memcg_may_oom = 0;
  372. }
  373. static inline bool task_in_memcg_oom(struct task_struct *p)
  374. {
  375. return p->memcg_in_oom;
  376. }
  377. bool mem_cgroup_oom_synchronize(bool wait);
  378. #ifdef CONFIG_MEMCG_SWAP
  379. extern int do_swap_account;
  380. #endif
  381. void lock_page_memcg(struct page *page);
  382. void unlock_page_memcg(struct page *page);
  383. /**
  384. * mem_cgroup_update_page_stat - update page state statistics
  385. * @page: the page
  386. * @idx: page state item to account
  387. * @val: number of pages (positive or negative)
  388. *
  389. * The @page must be locked or the caller must use lock_page_memcg()
  390. * to prevent double accounting when the page is concurrently being
  391. * moved to another memcg:
  392. *
  393. * lock_page(page) or lock_page_memcg(page)
  394. * if (TestClearPageState(page))
  395. * mem_cgroup_update_page_stat(page, state, -1);
  396. * unlock_page(page) or unlock_page_memcg(page)
  397. */
  398. static inline void mem_cgroup_update_page_stat(struct page *page,
  399. enum mem_cgroup_stat_index idx, int val)
  400. {
  401. VM_BUG_ON(!(rcu_read_lock_held() || PageLocked(page)));
  402. if (page->mem_cgroup)
  403. this_cpu_add(page->mem_cgroup->stat->count[idx], val);
  404. }
  405. static inline void mem_cgroup_inc_page_stat(struct page *page,
  406. enum mem_cgroup_stat_index idx)
  407. {
  408. mem_cgroup_update_page_stat(page, idx, 1);
  409. }
  410. static inline void mem_cgroup_dec_page_stat(struct page *page,
  411. enum mem_cgroup_stat_index idx)
  412. {
  413. mem_cgroup_update_page_stat(page, idx, -1);
  414. }
  415. unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
  416. gfp_t gfp_mask,
  417. unsigned long *total_scanned);
  418. static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
  419. enum vm_event_item idx)
  420. {
  421. struct mem_cgroup *memcg;
  422. if (mem_cgroup_disabled())
  423. return;
  424. rcu_read_lock();
  425. memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
  426. if (unlikely(!memcg))
  427. goto out;
  428. switch (idx) {
  429. case PGFAULT:
  430. this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
  431. break;
  432. case PGMAJFAULT:
  433. this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
  434. break;
  435. default:
  436. BUG();
  437. }
  438. out:
  439. rcu_read_unlock();
  440. }
  441. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  442. void mem_cgroup_split_huge_fixup(struct page *head);
  443. #endif
  444. #else /* CONFIG_MEMCG */
  445. #define MEM_CGROUP_ID_SHIFT 0
  446. #define MEM_CGROUP_ID_MAX 0
  447. struct mem_cgroup;
  448. static inline bool mem_cgroup_disabled(void)
  449. {
  450. return true;
  451. }
  452. static inline void mem_cgroup_events(struct mem_cgroup *memcg,
  453. enum mem_cgroup_events_index idx,
  454. unsigned int nr)
  455. {
  456. }
  457. static inline bool mem_cgroup_low(struct mem_cgroup *root,
  458. struct mem_cgroup *memcg)
  459. {
  460. return false;
  461. }
  462. static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
  463. gfp_t gfp_mask,
  464. struct mem_cgroup **memcgp,
  465. bool compound)
  466. {
  467. *memcgp = NULL;
  468. return 0;
  469. }
  470. static inline void mem_cgroup_commit_charge(struct page *page,
  471. struct mem_cgroup *memcg,
  472. bool lrucare, bool compound)
  473. {
  474. }
  475. static inline void mem_cgroup_cancel_charge(struct page *page,
  476. struct mem_cgroup *memcg,
  477. bool compound)
  478. {
  479. }
  480. static inline void mem_cgroup_uncharge(struct page *page)
  481. {
  482. }
  483. static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
  484. {
  485. }
  486. static inline void mem_cgroup_migrate(struct page *old, struct page *new)
  487. {
  488. }
  489. static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
  490. struct mem_cgroup *memcg)
  491. {
  492. return &zone->lruvec;
  493. }
  494. static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
  495. struct zone *zone)
  496. {
  497. return &zone->lruvec;
  498. }
  499. static inline bool mm_match_cgroup(struct mm_struct *mm,
  500. struct mem_cgroup *memcg)
  501. {
  502. return true;
  503. }
  504. static inline bool task_in_mem_cgroup(struct task_struct *task,
  505. const struct mem_cgroup *memcg)
  506. {
  507. return true;
  508. }
  509. static inline struct mem_cgroup *
  510. mem_cgroup_iter(struct mem_cgroup *root,
  511. struct mem_cgroup *prev,
  512. struct mem_cgroup_reclaim_cookie *reclaim)
  513. {
  514. return NULL;
  515. }
  516. static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
  517. struct mem_cgroup *prev)
  518. {
  519. }
  520. static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
  521. {
  522. return 0;
  523. }
  524. static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
  525. {
  526. WARN_ON_ONCE(id);
  527. /* XXX: This should always return root_mem_cgroup */
  528. return NULL;
  529. }
  530. static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
  531. {
  532. return true;
  533. }
  534. static inline bool
  535. mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
  536. {
  537. return true;
  538. }
  539. static inline unsigned long
  540. mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
  541. {
  542. return 0;
  543. }
  544. static inline void
  545. mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
  546. int increment)
  547. {
  548. }
  549. static inline void
  550. mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
  551. {
  552. }
  553. static inline void lock_page_memcg(struct page *page)
  554. {
  555. }
  556. static inline void unlock_page_memcg(struct page *page)
  557. {
  558. }
  559. static inline void mem_cgroup_handle_over_high(void)
  560. {
  561. }
  562. static inline void mem_cgroup_oom_enable(void)
  563. {
  564. }
  565. static inline void mem_cgroup_oom_disable(void)
  566. {
  567. }
  568. static inline bool task_in_memcg_oom(struct task_struct *p)
  569. {
  570. return false;
  571. }
  572. static inline bool mem_cgroup_oom_synchronize(bool wait)
  573. {
  574. return false;
  575. }
  576. static inline void mem_cgroup_inc_page_stat(struct page *page,
  577. enum mem_cgroup_stat_index idx)
  578. {
  579. }
  580. static inline void mem_cgroup_dec_page_stat(struct page *page,
  581. enum mem_cgroup_stat_index idx)
  582. {
  583. }
  584. static inline
  585. unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
  586. gfp_t gfp_mask,
  587. unsigned long *total_scanned)
  588. {
  589. return 0;
  590. }
  591. static inline void mem_cgroup_split_huge_fixup(struct page *head)
  592. {
  593. }
  594. static inline
  595. void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
  596. {
  597. }
  598. #endif /* CONFIG_MEMCG */
  599. #ifdef CONFIG_CGROUP_WRITEBACK
  600. struct list_head *mem_cgroup_cgwb_list(struct mem_cgroup *memcg);
  601. struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
  602. void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
  603. unsigned long *pheadroom, unsigned long *pdirty,
  604. unsigned long *pwriteback);
  605. #else /* CONFIG_CGROUP_WRITEBACK */
  606. static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
  607. {
  608. return NULL;
  609. }
  610. static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
  611. unsigned long *pfilepages,
  612. unsigned long *pheadroom,
  613. unsigned long *pdirty,
  614. unsigned long *pwriteback)
  615. {
  616. }
  617. #endif /* CONFIG_CGROUP_WRITEBACK */
  618. struct sock;
  619. void sock_update_memcg(struct sock *sk);
  620. void sock_release_memcg(struct sock *sk);
  621. bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
  622. void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
  623. #ifdef CONFIG_MEMCG
  624. extern struct static_key_false memcg_sockets_enabled_key;
  625. #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
  626. static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
  627. {
  628. if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
  629. return true;
  630. do {
  631. if (time_before(jiffies, memcg->socket_pressure))
  632. return true;
  633. } while ((memcg = parent_mem_cgroup(memcg)));
  634. return false;
  635. }
  636. #else
  637. #define mem_cgroup_sockets_enabled 0
  638. static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
  639. {
  640. return false;
  641. }
  642. #endif
  643. #if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
  644. extern struct static_key_false memcg_kmem_enabled_key;
  645. extern int memcg_nr_cache_ids;
  646. void memcg_get_cache_ids(void);
  647. void memcg_put_cache_ids(void);
  648. /*
  649. * Helper macro to loop through all memcg-specific caches. Callers must still
  650. * check if the cache is valid (it is either valid or NULL).
  651. * the slab_mutex must be held when looping through those caches
  652. */
  653. #define for_each_memcg_cache_index(_idx) \
  654. for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
  655. static inline bool memcg_kmem_enabled(void)
  656. {
  657. return static_branch_unlikely(&memcg_kmem_enabled_key);
  658. }
  659. static inline bool memcg_kmem_online(struct mem_cgroup *memcg)
  660. {
  661. return memcg->kmem_state == KMEM_ONLINE;
  662. }
  663. /*
  664. * In general, we'll do everything in our power to not incur in any overhead
  665. * for non-memcg users for the kmem functions. Not even a function call, if we
  666. * can avoid it.
  667. *
  668. * Therefore, we'll inline all those functions so that in the best case, we'll
  669. * see that kmemcg is off for everybody and proceed quickly. If it is on,
  670. * we'll still do most of the flag checking inline. We check a lot of
  671. * conditions, but because they are pretty simple, they are expected to be
  672. * fast.
  673. */
  674. int __memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order,
  675. struct mem_cgroup *memcg);
  676. int __memcg_kmem_charge(struct page *page, gfp_t gfp, int order);
  677. void __memcg_kmem_uncharge(struct page *page, int order);
  678. /*
  679. * helper for accessing a memcg's index. It will be used as an index in the
  680. * child cache array in kmem_cache, and also to derive its name. This function
  681. * will return -1 when this is not a kmem-limited memcg.
  682. */
  683. static inline int memcg_cache_id(struct mem_cgroup *memcg)
  684. {
  685. return memcg ? memcg->kmemcg_id : -1;
  686. }
  687. struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
  688. void __memcg_kmem_put_cache(struct kmem_cache *cachep);
  689. static inline bool __memcg_kmem_bypass(void)
  690. {
  691. if (!memcg_kmem_enabled())
  692. return true;
  693. if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
  694. return true;
  695. return false;
  696. }
  697. /**
  698. * memcg_kmem_charge: charge a kmem page
  699. * @page: page to charge
  700. * @gfp: reclaim mode
  701. * @order: allocation order
  702. *
  703. * Returns 0 on success, an error code on failure.
  704. */
  705. static __always_inline int memcg_kmem_charge(struct page *page,
  706. gfp_t gfp, int order)
  707. {
  708. if (__memcg_kmem_bypass())
  709. return 0;
  710. if (!(gfp & __GFP_ACCOUNT))
  711. return 0;
  712. return __memcg_kmem_charge(page, gfp, order);
  713. }
  714. /**
  715. * memcg_kmem_uncharge: uncharge a kmem page
  716. * @page: page to uncharge
  717. * @order: allocation order
  718. */
  719. static __always_inline void memcg_kmem_uncharge(struct page *page, int order)
  720. {
  721. if (memcg_kmem_enabled())
  722. __memcg_kmem_uncharge(page, order);
  723. }
  724. /**
  725. * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
  726. * @cachep: the original global kmem cache
  727. *
  728. * All memory allocated from a per-memcg cache is charged to the owner memcg.
  729. */
  730. static __always_inline struct kmem_cache *
  731. memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
  732. {
  733. if (__memcg_kmem_bypass())
  734. return cachep;
  735. return __memcg_kmem_get_cache(cachep, gfp);
  736. }
  737. static __always_inline void memcg_kmem_put_cache(struct kmem_cache *cachep)
  738. {
  739. if (memcg_kmem_enabled())
  740. __memcg_kmem_put_cache(cachep);
  741. }
  742. #else
  743. #define for_each_memcg_cache_index(_idx) \
  744. for (; NULL; )
  745. static inline bool memcg_kmem_enabled(void)
  746. {
  747. return false;
  748. }
  749. static inline bool memcg_kmem_online(struct mem_cgroup *memcg)
  750. {
  751. return false;
  752. }
  753. static inline int memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
  754. {
  755. return 0;
  756. }
  757. static inline void memcg_kmem_uncharge(struct page *page, int order)
  758. {
  759. }
  760. static inline int memcg_cache_id(struct mem_cgroup *memcg)
  761. {
  762. return -1;
  763. }
  764. static inline void memcg_get_cache_ids(void)
  765. {
  766. }
  767. static inline void memcg_put_cache_ids(void)
  768. {
  769. }
  770. static inline struct kmem_cache *
  771. memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
  772. {
  773. return cachep;
  774. }
  775. static inline void memcg_kmem_put_cache(struct kmem_cache *cachep)
  776. {
  777. }
  778. #endif /* CONFIG_MEMCG && !CONFIG_SLOB */
  779. #endif /* _LINUX_MEMCONTROL_H */