memcontrol-v1.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/memcontrol.h>
  3. #include <linux/swap.h>
  4. #include <linux/mm_inline.h>
  5. #include <linux/pagewalk.h>
  6. #include <linux/backing-dev.h>
  7. #include <linux/swap_cgroup.h>
  8. #include <linux/eventfd.h>
  9. #include <linux/poll.h>
  10. #include <linux/sort.h>
  11. #include <linux/file.h>
  12. #include <linux/seq_buf.h>
  13. #include "internal.h"
  14. #include "swap.h"
  15. #include "memcontrol-v1.h"
  16. /*
  17. * Cgroups above their limits are maintained in a RB-Tree, independent of
  18. * their hierarchy representation
  19. */
  20. struct mem_cgroup_tree_per_node {
  21. struct rb_root rb_root;
  22. struct rb_node *rb_rightmost;
  23. spinlock_t lock;
  24. };
  25. struct mem_cgroup_tree {
  26. struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
  27. };
  28. static struct mem_cgroup_tree soft_limit_tree __read_mostly;
  29. /*
  30. * Maximum loops in mem_cgroup_soft_reclaim(), used for soft
  31. * limit reclaim to prevent infinite loops, if they ever occur.
  32. */
  33. #define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
  34. #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
  35. /* Stuffs for move charges at task migration. */
  36. /*
  37. * Types of charges to be moved.
  38. */
  39. #define MOVE_ANON 0x1ULL
  40. #define MOVE_FILE 0x2ULL
  41. #define MOVE_MASK (MOVE_ANON | MOVE_FILE)
  42. /* "mc" and its members are protected by cgroup_mutex */
  43. static struct move_charge_struct {
  44. spinlock_t lock; /* for from, to */
  45. struct mm_struct *mm;
  46. struct mem_cgroup *from;
  47. struct mem_cgroup *to;
  48. unsigned long flags;
  49. unsigned long precharge;
  50. unsigned long moved_charge;
  51. unsigned long moved_swap;
  52. struct task_struct *moving_task; /* a task moving charges */
  53. wait_queue_head_t waitq; /* a waitq for other context */
  54. } mc = {
  55. .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
  56. .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
  57. };
  58. /* for OOM */
  59. struct mem_cgroup_eventfd_list {
  60. struct list_head list;
  61. struct eventfd_ctx *eventfd;
  62. };
  63. /*
  64. * cgroup_event represents events which userspace want to receive.
  65. */
  66. struct mem_cgroup_event {
  67. /*
  68. * memcg which the event belongs to.
  69. */
  70. struct mem_cgroup *memcg;
  71. /*
  72. * eventfd to signal userspace about the event.
  73. */
  74. struct eventfd_ctx *eventfd;
  75. /*
  76. * Each of these stored in a list by the cgroup.
  77. */
  78. struct list_head list;
  79. /*
  80. * register_event() callback will be used to add new userspace
  81. * waiter for changes related to this event. Use eventfd_signal()
  82. * on eventfd to send notification to userspace.
  83. */
  84. int (*register_event)(struct mem_cgroup *memcg,
  85. struct eventfd_ctx *eventfd, const char *args);
  86. /*
  87. * unregister_event() callback will be called when userspace closes
  88. * the eventfd or on cgroup removing. This callback must be set,
  89. * if you want provide notification functionality.
  90. */
  91. void (*unregister_event)(struct mem_cgroup *memcg,
  92. struct eventfd_ctx *eventfd);
  93. /*
  94. * All fields below needed to unregister event when
  95. * userspace closes eventfd.
  96. */
  97. poll_table pt;
  98. wait_queue_head_t *wqh;
  99. wait_queue_entry_t wait;
  100. struct work_struct remove;
  101. };
  102. #define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
  103. #define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
  104. #define MEMFILE_ATTR(val) ((val) & 0xffff)
  105. enum {
  106. RES_USAGE,
  107. RES_LIMIT,
  108. RES_MAX_USAGE,
  109. RES_FAILCNT,
  110. RES_SOFT_LIMIT,
  111. };
  112. #ifdef CONFIG_LOCKDEP
  113. static struct lockdep_map memcg_oom_lock_dep_map = {
  114. .name = "memcg_oom_lock",
  115. };
  116. #endif
  117. DEFINE_SPINLOCK(memcg_oom_lock);
  118. static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_node *mz,
  119. struct mem_cgroup_tree_per_node *mctz,
  120. unsigned long new_usage_in_excess)
  121. {
  122. struct rb_node **p = &mctz->rb_root.rb_node;
  123. struct rb_node *parent = NULL;
  124. struct mem_cgroup_per_node *mz_node;
  125. bool rightmost = true;
  126. if (mz->on_tree)
  127. return;
  128. mz->usage_in_excess = new_usage_in_excess;
  129. if (!mz->usage_in_excess)
  130. return;
  131. while (*p) {
  132. parent = *p;
  133. mz_node = rb_entry(parent, struct mem_cgroup_per_node,
  134. tree_node);
  135. if (mz->usage_in_excess < mz_node->usage_in_excess) {
  136. p = &(*p)->rb_left;
  137. rightmost = false;
  138. } else {
  139. p = &(*p)->rb_right;
  140. }
  141. }
  142. if (rightmost)
  143. mctz->rb_rightmost = &mz->tree_node;
  144. rb_link_node(&mz->tree_node, parent, p);
  145. rb_insert_color(&mz->tree_node, &mctz->rb_root);
  146. mz->on_tree = true;
  147. }
  148. static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz,
  149. struct mem_cgroup_tree_per_node *mctz)
  150. {
  151. if (!mz->on_tree)
  152. return;
  153. if (&mz->tree_node == mctz->rb_rightmost)
  154. mctz->rb_rightmost = rb_prev(&mz->tree_node);
  155. rb_erase(&mz->tree_node, &mctz->rb_root);
  156. mz->on_tree = false;
  157. }
  158. static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz,
  159. struct mem_cgroup_tree_per_node *mctz)
  160. {
  161. unsigned long flags;
  162. spin_lock_irqsave(&mctz->lock, flags);
  163. __mem_cgroup_remove_exceeded(mz, mctz);
  164. spin_unlock_irqrestore(&mctz->lock, flags);
  165. }
  166. static unsigned long soft_limit_excess(struct mem_cgroup *memcg)
  167. {
  168. unsigned long nr_pages = page_counter_read(&memcg->memory);
  169. unsigned long soft_limit = READ_ONCE(memcg->soft_limit);
  170. unsigned long excess = 0;
  171. if (nr_pages > soft_limit)
  172. excess = nr_pages - soft_limit;
  173. return excess;
  174. }
  175. static void memcg1_update_tree(struct mem_cgroup *memcg, int nid)
  176. {
  177. unsigned long excess;
  178. struct mem_cgroup_per_node *mz;
  179. struct mem_cgroup_tree_per_node *mctz;
  180. if (lru_gen_enabled()) {
  181. if (soft_limit_excess(memcg))
  182. lru_gen_soft_reclaim(memcg, nid);
  183. return;
  184. }
  185. mctz = soft_limit_tree.rb_tree_per_node[nid];
  186. if (!mctz)
  187. return;
  188. /*
  189. * Necessary to update all ancestors when hierarchy is used.
  190. * because their event counter is not touched.
  191. */
  192. for (; memcg; memcg = parent_mem_cgroup(memcg)) {
  193. mz = memcg->nodeinfo[nid];
  194. excess = soft_limit_excess(memcg);
  195. /*
  196. * We have to update the tree if mz is on RB-tree or
  197. * mem is over its softlimit.
  198. */
  199. if (excess || mz->on_tree) {
  200. unsigned long flags;
  201. spin_lock_irqsave(&mctz->lock, flags);
  202. /* if on-tree, remove it */
  203. if (mz->on_tree)
  204. __mem_cgroup_remove_exceeded(mz, mctz);
  205. /*
  206. * Insert again. mz->usage_in_excess will be updated.
  207. * If excess is 0, no tree ops.
  208. */
  209. __mem_cgroup_insert_exceeded(mz, mctz, excess);
  210. spin_unlock_irqrestore(&mctz->lock, flags);
  211. }
  212. }
  213. }
  214. void memcg1_remove_from_trees(struct mem_cgroup *memcg)
  215. {
  216. struct mem_cgroup_tree_per_node *mctz;
  217. struct mem_cgroup_per_node *mz;
  218. int nid;
  219. for_each_node(nid) {
  220. mz = memcg->nodeinfo[nid];
  221. mctz = soft_limit_tree.rb_tree_per_node[nid];
  222. if (mctz)
  223. mem_cgroup_remove_exceeded(mz, mctz);
  224. }
  225. }
  226. static struct mem_cgroup_per_node *
  227. __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
  228. {
  229. struct mem_cgroup_per_node *mz;
  230. retry:
  231. mz = NULL;
  232. if (!mctz->rb_rightmost)
  233. goto done; /* Nothing to reclaim from */
  234. mz = rb_entry(mctz->rb_rightmost,
  235. struct mem_cgroup_per_node, tree_node);
  236. /*
  237. * Remove the node now but someone else can add it back,
  238. * we will to add it back at the end of reclaim to its correct
  239. * position in the tree.
  240. */
  241. __mem_cgroup_remove_exceeded(mz, mctz);
  242. if (!soft_limit_excess(mz->memcg) ||
  243. !css_tryget(&mz->memcg->css))
  244. goto retry;
  245. done:
  246. return mz;
  247. }
  248. static struct mem_cgroup_per_node *
  249. mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
  250. {
  251. struct mem_cgroup_per_node *mz;
  252. spin_lock_irq(&mctz->lock);
  253. mz = __mem_cgroup_largest_soft_limit_node(mctz);
  254. spin_unlock_irq(&mctz->lock);
  255. return mz;
  256. }
  257. static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
  258. pg_data_t *pgdat,
  259. gfp_t gfp_mask,
  260. unsigned long *total_scanned)
  261. {
  262. struct mem_cgroup *victim = NULL;
  263. int total = 0;
  264. int loop = 0;
  265. unsigned long excess;
  266. unsigned long nr_scanned;
  267. struct mem_cgroup_reclaim_cookie reclaim = {
  268. .pgdat = pgdat,
  269. };
  270. excess = soft_limit_excess(root_memcg);
  271. while (1) {
  272. victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
  273. if (!victim) {
  274. loop++;
  275. if (loop >= 2) {
  276. /*
  277. * If we have not been able to reclaim
  278. * anything, it might because there are
  279. * no reclaimable pages under this hierarchy
  280. */
  281. if (!total)
  282. break;
  283. /*
  284. * We want to do more targeted reclaim.
  285. * excess >> 2 is not to excessive so as to
  286. * reclaim too much, nor too less that we keep
  287. * coming back to reclaim from this cgroup
  288. */
  289. if (total >= (excess >> 2) ||
  290. (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
  291. break;
  292. }
  293. continue;
  294. }
  295. total += mem_cgroup_shrink_node(victim, gfp_mask, false,
  296. pgdat, &nr_scanned);
  297. *total_scanned += nr_scanned;
  298. if (!soft_limit_excess(root_memcg))
  299. break;
  300. }
  301. mem_cgroup_iter_break(root_memcg, victim);
  302. return total;
  303. }
  304. unsigned long memcg1_soft_limit_reclaim(pg_data_t *pgdat, int order,
  305. gfp_t gfp_mask,
  306. unsigned long *total_scanned)
  307. {
  308. unsigned long nr_reclaimed = 0;
  309. struct mem_cgroup_per_node *mz, *next_mz = NULL;
  310. unsigned long reclaimed;
  311. int loop = 0;
  312. struct mem_cgroup_tree_per_node *mctz;
  313. unsigned long excess;
  314. if (lru_gen_enabled())
  315. return 0;
  316. if (order > 0)
  317. return 0;
  318. mctz = soft_limit_tree.rb_tree_per_node[pgdat->node_id];
  319. /*
  320. * Do not even bother to check the largest node if the root
  321. * is empty. Do it lockless to prevent lock bouncing. Races
  322. * are acceptable as soft limit is best effort anyway.
  323. */
  324. if (!mctz || RB_EMPTY_ROOT(&mctz->rb_root))
  325. return 0;
  326. /*
  327. * This loop can run a while, specially if mem_cgroup's continuously
  328. * keep exceeding their soft limit and putting the system under
  329. * pressure
  330. */
  331. do {
  332. if (next_mz)
  333. mz = next_mz;
  334. else
  335. mz = mem_cgroup_largest_soft_limit_node(mctz);
  336. if (!mz)
  337. break;
  338. reclaimed = mem_cgroup_soft_reclaim(mz->memcg, pgdat,
  339. gfp_mask, total_scanned);
  340. nr_reclaimed += reclaimed;
  341. spin_lock_irq(&mctz->lock);
  342. /*
  343. * If we failed to reclaim anything from this memory cgroup
  344. * it is time to move on to the next cgroup
  345. */
  346. next_mz = NULL;
  347. if (!reclaimed)
  348. next_mz = __mem_cgroup_largest_soft_limit_node(mctz);
  349. excess = soft_limit_excess(mz->memcg);
  350. /*
  351. * One school of thought says that we should not add
  352. * back the node to the tree if reclaim returns 0.
  353. * But our reclaim could return 0, simply because due
  354. * to priority we are exposing a smaller subset of
  355. * memory to reclaim from. Consider this as a longer
  356. * term TODO.
  357. */
  358. /* If excess == 0, no tree ops */
  359. __mem_cgroup_insert_exceeded(mz, mctz, excess);
  360. spin_unlock_irq(&mctz->lock);
  361. css_put(&mz->memcg->css);
  362. loop++;
  363. /*
  364. * Could not reclaim anything and there are no more
  365. * mem cgroups to try or we seem to be looping without
  366. * reclaiming anything.
  367. */
  368. if (!nr_reclaimed &&
  369. (next_mz == NULL ||
  370. loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
  371. break;
  372. } while (!nr_reclaimed);
  373. if (next_mz)
  374. css_put(&next_mz->memcg->css);
  375. return nr_reclaimed;
  376. }
  377. /*
  378. * A routine for checking "mem" is under move_account() or not.
  379. *
  380. * Checking a cgroup is mc.from or mc.to or under hierarchy of
  381. * moving cgroups. This is for waiting at high-memory pressure
  382. * caused by "move".
  383. */
  384. static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
  385. {
  386. struct mem_cgroup *from;
  387. struct mem_cgroup *to;
  388. bool ret = false;
  389. /*
  390. * Unlike task_move routines, we access mc.to, mc.from not under
  391. * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
  392. */
  393. spin_lock(&mc.lock);
  394. from = mc.from;
  395. to = mc.to;
  396. if (!from)
  397. goto unlock;
  398. ret = mem_cgroup_is_descendant(from, memcg) ||
  399. mem_cgroup_is_descendant(to, memcg);
  400. unlock:
  401. spin_unlock(&mc.lock);
  402. return ret;
  403. }
  404. bool memcg1_wait_acct_move(struct mem_cgroup *memcg)
  405. {
  406. if (mc.moving_task && current != mc.moving_task) {
  407. if (mem_cgroup_under_move(memcg)) {
  408. DEFINE_WAIT(wait);
  409. prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
  410. /* moving charge context might have finished. */
  411. if (mc.moving_task)
  412. schedule();
  413. finish_wait(&mc.waitq, &wait);
  414. return true;
  415. }
  416. }
  417. return false;
  418. }
  419. /**
  420. * folio_memcg_lock - Bind a folio to its memcg.
  421. * @folio: The folio.
  422. *
  423. * This function prevents unlocked LRU folios from being moved to
  424. * another cgroup.
  425. *
  426. * It ensures lifetime of the bound memcg. The caller is responsible
  427. * for the lifetime of the folio.
  428. */
  429. void folio_memcg_lock(struct folio *folio)
  430. {
  431. struct mem_cgroup *memcg;
  432. unsigned long flags;
  433. /*
  434. * The RCU lock is held throughout the transaction. The fast
  435. * path can get away without acquiring the memcg->move_lock
  436. * because page moving starts with an RCU grace period.
  437. */
  438. rcu_read_lock();
  439. if (mem_cgroup_disabled())
  440. return;
  441. again:
  442. memcg = folio_memcg(folio);
  443. if (unlikely(!memcg))
  444. return;
  445. #ifdef CONFIG_PROVE_LOCKING
  446. local_irq_save(flags);
  447. might_lock(&memcg->move_lock);
  448. local_irq_restore(flags);
  449. #endif
  450. if (atomic_read(&memcg->moving_account) <= 0)
  451. return;
  452. spin_lock_irqsave(&memcg->move_lock, flags);
  453. if (memcg != folio_memcg(folio)) {
  454. spin_unlock_irqrestore(&memcg->move_lock, flags);
  455. goto again;
  456. }
  457. /*
  458. * When charge migration first begins, we can have multiple
  459. * critical sections holding the fast-path RCU lock and one
  460. * holding the slowpath move_lock. Track the task who has the
  461. * move_lock for folio_memcg_unlock().
  462. */
  463. memcg->move_lock_task = current;
  464. memcg->move_lock_flags = flags;
  465. }
  466. static void __folio_memcg_unlock(struct mem_cgroup *memcg)
  467. {
  468. if (memcg && memcg->move_lock_task == current) {
  469. unsigned long flags = memcg->move_lock_flags;
  470. memcg->move_lock_task = NULL;
  471. memcg->move_lock_flags = 0;
  472. spin_unlock_irqrestore(&memcg->move_lock, flags);
  473. }
  474. rcu_read_unlock();
  475. }
  476. /**
  477. * folio_memcg_unlock - Release the binding between a folio and its memcg.
  478. * @folio: The folio.
  479. *
  480. * This releases the binding created by folio_memcg_lock(). This does
  481. * not change the accounting of this folio to its memcg, but it does
  482. * permit others to change it.
  483. */
  484. void folio_memcg_unlock(struct folio *folio)
  485. {
  486. __folio_memcg_unlock(folio_memcg(folio));
  487. }
  488. #ifdef CONFIG_SWAP
  489. /**
  490. * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
  491. * @entry: swap entry to be moved
  492. * @from: mem_cgroup which the entry is moved from
  493. * @to: mem_cgroup which the entry is moved to
  494. *
  495. * It succeeds only when the swap_cgroup's record for this entry is the same
  496. * as the mem_cgroup's id of @from.
  497. *
  498. * Returns 0 on success, -EINVAL on failure.
  499. *
  500. * The caller must have charged to @to, IOW, called page_counter_charge() about
  501. * both res and memsw, and called css_get().
  502. */
  503. static int mem_cgroup_move_swap_account(swp_entry_t entry,
  504. struct mem_cgroup *from, struct mem_cgroup *to)
  505. {
  506. unsigned short old_id, new_id;
  507. old_id = mem_cgroup_id(from);
  508. new_id = mem_cgroup_id(to);
  509. if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
  510. mod_memcg_state(from, MEMCG_SWAP, -1);
  511. mod_memcg_state(to, MEMCG_SWAP, 1);
  512. return 0;
  513. }
  514. return -EINVAL;
  515. }
  516. #else
  517. static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
  518. struct mem_cgroup *from, struct mem_cgroup *to)
  519. {
  520. return -EINVAL;
  521. }
  522. #endif
  523. static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
  524. struct cftype *cft)
  525. {
  526. return mem_cgroup_from_css(css)->move_charge_at_immigrate;
  527. }
  528. #ifdef CONFIG_MMU
  529. static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
  530. struct cftype *cft, u64 val)
  531. {
  532. struct mem_cgroup *memcg = mem_cgroup_from_css(css);
  533. pr_warn_once("Cgroup memory moving (move_charge_at_immigrate) is deprecated. "
  534. "Please report your usecase to linux-mm@kvack.org if you "
  535. "depend on this functionality.\n");
  536. if (val & ~MOVE_MASK)
  537. return -EINVAL;
  538. /*
  539. * No kind of locking is needed in here, because ->can_attach() will
  540. * check this value once in the beginning of the process, and then carry
  541. * on with stale data. This means that changes to this value will only
  542. * affect task migrations starting after the change.
  543. */
  544. memcg->move_charge_at_immigrate = val;
  545. return 0;
  546. }
  547. #else
  548. static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
  549. struct cftype *cft, u64 val)
  550. {
  551. return -ENOSYS;
  552. }
  553. #endif
  554. #ifdef CONFIG_MMU
  555. /* Handlers for move charge at task migration. */
  556. static int mem_cgroup_do_precharge(unsigned long count)
  557. {
  558. int ret;
  559. /* Try a single bulk charge without reclaim first, kswapd may wake */
  560. ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_DIRECT_RECLAIM, count);
  561. if (!ret) {
  562. mc.precharge += count;
  563. return ret;
  564. }
  565. /* Try charges one by one with reclaim, but do not retry */
  566. while (count--) {
  567. ret = try_charge(mc.to, GFP_KERNEL | __GFP_NORETRY, 1);
  568. if (ret)
  569. return ret;
  570. mc.precharge++;
  571. cond_resched();
  572. }
  573. return 0;
  574. }
  575. union mc_target {
  576. struct folio *folio;
  577. swp_entry_t ent;
  578. };
  579. enum mc_target_type {
  580. MC_TARGET_NONE = 0,
  581. MC_TARGET_PAGE,
  582. MC_TARGET_SWAP,
  583. MC_TARGET_DEVICE,
  584. };
  585. static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
  586. unsigned long addr, pte_t ptent)
  587. {
  588. struct page *page = vm_normal_page(vma, addr, ptent);
  589. if (!page)
  590. return NULL;
  591. if (PageAnon(page)) {
  592. if (!(mc.flags & MOVE_ANON))
  593. return NULL;
  594. } else {
  595. if (!(mc.flags & MOVE_FILE))
  596. return NULL;
  597. }
  598. get_page(page);
  599. return page;
  600. }
  601. #if defined(CONFIG_SWAP) || defined(CONFIG_DEVICE_PRIVATE)
  602. static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
  603. pte_t ptent, swp_entry_t *entry)
  604. {
  605. struct page *page = NULL;
  606. swp_entry_t ent = pte_to_swp_entry(ptent);
  607. if (!(mc.flags & MOVE_ANON))
  608. return NULL;
  609. /*
  610. * Handle device private pages that are not accessible by the CPU, but
  611. * stored as special swap entries in the page table.
  612. */
  613. if (is_device_private_entry(ent)) {
  614. page = pfn_swap_entry_to_page(ent);
  615. if (!get_page_unless_zero(page))
  616. return NULL;
  617. return page;
  618. }
  619. if (non_swap_entry(ent))
  620. return NULL;
  621. /*
  622. * Because swap_cache_get_folio() updates some statistics counter,
  623. * we call find_get_page() with swapper_space directly.
  624. */
  625. page = find_get_page(swap_address_space(ent), swap_cache_index(ent));
  626. entry->val = ent.val;
  627. return page;
  628. }
  629. #else
  630. static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
  631. pte_t ptent, swp_entry_t *entry)
  632. {
  633. return NULL;
  634. }
  635. #endif
  636. static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
  637. unsigned long addr, pte_t ptent)
  638. {
  639. unsigned long index;
  640. struct folio *folio;
  641. if (!vma->vm_file) /* anonymous vma */
  642. return NULL;
  643. if (!(mc.flags & MOVE_FILE))
  644. return NULL;
  645. /* folio is moved even if it's not RSS of this task(page-faulted). */
  646. /* shmem/tmpfs may report page out on swap: account for that too. */
  647. index = linear_page_index(vma, addr);
  648. folio = filemap_get_incore_folio(vma->vm_file->f_mapping, index);
  649. if (IS_ERR(folio))
  650. return NULL;
  651. return folio_file_page(folio, index);
  652. }
  653. static void memcg1_check_events(struct mem_cgroup *memcg, int nid);
  654. static void memcg1_charge_statistics(struct mem_cgroup *memcg, int nr_pages);
  655. /**
  656. * mem_cgroup_move_account - move account of the folio
  657. * @folio: The folio.
  658. * @compound: charge the page as compound or small page
  659. * @from: mem_cgroup which the folio is moved from.
  660. * @to: mem_cgroup which the folio is moved to. @from != @to.
  661. *
  662. * The folio must be locked and not on the LRU.
  663. *
  664. * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
  665. * from old cgroup.
  666. */
  667. static int mem_cgroup_move_account(struct folio *folio,
  668. bool compound,
  669. struct mem_cgroup *from,
  670. struct mem_cgroup *to)
  671. {
  672. struct lruvec *from_vec, *to_vec;
  673. struct pglist_data *pgdat;
  674. unsigned int nr_pages = compound ? folio_nr_pages(folio) : 1;
  675. int nid, ret;
  676. VM_BUG_ON(from == to);
  677. VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
  678. VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
  679. VM_BUG_ON(compound && !folio_test_large(folio));
  680. ret = -EINVAL;
  681. if (folio_memcg(folio) != from)
  682. goto out;
  683. pgdat = folio_pgdat(folio);
  684. from_vec = mem_cgroup_lruvec(from, pgdat);
  685. to_vec = mem_cgroup_lruvec(to, pgdat);
  686. folio_memcg_lock(folio);
  687. if (folio_test_anon(folio)) {
  688. if (folio_mapped(folio)) {
  689. __mod_lruvec_state(from_vec, NR_ANON_MAPPED, -nr_pages);
  690. __mod_lruvec_state(to_vec, NR_ANON_MAPPED, nr_pages);
  691. if (folio_test_pmd_mappable(folio)) {
  692. __mod_lruvec_state(from_vec, NR_ANON_THPS,
  693. -nr_pages);
  694. __mod_lruvec_state(to_vec, NR_ANON_THPS,
  695. nr_pages);
  696. }
  697. }
  698. } else {
  699. __mod_lruvec_state(from_vec, NR_FILE_PAGES, -nr_pages);
  700. __mod_lruvec_state(to_vec, NR_FILE_PAGES, nr_pages);
  701. if (folio_test_swapbacked(folio)) {
  702. __mod_lruvec_state(from_vec, NR_SHMEM, -nr_pages);
  703. __mod_lruvec_state(to_vec, NR_SHMEM, nr_pages);
  704. }
  705. if (folio_mapped(folio)) {
  706. __mod_lruvec_state(from_vec, NR_FILE_MAPPED, -nr_pages);
  707. __mod_lruvec_state(to_vec, NR_FILE_MAPPED, nr_pages);
  708. }
  709. if (folio_test_dirty(folio)) {
  710. struct address_space *mapping = folio_mapping(folio);
  711. if (mapping_can_writeback(mapping)) {
  712. __mod_lruvec_state(from_vec, NR_FILE_DIRTY,
  713. -nr_pages);
  714. __mod_lruvec_state(to_vec, NR_FILE_DIRTY,
  715. nr_pages);
  716. }
  717. }
  718. }
  719. #ifdef CONFIG_SWAP
  720. if (folio_test_swapcache(folio)) {
  721. __mod_lruvec_state(from_vec, NR_SWAPCACHE, -nr_pages);
  722. __mod_lruvec_state(to_vec, NR_SWAPCACHE, nr_pages);
  723. }
  724. #endif
  725. if (folio_test_writeback(folio)) {
  726. __mod_lruvec_state(from_vec, NR_WRITEBACK, -nr_pages);
  727. __mod_lruvec_state(to_vec, NR_WRITEBACK, nr_pages);
  728. }
  729. /*
  730. * All state has been migrated, let's switch to the new memcg.
  731. *
  732. * It is safe to change page's memcg here because the page
  733. * is referenced, charged, isolated, and locked: we can't race
  734. * with (un)charging, migration, LRU putback, or anything else
  735. * that would rely on a stable page's memory cgroup.
  736. *
  737. * Note that folio_memcg_lock is a memcg lock, not a page lock,
  738. * to save space. As soon as we switch page's memory cgroup to a
  739. * new memcg that isn't locked, the above state can change
  740. * concurrently again. Make sure we're truly done with it.
  741. */
  742. smp_mb();
  743. css_get(&to->css);
  744. css_put(&from->css);
  745. /* Warning should never happen, so don't worry about refcount non-0 */
  746. WARN_ON_ONCE(folio_unqueue_deferred_split(folio));
  747. folio->memcg_data = (unsigned long)to;
  748. __folio_memcg_unlock(from);
  749. ret = 0;
  750. nid = folio_nid(folio);
  751. local_irq_disable();
  752. memcg1_charge_statistics(to, nr_pages);
  753. memcg1_check_events(to, nid);
  754. memcg1_charge_statistics(from, -nr_pages);
  755. memcg1_check_events(from, nid);
  756. local_irq_enable();
  757. out:
  758. return ret;
  759. }
  760. /**
  761. * get_mctgt_type - get target type of moving charge
  762. * @vma: the vma the pte to be checked belongs
  763. * @addr: the address corresponding to the pte to be checked
  764. * @ptent: the pte to be checked
  765. * @target: the pointer the target page or swap ent will be stored(can be NULL)
  766. *
  767. * Context: Called with pte lock held.
  768. * Return:
  769. * * MC_TARGET_NONE - If the pte is not a target for move charge.
  770. * * MC_TARGET_PAGE - If the page corresponding to this pte is a target for
  771. * move charge. If @target is not NULL, the folio is stored in target->folio
  772. * with extra refcnt taken (Caller should release it).
  773. * * MC_TARGET_SWAP - If the swap entry corresponding to this pte is a
  774. * target for charge migration. If @target is not NULL, the entry is
  775. * stored in target->ent.
  776. * * MC_TARGET_DEVICE - Like MC_TARGET_PAGE but page is device memory and
  777. * thus not on the lru. For now such page is charged like a regular page
  778. * would be as it is just special memory taking the place of a regular page.
  779. * See Documentations/vm/hmm.txt and include/linux/hmm.h
  780. */
  781. static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
  782. unsigned long addr, pte_t ptent, union mc_target *target)
  783. {
  784. struct page *page = NULL;
  785. struct folio *folio;
  786. enum mc_target_type ret = MC_TARGET_NONE;
  787. swp_entry_t ent = { .val = 0 };
  788. if (pte_present(ptent))
  789. page = mc_handle_present_pte(vma, addr, ptent);
  790. else if (pte_none_mostly(ptent))
  791. /*
  792. * PTE markers should be treated as a none pte here, separated
  793. * from other swap handling below.
  794. */
  795. page = mc_handle_file_pte(vma, addr, ptent);
  796. else if (is_swap_pte(ptent))
  797. page = mc_handle_swap_pte(vma, ptent, &ent);
  798. if (page)
  799. folio = page_folio(page);
  800. if (target && page) {
  801. if (!folio_trylock(folio)) {
  802. folio_put(folio);
  803. return ret;
  804. }
  805. /*
  806. * page_mapped() must be stable during the move. This
  807. * pte is locked, so if it's present, the page cannot
  808. * become unmapped. If it isn't, we have only partial
  809. * control over the mapped state: the page lock will
  810. * prevent new faults against pagecache and swapcache,
  811. * so an unmapped page cannot become mapped. However,
  812. * if the page is already mapped elsewhere, it can
  813. * unmap, and there is nothing we can do about it.
  814. * Alas, skip moving the page in this case.
  815. */
  816. if (!pte_present(ptent) && page_mapped(page)) {
  817. folio_unlock(folio);
  818. folio_put(folio);
  819. return ret;
  820. }
  821. }
  822. if (!page && !ent.val)
  823. return ret;
  824. if (page) {
  825. /*
  826. * Do only loose check w/o serialization.
  827. * mem_cgroup_move_account() checks the page is valid or
  828. * not under LRU exclusion.
  829. */
  830. if (folio_memcg(folio) == mc.from) {
  831. ret = MC_TARGET_PAGE;
  832. if (folio_is_device_private(folio) ||
  833. folio_is_device_coherent(folio))
  834. ret = MC_TARGET_DEVICE;
  835. if (target)
  836. target->folio = folio;
  837. }
  838. if (!ret || !target) {
  839. if (target)
  840. folio_unlock(folio);
  841. folio_put(folio);
  842. }
  843. }
  844. /*
  845. * There is a swap entry and a page doesn't exist or isn't charged.
  846. * But we cannot move a tail-page in a THP.
  847. */
  848. if (ent.val && !ret && (!page || !PageTransCompound(page)) &&
  849. mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
  850. ret = MC_TARGET_SWAP;
  851. if (target)
  852. target->ent = ent;
  853. }
  854. return ret;
  855. }
  856. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  857. /*
  858. * We don't consider PMD mapped swapping or file mapped pages because THP does
  859. * not support them for now.
  860. * Caller should make sure that pmd_trans_huge(pmd) is true.
  861. */
  862. static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
  863. unsigned long addr, pmd_t pmd, union mc_target *target)
  864. {
  865. struct page *page = NULL;
  866. struct folio *folio;
  867. enum mc_target_type ret = MC_TARGET_NONE;
  868. if (unlikely(is_swap_pmd(pmd))) {
  869. VM_BUG_ON(thp_migration_supported() &&
  870. !is_pmd_migration_entry(pmd));
  871. return ret;
  872. }
  873. page = pmd_page(pmd);
  874. VM_BUG_ON_PAGE(!page || !PageHead(page), page);
  875. folio = page_folio(page);
  876. if (!(mc.flags & MOVE_ANON))
  877. return ret;
  878. if (folio_memcg(folio) == mc.from) {
  879. ret = MC_TARGET_PAGE;
  880. if (target) {
  881. folio_get(folio);
  882. if (!folio_trylock(folio)) {
  883. folio_put(folio);
  884. return MC_TARGET_NONE;
  885. }
  886. target->folio = folio;
  887. }
  888. }
  889. return ret;
  890. }
  891. #else
  892. static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
  893. unsigned long addr, pmd_t pmd, union mc_target *target)
  894. {
  895. return MC_TARGET_NONE;
  896. }
  897. #endif
  898. static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
  899. unsigned long addr, unsigned long end,
  900. struct mm_walk *walk)
  901. {
  902. struct vm_area_struct *vma = walk->vma;
  903. pte_t *pte;
  904. spinlock_t *ptl;
  905. ptl = pmd_trans_huge_lock(pmd, vma);
  906. if (ptl) {
  907. /*
  908. * Note their can not be MC_TARGET_DEVICE for now as we do not
  909. * support transparent huge page with MEMORY_DEVICE_PRIVATE but
  910. * this might change.
  911. */
  912. if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
  913. mc.precharge += HPAGE_PMD_NR;
  914. spin_unlock(ptl);
  915. return 0;
  916. }
  917. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  918. if (!pte)
  919. return 0;
  920. for (; addr != end; pte++, addr += PAGE_SIZE)
  921. if (get_mctgt_type(vma, addr, ptep_get(pte), NULL))
  922. mc.precharge++; /* increment precharge temporarily */
  923. pte_unmap_unlock(pte - 1, ptl);
  924. cond_resched();
  925. return 0;
  926. }
  927. static const struct mm_walk_ops precharge_walk_ops = {
  928. .pmd_entry = mem_cgroup_count_precharge_pte_range,
  929. .walk_lock = PGWALK_RDLOCK,
  930. };
  931. static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
  932. {
  933. unsigned long precharge;
  934. mmap_read_lock(mm);
  935. walk_page_range(mm, 0, ULONG_MAX, &precharge_walk_ops, NULL);
  936. mmap_read_unlock(mm);
  937. precharge = mc.precharge;
  938. mc.precharge = 0;
  939. return precharge;
  940. }
  941. static int mem_cgroup_precharge_mc(struct mm_struct *mm)
  942. {
  943. unsigned long precharge = mem_cgroup_count_precharge(mm);
  944. VM_BUG_ON(mc.moving_task);
  945. mc.moving_task = current;
  946. return mem_cgroup_do_precharge(precharge);
  947. }
  948. /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
  949. static void __mem_cgroup_clear_mc(void)
  950. {
  951. struct mem_cgroup *from = mc.from;
  952. struct mem_cgroup *to = mc.to;
  953. /* we must uncharge all the leftover precharges from mc.to */
  954. if (mc.precharge) {
  955. mem_cgroup_cancel_charge(mc.to, mc.precharge);
  956. mc.precharge = 0;
  957. }
  958. /*
  959. * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
  960. * we must uncharge here.
  961. */
  962. if (mc.moved_charge) {
  963. mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
  964. mc.moved_charge = 0;
  965. }
  966. /* we must fixup refcnts and charges */
  967. if (mc.moved_swap) {
  968. /* uncharge swap account from the old cgroup */
  969. if (!mem_cgroup_is_root(mc.from))
  970. page_counter_uncharge(&mc.from->memsw, mc.moved_swap);
  971. mem_cgroup_id_put_many(mc.from, mc.moved_swap);
  972. /*
  973. * we charged both to->memory and to->memsw, so we
  974. * should uncharge to->memory.
  975. */
  976. if (!mem_cgroup_is_root(mc.to))
  977. page_counter_uncharge(&mc.to->memory, mc.moved_swap);
  978. mc.moved_swap = 0;
  979. }
  980. memcg1_oom_recover(from);
  981. memcg1_oom_recover(to);
  982. wake_up_all(&mc.waitq);
  983. }
  984. static void mem_cgroup_clear_mc(void)
  985. {
  986. struct mm_struct *mm = mc.mm;
  987. /*
  988. * we must clear moving_task before waking up waiters at the end of
  989. * task migration.
  990. */
  991. mc.moving_task = NULL;
  992. __mem_cgroup_clear_mc();
  993. spin_lock(&mc.lock);
  994. mc.from = NULL;
  995. mc.to = NULL;
  996. mc.mm = NULL;
  997. spin_unlock(&mc.lock);
  998. mmput(mm);
  999. }
  1000. int memcg1_can_attach(struct cgroup_taskset *tset)
  1001. {
  1002. struct cgroup_subsys_state *css;
  1003. struct mem_cgroup *memcg = NULL; /* unneeded init to make gcc happy */
  1004. struct mem_cgroup *from;
  1005. struct task_struct *leader, *p;
  1006. struct mm_struct *mm;
  1007. unsigned long move_flags;
  1008. int ret = 0;
  1009. /* charge immigration isn't supported on the default hierarchy */
  1010. if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
  1011. return 0;
  1012. /*
  1013. * Multi-process migrations only happen on the default hierarchy
  1014. * where charge immigration is not used. Perform charge
  1015. * immigration if @tset contains a leader and whine if there are
  1016. * multiple.
  1017. */
  1018. p = NULL;
  1019. cgroup_taskset_for_each_leader(leader, css, tset) {
  1020. WARN_ON_ONCE(p);
  1021. p = leader;
  1022. memcg = mem_cgroup_from_css(css);
  1023. }
  1024. if (!p)
  1025. return 0;
  1026. /*
  1027. * We are now committed to this value whatever it is. Changes in this
  1028. * tunable will only affect upcoming migrations, not the current one.
  1029. * So we need to save it, and keep it going.
  1030. */
  1031. move_flags = READ_ONCE(memcg->move_charge_at_immigrate);
  1032. if (!move_flags)
  1033. return 0;
  1034. from = mem_cgroup_from_task(p);
  1035. VM_BUG_ON(from == memcg);
  1036. mm = get_task_mm(p);
  1037. if (!mm)
  1038. return 0;
  1039. /* We move charges only when we move a owner of the mm */
  1040. if (mm->owner == p) {
  1041. VM_BUG_ON(mc.from);
  1042. VM_BUG_ON(mc.to);
  1043. VM_BUG_ON(mc.precharge);
  1044. VM_BUG_ON(mc.moved_charge);
  1045. VM_BUG_ON(mc.moved_swap);
  1046. spin_lock(&mc.lock);
  1047. mc.mm = mm;
  1048. mc.from = from;
  1049. mc.to = memcg;
  1050. mc.flags = move_flags;
  1051. spin_unlock(&mc.lock);
  1052. /* We set mc.moving_task later */
  1053. ret = mem_cgroup_precharge_mc(mm);
  1054. if (ret)
  1055. mem_cgroup_clear_mc();
  1056. } else {
  1057. mmput(mm);
  1058. }
  1059. return ret;
  1060. }
  1061. void memcg1_cancel_attach(struct cgroup_taskset *tset)
  1062. {
  1063. if (mc.to)
  1064. mem_cgroup_clear_mc();
  1065. }
  1066. static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
  1067. unsigned long addr, unsigned long end,
  1068. struct mm_walk *walk)
  1069. {
  1070. int ret = 0;
  1071. struct vm_area_struct *vma = walk->vma;
  1072. pte_t *pte;
  1073. spinlock_t *ptl;
  1074. enum mc_target_type target_type;
  1075. union mc_target target;
  1076. struct folio *folio;
  1077. bool tried_split_before = false;
  1078. retry_pmd:
  1079. ptl = pmd_trans_huge_lock(pmd, vma);
  1080. if (ptl) {
  1081. if (mc.precharge < HPAGE_PMD_NR) {
  1082. spin_unlock(ptl);
  1083. return 0;
  1084. }
  1085. target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
  1086. if (target_type == MC_TARGET_PAGE) {
  1087. folio = target.folio;
  1088. /*
  1089. * Deferred split queue locking depends on memcg,
  1090. * and unqueue is unsafe unless folio refcount is 0:
  1091. * split or skip if on the queue? first try to split.
  1092. */
  1093. if (!list_empty(&folio->_deferred_list)) {
  1094. spin_unlock(ptl);
  1095. if (!tried_split_before)
  1096. split_folio(folio);
  1097. folio_unlock(folio);
  1098. folio_put(folio);
  1099. if (tried_split_before)
  1100. return 0;
  1101. tried_split_before = true;
  1102. goto retry_pmd;
  1103. }
  1104. /*
  1105. * So long as that pmd lock is held, the folio cannot
  1106. * be racily added to the _deferred_list, because
  1107. * __folio_remove_rmap() will find !partially_mapped.
  1108. */
  1109. if (folio_isolate_lru(folio)) {
  1110. if (!mem_cgroup_move_account(folio, true,
  1111. mc.from, mc.to)) {
  1112. mc.precharge -= HPAGE_PMD_NR;
  1113. mc.moved_charge += HPAGE_PMD_NR;
  1114. }
  1115. folio_putback_lru(folio);
  1116. }
  1117. folio_unlock(folio);
  1118. folio_put(folio);
  1119. } else if (target_type == MC_TARGET_DEVICE) {
  1120. folio = target.folio;
  1121. if (!mem_cgroup_move_account(folio, true,
  1122. mc.from, mc.to)) {
  1123. mc.precharge -= HPAGE_PMD_NR;
  1124. mc.moved_charge += HPAGE_PMD_NR;
  1125. }
  1126. folio_unlock(folio);
  1127. folio_put(folio);
  1128. }
  1129. spin_unlock(ptl);
  1130. return 0;
  1131. }
  1132. retry:
  1133. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  1134. if (!pte)
  1135. return 0;
  1136. for (; addr != end; addr += PAGE_SIZE) {
  1137. pte_t ptent = ptep_get(pte++);
  1138. bool device = false;
  1139. swp_entry_t ent;
  1140. if (!mc.precharge)
  1141. break;
  1142. switch (get_mctgt_type(vma, addr, ptent, &target)) {
  1143. case MC_TARGET_DEVICE:
  1144. device = true;
  1145. fallthrough;
  1146. case MC_TARGET_PAGE:
  1147. folio = target.folio;
  1148. /*
  1149. * We can have a part of the split pmd here. Moving it
  1150. * can be done but it would be too convoluted so simply
  1151. * ignore such a partial THP and keep it in original
  1152. * memcg. There should be somebody mapping the head.
  1153. */
  1154. if (folio_test_large(folio))
  1155. goto put;
  1156. if (!device && !folio_isolate_lru(folio))
  1157. goto put;
  1158. if (!mem_cgroup_move_account(folio, false,
  1159. mc.from, mc.to)) {
  1160. mc.precharge--;
  1161. /* we uncharge from mc.from later. */
  1162. mc.moved_charge++;
  1163. }
  1164. if (!device)
  1165. folio_putback_lru(folio);
  1166. put: /* get_mctgt_type() gets & locks the page */
  1167. folio_unlock(folio);
  1168. folio_put(folio);
  1169. break;
  1170. case MC_TARGET_SWAP:
  1171. ent = target.ent;
  1172. if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
  1173. mc.precharge--;
  1174. mem_cgroup_id_get_many(mc.to, 1);
  1175. /* we fixup other refcnts and charges later. */
  1176. mc.moved_swap++;
  1177. }
  1178. break;
  1179. default:
  1180. break;
  1181. }
  1182. }
  1183. pte_unmap_unlock(pte - 1, ptl);
  1184. cond_resched();
  1185. if (addr != end) {
  1186. /*
  1187. * We have consumed all precharges we got in can_attach().
  1188. * We try charge one by one, but don't do any additional
  1189. * charges to mc.to if we have failed in charge once in attach()
  1190. * phase.
  1191. */
  1192. ret = mem_cgroup_do_precharge(1);
  1193. if (!ret)
  1194. goto retry;
  1195. }
  1196. return ret;
  1197. }
  1198. static const struct mm_walk_ops charge_walk_ops = {
  1199. .pmd_entry = mem_cgroup_move_charge_pte_range,
  1200. .walk_lock = PGWALK_RDLOCK,
  1201. };
  1202. static void mem_cgroup_move_charge(void)
  1203. {
  1204. lru_add_drain_all();
  1205. /*
  1206. * Signal folio_memcg_lock() to take the memcg's move_lock
  1207. * while we're moving its pages to another memcg. Then wait
  1208. * for already started RCU-only updates to finish.
  1209. */
  1210. atomic_inc(&mc.from->moving_account);
  1211. synchronize_rcu();
  1212. retry:
  1213. if (unlikely(!mmap_read_trylock(mc.mm))) {
  1214. /*
  1215. * Someone who are holding the mmap_lock might be waiting in
  1216. * waitq. So we cancel all extra charges, wake up all waiters,
  1217. * and retry. Because we cancel precharges, we might not be able
  1218. * to move enough charges, but moving charge is a best-effort
  1219. * feature anyway, so it wouldn't be a big problem.
  1220. */
  1221. __mem_cgroup_clear_mc();
  1222. cond_resched();
  1223. goto retry;
  1224. }
  1225. /*
  1226. * When we have consumed all precharges and failed in doing
  1227. * additional charge, the page walk just aborts.
  1228. */
  1229. walk_page_range(mc.mm, 0, ULONG_MAX, &charge_walk_ops, NULL);
  1230. mmap_read_unlock(mc.mm);
  1231. atomic_dec(&mc.from->moving_account);
  1232. }
  1233. void memcg1_move_task(void)
  1234. {
  1235. if (mc.to) {
  1236. mem_cgroup_move_charge();
  1237. mem_cgroup_clear_mc();
  1238. }
  1239. }
  1240. #else /* !CONFIG_MMU */
  1241. int memcg1_can_attach(struct cgroup_taskset *tset)
  1242. {
  1243. return 0;
  1244. }
  1245. void memcg1_cancel_attach(struct cgroup_taskset *tset)
  1246. {
  1247. }
  1248. void memcg1_move_task(void)
  1249. {
  1250. }
  1251. #endif
  1252. static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
  1253. {
  1254. struct mem_cgroup_threshold_ary *t;
  1255. unsigned long usage;
  1256. int i;
  1257. rcu_read_lock();
  1258. if (!swap)
  1259. t = rcu_dereference(memcg->thresholds.primary);
  1260. else
  1261. t = rcu_dereference(memcg->memsw_thresholds.primary);
  1262. if (!t)
  1263. goto unlock;
  1264. usage = mem_cgroup_usage(memcg, swap);
  1265. /*
  1266. * current_threshold points to threshold just below or equal to usage.
  1267. * If it's not true, a threshold was crossed after last
  1268. * call of __mem_cgroup_threshold().
  1269. */
  1270. i = t->current_threshold;
  1271. /*
  1272. * Iterate backward over array of thresholds starting from
  1273. * current_threshold and check if a threshold is crossed.
  1274. * If none of thresholds below usage is crossed, we read
  1275. * only one element of the array here.
  1276. */
  1277. for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
  1278. eventfd_signal(t->entries[i].eventfd);
  1279. /* i = current_threshold + 1 */
  1280. i++;
  1281. /*
  1282. * Iterate forward over array of thresholds starting from
  1283. * current_threshold+1 and check if a threshold is crossed.
  1284. * If none of thresholds above usage is crossed, we read
  1285. * only one element of the array here.
  1286. */
  1287. for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
  1288. eventfd_signal(t->entries[i].eventfd);
  1289. /* Update current_threshold */
  1290. t->current_threshold = i - 1;
  1291. unlock:
  1292. rcu_read_unlock();
  1293. }
  1294. static void mem_cgroup_threshold(struct mem_cgroup *memcg)
  1295. {
  1296. while (memcg) {
  1297. __mem_cgroup_threshold(memcg, false);
  1298. if (do_memsw_account())
  1299. __mem_cgroup_threshold(memcg, true);
  1300. memcg = parent_mem_cgroup(memcg);
  1301. }
  1302. }
  1303. /* Cgroup1: threshold notifications & softlimit tree updates */
  1304. struct memcg1_events_percpu {
  1305. unsigned long nr_page_events;
  1306. unsigned long targets[MEM_CGROUP_NTARGETS];
  1307. };
  1308. static void memcg1_charge_statistics(struct mem_cgroup *memcg, int nr_pages)
  1309. {
  1310. /* pagein of a big page is an event. So, ignore page size */
  1311. if (nr_pages > 0)
  1312. __count_memcg_events(memcg, PGPGIN, 1);
  1313. else {
  1314. __count_memcg_events(memcg, PGPGOUT, 1);
  1315. nr_pages = -nr_pages; /* for event */
  1316. }
  1317. __this_cpu_add(memcg->events_percpu->nr_page_events, nr_pages);
  1318. }
  1319. #define THRESHOLDS_EVENTS_TARGET 128
  1320. #define SOFTLIMIT_EVENTS_TARGET 1024
  1321. static bool memcg1_event_ratelimit(struct mem_cgroup *memcg,
  1322. enum mem_cgroup_events_target target)
  1323. {
  1324. unsigned long val, next;
  1325. val = __this_cpu_read(memcg->events_percpu->nr_page_events);
  1326. next = __this_cpu_read(memcg->events_percpu->targets[target]);
  1327. /* from time_after() in jiffies.h */
  1328. if ((long)(next - val) < 0) {
  1329. switch (target) {
  1330. case MEM_CGROUP_TARGET_THRESH:
  1331. next = val + THRESHOLDS_EVENTS_TARGET;
  1332. break;
  1333. case MEM_CGROUP_TARGET_SOFTLIMIT:
  1334. next = val + SOFTLIMIT_EVENTS_TARGET;
  1335. break;
  1336. default:
  1337. break;
  1338. }
  1339. __this_cpu_write(memcg->events_percpu->targets[target], next);
  1340. return true;
  1341. }
  1342. return false;
  1343. }
  1344. /*
  1345. * Check events in order.
  1346. *
  1347. */
  1348. static void memcg1_check_events(struct mem_cgroup *memcg, int nid)
  1349. {
  1350. if (IS_ENABLED(CONFIG_PREEMPT_RT))
  1351. return;
  1352. /* threshold event is triggered in finer grain than soft limit */
  1353. if (unlikely(memcg1_event_ratelimit(memcg,
  1354. MEM_CGROUP_TARGET_THRESH))) {
  1355. bool do_softlimit;
  1356. do_softlimit = memcg1_event_ratelimit(memcg,
  1357. MEM_CGROUP_TARGET_SOFTLIMIT);
  1358. mem_cgroup_threshold(memcg);
  1359. if (unlikely(do_softlimit))
  1360. memcg1_update_tree(memcg, nid);
  1361. }
  1362. }
  1363. void memcg1_commit_charge(struct folio *folio, struct mem_cgroup *memcg)
  1364. {
  1365. unsigned long flags;
  1366. local_irq_save(flags);
  1367. memcg1_charge_statistics(memcg, folio_nr_pages(folio));
  1368. memcg1_check_events(memcg, folio_nid(folio));
  1369. local_irq_restore(flags);
  1370. }
  1371. void memcg1_swapout(struct folio *folio, struct mem_cgroup *memcg)
  1372. {
  1373. /*
  1374. * Interrupts should be disabled here because the caller holds the
  1375. * i_pages lock which is taken with interrupts-off. It is
  1376. * important here to have the interrupts disabled because it is the
  1377. * only synchronisation we have for updating the per-CPU variables.
  1378. */
  1379. preempt_disable_nested();
  1380. VM_WARN_ON_IRQS_ENABLED();
  1381. memcg1_charge_statistics(memcg, -folio_nr_pages(folio));
  1382. preempt_enable_nested();
  1383. memcg1_check_events(memcg, folio_nid(folio));
  1384. }
  1385. void memcg1_uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
  1386. unsigned long nr_memory, int nid)
  1387. {
  1388. unsigned long flags;
  1389. local_irq_save(flags);
  1390. __count_memcg_events(memcg, PGPGOUT, pgpgout);
  1391. __this_cpu_add(memcg->events_percpu->nr_page_events, nr_memory);
  1392. memcg1_check_events(memcg, nid);
  1393. local_irq_restore(flags);
  1394. }
  1395. static int compare_thresholds(const void *a, const void *b)
  1396. {
  1397. const struct mem_cgroup_threshold *_a = a;
  1398. const struct mem_cgroup_threshold *_b = b;
  1399. if (_a->threshold > _b->threshold)
  1400. return 1;
  1401. if (_a->threshold < _b->threshold)
  1402. return -1;
  1403. return 0;
  1404. }
  1405. static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
  1406. {
  1407. struct mem_cgroup_eventfd_list *ev;
  1408. spin_lock(&memcg_oom_lock);
  1409. list_for_each_entry(ev, &memcg->oom_notify, list)
  1410. eventfd_signal(ev->eventfd);
  1411. spin_unlock(&memcg_oom_lock);
  1412. return 0;
  1413. }
  1414. static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
  1415. {
  1416. struct mem_cgroup *iter;
  1417. for_each_mem_cgroup_tree(iter, memcg)
  1418. mem_cgroup_oom_notify_cb(iter);
  1419. }
  1420. static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
  1421. struct eventfd_ctx *eventfd, const char *args, enum res_type type)
  1422. {
  1423. struct mem_cgroup_thresholds *thresholds;
  1424. struct mem_cgroup_threshold_ary *new;
  1425. unsigned long threshold;
  1426. unsigned long usage;
  1427. int i, size, ret;
  1428. ret = page_counter_memparse(args, "-1", &threshold);
  1429. if (ret)
  1430. return ret;
  1431. mutex_lock(&memcg->thresholds_lock);
  1432. if (type == _MEM) {
  1433. thresholds = &memcg->thresholds;
  1434. usage = mem_cgroup_usage(memcg, false);
  1435. } else if (type == _MEMSWAP) {
  1436. thresholds = &memcg->memsw_thresholds;
  1437. usage = mem_cgroup_usage(memcg, true);
  1438. } else
  1439. BUG();
  1440. /* Check if a threshold crossed before adding a new one */
  1441. if (thresholds->primary)
  1442. __mem_cgroup_threshold(memcg, type == _MEMSWAP);
  1443. size = thresholds->primary ? thresholds->primary->size + 1 : 1;
  1444. /* Allocate memory for new array of thresholds */
  1445. new = kmalloc(struct_size(new, entries, size), GFP_KERNEL);
  1446. if (!new) {
  1447. ret = -ENOMEM;
  1448. goto unlock;
  1449. }
  1450. new->size = size;
  1451. /* Copy thresholds (if any) to new array */
  1452. if (thresholds->primary)
  1453. memcpy(new->entries, thresholds->primary->entries,
  1454. flex_array_size(new, entries, size - 1));
  1455. /* Add new threshold */
  1456. new->entries[size - 1].eventfd = eventfd;
  1457. new->entries[size - 1].threshold = threshold;
  1458. /* Sort thresholds. Registering of new threshold isn't time-critical */
  1459. sort(new->entries, size, sizeof(*new->entries),
  1460. compare_thresholds, NULL);
  1461. /* Find current threshold */
  1462. new->current_threshold = -1;
  1463. for (i = 0; i < size; i++) {
  1464. if (new->entries[i].threshold <= usage) {
  1465. /*
  1466. * new->current_threshold will not be used until
  1467. * rcu_assign_pointer(), so it's safe to increment
  1468. * it here.
  1469. */
  1470. ++new->current_threshold;
  1471. } else
  1472. break;
  1473. }
  1474. /* Free old spare buffer and save old primary buffer as spare */
  1475. kfree(thresholds->spare);
  1476. thresholds->spare = thresholds->primary;
  1477. rcu_assign_pointer(thresholds->primary, new);
  1478. /* To be sure that nobody uses thresholds */
  1479. synchronize_rcu();
  1480. unlock:
  1481. mutex_unlock(&memcg->thresholds_lock);
  1482. return ret;
  1483. }
  1484. static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
  1485. struct eventfd_ctx *eventfd, const char *args)
  1486. {
  1487. return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
  1488. }
  1489. static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
  1490. struct eventfd_ctx *eventfd, const char *args)
  1491. {
  1492. return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
  1493. }
  1494. static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
  1495. struct eventfd_ctx *eventfd, enum res_type type)
  1496. {
  1497. struct mem_cgroup_thresholds *thresholds;
  1498. struct mem_cgroup_threshold_ary *new;
  1499. unsigned long usage;
  1500. int i, j, size, entries;
  1501. mutex_lock(&memcg->thresholds_lock);
  1502. if (type == _MEM) {
  1503. thresholds = &memcg->thresholds;
  1504. usage = mem_cgroup_usage(memcg, false);
  1505. } else if (type == _MEMSWAP) {
  1506. thresholds = &memcg->memsw_thresholds;
  1507. usage = mem_cgroup_usage(memcg, true);
  1508. } else
  1509. BUG();
  1510. if (!thresholds->primary)
  1511. goto unlock;
  1512. /* Check if a threshold crossed before removing */
  1513. __mem_cgroup_threshold(memcg, type == _MEMSWAP);
  1514. /* Calculate new number of threshold */
  1515. size = entries = 0;
  1516. for (i = 0; i < thresholds->primary->size; i++) {
  1517. if (thresholds->primary->entries[i].eventfd != eventfd)
  1518. size++;
  1519. else
  1520. entries++;
  1521. }
  1522. new = thresholds->spare;
  1523. /* If no items related to eventfd have been cleared, nothing to do */
  1524. if (!entries)
  1525. goto unlock;
  1526. /* Set thresholds array to NULL if we don't have thresholds */
  1527. if (!size) {
  1528. kfree(new);
  1529. new = NULL;
  1530. goto swap_buffers;
  1531. }
  1532. new->size = size;
  1533. /* Copy thresholds and find current threshold */
  1534. new->current_threshold = -1;
  1535. for (i = 0, j = 0; i < thresholds->primary->size; i++) {
  1536. if (thresholds->primary->entries[i].eventfd == eventfd)
  1537. continue;
  1538. new->entries[j] = thresholds->primary->entries[i];
  1539. if (new->entries[j].threshold <= usage) {
  1540. /*
  1541. * new->current_threshold will not be used
  1542. * until rcu_assign_pointer(), so it's safe to increment
  1543. * it here.
  1544. */
  1545. ++new->current_threshold;
  1546. }
  1547. j++;
  1548. }
  1549. swap_buffers:
  1550. /* Swap primary and spare array */
  1551. thresholds->spare = thresholds->primary;
  1552. rcu_assign_pointer(thresholds->primary, new);
  1553. /* To be sure that nobody uses thresholds */
  1554. synchronize_rcu();
  1555. /* If all events are unregistered, free the spare array */
  1556. if (!new) {
  1557. kfree(thresholds->spare);
  1558. thresholds->spare = NULL;
  1559. }
  1560. unlock:
  1561. mutex_unlock(&memcg->thresholds_lock);
  1562. }
  1563. static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
  1564. struct eventfd_ctx *eventfd)
  1565. {
  1566. return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
  1567. }
  1568. static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
  1569. struct eventfd_ctx *eventfd)
  1570. {
  1571. return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
  1572. }
  1573. static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
  1574. struct eventfd_ctx *eventfd, const char *args)
  1575. {
  1576. struct mem_cgroup_eventfd_list *event;
  1577. event = kmalloc(sizeof(*event), GFP_KERNEL);
  1578. if (!event)
  1579. return -ENOMEM;
  1580. spin_lock(&memcg_oom_lock);
  1581. event->eventfd = eventfd;
  1582. list_add(&event->list, &memcg->oom_notify);
  1583. /* already in OOM ? */
  1584. if (memcg->under_oom)
  1585. eventfd_signal(eventfd);
  1586. spin_unlock(&memcg_oom_lock);
  1587. return 0;
  1588. }
  1589. static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg,
  1590. struct eventfd_ctx *eventfd)
  1591. {
  1592. struct mem_cgroup_eventfd_list *ev, *tmp;
  1593. spin_lock(&memcg_oom_lock);
  1594. list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
  1595. if (ev->eventfd == eventfd) {
  1596. list_del(&ev->list);
  1597. kfree(ev);
  1598. }
  1599. }
  1600. spin_unlock(&memcg_oom_lock);
  1601. }
  1602. /*
  1603. * DO NOT USE IN NEW FILES.
  1604. *
  1605. * "cgroup.event_control" implementation.
  1606. *
  1607. * This is way over-engineered. It tries to support fully configurable
  1608. * events for each user. Such level of flexibility is completely
  1609. * unnecessary especially in the light of the planned unified hierarchy.
  1610. *
  1611. * Please deprecate this and replace with something simpler if at all
  1612. * possible.
  1613. */
  1614. /*
  1615. * Unregister event and free resources.
  1616. *
  1617. * Gets called from workqueue.
  1618. */
  1619. static void memcg_event_remove(struct work_struct *work)
  1620. {
  1621. struct mem_cgroup_event *event =
  1622. container_of(work, struct mem_cgroup_event, remove);
  1623. struct mem_cgroup *memcg = event->memcg;
  1624. remove_wait_queue(event->wqh, &event->wait);
  1625. event->unregister_event(memcg, event->eventfd);
  1626. /* Notify userspace the event is going away. */
  1627. eventfd_signal(event->eventfd);
  1628. eventfd_ctx_put(event->eventfd);
  1629. kfree(event);
  1630. css_put(&memcg->css);
  1631. }
  1632. /*
  1633. * Gets called on EPOLLHUP on eventfd when user closes it.
  1634. *
  1635. * Called with wqh->lock held and interrupts disabled.
  1636. */
  1637. static int memcg_event_wake(wait_queue_entry_t *wait, unsigned mode,
  1638. int sync, void *key)
  1639. {
  1640. struct mem_cgroup_event *event =
  1641. container_of(wait, struct mem_cgroup_event, wait);
  1642. struct mem_cgroup *memcg = event->memcg;
  1643. __poll_t flags = key_to_poll(key);
  1644. if (flags & EPOLLHUP) {
  1645. /*
  1646. * If the event has been detached at cgroup removal, we
  1647. * can simply return knowing the other side will cleanup
  1648. * for us.
  1649. *
  1650. * We can't race against event freeing since the other
  1651. * side will require wqh->lock via remove_wait_queue(),
  1652. * which we hold.
  1653. */
  1654. spin_lock(&memcg->event_list_lock);
  1655. if (!list_empty(&event->list)) {
  1656. list_del_init(&event->list);
  1657. /*
  1658. * We are in atomic context, but cgroup_event_remove()
  1659. * may sleep, so we have to call it in workqueue.
  1660. */
  1661. schedule_work(&event->remove);
  1662. }
  1663. spin_unlock(&memcg->event_list_lock);
  1664. }
  1665. return 0;
  1666. }
  1667. static void memcg_event_ptable_queue_proc(struct file *file,
  1668. wait_queue_head_t *wqh, poll_table *pt)
  1669. {
  1670. struct mem_cgroup_event *event =
  1671. container_of(pt, struct mem_cgroup_event, pt);
  1672. event->wqh = wqh;
  1673. add_wait_queue(wqh, &event->wait);
  1674. }
  1675. /*
  1676. * DO NOT USE IN NEW FILES.
  1677. *
  1678. * Parse input and register new cgroup event handler.
  1679. *
  1680. * Input must be in format '<event_fd> <control_fd> <args>'.
  1681. * Interpretation of args is defined by control file implementation.
  1682. */
  1683. static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
  1684. char *buf, size_t nbytes, loff_t off)
  1685. {
  1686. struct cgroup_subsys_state *css = of_css(of);
  1687. struct mem_cgroup *memcg = mem_cgroup_from_css(css);
  1688. struct mem_cgroup_event *event;
  1689. struct cgroup_subsys_state *cfile_css;
  1690. unsigned int efd, cfd;
  1691. struct fd efile;
  1692. struct fd cfile;
  1693. struct dentry *cdentry;
  1694. const char *name;
  1695. char *endp;
  1696. int ret;
  1697. if (IS_ENABLED(CONFIG_PREEMPT_RT))
  1698. return -EOPNOTSUPP;
  1699. buf = strstrip(buf);
  1700. efd = simple_strtoul(buf, &endp, 10);
  1701. if (*endp != ' ')
  1702. return -EINVAL;
  1703. buf = endp + 1;
  1704. cfd = simple_strtoul(buf, &endp, 10);
  1705. if (*endp == '\0')
  1706. buf = endp;
  1707. else if (*endp == ' ')
  1708. buf = endp + 1;
  1709. else
  1710. return -EINVAL;
  1711. event = kzalloc(sizeof(*event), GFP_KERNEL);
  1712. if (!event)
  1713. return -ENOMEM;
  1714. event->memcg = memcg;
  1715. INIT_LIST_HEAD(&event->list);
  1716. init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc);
  1717. init_waitqueue_func_entry(&event->wait, memcg_event_wake);
  1718. INIT_WORK(&event->remove, memcg_event_remove);
  1719. efile = fdget(efd);
  1720. if (!fd_file(efile)) {
  1721. ret = -EBADF;
  1722. goto out_kfree;
  1723. }
  1724. event->eventfd = eventfd_ctx_fileget(fd_file(efile));
  1725. if (IS_ERR(event->eventfd)) {
  1726. ret = PTR_ERR(event->eventfd);
  1727. goto out_put_efile;
  1728. }
  1729. cfile = fdget(cfd);
  1730. if (!fd_file(cfile)) {
  1731. ret = -EBADF;
  1732. goto out_put_eventfd;
  1733. }
  1734. /* the process need read permission on control file */
  1735. /* AV: shouldn't we check that it's been opened for read instead? */
  1736. ret = file_permission(fd_file(cfile), MAY_READ);
  1737. if (ret < 0)
  1738. goto out_put_cfile;
  1739. /*
  1740. * The control file must be a regular cgroup1 file. As a regular cgroup
  1741. * file can't be renamed, it's safe to access its name afterwards.
  1742. */
  1743. cdentry = fd_file(cfile)->f_path.dentry;
  1744. if (cdentry->d_sb->s_type != &cgroup_fs_type || !d_is_reg(cdentry)) {
  1745. ret = -EINVAL;
  1746. goto out_put_cfile;
  1747. }
  1748. /*
  1749. * Determine the event callbacks and set them in @event. This used
  1750. * to be done via struct cftype but cgroup core no longer knows
  1751. * about these events. The following is crude but the whole thing
  1752. * is for compatibility anyway.
  1753. *
  1754. * DO NOT ADD NEW FILES.
  1755. */
  1756. name = cdentry->d_name.name;
  1757. if (!strcmp(name, "memory.usage_in_bytes")) {
  1758. event->register_event = mem_cgroup_usage_register_event;
  1759. event->unregister_event = mem_cgroup_usage_unregister_event;
  1760. } else if (!strcmp(name, "memory.oom_control")) {
  1761. pr_warn_once("oom_control is deprecated and will be removed. "
  1762. "Please report your usecase to linux-mm-@kvack.org"
  1763. " if you depend on this functionality. \n");
  1764. event->register_event = mem_cgroup_oom_register_event;
  1765. event->unregister_event = mem_cgroup_oom_unregister_event;
  1766. } else if (!strcmp(name, "memory.pressure_level")) {
  1767. pr_warn_once("pressure_level is deprecated and will be removed. "
  1768. "Please report your usecase to linux-mm-@kvack.org "
  1769. "if you depend on this functionality. \n");
  1770. event->register_event = vmpressure_register_event;
  1771. event->unregister_event = vmpressure_unregister_event;
  1772. } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) {
  1773. event->register_event = memsw_cgroup_usage_register_event;
  1774. event->unregister_event = memsw_cgroup_usage_unregister_event;
  1775. } else {
  1776. ret = -EINVAL;
  1777. goto out_put_cfile;
  1778. }
  1779. /*
  1780. * Verify @cfile should belong to @css. Also, remaining events are
  1781. * automatically removed on cgroup destruction but the removal is
  1782. * asynchronous, so take an extra ref on @css.
  1783. */
  1784. cfile_css = css_tryget_online_from_dir(cdentry->d_parent,
  1785. &memory_cgrp_subsys);
  1786. ret = -EINVAL;
  1787. if (IS_ERR(cfile_css))
  1788. goto out_put_cfile;
  1789. if (cfile_css != css) {
  1790. css_put(cfile_css);
  1791. goto out_put_cfile;
  1792. }
  1793. ret = event->register_event(memcg, event->eventfd, buf);
  1794. if (ret)
  1795. goto out_put_css;
  1796. vfs_poll(fd_file(efile), &event->pt);
  1797. spin_lock_irq(&memcg->event_list_lock);
  1798. list_add(&event->list, &memcg->event_list);
  1799. spin_unlock_irq(&memcg->event_list_lock);
  1800. fdput(cfile);
  1801. fdput(efile);
  1802. return nbytes;
  1803. out_put_css:
  1804. css_put(css);
  1805. out_put_cfile:
  1806. fdput(cfile);
  1807. out_put_eventfd:
  1808. eventfd_ctx_put(event->eventfd);
  1809. out_put_efile:
  1810. fdput(efile);
  1811. out_kfree:
  1812. kfree(event);
  1813. return ret;
  1814. }
  1815. void memcg1_memcg_init(struct mem_cgroup *memcg)
  1816. {
  1817. INIT_LIST_HEAD(&memcg->oom_notify);
  1818. mutex_init(&memcg->thresholds_lock);
  1819. spin_lock_init(&memcg->move_lock);
  1820. INIT_LIST_HEAD(&memcg->event_list);
  1821. spin_lock_init(&memcg->event_list_lock);
  1822. }
  1823. void memcg1_css_offline(struct mem_cgroup *memcg)
  1824. {
  1825. struct mem_cgroup_event *event, *tmp;
  1826. /*
  1827. * Unregister events and notify userspace.
  1828. * Notify userspace about cgroup removing only after rmdir of cgroup
  1829. * directory to avoid race between userspace and kernelspace.
  1830. */
  1831. spin_lock_irq(&memcg->event_list_lock);
  1832. list_for_each_entry_safe(event, tmp, &memcg->event_list, list) {
  1833. list_del_init(&event->list);
  1834. schedule_work(&event->remove);
  1835. }
  1836. spin_unlock_irq(&memcg->event_list_lock);
  1837. }
  1838. /*
  1839. * Check OOM-Killer is already running under our hierarchy.
  1840. * If someone is running, return false.
  1841. */
  1842. static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
  1843. {
  1844. struct mem_cgroup *iter, *failed = NULL;
  1845. spin_lock(&memcg_oom_lock);
  1846. for_each_mem_cgroup_tree(iter, memcg) {
  1847. if (iter->oom_lock) {
  1848. /*
  1849. * this subtree of our hierarchy is already locked
  1850. * so we cannot give a lock.
  1851. */
  1852. failed = iter;
  1853. mem_cgroup_iter_break(memcg, iter);
  1854. break;
  1855. } else
  1856. iter->oom_lock = true;
  1857. }
  1858. if (failed) {
  1859. /*
  1860. * OK, we failed to lock the whole subtree so we have
  1861. * to clean up what we set up to the failing subtree
  1862. */
  1863. for_each_mem_cgroup_tree(iter, memcg) {
  1864. if (iter == failed) {
  1865. mem_cgroup_iter_break(memcg, iter);
  1866. break;
  1867. }
  1868. iter->oom_lock = false;
  1869. }
  1870. } else
  1871. mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_);
  1872. spin_unlock(&memcg_oom_lock);
  1873. return !failed;
  1874. }
  1875. static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
  1876. {
  1877. struct mem_cgroup *iter;
  1878. spin_lock(&memcg_oom_lock);
  1879. mutex_release(&memcg_oom_lock_dep_map, _RET_IP_);
  1880. for_each_mem_cgroup_tree(iter, memcg)
  1881. iter->oom_lock = false;
  1882. spin_unlock(&memcg_oom_lock);
  1883. }
  1884. static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
  1885. {
  1886. struct mem_cgroup *iter;
  1887. spin_lock(&memcg_oom_lock);
  1888. for_each_mem_cgroup_tree(iter, memcg)
  1889. iter->under_oom++;
  1890. spin_unlock(&memcg_oom_lock);
  1891. }
  1892. static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
  1893. {
  1894. struct mem_cgroup *iter;
  1895. /*
  1896. * Be careful about under_oom underflows because a child memcg
  1897. * could have been added after mem_cgroup_mark_under_oom.
  1898. */
  1899. spin_lock(&memcg_oom_lock);
  1900. for_each_mem_cgroup_tree(iter, memcg)
  1901. if (iter->under_oom > 0)
  1902. iter->under_oom--;
  1903. spin_unlock(&memcg_oom_lock);
  1904. }
  1905. static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
  1906. struct oom_wait_info {
  1907. struct mem_cgroup *memcg;
  1908. wait_queue_entry_t wait;
  1909. };
  1910. static int memcg_oom_wake_function(wait_queue_entry_t *wait,
  1911. unsigned mode, int sync, void *arg)
  1912. {
  1913. struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
  1914. struct mem_cgroup *oom_wait_memcg;
  1915. struct oom_wait_info *oom_wait_info;
  1916. oom_wait_info = container_of(wait, struct oom_wait_info, wait);
  1917. oom_wait_memcg = oom_wait_info->memcg;
  1918. if (!mem_cgroup_is_descendant(wake_memcg, oom_wait_memcg) &&
  1919. !mem_cgroup_is_descendant(oom_wait_memcg, wake_memcg))
  1920. return 0;
  1921. return autoremove_wake_function(wait, mode, sync, arg);
  1922. }
  1923. void memcg1_oom_recover(struct mem_cgroup *memcg)
  1924. {
  1925. /*
  1926. * For the following lockless ->under_oom test, the only required
  1927. * guarantee is that it must see the state asserted by an OOM when
  1928. * this function is called as a result of userland actions
  1929. * triggered by the notification of the OOM. This is trivially
  1930. * achieved by invoking mem_cgroup_mark_under_oom() before
  1931. * triggering notification.
  1932. */
  1933. if (memcg && memcg->under_oom)
  1934. __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
  1935. }
  1936. /**
  1937. * mem_cgroup_oom_synchronize - complete memcg OOM handling
  1938. * @handle: actually kill/wait or just clean up the OOM state
  1939. *
  1940. * This has to be called at the end of a page fault if the memcg OOM
  1941. * handler was enabled.
  1942. *
  1943. * Memcg supports userspace OOM handling where failed allocations must
  1944. * sleep on a waitqueue until the userspace task resolves the
  1945. * situation. Sleeping directly in the charge context with all kinds
  1946. * of locks held is not a good idea, instead we remember an OOM state
  1947. * in the task and mem_cgroup_oom_synchronize() has to be called at
  1948. * the end of the page fault to complete the OOM handling.
  1949. *
  1950. * Returns %true if an ongoing memcg OOM situation was detected and
  1951. * completed, %false otherwise.
  1952. */
  1953. bool mem_cgroup_oom_synchronize(bool handle)
  1954. {
  1955. struct mem_cgroup *memcg = current->memcg_in_oom;
  1956. struct oom_wait_info owait;
  1957. bool locked;
  1958. /* OOM is global, do not handle */
  1959. if (!memcg)
  1960. return false;
  1961. if (!handle)
  1962. goto cleanup;
  1963. owait.memcg = memcg;
  1964. owait.wait.flags = 0;
  1965. owait.wait.func = memcg_oom_wake_function;
  1966. owait.wait.private = current;
  1967. INIT_LIST_HEAD(&owait.wait.entry);
  1968. prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
  1969. mem_cgroup_mark_under_oom(memcg);
  1970. locked = mem_cgroup_oom_trylock(memcg);
  1971. if (locked)
  1972. mem_cgroup_oom_notify(memcg);
  1973. schedule();
  1974. mem_cgroup_unmark_under_oom(memcg);
  1975. finish_wait(&memcg_oom_waitq, &owait.wait);
  1976. if (locked)
  1977. mem_cgroup_oom_unlock(memcg);
  1978. cleanup:
  1979. current->memcg_in_oom = NULL;
  1980. css_put(&memcg->css);
  1981. return true;
  1982. }
  1983. bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked)
  1984. {
  1985. /*
  1986. * We are in the middle of the charge context here, so we
  1987. * don't want to block when potentially sitting on a callstack
  1988. * that holds all kinds of filesystem and mm locks.
  1989. *
  1990. * cgroup1 allows disabling the OOM killer and waiting for outside
  1991. * handling until the charge can succeed; remember the context and put
  1992. * the task to sleep at the end of the page fault when all locks are
  1993. * released.
  1994. *
  1995. * On the other hand, in-kernel OOM killer allows for an async victim
  1996. * memory reclaim (oom_reaper) and that means that we are not solely
  1997. * relying on the oom victim to make a forward progress and we can
  1998. * invoke the oom killer here.
  1999. *
  2000. * Please note that mem_cgroup_out_of_memory might fail to find a
  2001. * victim and then we have to bail out from the charge path.
  2002. */
  2003. if (READ_ONCE(memcg->oom_kill_disable)) {
  2004. if (current->in_user_fault) {
  2005. css_get(&memcg->css);
  2006. current->memcg_in_oom = memcg;
  2007. }
  2008. return false;
  2009. }
  2010. mem_cgroup_mark_under_oom(memcg);
  2011. *locked = mem_cgroup_oom_trylock(memcg);
  2012. if (*locked)
  2013. mem_cgroup_oom_notify(memcg);
  2014. mem_cgroup_unmark_under_oom(memcg);
  2015. return true;
  2016. }
  2017. void memcg1_oom_finish(struct mem_cgroup *memcg, bool locked)
  2018. {
  2019. if (locked)
  2020. mem_cgroup_oom_unlock(memcg);
  2021. }
  2022. static DEFINE_MUTEX(memcg_max_mutex);
  2023. static int mem_cgroup_resize_max(struct mem_cgroup *memcg,
  2024. unsigned long max, bool memsw)
  2025. {
  2026. bool enlarge = false;
  2027. bool drained = false;
  2028. int ret;
  2029. bool limits_invariant;
  2030. struct page_counter *counter = memsw ? &memcg->memsw : &memcg->memory;
  2031. do {
  2032. if (signal_pending(current)) {
  2033. ret = -EINTR;
  2034. break;
  2035. }
  2036. mutex_lock(&memcg_max_mutex);
  2037. /*
  2038. * Make sure that the new limit (memsw or memory limit) doesn't
  2039. * break our basic invariant rule memory.max <= memsw.max.
  2040. */
  2041. limits_invariant = memsw ? max >= READ_ONCE(memcg->memory.max) :
  2042. max <= memcg->memsw.max;
  2043. if (!limits_invariant) {
  2044. mutex_unlock(&memcg_max_mutex);
  2045. ret = -EINVAL;
  2046. break;
  2047. }
  2048. if (max > counter->max)
  2049. enlarge = true;
  2050. ret = page_counter_set_max(counter, max);
  2051. mutex_unlock(&memcg_max_mutex);
  2052. if (!ret)
  2053. break;
  2054. if (!drained) {
  2055. drain_all_stock(memcg);
  2056. drained = true;
  2057. continue;
  2058. }
  2059. if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
  2060. memsw ? 0 : MEMCG_RECLAIM_MAY_SWAP, NULL)) {
  2061. ret = -EBUSY;
  2062. break;
  2063. }
  2064. } while (true);
  2065. if (!ret && enlarge)
  2066. memcg1_oom_recover(memcg);
  2067. return ret;
  2068. }
  2069. /*
  2070. * Reclaims as many pages from the given memcg as possible.
  2071. *
  2072. * Caller is responsible for holding css reference for memcg.
  2073. */
  2074. static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
  2075. {
  2076. int nr_retries = MAX_RECLAIM_RETRIES;
  2077. /* we call try-to-free pages for make this cgroup empty */
  2078. lru_add_drain_all();
  2079. drain_all_stock(memcg);
  2080. /* try to free all pages in this cgroup */
  2081. while (nr_retries && page_counter_read(&memcg->memory)) {
  2082. if (signal_pending(current))
  2083. return -EINTR;
  2084. if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
  2085. MEMCG_RECLAIM_MAY_SWAP, NULL))
  2086. nr_retries--;
  2087. }
  2088. return 0;
  2089. }
  2090. static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of,
  2091. char *buf, size_t nbytes,
  2092. loff_t off)
  2093. {
  2094. struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
  2095. if (mem_cgroup_is_root(memcg))
  2096. return -EINVAL;
  2097. return mem_cgroup_force_empty(memcg) ?: nbytes;
  2098. }
  2099. static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
  2100. struct cftype *cft)
  2101. {
  2102. return 1;
  2103. }
  2104. static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
  2105. struct cftype *cft, u64 val)
  2106. {
  2107. if (val == 1)
  2108. return 0;
  2109. pr_warn_once("Non-hierarchical mode is deprecated. "
  2110. "Please report your usecase to linux-mm@kvack.org if you "
  2111. "depend on this functionality.\n");
  2112. return -EINVAL;
  2113. }
  2114. static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
  2115. struct cftype *cft)
  2116. {
  2117. struct mem_cgroup *memcg = mem_cgroup_from_css(css);
  2118. struct page_counter *counter;
  2119. switch (MEMFILE_TYPE(cft->private)) {
  2120. case _MEM:
  2121. counter = &memcg->memory;
  2122. break;
  2123. case _MEMSWAP:
  2124. counter = &memcg->memsw;
  2125. break;
  2126. case _KMEM:
  2127. counter = &memcg->kmem;
  2128. break;
  2129. case _TCP:
  2130. counter = &memcg->tcpmem;
  2131. break;
  2132. default:
  2133. BUG();
  2134. }
  2135. switch (MEMFILE_ATTR(cft->private)) {
  2136. case RES_USAGE:
  2137. if (counter == &memcg->memory)
  2138. return (u64)mem_cgroup_usage(memcg, false) * PAGE_SIZE;
  2139. if (counter == &memcg->memsw)
  2140. return (u64)mem_cgroup_usage(memcg, true) * PAGE_SIZE;
  2141. return (u64)page_counter_read(counter) * PAGE_SIZE;
  2142. case RES_LIMIT:
  2143. return (u64)counter->max * PAGE_SIZE;
  2144. case RES_MAX_USAGE:
  2145. return (u64)counter->watermark * PAGE_SIZE;
  2146. case RES_FAILCNT:
  2147. return counter->failcnt;
  2148. case RES_SOFT_LIMIT:
  2149. return (u64)READ_ONCE(memcg->soft_limit) * PAGE_SIZE;
  2150. default:
  2151. BUG();
  2152. }
  2153. }
  2154. /*
  2155. * This function doesn't do anything useful. Its only job is to provide a read
  2156. * handler for a file so that cgroup_file_mode() will add read permissions.
  2157. */
  2158. static int mem_cgroup_dummy_seq_show(__always_unused struct seq_file *m,
  2159. __always_unused void *v)
  2160. {
  2161. return -EINVAL;
  2162. }
  2163. static int memcg_update_tcp_max(struct mem_cgroup *memcg, unsigned long max)
  2164. {
  2165. int ret;
  2166. mutex_lock(&memcg_max_mutex);
  2167. ret = page_counter_set_max(&memcg->tcpmem, max);
  2168. if (ret)
  2169. goto out;
  2170. if (!memcg->tcpmem_active) {
  2171. /*
  2172. * The active flag needs to be written after the static_key
  2173. * update. This is what guarantees that the socket activation
  2174. * function is the last one to run. See mem_cgroup_sk_alloc()
  2175. * for details, and note that we don't mark any socket as
  2176. * belonging to this memcg until that flag is up.
  2177. *
  2178. * We need to do this, because static_keys will span multiple
  2179. * sites, but we can't control their order. If we mark a socket
  2180. * as accounted, but the accounting functions are not patched in
  2181. * yet, we'll lose accounting.
  2182. *
  2183. * We never race with the readers in mem_cgroup_sk_alloc(),
  2184. * because when this value change, the code to process it is not
  2185. * patched in yet.
  2186. */
  2187. static_branch_inc(&memcg_sockets_enabled_key);
  2188. memcg->tcpmem_active = true;
  2189. }
  2190. out:
  2191. mutex_unlock(&memcg_max_mutex);
  2192. return ret;
  2193. }
  2194. /*
  2195. * The user of this function is...
  2196. * RES_LIMIT.
  2197. */
  2198. static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
  2199. char *buf, size_t nbytes, loff_t off)
  2200. {
  2201. struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
  2202. unsigned long nr_pages;
  2203. int ret;
  2204. buf = strstrip(buf);
  2205. ret = page_counter_memparse(buf, "-1", &nr_pages);
  2206. if (ret)
  2207. return ret;
  2208. switch (MEMFILE_ATTR(of_cft(of)->private)) {
  2209. case RES_LIMIT:
  2210. if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
  2211. ret = -EINVAL;
  2212. break;
  2213. }
  2214. switch (MEMFILE_TYPE(of_cft(of)->private)) {
  2215. case _MEM:
  2216. ret = mem_cgroup_resize_max(memcg, nr_pages, false);
  2217. break;
  2218. case _MEMSWAP:
  2219. ret = mem_cgroup_resize_max(memcg, nr_pages, true);
  2220. break;
  2221. case _KMEM:
  2222. pr_warn_once("kmem.limit_in_bytes is deprecated and will be removed. "
  2223. "Writing any value to this file has no effect. "
  2224. "Please report your usecase to linux-mm@kvack.org if you "
  2225. "depend on this functionality.\n");
  2226. ret = 0;
  2227. break;
  2228. case _TCP:
  2229. pr_warn_once("kmem.tcp.limit_in_bytes is deprecated and will be removed. "
  2230. "Please report your usecase to linux-mm@kvack.org if you "
  2231. "depend on this functionality.\n");
  2232. ret = memcg_update_tcp_max(memcg, nr_pages);
  2233. break;
  2234. }
  2235. break;
  2236. case RES_SOFT_LIMIT:
  2237. if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
  2238. ret = -EOPNOTSUPP;
  2239. } else {
  2240. pr_warn_once("soft_limit_in_bytes is deprecated and will be removed. "
  2241. "Please report your usecase to linux-mm@kvack.org if you "
  2242. "depend on this functionality.\n");
  2243. WRITE_ONCE(memcg->soft_limit, nr_pages);
  2244. ret = 0;
  2245. }
  2246. break;
  2247. }
  2248. return ret ?: nbytes;
  2249. }
  2250. static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf,
  2251. size_t nbytes, loff_t off)
  2252. {
  2253. struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
  2254. struct page_counter *counter;
  2255. switch (MEMFILE_TYPE(of_cft(of)->private)) {
  2256. case _MEM:
  2257. counter = &memcg->memory;
  2258. break;
  2259. case _MEMSWAP:
  2260. counter = &memcg->memsw;
  2261. break;
  2262. case _KMEM:
  2263. counter = &memcg->kmem;
  2264. break;
  2265. case _TCP:
  2266. counter = &memcg->tcpmem;
  2267. break;
  2268. default:
  2269. BUG();
  2270. }
  2271. switch (MEMFILE_ATTR(of_cft(of)->private)) {
  2272. case RES_MAX_USAGE:
  2273. page_counter_reset_watermark(counter);
  2274. break;
  2275. case RES_FAILCNT:
  2276. counter->failcnt = 0;
  2277. break;
  2278. default:
  2279. BUG();
  2280. }
  2281. return nbytes;
  2282. }
  2283. #ifdef CONFIG_NUMA
  2284. #define LRU_ALL_FILE (BIT(LRU_INACTIVE_FILE) | BIT(LRU_ACTIVE_FILE))
  2285. #define LRU_ALL_ANON (BIT(LRU_INACTIVE_ANON) | BIT(LRU_ACTIVE_ANON))
  2286. #define LRU_ALL ((1 << NR_LRU_LISTS) - 1)
  2287. static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
  2288. int nid, unsigned int lru_mask, bool tree)
  2289. {
  2290. struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid));
  2291. unsigned long nr = 0;
  2292. enum lru_list lru;
  2293. VM_BUG_ON((unsigned)nid >= nr_node_ids);
  2294. for_each_lru(lru) {
  2295. if (!(BIT(lru) & lru_mask))
  2296. continue;
  2297. if (tree)
  2298. nr += lruvec_page_state(lruvec, NR_LRU_BASE + lru);
  2299. else
  2300. nr += lruvec_page_state_local(lruvec, NR_LRU_BASE + lru);
  2301. }
  2302. return nr;
  2303. }
  2304. static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
  2305. unsigned int lru_mask,
  2306. bool tree)
  2307. {
  2308. unsigned long nr = 0;
  2309. enum lru_list lru;
  2310. for_each_lru(lru) {
  2311. if (!(BIT(lru) & lru_mask))
  2312. continue;
  2313. if (tree)
  2314. nr += memcg_page_state(memcg, NR_LRU_BASE + lru);
  2315. else
  2316. nr += memcg_page_state_local(memcg, NR_LRU_BASE + lru);
  2317. }
  2318. return nr;
  2319. }
  2320. static int memcg_numa_stat_show(struct seq_file *m, void *v)
  2321. {
  2322. struct numa_stat {
  2323. const char *name;
  2324. unsigned int lru_mask;
  2325. };
  2326. static const struct numa_stat stats[] = {
  2327. { "total", LRU_ALL },
  2328. { "file", LRU_ALL_FILE },
  2329. { "anon", LRU_ALL_ANON },
  2330. { "unevictable", BIT(LRU_UNEVICTABLE) },
  2331. };
  2332. const struct numa_stat *stat;
  2333. int nid;
  2334. struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
  2335. mem_cgroup_flush_stats(memcg);
  2336. for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
  2337. seq_printf(m, "%s=%lu", stat->name,
  2338. mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
  2339. false));
  2340. for_each_node_state(nid, N_MEMORY)
  2341. seq_printf(m, " N%d=%lu", nid,
  2342. mem_cgroup_node_nr_lru_pages(memcg, nid,
  2343. stat->lru_mask, false));
  2344. seq_putc(m, '\n');
  2345. }
  2346. for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
  2347. seq_printf(m, "hierarchical_%s=%lu", stat->name,
  2348. mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
  2349. true));
  2350. for_each_node_state(nid, N_MEMORY)
  2351. seq_printf(m, " N%d=%lu", nid,
  2352. mem_cgroup_node_nr_lru_pages(memcg, nid,
  2353. stat->lru_mask, true));
  2354. seq_putc(m, '\n');
  2355. }
  2356. return 0;
  2357. }
  2358. #endif /* CONFIG_NUMA */
  2359. static const unsigned int memcg1_stats[] = {
  2360. NR_FILE_PAGES,
  2361. NR_ANON_MAPPED,
  2362. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  2363. NR_ANON_THPS,
  2364. #endif
  2365. NR_SHMEM,
  2366. NR_FILE_MAPPED,
  2367. NR_FILE_DIRTY,
  2368. NR_WRITEBACK,
  2369. WORKINGSET_REFAULT_ANON,
  2370. WORKINGSET_REFAULT_FILE,
  2371. #ifdef CONFIG_SWAP
  2372. MEMCG_SWAP,
  2373. NR_SWAPCACHE,
  2374. #endif
  2375. };
  2376. static const char *const memcg1_stat_names[] = {
  2377. "cache",
  2378. "rss",
  2379. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  2380. "rss_huge",
  2381. #endif
  2382. "shmem",
  2383. "mapped_file",
  2384. "dirty",
  2385. "writeback",
  2386. "workingset_refault_anon",
  2387. "workingset_refault_file",
  2388. #ifdef CONFIG_SWAP
  2389. "swap",
  2390. "swapcached",
  2391. #endif
  2392. };
  2393. /* Universal VM events cgroup1 shows, original sort order */
  2394. static const unsigned int memcg1_events[] = {
  2395. PGPGIN,
  2396. PGPGOUT,
  2397. PGFAULT,
  2398. PGMAJFAULT,
  2399. };
  2400. void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s)
  2401. {
  2402. unsigned long memory, memsw;
  2403. struct mem_cgroup *mi;
  2404. unsigned int i;
  2405. BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) != ARRAY_SIZE(memcg1_stats));
  2406. mem_cgroup_flush_stats(memcg);
  2407. for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
  2408. unsigned long nr;
  2409. nr = memcg_page_state_local_output(memcg, memcg1_stats[i]);
  2410. seq_buf_printf(s, "%s %lu\n", memcg1_stat_names[i], nr);
  2411. }
  2412. for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
  2413. seq_buf_printf(s, "%s %lu\n", vm_event_name(memcg1_events[i]),
  2414. memcg_events_local(memcg, memcg1_events[i]));
  2415. for (i = 0; i < NR_LRU_LISTS; i++)
  2416. seq_buf_printf(s, "%s %lu\n", lru_list_name(i),
  2417. memcg_page_state_local(memcg, NR_LRU_BASE + i) *
  2418. PAGE_SIZE);
  2419. /* Hierarchical information */
  2420. memory = memsw = PAGE_COUNTER_MAX;
  2421. for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) {
  2422. memory = min(memory, READ_ONCE(mi->memory.max));
  2423. memsw = min(memsw, READ_ONCE(mi->memsw.max));
  2424. }
  2425. seq_buf_printf(s, "hierarchical_memory_limit %llu\n",
  2426. (u64)memory * PAGE_SIZE);
  2427. seq_buf_printf(s, "hierarchical_memsw_limit %llu\n",
  2428. (u64)memsw * PAGE_SIZE);
  2429. for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
  2430. unsigned long nr;
  2431. nr = memcg_page_state_output(memcg, memcg1_stats[i]);
  2432. seq_buf_printf(s, "total_%s %llu\n", memcg1_stat_names[i],
  2433. (u64)nr);
  2434. }
  2435. for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
  2436. seq_buf_printf(s, "total_%s %llu\n",
  2437. vm_event_name(memcg1_events[i]),
  2438. (u64)memcg_events(memcg, memcg1_events[i]));
  2439. for (i = 0; i < NR_LRU_LISTS; i++)
  2440. seq_buf_printf(s, "total_%s %llu\n", lru_list_name(i),
  2441. (u64)memcg_page_state(memcg, NR_LRU_BASE + i) *
  2442. PAGE_SIZE);
  2443. #ifdef CONFIG_DEBUG_VM
  2444. {
  2445. pg_data_t *pgdat;
  2446. struct mem_cgroup_per_node *mz;
  2447. unsigned long anon_cost = 0;
  2448. unsigned long file_cost = 0;
  2449. for_each_online_pgdat(pgdat) {
  2450. mz = memcg->nodeinfo[pgdat->node_id];
  2451. anon_cost += mz->lruvec.anon_cost;
  2452. file_cost += mz->lruvec.file_cost;
  2453. }
  2454. seq_buf_printf(s, "anon_cost %lu\n", anon_cost);
  2455. seq_buf_printf(s, "file_cost %lu\n", file_cost);
  2456. }
  2457. #endif
  2458. }
  2459. static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
  2460. struct cftype *cft)
  2461. {
  2462. struct mem_cgroup *memcg = mem_cgroup_from_css(css);
  2463. return mem_cgroup_swappiness(memcg);
  2464. }
  2465. static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
  2466. struct cftype *cft, u64 val)
  2467. {
  2468. struct mem_cgroup *memcg = mem_cgroup_from_css(css);
  2469. if (val > MAX_SWAPPINESS)
  2470. return -EINVAL;
  2471. if (!mem_cgroup_is_root(memcg))
  2472. WRITE_ONCE(memcg->swappiness, val);
  2473. else
  2474. WRITE_ONCE(vm_swappiness, val);
  2475. return 0;
  2476. }
  2477. static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
  2478. {
  2479. struct mem_cgroup *memcg = mem_cgroup_from_seq(sf);
  2480. seq_printf(sf, "oom_kill_disable %d\n", READ_ONCE(memcg->oom_kill_disable));
  2481. seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom);
  2482. seq_printf(sf, "oom_kill %lu\n",
  2483. atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL]));
  2484. return 0;
  2485. }
  2486. static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
  2487. struct cftype *cft, u64 val)
  2488. {
  2489. struct mem_cgroup *memcg = mem_cgroup_from_css(css);
  2490. pr_warn_once("oom_control is deprecated and will be removed. "
  2491. "Please report your usecase to linux-mm-@kvack.org if you "
  2492. "depend on this functionality. \n");
  2493. /* cannot set to root cgroup and only 0 and 1 are allowed */
  2494. if (mem_cgroup_is_root(memcg) || !((val == 0) || (val == 1)))
  2495. return -EINVAL;
  2496. WRITE_ONCE(memcg->oom_kill_disable, val);
  2497. if (!val)
  2498. memcg1_oom_recover(memcg);
  2499. return 0;
  2500. }
  2501. #ifdef CONFIG_SLUB_DEBUG
  2502. static int mem_cgroup_slab_show(struct seq_file *m, void *p)
  2503. {
  2504. /*
  2505. * Deprecated.
  2506. * Please, take a look at tools/cgroup/memcg_slabinfo.py .
  2507. */
  2508. return 0;
  2509. }
  2510. #endif
  2511. struct cftype mem_cgroup_legacy_files[] = {
  2512. {
  2513. .name = "usage_in_bytes",
  2514. .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
  2515. .read_u64 = mem_cgroup_read_u64,
  2516. },
  2517. {
  2518. .name = "max_usage_in_bytes",
  2519. .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
  2520. .write = mem_cgroup_reset,
  2521. .read_u64 = mem_cgroup_read_u64,
  2522. },
  2523. {
  2524. .name = "limit_in_bytes",
  2525. .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
  2526. .write = mem_cgroup_write,
  2527. .read_u64 = mem_cgroup_read_u64,
  2528. },
  2529. {
  2530. .name = "soft_limit_in_bytes",
  2531. .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
  2532. .write = mem_cgroup_write,
  2533. .read_u64 = mem_cgroup_read_u64,
  2534. },
  2535. {
  2536. .name = "failcnt",
  2537. .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
  2538. .write = mem_cgroup_reset,
  2539. .read_u64 = mem_cgroup_read_u64,
  2540. },
  2541. {
  2542. .name = "stat",
  2543. .seq_show = memory_stat_show,
  2544. },
  2545. {
  2546. .name = "force_empty",
  2547. .write = mem_cgroup_force_empty_write,
  2548. },
  2549. {
  2550. .name = "use_hierarchy",
  2551. .write_u64 = mem_cgroup_hierarchy_write,
  2552. .read_u64 = mem_cgroup_hierarchy_read,
  2553. },
  2554. {
  2555. .name = "cgroup.event_control", /* XXX: for compat */
  2556. .write = memcg_write_event_control,
  2557. .flags = CFTYPE_NO_PREFIX | CFTYPE_WORLD_WRITABLE,
  2558. },
  2559. {
  2560. .name = "swappiness",
  2561. .read_u64 = mem_cgroup_swappiness_read,
  2562. .write_u64 = mem_cgroup_swappiness_write,
  2563. },
  2564. {
  2565. .name = "move_charge_at_immigrate",
  2566. .read_u64 = mem_cgroup_move_charge_read,
  2567. .write_u64 = mem_cgroup_move_charge_write,
  2568. },
  2569. {
  2570. .name = "oom_control",
  2571. .seq_show = mem_cgroup_oom_control_read,
  2572. .write_u64 = mem_cgroup_oom_control_write,
  2573. },
  2574. {
  2575. .name = "pressure_level",
  2576. .seq_show = mem_cgroup_dummy_seq_show,
  2577. },
  2578. #ifdef CONFIG_NUMA
  2579. {
  2580. .name = "numa_stat",
  2581. .seq_show = memcg_numa_stat_show,
  2582. },
  2583. #endif
  2584. {
  2585. .name = "kmem.limit_in_bytes",
  2586. .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
  2587. .write = mem_cgroup_write,
  2588. .read_u64 = mem_cgroup_read_u64,
  2589. },
  2590. {
  2591. .name = "kmem.usage_in_bytes",
  2592. .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
  2593. .read_u64 = mem_cgroup_read_u64,
  2594. },
  2595. {
  2596. .name = "kmem.failcnt",
  2597. .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
  2598. .write = mem_cgroup_reset,
  2599. .read_u64 = mem_cgroup_read_u64,
  2600. },
  2601. {
  2602. .name = "kmem.max_usage_in_bytes",
  2603. .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
  2604. .write = mem_cgroup_reset,
  2605. .read_u64 = mem_cgroup_read_u64,
  2606. },
  2607. #ifdef CONFIG_SLUB_DEBUG
  2608. {
  2609. .name = "kmem.slabinfo",
  2610. .seq_show = mem_cgroup_slab_show,
  2611. },
  2612. #endif
  2613. {
  2614. .name = "kmem.tcp.limit_in_bytes",
  2615. .private = MEMFILE_PRIVATE(_TCP, RES_LIMIT),
  2616. .write = mem_cgroup_write,
  2617. .read_u64 = mem_cgroup_read_u64,
  2618. },
  2619. {
  2620. .name = "kmem.tcp.usage_in_bytes",
  2621. .private = MEMFILE_PRIVATE(_TCP, RES_USAGE),
  2622. .read_u64 = mem_cgroup_read_u64,
  2623. },
  2624. {
  2625. .name = "kmem.tcp.failcnt",
  2626. .private = MEMFILE_PRIVATE(_TCP, RES_FAILCNT),
  2627. .write = mem_cgroup_reset,
  2628. .read_u64 = mem_cgroup_read_u64,
  2629. },
  2630. {
  2631. .name = "kmem.tcp.max_usage_in_bytes",
  2632. .private = MEMFILE_PRIVATE(_TCP, RES_MAX_USAGE),
  2633. .write = mem_cgroup_reset,
  2634. .read_u64 = mem_cgroup_read_u64,
  2635. },
  2636. { }, /* terminate */
  2637. };
  2638. struct cftype memsw_files[] = {
  2639. {
  2640. .name = "memsw.usage_in_bytes",
  2641. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
  2642. .read_u64 = mem_cgroup_read_u64,
  2643. },
  2644. {
  2645. .name = "memsw.max_usage_in_bytes",
  2646. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
  2647. .write = mem_cgroup_reset,
  2648. .read_u64 = mem_cgroup_read_u64,
  2649. },
  2650. {
  2651. .name = "memsw.limit_in_bytes",
  2652. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
  2653. .write = mem_cgroup_write,
  2654. .read_u64 = mem_cgroup_read_u64,
  2655. },
  2656. {
  2657. .name = "memsw.failcnt",
  2658. .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
  2659. .write = mem_cgroup_reset,
  2660. .read_u64 = mem_cgroup_read_u64,
  2661. },
  2662. { }, /* terminate */
  2663. };
  2664. void memcg1_account_kmem(struct mem_cgroup *memcg, int nr_pages)
  2665. {
  2666. if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
  2667. if (nr_pages > 0)
  2668. page_counter_charge(&memcg->kmem, nr_pages);
  2669. else
  2670. page_counter_uncharge(&memcg->kmem, -nr_pages);
  2671. }
  2672. }
  2673. bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
  2674. gfp_t gfp_mask)
  2675. {
  2676. struct page_counter *fail;
  2677. if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) {
  2678. memcg->tcpmem_pressure = 0;
  2679. return true;
  2680. }
  2681. memcg->tcpmem_pressure = 1;
  2682. if (gfp_mask & __GFP_NOFAIL) {
  2683. page_counter_charge(&memcg->tcpmem, nr_pages);
  2684. return true;
  2685. }
  2686. return false;
  2687. }
  2688. bool memcg1_alloc_events(struct mem_cgroup *memcg)
  2689. {
  2690. memcg->events_percpu = alloc_percpu_gfp(struct memcg1_events_percpu,
  2691. GFP_KERNEL_ACCOUNT);
  2692. return !!memcg->events_percpu;
  2693. }
  2694. void memcg1_free_events(struct mem_cgroup *memcg)
  2695. {
  2696. if (memcg->events_percpu)
  2697. free_percpu(memcg->events_percpu);
  2698. }
  2699. static int __init memcg1_init(void)
  2700. {
  2701. int node;
  2702. for_each_node(node) {
  2703. struct mem_cgroup_tree_per_node *rtpn;
  2704. rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, node);
  2705. rtpn->rb_root = RB_ROOT;
  2706. rtpn->rb_rightmost = NULL;
  2707. spin_lock_init(&rtpn->lock);
  2708. soft_limit_tree.rb_tree_per_node[node] = rtpn;
  2709. }
  2710. return 0;
  2711. }
  2712. subsys_initcall(memcg1_init);