cpuset.c 121 KB

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