cpuset.c 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286
  1. /*
  2. * kernel/cpuset.c
  3. *
  4. * Processor and Memory placement constraints for sets of tasks.
  5. *
  6. * Copyright (C) 2003 BULL SA.
  7. * Copyright (C) 2004-2007 Silicon Graphics, Inc.
  8. * Copyright (C) 2006 Google, Inc
  9. *
  10. * Portions derived from Patrick Mochel's sysfs code.
  11. * sysfs is Copyright (c) 2001-3 Patrick Mochel
  12. *
  13. * 2003-10-10 Written by Simon Derr.
  14. * 2003-10-22 Updates by Stephen Hemminger.
  15. * 2004 May-July Rework by Paul Jackson.
  16. * 2006 Rework by Paul Menage to use generic cgroups
  17. * 2008 Rework of the scheduler domains and CPU hotplug handling
  18. * by Max Krasnyansky
  19. *
  20. * This file is subject to the terms and conditions of the GNU General Public
  21. * License. See the file COPYING in the main directory of the Linux
  22. * distribution for more details.
  23. */
  24. #include "cgroup-internal.h"
  25. #include "cpuset-internal.h"
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/kernel.h>
  29. #include <linux/mempolicy.h>
  30. #include <linux/mm.h>
  31. #include <linux/memory.h>
  32. #include <linux/export.h>
  33. #include <linux/rcupdate.h>
  34. #include <linux/sched.h>
  35. #include <linux/sched/deadline.h>
  36. #include <linux/sched/mm.h>
  37. #include <linux/sched/task.h>
  38. #include <linux/security.h>
  39. #include <linux/oom.h>
  40. #include <linux/sched/isolation.h>
  41. #include <linux/wait.h>
  42. #include <linux/workqueue.h>
  43. DEFINE_STATIC_KEY_FALSE(cpusets_pre_enable_key);
  44. DEFINE_STATIC_KEY_FALSE(cpusets_enabled_key);
  45. /*
  46. * There could be abnormal cpuset configurations for cpu or memory
  47. * node binding, add this key to provide a quick low-cost judgment
  48. * of the situation.
  49. */
  50. DEFINE_STATIC_KEY_FALSE(cpusets_insane_config_key);
  51. static const char * const perr_strings[] = {
  52. [PERR_INVCPUS] = "Invalid cpu list in cpuset.cpus.exclusive",
  53. [PERR_INVPARENT] = "Parent is an invalid partition root",
  54. [PERR_NOTPART] = "Parent is not a partition root",
  55. [PERR_NOTEXCL] = "Cpu list in cpuset.cpus not exclusive",
  56. [PERR_NOCPUS] = "Parent unable to distribute cpu downstream",
  57. [PERR_HOTPLUG] = "No cpu available due to hotplug",
  58. [PERR_CPUSEMPTY] = "cpuset.cpus and cpuset.cpus.exclusive are empty",
  59. [PERR_HKEEPING] = "partition config conflicts with housekeeping setup",
  60. [PERR_ACCESS] = "Enable partition not permitted",
  61. };
  62. /*
  63. * Exclusive CPUs distributed out to sub-partitions of top_cpuset
  64. */
  65. static cpumask_var_t subpartitions_cpus;
  66. /*
  67. * Exclusive CPUs in isolated partitions
  68. */
  69. static cpumask_var_t isolated_cpus;
  70. /*
  71. * Housekeeping (HK_TYPE_DOMAIN) CPUs at boot
  72. */
  73. static cpumask_var_t boot_hk_cpus;
  74. static bool have_boot_isolcpus;
  75. /* List of remote partition root children */
  76. static struct list_head remote_children;
  77. /*
  78. * A flag to force sched domain rebuild at the end of an operation while
  79. * inhibiting it in the intermediate stages when set. Currently it is only
  80. * set in hotplug code.
  81. */
  82. static bool force_sd_rebuild;
  83. /*
  84. * Partition root states:
  85. *
  86. * 0 - member (not a partition root)
  87. * 1 - partition root
  88. * 2 - partition root without load balancing (isolated)
  89. * -1 - invalid partition root
  90. * -2 - invalid isolated partition root
  91. *
  92. * There are 2 types of partitions - local or remote. Local partitions are
  93. * those whose parents are partition root themselves. Setting of
  94. * cpuset.cpus.exclusive are optional in setting up local partitions.
  95. * Remote partitions are those whose parents are not partition roots. Passing
  96. * down exclusive CPUs by setting cpuset.cpus.exclusive along its ancestor
  97. * nodes are mandatory in creating a remote partition.
  98. *
  99. * For simplicity, a local partition can be created under a local or remote
  100. * partition but a remote partition cannot have any partition root in its
  101. * ancestor chain except the cgroup root.
  102. */
  103. #define PRS_MEMBER 0
  104. #define PRS_ROOT 1
  105. #define PRS_ISOLATED 2
  106. #define PRS_INVALID_ROOT -1
  107. #define PRS_INVALID_ISOLATED -2
  108. static inline bool is_prs_invalid(int prs_state)
  109. {
  110. return prs_state < 0;
  111. }
  112. /*
  113. * Temporary cpumasks for working with partitions that are passed among
  114. * functions to avoid memory allocation in inner functions.
  115. */
  116. struct tmpmasks {
  117. cpumask_var_t addmask, delmask; /* For partition root */
  118. cpumask_var_t new_cpus; /* For update_cpumasks_hier() */
  119. };
  120. void inc_dl_tasks_cs(struct task_struct *p)
  121. {
  122. struct cpuset *cs = task_cs(p);
  123. cs->nr_deadline_tasks++;
  124. }
  125. void dec_dl_tasks_cs(struct task_struct *p)
  126. {
  127. struct cpuset *cs = task_cs(p);
  128. cs->nr_deadline_tasks--;
  129. }
  130. static inline int is_partition_valid(const struct cpuset *cs)
  131. {
  132. return cs->partition_root_state > 0;
  133. }
  134. static inline int is_partition_invalid(const struct cpuset *cs)
  135. {
  136. return cs->partition_root_state < 0;
  137. }
  138. /*
  139. * Callers should hold callback_lock to modify partition_root_state.
  140. */
  141. static inline void make_partition_invalid(struct cpuset *cs)
  142. {
  143. if (cs->partition_root_state > 0)
  144. cs->partition_root_state = -cs->partition_root_state;
  145. }
  146. /*
  147. * Send notification event of whenever partition_root_state changes.
  148. */
  149. static inline void notify_partition_change(struct cpuset *cs, int old_prs)
  150. {
  151. if (old_prs == cs->partition_root_state)
  152. return;
  153. cgroup_file_notify(&cs->partition_file);
  154. /* Reset prs_err if not invalid */
  155. if (is_partition_valid(cs))
  156. WRITE_ONCE(cs->prs_err, PERR_NONE);
  157. }
  158. static struct cpuset top_cpuset = {
  159. .flags = BIT(CS_ONLINE) | BIT(CS_CPU_EXCLUSIVE) |
  160. BIT(CS_MEM_EXCLUSIVE) | BIT(CS_SCHED_LOAD_BALANCE),
  161. .partition_root_state = PRS_ROOT,
  162. .relax_domain_level = -1,
  163. .remote_sibling = LIST_HEAD_INIT(top_cpuset.remote_sibling),
  164. };
  165. /*
  166. * There are two global locks guarding cpuset structures - cpuset_mutex and
  167. * callback_lock. We also require taking task_lock() when dereferencing a
  168. * task's cpuset pointer. See "The task_lock() exception", at the end of this
  169. * comment. The cpuset code uses only cpuset_mutex. Other kernel subsystems
  170. * can use cpuset_lock()/cpuset_unlock() to prevent change to cpuset
  171. * structures. Note that cpuset_mutex needs to be a mutex as it is used in
  172. * paths that rely on priority inheritance (e.g. scheduler - on RT) for
  173. * correctness.
  174. *
  175. * A task must hold both locks to modify cpusets. If a task holds
  176. * cpuset_mutex, it blocks others, ensuring that it is the only task able to
  177. * also acquire callback_lock and be able to modify cpusets. It can perform
  178. * various checks on the cpuset structure first, knowing nothing will change.
  179. * It can also allocate memory while just holding cpuset_mutex. While it is
  180. * performing these checks, various callback routines can briefly acquire
  181. * callback_lock to query cpusets. Once it is ready to make the changes, it
  182. * takes callback_lock, blocking everyone else.
  183. *
  184. * Calls to the kernel memory allocator can not be made while holding
  185. * callback_lock, as that would risk double tripping on callback_lock
  186. * from one of the callbacks into the cpuset code from within
  187. * __alloc_pages().
  188. *
  189. * If a task is only holding callback_lock, then it has read-only
  190. * access to cpusets.
  191. *
  192. * Now, the task_struct fields mems_allowed and mempolicy may be changed
  193. * by other task, we use alloc_lock in the task_struct fields to protect
  194. * them.
  195. *
  196. * The cpuset_common_seq_show() handlers only hold callback_lock across
  197. * small pieces of code, such as when reading out possibly multi-word
  198. * cpumasks and nodemasks.
  199. *
  200. * Accessing a task's cpuset should be done in accordance with the
  201. * guidelines for accessing subsystem state in kernel/cgroup.c
  202. */
  203. static DEFINE_MUTEX(cpuset_mutex);
  204. void cpuset_lock(void)
  205. {
  206. mutex_lock(&cpuset_mutex);
  207. }
  208. void cpuset_unlock(void)
  209. {
  210. mutex_unlock(&cpuset_mutex);
  211. }
  212. static DEFINE_SPINLOCK(callback_lock);
  213. void cpuset_callback_lock_irq(void)
  214. {
  215. spin_lock_irq(&callback_lock);
  216. }
  217. void cpuset_callback_unlock_irq(void)
  218. {
  219. spin_unlock_irq(&callback_lock);
  220. }
  221. static struct workqueue_struct *cpuset_migrate_mm_wq;
  222. static DECLARE_WAIT_QUEUE_HEAD(cpuset_attach_wq);
  223. static inline void check_insane_mems_config(nodemask_t *nodes)
  224. {
  225. if (!cpusets_insane_config() &&
  226. movable_only_nodes(nodes)) {
  227. static_branch_enable(&cpusets_insane_config_key);
  228. pr_info("Unsupported (movable nodes only) cpuset configuration detected (nmask=%*pbl)!\n"
  229. "Cpuset allocations might fail even with a lot of memory available.\n",
  230. nodemask_pr_args(nodes));
  231. }
  232. }
  233. /*
  234. * decrease cs->attach_in_progress.
  235. * wake_up cpuset_attach_wq if cs->attach_in_progress==0.
  236. */
  237. static inline void dec_attach_in_progress_locked(struct cpuset *cs)
  238. {
  239. lockdep_assert_held(&cpuset_mutex);
  240. cs->attach_in_progress--;
  241. if (!cs->attach_in_progress)
  242. wake_up(&cpuset_attach_wq);
  243. }
  244. static inline void dec_attach_in_progress(struct cpuset *cs)
  245. {
  246. mutex_lock(&cpuset_mutex);
  247. dec_attach_in_progress_locked(cs);
  248. mutex_unlock(&cpuset_mutex);
  249. }
  250. /*
  251. * Cgroup v2 behavior is used on the "cpus" and "mems" control files when
  252. * on default hierarchy or when the cpuset_v2_mode flag is set by mounting
  253. * the v1 cpuset cgroup filesystem with the "cpuset_v2_mode" mount option.
  254. * With v2 behavior, "cpus" and "mems" are always what the users have
  255. * requested and won't be changed by hotplug events. Only the effective
  256. * cpus or mems will be affected.
  257. */
  258. static inline bool is_in_v2_mode(void)
  259. {
  260. return cgroup_subsys_on_dfl(cpuset_cgrp_subsys) ||
  261. (cpuset_cgrp_subsys.root->flags & CGRP_ROOT_CPUSET_V2_MODE);
  262. }
  263. /**
  264. * partition_is_populated - check if partition has tasks
  265. * @cs: partition root to be checked
  266. * @excluded_child: a child cpuset to be excluded in task checking
  267. * Return: true if there are tasks, false otherwise
  268. *
  269. * It is assumed that @cs is a valid partition root. @excluded_child should
  270. * be non-NULL when this cpuset is going to become a partition itself.
  271. */
  272. static inline bool partition_is_populated(struct cpuset *cs,
  273. struct cpuset *excluded_child)
  274. {
  275. struct cgroup_subsys_state *css;
  276. struct cpuset *child;
  277. if (cs->css.cgroup->nr_populated_csets)
  278. return true;
  279. if (!excluded_child && !cs->nr_subparts)
  280. return cgroup_is_populated(cs->css.cgroup);
  281. rcu_read_lock();
  282. cpuset_for_each_child(child, css, cs) {
  283. if (child == excluded_child)
  284. continue;
  285. if (is_partition_valid(child))
  286. continue;
  287. if (cgroup_is_populated(child->css.cgroup)) {
  288. rcu_read_unlock();
  289. return true;
  290. }
  291. }
  292. rcu_read_unlock();
  293. return false;
  294. }
  295. /*
  296. * Return in pmask the portion of a task's cpusets's cpus_allowed that
  297. * are online and are capable of running the task. If none are found,
  298. * walk up the cpuset hierarchy until we find one that does have some
  299. * appropriate cpus.
  300. *
  301. * One way or another, we guarantee to return some non-empty subset
  302. * of cpu_online_mask.
  303. *
  304. * Call with callback_lock or cpuset_mutex held.
  305. */
  306. static void guarantee_online_cpus(struct task_struct *tsk,
  307. struct cpumask *pmask)
  308. {
  309. const struct cpumask *possible_mask = task_cpu_possible_mask(tsk);
  310. struct cpuset *cs;
  311. if (WARN_ON(!cpumask_and(pmask, possible_mask, cpu_online_mask)))
  312. cpumask_copy(pmask, cpu_online_mask);
  313. rcu_read_lock();
  314. cs = task_cs(tsk);
  315. while (!cpumask_intersects(cs->effective_cpus, pmask))
  316. cs = parent_cs(cs);
  317. cpumask_and(pmask, pmask, cs->effective_cpus);
  318. rcu_read_unlock();
  319. }
  320. /*
  321. * Return in *pmask the portion of a cpusets's mems_allowed that
  322. * are online, with memory. If none are online with memory, walk
  323. * up the cpuset hierarchy until we find one that does have some
  324. * online mems. The top cpuset always has some mems online.
  325. *
  326. * One way or another, we guarantee to return some non-empty subset
  327. * of node_states[N_MEMORY].
  328. *
  329. * Call with callback_lock or cpuset_mutex held.
  330. */
  331. static void guarantee_online_mems(struct cpuset *cs, nodemask_t *pmask)
  332. {
  333. while (!nodes_intersects(cs->effective_mems, node_states[N_MEMORY]))
  334. cs = parent_cs(cs);
  335. nodes_and(*pmask, cs->effective_mems, node_states[N_MEMORY]);
  336. }
  337. /**
  338. * alloc_cpumasks - allocate three cpumasks for cpuset
  339. * @cs: the cpuset that have cpumasks to be allocated.
  340. * @tmp: the tmpmasks structure pointer
  341. * Return: 0 if successful, -ENOMEM otherwise.
  342. *
  343. * Only one of the two input arguments should be non-NULL.
  344. */
  345. static inline int alloc_cpumasks(struct cpuset *cs, struct tmpmasks *tmp)
  346. {
  347. cpumask_var_t *pmask1, *pmask2, *pmask3, *pmask4;
  348. if (cs) {
  349. pmask1 = &cs->cpus_allowed;
  350. pmask2 = &cs->effective_cpus;
  351. pmask3 = &cs->effective_xcpus;
  352. pmask4 = &cs->exclusive_cpus;
  353. } else {
  354. pmask1 = &tmp->new_cpus;
  355. pmask2 = &tmp->addmask;
  356. pmask3 = &tmp->delmask;
  357. pmask4 = NULL;
  358. }
  359. if (!zalloc_cpumask_var(pmask1, GFP_KERNEL))
  360. return -ENOMEM;
  361. if (!zalloc_cpumask_var(pmask2, GFP_KERNEL))
  362. goto free_one;
  363. if (!zalloc_cpumask_var(pmask3, GFP_KERNEL))
  364. goto free_two;
  365. if (pmask4 && !zalloc_cpumask_var(pmask4, GFP_KERNEL))
  366. goto free_three;
  367. return 0;
  368. free_three:
  369. free_cpumask_var(*pmask3);
  370. free_two:
  371. free_cpumask_var(*pmask2);
  372. free_one:
  373. free_cpumask_var(*pmask1);
  374. return -ENOMEM;
  375. }
  376. /**
  377. * free_cpumasks - free cpumasks in a tmpmasks structure
  378. * @cs: the cpuset that have cpumasks to be free.
  379. * @tmp: the tmpmasks structure pointer
  380. */
  381. static inline void free_cpumasks(struct cpuset *cs, struct tmpmasks *tmp)
  382. {
  383. if (cs) {
  384. free_cpumask_var(cs->cpus_allowed);
  385. free_cpumask_var(cs->effective_cpus);
  386. free_cpumask_var(cs->effective_xcpus);
  387. free_cpumask_var(cs->exclusive_cpus);
  388. }
  389. if (tmp) {
  390. free_cpumask_var(tmp->new_cpus);
  391. free_cpumask_var(tmp->addmask);
  392. free_cpumask_var(tmp->delmask);
  393. }
  394. }
  395. /**
  396. * alloc_trial_cpuset - allocate a trial cpuset
  397. * @cs: the cpuset that the trial cpuset duplicates
  398. */
  399. static struct cpuset *alloc_trial_cpuset(struct cpuset *cs)
  400. {
  401. struct cpuset *trial;
  402. trial = kmemdup(cs, sizeof(*cs), GFP_KERNEL);
  403. if (!trial)
  404. return NULL;
  405. if (alloc_cpumasks(trial, NULL)) {
  406. kfree(trial);
  407. return NULL;
  408. }
  409. cpumask_copy(trial->cpus_allowed, cs->cpus_allowed);
  410. cpumask_copy(trial->effective_cpus, cs->effective_cpus);
  411. cpumask_copy(trial->effective_xcpus, cs->effective_xcpus);
  412. cpumask_copy(trial->exclusive_cpus, cs->exclusive_cpus);
  413. return trial;
  414. }
  415. /**
  416. * free_cpuset - free the cpuset
  417. * @cs: the cpuset to be freed
  418. */
  419. static inline void free_cpuset(struct cpuset *cs)
  420. {
  421. free_cpumasks(cs, NULL);
  422. kfree(cs);
  423. }
  424. /* Return user specified exclusive CPUs */
  425. static inline struct cpumask *user_xcpus(struct cpuset *cs)
  426. {
  427. return cpumask_empty(cs->exclusive_cpus) ? cs->cpus_allowed
  428. : cs->exclusive_cpus;
  429. }
  430. static inline bool xcpus_empty(struct cpuset *cs)
  431. {
  432. return cpumask_empty(cs->cpus_allowed) &&
  433. cpumask_empty(cs->exclusive_cpus);
  434. }
  435. /*
  436. * cpusets_are_exclusive() - check if two cpusets are exclusive
  437. *
  438. * Return true if exclusive, false if not
  439. */
  440. static inline bool cpusets_are_exclusive(struct cpuset *cs1, struct cpuset *cs2)
  441. {
  442. struct cpumask *xcpus1 = user_xcpus(cs1);
  443. struct cpumask *xcpus2 = user_xcpus(cs2);
  444. if (cpumask_intersects(xcpus1, xcpus2))
  445. return false;
  446. return true;
  447. }
  448. /*
  449. * validate_change() - Used to validate that any proposed cpuset change
  450. * follows the structural rules for cpusets.
  451. *
  452. * If we replaced the flag and mask values of the current cpuset
  453. * (cur) with those values in the trial cpuset (trial), would
  454. * our various subset and exclusive rules still be valid? Presumes
  455. * cpuset_mutex held.
  456. *
  457. * 'cur' is the address of an actual, in-use cpuset. Operations
  458. * such as list traversal that depend on the actual address of the
  459. * cpuset in the list must use cur below, not trial.
  460. *
  461. * 'trial' is the address of bulk structure copy of cur, with
  462. * perhaps one or more of the fields cpus_allowed, mems_allowed,
  463. * or flags changed to new, trial values.
  464. *
  465. * Return 0 if valid, -errno if not.
  466. */
  467. static int validate_change(struct cpuset *cur, struct cpuset *trial)
  468. {
  469. struct cgroup_subsys_state *css;
  470. struct cpuset *c, *par;
  471. int ret = 0;
  472. rcu_read_lock();
  473. if (!is_in_v2_mode())
  474. ret = cpuset1_validate_change(cur, trial);
  475. if (ret)
  476. goto out;
  477. /* Remaining checks don't apply to root cpuset */
  478. if (cur == &top_cpuset)
  479. goto out;
  480. par = parent_cs(cur);
  481. /*
  482. * Cpusets with tasks - existing or newly being attached - can't
  483. * be changed to have empty cpus_allowed or mems_allowed.
  484. */
  485. ret = -ENOSPC;
  486. if ((cgroup_is_populated(cur->css.cgroup) || cur->attach_in_progress)) {
  487. if (!cpumask_empty(cur->cpus_allowed) &&
  488. cpumask_empty(trial->cpus_allowed))
  489. goto out;
  490. if (!nodes_empty(cur->mems_allowed) &&
  491. nodes_empty(trial->mems_allowed))
  492. goto out;
  493. }
  494. /*
  495. * We can't shrink if we won't have enough room for SCHED_DEADLINE
  496. * tasks.
  497. */
  498. ret = -EBUSY;
  499. if (is_cpu_exclusive(cur) &&
  500. !cpuset_cpumask_can_shrink(cur->cpus_allowed,
  501. trial->cpus_allowed))
  502. goto out;
  503. /*
  504. * If either I or some sibling (!= me) is exclusive, we can't
  505. * overlap. exclusive_cpus cannot overlap with each other if set.
  506. */
  507. ret = -EINVAL;
  508. cpuset_for_each_child(c, css, par) {
  509. bool txset, cxset; /* Are exclusive_cpus set? */
  510. if (c == cur)
  511. continue;
  512. txset = !cpumask_empty(trial->exclusive_cpus);
  513. cxset = !cpumask_empty(c->exclusive_cpus);
  514. if (is_cpu_exclusive(trial) || is_cpu_exclusive(c) ||
  515. (txset && cxset)) {
  516. if (!cpusets_are_exclusive(trial, c))
  517. goto out;
  518. } else if (txset || cxset) {
  519. struct cpumask *xcpus, *acpus;
  520. /*
  521. * When just one of the exclusive_cpus's is set,
  522. * cpus_allowed of the other cpuset, if set, cannot be
  523. * a subset of it or none of those CPUs will be
  524. * available if these exclusive CPUs are activated.
  525. */
  526. if (txset) {
  527. xcpus = trial->exclusive_cpus;
  528. acpus = c->cpus_allowed;
  529. } else {
  530. xcpus = c->exclusive_cpus;
  531. acpus = trial->cpus_allowed;
  532. }
  533. if (!cpumask_empty(acpus) && cpumask_subset(acpus, xcpus))
  534. goto out;
  535. }
  536. if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
  537. nodes_intersects(trial->mems_allowed, c->mems_allowed))
  538. goto out;
  539. }
  540. ret = 0;
  541. out:
  542. rcu_read_unlock();
  543. return ret;
  544. }
  545. #ifdef CONFIG_SMP
  546. /*
  547. * Helper routine for generate_sched_domains().
  548. * Do cpusets a, b have overlapping effective cpus_allowed masks?
  549. */
  550. static int cpusets_overlap(struct cpuset *a, struct cpuset *b)
  551. {
  552. return cpumask_intersects(a->effective_cpus, b->effective_cpus);
  553. }
  554. static void
  555. update_domain_attr(struct sched_domain_attr *dattr, struct cpuset *c)
  556. {
  557. if (dattr->relax_domain_level < c->relax_domain_level)
  558. dattr->relax_domain_level = c->relax_domain_level;
  559. return;
  560. }
  561. static void update_domain_attr_tree(struct sched_domain_attr *dattr,
  562. struct cpuset *root_cs)
  563. {
  564. struct cpuset *cp;
  565. struct cgroup_subsys_state *pos_css;
  566. rcu_read_lock();
  567. cpuset_for_each_descendant_pre(cp, pos_css, root_cs) {
  568. /* skip the whole subtree if @cp doesn't have any CPU */
  569. if (cpumask_empty(cp->cpus_allowed)) {
  570. pos_css = css_rightmost_descendant(pos_css);
  571. continue;
  572. }
  573. if (is_sched_load_balance(cp))
  574. update_domain_attr(dattr, cp);
  575. }
  576. rcu_read_unlock();
  577. }
  578. /* Must be called with cpuset_mutex held. */
  579. static inline int nr_cpusets(void)
  580. {
  581. /* jump label reference count + the top-level cpuset */
  582. return static_key_count(&cpusets_enabled_key.key) + 1;
  583. }
  584. /*
  585. * generate_sched_domains()
  586. *
  587. * This function builds a partial partition of the systems CPUs
  588. * A 'partial partition' is a set of non-overlapping subsets whose
  589. * union is a subset of that set.
  590. * The output of this function needs to be passed to kernel/sched/core.c
  591. * partition_sched_domains() routine, which will rebuild the scheduler's
  592. * load balancing domains (sched domains) as specified by that partial
  593. * partition.
  594. *
  595. * See "What is sched_load_balance" in Documentation/admin-guide/cgroup-v1/cpusets.rst
  596. * for a background explanation of this.
  597. *
  598. * Does not return errors, on the theory that the callers of this
  599. * routine would rather not worry about failures to rebuild sched
  600. * domains when operating in the severe memory shortage situations
  601. * that could cause allocation failures below.
  602. *
  603. * Must be called with cpuset_mutex held.
  604. *
  605. * The three key local variables below are:
  606. * cp - cpuset pointer, used (together with pos_css) to perform a
  607. * top-down scan of all cpusets. For our purposes, rebuilding
  608. * the schedulers sched domains, we can ignore !is_sched_load_
  609. * balance cpusets.
  610. * csa - (for CpuSet Array) Array of pointers to all the cpusets
  611. * that need to be load balanced, for convenient iterative
  612. * access by the subsequent code that finds the best partition,
  613. * i.e the set of domains (subsets) of CPUs such that the
  614. * cpus_allowed of every cpuset marked is_sched_load_balance
  615. * is a subset of one of these domains, while there are as
  616. * many such domains as possible, each as small as possible.
  617. * doms - Conversion of 'csa' to an array of cpumasks, for passing to
  618. * the kernel/sched/core.c routine partition_sched_domains() in a
  619. * convenient format, that can be easily compared to the prior
  620. * value to determine what partition elements (sched domains)
  621. * were changed (added or removed.)
  622. *
  623. * Finding the best partition (set of domains):
  624. * The double nested loops below over i, j scan over the load
  625. * balanced cpusets (using the array of cpuset pointers in csa[])
  626. * looking for pairs of cpusets that have overlapping cpus_allowed
  627. * and merging them using a union-find algorithm.
  628. *
  629. * The union of the cpus_allowed masks from the set of all cpusets
  630. * having the same root then form the one element of the partition
  631. * (one sched domain) to be passed to partition_sched_domains().
  632. *
  633. */
  634. static int generate_sched_domains(cpumask_var_t **domains,
  635. struct sched_domain_attr **attributes)
  636. {
  637. struct cpuset *cp; /* top-down scan of cpusets */
  638. struct cpuset **csa; /* array of all cpuset ptrs */
  639. int csn; /* how many cpuset ptrs in csa so far */
  640. int i, j; /* indices for partition finding loops */
  641. cpumask_var_t *doms; /* resulting partition; i.e. sched domains */
  642. struct sched_domain_attr *dattr; /* attributes for custom domains */
  643. int ndoms = 0; /* number of sched domains in result */
  644. int nslot; /* next empty doms[] struct cpumask slot */
  645. struct cgroup_subsys_state *pos_css;
  646. bool root_load_balance = is_sched_load_balance(&top_cpuset);
  647. bool cgrpv2 = cgroup_subsys_on_dfl(cpuset_cgrp_subsys);
  648. int nslot_update;
  649. doms = NULL;
  650. dattr = NULL;
  651. csa = NULL;
  652. /* Special case for the 99% of systems with one, full, sched domain */
  653. if (root_load_balance && cpumask_empty(subpartitions_cpus)) {
  654. single_root_domain:
  655. ndoms = 1;
  656. doms = alloc_sched_domains(ndoms);
  657. if (!doms)
  658. goto done;
  659. dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
  660. if (dattr) {
  661. *dattr = SD_ATTR_INIT;
  662. update_domain_attr_tree(dattr, &top_cpuset);
  663. }
  664. cpumask_and(doms[0], top_cpuset.effective_cpus,
  665. housekeeping_cpumask(HK_TYPE_DOMAIN));
  666. goto done;
  667. }
  668. csa = kmalloc_array(nr_cpusets(), sizeof(cp), GFP_KERNEL);
  669. if (!csa)
  670. goto done;
  671. csn = 0;
  672. rcu_read_lock();
  673. if (root_load_balance)
  674. csa[csn++] = &top_cpuset;
  675. cpuset_for_each_descendant_pre(cp, pos_css, &top_cpuset) {
  676. if (cp == &top_cpuset)
  677. continue;
  678. if (cgrpv2)
  679. goto v2;
  680. /*
  681. * v1:
  682. * Continue traversing beyond @cp iff @cp has some CPUs and
  683. * isn't load balancing. The former is obvious. The
  684. * latter: All child cpusets contain a subset of the
  685. * parent's cpus, so just skip them, and then we call
  686. * update_domain_attr_tree() to calc relax_domain_level of
  687. * the corresponding sched domain.
  688. */
  689. if (!cpumask_empty(cp->cpus_allowed) &&
  690. !(is_sched_load_balance(cp) &&
  691. cpumask_intersects(cp->cpus_allowed,
  692. housekeeping_cpumask(HK_TYPE_DOMAIN))))
  693. continue;
  694. if (is_sched_load_balance(cp) &&
  695. !cpumask_empty(cp->effective_cpus))
  696. csa[csn++] = cp;
  697. /* skip @cp's subtree */
  698. pos_css = css_rightmost_descendant(pos_css);
  699. continue;
  700. v2:
  701. /*
  702. * Only valid partition roots that are not isolated and with
  703. * non-empty effective_cpus will be saved into csn[].
  704. */
  705. if ((cp->partition_root_state == PRS_ROOT) &&
  706. !cpumask_empty(cp->effective_cpus))
  707. csa[csn++] = cp;
  708. /*
  709. * Skip @cp's subtree if not a partition root and has no
  710. * exclusive CPUs to be granted to child cpusets.
  711. */
  712. if (!is_partition_valid(cp) && cpumask_empty(cp->exclusive_cpus))
  713. pos_css = css_rightmost_descendant(pos_css);
  714. }
  715. rcu_read_unlock();
  716. /*
  717. * If there are only isolated partitions underneath the cgroup root,
  718. * we can optimize out unneeded sched domains scanning.
  719. */
  720. if (root_load_balance && (csn == 1))
  721. goto single_root_domain;
  722. for (i = 0; i < csn; i++)
  723. uf_node_init(&csa[i]->node);
  724. /* Merge overlapping cpusets */
  725. for (i = 0; i < csn; i++) {
  726. for (j = i + 1; j < csn; j++) {
  727. if (cpusets_overlap(csa[i], csa[j])) {
  728. /*
  729. * Cgroup v2 shouldn't pass down overlapping
  730. * partition root cpusets.
  731. */
  732. WARN_ON_ONCE(cgrpv2);
  733. uf_union(&csa[i]->node, &csa[j]->node);
  734. }
  735. }
  736. }
  737. /* Count the total number of domains */
  738. for (i = 0; i < csn; i++) {
  739. if (uf_find(&csa[i]->node) == &csa[i]->node)
  740. ndoms++;
  741. }
  742. /*
  743. * Now we know how many domains to create.
  744. * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
  745. */
  746. doms = alloc_sched_domains(ndoms);
  747. if (!doms)
  748. goto done;
  749. /*
  750. * The rest of the code, including the scheduler, can deal with
  751. * dattr==NULL case. No need to abort if alloc fails.
  752. */
  753. dattr = kmalloc_array(ndoms, sizeof(struct sched_domain_attr),
  754. GFP_KERNEL);
  755. /*
  756. * Cgroup v2 doesn't support domain attributes, just set all of them
  757. * to SD_ATTR_INIT. Also non-isolating partition root CPUs are a
  758. * subset of HK_TYPE_DOMAIN housekeeping CPUs.
  759. */
  760. if (cgrpv2) {
  761. for (i = 0; i < ndoms; i++) {
  762. /*
  763. * The top cpuset may contain some boot time isolated
  764. * CPUs that need to be excluded from the sched domain.
  765. */
  766. if (csa[i] == &top_cpuset)
  767. cpumask_and(doms[i], csa[i]->effective_cpus,
  768. housekeeping_cpumask(HK_TYPE_DOMAIN));
  769. else
  770. cpumask_copy(doms[i], csa[i]->effective_cpus);
  771. if (dattr)
  772. dattr[i] = SD_ATTR_INIT;
  773. }
  774. goto done;
  775. }
  776. for (nslot = 0, i = 0; i < csn; i++) {
  777. nslot_update = 0;
  778. for (j = i; j < csn; j++) {
  779. if (uf_find(&csa[j]->node) == &csa[i]->node) {
  780. struct cpumask *dp = doms[nslot];
  781. if (i == j) {
  782. nslot_update = 1;
  783. cpumask_clear(dp);
  784. if (dattr)
  785. *(dattr + nslot) = SD_ATTR_INIT;
  786. }
  787. cpumask_or(dp, dp, csa[j]->effective_cpus);
  788. cpumask_and(dp, dp, housekeeping_cpumask(HK_TYPE_DOMAIN));
  789. if (dattr)
  790. update_domain_attr_tree(dattr + nslot, csa[j]);
  791. }
  792. }
  793. if (nslot_update)
  794. nslot++;
  795. }
  796. BUG_ON(nslot != ndoms);
  797. done:
  798. kfree(csa);
  799. /*
  800. * Fallback to the default domain if kmalloc() failed.
  801. * See comments in partition_sched_domains().
  802. */
  803. if (doms == NULL)
  804. ndoms = 1;
  805. *domains = doms;
  806. *attributes = dattr;
  807. return ndoms;
  808. }
  809. static void dl_update_tasks_root_domain(struct cpuset *cs)
  810. {
  811. struct css_task_iter it;
  812. struct task_struct *task;
  813. if (cs->nr_deadline_tasks == 0)
  814. return;
  815. css_task_iter_start(&cs->css, 0, &it);
  816. while ((task = css_task_iter_next(&it)))
  817. dl_add_task_root_domain(task);
  818. css_task_iter_end(&it);
  819. }
  820. static void dl_rebuild_rd_accounting(void)
  821. {
  822. struct cpuset *cs = NULL;
  823. struct cgroup_subsys_state *pos_css;
  824. lockdep_assert_held(&cpuset_mutex);
  825. lockdep_assert_cpus_held();
  826. lockdep_assert_held(&sched_domains_mutex);
  827. rcu_read_lock();
  828. /*
  829. * Clear default root domain DL accounting, it will be computed again
  830. * if a task belongs to it.
  831. */
  832. dl_clear_root_domain(&def_root_domain);
  833. cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) {
  834. if (cpumask_empty(cs->effective_cpus)) {
  835. pos_css = css_rightmost_descendant(pos_css);
  836. continue;
  837. }
  838. css_get(&cs->css);
  839. rcu_read_unlock();
  840. dl_update_tasks_root_domain(cs);
  841. rcu_read_lock();
  842. css_put(&cs->css);
  843. }
  844. rcu_read_unlock();
  845. }
  846. static void
  847. partition_and_rebuild_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
  848. struct sched_domain_attr *dattr_new)
  849. {
  850. mutex_lock(&sched_domains_mutex);
  851. partition_sched_domains_locked(ndoms_new, doms_new, dattr_new);
  852. dl_rebuild_rd_accounting();
  853. mutex_unlock(&sched_domains_mutex);
  854. }
  855. /*
  856. * Rebuild scheduler domains.
  857. *
  858. * If the flag 'sched_load_balance' of any cpuset with non-empty
  859. * 'cpus' changes, or if the 'cpus' allowed changes in any cpuset
  860. * which has that flag enabled, or if any cpuset with a non-empty
  861. * 'cpus' is removed, then call this routine to rebuild the
  862. * scheduler's dynamic sched domains.
  863. *
  864. * Call with cpuset_mutex held. Takes cpus_read_lock().
  865. */
  866. void rebuild_sched_domains_locked(void)
  867. {
  868. struct cgroup_subsys_state *pos_css;
  869. struct sched_domain_attr *attr;
  870. cpumask_var_t *doms;
  871. struct cpuset *cs;
  872. int ndoms;
  873. lockdep_assert_cpus_held();
  874. lockdep_assert_held(&cpuset_mutex);
  875. /*
  876. * If we have raced with CPU hotplug, return early to avoid
  877. * passing doms with offlined cpu to partition_sched_domains().
  878. * Anyways, cpuset_handle_hotplug() will rebuild sched domains.
  879. *
  880. * With no CPUs in any subpartitions, top_cpuset's effective CPUs
  881. * should be the same as the active CPUs, so checking only top_cpuset
  882. * is enough to detect racing CPU offlines.
  883. */
  884. if (cpumask_empty(subpartitions_cpus) &&
  885. !cpumask_equal(top_cpuset.effective_cpus, cpu_active_mask))
  886. return;
  887. /*
  888. * With subpartition CPUs, however, the effective CPUs of a partition
  889. * root should be only a subset of the active CPUs. Since a CPU in any
  890. * partition root could be offlined, all must be checked.
  891. */
  892. if (!cpumask_empty(subpartitions_cpus)) {
  893. rcu_read_lock();
  894. cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) {
  895. if (!is_partition_valid(cs)) {
  896. pos_css = css_rightmost_descendant(pos_css);
  897. continue;
  898. }
  899. if (!cpumask_subset(cs->effective_cpus,
  900. cpu_active_mask)) {
  901. rcu_read_unlock();
  902. return;
  903. }
  904. }
  905. rcu_read_unlock();
  906. }
  907. /* Generate domain masks and attrs */
  908. ndoms = generate_sched_domains(&doms, &attr);
  909. /* Have scheduler rebuild the domains */
  910. partition_and_rebuild_sched_domains(ndoms, doms, attr);
  911. }
  912. #else /* !CONFIG_SMP */
  913. void rebuild_sched_domains_locked(void)
  914. {
  915. }
  916. #endif /* CONFIG_SMP */
  917. static void rebuild_sched_domains_cpuslocked(void)
  918. {
  919. mutex_lock(&cpuset_mutex);
  920. rebuild_sched_domains_locked();
  921. mutex_unlock(&cpuset_mutex);
  922. }
  923. void rebuild_sched_domains(void)
  924. {
  925. cpus_read_lock();
  926. rebuild_sched_domains_cpuslocked();
  927. cpus_read_unlock();
  928. }
  929. /**
  930. * cpuset_update_tasks_cpumask - Update the cpumasks of tasks in the cpuset.
  931. * @cs: the cpuset in which each task's cpus_allowed mask needs to be changed
  932. * @new_cpus: the temp variable for the new effective_cpus mask
  933. *
  934. * Iterate through each task of @cs updating its cpus_allowed to the
  935. * effective cpuset's. As this function is called with cpuset_mutex held,
  936. * cpuset membership stays stable. For top_cpuset, task_cpu_possible_mask()
  937. * is used instead of effective_cpus to make sure all offline CPUs are also
  938. * included as hotplug code won't update cpumasks for tasks in top_cpuset.
  939. */
  940. void cpuset_update_tasks_cpumask(struct cpuset *cs, struct cpumask *new_cpus)
  941. {
  942. struct css_task_iter it;
  943. struct task_struct *task;
  944. bool top_cs = cs == &top_cpuset;
  945. css_task_iter_start(&cs->css, 0, &it);
  946. while ((task = css_task_iter_next(&it))) {
  947. const struct cpumask *possible_mask = task_cpu_possible_mask(task);
  948. if (top_cs) {
  949. /*
  950. * Percpu kthreads in top_cpuset are ignored
  951. */
  952. if (kthread_is_per_cpu(task))
  953. continue;
  954. cpumask_andnot(new_cpus, possible_mask, subpartitions_cpus);
  955. } else {
  956. cpumask_and(new_cpus, possible_mask, cs->effective_cpus);
  957. }
  958. set_cpus_allowed_ptr(task, new_cpus);
  959. }
  960. css_task_iter_end(&it);
  961. }
  962. /**
  963. * compute_effective_cpumask - Compute the effective cpumask of the cpuset
  964. * @new_cpus: the temp variable for the new effective_cpus mask
  965. * @cs: the cpuset the need to recompute the new effective_cpus mask
  966. * @parent: the parent cpuset
  967. *
  968. * The result is valid only if the given cpuset isn't a partition root.
  969. */
  970. static void compute_effective_cpumask(struct cpumask *new_cpus,
  971. struct cpuset *cs, struct cpuset *parent)
  972. {
  973. cpumask_and(new_cpus, cs->cpus_allowed, parent->effective_cpus);
  974. }
  975. /*
  976. * Commands for update_parent_effective_cpumask
  977. */
  978. enum partition_cmd {
  979. partcmd_enable, /* Enable partition root */
  980. partcmd_enablei, /* Enable isolated partition root */
  981. partcmd_disable, /* Disable partition root */
  982. partcmd_update, /* Update parent's effective_cpus */
  983. partcmd_invalidate, /* Make partition invalid */
  984. };
  985. static void update_sibling_cpumasks(struct cpuset *parent, struct cpuset *cs,
  986. struct tmpmasks *tmp);
  987. /*
  988. * Update partition exclusive flag
  989. *
  990. * Return: 0 if successful, an error code otherwise
  991. */
  992. static int update_partition_exclusive(struct cpuset *cs, int new_prs)
  993. {
  994. bool exclusive = (new_prs > PRS_MEMBER);
  995. if (exclusive && !is_cpu_exclusive(cs)) {
  996. if (cpuset_update_flag(CS_CPU_EXCLUSIVE, cs, 1))
  997. return PERR_NOTEXCL;
  998. } else if (!exclusive && is_cpu_exclusive(cs)) {
  999. /* Turning off CS_CPU_EXCLUSIVE will not return error */
  1000. cpuset_update_flag(CS_CPU_EXCLUSIVE, cs, 0);
  1001. }
  1002. return 0;
  1003. }
  1004. /*
  1005. * Update partition load balance flag and/or rebuild sched domain
  1006. *
  1007. * Changing load balance flag will automatically call
  1008. * rebuild_sched_domains_locked().
  1009. * This function is for cgroup v2 only.
  1010. */
  1011. static void update_partition_sd_lb(struct cpuset *cs, int old_prs)
  1012. {
  1013. int new_prs = cs->partition_root_state;
  1014. bool rebuild_domains = (new_prs > 0) || (old_prs > 0);
  1015. bool new_lb;
  1016. /*
  1017. * If cs is not a valid partition root, the load balance state
  1018. * will follow its parent.
  1019. */
  1020. if (new_prs > 0) {
  1021. new_lb = (new_prs != PRS_ISOLATED);
  1022. } else {
  1023. new_lb = is_sched_load_balance(parent_cs(cs));
  1024. }
  1025. if (new_lb != !!is_sched_load_balance(cs)) {
  1026. rebuild_domains = true;
  1027. if (new_lb)
  1028. set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
  1029. else
  1030. clear_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
  1031. }
  1032. if (rebuild_domains && !force_sd_rebuild)
  1033. rebuild_sched_domains_locked();
  1034. }
  1035. /*
  1036. * tasks_nocpu_error - Return true if tasks will have no effective_cpus
  1037. */
  1038. static bool tasks_nocpu_error(struct cpuset *parent, struct cpuset *cs,
  1039. struct cpumask *xcpus)
  1040. {
  1041. /*
  1042. * A populated partition (cs or parent) can't have empty effective_cpus
  1043. */
  1044. return (cpumask_subset(parent->effective_cpus, xcpus) &&
  1045. partition_is_populated(parent, cs)) ||
  1046. (!cpumask_intersects(xcpus, cpu_active_mask) &&
  1047. partition_is_populated(cs, NULL));
  1048. }
  1049. static void reset_partition_data(struct cpuset *cs)
  1050. {
  1051. struct cpuset *parent = parent_cs(cs);
  1052. if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys))
  1053. return;
  1054. lockdep_assert_held(&callback_lock);
  1055. cs->nr_subparts = 0;
  1056. if (cpumask_empty(cs->exclusive_cpus)) {
  1057. cpumask_clear(cs->effective_xcpus);
  1058. if (is_cpu_exclusive(cs))
  1059. clear_bit(CS_CPU_EXCLUSIVE, &cs->flags);
  1060. }
  1061. if (!cpumask_and(cs->effective_cpus, parent->effective_cpus, cs->cpus_allowed))
  1062. cpumask_copy(cs->effective_cpus, parent->effective_cpus);
  1063. }
  1064. /*
  1065. * partition_xcpus_newstate - Exclusive CPUs state change
  1066. * @old_prs: old partition_root_state
  1067. * @new_prs: new partition_root_state
  1068. * @xcpus: exclusive CPUs with state change
  1069. */
  1070. static void partition_xcpus_newstate(int old_prs, int new_prs, struct cpumask *xcpus)
  1071. {
  1072. WARN_ON_ONCE(old_prs == new_prs);
  1073. if (new_prs == PRS_ISOLATED)
  1074. cpumask_or(isolated_cpus, isolated_cpus, xcpus);
  1075. else
  1076. cpumask_andnot(isolated_cpus, isolated_cpus, xcpus);
  1077. }
  1078. /*
  1079. * partition_xcpus_add - Add new exclusive CPUs to partition
  1080. * @new_prs: new partition_root_state
  1081. * @parent: parent cpuset
  1082. * @xcpus: exclusive CPUs to be added
  1083. * Return: true if isolated_cpus modified, false otherwise
  1084. *
  1085. * Remote partition if parent == NULL
  1086. */
  1087. static bool partition_xcpus_add(int new_prs, struct cpuset *parent,
  1088. struct cpumask *xcpus)
  1089. {
  1090. bool isolcpus_updated;
  1091. WARN_ON_ONCE(new_prs < 0);
  1092. lockdep_assert_held(&callback_lock);
  1093. if (!parent)
  1094. parent = &top_cpuset;
  1095. if (parent == &top_cpuset)
  1096. cpumask_or(subpartitions_cpus, subpartitions_cpus, xcpus);
  1097. isolcpus_updated = (new_prs != parent->partition_root_state);
  1098. if (isolcpus_updated)
  1099. partition_xcpus_newstate(parent->partition_root_state, new_prs,
  1100. xcpus);
  1101. cpumask_andnot(parent->effective_cpus, parent->effective_cpus, xcpus);
  1102. return isolcpus_updated;
  1103. }
  1104. /*
  1105. * partition_xcpus_del - Remove exclusive CPUs from partition
  1106. * @old_prs: old partition_root_state
  1107. * @parent: parent cpuset
  1108. * @xcpus: exclusive CPUs to be removed
  1109. * Return: true if isolated_cpus modified, false otherwise
  1110. *
  1111. * Remote partition if parent == NULL
  1112. */
  1113. static bool partition_xcpus_del(int old_prs, struct cpuset *parent,
  1114. struct cpumask *xcpus)
  1115. {
  1116. bool isolcpus_updated;
  1117. WARN_ON_ONCE(old_prs < 0);
  1118. lockdep_assert_held(&callback_lock);
  1119. if (!parent)
  1120. parent = &top_cpuset;
  1121. if (parent == &top_cpuset)
  1122. cpumask_andnot(subpartitions_cpus, subpartitions_cpus, xcpus);
  1123. isolcpus_updated = (old_prs != parent->partition_root_state);
  1124. if (isolcpus_updated)
  1125. partition_xcpus_newstate(old_prs, parent->partition_root_state,
  1126. xcpus);
  1127. cpumask_and(xcpus, xcpus, cpu_active_mask);
  1128. cpumask_or(parent->effective_cpus, parent->effective_cpus, xcpus);
  1129. return isolcpus_updated;
  1130. }
  1131. static void update_unbound_workqueue_cpumask(bool isolcpus_updated)
  1132. {
  1133. int ret;
  1134. lockdep_assert_cpus_held();
  1135. if (!isolcpus_updated)
  1136. return;
  1137. ret = workqueue_unbound_exclude_cpumask(isolated_cpus);
  1138. WARN_ON_ONCE(ret < 0);
  1139. }
  1140. /**
  1141. * cpuset_cpu_is_isolated - Check if the given CPU is isolated
  1142. * @cpu: the CPU number to be checked
  1143. * Return: true if CPU is used in an isolated partition, false otherwise
  1144. */
  1145. bool cpuset_cpu_is_isolated(int cpu)
  1146. {
  1147. return cpumask_test_cpu(cpu, isolated_cpus);
  1148. }
  1149. EXPORT_SYMBOL_GPL(cpuset_cpu_is_isolated);
  1150. /*
  1151. * compute_effective_exclusive_cpumask - compute effective exclusive CPUs
  1152. * @cs: cpuset
  1153. * @xcpus: effective exclusive CPUs value to be set
  1154. * Return: true if xcpus is not empty, false otherwise.
  1155. *
  1156. * Starting with exclusive_cpus (cpus_allowed if exclusive_cpus is not set),
  1157. * it must be a subset of parent's effective_xcpus.
  1158. */
  1159. static bool compute_effective_exclusive_cpumask(struct cpuset *cs,
  1160. struct cpumask *xcpus)
  1161. {
  1162. struct cpuset *parent = parent_cs(cs);
  1163. if (!xcpus)
  1164. xcpus = cs->effective_xcpus;
  1165. return cpumask_and(xcpus, user_xcpus(cs), parent->effective_xcpus);
  1166. }
  1167. static inline bool is_remote_partition(struct cpuset *cs)
  1168. {
  1169. return !list_empty(&cs->remote_sibling);
  1170. }
  1171. static inline bool is_local_partition(struct cpuset *cs)
  1172. {
  1173. return is_partition_valid(cs) && !is_remote_partition(cs);
  1174. }
  1175. /*
  1176. * remote_partition_enable - Enable current cpuset as a remote partition root
  1177. * @cs: the cpuset to update
  1178. * @new_prs: new partition_root_state
  1179. * @tmp: temparary masks
  1180. * Return: 0 if successful, errcode if error
  1181. *
  1182. * Enable the current cpuset to become a remote partition root taking CPUs
  1183. * directly from the top cpuset. cpuset_mutex must be held by the caller.
  1184. */
  1185. static int remote_partition_enable(struct cpuset *cs, int new_prs,
  1186. struct tmpmasks *tmp)
  1187. {
  1188. bool isolcpus_updated;
  1189. /*
  1190. * The user must have sysadmin privilege.
  1191. */
  1192. if (!capable(CAP_SYS_ADMIN))
  1193. return PERR_ACCESS;
  1194. /*
  1195. * The requested exclusive_cpus must not be allocated to other
  1196. * partitions and it can't use up all the root's effective_cpus.
  1197. *
  1198. * Note that if there is any local partition root above it or
  1199. * remote partition root underneath it, its exclusive_cpus must
  1200. * have overlapped with subpartitions_cpus.
  1201. */
  1202. compute_effective_exclusive_cpumask(cs, tmp->new_cpus);
  1203. if (cpumask_empty(tmp->new_cpus) ||
  1204. cpumask_intersects(tmp->new_cpus, subpartitions_cpus) ||
  1205. cpumask_subset(top_cpuset.effective_cpus, tmp->new_cpus))
  1206. return PERR_INVCPUS;
  1207. spin_lock_irq(&callback_lock);
  1208. isolcpus_updated = partition_xcpus_add(new_prs, NULL, tmp->new_cpus);
  1209. list_add(&cs->remote_sibling, &remote_children);
  1210. spin_unlock_irq(&callback_lock);
  1211. update_unbound_workqueue_cpumask(isolcpus_updated);
  1212. /*
  1213. * Proprogate changes in top_cpuset's effective_cpus down the hierarchy.
  1214. */
  1215. cpuset_update_tasks_cpumask(&top_cpuset, tmp->new_cpus);
  1216. update_sibling_cpumasks(&top_cpuset, NULL, tmp);
  1217. return 0;
  1218. }
  1219. /*
  1220. * remote_partition_disable - Remove current cpuset from remote partition list
  1221. * @cs: the cpuset to update
  1222. * @tmp: temparary masks
  1223. *
  1224. * The effective_cpus is also updated.
  1225. *
  1226. * cpuset_mutex must be held by the caller.
  1227. */
  1228. static void remote_partition_disable(struct cpuset *cs, struct tmpmasks *tmp)
  1229. {
  1230. bool isolcpus_updated;
  1231. compute_effective_exclusive_cpumask(cs, tmp->new_cpus);
  1232. WARN_ON_ONCE(!is_remote_partition(cs));
  1233. WARN_ON_ONCE(!cpumask_subset(tmp->new_cpus, subpartitions_cpus));
  1234. spin_lock_irq(&callback_lock);
  1235. list_del_init(&cs->remote_sibling);
  1236. isolcpus_updated = partition_xcpus_del(cs->partition_root_state,
  1237. NULL, tmp->new_cpus);
  1238. cs->partition_root_state = -cs->partition_root_state;
  1239. if (!cs->prs_err)
  1240. cs->prs_err = PERR_INVCPUS;
  1241. reset_partition_data(cs);
  1242. spin_unlock_irq(&callback_lock);
  1243. update_unbound_workqueue_cpumask(isolcpus_updated);
  1244. /*
  1245. * Proprogate changes in top_cpuset's effective_cpus down the hierarchy.
  1246. */
  1247. cpuset_update_tasks_cpumask(&top_cpuset, tmp->new_cpus);
  1248. update_sibling_cpumasks(&top_cpuset, NULL, tmp);
  1249. }
  1250. /*
  1251. * remote_cpus_update - cpus_exclusive change of remote partition
  1252. * @cs: the cpuset to be updated
  1253. * @newmask: the new effective_xcpus mask
  1254. * @tmp: temparary masks
  1255. *
  1256. * top_cpuset and subpartitions_cpus will be updated or partition can be
  1257. * invalidated.
  1258. */
  1259. static void remote_cpus_update(struct cpuset *cs, struct cpumask *newmask,
  1260. struct tmpmasks *tmp)
  1261. {
  1262. bool adding, deleting;
  1263. int prs = cs->partition_root_state;
  1264. int isolcpus_updated = 0;
  1265. if (WARN_ON_ONCE(!is_remote_partition(cs)))
  1266. return;
  1267. WARN_ON_ONCE(!cpumask_subset(cs->effective_xcpus, subpartitions_cpus));
  1268. if (cpumask_empty(newmask))
  1269. goto invalidate;
  1270. adding = cpumask_andnot(tmp->addmask, newmask, cs->effective_xcpus);
  1271. deleting = cpumask_andnot(tmp->delmask, cs->effective_xcpus, newmask);
  1272. /*
  1273. * Additions of remote CPUs is only allowed if those CPUs are
  1274. * not allocated to other partitions and there are effective_cpus
  1275. * left in the top cpuset.
  1276. */
  1277. if (adding && (!capable(CAP_SYS_ADMIN) ||
  1278. cpumask_intersects(tmp->addmask, subpartitions_cpus) ||
  1279. cpumask_subset(top_cpuset.effective_cpus, tmp->addmask)))
  1280. goto invalidate;
  1281. spin_lock_irq(&callback_lock);
  1282. if (adding)
  1283. isolcpus_updated += partition_xcpus_add(prs, NULL, tmp->addmask);
  1284. if (deleting)
  1285. isolcpus_updated += partition_xcpus_del(prs, NULL, tmp->delmask);
  1286. spin_unlock_irq(&callback_lock);
  1287. update_unbound_workqueue_cpumask(isolcpus_updated);
  1288. /*
  1289. * Proprogate changes in top_cpuset's effective_cpus down the hierarchy.
  1290. */
  1291. cpuset_update_tasks_cpumask(&top_cpuset, tmp->new_cpus);
  1292. update_sibling_cpumasks(&top_cpuset, NULL, tmp);
  1293. return;
  1294. invalidate:
  1295. remote_partition_disable(cs, tmp);
  1296. }
  1297. /*
  1298. * remote_partition_check - check if a child remote partition needs update
  1299. * @cs: the cpuset to be updated
  1300. * @newmask: the new effective_xcpus mask
  1301. * @delmask: temporary mask for deletion (not in tmp)
  1302. * @tmp: temparary masks
  1303. *
  1304. * This should be called before the given cs has updated its cpus_allowed
  1305. * and/or effective_xcpus.
  1306. */
  1307. static void remote_partition_check(struct cpuset *cs, struct cpumask *newmask,
  1308. struct cpumask *delmask, struct tmpmasks *tmp)
  1309. {
  1310. struct cpuset *child, *next;
  1311. int disable_cnt = 0;
  1312. /*
  1313. * Compute the effective exclusive CPUs that will be deleted.
  1314. */
  1315. if (!cpumask_andnot(delmask, cs->effective_xcpus, newmask) ||
  1316. !cpumask_intersects(delmask, subpartitions_cpus))
  1317. return; /* No deletion of exclusive CPUs in partitions */
  1318. /*
  1319. * Searching the remote children list to look for those that will
  1320. * be impacted by the deletion of exclusive CPUs.
  1321. *
  1322. * Since a cpuset must be removed from the remote children list
  1323. * before it can go offline and holding cpuset_mutex will prevent
  1324. * any change in cpuset status. RCU read lock isn't needed.
  1325. */
  1326. lockdep_assert_held(&cpuset_mutex);
  1327. list_for_each_entry_safe(child, next, &remote_children, remote_sibling)
  1328. if (cpumask_intersects(child->effective_cpus, delmask)) {
  1329. remote_partition_disable(child, tmp);
  1330. disable_cnt++;
  1331. }
  1332. if (disable_cnt && !force_sd_rebuild)
  1333. rebuild_sched_domains_locked();
  1334. }
  1335. /*
  1336. * prstate_housekeeping_conflict - check for partition & housekeeping conflicts
  1337. * @prstate: partition root state to be checked
  1338. * @new_cpus: cpu mask
  1339. * Return: true if there is conflict, false otherwise
  1340. *
  1341. * CPUs outside of boot_hk_cpus, if defined, can only be used in an
  1342. * isolated partition.
  1343. */
  1344. static bool prstate_housekeeping_conflict(int prstate, struct cpumask *new_cpus)
  1345. {
  1346. if (!have_boot_isolcpus)
  1347. return false;
  1348. if ((prstate != PRS_ISOLATED) && !cpumask_subset(new_cpus, boot_hk_cpus))
  1349. return true;
  1350. return false;
  1351. }
  1352. /**
  1353. * update_parent_effective_cpumask - update effective_cpus mask of parent cpuset
  1354. * @cs: The cpuset that requests change in partition root state
  1355. * @cmd: Partition root state change command
  1356. * @newmask: Optional new cpumask for partcmd_update
  1357. * @tmp: Temporary addmask and delmask
  1358. * Return: 0 or a partition root state error code
  1359. *
  1360. * For partcmd_enable*, the cpuset is being transformed from a non-partition
  1361. * root to a partition root. The effective_xcpus (cpus_allowed if
  1362. * effective_xcpus not set) mask of the given cpuset will be taken away from
  1363. * parent's effective_cpus. The function will return 0 if all the CPUs listed
  1364. * in effective_xcpus can be granted or an error code will be returned.
  1365. *
  1366. * For partcmd_disable, the cpuset is being transformed from a partition
  1367. * root back to a non-partition root. Any CPUs in effective_xcpus will be
  1368. * given back to parent's effective_cpus. 0 will always be returned.
  1369. *
  1370. * For partcmd_update, if the optional newmask is specified, the cpu list is
  1371. * to be changed from effective_xcpus to newmask. Otherwise, effective_xcpus is
  1372. * assumed to remain the same. The cpuset should either be a valid or invalid
  1373. * partition root. The partition root state may change from valid to invalid
  1374. * or vice versa. An error code will be returned if transitioning from
  1375. * invalid to valid violates the exclusivity rule.
  1376. *
  1377. * For partcmd_invalidate, the current partition will be made invalid.
  1378. *
  1379. * The partcmd_enable* and partcmd_disable commands are used by
  1380. * update_prstate(). An error code may be returned and the caller will check
  1381. * for error.
  1382. *
  1383. * The partcmd_update command is used by update_cpumasks_hier() with newmask
  1384. * NULL and update_cpumask() with newmask set. The partcmd_invalidate is used
  1385. * by update_cpumask() with NULL newmask. In both cases, the callers won't
  1386. * check for error and so partition_root_state and prs_error will be updated
  1387. * directly.
  1388. */
  1389. static int update_parent_effective_cpumask(struct cpuset *cs, int cmd,
  1390. struct cpumask *newmask,
  1391. struct tmpmasks *tmp)
  1392. {
  1393. struct cpuset *parent = parent_cs(cs);
  1394. int adding; /* Adding cpus to parent's effective_cpus */
  1395. int deleting; /* Deleting cpus from parent's effective_cpus */
  1396. int old_prs, new_prs;
  1397. int part_error = PERR_NONE; /* Partition error? */
  1398. int subparts_delta = 0;
  1399. struct cpumask *xcpus; /* cs effective_xcpus */
  1400. int isolcpus_updated = 0;
  1401. bool nocpu;
  1402. lockdep_assert_held(&cpuset_mutex);
  1403. /*
  1404. * new_prs will only be changed for the partcmd_update and
  1405. * partcmd_invalidate commands.
  1406. */
  1407. adding = deleting = false;
  1408. old_prs = new_prs = cs->partition_root_state;
  1409. xcpus = user_xcpus(cs);
  1410. if (cmd == partcmd_invalidate) {
  1411. if (is_prs_invalid(old_prs))
  1412. return 0;
  1413. /*
  1414. * Make the current partition invalid.
  1415. */
  1416. if (is_partition_valid(parent))
  1417. adding = cpumask_and(tmp->addmask,
  1418. xcpus, parent->effective_xcpus);
  1419. if (old_prs > 0) {
  1420. new_prs = -old_prs;
  1421. subparts_delta--;
  1422. }
  1423. goto write_error;
  1424. }
  1425. /*
  1426. * The parent must be a partition root.
  1427. * The new cpumask, if present, or the current cpus_allowed must
  1428. * not be empty.
  1429. */
  1430. if (!is_partition_valid(parent)) {
  1431. return is_partition_invalid(parent)
  1432. ? PERR_INVPARENT : PERR_NOTPART;
  1433. }
  1434. if (!newmask && xcpus_empty(cs))
  1435. return PERR_CPUSEMPTY;
  1436. nocpu = tasks_nocpu_error(parent, cs, xcpus);
  1437. if ((cmd == partcmd_enable) || (cmd == partcmd_enablei)) {
  1438. /*
  1439. * Enabling partition root is not allowed if its
  1440. * effective_xcpus is empty or doesn't overlap with
  1441. * parent's effective_xcpus.
  1442. */
  1443. if (cpumask_empty(xcpus) ||
  1444. !cpumask_intersects(xcpus, parent->effective_xcpus))
  1445. return PERR_INVCPUS;
  1446. if (prstate_housekeeping_conflict(new_prs, xcpus))
  1447. return PERR_HKEEPING;
  1448. /*
  1449. * A parent can be left with no CPU as long as there is no
  1450. * task directly associated with the parent partition.
  1451. */
  1452. if (nocpu)
  1453. return PERR_NOCPUS;
  1454. cpumask_copy(tmp->delmask, xcpus);
  1455. deleting = true;
  1456. subparts_delta++;
  1457. new_prs = (cmd == partcmd_enable) ? PRS_ROOT : PRS_ISOLATED;
  1458. } else if (cmd == partcmd_disable) {
  1459. /*
  1460. * May need to add cpus to parent's effective_cpus for
  1461. * valid partition root.
  1462. */
  1463. adding = !is_prs_invalid(old_prs) &&
  1464. cpumask_and(tmp->addmask, xcpus, parent->effective_xcpus);
  1465. if (adding)
  1466. subparts_delta--;
  1467. new_prs = PRS_MEMBER;
  1468. } else if (newmask) {
  1469. /*
  1470. * Empty cpumask is not allowed
  1471. */
  1472. if (cpumask_empty(newmask)) {
  1473. part_error = PERR_CPUSEMPTY;
  1474. goto write_error;
  1475. }
  1476. /* Check newmask again, whether cpus are available for parent/cs */
  1477. nocpu |= tasks_nocpu_error(parent, cs, newmask);
  1478. /*
  1479. * partcmd_update with newmask:
  1480. *
  1481. * Compute add/delete mask to/from effective_cpus
  1482. *
  1483. * For valid partition:
  1484. * addmask = exclusive_cpus & ~newmask
  1485. * & parent->effective_xcpus
  1486. * delmask = newmask & ~exclusive_cpus
  1487. * & parent->effective_xcpus
  1488. *
  1489. * For invalid partition:
  1490. * delmask = newmask & parent->effective_xcpus
  1491. */
  1492. if (is_prs_invalid(old_prs)) {
  1493. adding = false;
  1494. deleting = cpumask_and(tmp->delmask,
  1495. newmask, parent->effective_xcpus);
  1496. } else {
  1497. cpumask_andnot(tmp->addmask, xcpus, newmask);
  1498. adding = cpumask_and(tmp->addmask, tmp->addmask,
  1499. parent->effective_xcpus);
  1500. cpumask_andnot(tmp->delmask, newmask, xcpus);
  1501. deleting = cpumask_and(tmp->delmask, tmp->delmask,
  1502. parent->effective_xcpus);
  1503. }
  1504. /*
  1505. * Make partition invalid if parent's effective_cpus could
  1506. * become empty and there are tasks in the parent.
  1507. */
  1508. if (nocpu && (!adding ||
  1509. !cpumask_intersects(tmp->addmask, cpu_active_mask))) {
  1510. part_error = PERR_NOCPUS;
  1511. deleting = false;
  1512. adding = cpumask_and(tmp->addmask,
  1513. xcpus, parent->effective_xcpus);
  1514. }
  1515. } else {
  1516. /*
  1517. * partcmd_update w/o newmask
  1518. *
  1519. * delmask = effective_xcpus & parent->effective_cpus
  1520. *
  1521. * This can be called from:
  1522. * 1) update_cpumasks_hier()
  1523. * 2) cpuset_hotplug_update_tasks()
  1524. *
  1525. * Check to see if it can be transitioned from valid to
  1526. * invalid partition or vice versa.
  1527. *
  1528. * A partition error happens when parent has tasks and all
  1529. * its effective CPUs will have to be distributed out.
  1530. */
  1531. WARN_ON_ONCE(!is_partition_valid(parent));
  1532. if (nocpu) {
  1533. part_error = PERR_NOCPUS;
  1534. if (is_partition_valid(cs))
  1535. adding = cpumask_and(tmp->addmask,
  1536. xcpus, parent->effective_xcpus);
  1537. } else if (is_partition_invalid(cs) &&
  1538. cpumask_subset(xcpus, parent->effective_xcpus)) {
  1539. struct cgroup_subsys_state *css;
  1540. struct cpuset *child;
  1541. bool exclusive = true;
  1542. /*
  1543. * Convert invalid partition to valid has to
  1544. * pass the cpu exclusivity test.
  1545. */
  1546. rcu_read_lock();
  1547. cpuset_for_each_child(child, css, parent) {
  1548. if (child == cs)
  1549. continue;
  1550. if (!cpusets_are_exclusive(cs, child)) {
  1551. exclusive = false;
  1552. break;
  1553. }
  1554. }
  1555. rcu_read_unlock();
  1556. if (exclusive)
  1557. deleting = cpumask_and(tmp->delmask,
  1558. xcpus, parent->effective_cpus);
  1559. else
  1560. part_error = PERR_NOTEXCL;
  1561. }
  1562. }
  1563. write_error:
  1564. if (part_error)
  1565. WRITE_ONCE(cs->prs_err, part_error);
  1566. if (cmd == partcmd_update) {
  1567. /*
  1568. * Check for possible transition between valid and invalid
  1569. * partition root.
  1570. */
  1571. switch (cs->partition_root_state) {
  1572. case PRS_ROOT:
  1573. case PRS_ISOLATED:
  1574. if (part_error) {
  1575. new_prs = -old_prs;
  1576. subparts_delta--;
  1577. }
  1578. break;
  1579. case PRS_INVALID_ROOT:
  1580. case PRS_INVALID_ISOLATED:
  1581. if (!part_error) {
  1582. new_prs = -old_prs;
  1583. subparts_delta++;
  1584. }
  1585. break;
  1586. }
  1587. }
  1588. if (!adding && !deleting && (new_prs == old_prs))
  1589. return 0;
  1590. /*
  1591. * Transitioning between invalid to valid or vice versa may require
  1592. * changing CS_CPU_EXCLUSIVE. In the case of partcmd_update,
  1593. * validate_change() has already been successfully called and
  1594. * CPU lists in cs haven't been updated yet. So defer it to later.
  1595. */
  1596. if ((old_prs != new_prs) && (cmd != partcmd_update)) {
  1597. int err = update_partition_exclusive(cs, new_prs);
  1598. if (err)
  1599. return err;
  1600. }
  1601. /*
  1602. * Change the parent's effective_cpus & effective_xcpus (top cpuset
  1603. * only).
  1604. *
  1605. * Newly added CPUs will be removed from effective_cpus and
  1606. * newly deleted ones will be added back to effective_cpus.
  1607. */
  1608. spin_lock_irq(&callback_lock);
  1609. if (old_prs != new_prs) {
  1610. cs->partition_root_state = new_prs;
  1611. if (new_prs <= 0)
  1612. cs->nr_subparts = 0;
  1613. }
  1614. /*
  1615. * Adding to parent's effective_cpus means deletion CPUs from cs
  1616. * and vice versa.
  1617. */
  1618. if (adding)
  1619. isolcpus_updated += partition_xcpus_del(old_prs, parent,
  1620. tmp->addmask);
  1621. if (deleting)
  1622. isolcpus_updated += partition_xcpus_add(new_prs, parent,
  1623. tmp->delmask);
  1624. if (is_partition_valid(parent)) {
  1625. parent->nr_subparts += subparts_delta;
  1626. WARN_ON_ONCE(parent->nr_subparts < 0);
  1627. }
  1628. spin_unlock_irq(&callback_lock);
  1629. update_unbound_workqueue_cpumask(isolcpus_updated);
  1630. if ((old_prs != new_prs) && (cmd == partcmd_update))
  1631. update_partition_exclusive(cs, new_prs);
  1632. if (adding || deleting) {
  1633. cpuset_update_tasks_cpumask(parent, tmp->addmask);
  1634. update_sibling_cpumasks(parent, cs, tmp);
  1635. }
  1636. /*
  1637. * For partcmd_update without newmask, it is being called from
  1638. * cpuset_handle_hotplug(). Update the load balance flag and
  1639. * scheduling domain accordingly.
  1640. */
  1641. if ((cmd == partcmd_update) && !newmask)
  1642. update_partition_sd_lb(cs, old_prs);
  1643. notify_partition_change(cs, old_prs);
  1644. return 0;
  1645. }
  1646. /**
  1647. * compute_partition_effective_cpumask - compute effective_cpus for partition
  1648. * @cs: partition root cpuset
  1649. * @new_ecpus: previously computed effective_cpus to be updated
  1650. *
  1651. * Compute the effective_cpus of a partition root by scanning effective_xcpus
  1652. * of child partition roots and excluding their effective_xcpus.
  1653. *
  1654. * This has the side effect of invalidating valid child partition roots,
  1655. * if necessary. Since it is called from either cpuset_hotplug_update_tasks()
  1656. * or update_cpumasks_hier() where parent and children are modified
  1657. * successively, we don't need to call update_parent_effective_cpumask()
  1658. * and the child's effective_cpus will be updated in later iterations.
  1659. *
  1660. * Note that rcu_read_lock() is assumed to be held.
  1661. */
  1662. static void compute_partition_effective_cpumask(struct cpuset *cs,
  1663. struct cpumask *new_ecpus)
  1664. {
  1665. struct cgroup_subsys_state *css;
  1666. struct cpuset *child;
  1667. bool populated = partition_is_populated(cs, NULL);
  1668. /*
  1669. * Check child partition roots to see if they should be
  1670. * invalidated when
  1671. * 1) child effective_xcpus not a subset of new
  1672. * excluisve_cpus
  1673. * 2) All the effective_cpus will be used up and cp
  1674. * has tasks
  1675. */
  1676. compute_effective_exclusive_cpumask(cs, new_ecpus);
  1677. cpumask_and(new_ecpus, new_ecpus, cpu_active_mask);
  1678. rcu_read_lock();
  1679. cpuset_for_each_child(child, css, cs) {
  1680. if (!is_partition_valid(child))
  1681. continue;
  1682. child->prs_err = 0;
  1683. if (!cpumask_subset(child->effective_xcpus,
  1684. cs->effective_xcpus))
  1685. child->prs_err = PERR_INVCPUS;
  1686. else if (populated &&
  1687. cpumask_subset(new_ecpus, child->effective_xcpus))
  1688. child->prs_err = PERR_NOCPUS;
  1689. if (child->prs_err) {
  1690. int old_prs = child->partition_root_state;
  1691. /*
  1692. * Invalidate child partition
  1693. */
  1694. spin_lock_irq(&callback_lock);
  1695. make_partition_invalid(child);
  1696. cs->nr_subparts--;
  1697. child->nr_subparts = 0;
  1698. spin_unlock_irq(&callback_lock);
  1699. notify_partition_change(child, old_prs);
  1700. continue;
  1701. }
  1702. cpumask_andnot(new_ecpus, new_ecpus,
  1703. child->effective_xcpus);
  1704. }
  1705. rcu_read_unlock();
  1706. }
  1707. /*
  1708. * update_cpumasks_hier() flags
  1709. */
  1710. #define HIER_CHECKALL 0x01 /* Check all cpusets with no skipping */
  1711. #define HIER_NO_SD_REBUILD 0x02 /* Don't rebuild sched domains */
  1712. /*
  1713. * update_cpumasks_hier - Update effective cpumasks and tasks in the subtree
  1714. * @cs: the cpuset to consider
  1715. * @tmp: temp variables for calculating effective_cpus & partition setup
  1716. * @force: don't skip any descendant cpusets if set
  1717. *
  1718. * When configured cpumask is changed, the effective cpumasks of this cpuset
  1719. * and all its descendants need to be updated.
  1720. *
  1721. * On legacy hierarchy, effective_cpus will be the same with cpu_allowed.
  1722. *
  1723. * Called with cpuset_mutex held
  1724. */
  1725. static void update_cpumasks_hier(struct cpuset *cs, struct tmpmasks *tmp,
  1726. int flags)
  1727. {
  1728. struct cpuset *cp;
  1729. struct cgroup_subsys_state *pos_css;
  1730. bool need_rebuild_sched_domains = false;
  1731. int old_prs, new_prs;
  1732. rcu_read_lock();
  1733. cpuset_for_each_descendant_pre(cp, pos_css, cs) {
  1734. struct cpuset *parent = parent_cs(cp);
  1735. bool remote = is_remote_partition(cp);
  1736. bool update_parent = false;
  1737. /*
  1738. * Skip descendent remote partition that acquires CPUs
  1739. * directly from top cpuset unless it is cs.
  1740. */
  1741. if (remote && (cp != cs)) {
  1742. pos_css = css_rightmost_descendant(pos_css);
  1743. continue;
  1744. }
  1745. /*
  1746. * Update effective_xcpus if exclusive_cpus set.
  1747. * The case when exclusive_cpus isn't set is handled later.
  1748. */
  1749. if (!cpumask_empty(cp->exclusive_cpus) && (cp != cs)) {
  1750. spin_lock_irq(&callback_lock);
  1751. compute_effective_exclusive_cpumask(cp, NULL);
  1752. spin_unlock_irq(&callback_lock);
  1753. }
  1754. old_prs = new_prs = cp->partition_root_state;
  1755. if (remote || (is_partition_valid(parent) &&
  1756. is_partition_valid(cp)))
  1757. compute_partition_effective_cpumask(cp, tmp->new_cpus);
  1758. else
  1759. compute_effective_cpumask(tmp->new_cpus, cp, parent);
  1760. /*
  1761. * A partition with no effective_cpus is allowed as long as
  1762. * there is no task associated with it. Call
  1763. * update_parent_effective_cpumask() to check it.
  1764. */
  1765. if (is_partition_valid(cp) && cpumask_empty(tmp->new_cpus)) {
  1766. update_parent = true;
  1767. goto update_parent_effective;
  1768. }
  1769. /*
  1770. * If it becomes empty, inherit the effective mask of the
  1771. * parent, which is guaranteed to have some CPUs unless
  1772. * it is a partition root that has explicitly distributed
  1773. * out all its CPUs.
  1774. */
  1775. if (is_in_v2_mode() && !remote && cpumask_empty(tmp->new_cpus))
  1776. cpumask_copy(tmp->new_cpus, parent->effective_cpus);
  1777. if (remote)
  1778. goto get_css;
  1779. /*
  1780. * Skip the whole subtree if
  1781. * 1) the cpumask remains the same,
  1782. * 2) has no partition root state,
  1783. * 3) HIER_CHECKALL flag not set, and
  1784. * 4) for v2 load balance state same as its parent.
  1785. */
  1786. if (!cp->partition_root_state && !(flags & HIER_CHECKALL) &&
  1787. cpumask_equal(tmp->new_cpus, cp->effective_cpus) &&
  1788. (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) ||
  1789. (is_sched_load_balance(parent) == is_sched_load_balance(cp)))) {
  1790. pos_css = css_rightmost_descendant(pos_css);
  1791. continue;
  1792. }
  1793. update_parent_effective:
  1794. /*
  1795. * update_parent_effective_cpumask() should have been called
  1796. * for cs already in update_cpumask(). We should also call
  1797. * cpuset_update_tasks_cpumask() again for tasks in the parent
  1798. * cpuset if the parent's effective_cpus changes.
  1799. */
  1800. if ((cp != cs) && old_prs) {
  1801. switch (parent->partition_root_state) {
  1802. case PRS_ROOT:
  1803. case PRS_ISOLATED:
  1804. update_parent = true;
  1805. break;
  1806. default:
  1807. /*
  1808. * When parent is not a partition root or is
  1809. * invalid, child partition roots become
  1810. * invalid too.
  1811. */
  1812. if (is_partition_valid(cp))
  1813. new_prs = -cp->partition_root_state;
  1814. WRITE_ONCE(cp->prs_err,
  1815. is_partition_invalid(parent)
  1816. ? PERR_INVPARENT : PERR_NOTPART);
  1817. break;
  1818. }
  1819. }
  1820. get_css:
  1821. if (!css_tryget_online(&cp->css))
  1822. continue;
  1823. rcu_read_unlock();
  1824. if (update_parent) {
  1825. update_parent_effective_cpumask(cp, partcmd_update, NULL, tmp);
  1826. /*
  1827. * The cpuset partition_root_state may become
  1828. * invalid. Capture it.
  1829. */
  1830. new_prs = cp->partition_root_state;
  1831. }
  1832. spin_lock_irq(&callback_lock);
  1833. cpumask_copy(cp->effective_cpus, tmp->new_cpus);
  1834. cp->partition_root_state = new_prs;
  1835. /*
  1836. * Make sure effective_xcpus is properly set for a valid
  1837. * partition root.
  1838. */
  1839. if ((new_prs > 0) && cpumask_empty(cp->exclusive_cpus))
  1840. cpumask_and(cp->effective_xcpus,
  1841. cp->cpus_allowed, parent->effective_xcpus);
  1842. else if (new_prs < 0)
  1843. reset_partition_data(cp);
  1844. spin_unlock_irq(&callback_lock);
  1845. notify_partition_change(cp, old_prs);
  1846. WARN_ON(!is_in_v2_mode() &&
  1847. !cpumask_equal(cp->cpus_allowed, cp->effective_cpus));
  1848. cpuset_update_tasks_cpumask(cp, cp->effective_cpus);
  1849. /*
  1850. * On default hierarchy, inherit the CS_SCHED_LOAD_BALANCE
  1851. * from parent if current cpuset isn't a valid partition root
  1852. * and their load balance states differ.
  1853. */
  1854. if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
  1855. !is_partition_valid(cp) &&
  1856. (is_sched_load_balance(parent) != is_sched_load_balance(cp))) {
  1857. if (is_sched_load_balance(parent))
  1858. set_bit(CS_SCHED_LOAD_BALANCE, &cp->flags);
  1859. else
  1860. clear_bit(CS_SCHED_LOAD_BALANCE, &cp->flags);
  1861. }
  1862. /*
  1863. * On legacy hierarchy, if the effective cpumask of any non-
  1864. * empty cpuset is changed, we need to rebuild sched domains.
  1865. * On default hierarchy, the cpuset needs to be a partition
  1866. * root as well.
  1867. */
  1868. if (!cpumask_empty(cp->cpus_allowed) &&
  1869. is_sched_load_balance(cp) &&
  1870. (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) ||
  1871. is_partition_valid(cp)))
  1872. need_rebuild_sched_domains = true;
  1873. rcu_read_lock();
  1874. css_put(&cp->css);
  1875. }
  1876. rcu_read_unlock();
  1877. if (need_rebuild_sched_domains && !(flags & HIER_NO_SD_REBUILD) &&
  1878. !force_sd_rebuild)
  1879. rebuild_sched_domains_locked();
  1880. }
  1881. /**
  1882. * update_sibling_cpumasks - Update siblings cpumasks
  1883. * @parent: Parent cpuset
  1884. * @cs: Current cpuset
  1885. * @tmp: Temp variables
  1886. */
  1887. static void update_sibling_cpumasks(struct cpuset *parent, struct cpuset *cs,
  1888. struct tmpmasks *tmp)
  1889. {
  1890. struct cpuset *sibling;
  1891. struct cgroup_subsys_state *pos_css;
  1892. lockdep_assert_held(&cpuset_mutex);
  1893. /*
  1894. * Check all its siblings and call update_cpumasks_hier()
  1895. * if their effective_cpus will need to be changed.
  1896. *
  1897. * It is possible a change in parent's effective_cpus
  1898. * due to a change in a child partition's effective_xcpus will impact
  1899. * its siblings even if they do not inherit parent's effective_cpus
  1900. * directly.
  1901. *
  1902. * The update_cpumasks_hier() function may sleep. So we have to
  1903. * release the RCU read lock before calling it. HIER_NO_SD_REBUILD
  1904. * flag is used to suppress rebuild of sched domains as the callers
  1905. * will take care of that.
  1906. */
  1907. rcu_read_lock();
  1908. cpuset_for_each_child(sibling, pos_css, parent) {
  1909. if (sibling == cs)
  1910. continue;
  1911. if (!is_partition_valid(sibling)) {
  1912. compute_effective_cpumask(tmp->new_cpus, sibling,
  1913. parent);
  1914. if (cpumask_equal(tmp->new_cpus, sibling->effective_cpus))
  1915. continue;
  1916. }
  1917. if (!css_tryget_online(&sibling->css))
  1918. continue;
  1919. rcu_read_unlock();
  1920. update_cpumasks_hier(sibling, tmp, HIER_NO_SD_REBUILD);
  1921. rcu_read_lock();
  1922. css_put(&sibling->css);
  1923. }
  1924. rcu_read_unlock();
  1925. }
  1926. /**
  1927. * update_cpumask - update the cpus_allowed mask of a cpuset and all tasks in it
  1928. * @cs: the cpuset to consider
  1929. * @trialcs: trial cpuset
  1930. * @buf: buffer of cpu numbers written to this cpuset
  1931. */
  1932. static int update_cpumask(struct cpuset *cs, struct cpuset *trialcs,
  1933. const char *buf)
  1934. {
  1935. int retval;
  1936. struct tmpmasks tmp;
  1937. struct cpuset *parent = parent_cs(cs);
  1938. bool invalidate = false;
  1939. int hier_flags = 0;
  1940. int old_prs = cs->partition_root_state;
  1941. /* top_cpuset.cpus_allowed tracks cpu_online_mask; it's read-only */
  1942. if (cs == &top_cpuset)
  1943. return -EACCES;
  1944. /*
  1945. * An empty cpus_allowed is ok only if the cpuset has no tasks.
  1946. * Since cpulist_parse() fails on an empty mask, we special case
  1947. * that parsing. The validate_change() call ensures that cpusets
  1948. * with tasks have cpus.
  1949. */
  1950. if (!*buf) {
  1951. cpumask_clear(trialcs->cpus_allowed);
  1952. if (cpumask_empty(trialcs->exclusive_cpus))
  1953. cpumask_clear(trialcs->effective_xcpus);
  1954. } else {
  1955. retval = cpulist_parse(buf, trialcs->cpus_allowed);
  1956. if (retval < 0)
  1957. return retval;
  1958. if (!cpumask_subset(trialcs->cpus_allowed,
  1959. top_cpuset.cpus_allowed))
  1960. return -EINVAL;
  1961. /*
  1962. * When exclusive_cpus isn't explicitly set, it is constrainted
  1963. * by cpus_allowed and parent's effective_xcpus. Otherwise,
  1964. * trialcs->effective_xcpus is used as a temporary cpumask
  1965. * for checking validity of the partition root.
  1966. */
  1967. if (!cpumask_empty(trialcs->exclusive_cpus) || is_partition_valid(cs))
  1968. compute_effective_exclusive_cpumask(trialcs, NULL);
  1969. }
  1970. /* Nothing to do if the cpus didn't change */
  1971. if (cpumask_equal(cs->cpus_allowed, trialcs->cpus_allowed))
  1972. return 0;
  1973. if (alloc_cpumasks(NULL, &tmp))
  1974. return -ENOMEM;
  1975. if (old_prs) {
  1976. if (is_partition_valid(cs) &&
  1977. cpumask_empty(trialcs->effective_xcpus)) {
  1978. invalidate = true;
  1979. cs->prs_err = PERR_INVCPUS;
  1980. } else if (prstate_housekeeping_conflict(old_prs, trialcs->effective_xcpus)) {
  1981. invalidate = true;
  1982. cs->prs_err = PERR_HKEEPING;
  1983. } else if (tasks_nocpu_error(parent, cs, trialcs->effective_xcpus)) {
  1984. invalidate = true;
  1985. cs->prs_err = PERR_NOCPUS;
  1986. }
  1987. }
  1988. /*
  1989. * Check all the descendants in update_cpumasks_hier() if
  1990. * effective_xcpus is to be changed.
  1991. */
  1992. if (!cpumask_equal(cs->effective_xcpus, trialcs->effective_xcpus))
  1993. hier_flags = HIER_CHECKALL;
  1994. retval = validate_change(cs, trialcs);
  1995. if ((retval == -EINVAL) && cgroup_subsys_on_dfl(cpuset_cgrp_subsys)) {
  1996. struct cgroup_subsys_state *css;
  1997. struct cpuset *cp;
  1998. /*
  1999. * The -EINVAL error code indicates that partition sibling
  2000. * CPU exclusivity rule has been violated. We still allow
  2001. * the cpumask change to proceed while invalidating the
  2002. * partition. However, any conflicting sibling partitions
  2003. * have to be marked as invalid too.
  2004. */
  2005. invalidate = true;
  2006. rcu_read_lock();
  2007. cpuset_for_each_child(cp, css, parent) {
  2008. struct cpumask *xcpus = user_xcpus(trialcs);
  2009. if (is_partition_valid(cp) &&
  2010. cpumask_intersects(xcpus, cp->effective_xcpus)) {
  2011. rcu_read_unlock();
  2012. update_parent_effective_cpumask(cp, partcmd_invalidate, NULL, &tmp);
  2013. rcu_read_lock();
  2014. }
  2015. }
  2016. rcu_read_unlock();
  2017. retval = 0;
  2018. }
  2019. if (retval < 0)
  2020. goto out_free;
  2021. if (is_partition_valid(cs) ||
  2022. (is_partition_invalid(cs) && !invalidate)) {
  2023. struct cpumask *xcpus = trialcs->effective_xcpus;
  2024. if (cpumask_empty(xcpus) && is_partition_invalid(cs))
  2025. xcpus = trialcs->cpus_allowed;
  2026. /*
  2027. * Call remote_cpus_update() to handle valid remote partition
  2028. */
  2029. if (is_remote_partition(cs))
  2030. remote_cpus_update(cs, xcpus, &tmp);
  2031. else if (invalidate)
  2032. update_parent_effective_cpumask(cs, partcmd_invalidate,
  2033. NULL, &tmp);
  2034. else
  2035. update_parent_effective_cpumask(cs, partcmd_update,
  2036. xcpus, &tmp);
  2037. } else if (!cpumask_empty(cs->exclusive_cpus)) {
  2038. /*
  2039. * Use trialcs->effective_cpus as a temp cpumask
  2040. */
  2041. remote_partition_check(cs, trialcs->effective_xcpus,
  2042. trialcs->effective_cpus, &tmp);
  2043. }
  2044. spin_lock_irq(&callback_lock);
  2045. cpumask_copy(cs->cpus_allowed, trialcs->cpus_allowed);
  2046. cpumask_copy(cs->effective_xcpus, trialcs->effective_xcpus);
  2047. if ((old_prs > 0) && !is_partition_valid(cs))
  2048. reset_partition_data(cs);
  2049. spin_unlock_irq(&callback_lock);
  2050. /* effective_cpus/effective_xcpus will be updated here */
  2051. update_cpumasks_hier(cs, &tmp, hier_flags);
  2052. /* Update CS_SCHED_LOAD_BALANCE and/or sched_domains, if necessary */
  2053. if (cs->partition_root_state)
  2054. update_partition_sd_lb(cs, old_prs);
  2055. out_free:
  2056. free_cpumasks(NULL, &tmp);
  2057. return retval;
  2058. }
  2059. /**
  2060. * update_exclusive_cpumask - update the exclusive_cpus mask of a cpuset
  2061. * @cs: the cpuset to consider
  2062. * @trialcs: trial cpuset
  2063. * @buf: buffer of cpu numbers written to this cpuset
  2064. *
  2065. * The tasks' cpumask will be updated if cs is a valid partition root.
  2066. */
  2067. static int update_exclusive_cpumask(struct cpuset *cs, struct cpuset *trialcs,
  2068. const char *buf)
  2069. {
  2070. int retval;
  2071. struct tmpmasks tmp;
  2072. struct cpuset *parent = parent_cs(cs);
  2073. bool invalidate = false;
  2074. int hier_flags = 0;
  2075. int old_prs = cs->partition_root_state;
  2076. if (!*buf) {
  2077. cpumask_clear(trialcs->exclusive_cpus);
  2078. cpumask_clear(trialcs->effective_xcpus);
  2079. } else {
  2080. retval = cpulist_parse(buf, trialcs->exclusive_cpus);
  2081. if (retval < 0)
  2082. return retval;
  2083. }
  2084. /* Nothing to do if the CPUs didn't change */
  2085. if (cpumask_equal(cs->exclusive_cpus, trialcs->exclusive_cpus))
  2086. return 0;
  2087. if (*buf)
  2088. compute_effective_exclusive_cpumask(trialcs, NULL);
  2089. /*
  2090. * Check all the descendants in update_cpumasks_hier() if
  2091. * effective_xcpus is to be changed.
  2092. */
  2093. if (!cpumask_equal(cs->effective_xcpus, trialcs->effective_xcpus))
  2094. hier_flags = HIER_CHECKALL;
  2095. retval = validate_change(cs, trialcs);
  2096. if (retval)
  2097. return retval;
  2098. if (alloc_cpumasks(NULL, &tmp))
  2099. return -ENOMEM;
  2100. if (old_prs) {
  2101. if (cpumask_empty(trialcs->effective_xcpus)) {
  2102. invalidate = true;
  2103. cs->prs_err = PERR_INVCPUS;
  2104. } else if (prstate_housekeeping_conflict(old_prs, trialcs->effective_xcpus)) {
  2105. invalidate = true;
  2106. cs->prs_err = PERR_HKEEPING;
  2107. } else if (tasks_nocpu_error(parent, cs, trialcs->effective_xcpus)) {
  2108. invalidate = true;
  2109. cs->prs_err = PERR_NOCPUS;
  2110. }
  2111. if (is_remote_partition(cs)) {
  2112. if (invalidate)
  2113. remote_partition_disable(cs, &tmp);
  2114. else
  2115. remote_cpus_update(cs, trialcs->effective_xcpus,
  2116. &tmp);
  2117. } else if (invalidate) {
  2118. update_parent_effective_cpumask(cs, partcmd_invalidate,
  2119. NULL, &tmp);
  2120. } else {
  2121. update_parent_effective_cpumask(cs, partcmd_update,
  2122. trialcs->effective_xcpus, &tmp);
  2123. }
  2124. } else if (!cpumask_empty(trialcs->exclusive_cpus)) {
  2125. /*
  2126. * Use trialcs->effective_cpus as a temp cpumask
  2127. */
  2128. remote_partition_check(cs, trialcs->effective_xcpus,
  2129. trialcs->effective_cpus, &tmp);
  2130. }
  2131. spin_lock_irq(&callback_lock);
  2132. cpumask_copy(cs->exclusive_cpus, trialcs->exclusive_cpus);
  2133. cpumask_copy(cs->effective_xcpus, trialcs->effective_xcpus);
  2134. if ((old_prs > 0) && !is_partition_valid(cs))
  2135. reset_partition_data(cs);
  2136. spin_unlock_irq(&callback_lock);
  2137. /*
  2138. * Call update_cpumasks_hier() to update effective_cpus/effective_xcpus
  2139. * of the subtree when it is a valid partition root or effective_xcpus
  2140. * is updated.
  2141. */
  2142. if (is_partition_valid(cs) || hier_flags)
  2143. update_cpumasks_hier(cs, &tmp, hier_flags);
  2144. /* Update CS_SCHED_LOAD_BALANCE and/or sched_domains, if necessary */
  2145. if (cs->partition_root_state)
  2146. update_partition_sd_lb(cs, old_prs);
  2147. free_cpumasks(NULL, &tmp);
  2148. return 0;
  2149. }
  2150. /*
  2151. * Migrate memory region from one set of nodes to another. This is
  2152. * performed asynchronously as it can be called from process migration path
  2153. * holding locks involved in process management. All mm migrations are
  2154. * performed in the queued order and can be waited for by flushing
  2155. * cpuset_migrate_mm_wq.
  2156. */
  2157. struct cpuset_migrate_mm_work {
  2158. struct work_struct work;
  2159. struct mm_struct *mm;
  2160. nodemask_t from;
  2161. nodemask_t to;
  2162. };
  2163. static void cpuset_migrate_mm_workfn(struct work_struct *work)
  2164. {
  2165. struct cpuset_migrate_mm_work *mwork =
  2166. container_of(work, struct cpuset_migrate_mm_work, work);
  2167. /* on a wq worker, no need to worry about %current's mems_allowed */
  2168. do_migrate_pages(mwork->mm, &mwork->from, &mwork->to, MPOL_MF_MOVE_ALL);
  2169. mmput(mwork->mm);
  2170. kfree(mwork);
  2171. }
  2172. static void cpuset_migrate_mm(struct mm_struct *mm, const nodemask_t *from,
  2173. const nodemask_t *to)
  2174. {
  2175. struct cpuset_migrate_mm_work *mwork;
  2176. if (nodes_equal(*from, *to)) {
  2177. mmput(mm);
  2178. return;
  2179. }
  2180. mwork = kzalloc(sizeof(*mwork), GFP_KERNEL);
  2181. if (mwork) {
  2182. mwork->mm = mm;
  2183. mwork->from = *from;
  2184. mwork->to = *to;
  2185. INIT_WORK(&mwork->work, cpuset_migrate_mm_workfn);
  2186. queue_work(cpuset_migrate_mm_wq, &mwork->work);
  2187. } else {
  2188. mmput(mm);
  2189. }
  2190. }
  2191. static void cpuset_post_attach(void)
  2192. {
  2193. flush_workqueue(cpuset_migrate_mm_wq);
  2194. }
  2195. /*
  2196. * cpuset_change_task_nodemask - change task's mems_allowed and mempolicy
  2197. * @tsk: the task to change
  2198. * @newmems: new nodes that the task will be set
  2199. *
  2200. * We use the mems_allowed_seq seqlock to safely update both tsk->mems_allowed
  2201. * and rebind an eventual tasks' mempolicy. If the task is allocating in
  2202. * parallel, it might temporarily see an empty intersection, which results in
  2203. * a seqlock check and retry before OOM or allocation failure.
  2204. */
  2205. static void cpuset_change_task_nodemask(struct task_struct *tsk,
  2206. nodemask_t *newmems)
  2207. {
  2208. task_lock(tsk);
  2209. local_irq_disable();
  2210. write_seqcount_begin(&tsk->mems_allowed_seq);
  2211. nodes_or(tsk->mems_allowed, tsk->mems_allowed, *newmems);
  2212. mpol_rebind_task(tsk, newmems);
  2213. tsk->mems_allowed = *newmems;
  2214. write_seqcount_end(&tsk->mems_allowed_seq);
  2215. local_irq_enable();
  2216. task_unlock(tsk);
  2217. }
  2218. static void *cpuset_being_rebound;
  2219. /**
  2220. * cpuset_update_tasks_nodemask - Update the nodemasks of tasks in the cpuset.
  2221. * @cs: the cpuset in which each task's mems_allowed mask needs to be changed
  2222. *
  2223. * Iterate through each task of @cs updating its mems_allowed to the
  2224. * effective cpuset's. As this function is called with cpuset_mutex held,
  2225. * cpuset membership stays stable.
  2226. */
  2227. void cpuset_update_tasks_nodemask(struct cpuset *cs)
  2228. {
  2229. static nodemask_t newmems; /* protected by cpuset_mutex */
  2230. struct css_task_iter it;
  2231. struct task_struct *task;
  2232. cpuset_being_rebound = cs; /* causes mpol_dup() rebind */
  2233. guarantee_online_mems(cs, &newmems);
  2234. /*
  2235. * The mpol_rebind_mm() call takes mmap_lock, which we couldn't
  2236. * take while holding tasklist_lock. Forks can happen - the
  2237. * mpol_dup() cpuset_being_rebound check will catch such forks,
  2238. * and rebind their vma mempolicies too. Because we still hold
  2239. * the global cpuset_mutex, we know that no other rebind effort
  2240. * will be contending for the global variable cpuset_being_rebound.
  2241. * It's ok if we rebind the same mm twice; mpol_rebind_mm()
  2242. * is idempotent. Also migrate pages in each mm to new nodes.
  2243. */
  2244. css_task_iter_start(&cs->css, 0, &it);
  2245. while ((task = css_task_iter_next(&it))) {
  2246. struct mm_struct *mm;
  2247. bool migrate;
  2248. cpuset_change_task_nodemask(task, &newmems);
  2249. mm = get_task_mm(task);
  2250. if (!mm)
  2251. continue;
  2252. migrate = is_memory_migrate(cs);
  2253. mpol_rebind_mm(mm, &cs->mems_allowed);
  2254. if (migrate)
  2255. cpuset_migrate_mm(mm, &cs->old_mems_allowed, &newmems);
  2256. else
  2257. mmput(mm);
  2258. }
  2259. css_task_iter_end(&it);
  2260. /*
  2261. * All the tasks' nodemasks have been updated, update
  2262. * cs->old_mems_allowed.
  2263. */
  2264. cs->old_mems_allowed = newmems;
  2265. /* We're done rebinding vmas to this cpuset's new mems_allowed. */
  2266. cpuset_being_rebound = NULL;
  2267. }
  2268. /*
  2269. * update_nodemasks_hier - Update effective nodemasks and tasks in the subtree
  2270. * @cs: the cpuset to consider
  2271. * @new_mems: a temp variable for calculating new effective_mems
  2272. *
  2273. * When configured nodemask is changed, the effective nodemasks of this cpuset
  2274. * and all its descendants need to be updated.
  2275. *
  2276. * On legacy hierarchy, effective_mems will be the same with mems_allowed.
  2277. *
  2278. * Called with cpuset_mutex held
  2279. */
  2280. static void update_nodemasks_hier(struct cpuset *cs, nodemask_t *new_mems)
  2281. {
  2282. struct cpuset *cp;
  2283. struct cgroup_subsys_state *pos_css;
  2284. rcu_read_lock();
  2285. cpuset_for_each_descendant_pre(cp, pos_css, cs) {
  2286. struct cpuset *parent = parent_cs(cp);
  2287. nodes_and(*new_mems, cp->mems_allowed, parent->effective_mems);
  2288. /*
  2289. * If it becomes empty, inherit the effective mask of the
  2290. * parent, which is guaranteed to have some MEMs.
  2291. */
  2292. if (is_in_v2_mode() && nodes_empty(*new_mems))
  2293. *new_mems = parent->effective_mems;
  2294. /* Skip the whole subtree if the nodemask remains the same. */
  2295. if (nodes_equal(*new_mems, cp->effective_mems)) {
  2296. pos_css = css_rightmost_descendant(pos_css);
  2297. continue;
  2298. }
  2299. if (!css_tryget_online(&cp->css))
  2300. continue;
  2301. rcu_read_unlock();
  2302. spin_lock_irq(&callback_lock);
  2303. cp->effective_mems = *new_mems;
  2304. spin_unlock_irq(&callback_lock);
  2305. WARN_ON(!is_in_v2_mode() &&
  2306. !nodes_equal(cp->mems_allowed, cp->effective_mems));
  2307. cpuset_update_tasks_nodemask(cp);
  2308. rcu_read_lock();
  2309. css_put(&cp->css);
  2310. }
  2311. rcu_read_unlock();
  2312. }
  2313. /*
  2314. * Handle user request to change the 'mems' memory placement
  2315. * of a cpuset. Needs to validate the request, update the
  2316. * cpusets mems_allowed, and for each task in the cpuset,
  2317. * update mems_allowed and rebind task's mempolicy and any vma
  2318. * mempolicies and if the cpuset is marked 'memory_migrate',
  2319. * migrate the tasks pages to the new memory.
  2320. *
  2321. * Call with cpuset_mutex held. May take callback_lock during call.
  2322. * Will take tasklist_lock, scan tasklist for tasks in cpuset cs,
  2323. * lock each such tasks mm->mmap_lock, scan its vma's and rebind
  2324. * their mempolicies to the cpusets new mems_allowed.
  2325. */
  2326. static int update_nodemask(struct cpuset *cs, struct cpuset *trialcs,
  2327. const char *buf)
  2328. {
  2329. int retval;
  2330. /*
  2331. * top_cpuset.mems_allowed tracks node_stats[N_MEMORY];
  2332. * it's read-only
  2333. */
  2334. if (cs == &top_cpuset) {
  2335. retval = -EACCES;
  2336. goto done;
  2337. }
  2338. /*
  2339. * An empty mems_allowed is ok iff there are no tasks in the cpuset.
  2340. * Since nodelist_parse() fails on an empty mask, we special case
  2341. * that parsing. The validate_change() call ensures that cpusets
  2342. * with tasks have memory.
  2343. */
  2344. if (!*buf) {
  2345. nodes_clear(trialcs->mems_allowed);
  2346. } else {
  2347. retval = nodelist_parse(buf, trialcs->mems_allowed);
  2348. if (retval < 0)
  2349. goto done;
  2350. if (!nodes_subset(trialcs->mems_allowed,
  2351. top_cpuset.mems_allowed)) {
  2352. retval = -EINVAL;
  2353. goto done;
  2354. }
  2355. }
  2356. if (nodes_equal(cs->mems_allowed, trialcs->mems_allowed)) {
  2357. retval = 0; /* Too easy - nothing to do */
  2358. goto done;
  2359. }
  2360. retval = validate_change(cs, trialcs);
  2361. if (retval < 0)
  2362. goto done;
  2363. check_insane_mems_config(&trialcs->mems_allowed);
  2364. spin_lock_irq(&callback_lock);
  2365. cs->mems_allowed = trialcs->mems_allowed;
  2366. spin_unlock_irq(&callback_lock);
  2367. /* use trialcs->mems_allowed as a temp variable */
  2368. update_nodemasks_hier(cs, &trialcs->mems_allowed);
  2369. done:
  2370. return retval;
  2371. }
  2372. bool current_cpuset_is_being_rebound(void)
  2373. {
  2374. bool ret;
  2375. rcu_read_lock();
  2376. ret = task_cs(current) == cpuset_being_rebound;
  2377. rcu_read_unlock();
  2378. return ret;
  2379. }
  2380. /*
  2381. * cpuset_update_flag - read a 0 or a 1 in a file and update associated flag
  2382. * bit: the bit to update (see cpuset_flagbits_t)
  2383. * cs: the cpuset to update
  2384. * turning_on: whether the flag is being set or cleared
  2385. *
  2386. * Call with cpuset_mutex held.
  2387. */
  2388. int cpuset_update_flag(cpuset_flagbits_t bit, struct cpuset *cs,
  2389. int turning_on)
  2390. {
  2391. struct cpuset *trialcs;
  2392. int balance_flag_changed;
  2393. int spread_flag_changed;
  2394. int err;
  2395. trialcs = alloc_trial_cpuset(cs);
  2396. if (!trialcs)
  2397. return -ENOMEM;
  2398. if (turning_on)
  2399. set_bit(bit, &trialcs->flags);
  2400. else
  2401. clear_bit(bit, &trialcs->flags);
  2402. err = validate_change(cs, trialcs);
  2403. if (err < 0)
  2404. goto out;
  2405. balance_flag_changed = (is_sched_load_balance(cs) !=
  2406. is_sched_load_balance(trialcs));
  2407. spread_flag_changed = ((is_spread_slab(cs) != is_spread_slab(trialcs))
  2408. || (is_spread_page(cs) != is_spread_page(trialcs)));
  2409. spin_lock_irq(&callback_lock);
  2410. cs->flags = trialcs->flags;
  2411. spin_unlock_irq(&callback_lock);
  2412. if (!cpumask_empty(trialcs->cpus_allowed) && balance_flag_changed &&
  2413. !force_sd_rebuild)
  2414. rebuild_sched_domains_locked();
  2415. if (spread_flag_changed)
  2416. cpuset1_update_tasks_flags(cs);
  2417. out:
  2418. free_cpuset(trialcs);
  2419. return err;
  2420. }
  2421. /**
  2422. * update_prstate - update partition_root_state
  2423. * @cs: the cpuset to update
  2424. * @new_prs: new partition root state
  2425. * Return: 0 if successful, != 0 if error
  2426. *
  2427. * Call with cpuset_mutex held.
  2428. */
  2429. static int update_prstate(struct cpuset *cs, int new_prs)
  2430. {
  2431. int err = PERR_NONE, old_prs = cs->partition_root_state;
  2432. struct cpuset *parent = parent_cs(cs);
  2433. struct tmpmasks tmpmask;
  2434. bool new_xcpus_state = false;
  2435. if (old_prs == new_prs)
  2436. return 0;
  2437. /*
  2438. * Treat a previously invalid partition root as if it is a "member".
  2439. */
  2440. if (new_prs && is_prs_invalid(old_prs))
  2441. old_prs = PRS_MEMBER;
  2442. if (alloc_cpumasks(NULL, &tmpmask))
  2443. return -ENOMEM;
  2444. /*
  2445. * Setup effective_xcpus if not properly set yet, it will be cleared
  2446. * later if partition becomes invalid.
  2447. */
  2448. if ((new_prs > 0) && cpumask_empty(cs->exclusive_cpus)) {
  2449. spin_lock_irq(&callback_lock);
  2450. cpumask_and(cs->effective_xcpus,
  2451. cs->cpus_allowed, parent->effective_xcpus);
  2452. spin_unlock_irq(&callback_lock);
  2453. }
  2454. err = update_partition_exclusive(cs, new_prs);
  2455. if (err)
  2456. goto out;
  2457. if (!old_prs) {
  2458. /*
  2459. * cpus_allowed and exclusive_cpus cannot be both empty.
  2460. */
  2461. if (xcpus_empty(cs)) {
  2462. err = PERR_CPUSEMPTY;
  2463. goto out;
  2464. }
  2465. /*
  2466. * If parent is valid partition, enable local partiion.
  2467. * Otherwise, enable a remote partition.
  2468. */
  2469. if (is_partition_valid(parent)) {
  2470. enum partition_cmd cmd = (new_prs == PRS_ROOT)
  2471. ? partcmd_enable : partcmd_enablei;
  2472. err = update_parent_effective_cpumask(cs, cmd, NULL, &tmpmask);
  2473. } else {
  2474. err = remote_partition_enable(cs, new_prs, &tmpmask);
  2475. }
  2476. } else if (old_prs && new_prs) {
  2477. /*
  2478. * A change in load balance state only, no change in cpumasks.
  2479. */
  2480. new_xcpus_state = true;
  2481. } else {
  2482. /*
  2483. * Switching back to member is always allowed even if it
  2484. * disables child partitions.
  2485. */
  2486. if (is_remote_partition(cs))
  2487. remote_partition_disable(cs, &tmpmask);
  2488. else
  2489. update_parent_effective_cpumask(cs, partcmd_disable,
  2490. NULL, &tmpmask);
  2491. /*
  2492. * Invalidation of child partitions will be done in
  2493. * update_cpumasks_hier().
  2494. */
  2495. }
  2496. out:
  2497. /*
  2498. * Make partition invalid & disable CS_CPU_EXCLUSIVE if an error
  2499. * happens.
  2500. */
  2501. if (err) {
  2502. new_prs = -new_prs;
  2503. update_partition_exclusive(cs, new_prs);
  2504. }
  2505. spin_lock_irq(&callback_lock);
  2506. cs->partition_root_state = new_prs;
  2507. WRITE_ONCE(cs->prs_err, err);
  2508. if (!is_partition_valid(cs))
  2509. reset_partition_data(cs);
  2510. else if (new_xcpus_state)
  2511. partition_xcpus_newstate(old_prs, new_prs, cs->effective_xcpus);
  2512. spin_unlock_irq(&callback_lock);
  2513. update_unbound_workqueue_cpumask(new_xcpus_state);
  2514. /* Force update if switching back to member */
  2515. update_cpumasks_hier(cs, &tmpmask, !new_prs ? HIER_CHECKALL : 0);
  2516. /* Update sched domains and load balance flag */
  2517. update_partition_sd_lb(cs, old_prs);
  2518. notify_partition_change(cs, old_prs);
  2519. free_cpumasks(NULL, &tmpmask);
  2520. return 0;
  2521. }
  2522. static struct cpuset *cpuset_attach_old_cs;
  2523. /*
  2524. * Check to see if a cpuset can accept a new task
  2525. * For v1, cpus_allowed and mems_allowed can't be empty.
  2526. * For v2, effective_cpus can't be empty.
  2527. * Note that in v1, effective_cpus = cpus_allowed.
  2528. */
  2529. static int cpuset_can_attach_check(struct cpuset *cs)
  2530. {
  2531. if (cpumask_empty(cs->effective_cpus) ||
  2532. (!is_in_v2_mode() && nodes_empty(cs->mems_allowed)))
  2533. return -ENOSPC;
  2534. return 0;
  2535. }
  2536. static void reset_migrate_dl_data(struct cpuset *cs)
  2537. {
  2538. cs->nr_migrate_dl_tasks = 0;
  2539. cs->sum_migrate_dl_bw = 0;
  2540. }
  2541. /* Called by cgroups to determine if a cpuset is usable; cpuset_mutex held */
  2542. static int cpuset_can_attach(struct cgroup_taskset *tset)
  2543. {
  2544. struct cgroup_subsys_state *css;
  2545. struct cpuset *cs, *oldcs;
  2546. struct task_struct *task;
  2547. bool cpus_updated, mems_updated;
  2548. int ret;
  2549. /* used later by cpuset_attach() */
  2550. cpuset_attach_old_cs = task_cs(cgroup_taskset_first(tset, &css));
  2551. oldcs = cpuset_attach_old_cs;
  2552. cs = css_cs(css);
  2553. mutex_lock(&cpuset_mutex);
  2554. /* Check to see if task is allowed in the cpuset */
  2555. ret = cpuset_can_attach_check(cs);
  2556. if (ret)
  2557. goto out_unlock;
  2558. cpus_updated = !cpumask_equal(cs->effective_cpus, oldcs->effective_cpus);
  2559. mems_updated = !nodes_equal(cs->effective_mems, oldcs->effective_mems);
  2560. cgroup_taskset_for_each(task, css, tset) {
  2561. ret = task_can_attach(task);
  2562. if (ret)
  2563. goto out_unlock;
  2564. /*
  2565. * Skip rights over task check in v2 when nothing changes,
  2566. * migration permission derives from hierarchy ownership in
  2567. * cgroup_procs_write_permission()).
  2568. */
  2569. if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) ||
  2570. (cpus_updated || mems_updated)) {
  2571. ret = security_task_setscheduler(task);
  2572. if (ret)
  2573. goto out_unlock;
  2574. }
  2575. if (dl_task(task)) {
  2576. cs->nr_migrate_dl_tasks++;
  2577. cs->sum_migrate_dl_bw += task->dl.dl_bw;
  2578. }
  2579. }
  2580. if (!cs->nr_migrate_dl_tasks)
  2581. goto out_success;
  2582. if (!cpumask_intersects(oldcs->effective_cpus, cs->effective_cpus)) {
  2583. int cpu = cpumask_any_and(cpu_active_mask, cs->effective_cpus);
  2584. if (unlikely(cpu >= nr_cpu_ids)) {
  2585. reset_migrate_dl_data(cs);
  2586. ret = -EINVAL;
  2587. goto out_unlock;
  2588. }
  2589. ret = dl_bw_alloc(cpu, cs->sum_migrate_dl_bw);
  2590. if (ret) {
  2591. reset_migrate_dl_data(cs);
  2592. goto out_unlock;
  2593. }
  2594. }
  2595. out_success:
  2596. /*
  2597. * Mark attach is in progress. This makes validate_change() fail
  2598. * changes which zero cpus/mems_allowed.
  2599. */
  2600. cs->attach_in_progress++;
  2601. out_unlock:
  2602. mutex_unlock(&cpuset_mutex);
  2603. return ret;
  2604. }
  2605. static void cpuset_cancel_attach(struct cgroup_taskset *tset)
  2606. {
  2607. struct cgroup_subsys_state *css;
  2608. struct cpuset *cs;
  2609. cgroup_taskset_first(tset, &css);
  2610. cs = css_cs(css);
  2611. mutex_lock(&cpuset_mutex);
  2612. dec_attach_in_progress_locked(cs);
  2613. if (cs->nr_migrate_dl_tasks) {
  2614. int cpu = cpumask_any(cs->effective_cpus);
  2615. dl_bw_free(cpu, cs->sum_migrate_dl_bw);
  2616. reset_migrate_dl_data(cs);
  2617. }
  2618. mutex_unlock(&cpuset_mutex);
  2619. }
  2620. /*
  2621. * Protected by cpuset_mutex. cpus_attach is used only by cpuset_attach_task()
  2622. * but we can't allocate it dynamically there. Define it global and
  2623. * allocate from cpuset_init().
  2624. */
  2625. static cpumask_var_t cpus_attach;
  2626. static nodemask_t cpuset_attach_nodemask_to;
  2627. static void cpuset_attach_task(struct cpuset *cs, struct task_struct *task)
  2628. {
  2629. lockdep_assert_held(&cpuset_mutex);
  2630. if (cs != &top_cpuset)
  2631. guarantee_online_cpus(task, cpus_attach);
  2632. else
  2633. cpumask_andnot(cpus_attach, task_cpu_possible_mask(task),
  2634. subpartitions_cpus);
  2635. /*
  2636. * can_attach beforehand should guarantee that this doesn't
  2637. * fail. TODO: have a better way to handle failure here
  2638. */
  2639. WARN_ON_ONCE(set_cpus_allowed_ptr(task, cpus_attach));
  2640. cpuset_change_task_nodemask(task, &cpuset_attach_nodemask_to);
  2641. cpuset1_update_task_spread_flags(cs, task);
  2642. }
  2643. static void cpuset_attach(struct cgroup_taskset *tset)
  2644. {
  2645. struct task_struct *task;
  2646. struct task_struct *leader;
  2647. struct cgroup_subsys_state *css;
  2648. struct cpuset *cs;
  2649. struct cpuset *oldcs = cpuset_attach_old_cs;
  2650. bool cpus_updated, mems_updated;
  2651. cgroup_taskset_first(tset, &css);
  2652. cs = css_cs(css);
  2653. lockdep_assert_cpus_held(); /* see cgroup_attach_lock() */
  2654. mutex_lock(&cpuset_mutex);
  2655. cpus_updated = !cpumask_equal(cs->effective_cpus,
  2656. oldcs->effective_cpus);
  2657. mems_updated = !nodes_equal(cs->effective_mems, oldcs->effective_mems);
  2658. /*
  2659. * In the default hierarchy, enabling cpuset in the child cgroups
  2660. * will trigger a number of cpuset_attach() calls with no change
  2661. * in effective cpus and mems. In that case, we can optimize out
  2662. * by skipping the task iteration and update.
  2663. */
  2664. if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
  2665. !cpus_updated && !mems_updated) {
  2666. cpuset_attach_nodemask_to = cs->effective_mems;
  2667. goto out;
  2668. }
  2669. guarantee_online_mems(cs, &cpuset_attach_nodemask_to);
  2670. cgroup_taskset_for_each(task, css, tset)
  2671. cpuset_attach_task(cs, task);
  2672. /*
  2673. * Change mm for all threadgroup leaders. This is expensive and may
  2674. * sleep and should be moved outside migration path proper. Skip it
  2675. * if there is no change in effective_mems and CS_MEMORY_MIGRATE is
  2676. * not set.
  2677. */
  2678. cpuset_attach_nodemask_to = cs->effective_mems;
  2679. if (!is_memory_migrate(cs) && !mems_updated)
  2680. goto out;
  2681. cgroup_taskset_for_each_leader(leader, css, tset) {
  2682. struct mm_struct *mm = get_task_mm(leader);
  2683. if (mm) {
  2684. mpol_rebind_mm(mm, &cpuset_attach_nodemask_to);
  2685. /*
  2686. * old_mems_allowed is the same with mems_allowed
  2687. * here, except if this task is being moved
  2688. * automatically due to hotplug. In that case
  2689. * @mems_allowed has been updated and is empty, so
  2690. * @old_mems_allowed is the right nodesets that we
  2691. * migrate mm from.
  2692. */
  2693. if (is_memory_migrate(cs))
  2694. cpuset_migrate_mm(mm, &oldcs->old_mems_allowed,
  2695. &cpuset_attach_nodemask_to);
  2696. else
  2697. mmput(mm);
  2698. }
  2699. }
  2700. out:
  2701. cs->old_mems_allowed = cpuset_attach_nodemask_to;
  2702. if (cs->nr_migrate_dl_tasks) {
  2703. cs->nr_deadline_tasks += cs->nr_migrate_dl_tasks;
  2704. oldcs->nr_deadline_tasks -= cs->nr_migrate_dl_tasks;
  2705. reset_migrate_dl_data(cs);
  2706. }
  2707. dec_attach_in_progress_locked(cs);
  2708. mutex_unlock(&cpuset_mutex);
  2709. }
  2710. /*
  2711. * Common handling for a write to a "cpus" or "mems" file.
  2712. */
  2713. ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
  2714. char *buf, size_t nbytes, loff_t off)
  2715. {
  2716. struct cpuset *cs = css_cs(of_css(of));
  2717. struct cpuset *trialcs;
  2718. int retval = -ENODEV;
  2719. buf = strstrip(buf);
  2720. cpus_read_lock();
  2721. mutex_lock(&cpuset_mutex);
  2722. if (!is_cpuset_online(cs))
  2723. goto out_unlock;
  2724. trialcs = alloc_trial_cpuset(cs);
  2725. if (!trialcs) {
  2726. retval = -ENOMEM;
  2727. goto out_unlock;
  2728. }
  2729. switch (of_cft(of)->private) {
  2730. case FILE_CPULIST:
  2731. retval = update_cpumask(cs, trialcs, buf);
  2732. break;
  2733. case FILE_EXCLUSIVE_CPULIST:
  2734. retval = update_exclusive_cpumask(cs, trialcs, buf);
  2735. break;
  2736. case FILE_MEMLIST:
  2737. retval = update_nodemask(cs, trialcs, buf);
  2738. break;
  2739. default:
  2740. retval = -EINVAL;
  2741. break;
  2742. }
  2743. free_cpuset(trialcs);
  2744. out_unlock:
  2745. mutex_unlock(&cpuset_mutex);
  2746. cpus_read_unlock();
  2747. flush_workqueue(cpuset_migrate_mm_wq);
  2748. return retval ?: nbytes;
  2749. }
  2750. /*
  2751. * These ascii lists should be read in a single call, by using a user
  2752. * buffer large enough to hold the entire map. If read in smaller
  2753. * chunks, there is no guarantee of atomicity. Since the display format
  2754. * used, list of ranges of sequential numbers, is variable length,
  2755. * and since these maps can change value dynamically, one could read
  2756. * gibberish by doing partial reads while a list was changing.
  2757. */
  2758. int cpuset_common_seq_show(struct seq_file *sf, void *v)
  2759. {
  2760. struct cpuset *cs = css_cs(seq_css(sf));
  2761. cpuset_filetype_t type = seq_cft(sf)->private;
  2762. int ret = 0;
  2763. spin_lock_irq(&callback_lock);
  2764. switch (type) {
  2765. case FILE_CPULIST:
  2766. seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->cpus_allowed));
  2767. break;
  2768. case FILE_MEMLIST:
  2769. seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->mems_allowed));
  2770. break;
  2771. case FILE_EFFECTIVE_CPULIST:
  2772. seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->effective_cpus));
  2773. break;
  2774. case FILE_EFFECTIVE_MEMLIST:
  2775. seq_printf(sf, "%*pbl\n", nodemask_pr_args(&cs->effective_mems));
  2776. break;
  2777. case FILE_EXCLUSIVE_CPULIST:
  2778. seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->exclusive_cpus));
  2779. break;
  2780. case FILE_EFFECTIVE_XCPULIST:
  2781. seq_printf(sf, "%*pbl\n", cpumask_pr_args(cs->effective_xcpus));
  2782. break;
  2783. case FILE_SUBPARTS_CPULIST:
  2784. seq_printf(sf, "%*pbl\n", cpumask_pr_args(subpartitions_cpus));
  2785. break;
  2786. case FILE_ISOLATED_CPULIST:
  2787. seq_printf(sf, "%*pbl\n", cpumask_pr_args(isolated_cpus));
  2788. break;
  2789. default:
  2790. ret = -EINVAL;
  2791. }
  2792. spin_unlock_irq(&callback_lock);
  2793. return ret;
  2794. }
  2795. static int sched_partition_show(struct seq_file *seq, void *v)
  2796. {
  2797. struct cpuset *cs = css_cs(seq_css(seq));
  2798. const char *err, *type = NULL;
  2799. switch (cs->partition_root_state) {
  2800. case PRS_ROOT:
  2801. seq_puts(seq, "root\n");
  2802. break;
  2803. case PRS_ISOLATED:
  2804. seq_puts(seq, "isolated\n");
  2805. break;
  2806. case PRS_MEMBER:
  2807. seq_puts(seq, "member\n");
  2808. break;
  2809. case PRS_INVALID_ROOT:
  2810. type = "root";
  2811. fallthrough;
  2812. case PRS_INVALID_ISOLATED:
  2813. if (!type)
  2814. type = "isolated";
  2815. err = perr_strings[READ_ONCE(cs->prs_err)];
  2816. if (err)
  2817. seq_printf(seq, "%s invalid (%s)\n", type, err);
  2818. else
  2819. seq_printf(seq, "%s invalid\n", type);
  2820. break;
  2821. }
  2822. return 0;
  2823. }
  2824. static ssize_t sched_partition_write(struct kernfs_open_file *of, char *buf,
  2825. size_t nbytes, loff_t off)
  2826. {
  2827. struct cpuset *cs = css_cs(of_css(of));
  2828. int val;
  2829. int retval = -ENODEV;
  2830. buf = strstrip(buf);
  2831. if (!strcmp(buf, "root"))
  2832. val = PRS_ROOT;
  2833. else if (!strcmp(buf, "member"))
  2834. val = PRS_MEMBER;
  2835. else if (!strcmp(buf, "isolated"))
  2836. val = PRS_ISOLATED;
  2837. else
  2838. return -EINVAL;
  2839. css_get(&cs->css);
  2840. cpus_read_lock();
  2841. mutex_lock(&cpuset_mutex);
  2842. if (!is_cpuset_online(cs))
  2843. goto out_unlock;
  2844. retval = update_prstate(cs, val);
  2845. out_unlock:
  2846. mutex_unlock(&cpuset_mutex);
  2847. cpus_read_unlock();
  2848. css_put(&cs->css);
  2849. return retval ?: nbytes;
  2850. }
  2851. /*
  2852. * This is currently a minimal set for the default hierarchy. It can be
  2853. * expanded later on by migrating more features and control files from v1.
  2854. */
  2855. static struct cftype dfl_files[] = {
  2856. {
  2857. .name = "cpus",
  2858. .seq_show = cpuset_common_seq_show,
  2859. .write = cpuset_write_resmask,
  2860. .max_write_len = (100U + 6 * NR_CPUS),
  2861. .private = FILE_CPULIST,
  2862. .flags = CFTYPE_NOT_ON_ROOT,
  2863. },
  2864. {
  2865. .name = "mems",
  2866. .seq_show = cpuset_common_seq_show,
  2867. .write = cpuset_write_resmask,
  2868. .max_write_len = (100U + 6 * MAX_NUMNODES),
  2869. .private = FILE_MEMLIST,
  2870. .flags = CFTYPE_NOT_ON_ROOT,
  2871. },
  2872. {
  2873. .name = "cpus.effective",
  2874. .seq_show = cpuset_common_seq_show,
  2875. .private = FILE_EFFECTIVE_CPULIST,
  2876. },
  2877. {
  2878. .name = "mems.effective",
  2879. .seq_show = cpuset_common_seq_show,
  2880. .private = FILE_EFFECTIVE_MEMLIST,
  2881. },
  2882. {
  2883. .name = "cpus.partition",
  2884. .seq_show = sched_partition_show,
  2885. .write = sched_partition_write,
  2886. .private = FILE_PARTITION_ROOT,
  2887. .flags = CFTYPE_NOT_ON_ROOT,
  2888. .file_offset = offsetof(struct cpuset, partition_file),
  2889. },
  2890. {
  2891. .name = "cpus.exclusive",
  2892. .seq_show = cpuset_common_seq_show,
  2893. .write = cpuset_write_resmask,
  2894. .max_write_len = (100U + 6 * NR_CPUS),
  2895. .private = FILE_EXCLUSIVE_CPULIST,
  2896. .flags = CFTYPE_NOT_ON_ROOT,
  2897. },
  2898. {
  2899. .name = "cpus.exclusive.effective",
  2900. .seq_show = cpuset_common_seq_show,
  2901. .private = FILE_EFFECTIVE_XCPULIST,
  2902. .flags = CFTYPE_NOT_ON_ROOT,
  2903. },
  2904. {
  2905. .name = "cpus.subpartitions",
  2906. .seq_show = cpuset_common_seq_show,
  2907. .private = FILE_SUBPARTS_CPULIST,
  2908. .flags = CFTYPE_ONLY_ON_ROOT | CFTYPE_DEBUG,
  2909. },
  2910. {
  2911. .name = "cpus.isolated",
  2912. .seq_show = cpuset_common_seq_show,
  2913. .private = FILE_ISOLATED_CPULIST,
  2914. .flags = CFTYPE_ONLY_ON_ROOT,
  2915. },
  2916. { } /* terminate */
  2917. };
  2918. /**
  2919. * cpuset_css_alloc - Allocate a cpuset css
  2920. * @parent_css: Parent css of the control group that the new cpuset will be
  2921. * part of
  2922. * Return: cpuset css on success, -ENOMEM on failure.
  2923. *
  2924. * Allocate and initialize a new cpuset css, for non-NULL @parent_css, return
  2925. * top cpuset css otherwise.
  2926. */
  2927. static struct cgroup_subsys_state *
  2928. cpuset_css_alloc(struct cgroup_subsys_state *parent_css)
  2929. {
  2930. struct cpuset *cs;
  2931. if (!parent_css)
  2932. return &top_cpuset.css;
  2933. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  2934. if (!cs)
  2935. return ERR_PTR(-ENOMEM);
  2936. if (alloc_cpumasks(cs, NULL)) {
  2937. kfree(cs);
  2938. return ERR_PTR(-ENOMEM);
  2939. }
  2940. __set_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
  2941. fmeter_init(&cs->fmeter);
  2942. cs->relax_domain_level = -1;
  2943. INIT_LIST_HEAD(&cs->remote_sibling);
  2944. /* Set CS_MEMORY_MIGRATE for default hierarchy */
  2945. if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys))
  2946. __set_bit(CS_MEMORY_MIGRATE, &cs->flags);
  2947. return &cs->css;
  2948. }
  2949. static int cpuset_css_online(struct cgroup_subsys_state *css)
  2950. {
  2951. struct cpuset *cs = css_cs(css);
  2952. struct cpuset *parent = parent_cs(cs);
  2953. struct cpuset *tmp_cs;
  2954. struct cgroup_subsys_state *pos_css;
  2955. if (!parent)
  2956. return 0;
  2957. cpus_read_lock();
  2958. mutex_lock(&cpuset_mutex);
  2959. set_bit(CS_ONLINE, &cs->flags);
  2960. if (is_spread_page(parent))
  2961. set_bit(CS_SPREAD_PAGE, &cs->flags);
  2962. if (is_spread_slab(parent))
  2963. set_bit(CS_SPREAD_SLAB, &cs->flags);
  2964. /*
  2965. * For v2, clear CS_SCHED_LOAD_BALANCE if parent is isolated
  2966. */
  2967. if (cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
  2968. !is_sched_load_balance(parent))
  2969. clear_bit(CS_SCHED_LOAD_BALANCE, &cs->flags);
  2970. cpuset_inc();
  2971. spin_lock_irq(&callback_lock);
  2972. if (is_in_v2_mode()) {
  2973. cpumask_copy(cs->effective_cpus, parent->effective_cpus);
  2974. cs->effective_mems = parent->effective_mems;
  2975. }
  2976. spin_unlock_irq(&callback_lock);
  2977. if (!test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags))
  2978. goto out_unlock;
  2979. /*
  2980. * Clone @parent's configuration if CGRP_CPUSET_CLONE_CHILDREN is
  2981. * set. This flag handling is implemented in cgroup core for
  2982. * historical reasons - the flag may be specified during mount.
  2983. *
  2984. * Currently, if any sibling cpusets have exclusive cpus or mem, we
  2985. * refuse to clone the configuration - thereby refusing the task to
  2986. * be entered, and as a result refusing the sys_unshare() or
  2987. * clone() which initiated it. If this becomes a problem for some
  2988. * users who wish to allow that scenario, then this could be
  2989. * changed to grant parent->cpus_allowed-sibling_cpus_exclusive
  2990. * (and likewise for mems) to the new cgroup.
  2991. */
  2992. rcu_read_lock();
  2993. cpuset_for_each_child(tmp_cs, pos_css, parent) {
  2994. if (is_mem_exclusive(tmp_cs) || is_cpu_exclusive(tmp_cs)) {
  2995. rcu_read_unlock();
  2996. goto out_unlock;
  2997. }
  2998. }
  2999. rcu_read_unlock();
  3000. spin_lock_irq(&callback_lock);
  3001. cs->mems_allowed = parent->mems_allowed;
  3002. cs->effective_mems = parent->mems_allowed;
  3003. cpumask_copy(cs->cpus_allowed, parent->cpus_allowed);
  3004. cpumask_copy(cs->effective_cpus, parent->cpus_allowed);
  3005. spin_unlock_irq(&callback_lock);
  3006. out_unlock:
  3007. mutex_unlock(&cpuset_mutex);
  3008. cpus_read_unlock();
  3009. return 0;
  3010. }
  3011. /*
  3012. * If the cpuset being removed has its flag 'sched_load_balance'
  3013. * enabled, then simulate turning sched_load_balance off, which
  3014. * will call rebuild_sched_domains_locked(). That is not needed
  3015. * in the default hierarchy where only changes in partition
  3016. * will cause repartitioning.
  3017. *
  3018. * If the cpuset has the 'sched.partition' flag enabled, simulate
  3019. * turning 'sched.partition" off.
  3020. */
  3021. static void cpuset_css_offline(struct cgroup_subsys_state *css)
  3022. {
  3023. struct cpuset *cs = css_cs(css);
  3024. cpus_read_lock();
  3025. mutex_lock(&cpuset_mutex);
  3026. if (is_partition_valid(cs))
  3027. update_prstate(cs, 0);
  3028. if (!cgroup_subsys_on_dfl(cpuset_cgrp_subsys) &&
  3029. is_sched_load_balance(cs))
  3030. cpuset_update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
  3031. cpuset_dec();
  3032. clear_bit(CS_ONLINE, &cs->flags);
  3033. mutex_unlock(&cpuset_mutex);
  3034. cpus_read_unlock();
  3035. }
  3036. static void cpuset_css_free(struct cgroup_subsys_state *css)
  3037. {
  3038. struct cpuset *cs = css_cs(css);
  3039. free_cpuset(cs);
  3040. }
  3041. static void cpuset_bind(struct cgroup_subsys_state *root_css)
  3042. {
  3043. mutex_lock(&cpuset_mutex);
  3044. spin_lock_irq(&callback_lock);
  3045. if (is_in_v2_mode()) {
  3046. cpumask_copy(top_cpuset.cpus_allowed, cpu_possible_mask);
  3047. cpumask_copy(top_cpuset.effective_xcpus, cpu_possible_mask);
  3048. top_cpuset.mems_allowed = node_possible_map;
  3049. } else {
  3050. cpumask_copy(top_cpuset.cpus_allowed,
  3051. top_cpuset.effective_cpus);
  3052. top_cpuset.mems_allowed = top_cpuset.effective_mems;
  3053. }
  3054. spin_unlock_irq(&callback_lock);
  3055. mutex_unlock(&cpuset_mutex);
  3056. }
  3057. /*
  3058. * In case the child is cloned into a cpuset different from its parent,
  3059. * additional checks are done to see if the move is allowed.
  3060. */
  3061. static int cpuset_can_fork(struct task_struct *task, struct css_set *cset)
  3062. {
  3063. struct cpuset *cs = css_cs(cset->subsys[cpuset_cgrp_id]);
  3064. bool same_cs;
  3065. int ret;
  3066. rcu_read_lock();
  3067. same_cs = (cs == task_cs(current));
  3068. rcu_read_unlock();
  3069. if (same_cs)
  3070. return 0;
  3071. lockdep_assert_held(&cgroup_mutex);
  3072. mutex_lock(&cpuset_mutex);
  3073. /* Check to see if task is allowed in the cpuset */
  3074. ret = cpuset_can_attach_check(cs);
  3075. if (ret)
  3076. goto out_unlock;
  3077. ret = task_can_attach(task);
  3078. if (ret)
  3079. goto out_unlock;
  3080. ret = security_task_setscheduler(task);
  3081. if (ret)
  3082. goto out_unlock;
  3083. /*
  3084. * Mark attach is in progress. This makes validate_change() fail
  3085. * changes which zero cpus/mems_allowed.
  3086. */
  3087. cs->attach_in_progress++;
  3088. out_unlock:
  3089. mutex_unlock(&cpuset_mutex);
  3090. return ret;
  3091. }
  3092. static void cpuset_cancel_fork(struct task_struct *task, struct css_set *cset)
  3093. {
  3094. struct cpuset *cs = css_cs(cset->subsys[cpuset_cgrp_id]);
  3095. bool same_cs;
  3096. rcu_read_lock();
  3097. same_cs = (cs == task_cs(current));
  3098. rcu_read_unlock();
  3099. if (same_cs)
  3100. return;
  3101. dec_attach_in_progress(cs);
  3102. }
  3103. /*
  3104. * Make sure the new task conform to the current state of its parent,
  3105. * which could have been changed by cpuset just after it inherits the
  3106. * state from the parent and before it sits on the cgroup's task list.
  3107. */
  3108. static void cpuset_fork(struct task_struct *task)
  3109. {
  3110. struct cpuset *cs;
  3111. bool same_cs;
  3112. rcu_read_lock();
  3113. cs = task_cs(task);
  3114. same_cs = (cs == task_cs(current));
  3115. rcu_read_unlock();
  3116. if (same_cs) {
  3117. if (cs == &top_cpuset)
  3118. return;
  3119. set_cpus_allowed_ptr(task, current->cpus_ptr);
  3120. task->mems_allowed = current->mems_allowed;
  3121. return;
  3122. }
  3123. /* CLONE_INTO_CGROUP */
  3124. mutex_lock(&cpuset_mutex);
  3125. guarantee_online_mems(cs, &cpuset_attach_nodemask_to);
  3126. cpuset_attach_task(cs, task);
  3127. dec_attach_in_progress_locked(cs);
  3128. mutex_unlock(&cpuset_mutex);
  3129. }
  3130. struct cgroup_subsys cpuset_cgrp_subsys = {
  3131. .css_alloc = cpuset_css_alloc,
  3132. .css_online = cpuset_css_online,
  3133. .css_offline = cpuset_css_offline,
  3134. .css_free = cpuset_css_free,
  3135. .can_attach = cpuset_can_attach,
  3136. .cancel_attach = cpuset_cancel_attach,
  3137. .attach = cpuset_attach,
  3138. .post_attach = cpuset_post_attach,
  3139. .bind = cpuset_bind,
  3140. .can_fork = cpuset_can_fork,
  3141. .cancel_fork = cpuset_cancel_fork,
  3142. .fork = cpuset_fork,
  3143. #ifdef CONFIG_CPUSETS_V1
  3144. .legacy_cftypes = cpuset1_files,
  3145. #endif
  3146. .dfl_cftypes = dfl_files,
  3147. .early_init = true,
  3148. .threaded = true,
  3149. };
  3150. /**
  3151. * cpuset_init - initialize cpusets at system boot
  3152. *
  3153. * Description: Initialize top_cpuset
  3154. **/
  3155. int __init cpuset_init(void)
  3156. {
  3157. BUG_ON(!alloc_cpumask_var(&top_cpuset.cpus_allowed, GFP_KERNEL));
  3158. BUG_ON(!alloc_cpumask_var(&top_cpuset.effective_cpus, GFP_KERNEL));
  3159. BUG_ON(!alloc_cpumask_var(&top_cpuset.effective_xcpus, GFP_KERNEL));
  3160. BUG_ON(!alloc_cpumask_var(&top_cpuset.exclusive_cpus, GFP_KERNEL));
  3161. BUG_ON(!zalloc_cpumask_var(&subpartitions_cpus, GFP_KERNEL));
  3162. BUG_ON(!zalloc_cpumask_var(&isolated_cpus, GFP_KERNEL));
  3163. cpumask_setall(top_cpuset.cpus_allowed);
  3164. nodes_setall(top_cpuset.mems_allowed);
  3165. cpumask_setall(top_cpuset.effective_cpus);
  3166. cpumask_setall(top_cpuset.effective_xcpus);
  3167. cpumask_setall(top_cpuset.exclusive_cpus);
  3168. nodes_setall(top_cpuset.effective_mems);
  3169. fmeter_init(&top_cpuset.fmeter);
  3170. INIT_LIST_HEAD(&remote_children);
  3171. BUG_ON(!alloc_cpumask_var(&cpus_attach, GFP_KERNEL));
  3172. have_boot_isolcpus = housekeeping_enabled(HK_TYPE_DOMAIN);
  3173. if (have_boot_isolcpus) {
  3174. BUG_ON(!alloc_cpumask_var(&boot_hk_cpus, GFP_KERNEL));
  3175. cpumask_copy(boot_hk_cpus, housekeeping_cpumask(HK_TYPE_DOMAIN));
  3176. cpumask_andnot(isolated_cpus, cpu_possible_mask, boot_hk_cpus);
  3177. }
  3178. return 0;
  3179. }
  3180. static void
  3181. hotplug_update_tasks(struct cpuset *cs,
  3182. struct cpumask *new_cpus, nodemask_t *new_mems,
  3183. bool cpus_updated, bool mems_updated)
  3184. {
  3185. /* A partition root is allowed to have empty effective cpus */
  3186. if (cpumask_empty(new_cpus) && !is_partition_valid(cs))
  3187. cpumask_copy(new_cpus, parent_cs(cs)->effective_cpus);
  3188. if (nodes_empty(*new_mems))
  3189. *new_mems = parent_cs(cs)->effective_mems;
  3190. spin_lock_irq(&callback_lock);
  3191. cpumask_copy(cs->effective_cpus, new_cpus);
  3192. cs->effective_mems = *new_mems;
  3193. spin_unlock_irq(&callback_lock);
  3194. if (cpus_updated)
  3195. cpuset_update_tasks_cpumask(cs, new_cpus);
  3196. if (mems_updated)
  3197. cpuset_update_tasks_nodemask(cs);
  3198. }
  3199. void cpuset_force_rebuild(void)
  3200. {
  3201. force_sd_rebuild = true;
  3202. }
  3203. /**
  3204. * cpuset_hotplug_update_tasks - update tasks in a cpuset for hotunplug
  3205. * @cs: cpuset in interest
  3206. * @tmp: the tmpmasks structure pointer
  3207. *
  3208. * Compare @cs's cpu and mem masks against top_cpuset and if some have gone
  3209. * offline, update @cs accordingly. If @cs ends up with no CPU or memory,
  3210. * all its tasks are moved to the nearest ancestor with both resources.
  3211. */
  3212. static void cpuset_hotplug_update_tasks(struct cpuset *cs, struct tmpmasks *tmp)
  3213. {
  3214. static cpumask_t new_cpus;
  3215. static nodemask_t new_mems;
  3216. bool cpus_updated;
  3217. bool mems_updated;
  3218. bool remote;
  3219. int partcmd = -1;
  3220. struct cpuset *parent;
  3221. retry:
  3222. wait_event(cpuset_attach_wq, cs->attach_in_progress == 0);
  3223. mutex_lock(&cpuset_mutex);
  3224. /*
  3225. * We have raced with task attaching. We wait until attaching
  3226. * is finished, so we won't attach a task to an empty cpuset.
  3227. */
  3228. if (cs->attach_in_progress) {
  3229. mutex_unlock(&cpuset_mutex);
  3230. goto retry;
  3231. }
  3232. parent = parent_cs(cs);
  3233. compute_effective_cpumask(&new_cpus, cs, parent);
  3234. nodes_and(new_mems, cs->mems_allowed, parent->effective_mems);
  3235. if (!tmp || !cs->partition_root_state)
  3236. goto update_tasks;
  3237. /*
  3238. * Compute effective_cpus for valid partition root, may invalidate
  3239. * child partition roots if necessary.
  3240. */
  3241. remote = is_remote_partition(cs);
  3242. if (remote || (is_partition_valid(cs) && is_partition_valid(parent)))
  3243. compute_partition_effective_cpumask(cs, &new_cpus);
  3244. if (remote && cpumask_empty(&new_cpus) &&
  3245. partition_is_populated(cs, NULL)) {
  3246. remote_partition_disable(cs, tmp);
  3247. compute_effective_cpumask(&new_cpus, cs, parent);
  3248. remote = false;
  3249. cpuset_force_rebuild();
  3250. }
  3251. /*
  3252. * Force the partition to become invalid if either one of
  3253. * the following conditions hold:
  3254. * 1) empty effective cpus but not valid empty partition.
  3255. * 2) parent is invalid or doesn't grant any cpus to child
  3256. * partitions.
  3257. */
  3258. if (is_local_partition(cs) && (!is_partition_valid(parent) ||
  3259. tasks_nocpu_error(parent, cs, &new_cpus)))
  3260. partcmd = partcmd_invalidate;
  3261. /*
  3262. * On the other hand, an invalid partition root may be transitioned
  3263. * back to a regular one.
  3264. */
  3265. else if (is_partition_valid(parent) && is_partition_invalid(cs))
  3266. partcmd = partcmd_update;
  3267. if (partcmd >= 0) {
  3268. update_parent_effective_cpumask(cs, partcmd, NULL, tmp);
  3269. if ((partcmd == partcmd_invalidate) || is_partition_valid(cs)) {
  3270. compute_partition_effective_cpumask(cs, &new_cpus);
  3271. cpuset_force_rebuild();
  3272. }
  3273. }
  3274. update_tasks:
  3275. cpus_updated = !cpumask_equal(&new_cpus, cs->effective_cpus);
  3276. mems_updated = !nodes_equal(new_mems, cs->effective_mems);
  3277. if (!cpus_updated && !mems_updated)
  3278. goto unlock; /* Hotplug doesn't affect this cpuset */
  3279. if (mems_updated)
  3280. check_insane_mems_config(&new_mems);
  3281. if (is_in_v2_mode())
  3282. hotplug_update_tasks(cs, &new_cpus, &new_mems,
  3283. cpus_updated, mems_updated);
  3284. else
  3285. cpuset1_hotplug_update_tasks(cs, &new_cpus, &new_mems,
  3286. cpus_updated, mems_updated);
  3287. unlock:
  3288. mutex_unlock(&cpuset_mutex);
  3289. }
  3290. /**
  3291. * cpuset_handle_hotplug - handle CPU/memory hot{,un}plug for a cpuset
  3292. *
  3293. * This function is called after either CPU or memory configuration has
  3294. * changed and updates cpuset accordingly. The top_cpuset is always
  3295. * synchronized to cpu_active_mask and N_MEMORY, which is necessary in
  3296. * order to make cpusets transparent (of no affect) on systems that are
  3297. * actively using CPU hotplug but making no active use of cpusets.
  3298. *
  3299. * Non-root cpusets are only affected by offlining. If any CPUs or memory
  3300. * nodes have been taken down, cpuset_hotplug_update_tasks() is invoked on
  3301. * all descendants.
  3302. *
  3303. * Note that CPU offlining during suspend is ignored. We don't modify
  3304. * cpusets across suspend/resume cycles at all.
  3305. *
  3306. * CPU / memory hotplug is handled synchronously.
  3307. */
  3308. static void cpuset_handle_hotplug(void)
  3309. {
  3310. static cpumask_t new_cpus;
  3311. static nodemask_t new_mems;
  3312. bool cpus_updated, mems_updated;
  3313. bool on_dfl = is_in_v2_mode();
  3314. struct tmpmasks tmp, *ptmp = NULL;
  3315. if (on_dfl && !alloc_cpumasks(NULL, &tmp))
  3316. ptmp = &tmp;
  3317. lockdep_assert_cpus_held();
  3318. mutex_lock(&cpuset_mutex);
  3319. /* fetch the available cpus/mems and find out which changed how */
  3320. cpumask_copy(&new_cpus, cpu_active_mask);
  3321. new_mems = node_states[N_MEMORY];
  3322. /*
  3323. * If subpartitions_cpus is populated, it is likely that the check
  3324. * below will produce a false positive on cpus_updated when the cpu
  3325. * list isn't changed. It is extra work, but it is better to be safe.
  3326. */
  3327. cpus_updated = !cpumask_equal(top_cpuset.effective_cpus, &new_cpus) ||
  3328. !cpumask_empty(subpartitions_cpus);
  3329. mems_updated = !nodes_equal(top_cpuset.effective_mems, new_mems);
  3330. /* For v1, synchronize cpus_allowed to cpu_active_mask */
  3331. if (cpus_updated) {
  3332. cpuset_force_rebuild();
  3333. spin_lock_irq(&callback_lock);
  3334. if (!on_dfl)
  3335. cpumask_copy(top_cpuset.cpus_allowed, &new_cpus);
  3336. /*
  3337. * Make sure that CPUs allocated to child partitions
  3338. * do not show up in effective_cpus. If no CPU is left,
  3339. * we clear the subpartitions_cpus & let the child partitions
  3340. * fight for the CPUs again.
  3341. */
  3342. if (!cpumask_empty(subpartitions_cpus)) {
  3343. if (cpumask_subset(&new_cpus, subpartitions_cpus)) {
  3344. top_cpuset.nr_subparts = 0;
  3345. cpumask_clear(subpartitions_cpus);
  3346. } else {
  3347. cpumask_andnot(&new_cpus, &new_cpus,
  3348. subpartitions_cpus);
  3349. }
  3350. }
  3351. cpumask_copy(top_cpuset.effective_cpus, &new_cpus);
  3352. spin_unlock_irq(&callback_lock);
  3353. /* we don't mess with cpumasks of tasks in top_cpuset */
  3354. }
  3355. /* synchronize mems_allowed to N_MEMORY */
  3356. if (mems_updated) {
  3357. spin_lock_irq(&callback_lock);
  3358. if (!on_dfl)
  3359. top_cpuset.mems_allowed = new_mems;
  3360. top_cpuset.effective_mems = new_mems;
  3361. spin_unlock_irq(&callback_lock);
  3362. cpuset_update_tasks_nodemask(&top_cpuset);
  3363. }
  3364. mutex_unlock(&cpuset_mutex);
  3365. /* if cpus or mems changed, we need to propagate to descendants */
  3366. if (cpus_updated || mems_updated) {
  3367. struct cpuset *cs;
  3368. struct cgroup_subsys_state *pos_css;
  3369. rcu_read_lock();
  3370. cpuset_for_each_descendant_pre(cs, pos_css, &top_cpuset) {
  3371. if (cs == &top_cpuset || !css_tryget_online(&cs->css))
  3372. continue;
  3373. rcu_read_unlock();
  3374. cpuset_hotplug_update_tasks(cs, ptmp);
  3375. rcu_read_lock();
  3376. css_put(&cs->css);
  3377. }
  3378. rcu_read_unlock();
  3379. }
  3380. /* rebuild sched domains if cpus_allowed has changed */
  3381. if (force_sd_rebuild) {
  3382. force_sd_rebuild = false;
  3383. rebuild_sched_domains_cpuslocked();
  3384. }
  3385. free_cpumasks(NULL, ptmp);
  3386. }
  3387. void cpuset_update_active_cpus(void)
  3388. {
  3389. /*
  3390. * We're inside cpu hotplug critical region which usually nests
  3391. * inside cgroup synchronization. Bounce actual hotplug processing
  3392. * to a work item to avoid reverse locking order.
  3393. */
  3394. cpuset_handle_hotplug();
  3395. }
  3396. /*
  3397. * Keep top_cpuset.mems_allowed tracking node_states[N_MEMORY].
  3398. * Call this routine anytime after node_states[N_MEMORY] changes.
  3399. * See cpuset_update_active_cpus() for CPU hotplug handling.
  3400. */
  3401. static int cpuset_track_online_nodes(struct notifier_block *self,
  3402. unsigned long action, void *arg)
  3403. {
  3404. cpuset_handle_hotplug();
  3405. return NOTIFY_OK;
  3406. }
  3407. /**
  3408. * cpuset_init_smp - initialize cpus_allowed
  3409. *
  3410. * Description: Finish top cpuset after cpu, node maps are initialized
  3411. */
  3412. void __init cpuset_init_smp(void)
  3413. {
  3414. /*
  3415. * cpus_allowd/mems_allowed set to v2 values in the initial
  3416. * cpuset_bind() call will be reset to v1 values in another
  3417. * cpuset_bind() call when v1 cpuset is mounted.
  3418. */
  3419. top_cpuset.old_mems_allowed = top_cpuset.mems_allowed;
  3420. cpumask_copy(top_cpuset.effective_cpus, cpu_active_mask);
  3421. top_cpuset.effective_mems = node_states[N_MEMORY];
  3422. hotplug_memory_notifier(cpuset_track_online_nodes, CPUSET_CALLBACK_PRI);
  3423. cpuset_migrate_mm_wq = alloc_ordered_workqueue("cpuset_migrate_mm", 0);
  3424. BUG_ON(!cpuset_migrate_mm_wq);
  3425. }
  3426. /**
  3427. * cpuset_cpus_allowed - return cpus_allowed mask from a tasks cpuset.
  3428. * @tsk: pointer to task_struct from which to obtain cpuset->cpus_allowed.
  3429. * @pmask: pointer to struct cpumask variable to receive cpus_allowed set.
  3430. *
  3431. * Description: Returns the cpumask_var_t cpus_allowed of the cpuset
  3432. * attached to the specified @tsk. Guaranteed to return some non-empty
  3433. * subset of cpu_online_mask, even if this means going outside the
  3434. * tasks cpuset, except when the task is in the top cpuset.
  3435. **/
  3436. void cpuset_cpus_allowed(struct task_struct *tsk, struct cpumask *pmask)
  3437. {
  3438. unsigned long flags;
  3439. struct cpuset *cs;
  3440. spin_lock_irqsave(&callback_lock, flags);
  3441. rcu_read_lock();
  3442. cs = task_cs(tsk);
  3443. if (cs != &top_cpuset)
  3444. guarantee_online_cpus(tsk, pmask);
  3445. /*
  3446. * Tasks in the top cpuset won't get update to their cpumasks
  3447. * when a hotplug online/offline event happens. So we include all
  3448. * offline cpus in the allowed cpu list.
  3449. */
  3450. if ((cs == &top_cpuset) || cpumask_empty(pmask)) {
  3451. const struct cpumask *possible_mask = task_cpu_possible_mask(tsk);
  3452. /*
  3453. * We first exclude cpus allocated to partitions. If there is no
  3454. * allowable online cpu left, we fall back to all possible cpus.
  3455. */
  3456. cpumask_andnot(pmask, possible_mask, subpartitions_cpus);
  3457. if (!cpumask_intersects(pmask, cpu_online_mask))
  3458. cpumask_copy(pmask, possible_mask);
  3459. }
  3460. rcu_read_unlock();
  3461. spin_unlock_irqrestore(&callback_lock, flags);
  3462. }
  3463. /**
  3464. * cpuset_cpus_allowed_fallback - final fallback before complete catastrophe.
  3465. * @tsk: pointer to task_struct with which the scheduler is struggling
  3466. *
  3467. * Description: In the case that the scheduler cannot find an allowed cpu in
  3468. * tsk->cpus_allowed, we fall back to task_cs(tsk)->cpus_allowed. In legacy
  3469. * mode however, this value is the same as task_cs(tsk)->effective_cpus,
  3470. * which will not contain a sane cpumask during cases such as cpu hotplugging.
  3471. * This is the absolute last resort for the scheduler and it is only used if
  3472. * _every_ other avenue has been traveled.
  3473. *
  3474. * Returns true if the affinity of @tsk was changed, false otherwise.
  3475. **/
  3476. bool cpuset_cpus_allowed_fallback(struct task_struct *tsk)
  3477. {
  3478. const struct cpumask *possible_mask = task_cpu_possible_mask(tsk);
  3479. const struct cpumask *cs_mask;
  3480. bool changed = false;
  3481. rcu_read_lock();
  3482. cs_mask = task_cs(tsk)->cpus_allowed;
  3483. if (is_in_v2_mode() && cpumask_subset(cs_mask, possible_mask)) {
  3484. do_set_cpus_allowed(tsk, cs_mask);
  3485. changed = true;
  3486. }
  3487. rcu_read_unlock();
  3488. /*
  3489. * We own tsk->cpus_allowed, nobody can change it under us.
  3490. *
  3491. * But we used cs && cs->cpus_allowed lockless and thus can
  3492. * race with cgroup_attach_task() or update_cpumask() and get
  3493. * the wrong tsk->cpus_allowed. However, both cases imply the
  3494. * subsequent cpuset_change_cpumask()->set_cpus_allowed_ptr()
  3495. * which takes task_rq_lock().
  3496. *
  3497. * If we are called after it dropped the lock we must see all
  3498. * changes in tsk_cs()->cpus_allowed. Otherwise we can temporary
  3499. * set any mask even if it is not right from task_cs() pov,
  3500. * the pending set_cpus_allowed_ptr() will fix things.
  3501. *
  3502. * select_fallback_rq() will fix things ups and set cpu_possible_mask
  3503. * if required.
  3504. */
  3505. return changed;
  3506. }
  3507. void __init cpuset_init_current_mems_allowed(void)
  3508. {
  3509. nodes_setall(current->mems_allowed);
  3510. }
  3511. /**
  3512. * cpuset_mems_allowed - return mems_allowed mask from a tasks cpuset.
  3513. * @tsk: pointer to task_struct from which to obtain cpuset->mems_allowed.
  3514. *
  3515. * Description: Returns the nodemask_t mems_allowed of the cpuset
  3516. * attached to the specified @tsk. Guaranteed to return some non-empty
  3517. * subset of node_states[N_MEMORY], even if this means going outside the
  3518. * tasks cpuset.
  3519. **/
  3520. nodemask_t cpuset_mems_allowed(struct task_struct *tsk)
  3521. {
  3522. nodemask_t mask;
  3523. unsigned long flags;
  3524. spin_lock_irqsave(&callback_lock, flags);
  3525. rcu_read_lock();
  3526. guarantee_online_mems(task_cs(tsk), &mask);
  3527. rcu_read_unlock();
  3528. spin_unlock_irqrestore(&callback_lock, flags);
  3529. return mask;
  3530. }
  3531. /**
  3532. * cpuset_nodemask_valid_mems_allowed - check nodemask vs. current mems_allowed
  3533. * @nodemask: the nodemask to be checked
  3534. *
  3535. * Are any of the nodes in the nodemask allowed in current->mems_allowed?
  3536. */
  3537. int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
  3538. {
  3539. return nodes_intersects(*nodemask, current->mems_allowed);
  3540. }
  3541. /*
  3542. * nearest_hardwall_ancestor() - Returns the nearest mem_exclusive or
  3543. * mem_hardwall ancestor to the specified cpuset. Call holding
  3544. * callback_lock. If no ancestor is mem_exclusive or mem_hardwall
  3545. * (an unusual configuration), then returns the root cpuset.
  3546. */
  3547. static struct cpuset *nearest_hardwall_ancestor(struct cpuset *cs)
  3548. {
  3549. while (!(is_mem_exclusive(cs) || is_mem_hardwall(cs)) && parent_cs(cs))
  3550. cs = parent_cs(cs);
  3551. return cs;
  3552. }
  3553. /*
  3554. * cpuset_node_allowed - Can we allocate on a memory node?
  3555. * @node: is this an allowed node?
  3556. * @gfp_mask: memory allocation flags
  3557. *
  3558. * If we're in interrupt, yes, we can always allocate. If @node is set in
  3559. * current's mems_allowed, yes. If it's not a __GFP_HARDWALL request and this
  3560. * node is set in the nearest hardwalled cpuset ancestor to current's cpuset,
  3561. * yes. If current has access to memory reserves as an oom victim, yes.
  3562. * Otherwise, no.
  3563. *
  3564. * GFP_USER allocations are marked with the __GFP_HARDWALL bit,
  3565. * and do not allow allocations outside the current tasks cpuset
  3566. * unless the task has been OOM killed.
  3567. * GFP_KERNEL allocations are not so marked, so can escape to the
  3568. * nearest enclosing hardwalled ancestor cpuset.
  3569. *
  3570. * Scanning up parent cpusets requires callback_lock. The
  3571. * __alloc_pages() routine only calls here with __GFP_HARDWALL bit
  3572. * _not_ set if it's a GFP_KERNEL allocation, and all nodes in the
  3573. * current tasks mems_allowed came up empty on the first pass over
  3574. * the zonelist. So only GFP_KERNEL allocations, if all nodes in the
  3575. * cpuset are short of memory, might require taking the callback_lock.
  3576. *
  3577. * The first call here from mm/page_alloc:get_page_from_freelist()
  3578. * has __GFP_HARDWALL set in gfp_mask, enforcing hardwall cpusets,
  3579. * so no allocation on a node outside the cpuset is allowed (unless
  3580. * in interrupt, of course).
  3581. *
  3582. * The second pass through get_page_from_freelist() doesn't even call
  3583. * here for GFP_ATOMIC calls. For those calls, the __alloc_pages()
  3584. * variable 'wait' is not set, and the bit ALLOC_CPUSET is not set
  3585. * in alloc_flags. That logic and the checks below have the combined
  3586. * affect that:
  3587. * in_interrupt - any node ok (current task context irrelevant)
  3588. * GFP_ATOMIC - any node ok
  3589. * tsk_is_oom_victim - any node ok
  3590. * GFP_KERNEL - any node in enclosing hardwalled cpuset ok
  3591. * GFP_USER - only nodes in current tasks mems allowed ok.
  3592. */
  3593. bool cpuset_node_allowed(int node, gfp_t gfp_mask)
  3594. {
  3595. struct cpuset *cs; /* current cpuset ancestors */
  3596. bool allowed; /* is allocation in zone z allowed? */
  3597. unsigned long flags;
  3598. if (in_interrupt())
  3599. return true;
  3600. if (node_isset(node, current->mems_allowed))
  3601. return true;
  3602. /*
  3603. * Allow tasks that have access to memory reserves because they have
  3604. * been OOM killed to get memory anywhere.
  3605. */
  3606. if (unlikely(tsk_is_oom_victim(current)))
  3607. return true;
  3608. if (gfp_mask & __GFP_HARDWALL) /* If hardwall request, stop here */
  3609. return false;
  3610. if (current->flags & PF_EXITING) /* Let dying task have memory */
  3611. return true;
  3612. /* Not hardwall and node outside mems_allowed: scan up cpusets */
  3613. spin_lock_irqsave(&callback_lock, flags);
  3614. rcu_read_lock();
  3615. cs = nearest_hardwall_ancestor(task_cs(current));
  3616. allowed = node_isset(node, cs->mems_allowed);
  3617. rcu_read_unlock();
  3618. spin_unlock_irqrestore(&callback_lock, flags);
  3619. return allowed;
  3620. }
  3621. /**
  3622. * cpuset_spread_node() - On which node to begin search for a page
  3623. * @rotor: round robin rotor
  3624. *
  3625. * If a task is marked PF_SPREAD_PAGE or PF_SPREAD_SLAB (as for
  3626. * tasks in a cpuset with is_spread_page or is_spread_slab set),
  3627. * and if the memory allocation used cpuset_mem_spread_node()
  3628. * to determine on which node to start looking, as it will for
  3629. * certain page cache or slab cache pages such as used for file
  3630. * system buffers and inode caches, then instead of starting on the
  3631. * local node to look for a free page, rather spread the starting
  3632. * node around the tasks mems_allowed nodes.
  3633. *
  3634. * We don't have to worry about the returned node being offline
  3635. * because "it can't happen", and even if it did, it would be ok.
  3636. *
  3637. * The routines calling guarantee_online_mems() are careful to
  3638. * only set nodes in task->mems_allowed that are online. So it
  3639. * should not be possible for the following code to return an
  3640. * offline node. But if it did, that would be ok, as this routine
  3641. * is not returning the node where the allocation must be, only
  3642. * the node where the search should start. The zonelist passed to
  3643. * __alloc_pages() will include all nodes. If the slab allocator
  3644. * is passed an offline node, it will fall back to the local node.
  3645. * See kmem_cache_alloc_node().
  3646. */
  3647. static int cpuset_spread_node(int *rotor)
  3648. {
  3649. return *rotor = next_node_in(*rotor, current->mems_allowed);
  3650. }
  3651. /**
  3652. * cpuset_mem_spread_node() - On which node to begin search for a file page
  3653. */
  3654. int cpuset_mem_spread_node(void)
  3655. {
  3656. if (current->cpuset_mem_spread_rotor == NUMA_NO_NODE)
  3657. current->cpuset_mem_spread_rotor =
  3658. node_random(&current->mems_allowed);
  3659. return cpuset_spread_node(&current->cpuset_mem_spread_rotor);
  3660. }
  3661. /**
  3662. * cpuset_mems_allowed_intersects - Does @tsk1's mems_allowed intersect @tsk2's?
  3663. * @tsk1: pointer to task_struct of some task.
  3664. * @tsk2: pointer to task_struct of some other task.
  3665. *
  3666. * Description: Return true if @tsk1's mems_allowed intersects the
  3667. * mems_allowed of @tsk2. Used by the OOM killer to determine if
  3668. * one of the task's memory usage might impact the memory available
  3669. * to the other.
  3670. **/
  3671. int cpuset_mems_allowed_intersects(const struct task_struct *tsk1,
  3672. const struct task_struct *tsk2)
  3673. {
  3674. return nodes_intersects(tsk1->mems_allowed, tsk2->mems_allowed);
  3675. }
  3676. /**
  3677. * cpuset_print_current_mems_allowed - prints current's cpuset and mems_allowed
  3678. *
  3679. * Description: Prints current's name, cpuset name, and cached copy of its
  3680. * mems_allowed to the kernel log.
  3681. */
  3682. void cpuset_print_current_mems_allowed(void)
  3683. {
  3684. struct cgroup *cgrp;
  3685. rcu_read_lock();
  3686. cgrp = task_cs(current)->css.cgroup;
  3687. pr_cont(",cpuset=");
  3688. pr_cont_cgroup_name(cgrp);
  3689. pr_cont(",mems_allowed=%*pbl",
  3690. nodemask_pr_args(&current->mems_allowed));
  3691. rcu_read_unlock();
  3692. }
  3693. #ifdef CONFIG_PROC_PID_CPUSET
  3694. /*
  3695. * proc_cpuset_show()
  3696. * - Print tasks cpuset path into seq_file.
  3697. * - Used for /proc/<pid>/cpuset.
  3698. * - No need to task_lock(tsk) on this tsk->cpuset reference, as it
  3699. * doesn't really matter if tsk->cpuset changes after we read it,
  3700. * and we take cpuset_mutex, keeping cpuset_attach() from changing it
  3701. * anyway.
  3702. */
  3703. int proc_cpuset_show(struct seq_file *m, struct pid_namespace *ns,
  3704. struct pid *pid, struct task_struct *tsk)
  3705. {
  3706. char *buf;
  3707. struct cgroup_subsys_state *css;
  3708. int retval;
  3709. retval = -ENOMEM;
  3710. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  3711. if (!buf)
  3712. goto out;
  3713. rcu_read_lock();
  3714. spin_lock_irq(&css_set_lock);
  3715. css = task_css(tsk, cpuset_cgrp_id);
  3716. retval = cgroup_path_ns_locked(css->cgroup, buf, PATH_MAX,
  3717. current->nsproxy->cgroup_ns);
  3718. spin_unlock_irq(&css_set_lock);
  3719. rcu_read_unlock();
  3720. if (retval == -E2BIG)
  3721. retval = -ENAMETOOLONG;
  3722. if (retval < 0)
  3723. goto out_free;
  3724. seq_puts(m, buf);
  3725. seq_putc(m, '\n');
  3726. retval = 0;
  3727. out_free:
  3728. kfree(buf);
  3729. out:
  3730. return retval;
  3731. }
  3732. #endif /* CONFIG_PROC_PID_CPUSET */
  3733. /* Display task mems_allowed in /proc/<pid>/status file. */
  3734. void cpuset_task_status_allowed(struct seq_file *m, struct task_struct *task)
  3735. {
  3736. seq_printf(m, "Mems_allowed:\t%*pb\n",
  3737. nodemask_pr_args(&task->mems_allowed));
  3738. seq_printf(m, "Mems_allowed_list:\t%*pbl\n",
  3739. nodemask_pr_args(&task->mems_allowed));
  3740. }