memcontrol.h 29 KB

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