memcontrol.h 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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/mm.h>
  29. #include <linux/vmstat.h>
  30. #include <linux/writeback.h>
  31. #include <linux/page-flags.h>
  32. struct mem_cgroup;
  33. struct page;
  34. struct mm_struct;
  35. struct kmem_cache;
  36. /* Cgroup-specific page state, on top of universal node page state */
  37. enum memcg_stat_item {
  38. MEMCG_CACHE = NR_VM_NODE_STAT_ITEMS,
  39. MEMCG_RSS,
  40. MEMCG_RSS_HUGE,
  41. MEMCG_SWAP,
  42. MEMCG_SOCK,
  43. /* XXX: why are these zone and not node counters? */
  44. MEMCG_KERNEL_STACK_KB,
  45. MEMCG_NR_STAT,
  46. };
  47. enum memcg_memory_event {
  48. MEMCG_LOW,
  49. MEMCG_HIGH,
  50. MEMCG_MAX,
  51. MEMCG_OOM,
  52. MEMCG_SWAP_MAX,
  53. MEMCG_SWAP_FAIL,
  54. MEMCG_NR_MEMORY_EVENTS,
  55. };
  56. enum mem_cgroup_protection {
  57. MEMCG_PROT_NONE,
  58. MEMCG_PROT_LOW,
  59. MEMCG_PROT_MIN,
  60. };
  61. struct mem_cgroup_reclaim_cookie {
  62. pg_data_t *pgdat;
  63. int priority;
  64. unsigned int generation;
  65. };
  66. #ifdef CONFIG_MEMCG
  67. #define MEM_CGROUP_ID_SHIFT 16
  68. #define MEM_CGROUP_ID_MAX USHRT_MAX
  69. struct mem_cgroup_id {
  70. int id;
  71. atomic_t ref;
  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. struct mem_cgroup_stat_cpu {
  86. long count[MEMCG_NR_STAT];
  87. unsigned long events[NR_VM_EVENT_ITEMS];
  88. unsigned long nr_page_events;
  89. unsigned long targets[MEM_CGROUP_NTARGETS];
  90. };
  91. struct mem_cgroup_reclaim_iter {
  92. struct mem_cgroup *position;
  93. /* scan generation, increased every round-trip */
  94. unsigned int generation;
  95. };
  96. struct lruvec_stat {
  97. long count[NR_VM_NODE_STAT_ITEMS];
  98. };
  99. /*
  100. * per-zone information in memory controller.
  101. */
  102. struct mem_cgroup_per_node {
  103. struct lruvec lruvec;
  104. struct lruvec_stat __percpu *lruvec_stat_cpu;
  105. atomic_long_t lruvec_stat[NR_VM_NODE_STAT_ITEMS];
  106. unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
  107. struct mem_cgroup_reclaim_iter iter[DEF_PRIORITY + 1];
  108. struct rb_node tree_node; /* RB tree node */
  109. unsigned long usage_in_excess;/* Set to the value by which */
  110. /* the soft limit is exceeded*/
  111. bool on_tree;
  112. bool congested; /* memcg has many dirty pages */
  113. /* backed by a congested BDI */
  114. struct mem_cgroup *memcg; /* Back pointer, we cannot */
  115. /* use container_of */
  116. };
  117. struct mem_cgroup_threshold {
  118. struct eventfd_ctx *eventfd;
  119. unsigned long threshold;
  120. };
  121. /* For threshold */
  122. struct mem_cgroup_threshold_ary {
  123. /* An array index points to threshold just below or equal to usage. */
  124. int current_threshold;
  125. /* Size of entries[] */
  126. unsigned int size;
  127. /* Array of thresholds */
  128. struct mem_cgroup_threshold entries[0];
  129. };
  130. struct mem_cgroup_thresholds {
  131. /* Primary thresholds array */
  132. struct mem_cgroup_threshold_ary *primary;
  133. /*
  134. * Spare threshold array.
  135. * This is needed to make mem_cgroup_unregister_event() "never fail".
  136. * It must be able to store at least primary->size - 1 entries.
  137. */
  138. struct mem_cgroup_threshold_ary *spare;
  139. };
  140. enum memcg_kmem_state {
  141. KMEM_NONE,
  142. KMEM_ALLOCATED,
  143. KMEM_ONLINE,
  144. };
  145. /*
  146. * The memory controller data structure. The memory controller controls both
  147. * page cache and RSS per cgroup. We would eventually like to provide
  148. * statistics based on the statistics developed by Rik Van Riel for clock-pro,
  149. * to help the administrator determine what knobs to tune.
  150. */
  151. struct mem_cgroup {
  152. struct cgroup_subsys_state css;
  153. /* Private memcg ID. Used to ID objects that outlive the cgroup */
  154. struct mem_cgroup_id id;
  155. /* Accounted resources */
  156. struct page_counter memory;
  157. struct page_counter swap;
  158. /* Legacy consumer-oriented counters */
  159. struct page_counter memsw;
  160. struct page_counter kmem;
  161. struct page_counter tcpmem;
  162. /* Upper bound of normal memory consumption range */
  163. unsigned long high;
  164. /* Range enforcement for interrupt charges */
  165. struct work_struct high_work;
  166. unsigned long soft_limit;
  167. /* vmpressure notifications */
  168. struct vmpressure vmpressure;
  169. /*
  170. * Should the accounting and control be hierarchical, per subtree?
  171. */
  172. bool use_hierarchy;
  173. /* protected by memcg_oom_lock */
  174. bool oom_lock;
  175. int under_oom;
  176. int swappiness;
  177. /* OOM-Killer disable */
  178. int oom_kill_disable;
  179. /* memory.events */
  180. atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
  181. struct cgroup_file events_file;
  182. /* handle for "memory.swap.events" */
  183. struct cgroup_file swap_events_file;
  184. /* protect arrays of thresholds */
  185. struct mutex thresholds_lock;
  186. /* thresholds for memory usage. RCU-protected */
  187. struct mem_cgroup_thresholds thresholds;
  188. /* thresholds for mem+swap usage. RCU-protected */
  189. struct mem_cgroup_thresholds memsw_thresholds;
  190. /* For oom notifier event fd */
  191. struct list_head oom_notify;
  192. /*
  193. * Should we move charges of a task when a task is moved into this
  194. * mem_cgroup ? And what type of charges should we move ?
  195. */
  196. unsigned long move_charge_at_immigrate;
  197. /*
  198. * set > 0 if pages under this cgroup are moving to other cgroup.
  199. */
  200. atomic_t moving_account;
  201. /* taken only while moving_account > 0 */
  202. spinlock_t move_lock;
  203. struct task_struct *move_lock_task;
  204. unsigned long move_lock_flags;
  205. /* memory.stat */
  206. struct mem_cgroup_stat_cpu __percpu *stat_cpu;
  207. atomic_long_t stat[MEMCG_NR_STAT];
  208. atomic_long_t events[NR_VM_EVENT_ITEMS];
  209. unsigned long socket_pressure;
  210. /* Legacy tcp memory accounting */
  211. bool tcpmem_active;
  212. int tcpmem_pressure;
  213. #ifndef CONFIG_SLOB
  214. /* Index in the kmem_cache->memcg_params.memcg_caches array */
  215. int kmemcg_id;
  216. enum memcg_kmem_state kmem_state;
  217. struct list_head kmem_caches;
  218. #endif
  219. int last_scanned_node;
  220. #if MAX_NUMNODES > 1
  221. nodemask_t scan_nodes;
  222. atomic_t numainfo_events;
  223. atomic_t numainfo_updating;
  224. #endif
  225. #ifdef CONFIG_CGROUP_WRITEBACK
  226. struct list_head cgwb_list;
  227. struct wb_domain cgwb_domain;
  228. #endif
  229. /* List of events which userspace want to receive */
  230. struct list_head event_list;
  231. spinlock_t event_list_lock;
  232. struct mem_cgroup_per_node *nodeinfo[0];
  233. /* WARNING: nodeinfo must be the last member here */
  234. };
  235. /*
  236. * size of first charge trial. "32" comes from vmscan.c's magic value.
  237. * TODO: maybe necessary to use big numbers in big irons.
  238. */
  239. #define MEMCG_CHARGE_BATCH 32U
  240. extern struct mem_cgroup *root_mem_cgroup;
  241. static inline bool mem_cgroup_disabled(void)
  242. {
  243. return !cgroup_subsys_enabled(memory_cgrp_subsys);
  244. }
  245. enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root,
  246. struct mem_cgroup *memcg);
  247. int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
  248. gfp_t gfp_mask, struct mem_cgroup **memcgp,
  249. bool compound);
  250. void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
  251. bool lrucare, bool compound);
  252. void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg,
  253. bool compound);
  254. void mem_cgroup_uncharge(struct page *page);
  255. void mem_cgroup_uncharge_list(struct list_head *page_list);
  256. void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
  257. static struct mem_cgroup_per_node *
  258. mem_cgroup_nodeinfo(struct mem_cgroup *memcg, int nid)
  259. {
  260. return memcg->nodeinfo[nid];
  261. }
  262. /**
  263. * mem_cgroup_lruvec - get the lru list vector for a node or a memcg zone
  264. * @node: node of the wanted lruvec
  265. * @memcg: memcg of the wanted lruvec
  266. *
  267. * Returns the lru list vector holding pages for a given @node or a given
  268. * @memcg and @zone. This can be the node lruvec, if the memory controller
  269. * is disabled.
  270. */
  271. static inline struct lruvec *mem_cgroup_lruvec(struct pglist_data *pgdat,
  272. struct mem_cgroup *memcg)
  273. {
  274. struct mem_cgroup_per_node *mz;
  275. struct lruvec *lruvec;
  276. if (mem_cgroup_disabled()) {
  277. lruvec = node_lruvec(pgdat);
  278. goto out;
  279. }
  280. mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
  281. lruvec = &mz->lruvec;
  282. out:
  283. /*
  284. * Since a node can be onlined after the mem_cgroup was created,
  285. * we have to be prepared to initialize lruvec->pgdat here;
  286. * and if offlined then reonlined, we need to reinitialize it.
  287. */
  288. if (unlikely(lruvec->pgdat != pgdat))
  289. lruvec->pgdat = pgdat;
  290. return lruvec;
  291. }
  292. struct lruvec *mem_cgroup_page_lruvec(struct page *, struct pglist_data *);
  293. bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg);
  294. struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
  295. static inline
  296. struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
  297. return css ? container_of(css, struct mem_cgroup, css) : NULL;
  298. }
  299. #define mem_cgroup_from_counter(counter, member) \
  300. container_of(counter, struct mem_cgroup, member)
  301. struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
  302. struct mem_cgroup *,
  303. struct mem_cgroup_reclaim_cookie *);
  304. void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
  305. int mem_cgroup_scan_tasks(struct mem_cgroup *,
  306. int (*)(struct task_struct *, void *), void *);
  307. static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
  308. {
  309. if (mem_cgroup_disabled())
  310. return 0;
  311. return memcg->id.id;
  312. }
  313. struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
  314. static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
  315. {
  316. struct mem_cgroup_per_node *mz;
  317. if (mem_cgroup_disabled())
  318. return NULL;
  319. mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
  320. return mz->memcg;
  321. }
  322. /**
  323. * parent_mem_cgroup - find the accounting parent of a memcg
  324. * @memcg: memcg whose parent to find
  325. *
  326. * Returns the parent memcg, or NULL if this is the root or the memory
  327. * controller is in legacy no-hierarchy mode.
  328. */
  329. static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
  330. {
  331. if (!memcg->memory.parent)
  332. return NULL;
  333. return mem_cgroup_from_counter(memcg->memory.parent, memory);
  334. }
  335. static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
  336. struct mem_cgroup *root)
  337. {
  338. if (root == memcg)
  339. return true;
  340. if (!root->use_hierarchy)
  341. return false;
  342. return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
  343. }
  344. static inline bool mm_match_cgroup(struct mm_struct *mm,
  345. struct mem_cgroup *memcg)
  346. {
  347. struct mem_cgroup *task_memcg;
  348. bool match = false;
  349. rcu_read_lock();
  350. task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
  351. if (task_memcg)
  352. match = mem_cgroup_is_descendant(task_memcg, memcg);
  353. rcu_read_unlock();
  354. return match;
  355. }
  356. struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
  357. ino_t page_cgroup_ino(struct page *page);
  358. static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
  359. {
  360. if (mem_cgroup_disabled())
  361. return true;
  362. return !!(memcg->css.flags & CSS_ONLINE);
  363. }
  364. /*
  365. * For memory reclaim.
  366. */
  367. int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
  368. void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
  369. int zid, int nr_pages);
  370. unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
  371. int nid, unsigned int lru_mask);
  372. static inline
  373. unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
  374. {
  375. struct mem_cgroup_per_node *mz;
  376. unsigned long nr_pages = 0;
  377. int zid;
  378. mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
  379. for (zid = 0; zid < MAX_NR_ZONES; zid++)
  380. nr_pages += mz->lru_zone_size[zid][lru];
  381. return nr_pages;
  382. }
  383. static inline
  384. unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
  385. enum lru_list lru, int zone_idx)
  386. {
  387. struct mem_cgroup_per_node *mz;
  388. mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
  389. return mz->lru_zone_size[zone_idx][lru];
  390. }
  391. void mem_cgroup_handle_over_high(void);
  392. unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
  393. void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
  394. struct task_struct *p);
  395. static inline void mem_cgroup_oom_enable(void)
  396. {
  397. WARN_ON(current->memcg_may_oom);
  398. current->memcg_may_oom = 1;
  399. }
  400. static inline void mem_cgroup_oom_disable(void)
  401. {
  402. WARN_ON(!current->memcg_may_oom);
  403. current->memcg_may_oom = 0;
  404. }
  405. static inline bool task_in_memcg_oom(struct task_struct *p)
  406. {
  407. return p->memcg_in_oom;
  408. }
  409. bool mem_cgroup_oom_synchronize(bool wait);
  410. #ifdef CONFIG_MEMCG_SWAP
  411. extern int do_swap_account;
  412. #endif
  413. struct mem_cgroup *lock_page_memcg(struct page *page);
  414. void __unlock_page_memcg(struct mem_cgroup *memcg);
  415. void unlock_page_memcg(struct page *page);
  416. /* idx can be of type enum memcg_stat_item or node_stat_item */
  417. static inline unsigned long memcg_page_state(struct mem_cgroup *memcg,
  418. int idx)
  419. {
  420. long x = atomic_long_read(&memcg->stat[idx]);
  421. #ifdef CONFIG_SMP
  422. if (x < 0)
  423. x = 0;
  424. #endif
  425. return x;
  426. }
  427. /* idx can be of type enum memcg_stat_item or node_stat_item */
  428. static inline void __mod_memcg_state(struct mem_cgroup *memcg,
  429. int idx, int val)
  430. {
  431. long x;
  432. if (mem_cgroup_disabled())
  433. return;
  434. x = val + __this_cpu_read(memcg->stat_cpu->count[idx]);
  435. if (unlikely(abs(x) > MEMCG_CHARGE_BATCH)) {
  436. atomic_long_add(x, &memcg->stat[idx]);
  437. x = 0;
  438. }
  439. __this_cpu_write(memcg->stat_cpu->count[idx], x);
  440. }
  441. /* idx can be of type enum memcg_stat_item or node_stat_item */
  442. static inline void mod_memcg_state(struct mem_cgroup *memcg,
  443. int idx, int val)
  444. {
  445. unsigned long flags;
  446. local_irq_save(flags);
  447. __mod_memcg_state(memcg, idx, val);
  448. local_irq_restore(flags);
  449. }
  450. /**
  451. * mod_memcg_page_state - update page state statistics
  452. * @page: the page
  453. * @idx: page state item to account
  454. * @val: number of pages (positive or negative)
  455. *
  456. * The @page must be locked or the caller must use lock_page_memcg()
  457. * to prevent double accounting when the page is concurrently being
  458. * moved to another memcg:
  459. *
  460. * lock_page(page) or lock_page_memcg(page)
  461. * if (TestClearPageState(page))
  462. * mod_memcg_page_state(page, state, -1);
  463. * unlock_page(page) or unlock_page_memcg(page)
  464. *
  465. * Kernel pages are an exception to this, since they'll never move.
  466. */
  467. static inline void __mod_memcg_page_state(struct page *page,
  468. int idx, int val)
  469. {
  470. if (page->mem_cgroup)
  471. __mod_memcg_state(page->mem_cgroup, idx, val);
  472. }
  473. static inline void mod_memcg_page_state(struct page *page,
  474. int idx, int val)
  475. {
  476. if (page->mem_cgroup)
  477. mod_memcg_state(page->mem_cgroup, idx, val);
  478. }
  479. static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
  480. enum node_stat_item idx)
  481. {
  482. struct mem_cgroup_per_node *pn;
  483. long x;
  484. if (mem_cgroup_disabled())
  485. return node_page_state(lruvec_pgdat(lruvec), idx);
  486. pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
  487. x = atomic_long_read(&pn->lruvec_stat[idx]);
  488. #ifdef CONFIG_SMP
  489. if (x < 0)
  490. x = 0;
  491. #endif
  492. return x;
  493. }
  494. static inline void __mod_lruvec_state(struct lruvec *lruvec,
  495. enum node_stat_item idx, int val)
  496. {
  497. struct mem_cgroup_per_node *pn;
  498. long x;
  499. /* Update node */
  500. __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
  501. if (mem_cgroup_disabled())
  502. return;
  503. pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
  504. /* Update memcg */
  505. __mod_memcg_state(pn->memcg, idx, val);
  506. /* Update lruvec */
  507. x = val + __this_cpu_read(pn->lruvec_stat_cpu->count[idx]);
  508. if (unlikely(abs(x) > MEMCG_CHARGE_BATCH)) {
  509. atomic_long_add(x, &pn->lruvec_stat[idx]);
  510. x = 0;
  511. }
  512. __this_cpu_write(pn->lruvec_stat_cpu->count[idx], x);
  513. }
  514. static inline void mod_lruvec_state(struct lruvec *lruvec,
  515. enum node_stat_item idx, int val)
  516. {
  517. unsigned long flags;
  518. local_irq_save(flags);
  519. __mod_lruvec_state(lruvec, idx, val);
  520. local_irq_restore(flags);
  521. }
  522. static inline void __mod_lruvec_page_state(struct page *page,
  523. enum node_stat_item idx, int val)
  524. {
  525. pg_data_t *pgdat = page_pgdat(page);
  526. struct lruvec *lruvec;
  527. /* Untracked pages have no memcg, no lruvec. Update only the node */
  528. if (!page->mem_cgroup) {
  529. __mod_node_page_state(pgdat, idx, val);
  530. return;
  531. }
  532. lruvec = mem_cgroup_lruvec(pgdat, page->mem_cgroup);
  533. __mod_lruvec_state(lruvec, idx, val);
  534. }
  535. static inline void mod_lruvec_page_state(struct page *page,
  536. enum node_stat_item idx, int val)
  537. {
  538. unsigned long flags;
  539. local_irq_save(flags);
  540. __mod_lruvec_page_state(page, idx, val);
  541. local_irq_restore(flags);
  542. }
  543. unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
  544. gfp_t gfp_mask,
  545. unsigned long *total_scanned);
  546. static inline void __count_memcg_events(struct mem_cgroup *memcg,
  547. enum vm_event_item idx,
  548. unsigned long count)
  549. {
  550. unsigned long x;
  551. if (mem_cgroup_disabled())
  552. return;
  553. x = count + __this_cpu_read(memcg->stat_cpu->events[idx]);
  554. if (unlikely(x > MEMCG_CHARGE_BATCH)) {
  555. atomic_long_add(x, &memcg->events[idx]);
  556. x = 0;
  557. }
  558. __this_cpu_write(memcg->stat_cpu->events[idx], x);
  559. }
  560. static inline void count_memcg_events(struct mem_cgroup *memcg,
  561. enum vm_event_item idx,
  562. unsigned long count)
  563. {
  564. unsigned long flags;
  565. local_irq_save(flags);
  566. __count_memcg_events(memcg, idx, count);
  567. local_irq_restore(flags);
  568. }
  569. static inline void count_memcg_page_event(struct page *page,
  570. enum vm_event_item idx)
  571. {
  572. if (page->mem_cgroup)
  573. count_memcg_events(page->mem_cgroup, idx, 1);
  574. }
  575. static inline void count_memcg_event_mm(struct mm_struct *mm,
  576. enum vm_event_item idx)
  577. {
  578. struct mem_cgroup *memcg;
  579. if (mem_cgroup_disabled())
  580. return;
  581. rcu_read_lock();
  582. memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
  583. if (likely(memcg)) {
  584. count_memcg_events(memcg, idx, 1);
  585. if (idx == OOM_KILL)
  586. cgroup_file_notify(&memcg->events_file);
  587. }
  588. rcu_read_unlock();
  589. }
  590. static inline void memcg_memory_event(struct mem_cgroup *memcg,
  591. enum memcg_memory_event event)
  592. {
  593. atomic_long_inc(&memcg->memory_events[event]);
  594. cgroup_file_notify(&memcg->events_file);
  595. }
  596. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  597. void mem_cgroup_split_huge_fixup(struct page *head);
  598. #endif
  599. #else /* CONFIG_MEMCG */
  600. #define MEM_CGROUP_ID_SHIFT 0
  601. #define MEM_CGROUP_ID_MAX 0
  602. struct mem_cgroup;
  603. static inline bool mem_cgroup_disabled(void)
  604. {
  605. return true;
  606. }
  607. static inline void memcg_memory_event(struct mem_cgroup *memcg,
  608. enum memcg_memory_event event)
  609. {
  610. }
  611. static inline enum mem_cgroup_protection mem_cgroup_protected(
  612. struct mem_cgroup *root, struct mem_cgroup *memcg)
  613. {
  614. return MEMCG_PROT_NONE;
  615. }
  616. static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
  617. gfp_t gfp_mask,
  618. struct mem_cgroup **memcgp,
  619. bool compound)
  620. {
  621. *memcgp = NULL;
  622. return 0;
  623. }
  624. static inline void mem_cgroup_commit_charge(struct page *page,
  625. struct mem_cgroup *memcg,
  626. bool lrucare, bool compound)
  627. {
  628. }
  629. static inline void mem_cgroup_cancel_charge(struct page *page,
  630. struct mem_cgroup *memcg,
  631. bool compound)
  632. {
  633. }
  634. static inline void mem_cgroup_uncharge(struct page *page)
  635. {
  636. }
  637. static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
  638. {
  639. }
  640. static inline void mem_cgroup_migrate(struct page *old, struct page *new)
  641. {
  642. }
  643. static inline struct lruvec *mem_cgroup_lruvec(struct pglist_data *pgdat,
  644. struct mem_cgroup *memcg)
  645. {
  646. return node_lruvec(pgdat);
  647. }
  648. static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
  649. struct pglist_data *pgdat)
  650. {
  651. return &pgdat->lruvec;
  652. }
  653. static inline bool mm_match_cgroup(struct mm_struct *mm,
  654. struct mem_cgroup *memcg)
  655. {
  656. return true;
  657. }
  658. static inline bool task_in_mem_cgroup(struct task_struct *task,
  659. const struct mem_cgroup *memcg)
  660. {
  661. return true;
  662. }
  663. static inline struct mem_cgroup *
  664. mem_cgroup_iter(struct mem_cgroup *root,
  665. struct mem_cgroup *prev,
  666. struct mem_cgroup_reclaim_cookie *reclaim)
  667. {
  668. return NULL;
  669. }
  670. static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
  671. struct mem_cgroup *prev)
  672. {
  673. }
  674. static inline int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
  675. int (*fn)(struct task_struct *, void *), void *arg)
  676. {
  677. return 0;
  678. }
  679. static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
  680. {
  681. return 0;
  682. }
  683. static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
  684. {
  685. WARN_ON_ONCE(id);
  686. /* XXX: This should always return root_mem_cgroup */
  687. return NULL;
  688. }
  689. static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
  690. {
  691. return NULL;
  692. }
  693. static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
  694. {
  695. return true;
  696. }
  697. static inline unsigned long
  698. mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
  699. {
  700. return 0;
  701. }
  702. static inline
  703. unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
  704. enum lru_list lru, int zone_idx)
  705. {
  706. return 0;
  707. }
  708. static inline unsigned long
  709. mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
  710. int nid, unsigned int lru_mask)
  711. {
  712. return 0;
  713. }
  714. static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
  715. {
  716. return 0;
  717. }
  718. static inline void
  719. mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
  720. {
  721. }
  722. static inline struct mem_cgroup *lock_page_memcg(struct page *page)
  723. {
  724. return NULL;
  725. }
  726. static inline void __unlock_page_memcg(struct mem_cgroup *memcg)
  727. {
  728. }
  729. static inline void unlock_page_memcg(struct page *page)
  730. {
  731. }
  732. static inline void mem_cgroup_handle_over_high(void)
  733. {
  734. }
  735. static inline void mem_cgroup_oom_enable(void)
  736. {
  737. }
  738. static inline void mem_cgroup_oom_disable(void)
  739. {
  740. }
  741. static inline bool task_in_memcg_oom(struct task_struct *p)
  742. {
  743. return false;
  744. }
  745. static inline bool mem_cgroup_oom_synchronize(bool wait)
  746. {
  747. return false;
  748. }
  749. static inline unsigned long memcg_page_state(struct mem_cgroup *memcg,
  750. int idx)
  751. {
  752. return 0;
  753. }
  754. static inline void __mod_memcg_state(struct mem_cgroup *memcg,
  755. int idx,
  756. int nr)
  757. {
  758. }
  759. static inline void mod_memcg_state(struct mem_cgroup *memcg,
  760. int idx,
  761. int nr)
  762. {
  763. }
  764. static inline void __mod_memcg_page_state(struct page *page,
  765. int idx,
  766. int nr)
  767. {
  768. }
  769. static inline void mod_memcg_page_state(struct page *page,
  770. int idx,
  771. int nr)
  772. {
  773. }
  774. static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
  775. enum node_stat_item idx)
  776. {
  777. return node_page_state(lruvec_pgdat(lruvec), idx);
  778. }
  779. static inline void __mod_lruvec_state(struct lruvec *lruvec,
  780. enum node_stat_item idx, int val)
  781. {
  782. __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
  783. }
  784. static inline void mod_lruvec_state(struct lruvec *lruvec,
  785. enum node_stat_item idx, int val)
  786. {
  787. mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
  788. }
  789. static inline void __mod_lruvec_page_state(struct page *page,
  790. enum node_stat_item idx, int val)
  791. {
  792. __mod_node_page_state(page_pgdat(page), idx, val);
  793. }
  794. static inline void mod_lruvec_page_state(struct page *page,
  795. enum node_stat_item idx, int val)
  796. {
  797. mod_node_page_state(page_pgdat(page), idx, val);
  798. }
  799. static inline
  800. unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
  801. gfp_t gfp_mask,
  802. unsigned long *total_scanned)
  803. {
  804. return 0;
  805. }
  806. static inline void mem_cgroup_split_huge_fixup(struct page *head)
  807. {
  808. }
  809. static inline void count_memcg_events(struct mem_cgroup *memcg,
  810. enum vm_event_item idx,
  811. unsigned long count)
  812. {
  813. }
  814. static inline void count_memcg_page_event(struct page *page,
  815. int idx)
  816. {
  817. }
  818. static inline
  819. void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
  820. {
  821. }
  822. #endif /* CONFIG_MEMCG */
  823. /* idx can be of type enum memcg_stat_item or node_stat_item */
  824. static inline void __inc_memcg_state(struct mem_cgroup *memcg,
  825. int idx)
  826. {
  827. __mod_memcg_state(memcg, idx, 1);
  828. }
  829. /* idx can be of type enum memcg_stat_item or node_stat_item */
  830. static inline void __dec_memcg_state(struct mem_cgroup *memcg,
  831. int idx)
  832. {
  833. __mod_memcg_state(memcg, idx, -1);
  834. }
  835. /* idx can be of type enum memcg_stat_item or node_stat_item */
  836. static inline void __inc_memcg_page_state(struct page *page,
  837. int idx)
  838. {
  839. __mod_memcg_page_state(page, idx, 1);
  840. }
  841. /* idx can be of type enum memcg_stat_item or node_stat_item */
  842. static inline void __dec_memcg_page_state(struct page *page,
  843. int idx)
  844. {
  845. __mod_memcg_page_state(page, idx, -1);
  846. }
  847. static inline void __inc_lruvec_state(struct lruvec *lruvec,
  848. enum node_stat_item idx)
  849. {
  850. __mod_lruvec_state(lruvec, idx, 1);
  851. }
  852. static inline void __dec_lruvec_state(struct lruvec *lruvec,
  853. enum node_stat_item idx)
  854. {
  855. __mod_lruvec_state(lruvec, idx, -1);
  856. }
  857. static inline void __inc_lruvec_page_state(struct page *page,
  858. enum node_stat_item idx)
  859. {
  860. __mod_lruvec_page_state(page, idx, 1);
  861. }
  862. static inline void __dec_lruvec_page_state(struct page *page,
  863. enum node_stat_item idx)
  864. {
  865. __mod_lruvec_page_state(page, idx, -1);
  866. }
  867. /* idx can be of type enum memcg_stat_item or node_stat_item */
  868. static inline void inc_memcg_state(struct mem_cgroup *memcg,
  869. int idx)
  870. {
  871. mod_memcg_state(memcg, idx, 1);
  872. }
  873. /* idx can be of type enum memcg_stat_item or node_stat_item */
  874. static inline void dec_memcg_state(struct mem_cgroup *memcg,
  875. int idx)
  876. {
  877. mod_memcg_state(memcg, idx, -1);
  878. }
  879. /* idx can be of type enum memcg_stat_item or node_stat_item */
  880. static inline void inc_memcg_page_state(struct page *page,
  881. int idx)
  882. {
  883. mod_memcg_page_state(page, idx, 1);
  884. }
  885. /* idx can be of type enum memcg_stat_item or node_stat_item */
  886. static inline void dec_memcg_page_state(struct page *page,
  887. int idx)
  888. {
  889. mod_memcg_page_state(page, idx, -1);
  890. }
  891. static inline void inc_lruvec_state(struct lruvec *lruvec,
  892. enum node_stat_item idx)
  893. {
  894. mod_lruvec_state(lruvec, idx, 1);
  895. }
  896. static inline void dec_lruvec_state(struct lruvec *lruvec,
  897. enum node_stat_item idx)
  898. {
  899. mod_lruvec_state(lruvec, idx, -1);
  900. }
  901. static inline void inc_lruvec_page_state(struct page *page,
  902. enum node_stat_item idx)
  903. {
  904. mod_lruvec_page_state(page, idx, 1);
  905. }
  906. static inline void dec_lruvec_page_state(struct page *page,
  907. enum node_stat_item idx)
  908. {
  909. mod_lruvec_page_state(page, idx, -1);
  910. }
  911. #ifdef CONFIG_CGROUP_WRITEBACK
  912. struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
  913. void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
  914. unsigned long *pheadroom, unsigned long *pdirty,
  915. unsigned long *pwriteback);
  916. #else /* CONFIG_CGROUP_WRITEBACK */
  917. static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
  918. {
  919. return NULL;
  920. }
  921. static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
  922. unsigned long *pfilepages,
  923. unsigned long *pheadroom,
  924. unsigned long *pdirty,
  925. unsigned long *pwriteback)
  926. {
  927. }
  928. #endif /* CONFIG_CGROUP_WRITEBACK */
  929. struct sock;
  930. bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
  931. void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
  932. #ifdef CONFIG_MEMCG
  933. extern struct static_key_false memcg_sockets_enabled_key;
  934. #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
  935. void mem_cgroup_sk_alloc(struct sock *sk);
  936. void mem_cgroup_sk_free(struct sock *sk);
  937. static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
  938. {
  939. if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
  940. return true;
  941. do {
  942. if (time_before(jiffies, memcg->socket_pressure))
  943. return true;
  944. } while ((memcg = parent_mem_cgroup(memcg)));
  945. return false;
  946. }
  947. #else
  948. #define mem_cgroup_sockets_enabled 0
  949. static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
  950. static inline void mem_cgroup_sk_free(struct sock *sk) { };
  951. static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
  952. {
  953. return false;
  954. }
  955. #endif
  956. struct kmem_cache *memcg_kmem_get_cache(struct kmem_cache *cachep);
  957. void memcg_kmem_put_cache(struct kmem_cache *cachep);
  958. int memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order,
  959. struct mem_cgroup *memcg);
  960. int memcg_kmem_charge(struct page *page, gfp_t gfp, int order);
  961. void memcg_kmem_uncharge(struct page *page, int order);
  962. #if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
  963. extern struct static_key_false memcg_kmem_enabled_key;
  964. extern struct workqueue_struct *memcg_kmem_cache_wq;
  965. extern int memcg_nr_cache_ids;
  966. void memcg_get_cache_ids(void);
  967. void memcg_put_cache_ids(void);
  968. /*
  969. * Helper macro to loop through all memcg-specific caches. Callers must still
  970. * check if the cache is valid (it is either valid or NULL).
  971. * the slab_mutex must be held when looping through those caches
  972. */
  973. #define for_each_memcg_cache_index(_idx) \
  974. for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
  975. static inline bool memcg_kmem_enabled(void)
  976. {
  977. return static_branch_unlikely(&memcg_kmem_enabled_key);
  978. }
  979. /*
  980. * helper for accessing a memcg's index. It will be used as an index in the
  981. * child cache array in kmem_cache, and also to derive its name. This function
  982. * will return -1 when this is not a kmem-limited memcg.
  983. */
  984. static inline int memcg_cache_id(struct mem_cgroup *memcg)
  985. {
  986. return memcg ? memcg->kmemcg_id : -1;
  987. }
  988. #else
  989. #define for_each_memcg_cache_index(_idx) \
  990. for (; NULL; )
  991. static inline bool memcg_kmem_enabled(void)
  992. {
  993. return false;
  994. }
  995. static inline int memcg_cache_id(struct mem_cgroup *memcg)
  996. {
  997. return -1;
  998. }
  999. static inline void memcg_get_cache_ids(void)
  1000. {
  1001. }
  1002. static inline void memcg_put_cache_ids(void)
  1003. {
  1004. }
  1005. #endif /* CONFIG_MEMCG && !CONFIG_SLOB */
  1006. #endif /* _LINUX_MEMCONTROL_H */