cpu.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239
  1. /* CPU control.
  2. * (C) 2001, 2002, 2003, 2004 Rusty Russell
  3. *
  4. * This code is licenced under the GPL.
  5. */
  6. #include <linux/sched/mm.h>
  7. #include <linux/proc_fs.h>
  8. #include <linux/smp.h>
  9. #include <linux/init.h>
  10. #include <linux/notifier.h>
  11. #include <linux/sched/signal.h>
  12. #include <linux/sched/hotplug.h>
  13. #include <linux/sched/isolation.h>
  14. #include <linux/sched/task.h>
  15. #include <linux/sched/smt.h>
  16. #include <linux/unistd.h>
  17. #include <linux/cpu.h>
  18. #include <linux/oom.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/delay.h>
  21. #include <linux/export.h>
  22. #include <linux/bug.h>
  23. #include <linux/kthread.h>
  24. #include <linux/stop_machine.h>
  25. #include <linux/mutex.h>
  26. #include <linux/gfp.h>
  27. #include <linux/suspend.h>
  28. #include <linux/lockdep.h>
  29. #include <linux/tick.h>
  30. #include <linux/irq.h>
  31. #include <linux/nmi.h>
  32. #include <linux/smpboot.h>
  33. #include <linux/relay.h>
  34. #include <linux/slab.h>
  35. #include <linux/scs.h>
  36. #include <linux/percpu-rwsem.h>
  37. #include <linux/cpuset.h>
  38. #include <linux/random.h>
  39. #include <linux/cc_platform.h>
  40. #include <trace/events/power.h>
  41. #define CREATE_TRACE_POINTS
  42. #include <trace/events/cpuhp.h>
  43. #include "smpboot.h"
  44. /**
  45. * struct cpuhp_cpu_state - Per cpu hotplug state storage
  46. * @state: The current cpu state
  47. * @target: The target state
  48. * @fail: Current CPU hotplug callback state
  49. * @thread: Pointer to the hotplug thread
  50. * @should_run: Thread should execute
  51. * @rollback: Perform a rollback
  52. * @single: Single callback invocation
  53. * @bringup: Single callback bringup or teardown selector
  54. * @node: Remote CPU node; for multi-instance, do a
  55. * single entry callback for install/remove
  56. * @last: For multi-instance rollback, remember how far we got
  57. * @cb_state: The state for a single callback (install/uninstall)
  58. * @result: Result of the operation
  59. * @ap_sync_state: State for AP synchronization
  60. * @done_up: Signal completion to the issuer of the task for cpu-up
  61. * @done_down: Signal completion to the issuer of the task for cpu-down
  62. */
  63. struct cpuhp_cpu_state {
  64. enum cpuhp_state state;
  65. enum cpuhp_state target;
  66. enum cpuhp_state fail;
  67. #ifdef CONFIG_SMP
  68. struct task_struct *thread;
  69. bool should_run;
  70. bool rollback;
  71. bool single;
  72. bool bringup;
  73. struct hlist_node *node;
  74. struct hlist_node *last;
  75. enum cpuhp_state cb_state;
  76. int result;
  77. atomic_t ap_sync_state;
  78. struct completion done_up;
  79. struct completion done_down;
  80. #endif
  81. };
  82. static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = {
  83. .fail = CPUHP_INVALID,
  84. };
  85. #ifdef CONFIG_SMP
  86. cpumask_t cpus_booted_once_mask;
  87. #endif
  88. #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
  89. static struct lockdep_map cpuhp_state_up_map =
  90. STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map);
  91. static struct lockdep_map cpuhp_state_down_map =
  92. STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map);
  93. static inline void cpuhp_lock_acquire(bool bringup)
  94. {
  95. lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
  96. }
  97. static inline void cpuhp_lock_release(bool bringup)
  98. {
  99. lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
  100. }
  101. #else
  102. static inline void cpuhp_lock_acquire(bool bringup) { }
  103. static inline void cpuhp_lock_release(bool bringup) { }
  104. #endif
  105. /**
  106. * struct cpuhp_step - Hotplug state machine step
  107. * @name: Name of the step
  108. * @startup: Startup function of the step
  109. * @teardown: Teardown function of the step
  110. * @cant_stop: Bringup/teardown can't be stopped at this step
  111. * @multi_instance: State has multiple instances which get added afterwards
  112. */
  113. struct cpuhp_step {
  114. const char *name;
  115. union {
  116. int (*single)(unsigned int cpu);
  117. int (*multi)(unsigned int cpu,
  118. struct hlist_node *node);
  119. } startup;
  120. union {
  121. int (*single)(unsigned int cpu);
  122. int (*multi)(unsigned int cpu,
  123. struct hlist_node *node);
  124. } teardown;
  125. /* private: */
  126. struct hlist_head list;
  127. /* public: */
  128. bool cant_stop;
  129. bool multi_instance;
  130. };
  131. static DEFINE_MUTEX(cpuhp_state_mutex);
  132. static struct cpuhp_step cpuhp_hp_states[];
  133. static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
  134. {
  135. return cpuhp_hp_states + state;
  136. }
  137. static bool cpuhp_step_empty(bool bringup, struct cpuhp_step *step)
  138. {
  139. return bringup ? !step->startup.single : !step->teardown.single;
  140. }
  141. /**
  142. * cpuhp_invoke_callback - Invoke the callbacks for a given state
  143. * @cpu: The cpu for which the callback should be invoked
  144. * @state: The state to do callbacks for
  145. * @bringup: True if the bringup callback should be invoked
  146. * @node: For multi-instance, do a single entry callback for install/remove
  147. * @lastp: For multi-instance rollback, remember how far we got
  148. *
  149. * Called from cpu hotplug and from the state register machinery.
  150. *
  151. * Return: %0 on success or a negative errno code
  152. */
  153. static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
  154. bool bringup, struct hlist_node *node,
  155. struct hlist_node **lastp)
  156. {
  157. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  158. struct cpuhp_step *step = cpuhp_get_step(state);
  159. int (*cbm)(unsigned int cpu, struct hlist_node *node);
  160. int (*cb)(unsigned int cpu);
  161. int ret, cnt;
  162. if (st->fail == state) {
  163. st->fail = CPUHP_INVALID;
  164. return -EAGAIN;
  165. }
  166. if (cpuhp_step_empty(bringup, step)) {
  167. WARN_ON_ONCE(1);
  168. return 0;
  169. }
  170. if (!step->multi_instance) {
  171. WARN_ON_ONCE(lastp && *lastp);
  172. cb = bringup ? step->startup.single : step->teardown.single;
  173. trace_cpuhp_enter(cpu, st->target, state, cb);
  174. ret = cb(cpu);
  175. trace_cpuhp_exit(cpu, st->state, state, ret);
  176. return ret;
  177. }
  178. cbm = bringup ? step->startup.multi : step->teardown.multi;
  179. /* Single invocation for instance add/remove */
  180. if (node) {
  181. WARN_ON_ONCE(lastp && *lastp);
  182. trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
  183. ret = cbm(cpu, node);
  184. trace_cpuhp_exit(cpu, st->state, state, ret);
  185. return ret;
  186. }
  187. /* State transition. Invoke on all instances */
  188. cnt = 0;
  189. hlist_for_each(node, &step->list) {
  190. if (lastp && node == *lastp)
  191. break;
  192. trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
  193. ret = cbm(cpu, node);
  194. trace_cpuhp_exit(cpu, st->state, state, ret);
  195. if (ret) {
  196. if (!lastp)
  197. goto err;
  198. *lastp = node;
  199. return ret;
  200. }
  201. cnt++;
  202. }
  203. if (lastp)
  204. *lastp = NULL;
  205. return 0;
  206. err:
  207. /* Rollback the instances if one failed */
  208. cbm = !bringup ? step->startup.multi : step->teardown.multi;
  209. if (!cbm)
  210. return ret;
  211. hlist_for_each(node, &step->list) {
  212. if (!cnt--)
  213. break;
  214. trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
  215. ret = cbm(cpu, node);
  216. trace_cpuhp_exit(cpu, st->state, state, ret);
  217. /*
  218. * Rollback must not fail,
  219. */
  220. WARN_ON_ONCE(ret);
  221. }
  222. return ret;
  223. }
  224. #ifdef CONFIG_SMP
  225. static bool cpuhp_is_ap_state(enum cpuhp_state state)
  226. {
  227. /*
  228. * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
  229. * purposes as that state is handled explicitly in cpu_down.
  230. */
  231. return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
  232. }
  233. static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
  234. {
  235. struct completion *done = bringup ? &st->done_up : &st->done_down;
  236. wait_for_completion(done);
  237. }
  238. static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
  239. {
  240. struct completion *done = bringup ? &st->done_up : &st->done_down;
  241. complete(done);
  242. }
  243. /*
  244. * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
  245. */
  246. static bool cpuhp_is_atomic_state(enum cpuhp_state state)
  247. {
  248. return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
  249. }
  250. /* Synchronization state management */
  251. enum cpuhp_sync_state {
  252. SYNC_STATE_DEAD,
  253. SYNC_STATE_KICKED,
  254. SYNC_STATE_SHOULD_DIE,
  255. SYNC_STATE_ALIVE,
  256. SYNC_STATE_SHOULD_ONLINE,
  257. SYNC_STATE_ONLINE,
  258. };
  259. #ifdef CONFIG_HOTPLUG_CORE_SYNC
  260. /**
  261. * cpuhp_ap_update_sync_state - Update synchronization state during bringup/teardown
  262. * @state: The synchronization state to set
  263. *
  264. * No synchronization point. Just update of the synchronization state, but implies
  265. * a full barrier so that the AP changes are visible before the control CPU proceeds.
  266. */
  267. static inline void cpuhp_ap_update_sync_state(enum cpuhp_sync_state state)
  268. {
  269. atomic_t *st = this_cpu_ptr(&cpuhp_state.ap_sync_state);
  270. (void)atomic_xchg(st, state);
  271. }
  272. void __weak arch_cpuhp_sync_state_poll(void) { cpu_relax(); }
  273. static bool cpuhp_wait_for_sync_state(unsigned int cpu, enum cpuhp_sync_state state,
  274. enum cpuhp_sync_state next_state)
  275. {
  276. atomic_t *st = per_cpu_ptr(&cpuhp_state.ap_sync_state, cpu);
  277. ktime_t now, end, start = ktime_get();
  278. int sync;
  279. end = start + 10ULL * NSEC_PER_SEC;
  280. sync = atomic_read(st);
  281. while (1) {
  282. if (sync == state) {
  283. if (!atomic_try_cmpxchg(st, &sync, next_state))
  284. continue;
  285. return true;
  286. }
  287. now = ktime_get();
  288. if (now > end) {
  289. /* Timeout. Leave the state unchanged */
  290. return false;
  291. } else if (now - start < NSEC_PER_MSEC) {
  292. /* Poll for one millisecond */
  293. arch_cpuhp_sync_state_poll();
  294. } else {
  295. usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
  296. }
  297. sync = atomic_read(st);
  298. }
  299. return true;
  300. }
  301. #else /* CONFIG_HOTPLUG_CORE_SYNC */
  302. static inline void cpuhp_ap_update_sync_state(enum cpuhp_sync_state state) { }
  303. #endif /* !CONFIG_HOTPLUG_CORE_SYNC */
  304. #ifdef CONFIG_HOTPLUG_CORE_SYNC_DEAD
  305. /**
  306. * cpuhp_ap_report_dead - Update synchronization state to DEAD
  307. *
  308. * No synchronization point. Just update of the synchronization state.
  309. */
  310. void cpuhp_ap_report_dead(void)
  311. {
  312. cpuhp_ap_update_sync_state(SYNC_STATE_DEAD);
  313. }
  314. void __weak arch_cpuhp_cleanup_dead_cpu(unsigned int cpu) { }
  315. /*
  316. * Late CPU shutdown synchronization point. Cannot use cpuhp_state::done_down
  317. * because the AP cannot issue complete() at this stage.
  318. */
  319. static void cpuhp_bp_sync_dead(unsigned int cpu)
  320. {
  321. atomic_t *st = per_cpu_ptr(&cpuhp_state.ap_sync_state, cpu);
  322. int sync = atomic_read(st);
  323. do {
  324. /* CPU can have reported dead already. Don't overwrite that! */
  325. if (sync == SYNC_STATE_DEAD)
  326. break;
  327. } while (!atomic_try_cmpxchg(st, &sync, SYNC_STATE_SHOULD_DIE));
  328. if (cpuhp_wait_for_sync_state(cpu, SYNC_STATE_DEAD, SYNC_STATE_DEAD)) {
  329. /* CPU reached dead state. Invoke the cleanup function */
  330. arch_cpuhp_cleanup_dead_cpu(cpu);
  331. return;
  332. }
  333. /* No further action possible. Emit message and give up. */
  334. pr_err("CPU%u failed to report dead state\n", cpu);
  335. }
  336. #else /* CONFIG_HOTPLUG_CORE_SYNC_DEAD */
  337. static inline void cpuhp_bp_sync_dead(unsigned int cpu) { }
  338. #endif /* !CONFIG_HOTPLUG_CORE_SYNC_DEAD */
  339. #ifdef CONFIG_HOTPLUG_CORE_SYNC_FULL
  340. /**
  341. * cpuhp_ap_sync_alive - Synchronize AP with the control CPU once it is alive
  342. *
  343. * Updates the AP synchronization state to SYNC_STATE_ALIVE and waits
  344. * for the BP to release it.
  345. */
  346. void cpuhp_ap_sync_alive(void)
  347. {
  348. atomic_t *st = this_cpu_ptr(&cpuhp_state.ap_sync_state);
  349. cpuhp_ap_update_sync_state(SYNC_STATE_ALIVE);
  350. /* Wait for the control CPU to release it. */
  351. while (atomic_read(st) != SYNC_STATE_SHOULD_ONLINE)
  352. cpu_relax();
  353. }
  354. static bool cpuhp_can_boot_ap(unsigned int cpu)
  355. {
  356. atomic_t *st = per_cpu_ptr(&cpuhp_state.ap_sync_state, cpu);
  357. int sync = atomic_read(st);
  358. again:
  359. switch (sync) {
  360. case SYNC_STATE_DEAD:
  361. /* CPU is properly dead */
  362. break;
  363. case SYNC_STATE_KICKED:
  364. /* CPU did not come up in previous attempt */
  365. break;
  366. case SYNC_STATE_ALIVE:
  367. /* CPU is stuck cpuhp_ap_sync_alive(). */
  368. break;
  369. default:
  370. /* CPU failed to report online or dead and is in limbo state. */
  371. return false;
  372. }
  373. /* Prepare for booting */
  374. if (!atomic_try_cmpxchg(st, &sync, SYNC_STATE_KICKED))
  375. goto again;
  376. return true;
  377. }
  378. void __weak arch_cpuhp_cleanup_kick_cpu(unsigned int cpu) { }
  379. /*
  380. * Early CPU bringup synchronization point. Cannot use cpuhp_state::done_up
  381. * because the AP cannot issue complete() so early in the bringup.
  382. */
  383. static int cpuhp_bp_sync_alive(unsigned int cpu)
  384. {
  385. int ret = 0;
  386. if (!IS_ENABLED(CONFIG_HOTPLUG_CORE_SYNC_FULL))
  387. return 0;
  388. if (!cpuhp_wait_for_sync_state(cpu, SYNC_STATE_ALIVE, SYNC_STATE_SHOULD_ONLINE)) {
  389. pr_err("CPU%u failed to report alive state\n", cpu);
  390. ret = -EIO;
  391. }
  392. /* Let the architecture cleanup the kick alive mechanics. */
  393. arch_cpuhp_cleanup_kick_cpu(cpu);
  394. return ret;
  395. }
  396. #else /* CONFIG_HOTPLUG_CORE_SYNC_FULL */
  397. static inline int cpuhp_bp_sync_alive(unsigned int cpu) { return 0; }
  398. static inline bool cpuhp_can_boot_ap(unsigned int cpu) { return true; }
  399. #endif /* !CONFIG_HOTPLUG_CORE_SYNC_FULL */
  400. /* Serializes the updates to cpu_online_mask, cpu_present_mask */
  401. static DEFINE_MUTEX(cpu_add_remove_lock);
  402. bool cpuhp_tasks_frozen;
  403. EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
  404. /*
  405. * The following two APIs (cpu_maps_update_begin/done) must be used when
  406. * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
  407. */
  408. void cpu_maps_update_begin(void)
  409. {
  410. mutex_lock(&cpu_add_remove_lock);
  411. }
  412. void cpu_maps_update_done(void)
  413. {
  414. mutex_unlock(&cpu_add_remove_lock);
  415. }
  416. /*
  417. * If set, cpu_up and cpu_down will return -EBUSY and do nothing.
  418. * Should always be manipulated under cpu_add_remove_lock
  419. */
  420. static int cpu_hotplug_disabled;
  421. #ifdef CONFIG_HOTPLUG_CPU
  422. DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
  423. static bool cpu_hotplug_offline_disabled __ro_after_init;
  424. void cpus_read_lock(void)
  425. {
  426. percpu_down_read(&cpu_hotplug_lock);
  427. }
  428. EXPORT_SYMBOL_GPL(cpus_read_lock);
  429. int cpus_read_trylock(void)
  430. {
  431. return percpu_down_read_trylock(&cpu_hotplug_lock);
  432. }
  433. EXPORT_SYMBOL_GPL(cpus_read_trylock);
  434. void cpus_read_unlock(void)
  435. {
  436. percpu_up_read(&cpu_hotplug_lock);
  437. }
  438. EXPORT_SYMBOL_GPL(cpus_read_unlock);
  439. void cpus_write_lock(void)
  440. {
  441. percpu_down_write(&cpu_hotplug_lock);
  442. }
  443. void cpus_write_unlock(void)
  444. {
  445. percpu_up_write(&cpu_hotplug_lock);
  446. }
  447. void lockdep_assert_cpus_held(void)
  448. {
  449. /*
  450. * We can't have hotplug operations before userspace starts running,
  451. * and some init codepaths will knowingly not take the hotplug lock.
  452. * This is all valid, so mute lockdep until it makes sense to report
  453. * unheld locks.
  454. */
  455. if (system_state < SYSTEM_RUNNING)
  456. return;
  457. percpu_rwsem_assert_held(&cpu_hotplug_lock);
  458. }
  459. #ifdef CONFIG_LOCKDEP
  460. int lockdep_is_cpus_held(void)
  461. {
  462. return percpu_rwsem_is_held(&cpu_hotplug_lock);
  463. }
  464. #endif
  465. static void lockdep_acquire_cpus_lock(void)
  466. {
  467. rwsem_acquire(&cpu_hotplug_lock.dep_map, 0, 0, _THIS_IP_);
  468. }
  469. static void lockdep_release_cpus_lock(void)
  470. {
  471. rwsem_release(&cpu_hotplug_lock.dep_map, _THIS_IP_);
  472. }
  473. /* Declare CPU offlining not supported */
  474. void cpu_hotplug_disable_offlining(void)
  475. {
  476. cpu_maps_update_begin();
  477. cpu_hotplug_offline_disabled = true;
  478. cpu_maps_update_done();
  479. }
  480. /*
  481. * Wait for currently running CPU hotplug operations to complete (if any) and
  482. * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
  483. * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
  484. * hotplug path before performing hotplug operations. So acquiring that lock
  485. * guarantees mutual exclusion from any currently running hotplug operations.
  486. */
  487. void cpu_hotplug_disable(void)
  488. {
  489. cpu_maps_update_begin();
  490. cpu_hotplug_disabled++;
  491. cpu_maps_update_done();
  492. }
  493. EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
  494. static void __cpu_hotplug_enable(void)
  495. {
  496. if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
  497. return;
  498. cpu_hotplug_disabled--;
  499. }
  500. void cpu_hotplug_enable(void)
  501. {
  502. cpu_maps_update_begin();
  503. __cpu_hotplug_enable();
  504. cpu_maps_update_done();
  505. }
  506. EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
  507. #else
  508. static void lockdep_acquire_cpus_lock(void)
  509. {
  510. }
  511. static void lockdep_release_cpus_lock(void)
  512. {
  513. }
  514. #endif /* CONFIG_HOTPLUG_CPU */
  515. /*
  516. * Architectures that need SMT-specific errata handling during SMT hotplug
  517. * should override this.
  518. */
  519. void __weak arch_smt_update(void) { }
  520. #ifdef CONFIG_HOTPLUG_SMT
  521. enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
  522. static unsigned int cpu_smt_max_threads __ro_after_init;
  523. unsigned int cpu_smt_num_threads __read_mostly = UINT_MAX;
  524. void __init cpu_smt_disable(bool force)
  525. {
  526. if (!cpu_smt_possible())
  527. return;
  528. if (force) {
  529. pr_info("SMT: Force disabled\n");
  530. cpu_smt_control = CPU_SMT_FORCE_DISABLED;
  531. } else {
  532. pr_info("SMT: disabled\n");
  533. cpu_smt_control = CPU_SMT_DISABLED;
  534. }
  535. cpu_smt_num_threads = 1;
  536. }
  537. /*
  538. * The decision whether SMT is supported can only be done after the full
  539. * CPU identification. Called from architecture code.
  540. */
  541. void __init cpu_smt_set_num_threads(unsigned int num_threads,
  542. unsigned int max_threads)
  543. {
  544. WARN_ON(!num_threads || (num_threads > max_threads));
  545. if (max_threads == 1)
  546. cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
  547. cpu_smt_max_threads = max_threads;
  548. /*
  549. * If SMT has been disabled via the kernel command line or SMT is
  550. * not supported, set cpu_smt_num_threads to 1 for consistency.
  551. * If enabled, take the architecture requested number of threads
  552. * to bring up into account.
  553. */
  554. if (cpu_smt_control != CPU_SMT_ENABLED)
  555. cpu_smt_num_threads = 1;
  556. else if (num_threads < cpu_smt_num_threads)
  557. cpu_smt_num_threads = num_threads;
  558. }
  559. static int __init smt_cmdline_disable(char *str)
  560. {
  561. cpu_smt_disable(str && !strcmp(str, "force"));
  562. return 0;
  563. }
  564. early_param("nosmt", smt_cmdline_disable);
  565. /*
  566. * For Archicture supporting partial SMT states check if the thread is allowed.
  567. * Otherwise this has already been checked through cpu_smt_max_threads when
  568. * setting the SMT level.
  569. */
  570. static inline bool cpu_smt_thread_allowed(unsigned int cpu)
  571. {
  572. #ifdef CONFIG_SMT_NUM_THREADS_DYNAMIC
  573. return topology_smt_thread_allowed(cpu);
  574. #else
  575. return true;
  576. #endif
  577. }
  578. static inline bool cpu_bootable(unsigned int cpu)
  579. {
  580. if (cpu_smt_control == CPU_SMT_ENABLED && cpu_smt_thread_allowed(cpu))
  581. return true;
  582. /* All CPUs are bootable if controls are not configured */
  583. if (cpu_smt_control == CPU_SMT_NOT_IMPLEMENTED)
  584. return true;
  585. /* All CPUs are bootable if CPU is not SMT capable */
  586. if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
  587. return true;
  588. if (topology_is_primary_thread(cpu))
  589. return true;
  590. /*
  591. * On x86 it's required to boot all logical CPUs at least once so
  592. * that the init code can get a chance to set CR4.MCE on each
  593. * CPU. Otherwise, a broadcasted MCE observing CR4.MCE=0b on any
  594. * core will shutdown the machine.
  595. */
  596. return !cpumask_test_cpu(cpu, &cpus_booted_once_mask);
  597. }
  598. /* Returns true if SMT is supported and not forcefully (irreversibly) disabled */
  599. bool cpu_smt_possible(void)
  600. {
  601. return cpu_smt_control != CPU_SMT_FORCE_DISABLED &&
  602. cpu_smt_control != CPU_SMT_NOT_SUPPORTED;
  603. }
  604. EXPORT_SYMBOL_GPL(cpu_smt_possible);
  605. #else
  606. static inline bool cpu_bootable(unsigned int cpu) { return true; }
  607. #endif
  608. static inline enum cpuhp_state
  609. cpuhp_set_state(int cpu, struct cpuhp_cpu_state *st, enum cpuhp_state target)
  610. {
  611. enum cpuhp_state prev_state = st->state;
  612. bool bringup = st->state < target;
  613. st->rollback = false;
  614. st->last = NULL;
  615. st->target = target;
  616. st->single = false;
  617. st->bringup = bringup;
  618. if (cpu_dying(cpu) != !bringup)
  619. set_cpu_dying(cpu, !bringup);
  620. return prev_state;
  621. }
  622. static inline void
  623. cpuhp_reset_state(int cpu, struct cpuhp_cpu_state *st,
  624. enum cpuhp_state prev_state)
  625. {
  626. bool bringup = !st->bringup;
  627. st->target = prev_state;
  628. /*
  629. * Already rolling back. No need invert the bringup value or to change
  630. * the current state.
  631. */
  632. if (st->rollback)
  633. return;
  634. st->rollback = true;
  635. /*
  636. * If we have st->last we need to undo partial multi_instance of this
  637. * state first. Otherwise start undo at the previous state.
  638. */
  639. if (!st->last) {
  640. if (st->bringup)
  641. st->state--;
  642. else
  643. st->state++;
  644. }
  645. st->bringup = bringup;
  646. if (cpu_dying(cpu) != !bringup)
  647. set_cpu_dying(cpu, !bringup);
  648. }
  649. /* Regular hotplug invocation of the AP hotplug thread */
  650. static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st)
  651. {
  652. if (!st->single && st->state == st->target)
  653. return;
  654. st->result = 0;
  655. /*
  656. * Make sure the above stores are visible before should_run becomes
  657. * true. Paired with the mb() above in cpuhp_thread_fun()
  658. */
  659. smp_mb();
  660. st->should_run = true;
  661. wake_up_process(st->thread);
  662. wait_for_ap_thread(st, st->bringup);
  663. }
  664. static int cpuhp_kick_ap(int cpu, struct cpuhp_cpu_state *st,
  665. enum cpuhp_state target)
  666. {
  667. enum cpuhp_state prev_state;
  668. int ret;
  669. prev_state = cpuhp_set_state(cpu, st, target);
  670. __cpuhp_kick_ap(st);
  671. if ((ret = st->result)) {
  672. cpuhp_reset_state(cpu, st, prev_state);
  673. __cpuhp_kick_ap(st);
  674. }
  675. return ret;
  676. }
  677. static int bringup_wait_for_ap_online(unsigned int cpu)
  678. {
  679. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  680. /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
  681. wait_for_ap_thread(st, true);
  682. if (WARN_ON_ONCE((!cpu_online(cpu))))
  683. return -ECANCELED;
  684. /* Unpark the hotplug thread of the target cpu */
  685. kthread_unpark(st->thread);
  686. /*
  687. * SMT soft disabling on X86 requires to bring the CPU out of the
  688. * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit. The
  689. * CPU marked itself as booted_once in notify_cpu_starting() so the
  690. * cpu_bootable() check will now return false if this is not the
  691. * primary sibling.
  692. */
  693. if (!cpu_bootable(cpu))
  694. return -ECANCELED;
  695. return 0;
  696. }
  697. #ifdef CONFIG_HOTPLUG_SPLIT_STARTUP
  698. static int cpuhp_kick_ap_alive(unsigned int cpu)
  699. {
  700. if (!cpuhp_can_boot_ap(cpu))
  701. return -EAGAIN;
  702. return arch_cpuhp_kick_ap_alive(cpu, idle_thread_get(cpu));
  703. }
  704. static int cpuhp_bringup_ap(unsigned int cpu)
  705. {
  706. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  707. int ret;
  708. /*
  709. * Some architectures have to walk the irq descriptors to
  710. * setup the vector space for the cpu which comes online.
  711. * Prevent irq alloc/free across the bringup.
  712. */
  713. irq_lock_sparse();
  714. ret = cpuhp_bp_sync_alive(cpu);
  715. if (ret)
  716. goto out_unlock;
  717. ret = bringup_wait_for_ap_online(cpu);
  718. if (ret)
  719. goto out_unlock;
  720. irq_unlock_sparse();
  721. if (st->target <= CPUHP_AP_ONLINE_IDLE)
  722. return 0;
  723. return cpuhp_kick_ap(cpu, st, st->target);
  724. out_unlock:
  725. irq_unlock_sparse();
  726. return ret;
  727. }
  728. #else
  729. static int bringup_cpu(unsigned int cpu)
  730. {
  731. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  732. struct task_struct *idle = idle_thread_get(cpu);
  733. int ret;
  734. if (!cpuhp_can_boot_ap(cpu))
  735. return -EAGAIN;
  736. /*
  737. * Some architectures have to walk the irq descriptors to
  738. * setup the vector space for the cpu which comes online.
  739. *
  740. * Prevent irq alloc/free across the bringup by acquiring the
  741. * sparse irq lock. Hold it until the upcoming CPU completes the
  742. * startup in cpuhp_online_idle() which allows to avoid
  743. * intermediate synchronization points in the architecture code.
  744. */
  745. irq_lock_sparse();
  746. ret = __cpu_up(cpu, idle);
  747. if (ret)
  748. goto out_unlock;
  749. ret = cpuhp_bp_sync_alive(cpu);
  750. if (ret)
  751. goto out_unlock;
  752. ret = bringup_wait_for_ap_online(cpu);
  753. if (ret)
  754. goto out_unlock;
  755. irq_unlock_sparse();
  756. if (st->target <= CPUHP_AP_ONLINE_IDLE)
  757. return 0;
  758. return cpuhp_kick_ap(cpu, st, st->target);
  759. out_unlock:
  760. irq_unlock_sparse();
  761. return ret;
  762. }
  763. #endif
  764. static int finish_cpu(unsigned int cpu)
  765. {
  766. struct task_struct *idle = idle_thread_get(cpu);
  767. struct mm_struct *mm = idle->active_mm;
  768. /*
  769. * idle_task_exit() will have switched to &init_mm, now
  770. * clean up any remaining active_mm state.
  771. */
  772. if (mm != &init_mm)
  773. idle->active_mm = &init_mm;
  774. mmdrop_lazy_tlb(mm);
  775. return 0;
  776. }
  777. /*
  778. * Hotplug state machine related functions
  779. */
  780. /*
  781. * Get the next state to run. Empty ones will be skipped. Returns true if a
  782. * state must be run.
  783. *
  784. * st->state will be modified ahead of time, to match state_to_run, as if it
  785. * has already ran.
  786. */
  787. static bool cpuhp_next_state(bool bringup,
  788. enum cpuhp_state *state_to_run,
  789. struct cpuhp_cpu_state *st,
  790. enum cpuhp_state target)
  791. {
  792. do {
  793. if (bringup) {
  794. if (st->state >= target)
  795. return false;
  796. *state_to_run = ++st->state;
  797. } else {
  798. if (st->state <= target)
  799. return false;
  800. *state_to_run = st->state--;
  801. }
  802. if (!cpuhp_step_empty(bringup, cpuhp_get_step(*state_to_run)))
  803. break;
  804. } while (true);
  805. return true;
  806. }
  807. static int __cpuhp_invoke_callback_range(bool bringup,
  808. unsigned int cpu,
  809. struct cpuhp_cpu_state *st,
  810. enum cpuhp_state target,
  811. bool nofail)
  812. {
  813. enum cpuhp_state state;
  814. int ret = 0;
  815. while (cpuhp_next_state(bringup, &state, st, target)) {
  816. int err;
  817. err = cpuhp_invoke_callback(cpu, state, bringup, NULL, NULL);
  818. if (!err)
  819. continue;
  820. if (nofail) {
  821. pr_warn("CPU %u %s state %s (%d) failed (%d)\n",
  822. cpu, bringup ? "UP" : "DOWN",
  823. cpuhp_get_step(st->state)->name,
  824. st->state, err);
  825. ret = -1;
  826. } else {
  827. ret = err;
  828. break;
  829. }
  830. }
  831. return ret;
  832. }
  833. static inline int cpuhp_invoke_callback_range(bool bringup,
  834. unsigned int cpu,
  835. struct cpuhp_cpu_state *st,
  836. enum cpuhp_state target)
  837. {
  838. return __cpuhp_invoke_callback_range(bringup, cpu, st, target, false);
  839. }
  840. static inline void cpuhp_invoke_callback_range_nofail(bool bringup,
  841. unsigned int cpu,
  842. struct cpuhp_cpu_state *st,
  843. enum cpuhp_state target)
  844. {
  845. __cpuhp_invoke_callback_range(bringup, cpu, st, target, true);
  846. }
  847. static inline bool can_rollback_cpu(struct cpuhp_cpu_state *st)
  848. {
  849. if (IS_ENABLED(CONFIG_HOTPLUG_CPU))
  850. return true;
  851. /*
  852. * When CPU hotplug is disabled, then taking the CPU down is not
  853. * possible because takedown_cpu() and the architecture and
  854. * subsystem specific mechanisms are not available. So the CPU
  855. * which would be completely unplugged again needs to stay around
  856. * in the current state.
  857. */
  858. return st->state <= CPUHP_BRINGUP_CPU;
  859. }
  860. static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
  861. enum cpuhp_state target)
  862. {
  863. enum cpuhp_state prev_state = st->state;
  864. int ret = 0;
  865. ret = cpuhp_invoke_callback_range(true, cpu, st, target);
  866. if (ret) {
  867. pr_debug("CPU UP failed (%d) CPU %u state %s (%d)\n",
  868. ret, cpu, cpuhp_get_step(st->state)->name,
  869. st->state);
  870. cpuhp_reset_state(cpu, st, prev_state);
  871. if (can_rollback_cpu(st))
  872. WARN_ON(cpuhp_invoke_callback_range(false, cpu, st,
  873. prev_state));
  874. }
  875. return ret;
  876. }
  877. /*
  878. * The cpu hotplug threads manage the bringup and teardown of the cpus
  879. */
  880. static int cpuhp_should_run(unsigned int cpu)
  881. {
  882. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  883. return st->should_run;
  884. }
  885. /*
  886. * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
  887. * callbacks when a state gets [un]installed at runtime.
  888. *
  889. * Each invocation of this function by the smpboot thread does a single AP
  890. * state callback.
  891. *
  892. * It has 3 modes of operation:
  893. * - single: runs st->cb_state
  894. * - up: runs ++st->state, while st->state < st->target
  895. * - down: runs st->state--, while st->state > st->target
  896. *
  897. * When complete or on error, should_run is cleared and the completion is fired.
  898. */
  899. static void cpuhp_thread_fun(unsigned int cpu)
  900. {
  901. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  902. bool bringup = st->bringup;
  903. enum cpuhp_state state;
  904. if (WARN_ON_ONCE(!st->should_run))
  905. return;
  906. /*
  907. * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures
  908. * that if we see ->should_run we also see the rest of the state.
  909. */
  910. smp_mb();
  911. /*
  912. * The BP holds the hotplug lock, but we're now running on the AP,
  913. * ensure that anybody asserting the lock is held, will actually find
  914. * it so.
  915. */
  916. lockdep_acquire_cpus_lock();
  917. cpuhp_lock_acquire(bringup);
  918. if (st->single) {
  919. state = st->cb_state;
  920. st->should_run = false;
  921. } else {
  922. st->should_run = cpuhp_next_state(bringup, &state, st, st->target);
  923. if (!st->should_run)
  924. goto end;
  925. }
  926. WARN_ON_ONCE(!cpuhp_is_ap_state(state));
  927. if (cpuhp_is_atomic_state(state)) {
  928. local_irq_disable();
  929. st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
  930. local_irq_enable();
  931. /*
  932. * STARTING/DYING must not fail!
  933. */
  934. WARN_ON_ONCE(st->result);
  935. } else {
  936. st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
  937. }
  938. if (st->result) {
  939. /*
  940. * If we fail on a rollback, we're up a creek without no
  941. * paddle, no way forward, no way back. We loose, thanks for
  942. * playing.
  943. */
  944. WARN_ON_ONCE(st->rollback);
  945. st->should_run = false;
  946. }
  947. end:
  948. cpuhp_lock_release(bringup);
  949. lockdep_release_cpus_lock();
  950. if (!st->should_run)
  951. complete_ap_thread(st, bringup);
  952. }
  953. /* Invoke a single callback on a remote cpu */
  954. static int
  955. cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
  956. struct hlist_node *node)
  957. {
  958. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  959. int ret;
  960. if (!cpu_online(cpu))
  961. return 0;
  962. cpuhp_lock_acquire(false);
  963. cpuhp_lock_release(false);
  964. cpuhp_lock_acquire(true);
  965. cpuhp_lock_release(true);
  966. /*
  967. * If we are up and running, use the hotplug thread. For early calls
  968. * we invoke the thread function directly.
  969. */
  970. if (!st->thread)
  971. return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
  972. st->rollback = false;
  973. st->last = NULL;
  974. st->node = node;
  975. st->bringup = bringup;
  976. st->cb_state = state;
  977. st->single = true;
  978. __cpuhp_kick_ap(st);
  979. /*
  980. * If we failed and did a partial, do a rollback.
  981. */
  982. if ((ret = st->result) && st->last) {
  983. st->rollback = true;
  984. st->bringup = !bringup;
  985. __cpuhp_kick_ap(st);
  986. }
  987. /*
  988. * Clean up the leftovers so the next hotplug operation wont use stale
  989. * data.
  990. */
  991. st->node = st->last = NULL;
  992. return ret;
  993. }
  994. static int cpuhp_kick_ap_work(unsigned int cpu)
  995. {
  996. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  997. enum cpuhp_state prev_state = st->state;
  998. int ret;
  999. cpuhp_lock_acquire(false);
  1000. cpuhp_lock_release(false);
  1001. cpuhp_lock_acquire(true);
  1002. cpuhp_lock_release(true);
  1003. trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work);
  1004. ret = cpuhp_kick_ap(cpu, st, st->target);
  1005. trace_cpuhp_exit(cpu, st->state, prev_state, ret);
  1006. return ret;
  1007. }
  1008. static struct smp_hotplug_thread cpuhp_threads = {
  1009. .store = &cpuhp_state.thread,
  1010. .thread_should_run = cpuhp_should_run,
  1011. .thread_fn = cpuhp_thread_fun,
  1012. .thread_comm = "cpuhp/%u",
  1013. .selfparking = true,
  1014. };
  1015. static __init void cpuhp_init_state(void)
  1016. {
  1017. struct cpuhp_cpu_state *st;
  1018. int cpu;
  1019. for_each_possible_cpu(cpu) {
  1020. st = per_cpu_ptr(&cpuhp_state, cpu);
  1021. init_completion(&st->done_up);
  1022. init_completion(&st->done_down);
  1023. }
  1024. }
  1025. void __init cpuhp_threads_init(void)
  1026. {
  1027. cpuhp_init_state();
  1028. BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
  1029. kthread_unpark(this_cpu_read(cpuhp_state.thread));
  1030. }
  1031. #ifdef CONFIG_HOTPLUG_CPU
  1032. #ifndef arch_clear_mm_cpumask_cpu
  1033. #define arch_clear_mm_cpumask_cpu(cpu, mm) cpumask_clear_cpu(cpu, mm_cpumask(mm))
  1034. #endif
  1035. /**
  1036. * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
  1037. * @cpu: a CPU id
  1038. *
  1039. * This function walks all processes, finds a valid mm struct for each one and
  1040. * then clears a corresponding bit in mm's cpumask. While this all sounds
  1041. * trivial, there are various non-obvious corner cases, which this function
  1042. * tries to solve in a safe manner.
  1043. *
  1044. * Also note that the function uses a somewhat relaxed locking scheme, so it may
  1045. * be called only for an already offlined CPU.
  1046. */
  1047. void clear_tasks_mm_cpumask(int cpu)
  1048. {
  1049. struct task_struct *p;
  1050. /*
  1051. * This function is called after the cpu is taken down and marked
  1052. * offline, so its not like new tasks will ever get this cpu set in
  1053. * their mm mask. -- Peter Zijlstra
  1054. * Thus, we may use rcu_read_lock() here, instead of grabbing
  1055. * full-fledged tasklist_lock.
  1056. */
  1057. WARN_ON(cpu_online(cpu));
  1058. rcu_read_lock();
  1059. for_each_process(p) {
  1060. struct task_struct *t;
  1061. /*
  1062. * Main thread might exit, but other threads may still have
  1063. * a valid mm. Find one.
  1064. */
  1065. t = find_lock_task_mm(p);
  1066. if (!t)
  1067. continue;
  1068. arch_clear_mm_cpumask_cpu(cpu, t->mm);
  1069. task_unlock(t);
  1070. }
  1071. rcu_read_unlock();
  1072. }
  1073. /* Take this CPU down. */
  1074. static int take_cpu_down(void *_param)
  1075. {
  1076. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  1077. enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
  1078. int err, cpu = smp_processor_id();
  1079. /* Ensure this CPU doesn't handle any more interrupts. */
  1080. err = __cpu_disable();
  1081. if (err < 0)
  1082. return err;
  1083. /*
  1084. * Must be called from CPUHP_TEARDOWN_CPU, which means, as we are going
  1085. * down, that the current state is CPUHP_TEARDOWN_CPU - 1.
  1086. */
  1087. WARN_ON(st->state != (CPUHP_TEARDOWN_CPU - 1));
  1088. /*
  1089. * Invoke the former CPU_DYING callbacks. DYING must not fail!
  1090. */
  1091. cpuhp_invoke_callback_range_nofail(false, cpu, st, target);
  1092. /* Park the stopper thread */
  1093. stop_machine_park(cpu);
  1094. return 0;
  1095. }
  1096. static int takedown_cpu(unsigned int cpu)
  1097. {
  1098. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1099. int err;
  1100. /* Park the smpboot threads */
  1101. kthread_park(st->thread);
  1102. /*
  1103. * Prevent irq alloc/free while the dying cpu reorganizes the
  1104. * interrupt affinities.
  1105. */
  1106. irq_lock_sparse();
  1107. /*
  1108. * So now all preempt/rcu users must observe !cpu_active().
  1109. */
  1110. err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
  1111. if (err) {
  1112. /* CPU refused to die */
  1113. irq_unlock_sparse();
  1114. /* Unpark the hotplug thread so we can rollback there */
  1115. kthread_unpark(st->thread);
  1116. return err;
  1117. }
  1118. BUG_ON(cpu_online(cpu));
  1119. /*
  1120. * The teardown callback for CPUHP_AP_SCHED_STARTING will have removed
  1121. * all runnable tasks from the CPU, there's only the idle task left now
  1122. * that the migration thread is done doing the stop_machine thing.
  1123. *
  1124. * Wait for the stop thread to go away.
  1125. */
  1126. wait_for_ap_thread(st, false);
  1127. BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
  1128. /* Interrupts are moved away from the dying cpu, reenable alloc/free */
  1129. irq_unlock_sparse();
  1130. hotplug_cpu__broadcast_tick_pull(cpu);
  1131. /* This actually kills the CPU. */
  1132. __cpu_die(cpu);
  1133. cpuhp_bp_sync_dead(cpu);
  1134. tick_cleanup_dead_cpu(cpu);
  1135. /*
  1136. * Callbacks must be re-integrated right away to the RCU state machine.
  1137. * Otherwise an RCU callback could block a further teardown function
  1138. * waiting for its completion.
  1139. */
  1140. rcutree_migrate_callbacks(cpu);
  1141. return 0;
  1142. }
  1143. static void cpuhp_complete_idle_dead(void *arg)
  1144. {
  1145. struct cpuhp_cpu_state *st = arg;
  1146. complete_ap_thread(st, false);
  1147. }
  1148. void cpuhp_report_idle_dead(void)
  1149. {
  1150. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  1151. BUG_ON(st->state != CPUHP_AP_OFFLINE);
  1152. tick_assert_timekeeping_handover();
  1153. rcutree_report_cpu_dead();
  1154. st->state = CPUHP_AP_IDLE_DEAD;
  1155. /*
  1156. * We cannot call complete after rcutree_report_cpu_dead() so we delegate it
  1157. * to an online cpu.
  1158. */
  1159. smp_call_function_single(cpumask_first(cpu_online_mask),
  1160. cpuhp_complete_idle_dead, st, 0);
  1161. }
  1162. static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
  1163. enum cpuhp_state target)
  1164. {
  1165. enum cpuhp_state prev_state = st->state;
  1166. int ret = 0;
  1167. ret = cpuhp_invoke_callback_range(false, cpu, st, target);
  1168. if (ret) {
  1169. pr_debug("CPU DOWN failed (%d) CPU %u state %s (%d)\n",
  1170. ret, cpu, cpuhp_get_step(st->state)->name,
  1171. st->state);
  1172. cpuhp_reset_state(cpu, st, prev_state);
  1173. if (st->state < prev_state)
  1174. WARN_ON(cpuhp_invoke_callback_range(true, cpu, st,
  1175. prev_state));
  1176. }
  1177. return ret;
  1178. }
  1179. /* Requires cpu_add_remove_lock to be held */
  1180. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
  1181. enum cpuhp_state target)
  1182. {
  1183. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1184. int prev_state, ret = 0;
  1185. if (num_online_cpus() == 1)
  1186. return -EBUSY;
  1187. if (!cpu_present(cpu))
  1188. return -EINVAL;
  1189. cpus_write_lock();
  1190. cpuhp_tasks_frozen = tasks_frozen;
  1191. prev_state = cpuhp_set_state(cpu, st, target);
  1192. /*
  1193. * If the current CPU state is in the range of the AP hotplug thread,
  1194. * then we need to kick the thread.
  1195. */
  1196. if (st->state > CPUHP_TEARDOWN_CPU) {
  1197. st->target = max((int)target, CPUHP_TEARDOWN_CPU);
  1198. ret = cpuhp_kick_ap_work(cpu);
  1199. /*
  1200. * The AP side has done the error rollback already. Just
  1201. * return the error code..
  1202. */
  1203. if (ret)
  1204. goto out;
  1205. /*
  1206. * We might have stopped still in the range of the AP hotplug
  1207. * thread. Nothing to do anymore.
  1208. */
  1209. if (st->state > CPUHP_TEARDOWN_CPU)
  1210. goto out;
  1211. st->target = target;
  1212. }
  1213. /*
  1214. * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
  1215. * to do the further cleanups.
  1216. */
  1217. ret = cpuhp_down_callbacks(cpu, st, target);
  1218. if (ret && st->state < prev_state) {
  1219. if (st->state == CPUHP_TEARDOWN_CPU) {
  1220. cpuhp_reset_state(cpu, st, prev_state);
  1221. __cpuhp_kick_ap(st);
  1222. } else {
  1223. WARN(1, "DEAD callback error for CPU%d", cpu);
  1224. }
  1225. }
  1226. out:
  1227. cpus_write_unlock();
  1228. /*
  1229. * Do post unplug cleanup. This is still protected against
  1230. * concurrent CPU hotplug via cpu_add_remove_lock.
  1231. */
  1232. lockup_detector_cleanup();
  1233. arch_smt_update();
  1234. return ret;
  1235. }
  1236. struct cpu_down_work {
  1237. unsigned int cpu;
  1238. enum cpuhp_state target;
  1239. };
  1240. static long __cpu_down_maps_locked(void *arg)
  1241. {
  1242. struct cpu_down_work *work = arg;
  1243. return _cpu_down(work->cpu, 0, work->target);
  1244. }
  1245. static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
  1246. {
  1247. struct cpu_down_work work = { .cpu = cpu, .target = target, };
  1248. /*
  1249. * If the platform does not support hotplug, report it explicitly to
  1250. * differentiate it from a transient offlining failure.
  1251. */
  1252. if (cpu_hotplug_offline_disabled)
  1253. return -EOPNOTSUPP;
  1254. if (cpu_hotplug_disabled)
  1255. return -EBUSY;
  1256. /*
  1257. * Ensure that the control task does not run on the to be offlined
  1258. * CPU to prevent a deadlock against cfs_b->period_timer.
  1259. * Also keep at least one housekeeping cpu onlined to avoid generating
  1260. * an empty sched_domain span.
  1261. */
  1262. for_each_cpu_and(cpu, cpu_online_mask, housekeeping_cpumask(HK_TYPE_DOMAIN)) {
  1263. if (cpu != work.cpu)
  1264. return work_on_cpu(cpu, __cpu_down_maps_locked, &work);
  1265. }
  1266. return -EBUSY;
  1267. }
  1268. static int cpu_down(unsigned int cpu, enum cpuhp_state target)
  1269. {
  1270. int err;
  1271. cpu_maps_update_begin();
  1272. err = cpu_down_maps_locked(cpu, target);
  1273. cpu_maps_update_done();
  1274. return err;
  1275. }
  1276. /**
  1277. * cpu_device_down - Bring down a cpu device
  1278. * @dev: Pointer to the cpu device to offline
  1279. *
  1280. * This function is meant to be used by device core cpu subsystem only.
  1281. *
  1282. * Other subsystems should use remove_cpu() instead.
  1283. *
  1284. * Return: %0 on success or a negative errno code
  1285. */
  1286. int cpu_device_down(struct device *dev)
  1287. {
  1288. return cpu_down(dev->id, CPUHP_OFFLINE);
  1289. }
  1290. int remove_cpu(unsigned int cpu)
  1291. {
  1292. int ret;
  1293. lock_device_hotplug();
  1294. ret = device_offline(get_cpu_device(cpu));
  1295. unlock_device_hotplug();
  1296. return ret;
  1297. }
  1298. EXPORT_SYMBOL_GPL(remove_cpu);
  1299. void smp_shutdown_nonboot_cpus(unsigned int primary_cpu)
  1300. {
  1301. unsigned int cpu;
  1302. int error;
  1303. cpu_maps_update_begin();
  1304. /*
  1305. * Make certain the cpu I'm about to reboot on is online.
  1306. *
  1307. * This is inline to what migrate_to_reboot_cpu() already do.
  1308. */
  1309. if (!cpu_online(primary_cpu))
  1310. primary_cpu = cpumask_first(cpu_online_mask);
  1311. for_each_online_cpu(cpu) {
  1312. if (cpu == primary_cpu)
  1313. continue;
  1314. error = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
  1315. if (error) {
  1316. pr_err("Failed to offline CPU%d - error=%d",
  1317. cpu, error);
  1318. break;
  1319. }
  1320. }
  1321. /*
  1322. * Ensure all but the reboot CPU are offline.
  1323. */
  1324. BUG_ON(num_online_cpus() > 1);
  1325. /*
  1326. * Make sure the CPUs won't be enabled by someone else after this
  1327. * point. Kexec will reboot to a new kernel shortly resetting
  1328. * everything along the way.
  1329. */
  1330. cpu_hotplug_disabled++;
  1331. cpu_maps_update_done();
  1332. }
  1333. #else
  1334. #define takedown_cpu NULL
  1335. #endif /*CONFIG_HOTPLUG_CPU*/
  1336. /**
  1337. * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
  1338. * @cpu: cpu that just started
  1339. *
  1340. * It must be called by the arch code on the new cpu, before the new cpu
  1341. * enables interrupts and before the "boot" cpu returns from __cpu_up().
  1342. */
  1343. void notify_cpu_starting(unsigned int cpu)
  1344. {
  1345. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1346. enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
  1347. rcutree_report_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
  1348. cpumask_set_cpu(cpu, &cpus_booted_once_mask);
  1349. /*
  1350. * STARTING must not fail!
  1351. */
  1352. cpuhp_invoke_callback_range_nofail(true, cpu, st, target);
  1353. }
  1354. /*
  1355. * Called from the idle task. Wake up the controlling task which brings the
  1356. * hotplug thread of the upcoming CPU up and then delegates the rest of the
  1357. * online bringup to the hotplug thread.
  1358. */
  1359. void cpuhp_online_idle(enum cpuhp_state state)
  1360. {
  1361. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  1362. /* Happens for the boot cpu */
  1363. if (state != CPUHP_AP_ONLINE_IDLE)
  1364. return;
  1365. cpuhp_ap_update_sync_state(SYNC_STATE_ONLINE);
  1366. /*
  1367. * Unpark the stopper thread before we start the idle loop (and start
  1368. * scheduling); this ensures the stopper task is always available.
  1369. */
  1370. stop_machine_unpark(smp_processor_id());
  1371. st->state = CPUHP_AP_ONLINE_IDLE;
  1372. complete_ap_thread(st, true);
  1373. }
  1374. /* Requires cpu_add_remove_lock to be held */
  1375. static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
  1376. {
  1377. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1378. struct task_struct *idle;
  1379. int ret = 0;
  1380. cpus_write_lock();
  1381. if (!cpu_present(cpu)) {
  1382. ret = -EINVAL;
  1383. goto out;
  1384. }
  1385. /*
  1386. * The caller of cpu_up() might have raced with another
  1387. * caller. Nothing to do.
  1388. */
  1389. if (st->state >= target)
  1390. goto out;
  1391. if (st->state == CPUHP_OFFLINE) {
  1392. /* Let it fail before we try to bring the cpu up */
  1393. idle = idle_thread_get(cpu);
  1394. if (IS_ERR(idle)) {
  1395. ret = PTR_ERR(idle);
  1396. goto out;
  1397. }
  1398. /*
  1399. * Reset stale stack state from the last time this CPU was online.
  1400. */
  1401. scs_task_reset(idle);
  1402. kasan_unpoison_task_stack(idle);
  1403. }
  1404. cpuhp_tasks_frozen = tasks_frozen;
  1405. cpuhp_set_state(cpu, st, target);
  1406. /*
  1407. * If the current CPU state is in the range of the AP hotplug thread,
  1408. * then we need to kick the thread once more.
  1409. */
  1410. if (st->state > CPUHP_BRINGUP_CPU) {
  1411. ret = cpuhp_kick_ap_work(cpu);
  1412. /*
  1413. * The AP side has done the error rollback already. Just
  1414. * return the error code..
  1415. */
  1416. if (ret)
  1417. goto out;
  1418. }
  1419. /*
  1420. * Try to reach the target state. We max out on the BP at
  1421. * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
  1422. * responsible for bringing it up to the target state.
  1423. */
  1424. target = min((int)target, CPUHP_BRINGUP_CPU);
  1425. ret = cpuhp_up_callbacks(cpu, st, target);
  1426. out:
  1427. cpus_write_unlock();
  1428. arch_smt_update();
  1429. return ret;
  1430. }
  1431. static int cpu_up(unsigned int cpu, enum cpuhp_state target)
  1432. {
  1433. int err = 0;
  1434. if (!cpu_possible(cpu)) {
  1435. pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
  1436. cpu);
  1437. return -EINVAL;
  1438. }
  1439. err = try_online_node(cpu_to_node(cpu));
  1440. if (err)
  1441. return err;
  1442. cpu_maps_update_begin();
  1443. if (cpu_hotplug_disabled) {
  1444. err = -EBUSY;
  1445. goto out;
  1446. }
  1447. if (!cpu_bootable(cpu)) {
  1448. err = -EPERM;
  1449. goto out;
  1450. }
  1451. err = _cpu_up(cpu, 0, target);
  1452. out:
  1453. cpu_maps_update_done();
  1454. return err;
  1455. }
  1456. /**
  1457. * cpu_device_up - Bring up a cpu device
  1458. * @dev: Pointer to the cpu device to online
  1459. *
  1460. * This function is meant to be used by device core cpu subsystem only.
  1461. *
  1462. * Other subsystems should use add_cpu() instead.
  1463. *
  1464. * Return: %0 on success or a negative errno code
  1465. */
  1466. int cpu_device_up(struct device *dev)
  1467. {
  1468. return cpu_up(dev->id, CPUHP_ONLINE);
  1469. }
  1470. int add_cpu(unsigned int cpu)
  1471. {
  1472. int ret;
  1473. lock_device_hotplug();
  1474. ret = device_online(get_cpu_device(cpu));
  1475. unlock_device_hotplug();
  1476. return ret;
  1477. }
  1478. EXPORT_SYMBOL_GPL(add_cpu);
  1479. /**
  1480. * bringup_hibernate_cpu - Bring up the CPU that we hibernated on
  1481. * @sleep_cpu: The cpu we hibernated on and should be brought up.
  1482. *
  1483. * On some architectures like arm64, we can hibernate on any CPU, but on
  1484. * wake up the CPU we hibernated on might be offline as a side effect of
  1485. * using maxcpus= for example.
  1486. *
  1487. * Return: %0 on success or a negative errno code
  1488. */
  1489. int bringup_hibernate_cpu(unsigned int sleep_cpu)
  1490. {
  1491. int ret;
  1492. if (!cpu_online(sleep_cpu)) {
  1493. pr_info("Hibernated on a CPU that is offline! Bringing CPU up.\n");
  1494. ret = cpu_up(sleep_cpu, CPUHP_ONLINE);
  1495. if (ret) {
  1496. pr_err("Failed to bring hibernate-CPU up!\n");
  1497. return ret;
  1498. }
  1499. }
  1500. return 0;
  1501. }
  1502. static void __init cpuhp_bringup_mask(const struct cpumask *mask, unsigned int ncpus,
  1503. enum cpuhp_state target)
  1504. {
  1505. unsigned int cpu;
  1506. for_each_cpu(cpu, mask) {
  1507. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1508. if (cpu_up(cpu, target) && can_rollback_cpu(st)) {
  1509. /*
  1510. * If this failed then cpu_up() might have only
  1511. * rolled back to CPUHP_BP_KICK_AP for the final
  1512. * online. Clean it up. NOOP if already rolled back.
  1513. */
  1514. WARN_ON(cpuhp_invoke_callback_range(false, cpu, st, CPUHP_OFFLINE));
  1515. }
  1516. if (!--ncpus)
  1517. break;
  1518. }
  1519. }
  1520. #ifdef CONFIG_HOTPLUG_PARALLEL
  1521. static bool __cpuhp_parallel_bringup __ro_after_init = true;
  1522. static int __init parallel_bringup_parse_param(char *arg)
  1523. {
  1524. return kstrtobool(arg, &__cpuhp_parallel_bringup);
  1525. }
  1526. early_param("cpuhp.parallel", parallel_bringup_parse_param);
  1527. #ifdef CONFIG_HOTPLUG_SMT
  1528. static inline bool cpuhp_smt_aware(void)
  1529. {
  1530. return cpu_smt_max_threads > 1;
  1531. }
  1532. static inline const struct cpumask *cpuhp_get_primary_thread_mask(void)
  1533. {
  1534. return cpu_primary_thread_mask;
  1535. }
  1536. #else
  1537. static inline bool cpuhp_smt_aware(void)
  1538. {
  1539. return false;
  1540. }
  1541. static inline const struct cpumask *cpuhp_get_primary_thread_mask(void)
  1542. {
  1543. return cpu_none_mask;
  1544. }
  1545. #endif
  1546. bool __weak arch_cpuhp_init_parallel_bringup(void)
  1547. {
  1548. return true;
  1549. }
  1550. /*
  1551. * On architectures which have enabled parallel bringup this invokes all BP
  1552. * prepare states for each of the to be onlined APs first. The last state
  1553. * sends the startup IPI to the APs. The APs proceed through the low level
  1554. * bringup code in parallel and then wait for the control CPU to release
  1555. * them one by one for the final onlining procedure.
  1556. *
  1557. * This avoids waiting for each AP to respond to the startup IPI in
  1558. * CPUHP_BRINGUP_CPU.
  1559. */
  1560. static bool __init cpuhp_bringup_cpus_parallel(unsigned int ncpus)
  1561. {
  1562. const struct cpumask *mask = cpu_present_mask;
  1563. if (__cpuhp_parallel_bringup)
  1564. __cpuhp_parallel_bringup = arch_cpuhp_init_parallel_bringup();
  1565. if (!__cpuhp_parallel_bringup)
  1566. return false;
  1567. if (cpuhp_smt_aware()) {
  1568. const struct cpumask *pmask = cpuhp_get_primary_thread_mask();
  1569. static struct cpumask tmp_mask __initdata;
  1570. /*
  1571. * X86 requires to prevent that SMT siblings stopped while
  1572. * the primary thread does a microcode update for various
  1573. * reasons. Bring the primary threads up first.
  1574. */
  1575. cpumask_and(&tmp_mask, mask, pmask);
  1576. cpuhp_bringup_mask(&tmp_mask, ncpus, CPUHP_BP_KICK_AP);
  1577. cpuhp_bringup_mask(&tmp_mask, ncpus, CPUHP_ONLINE);
  1578. /* Account for the online CPUs */
  1579. ncpus -= num_online_cpus();
  1580. if (!ncpus)
  1581. return true;
  1582. /* Create the mask for secondary CPUs */
  1583. cpumask_andnot(&tmp_mask, mask, pmask);
  1584. mask = &tmp_mask;
  1585. }
  1586. /* Bring the not-yet started CPUs up */
  1587. cpuhp_bringup_mask(mask, ncpus, CPUHP_BP_KICK_AP);
  1588. cpuhp_bringup_mask(mask, ncpus, CPUHP_ONLINE);
  1589. return true;
  1590. }
  1591. #else
  1592. static inline bool cpuhp_bringup_cpus_parallel(unsigned int ncpus) { return false; }
  1593. #endif /* CONFIG_HOTPLUG_PARALLEL */
  1594. void __init bringup_nonboot_cpus(unsigned int max_cpus)
  1595. {
  1596. if (!max_cpus)
  1597. return;
  1598. /* Try parallel bringup optimization if enabled */
  1599. if (cpuhp_bringup_cpus_parallel(max_cpus))
  1600. return;
  1601. /* Full per CPU serialized bringup */
  1602. cpuhp_bringup_mask(cpu_present_mask, max_cpus, CPUHP_ONLINE);
  1603. }
  1604. #ifdef CONFIG_PM_SLEEP_SMP
  1605. static cpumask_var_t frozen_cpus;
  1606. int freeze_secondary_cpus(int primary)
  1607. {
  1608. int cpu, error = 0;
  1609. cpu_maps_update_begin();
  1610. if (primary == -1) {
  1611. primary = cpumask_first(cpu_online_mask);
  1612. if (!housekeeping_cpu(primary, HK_TYPE_TIMER))
  1613. primary = housekeeping_any_cpu(HK_TYPE_TIMER);
  1614. } else {
  1615. if (!cpu_online(primary))
  1616. primary = cpumask_first(cpu_online_mask);
  1617. }
  1618. /*
  1619. * We take down all of the non-boot CPUs in one shot to avoid races
  1620. * with the userspace trying to use the CPU hotplug at the same time
  1621. */
  1622. cpumask_clear(frozen_cpus);
  1623. pr_info("Disabling non-boot CPUs ...\n");
  1624. for (cpu = nr_cpu_ids - 1; cpu >= 0; cpu--) {
  1625. if (!cpu_online(cpu) || cpu == primary)
  1626. continue;
  1627. if (pm_wakeup_pending()) {
  1628. pr_info("Wakeup pending. Abort CPU freeze\n");
  1629. error = -EBUSY;
  1630. break;
  1631. }
  1632. trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
  1633. error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
  1634. trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
  1635. if (!error)
  1636. cpumask_set_cpu(cpu, frozen_cpus);
  1637. else {
  1638. pr_err("Error taking CPU%d down: %d\n", cpu, error);
  1639. break;
  1640. }
  1641. }
  1642. if (!error)
  1643. BUG_ON(num_online_cpus() > 1);
  1644. else
  1645. pr_err("Non-boot CPUs are not disabled\n");
  1646. /*
  1647. * Make sure the CPUs won't be enabled by someone else. We need to do
  1648. * this even in case of failure as all freeze_secondary_cpus() users are
  1649. * supposed to do thaw_secondary_cpus() on the failure path.
  1650. */
  1651. cpu_hotplug_disabled++;
  1652. cpu_maps_update_done();
  1653. return error;
  1654. }
  1655. void __weak arch_thaw_secondary_cpus_begin(void)
  1656. {
  1657. }
  1658. void __weak arch_thaw_secondary_cpus_end(void)
  1659. {
  1660. }
  1661. void thaw_secondary_cpus(void)
  1662. {
  1663. int cpu, error;
  1664. /* Allow everyone to use the CPU hotplug again */
  1665. cpu_maps_update_begin();
  1666. __cpu_hotplug_enable();
  1667. if (cpumask_empty(frozen_cpus))
  1668. goto out;
  1669. pr_info("Enabling non-boot CPUs ...\n");
  1670. arch_thaw_secondary_cpus_begin();
  1671. for_each_cpu(cpu, frozen_cpus) {
  1672. trace_suspend_resume(TPS("CPU_ON"), cpu, true);
  1673. error = _cpu_up(cpu, 1, CPUHP_ONLINE);
  1674. trace_suspend_resume(TPS("CPU_ON"), cpu, false);
  1675. if (!error) {
  1676. pr_info("CPU%d is up\n", cpu);
  1677. continue;
  1678. }
  1679. pr_warn("Error taking CPU%d up: %d\n", cpu, error);
  1680. }
  1681. arch_thaw_secondary_cpus_end();
  1682. cpumask_clear(frozen_cpus);
  1683. out:
  1684. cpu_maps_update_done();
  1685. }
  1686. static int __init alloc_frozen_cpus(void)
  1687. {
  1688. if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
  1689. return -ENOMEM;
  1690. return 0;
  1691. }
  1692. core_initcall(alloc_frozen_cpus);
  1693. /*
  1694. * When callbacks for CPU hotplug notifications are being executed, we must
  1695. * ensure that the state of the system with respect to the tasks being frozen
  1696. * or not, as reported by the notification, remains unchanged *throughout the
  1697. * duration* of the execution of the callbacks.
  1698. * Hence we need to prevent the freezer from racing with regular CPU hotplug.
  1699. *
  1700. * This synchronization is implemented by mutually excluding regular CPU
  1701. * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
  1702. * Hibernate notifications.
  1703. */
  1704. static int
  1705. cpu_hotplug_pm_callback(struct notifier_block *nb,
  1706. unsigned long action, void *ptr)
  1707. {
  1708. switch (action) {
  1709. case PM_SUSPEND_PREPARE:
  1710. case PM_HIBERNATION_PREPARE:
  1711. cpu_hotplug_disable();
  1712. break;
  1713. case PM_POST_SUSPEND:
  1714. case PM_POST_HIBERNATION:
  1715. cpu_hotplug_enable();
  1716. break;
  1717. default:
  1718. return NOTIFY_DONE;
  1719. }
  1720. return NOTIFY_OK;
  1721. }
  1722. static int __init cpu_hotplug_pm_sync_init(void)
  1723. {
  1724. /*
  1725. * cpu_hotplug_pm_callback has higher priority than x86
  1726. * bsp_pm_callback which depends on cpu_hotplug_pm_callback
  1727. * to disable cpu hotplug to avoid cpu hotplug race.
  1728. */
  1729. pm_notifier(cpu_hotplug_pm_callback, 0);
  1730. return 0;
  1731. }
  1732. core_initcall(cpu_hotplug_pm_sync_init);
  1733. #endif /* CONFIG_PM_SLEEP_SMP */
  1734. int __boot_cpu_id;
  1735. #endif /* CONFIG_SMP */
  1736. /* Boot processor state steps */
  1737. static struct cpuhp_step cpuhp_hp_states[] = {
  1738. [CPUHP_OFFLINE] = {
  1739. .name = "offline",
  1740. .startup.single = NULL,
  1741. .teardown.single = NULL,
  1742. },
  1743. #ifdef CONFIG_SMP
  1744. [CPUHP_CREATE_THREADS]= {
  1745. .name = "threads:prepare",
  1746. .startup.single = smpboot_create_threads,
  1747. .teardown.single = NULL,
  1748. .cant_stop = true,
  1749. },
  1750. [CPUHP_PERF_PREPARE] = {
  1751. .name = "perf:prepare",
  1752. .startup.single = perf_event_init_cpu,
  1753. .teardown.single = perf_event_exit_cpu,
  1754. },
  1755. [CPUHP_RANDOM_PREPARE] = {
  1756. .name = "random:prepare",
  1757. .startup.single = random_prepare_cpu,
  1758. .teardown.single = NULL,
  1759. },
  1760. [CPUHP_WORKQUEUE_PREP] = {
  1761. .name = "workqueue:prepare",
  1762. .startup.single = workqueue_prepare_cpu,
  1763. .teardown.single = NULL,
  1764. },
  1765. [CPUHP_HRTIMERS_PREPARE] = {
  1766. .name = "hrtimers:prepare",
  1767. .startup.single = hrtimers_prepare_cpu,
  1768. .teardown.single = NULL,
  1769. },
  1770. [CPUHP_SMPCFD_PREPARE] = {
  1771. .name = "smpcfd:prepare",
  1772. .startup.single = smpcfd_prepare_cpu,
  1773. .teardown.single = smpcfd_dead_cpu,
  1774. },
  1775. [CPUHP_RELAY_PREPARE] = {
  1776. .name = "relay:prepare",
  1777. .startup.single = relay_prepare_cpu,
  1778. .teardown.single = NULL,
  1779. },
  1780. [CPUHP_RCUTREE_PREP] = {
  1781. .name = "RCU/tree:prepare",
  1782. .startup.single = rcutree_prepare_cpu,
  1783. .teardown.single = rcutree_dead_cpu,
  1784. },
  1785. /*
  1786. * On the tear-down path, timers_dead_cpu() must be invoked
  1787. * before blk_mq_queue_reinit_notify() from notify_dead(),
  1788. * otherwise a RCU stall occurs.
  1789. */
  1790. [CPUHP_TIMERS_PREPARE] = {
  1791. .name = "timers:prepare",
  1792. .startup.single = timers_prepare_cpu,
  1793. .teardown.single = timers_dead_cpu,
  1794. },
  1795. #ifdef CONFIG_HOTPLUG_SPLIT_STARTUP
  1796. /*
  1797. * Kicks the AP alive. AP will wait in cpuhp_ap_sync_alive() until
  1798. * the next step will release it.
  1799. */
  1800. [CPUHP_BP_KICK_AP] = {
  1801. .name = "cpu:kick_ap",
  1802. .startup.single = cpuhp_kick_ap_alive,
  1803. },
  1804. /*
  1805. * Waits for the AP to reach cpuhp_ap_sync_alive() and then
  1806. * releases it for the complete bringup.
  1807. */
  1808. [CPUHP_BRINGUP_CPU] = {
  1809. .name = "cpu:bringup",
  1810. .startup.single = cpuhp_bringup_ap,
  1811. .teardown.single = finish_cpu,
  1812. .cant_stop = true,
  1813. },
  1814. #else
  1815. /*
  1816. * All-in-one CPU bringup state which includes the kick alive.
  1817. */
  1818. [CPUHP_BRINGUP_CPU] = {
  1819. .name = "cpu:bringup",
  1820. .startup.single = bringup_cpu,
  1821. .teardown.single = finish_cpu,
  1822. .cant_stop = true,
  1823. },
  1824. #endif
  1825. /* Final state before CPU kills itself */
  1826. [CPUHP_AP_IDLE_DEAD] = {
  1827. .name = "idle:dead",
  1828. },
  1829. /*
  1830. * Last state before CPU enters the idle loop to die. Transient state
  1831. * for synchronization.
  1832. */
  1833. [CPUHP_AP_OFFLINE] = {
  1834. .name = "ap:offline",
  1835. .cant_stop = true,
  1836. },
  1837. /* First state is scheduler control. Interrupts are disabled */
  1838. [CPUHP_AP_SCHED_STARTING] = {
  1839. .name = "sched:starting",
  1840. .startup.single = sched_cpu_starting,
  1841. .teardown.single = sched_cpu_dying,
  1842. },
  1843. [CPUHP_AP_RCUTREE_DYING] = {
  1844. .name = "RCU/tree:dying",
  1845. .startup.single = NULL,
  1846. .teardown.single = rcutree_dying_cpu,
  1847. },
  1848. [CPUHP_AP_SMPCFD_DYING] = {
  1849. .name = "smpcfd:dying",
  1850. .startup.single = NULL,
  1851. .teardown.single = smpcfd_dying_cpu,
  1852. },
  1853. [CPUHP_AP_HRTIMERS_DYING] = {
  1854. .name = "hrtimers:dying",
  1855. .startup.single = hrtimers_cpu_starting,
  1856. .teardown.single = hrtimers_cpu_dying,
  1857. },
  1858. [CPUHP_AP_TICK_DYING] = {
  1859. .name = "tick:dying",
  1860. .startup.single = NULL,
  1861. .teardown.single = tick_cpu_dying,
  1862. },
  1863. /* Entry state on starting. Interrupts enabled from here on. Transient
  1864. * state for synchronsization */
  1865. [CPUHP_AP_ONLINE] = {
  1866. .name = "ap:online",
  1867. },
  1868. /*
  1869. * Handled on control processor until the plugged processor manages
  1870. * this itself.
  1871. */
  1872. [CPUHP_TEARDOWN_CPU] = {
  1873. .name = "cpu:teardown",
  1874. .startup.single = NULL,
  1875. .teardown.single = takedown_cpu,
  1876. .cant_stop = true,
  1877. },
  1878. [CPUHP_AP_SCHED_WAIT_EMPTY] = {
  1879. .name = "sched:waitempty",
  1880. .startup.single = NULL,
  1881. .teardown.single = sched_cpu_wait_empty,
  1882. },
  1883. /* Handle smpboot threads park/unpark */
  1884. [CPUHP_AP_SMPBOOT_THREADS] = {
  1885. .name = "smpboot/threads:online",
  1886. .startup.single = smpboot_unpark_threads,
  1887. .teardown.single = smpboot_park_threads,
  1888. },
  1889. [CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
  1890. .name = "irq/affinity:online",
  1891. .startup.single = irq_affinity_online_cpu,
  1892. .teardown.single = NULL,
  1893. },
  1894. [CPUHP_AP_PERF_ONLINE] = {
  1895. .name = "perf:online",
  1896. .startup.single = perf_event_init_cpu,
  1897. .teardown.single = perf_event_exit_cpu,
  1898. },
  1899. [CPUHP_AP_WATCHDOG_ONLINE] = {
  1900. .name = "lockup_detector:online",
  1901. .startup.single = lockup_detector_online_cpu,
  1902. .teardown.single = lockup_detector_offline_cpu,
  1903. },
  1904. [CPUHP_AP_WORKQUEUE_ONLINE] = {
  1905. .name = "workqueue:online",
  1906. .startup.single = workqueue_online_cpu,
  1907. .teardown.single = workqueue_offline_cpu,
  1908. },
  1909. [CPUHP_AP_RANDOM_ONLINE] = {
  1910. .name = "random:online",
  1911. .startup.single = random_online_cpu,
  1912. .teardown.single = NULL,
  1913. },
  1914. [CPUHP_AP_RCUTREE_ONLINE] = {
  1915. .name = "RCU/tree:online",
  1916. .startup.single = rcutree_online_cpu,
  1917. .teardown.single = rcutree_offline_cpu,
  1918. },
  1919. #endif
  1920. /*
  1921. * The dynamically registered state space is here
  1922. */
  1923. #ifdef CONFIG_SMP
  1924. /* Last state is scheduler control setting the cpu active */
  1925. [CPUHP_AP_ACTIVE] = {
  1926. .name = "sched:active",
  1927. .startup.single = sched_cpu_activate,
  1928. .teardown.single = sched_cpu_deactivate,
  1929. },
  1930. #endif
  1931. /* CPU is fully up and running. */
  1932. [CPUHP_ONLINE] = {
  1933. .name = "online",
  1934. .startup.single = NULL,
  1935. .teardown.single = NULL,
  1936. },
  1937. };
  1938. /* Sanity check for callbacks */
  1939. static int cpuhp_cb_check(enum cpuhp_state state)
  1940. {
  1941. if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
  1942. return -EINVAL;
  1943. return 0;
  1944. }
  1945. /*
  1946. * Returns a free for dynamic slot assignment of the Online state. The states
  1947. * are protected by the cpuhp_slot_states mutex and an empty slot is identified
  1948. * by having no name assigned.
  1949. */
  1950. static int cpuhp_reserve_state(enum cpuhp_state state)
  1951. {
  1952. enum cpuhp_state i, end;
  1953. struct cpuhp_step *step;
  1954. switch (state) {
  1955. case CPUHP_AP_ONLINE_DYN:
  1956. step = cpuhp_hp_states + CPUHP_AP_ONLINE_DYN;
  1957. end = CPUHP_AP_ONLINE_DYN_END;
  1958. break;
  1959. case CPUHP_BP_PREPARE_DYN:
  1960. step = cpuhp_hp_states + CPUHP_BP_PREPARE_DYN;
  1961. end = CPUHP_BP_PREPARE_DYN_END;
  1962. break;
  1963. default:
  1964. return -EINVAL;
  1965. }
  1966. for (i = state; i <= end; i++, step++) {
  1967. if (!step->name)
  1968. return i;
  1969. }
  1970. WARN(1, "No more dynamic states available for CPU hotplug\n");
  1971. return -ENOSPC;
  1972. }
  1973. static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
  1974. int (*startup)(unsigned int cpu),
  1975. int (*teardown)(unsigned int cpu),
  1976. bool multi_instance)
  1977. {
  1978. /* (Un)Install the callbacks for further cpu hotplug operations */
  1979. struct cpuhp_step *sp;
  1980. int ret = 0;
  1981. /*
  1982. * If name is NULL, then the state gets removed.
  1983. *
  1984. * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
  1985. * the first allocation from these dynamic ranges, so the removal
  1986. * would trigger a new allocation and clear the wrong (already
  1987. * empty) state, leaving the callbacks of the to be cleared state
  1988. * dangling, which causes wreckage on the next hotplug operation.
  1989. */
  1990. if (name && (state == CPUHP_AP_ONLINE_DYN ||
  1991. state == CPUHP_BP_PREPARE_DYN)) {
  1992. ret = cpuhp_reserve_state(state);
  1993. if (ret < 0)
  1994. return ret;
  1995. state = ret;
  1996. }
  1997. sp = cpuhp_get_step(state);
  1998. if (name && sp->name)
  1999. return -EBUSY;
  2000. sp->startup.single = startup;
  2001. sp->teardown.single = teardown;
  2002. sp->name = name;
  2003. sp->multi_instance = multi_instance;
  2004. INIT_HLIST_HEAD(&sp->list);
  2005. return ret;
  2006. }
  2007. static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
  2008. {
  2009. return cpuhp_get_step(state)->teardown.single;
  2010. }
  2011. /*
  2012. * Call the startup/teardown function for a step either on the AP or
  2013. * on the current CPU.
  2014. */
  2015. static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
  2016. struct hlist_node *node)
  2017. {
  2018. struct cpuhp_step *sp = cpuhp_get_step(state);
  2019. int ret;
  2020. /*
  2021. * If there's nothing to do, we done.
  2022. * Relies on the union for multi_instance.
  2023. */
  2024. if (cpuhp_step_empty(bringup, sp))
  2025. return 0;
  2026. /*
  2027. * The non AP bound callbacks can fail on bringup. On teardown
  2028. * e.g. module removal we crash for now.
  2029. */
  2030. #ifdef CONFIG_SMP
  2031. if (cpuhp_is_ap_state(state))
  2032. ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
  2033. else
  2034. ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
  2035. #else
  2036. ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
  2037. #endif
  2038. BUG_ON(ret && !bringup);
  2039. return ret;
  2040. }
  2041. /*
  2042. * Called from __cpuhp_setup_state on a recoverable failure.
  2043. *
  2044. * Note: The teardown callbacks for rollback are not allowed to fail!
  2045. */
  2046. static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
  2047. struct hlist_node *node)
  2048. {
  2049. int cpu;
  2050. /* Roll back the already executed steps on the other cpus */
  2051. for_each_present_cpu(cpu) {
  2052. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  2053. int cpustate = st->state;
  2054. if (cpu >= failedcpu)
  2055. break;
  2056. /* Did we invoke the startup call on that cpu ? */
  2057. if (cpustate >= state)
  2058. cpuhp_issue_call(cpu, state, false, node);
  2059. }
  2060. }
  2061. int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
  2062. struct hlist_node *node,
  2063. bool invoke)
  2064. {
  2065. struct cpuhp_step *sp;
  2066. int cpu;
  2067. int ret;
  2068. lockdep_assert_cpus_held();
  2069. sp = cpuhp_get_step(state);
  2070. if (sp->multi_instance == false)
  2071. return -EINVAL;
  2072. mutex_lock(&cpuhp_state_mutex);
  2073. if (!invoke || !sp->startup.multi)
  2074. goto add_node;
  2075. /*
  2076. * Try to call the startup callback for each present cpu
  2077. * depending on the hotplug state of the cpu.
  2078. */
  2079. for_each_present_cpu(cpu) {
  2080. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  2081. int cpustate = st->state;
  2082. if (cpustate < state)
  2083. continue;
  2084. ret = cpuhp_issue_call(cpu, state, true, node);
  2085. if (ret) {
  2086. if (sp->teardown.multi)
  2087. cpuhp_rollback_install(cpu, state, node);
  2088. goto unlock;
  2089. }
  2090. }
  2091. add_node:
  2092. ret = 0;
  2093. hlist_add_head(node, &sp->list);
  2094. unlock:
  2095. mutex_unlock(&cpuhp_state_mutex);
  2096. return ret;
  2097. }
  2098. int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
  2099. bool invoke)
  2100. {
  2101. int ret;
  2102. cpus_read_lock();
  2103. ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
  2104. cpus_read_unlock();
  2105. return ret;
  2106. }
  2107. EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
  2108. /**
  2109. * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
  2110. * @state: The state to setup
  2111. * @name: Name of the step
  2112. * @invoke: If true, the startup function is invoked for cpus where
  2113. * cpu state >= @state
  2114. * @startup: startup callback function
  2115. * @teardown: teardown callback function
  2116. * @multi_instance: State is set up for multiple instances which get
  2117. * added afterwards.
  2118. *
  2119. * The caller needs to hold cpus read locked while calling this function.
  2120. * Return:
  2121. * On success:
  2122. * Positive state number if @state is CPUHP_AP_ONLINE_DYN or CPUHP_BP_PREPARE_DYN;
  2123. * 0 for all other states
  2124. * On failure: proper (negative) error code
  2125. */
  2126. int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state,
  2127. const char *name, bool invoke,
  2128. int (*startup)(unsigned int cpu),
  2129. int (*teardown)(unsigned int cpu),
  2130. bool multi_instance)
  2131. {
  2132. int cpu, ret = 0;
  2133. bool dynstate;
  2134. lockdep_assert_cpus_held();
  2135. if (cpuhp_cb_check(state) || !name)
  2136. return -EINVAL;
  2137. mutex_lock(&cpuhp_state_mutex);
  2138. ret = cpuhp_store_callbacks(state, name, startup, teardown,
  2139. multi_instance);
  2140. dynstate = state == CPUHP_AP_ONLINE_DYN || state == CPUHP_BP_PREPARE_DYN;
  2141. if (ret > 0 && dynstate) {
  2142. state = ret;
  2143. ret = 0;
  2144. }
  2145. if (ret || !invoke || !startup)
  2146. goto out;
  2147. /*
  2148. * Try to call the startup callback for each present cpu
  2149. * depending on the hotplug state of the cpu.
  2150. */
  2151. for_each_present_cpu(cpu) {
  2152. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  2153. int cpustate = st->state;
  2154. if (cpustate < state)
  2155. continue;
  2156. ret = cpuhp_issue_call(cpu, state, true, NULL);
  2157. if (ret) {
  2158. if (teardown)
  2159. cpuhp_rollback_install(cpu, state, NULL);
  2160. cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
  2161. goto out;
  2162. }
  2163. }
  2164. out:
  2165. mutex_unlock(&cpuhp_state_mutex);
  2166. /*
  2167. * If the requested state is CPUHP_AP_ONLINE_DYN or CPUHP_BP_PREPARE_DYN,
  2168. * return the dynamically allocated state in case of success.
  2169. */
  2170. if (!ret && dynstate)
  2171. return state;
  2172. return ret;
  2173. }
  2174. EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
  2175. int __cpuhp_setup_state(enum cpuhp_state state,
  2176. const char *name, bool invoke,
  2177. int (*startup)(unsigned int cpu),
  2178. int (*teardown)(unsigned int cpu),
  2179. bool multi_instance)
  2180. {
  2181. int ret;
  2182. cpus_read_lock();
  2183. ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
  2184. teardown, multi_instance);
  2185. cpus_read_unlock();
  2186. return ret;
  2187. }
  2188. EXPORT_SYMBOL(__cpuhp_setup_state);
  2189. int __cpuhp_state_remove_instance(enum cpuhp_state state,
  2190. struct hlist_node *node, bool invoke)
  2191. {
  2192. struct cpuhp_step *sp = cpuhp_get_step(state);
  2193. int cpu;
  2194. BUG_ON(cpuhp_cb_check(state));
  2195. if (!sp->multi_instance)
  2196. return -EINVAL;
  2197. cpus_read_lock();
  2198. mutex_lock(&cpuhp_state_mutex);
  2199. if (!invoke || !cpuhp_get_teardown_cb(state))
  2200. goto remove;
  2201. /*
  2202. * Call the teardown callback for each present cpu depending
  2203. * on the hotplug state of the cpu. This function is not
  2204. * allowed to fail currently!
  2205. */
  2206. for_each_present_cpu(cpu) {
  2207. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  2208. int cpustate = st->state;
  2209. if (cpustate >= state)
  2210. cpuhp_issue_call(cpu, state, false, node);
  2211. }
  2212. remove:
  2213. hlist_del(node);
  2214. mutex_unlock(&cpuhp_state_mutex);
  2215. cpus_read_unlock();
  2216. return 0;
  2217. }
  2218. EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
  2219. /**
  2220. * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
  2221. * @state: The state to remove
  2222. * @invoke: If true, the teardown function is invoked for cpus where
  2223. * cpu state >= @state
  2224. *
  2225. * The caller needs to hold cpus read locked while calling this function.
  2226. * The teardown callback is currently not allowed to fail. Think
  2227. * about module removal!
  2228. */
  2229. void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
  2230. {
  2231. struct cpuhp_step *sp = cpuhp_get_step(state);
  2232. int cpu;
  2233. BUG_ON(cpuhp_cb_check(state));
  2234. lockdep_assert_cpus_held();
  2235. mutex_lock(&cpuhp_state_mutex);
  2236. if (sp->multi_instance) {
  2237. WARN(!hlist_empty(&sp->list),
  2238. "Error: Removing state %d which has instances left.\n",
  2239. state);
  2240. goto remove;
  2241. }
  2242. if (!invoke || !cpuhp_get_teardown_cb(state))
  2243. goto remove;
  2244. /*
  2245. * Call the teardown callback for each present cpu depending
  2246. * on the hotplug state of the cpu. This function is not
  2247. * allowed to fail currently!
  2248. */
  2249. for_each_present_cpu(cpu) {
  2250. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  2251. int cpustate = st->state;
  2252. if (cpustate >= state)
  2253. cpuhp_issue_call(cpu, state, false, NULL);
  2254. }
  2255. remove:
  2256. cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
  2257. mutex_unlock(&cpuhp_state_mutex);
  2258. }
  2259. EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
  2260. void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
  2261. {
  2262. cpus_read_lock();
  2263. __cpuhp_remove_state_cpuslocked(state, invoke);
  2264. cpus_read_unlock();
  2265. }
  2266. EXPORT_SYMBOL(__cpuhp_remove_state);
  2267. #ifdef CONFIG_HOTPLUG_SMT
  2268. static void cpuhp_offline_cpu_device(unsigned int cpu)
  2269. {
  2270. struct device *dev = get_cpu_device(cpu);
  2271. dev->offline = true;
  2272. /* Tell user space about the state change */
  2273. kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  2274. }
  2275. static void cpuhp_online_cpu_device(unsigned int cpu)
  2276. {
  2277. struct device *dev = get_cpu_device(cpu);
  2278. dev->offline = false;
  2279. /* Tell user space about the state change */
  2280. kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  2281. }
  2282. int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
  2283. {
  2284. int cpu, ret = 0;
  2285. cpu_maps_update_begin();
  2286. for_each_online_cpu(cpu) {
  2287. if (topology_is_primary_thread(cpu))
  2288. continue;
  2289. /*
  2290. * Disable can be called with CPU_SMT_ENABLED when changing
  2291. * from a higher to lower number of SMT threads per core.
  2292. */
  2293. if (ctrlval == CPU_SMT_ENABLED && cpu_smt_thread_allowed(cpu))
  2294. continue;
  2295. ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
  2296. if (ret)
  2297. break;
  2298. /*
  2299. * As this needs to hold the cpu maps lock it's impossible
  2300. * to call device_offline() because that ends up calling
  2301. * cpu_down() which takes cpu maps lock. cpu maps lock
  2302. * needs to be held as this might race against in kernel
  2303. * abusers of the hotplug machinery (thermal management).
  2304. *
  2305. * So nothing would update device:offline state. That would
  2306. * leave the sysfs entry stale and prevent onlining after
  2307. * smt control has been changed to 'off' again. This is
  2308. * called under the sysfs hotplug lock, so it is properly
  2309. * serialized against the regular offline usage.
  2310. */
  2311. cpuhp_offline_cpu_device(cpu);
  2312. }
  2313. if (!ret)
  2314. cpu_smt_control = ctrlval;
  2315. cpu_maps_update_done();
  2316. return ret;
  2317. }
  2318. /* Check if the core a CPU belongs to is online */
  2319. #if !defined(topology_is_core_online)
  2320. static inline bool topology_is_core_online(unsigned int cpu)
  2321. {
  2322. return true;
  2323. }
  2324. #endif
  2325. int cpuhp_smt_enable(void)
  2326. {
  2327. int cpu, ret = 0;
  2328. cpu_maps_update_begin();
  2329. cpu_smt_control = CPU_SMT_ENABLED;
  2330. for_each_present_cpu(cpu) {
  2331. /* Skip online CPUs and CPUs on offline nodes */
  2332. if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
  2333. continue;
  2334. if (!cpu_smt_thread_allowed(cpu) || !topology_is_core_online(cpu))
  2335. continue;
  2336. ret = _cpu_up(cpu, 0, CPUHP_ONLINE);
  2337. if (ret)
  2338. break;
  2339. /* See comment in cpuhp_smt_disable() */
  2340. cpuhp_online_cpu_device(cpu);
  2341. }
  2342. cpu_maps_update_done();
  2343. return ret;
  2344. }
  2345. #endif
  2346. #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
  2347. static ssize_t state_show(struct device *dev,
  2348. struct device_attribute *attr, char *buf)
  2349. {
  2350. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  2351. return sprintf(buf, "%d\n", st->state);
  2352. }
  2353. static DEVICE_ATTR_RO(state);
  2354. static ssize_t target_store(struct device *dev, struct device_attribute *attr,
  2355. const char *buf, size_t count)
  2356. {
  2357. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  2358. struct cpuhp_step *sp;
  2359. int target, ret;
  2360. ret = kstrtoint(buf, 10, &target);
  2361. if (ret)
  2362. return ret;
  2363. #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
  2364. if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
  2365. return -EINVAL;
  2366. #else
  2367. if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
  2368. return -EINVAL;
  2369. #endif
  2370. ret = lock_device_hotplug_sysfs();
  2371. if (ret)
  2372. return ret;
  2373. mutex_lock(&cpuhp_state_mutex);
  2374. sp = cpuhp_get_step(target);
  2375. ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
  2376. mutex_unlock(&cpuhp_state_mutex);
  2377. if (ret)
  2378. goto out;
  2379. if (st->state < target)
  2380. ret = cpu_up(dev->id, target);
  2381. else if (st->state > target)
  2382. ret = cpu_down(dev->id, target);
  2383. else if (WARN_ON(st->target != target))
  2384. st->target = target;
  2385. out:
  2386. unlock_device_hotplug();
  2387. return ret ? ret : count;
  2388. }
  2389. static ssize_t target_show(struct device *dev,
  2390. struct device_attribute *attr, char *buf)
  2391. {
  2392. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  2393. return sprintf(buf, "%d\n", st->target);
  2394. }
  2395. static DEVICE_ATTR_RW(target);
  2396. static ssize_t fail_store(struct device *dev, struct device_attribute *attr,
  2397. const char *buf, size_t count)
  2398. {
  2399. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  2400. struct cpuhp_step *sp;
  2401. int fail, ret;
  2402. ret = kstrtoint(buf, 10, &fail);
  2403. if (ret)
  2404. return ret;
  2405. if (fail == CPUHP_INVALID) {
  2406. st->fail = fail;
  2407. return count;
  2408. }
  2409. if (fail < CPUHP_OFFLINE || fail > CPUHP_ONLINE)
  2410. return -EINVAL;
  2411. /*
  2412. * Cannot fail STARTING/DYING callbacks.
  2413. */
  2414. if (cpuhp_is_atomic_state(fail))
  2415. return -EINVAL;
  2416. /*
  2417. * DEAD callbacks cannot fail...
  2418. * ... neither can CPUHP_BRINGUP_CPU during hotunplug. The latter
  2419. * triggering STARTING callbacks, a failure in this state would
  2420. * hinder rollback.
  2421. */
  2422. if (fail <= CPUHP_BRINGUP_CPU && st->state > CPUHP_BRINGUP_CPU)
  2423. return -EINVAL;
  2424. /*
  2425. * Cannot fail anything that doesn't have callbacks.
  2426. */
  2427. mutex_lock(&cpuhp_state_mutex);
  2428. sp = cpuhp_get_step(fail);
  2429. if (!sp->startup.single && !sp->teardown.single)
  2430. ret = -EINVAL;
  2431. mutex_unlock(&cpuhp_state_mutex);
  2432. if (ret)
  2433. return ret;
  2434. st->fail = fail;
  2435. return count;
  2436. }
  2437. static ssize_t fail_show(struct device *dev,
  2438. struct device_attribute *attr, char *buf)
  2439. {
  2440. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  2441. return sprintf(buf, "%d\n", st->fail);
  2442. }
  2443. static DEVICE_ATTR_RW(fail);
  2444. static struct attribute *cpuhp_cpu_attrs[] = {
  2445. &dev_attr_state.attr,
  2446. &dev_attr_target.attr,
  2447. &dev_attr_fail.attr,
  2448. NULL
  2449. };
  2450. static const struct attribute_group cpuhp_cpu_attr_group = {
  2451. .attrs = cpuhp_cpu_attrs,
  2452. .name = "hotplug",
  2453. NULL
  2454. };
  2455. static ssize_t states_show(struct device *dev,
  2456. struct device_attribute *attr, char *buf)
  2457. {
  2458. ssize_t cur, res = 0;
  2459. int i;
  2460. mutex_lock(&cpuhp_state_mutex);
  2461. for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
  2462. struct cpuhp_step *sp = cpuhp_get_step(i);
  2463. if (sp->name) {
  2464. cur = sprintf(buf, "%3d: %s\n", i, sp->name);
  2465. buf += cur;
  2466. res += cur;
  2467. }
  2468. }
  2469. mutex_unlock(&cpuhp_state_mutex);
  2470. return res;
  2471. }
  2472. static DEVICE_ATTR_RO(states);
  2473. static struct attribute *cpuhp_cpu_root_attrs[] = {
  2474. &dev_attr_states.attr,
  2475. NULL
  2476. };
  2477. static const struct attribute_group cpuhp_cpu_root_attr_group = {
  2478. .attrs = cpuhp_cpu_root_attrs,
  2479. .name = "hotplug",
  2480. NULL
  2481. };
  2482. #ifdef CONFIG_HOTPLUG_SMT
  2483. static bool cpu_smt_num_threads_valid(unsigned int threads)
  2484. {
  2485. if (IS_ENABLED(CONFIG_SMT_NUM_THREADS_DYNAMIC))
  2486. return threads >= 1 && threads <= cpu_smt_max_threads;
  2487. return threads == 1 || threads == cpu_smt_max_threads;
  2488. }
  2489. static ssize_t
  2490. __store_smt_control(struct device *dev, struct device_attribute *attr,
  2491. const char *buf, size_t count)
  2492. {
  2493. int ctrlval, ret, num_threads, orig_threads;
  2494. bool force_off;
  2495. if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
  2496. return -EPERM;
  2497. if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
  2498. return -ENODEV;
  2499. if (sysfs_streq(buf, "on")) {
  2500. ctrlval = CPU_SMT_ENABLED;
  2501. num_threads = cpu_smt_max_threads;
  2502. } else if (sysfs_streq(buf, "off")) {
  2503. ctrlval = CPU_SMT_DISABLED;
  2504. num_threads = 1;
  2505. } else if (sysfs_streq(buf, "forceoff")) {
  2506. ctrlval = CPU_SMT_FORCE_DISABLED;
  2507. num_threads = 1;
  2508. } else if (kstrtoint(buf, 10, &num_threads) == 0) {
  2509. if (num_threads == 1)
  2510. ctrlval = CPU_SMT_DISABLED;
  2511. else if (cpu_smt_num_threads_valid(num_threads))
  2512. ctrlval = CPU_SMT_ENABLED;
  2513. else
  2514. return -EINVAL;
  2515. } else {
  2516. return -EINVAL;
  2517. }
  2518. ret = lock_device_hotplug_sysfs();
  2519. if (ret)
  2520. return ret;
  2521. orig_threads = cpu_smt_num_threads;
  2522. cpu_smt_num_threads = num_threads;
  2523. force_off = ctrlval != cpu_smt_control && ctrlval == CPU_SMT_FORCE_DISABLED;
  2524. if (num_threads > orig_threads)
  2525. ret = cpuhp_smt_enable();
  2526. else if (num_threads < orig_threads || force_off)
  2527. ret = cpuhp_smt_disable(ctrlval);
  2528. unlock_device_hotplug();
  2529. return ret ? ret : count;
  2530. }
  2531. #else /* !CONFIG_HOTPLUG_SMT */
  2532. static ssize_t
  2533. __store_smt_control(struct device *dev, struct device_attribute *attr,
  2534. const char *buf, size_t count)
  2535. {
  2536. return -ENODEV;
  2537. }
  2538. #endif /* CONFIG_HOTPLUG_SMT */
  2539. static const char *smt_states[] = {
  2540. [CPU_SMT_ENABLED] = "on",
  2541. [CPU_SMT_DISABLED] = "off",
  2542. [CPU_SMT_FORCE_DISABLED] = "forceoff",
  2543. [CPU_SMT_NOT_SUPPORTED] = "notsupported",
  2544. [CPU_SMT_NOT_IMPLEMENTED] = "notimplemented",
  2545. };
  2546. static ssize_t control_show(struct device *dev,
  2547. struct device_attribute *attr, char *buf)
  2548. {
  2549. const char *state = smt_states[cpu_smt_control];
  2550. #ifdef CONFIG_HOTPLUG_SMT
  2551. /*
  2552. * If SMT is enabled but not all threads are enabled then show the
  2553. * number of threads. If all threads are enabled show "on". Otherwise
  2554. * show the state name.
  2555. */
  2556. if (cpu_smt_control == CPU_SMT_ENABLED &&
  2557. cpu_smt_num_threads != cpu_smt_max_threads)
  2558. return sysfs_emit(buf, "%d\n", cpu_smt_num_threads);
  2559. #endif
  2560. return sysfs_emit(buf, "%s\n", state);
  2561. }
  2562. static ssize_t control_store(struct device *dev, struct device_attribute *attr,
  2563. const char *buf, size_t count)
  2564. {
  2565. return __store_smt_control(dev, attr, buf, count);
  2566. }
  2567. static DEVICE_ATTR_RW(control);
  2568. static ssize_t active_show(struct device *dev,
  2569. struct device_attribute *attr, char *buf)
  2570. {
  2571. return sysfs_emit(buf, "%d\n", sched_smt_active());
  2572. }
  2573. static DEVICE_ATTR_RO(active);
  2574. static struct attribute *cpuhp_smt_attrs[] = {
  2575. &dev_attr_control.attr,
  2576. &dev_attr_active.attr,
  2577. NULL
  2578. };
  2579. static const struct attribute_group cpuhp_smt_attr_group = {
  2580. .attrs = cpuhp_smt_attrs,
  2581. .name = "smt",
  2582. NULL
  2583. };
  2584. static int __init cpu_smt_sysfs_init(void)
  2585. {
  2586. struct device *dev_root;
  2587. int ret = -ENODEV;
  2588. dev_root = bus_get_dev_root(&cpu_subsys);
  2589. if (dev_root) {
  2590. ret = sysfs_create_group(&dev_root->kobj, &cpuhp_smt_attr_group);
  2591. put_device(dev_root);
  2592. }
  2593. return ret;
  2594. }
  2595. static int __init cpuhp_sysfs_init(void)
  2596. {
  2597. struct device *dev_root;
  2598. int cpu, ret;
  2599. ret = cpu_smt_sysfs_init();
  2600. if (ret)
  2601. return ret;
  2602. dev_root = bus_get_dev_root(&cpu_subsys);
  2603. if (dev_root) {
  2604. ret = sysfs_create_group(&dev_root->kobj, &cpuhp_cpu_root_attr_group);
  2605. put_device(dev_root);
  2606. if (ret)
  2607. return ret;
  2608. }
  2609. for_each_possible_cpu(cpu) {
  2610. struct device *dev = get_cpu_device(cpu);
  2611. if (!dev)
  2612. continue;
  2613. ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
  2614. if (ret)
  2615. return ret;
  2616. }
  2617. return 0;
  2618. }
  2619. device_initcall(cpuhp_sysfs_init);
  2620. #endif /* CONFIG_SYSFS && CONFIG_HOTPLUG_CPU */
  2621. /*
  2622. * cpu_bit_bitmap[] is a special, "compressed" data structure that
  2623. * represents all NR_CPUS bits binary values of 1<<nr.
  2624. *
  2625. * It is used by cpumask_of() to get a constant address to a CPU
  2626. * mask value that has a single bit set only.
  2627. */
  2628. /* cpu_bit_bitmap[0] is empty - so we can back into it */
  2629. #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
  2630. #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
  2631. #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
  2632. #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
  2633. const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
  2634. MASK_DECLARE_8(0), MASK_DECLARE_8(8),
  2635. MASK_DECLARE_8(16), MASK_DECLARE_8(24),
  2636. #if BITS_PER_LONG > 32
  2637. MASK_DECLARE_8(32), MASK_DECLARE_8(40),
  2638. MASK_DECLARE_8(48), MASK_DECLARE_8(56),
  2639. #endif
  2640. };
  2641. EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
  2642. const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  2643. EXPORT_SYMBOL(cpu_all_bits);
  2644. #ifdef CONFIG_INIT_ALL_POSSIBLE
  2645. struct cpumask __cpu_possible_mask __ro_after_init
  2646. = {CPU_BITS_ALL};
  2647. #else
  2648. struct cpumask __cpu_possible_mask __ro_after_init;
  2649. #endif
  2650. EXPORT_SYMBOL(__cpu_possible_mask);
  2651. struct cpumask __cpu_online_mask __read_mostly;
  2652. EXPORT_SYMBOL(__cpu_online_mask);
  2653. struct cpumask __cpu_enabled_mask __read_mostly;
  2654. EXPORT_SYMBOL(__cpu_enabled_mask);
  2655. struct cpumask __cpu_present_mask __read_mostly;
  2656. EXPORT_SYMBOL(__cpu_present_mask);
  2657. struct cpumask __cpu_active_mask __read_mostly;
  2658. EXPORT_SYMBOL(__cpu_active_mask);
  2659. struct cpumask __cpu_dying_mask __read_mostly;
  2660. EXPORT_SYMBOL(__cpu_dying_mask);
  2661. atomic_t __num_online_cpus __read_mostly;
  2662. EXPORT_SYMBOL(__num_online_cpus);
  2663. void init_cpu_present(const struct cpumask *src)
  2664. {
  2665. cpumask_copy(&__cpu_present_mask, src);
  2666. }
  2667. void init_cpu_possible(const struct cpumask *src)
  2668. {
  2669. cpumask_copy(&__cpu_possible_mask, src);
  2670. }
  2671. void init_cpu_online(const struct cpumask *src)
  2672. {
  2673. cpumask_copy(&__cpu_online_mask, src);
  2674. }
  2675. void set_cpu_online(unsigned int cpu, bool online)
  2676. {
  2677. /*
  2678. * atomic_inc/dec() is required to handle the horrid abuse of this
  2679. * function by the reboot and kexec code which invoke it from
  2680. * IPI/NMI broadcasts when shutting down CPUs. Invocation from
  2681. * regular CPU hotplug is properly serialized.
  2682. *
  2683. * Note, that the fact that __num_online_cpus is of type atomic_t
  2684. * does not protect readers which are not serialized against
  2685. * concurrent hotplug operations.
  2686. */
  2687. if (online) {
  2688. if (!cpumask_test_and_set_cpu(cpu, &__cpu_online_mask))
  2689. atomic_inc(&__num_online_cpus);
  2690. } else {
  2691. if (cpumask_test_and_clear_cpu(cpu, &__cpu_online_mask))
  2692. atomic_dec(&__num_online_cpus);
  2693. }
  2694. }
  2695. /*
  2696. * Activate the first processor.
  2697. */
  2698. void __init boot_cpu_init(void)
  2699. {
  2700. int cpu = smp_processor_id();
  2701. /* Mark the boot cpu "present", "online" etc for SMP and UP case */
  2702. set_cpu_online(cpu, true);
  2703. set_cpu_active(cpu, true);
  2704. set_cpu_present(cpu, true);
  2705. set_cpu_possible(cpu, true);
  2706. #ifdef CONFIG_SMP
  2707. __boot_cpu_id = cpu;
  2708. #endif
  2709. }
  2710. /*
  2711. * Must be called _AFTER_ setting up the per_cpu areas
  2712. */
  2713. void __init boot_cpu_hotplug_init(void)
  2714. {
  2715. #ifdef CONFIG_SMP
  2716. cpumask_set_cpu(smp_processor_id(), &cpus_booted_once_mask);
  2717. atomic_set(this_cpu_ptr(&cpuhp_state.ap_sync_state), SYNC_STATE_ONLINE);
  2718. #endif
  2719. this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
  2720. this_cpu_write(cpuhp_state.target, CPUHP_ONLINE);
  2721. }
  2722. #ifdef CONFIG_CPU_MITIGATIONS
  2723. /*
  2724. * These are used for a global "mitigations=" cmdline option for toggling
  2725. * optional CPU mitigations.
  2726. */
  2727. enum cpu_mitigations {
  2728. CPU_MITIGATIONS_OFF,
  2729. CPU_MITIGATIONS_AUTO,
  2730. CPU_MITIGATIONS_AUTO_NOSMT,
  2731. };
  2732. static enum cpu_mitigations cpu_mitigations __ro_after_init = CPU_MITIGATIONS_AUTO;
  2733. static int __init mitigations_parse_cmdline(char *arg)
  2734. {
  2735. if (!strcmp(arg, "off"))
  2736. cpu_mitigations = CPU_MITIGATIONS_OFF;
  2737. else if (!strcmp(arg, "auto"))
  2738. cpu_mitigations = CPU_MITIGATIONS_AUTO;
  2739. else if (!strcmp(arg, "auto,nosmt"))
  2740. cpu_mitigations = CPU_MITIGATIONS_AUTO_NOSMT;
  2741. else
  2742. pr_crit("Unsupported mitigations=%s, system may still be vulnerable\n",
  2743. arg);
  2744. return 0;
  2745. }
  2746. /* mitigations=off */
  2747. bool cpu_mitigations_off(void)
  2748. {
  2749. return cpu_mitigations == CPU_MITIGATIONS_OFF;
  2750. }
  2751. EXPORT_SYMBOL_GPL(cpu_mitigations_off);
  2752. /* mitigations=auto,nosmt */
  2753. bool cpu_mitigations_auto_nosmt(void)
  2754. {
  2755. return cpu_mitigations == CPU_MITIGATIONS_AUTO_NOSMT;
  2756. }
  2757. EXPORT_SYMBOL_GPL(cpu_mitigations_auto_nosmt);
  2758. #else
  2759. static int __init mitigations_parse_cmdline(char *arg)
  2760. {
  2761. pr_crit("Kernel compiled without mitigations, ignoring 'mitigations'; system may still be vulnerable\n");
  2762. return 0;
  2763. }
  2764. #endif
  2765. early_param("mitigations", mitigations_parse_cmdline);