cpu.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234
  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. arch_smt_update();
  1229. return ret;
  1230. }
  1231. struct cpu_down_work {
  1232. unsigned int cpu;
  1233. enum cpuhp_state target;
  1234. };
  1235. static long __cpu_down_maps_locked(void *arg)
  1236. {
  1237. struct cpu_down_work *work = arg;
  1238. return _cpu_down(work->cpu, 0, work->target);
  1239. }
  1240. static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
  1241. {
  1242. struct cpu_down_work work = { .cpu = cpu, .target = target, };
  1243. /*
  1244. * If the platform does not support hotplug, report it explicitly to
  1245. * differentiate it from a transient offlining failure.
  1246. */
  1247. if (cpu_hotplug_offline_disabled)
  1248. return -EOPNOTSUPP;
  1249. if (cpu_hotplug_disabled)
  1250. return -EBUSY;
  1251. /*
  1252. * Ensure that the control task does not run on the to be offlined
  1253. * CPU to prevent a deadlock against cfs_b->period_timer.
  1254. * Also keep at least one housekeeping cpu onlined to avoid generating
  1255. * an empty sched_domain span.
  1256. */
  1257. for_each_cpu_and(cpu, cpu_online_mask, housekeeping_cpumask(HK_TYPE_DOMAIN)) {
  1258. if (cpu != work.cpu)
  1259. return work_on_cpu(cpu, __cpu_down_maps_locked, &work);
  1260. }
  1261. return -EBUSY;
  1262. }
  1263. static int cpu_down(unsigned int cpu, enum cpuhp_state target)
  1264. {
  1265. int err;
  1266. cpu_maps_update_begin();
  1267. err = cpu_down_maps_locked(cpu, target);
  1268. cpu_maps_update_done();
  1269. return err;
  1270. }
  1271. /**
  1272. * cpu_device_down - Bring down a cpu device
  1273. * @dev: Pointer to the cpu device to offline
  1274. *
  1275. * This function is meant to be used by device core cpu subsystem only.
  1276. *
  1277. * Other subsystems should use remove_cpu() instead.
  1278. *
  1279. * Return: %0 on success or a negative errno code
  1280. */
  1281. int cpu_device_down(struct device *dev)
  1282. {
  1283. return cpu_down(dev->id, CPUHP_OFFLINE);
  1284. }
  1285. int remove_cpu(unsigned int cpu)
  1286. {
  1287. int ret;
  1288. lock_device_hotplug();
  1289. ret = device_offline(get_cpu_device(cpu));
  1290. unlock_device_hotplug();
  1291. return ret;
  1292. }
  1293. EXPORT_SYMBOL_GPL(remove_cpu);
  1294. void smp_shutdown_nonboot_cpus(unsigned int primary_cpu)
  1295. {
  1296. unsigned int cpu;
  1297. int error;
  1298. cpu_maps_update_begin();
  1299. /*
  1300. * Make certain the cpu I'm about to reboot on is online.
  1301. *
  1302. * This is inline to what migrate_to_reboot_cpu() already do.
  1303. */
  1304. if (!cpu_online(primary_cpu))
  1305. primary_cpu = cpumask_first(cpu_online_mask);
  1306. for_each_online_cpu(cpu) {
  1307. if (cpu == primary_cpu)
  1308. continue;
  1309. error = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
  1310. if (error) {
  1311. pr_err("Failed to offline CPU%d - error=%d",
  1312. cpu, error);
  1313. break;
  1314. }
  1315. }
  1316. /*
  1317. * Ensure all but the reboot CPU are offline.
  1318. */
  1319. BUG_ON(num_online_cpus() > 1);
  1320. /*
  1321. * Make sure the CPUs won't be enabled by someone else after this
  1322. * point. Kexec will reboot to a new kernel shortly resetting
  1323. * everything along the way.
  1324. */
  1325. cpu_hotplug_disabled++;
  1326. cpu_maps_update_done();
  1327. }
  1328. #else
  1329. #define takedown_cpu NULL
  1330. #endif /*CONFIG_HOTPLUG_CPU*/
  1331. /**
  1332. * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
  1333. * @cpu: cpu that just started
  1334. *
  1335. * It must be called by the arch code on the new cpu, before the new cpu
  1336. * enables interrupts and before the "boot" cpu returns from __cpu_up().
  1337. */
  1338. void notify_cpu_starting(unsigned int cpu)
  1339. {
  1340. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1341. enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
  1342. rcutree_report_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
  1343. cpumask_set_cpu(cpu, &cpus_booted_once_mask);
  1344. /*
  1345. * STARTING must not fail!
  1346. */
  1347. cpuhp_invoke_callback_range_nofail(true, cpu, st, target);
  1348. }
  1349. /*
  1350. * Called from the idle task. Wake up the controlling task which brings the
  1351. * hotplug thread of the upcoming CPU up and then delegates the rest of the
  1352. * online bringup to the hotplug thread.
  1353. */
  1354. void cpuhp_online_idle(enum cpuhp_state state)
  1355. {
  1356. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  1357. /* Happens for the boot cpu */
  1358. if (state != CPUHP_AP_ONLINE_IDLE)
  1359. return;
  1360. cpuhp_ap_update_sync_state(SYNC_STATE_ONLINE);
  1361. /*
  1362. * Unpark the stopper thread before we start the idle loop (and start
  1363. * scheduling); this ensures the stopper task is always available.
  1364. */
  1365. stop_machine_unpark(smp_processor_id());
  1366. st->state = CPUHP_AP_ONLINE_IDLE;
  1367. complete_ap_thread(st, true);
  1368. }
  1369. /* Requires cpu_add_remove_lock to be held */
  1370. static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
  1371. {
  1372. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1373. struct task_struct *idle;
  1374. int ret = 0;
  1375. cpus_write_lock();
  1376. if (!cpu_present(cpu)) {
  1377. ret = -EINVAL;
  1378. goto out;
  1379. }
  1380. /*
  1381. * The caller of cpu_up() might have raced with another
  1382. * caller. Nothing to do.
  1383. */
  1384. if (st->state >= target)
  1385. goto out;
  1386. if (st->state == CPUHP_OFFLINE) {
  1387. /* Let it fail before we try to bring the cpu up */
  1388. idle = idle_thread_get(cpu);
  1389. if (IS_ERR(idle)) {
  1390. ret = PTR_ERR(idle);
  1391. goto out;
  1392. }
  1393. /*
  1394. * Reset stale stack state from the last time this CPU was online.
  1395. */
  1396. scs_task_reset(idle);
  1397. kasan_unpoison_task_stack(idle);
  1398. }
  1399. cpuhp_tasks_frozen = tasks_frozen;
  1400. cpuhp_set_state(cpu, st, target);
  1401. /*
  1402. * If the current CPU state is in the range of the AP hotplug thread,
  1403. * then we need to kick the thread once more.
  1404. */
  1405. if (st->state > CPUHP_BRINGUP_CPU) {
  1406. ret = cpuhp_kick_ap_work(cpu);
  1407. /*
  1408. * The AP side has done the error rollback already. Just
  1409. * return the error code..
  1410. */
  1411. if (ret)
  1412. goto out;
  1413. }
  1414. /*
  1415. * Try to reach the target state. We max out on the BP at
  1416. * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
  1417. * responsible for bringing it up to the target state.
  1418. */
  1419. target = min((int)target, CPUHP_BRINGUP_CPU);
  1420. ret = cpuhp_up_callbacks(cpu, st, target);
  1421. out:
  1422. cpus_write_unlock();
  1423. arch_smt_update();
  1424. return ret;
  1425. }
  1426. static int cpu_up(unsigned int cpu, enum cpuhp_state target)
  1427. {
  1428. int err = 0;
  1429. if (!cpu_possible(cpu)) {
  1430. pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
  1431. cpu);
  1432. return -EINVAL;
  1433. }
  1434. err = try_online_node(cpu_to_node(cpu));
  1435. if (err)
  1436. return err;
  1437. cpu_maps_update_begin();
  1438. if (cpu_hotplug_disabled) {
  1439. err = -EBUSY;
  1440. goto out;
  1441. }
  1442. if (!cpu_bootable(cpu)) {
  1443. err = -EPERM;
  1444. goto out;
  1445. }
  1446. err = _cpu_up(cpu, 0, target);
  1447. out:
  1448. cpu_maps_update_done();
  1449. return err;
  1450. }
  1451. /**
  1452. * cpu_device_up - Bring up a cpu device
  1453. * @dev: Pointer to the cpu device to online
  1454. *
  1455. * This function is meant to be used by device core cpu subsystem only.
  1456. *
  1457. * Other subsystems should use add_cpu() instead.
  1458. *
  1459. * Return: %0 on success or a negative errno code
  1460. */
  1461. int cpu_device_up(struct device *dev)
  1462. {
  1463. return cpu_up(dev->id, CPUHP_ONLINE);
  1464. }
  1465. int add_cpu(unsigned int cpu)
  1466. {
  1467. int ret;
  1468. lock_device_hotplug();
  1469. ret = device_online(get_cpu_device(cpu));
  1470. unlock_device_hotplug();
  1471. return ret;
  1472. }
  1473. EXPORT_SYMBOL_GPL(add_cpu);
  1474. /**
  1475. * bringup_hibernate_cpu - Bring up the CPU that we hibernated on
  1476. * @sleep_cpu: The cpu we hibernated on and should be brought up.
  1477. *
  1478. * On some architectures like arm64, we can hibernate on any CPU, but on
  1479. * wake up the CPU we hibernated on might be offline as a side effect of
  1480. * using maxcpus= for example.
  1481. *
  1482. * Return: %0 on success or a negative errno code
  1483. */
  1484. int bringup_hibernate_cpu(unsigned int sleep_cpu)
  1485. {
  1486. int ret;
  1487. if (!cpu_online(sleep_cpu)) {
  1488. pr_info("Hibernated on a CPU that is offline! Bringing CPU up.\n");
  1489. ret = cpu_up(sleep_cpu, CPUHP_ONLINE);
  1490. if (ret) {
  1491. pr_err("Failed to bring hibernate-CPU up!\n");
  1492. return ret;
  1493. }
  1494. }
  1495. return 0;
  1496. }
  1497. static void __init cpuhp_bringup_mask(const struct cpumask *mask, unsigned int ncpus,
  1498. enum cpuhp_state target)
  1499. {
  1500. unsigned int cpu;
  1501. for_each_cpu(cpu, mask) {
  1502. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1503. if (cpu_up(cpu, target) && can_rollback_cpu(st)) {
  1504. /*
  1505. * If this failed then cpu_up() might have only
  1506. * rolled back to CPUHP_BP_KICK_AP for the final
  1507. * online. Clean it up. NOOP if already rolled back.
  1508. */
  1509. WARN_ON(cpuhp_invoke_callback_range(false, cpu, st, CPUHP_OFFLINE));
  1510. }
  1511. if (!--ncpus)
  1512. break;
  1513. }
  1514. }
  1515. #ifdef CONFIG_HOTPLUG_PARALLEL
  1516. static bool __cpuhp_parallel_bringup __ro_after_init = true;
  1517. static int __init parallel_bringup_parse_param(char *arg)
  1518. {
  1519. return kstrtobool(arg, &__cpuhp_parallel_bringup);
  1520. }
  1521. early_param("cpuhp.parallel", parallel_bringup_parse_param);
  1522. #ifdef CONFIG_HOTPLUG_SMT
  1523. static inline bool cpuhp_smt_aware(void)
  1524. {
  1525. return cpu_smt_max_threads > 1;
  1526. }
  1527. static inline const struct cpumask *cpuhp_get_primary_thread_mask(void)
  1528. {
  1529. return cpu_primary_thread_mask;
  1530. }
  1531. #else
  1532. static inline bool cpuhp_smt_aware(void)
  1533. {
  1534. return false;
  1535. }
  1536. static inline const struct cpumask *cpuhp_get_primary_thread_mask(void)
  1537. {
  1538. return cpu_none_mask;
  1539. }
  1540. #endif
  1541. bool __weak arch_cpuhp_init_parallel_bringup(void)
  1542. {
  1543. return true;
  1544. }
  1545. /*
  1546. * On architectures which have enabled parallel bringup this invokes all BP
  1547. * prepare states for each of the to be onlined APs first. The last state
  1548. * sends the startup IPI to the APs. The APs proceed through the low level
  1549. * bringup code in parallel and then wait for the control CPU to release
  1550. * them one by one for the final onlining procedure.
  1551. *
  1552. * This avoids waiting for each AP to respond to the startup IPI in
  1553. * CPUHP_BRINGUP_CPU.
  1554. */
  1555. static bool __init cpuhp_bringup_cpus_parallel(unsigned int ncpus)
  1556. {
  1557. const struct cpumask *mask = cpu_present_mask;
  1558. if (__cpuhp_parallel_bringup)
  1559. __cpuhp_parallel_bringup = arch_cpuhp_init_parallel_bringup();
  1560. if (!__cpuhp_parallel_bringup)
  1561. return false;
  1562. if (cpuhp_smt_aware()) {
  1563. const struct cpumask *pmask = cpuhp_get_primary_thread_mask();
  1564. static struct cpumask tmp_mask __initdata;
  1565. /*
  1566. * X86 requires to prevent that SMT siblings stopped while
  1567. * the primary thread does a microcode update for various
  1568. * reasons. Bring the primary threads up first.
  1569. */
  1570. cpumask_and(&tmp_mask, mask, pmask);
  1571. cpuhp_bringup_mask(&tmp_mask, ncpus, CPUHP_BP_KICK_AP);
  1572. cpuhp_bringup_mask(&tmp_mask, ncpus, CPUHP_ONLINE);
  1573. /* Account for the online CPUs */
  1574. ncpus -= num_online_cpus();
  1575. if (!ncpus)
  1576. return true;
  1577. /* Create the mask for secondary CPUs */
  1578. cpumask_andnot(&tmp_mask, mask, pmask);
  1579. mask = &tmp_mask;
  1580. }
  1581. /* Bring the not-yet started CPUs up */
  1582. cpuhp_bringup_mask(mask, ncpus, CPUHP_BP_KICK_AP);
  1583. cpuhp_bringup_mask(mask, ncpus, CPUHP_ONLINE);
  1584. return true;
  1585. }
  1586. #else
  1587. static inline bool cpuhp_bringup_cpus_parallel(unsigned int ncpus) { return false; }
  1588. #endif /* CONFIG_HOTPLUG_PARALLEL */
  1589. void __init bringup_nonboot_cpus(unsigned int max_cpus)
  1590. {
  1591. if (!max_cpus)
  1592. return;
  1593. /* Try parallel bringup optimization if enabled */
  1594. if (cpuhp_bringup_cpus_parallel(max_cpus))
  1595. return;
  1596. /* Full per CPU serialized bringup */
  1597. cpuhp_bringup_mask(cpu_present_mask, max_cpus, CPUHP_ONLINE);
  1598. }
  1599. #ifdef CONFIG_PM_SLEEP_SMP
  1600. static cpumask_var_t frozen_cpus;
  1601. int freeze_secondary_cpus(int primary)
  1602. {
  1603. int cpu, error = 0;
  1604. cpu_maps_update_begin();
  1605. if (primary == -1) {
  1606. primary = cpumask_first(cpu_online_mask);
  1607. if (!housekeeping_cpu(primary, HK_TYPE_TIMER))
  1608. primary = housekeeping_any_cpu(HK_TYPE_TIMER);
  1609. } else {
  1610. if (!cpu_online(primary))
  1611. primary = cpumask_first(cpu_online_mask);
  1612. }
  1613. /*
  1614. * We take down all of the non-boot CPUs in one shot to avoid races
  1615. * with the userspace trying to use the CPU hotplug at the same time
  1616. */
  1617. cpumask_clear(frozen_cpus);
  1618. pr_info("Disabling non-boot CPUs ...\n");
  1619. for (cpu = nr_cpu_ids - 1; cpu >= 0; cpu--) {
  1620. if (!cpu_online(cpu) || cpu == primary)
  1621. continue;
  1622. if (pm_wakeup_pending()) {
  1623. pr_info("Wakeup pending. Abort CPU freeze\n");
  1624. error = -EBUSY;
  1625. break;
  1626. }
  1627. trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
  1628. error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
  1629. trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
  1630. if (!error)
  1631. cpumask_set_cpu(cpu, frozen_cpus);
  1632. else {
  1633. pr_err("Error taking CPU%d down: %d\n", cpu, error);
  1634. break;
  1635. }
  1636. }
  1637. if (!error)
  1638. BUG_ON(num_online_cpus() > 1);
  1639. else
  1640. pr_err("Non-boot CPUs are not disabled\n");
  1641. /*
  1642. * Make sure the CPUs won't be enabled by someone else. We need to do
  1643. * this even in case of failure as all freeze_secondary_cpus() users are
  1644. * supposed to do thaw_secondary_cpus() on the failure path.
  1645. */
  1646. cpu_hotplug_disabled++;
  1647. cpu_maps_update_done();
  1648. return error;
  1649. }
  1650. void __weak arch_thaw_secondary_cpus_begin(void)
  1651. {
  1652. }
  1653. void __weak arch_thaw_secondary_cpus_end(void)
  1654. {
  1655. }
  1656. void thaw_secondary_cpus(void)
  1657. {
  1658. int cpu, error;
  1659. /* Allow everyone to use the CPU hotplug again */
  1660. cpu_maps_update_begin();
  1661. __cpu_hotplug_enable();
  1662. if (cpumask_empty(frozen_cpus))
  1663. goto out;
  1664. pr_info("Enabling non-boot CPUs ...\n");
  1665. arch_thaw_secondary_cpus_begin();
  1666. for_each_cpu(cpu, frozen_cpus) {
  1667. trace_suspend_resume(TPS("CPU_ON"), cpu, true);
  1668. error = _cpu_up(cpu, 1, CPUHP_ONLINE);
  1669. trace_suspend_resume(TPS("CPU_ON"), cpu, false);
  1670. if (!error) {
  1671. pr_info("CPU%d is up\n", cpu);
  1672. continue;
  1673. }
  1674. pr_warn("Error taking CPU%d up: %d\n", cpu, error);
  1675. }
  1676. arch_thaw_secondary_cpus_end();
  1677. cpumask_clear(frozen_cpus);
  1678. out:
  1679. cpu_maps_update_done();
  1680. }
  1681. static int __init alloc_frozen_cpus(void)
  1682. {
  1683. if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
  1684. return -ENOMEM;
  1685. return 0;
  1686. }
  1687. core_initcall(alloc_frozen_cpus);
  1688. /*
  1689. * When callbacks for CPU hotplug notifications are being executed, we must
  1690. * ensure that the state of the system with respect to the tasks being frozen
  1691. * or not, as reported by the notification, remains unchanged *throughout the
  1692. * duration* of the execution of the callbacks.
  1693. * Hence we need to prevent the freezer from racing with regular CPU hotplug.
  1694. *
  1695. * This synchronization is implemented by mutually excluding regular CPU
  1696. * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
  1697. * Hibernate notifications.
  1698. */
  1699. static int
  1700. cpu_hotplug_pm_callback(struct notifier_block *nb,
  1701. unsigned long action, void *ptr)
  1702. {
  1703. switch (action) {
  1704. case PM_SUSPEND_PREPARE:
  1705. case PM_HIBERNATION_PREPARE:
  1706. cpu_hotplug_disable();
  1707. break;
  1708. case PM_POST_SUSPEND:
  1709. case PM_POST_HIBERNATION:
  1710. cpu_hotplug_enable();
  1711. break;
  1712. default:
  1713. return NOTIFY_DONE;
  1714. }
  1715. return NOTIFY_OK;
  1716. }
  1717. static int __init cpu_hotplug_pm_sync_init(void)
  1718. {
  1719. /*
  1720. * cpu_hotplug_pm_callback has higher priority than x86
  1721. * bsp_pm_callback which depends on cpu_hotplug_pm_callback
  1722. * to disable cpu hotplug to avoid cpu hotplug race.
  1723. */
  1724. pm_notifier(cpu_hotplug_pm_callback, 0);
  1725. return 0;
  1726. }
  1727. core_initcall(cpu_hotplug_pm_sync_init);
  1728. #endif /* CONFIG_PM_SLEEP_SMP */
  1729. int __boot_cpu_id;
  1730. #endif /* CONFIG_SMP */
  1731. /* Boot processor state steps */
  1732. static struct cpuhp_step cpuhp_hp_states[] = {
  1733. [CPUHP_OFFLINE] = {
  1734. .name = "offline",
  1735. .startup.single = NULL,
  1736. .teardown.single = NULL,
  1737. },
  1738. #ifdef CONFIG_SMP
  1739. [CPUHP_CREATE_THREADS]= {
  1740. .name = "threads:prepare",
  1741. .startup.single = smpboot_create_threads,
  1742. .teardown.single = NULL,
  1743. .cant_stop = true,
  1744. },
  1745. [CPUHP_PERF_PREPARE] = {
  1746. .name = "perf:prepare",
  1747. .startup.single = perf_event_init_cpu,
  1748. .teardown.single = perf_event_exit_cpu,
  1749. },
  1750. [CPUHP_RANDOM_PREPARE] = {
  1751. .name = "random:prepare",
  1752. .startup.single = random_prepare_cpu,
  1753. .teardown.single = NULL,
  1754. },
  1755. [CPUHP_WORKQUEUE_PREP] = {
  1756. .name = "workqueue:prepare",
  1757. .startup.single = workqueue_prepare_cpu,
  1758. .teardown.single = NULL,
  1759. },
  1760. [CPUHP_HRTIMERS_PREPARE] = {
  1761. .name = "hrtimers:prepare",
  1762. .startup.single = hrtimers_prepare_cpu,
  1763. .teardown.single = NULL,
  1764. },
  1765. [CPUHP_SMPCFD_PREPARE] = {
  1766. .name = "smpcfd:prepare",
  1767. .startup.single = smpcfd_prepare_cpu,
  1768. .teardown.single = smpcfd_dead_cpu,
  1769. },
  1770. [CPUHP_RELAY_PREPARE] = {
  1771. .name = "relay:prepare",
  1772. .startup.single = relay_prepare_cpu,
  1773. .teardown.single = NULL,
  1774. },
  1775. [CPUHP_RCUTREE_PREP] = {
  1776. .name = "RCU/tree:prepare",
  1777. .startup.single = rcutree_prepare_cpu,
  1778. .teardown.single = rcutree_dead_cpu,
  1779. },
  1780. /*
  1781. * On the tear-down path, timers_dead_cpu() must be invoked
  1782. * before blk_mq_queue_reinit_notify() from notify_dead(),
  1783. * otherwise a RCU stall occurs.
  1784. */
  1785. [CPUHP_TIMERS_PREPARE] = {
  1786. .name = "timers:prepare",
  1787. .startup.single = timers_prepare_cpu,
  1788. .teardown.single = timers_dead_cpu,
  1789. },
  1790. #ifdef CONFIG_HOTPLUG_SPLIT_STARTUP
  1791. /*
  1792. * Kicks the AP alive. AP will wait in cpuhp_ap_sync_alive() until
  1793. * the next step will release it.
  1794. */
  1795. [CPUHP_BP_KICK_AP] = {
  1796. .name = "cpu:kick_ap",
  1797. .startup.single = cpuhp_kick_ap_alive,
  1798. },
  1799. /*
  1800. * Waits for the AP to reach cpuhp_ap_sync_alive() and then
  1801. * releases it for the complete bringup.
  1802. */
  1803. [CPUHP_BRINGUP_CPU] = {
  1804. .name = "cpu:bringup",
  1805. .startup.single = cpuhp_bringup_ap,
  1806. .teardown.single = finish_cpu,
  1807. .cant_stop = true,
  1808. },
  1809. #else
  1810. /*
  1811. * All-in-one CPU bringup state which includes the kick alive.
  1812. */
  1813. [CPUHP_BRINGUP_CPU] = {
  1814. .name = "cpu:bringup",
  1815. .startup.single = bringup_cpu,
  1816. .teardown.single = finish_cpu,
  1817. .cant_stop = true,
  1818. },
  1819. #endif
  1820. /* Final state before CPU kills itself */
  1821. [CPUHP_AP_IDLE_DEAD] = {
  1822. .name = "idle:dead",
  1823. },
  1824. /*
  1825. * Last state before CPU enters the idle loop to die. Transient state
  1826. * for synchronization.
  1827. */
  1828. [CPUHP_AP_OFFLINE] = {
  1829. .name = "ap:offline",
  1830. .cant_stop = true,
  1831. },
  1832. /* First state is scheduler control. Interrupts are disabled */
  1833. [CPUHP_AP_SCHED_STARTING] = {
  1834. .name = "sched:starting",
  1835. .startup.single = sched_cpu_starting,
  1836. .teardown.single = sched_cpu_dying,
  1837. },
  1838. [CPUHP_AP_RCUTREE_DYING] = {
  1839. .name = "RCU/tree:dying",
  1840. .startup.single = NULL,
  1841. .teardown.single = rcutree_dying_cpu,
  1842. },
  1843. [CPUHP_AP_SMPCFD_DYING] = {
  1844. .name = "smpcfd:dying",
  1845. .startup.single = NULL,
  1846. .teardown.single = smpcfd_dying_cpu,
  1847. },
  1848. [CPUHP_AP_HRTIMERS_DYING] = {
  1849. .name = "hrtimers:dying",
  1850. .startup.single = hrtimers_cpu_starting,
  1851. .teardown.single = hrtimers_cpu_dying,
  1852. },
  1853. [CPUHP_AP_TICK_DYING] = {
  1854. .name = "tick:dying",
  1855. .startup.single = NULL,
  1856. .teardown.single = tick_cpu_dying,
  1857. },
  1858. /* Entry state on starting. Interrupts enabled from here on. Transient
  1859. * state for synchronsization */
  1860. [CPUHP_AP_ONLINE] = {
  1861. .name = "ap:online",
  1862. },
  1863. /*
  1864. * Handled on control processor until the plugged processor manages
  1865. * this itself.
  1866. */
  1867. [CPUHP_TEARDOWN_CPU] = {
  1868. .name = "cpu:teardown",
  1869. .startup.single = NULL,
  1870. .teardown.single = takedown_cpu,
  1871. .cant_stop = true,
  1872. },
  1873. [CPUHP_AP_SCHED_WAIT_EMPTY] = {
  1874. .name = "sched:waitempty",
  1875. .startup.single = NULL,
  1876. .teardown.single = sched_cpu_wait_empty,
  1877. },
  1878. /* Handle smpboot threads park/unpark */
  1879. [CPUHP_AP_SMPBOOT_THREADS] = {
  1880. .name = "smpboot/threads:online",
  1881. .startup.single = smpboot_unpark_threads,
  1882. .teardown.single = smpboot_park_threads,
  1883. },
  1884. [CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
  1885. .name = "irq/affinity:online",
  1886. .startup.single = irq_affinity_online_cpu,
  1887. .teardown.single = NULL,
  1888. },
  1889. [CPUHP_AP_PERF_ONLINE] = {
  1890. .name = "perf:online",
  1891. .startup.single = perf_event_init_cpu,
  1892. .teardown.single = perf_event_exit_cpu,
  1893. },
  1894. [CPUHP_AP_WATCHDOG_ONLINE] = {
  1895. .name = "lockup_detector:online",
  1896. .startup.single = lockup_detector_online_cpu,
  1897. .teardown.single = lockup_detector_offline_cpu,
  1898. },
  1899. [CPUHP_AP_WORKQUEUE_ONLINE] = {
  1900. .name = "workqueue:online",
  1901. .startup.single = workqueue_online_cpu,
  1902. .teardown.single = workqueue_offline_cpu,
  1903. },
  1904. [CPUHP_AP_RANDOM_ONLINE] = {
  1905. .name = "random:online",
  1906. .startup.single = random_online_cpu,
  1907. .teardown.single = NULL,
  1908. },
  1909. [CPUHP_AP_RCUTREE_ONLINE] = {
  1910. .name = "RCU/tree:online",
  1911. .startup.single = rcutree_online_cpu,
  1912. .teardown.single = rcutree_offline_cpu,
  1913. },
  1914. #endif
  1915. /*
  1916. * The dynamically registered state space is here
  1917. */
  1918. #ifdef CONFIG_SMP
  1919. /* Last state is scheduler control setting the cpu active */
  1920. [CPUHP_AP_ACTIVE] = {
  1921. .name = "sched:active",
  1922. .startup.single = sched_cpu_activate,
  1923. .teardown.single = sched_cpu_deactivate,
  1924. },
  1925. #endif
  1926. /* CPU is fully up and running. */
  1927. [CPUHP_ONLINE] = {
  1928. .name = "online",
  1929. .startup.single = NULL,
  1930. .teardown.single = NULL,
  1931. },
  1932. };
  1933. /* Sanity check for callbacks */
  1934. static int cpuhp_cb_check(enum cpuhp_state state)
  1935. {
  1936. if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
  1937. return -EINVAL;
  1938. return 0;
  1939. }
  1940. /*
  1941. * Returns a free for dynamic slot assignment of the Online state. The states
  1942. * are protected by the cpuhp_slot_states mutex and an empty slot is identified
  1943. * by having no name assigned.
  1944. */
  1945. static int cpuhp_reserve_state(enum cpuhp_state state)
  1946. {
  1947. enum cpuhp_state i, end;
  1948. struct cpuhp_step *step;
  1949. switch (state) {
  1950. case CPUHP_AP_ONLINE_DYN:
  1951. step = cpuhp_hp_states + CPUHP_AP_ONLINE_DYN;
  1952. end = CPUHP_AP_ONLINE_DYN_END;
  1953. break;
  1954. case CPUHP_BP_PREPARE_DYN:
  1955. step = cpuhp_hp_states + CPUHP_BP_PREPARE_DYN;
  1956. end = CPUHP_BP_PREPARE_DYN_END;
  1957. break;
  1958. default:
  1959. return -EINVAL;
  1960. }
  1961. for (i = state; i <= end; i++, step++) {
  1962. if (!step->name)
  1963. return i;
  1964. }
  1965. WARN(1, "No more dynamic states available for CPU hotplug\n");
  1966. return -ENOSPC;
  1967. }
  1968. static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
  1969. int (*startup)(unsigned int cpu),
  1970. int (*teardown)(unsigned int cpu),
  1971. bool multi_instance)
  1972. {
  1973. /* (Un)Install the callbacks for further cpu hotplug operations */
  1974. struct cpuhp_step *sp;
  1975. int ret = 0;
  1976. /*
  1977. * If name is NULL, then the state gets removed.
  1978. *
  1979. * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
  1980. * the first allocation from these dynamic ranges, so the removal
  1981. * would trigger a new allocation and clear the wrong (already
  1982. * empty) state, leaving the callbacks of the to be cleared state
  1983. * dangling, which causes wreckage on the next hotplug operation.
  1984. */
  1985. if (name && (state == CPUHP_AP_ONLINE_DYN ||
  1986. state == CPUHP_BP_PREPARE_DYN)) {
  1987. ret = cpuhp_reserve_state(state);
  1988. if (ret < 0)
  1989. return ret;
  1990. state = ret;
  1991. }
  1992. sp = cpuhp_get_step(state);
  1993. if (name && sp->name)
  1994. return -EBUSY;
  1995. sp->startup.single = startup;
  1996. sp->teardown.single = teardown;
  1997. sp->name = name;
  1998. sp->multi_instance = multi_instance;
  1999. INIT_HLIST_HEAD(&sp->list);
  2000. return ret;
  2001. }
  2002. static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
  2003. {
  2004. return cpuhp_get_step(state)->teardown.single;
  2005. }
  2006. /*
  2007. * Call the startup/teardown function for a step either on the AP or
  2008. * on the current CPU.
  2009. */
  2010. static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
  2011. struct hlist_node *node)
  2012. {
  2013. struct cpuhp_step *sp = cpuhp_get_step(state);
  2014. int ret;
  2015. /*
  2016. * If there's nothing to do, we done.
  2017. * Relies on the union for multi_instance.
  2018. */
  2019. if (cpuhp_step_empty(bringup, sp))
  2020. return 0;
  2021. /*
  2022. * The non AP bound callbacks can fail on bringup. On teardown
  2023. * e.g. module removal we crash for now.
  2024. */
  2025. #ifdef CONFIG_SMP
  2026. if (cpuhp_is_ap_state(state))
  2027. ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
  2028. else
  2029. ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
  2030. #else
  2031. ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
  2032. #endif
  2033. BUG_ON(ret && !bringup);
  2034. return ret;
  2035. }
  2036. /*
  2037. * Called from __cpuhp_setup_state on a recoverable failure.
  2038. *
  2039. * Note: The teardown callbacks for rollback are not allowed to fail!
  2040. */
  2041. static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
  2042. struct hlist_node *node)
  2043. {
  2044. int cpu;
  2045. /* Roll back the already executed steps on the other cpus */
  2046. for_each_present_cpu(cpu) {
  2047. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  2048. int cpustate = st->state;
  2049. if (cpu >= failedcpu)
  2050. break;
  2051. /* Did we invoke the startup call on that cpu ? */
  2052. if (cpustate >= state)
  2053. cpuhp_issue_call(cpu, state, false, node);
  2054. }
  2055. }
  2056. int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
  2057. struct hlist_node *node,
  2058. bool invoke)
  2059. {
  2060. struct cpuhp_step *sp;
  2061. int cpu;
  2062. int ret;
  2063. lockdep_assert_cpus_held();
  2064. sp = cpuhp_get_step(state);
  2065. if (sp->multi_instance == false)
  2066. return -EINVAL;
  2067. mutex_lock(&cpuhp_state_mutex);
  2068. if (!invoke || !sp->startup.multi)
  2069. goto add_node;
  2070. /*
  2071. * Try to call the startup callback for each present cpu
  2072. * depending on the hotplug state of the cpu.
  2073. */
  2074. for_each_present_cpu(cpu) {
  2075. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  2076. int cpustate = st->state;
  2077. if (cpustate < state)
  2078. continue;
  2079. ret = cpuhp_issue_call(cpu, state, true, node);
  2080. if (ret) {
  2081. if (sp->teardown.multi)
  2082. cpuhp_rollback_install(cpu, state, node);
  2083. goto unlock;
  2084. }
  2085. }
  2086. add_node:
  2087. ret = 0;
  2088. hlist_add_head(node, &sp->list);
  2089. unlock:
  2090. mutex_unlock(&cpuhp_state_mutex);
  2091. return ret;
  2092. }
  2093. int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
  2094. bool invoke)
  2095. {
  2096. int ret;
  2097. cpus_read_lock();
  2098. ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
  2099. cpus_read_unlock();
  2100. return ret;
  2101. }
  2102. EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
  2103. /**
  2104. * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
  2105. * @state: The state to setup
  2106. * @name: Name of the step
  2107. * @invoke: If true, the startup function is invoked for cpus where
  2108. * cpu state >= @state
  2109. * @startup: startup callback function
  2110. * @teardown: teardown callback function
  2111. * @multi_instance: State is set up for multiple instances which get
  2112. * added afterwards.
  2113. *
  2114. * The caller needs to hold cpus read locked while calling this function.
  2115. * Return:
  2116. * On success:
  2117. * Positive state number if @state is CPUHP_AP_ONLINE_DYN or CPUHP_BP_PREPARE_DYN;
  2118. * 0 for all other states
  2119. * On failure: proper (negative) error code
  2120. */
  2121. int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state,
  2122. const char *name, bool invoke,
  2123. int (*startup)(unsigned int cpu),
  2124. int (*teardown)(unsigned int cpu),
  2125. bool multi_instance)
  2126. {
  2127. int cpu, ret = 0;
  2128. bool dynstate;
  2129. lockdep_assert_cpus_held();
  2130. if (cpuhp_cb_check(state) || !name)
  2131. return -EINVAL;
  2132. mutex_lock(&cpuhp_state_mutex);
  2133. ret = cpuhp_store_callbacks(state, name, startup, teardown,
  2134. multi_instance);
  2135. dynstate = state == CPUHP_AP_ONLINE_DYN || state == CPUHP_BP_PREPARE_DYN;
  2136. if (ret > 0 && dynstate) {
  2137. state = ret;
  2138. ret = 0;
  2139. }
  2140. if (ret || !invoke || !startup)
  2141. goto out;
  2142. /*
  2143. * Try to call the startup callback for each present cpu
  2144. * depending on the hotplug state of the cpu.
  2145. */
  2146. for_each_present_cpu(cpu) {
  2147. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  2148. int cpustate = st->state;
  2149. if (cpustate < state)
  2150. continue;
  2151. ret = cpuhp_issue_call(cpu, state, true, NULL);
  2152. if (ret) {
  2153. if (teardown)
  2154. cpuhp_rollback_install(cpu, state, NULL);
  2155. cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
  2156. goto out;
  2157. }
  2158. }
  2159. out:
  2160. mutex_unlock(&cpuhp_state_mutex);
  2161. /*
  2162. * If the requested state is CPUHP_AP_ONLINE_DYN or CPUHP_BP_PREPARE_DYN,
  2163. * return the dynamically allocated state in case of success.
  2164. */
  2165. if (!ret && dynstate)
  2166. return state;
  2167. return ret;
  2168. }
  2169. EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
  2170. int __cpuhp_setup_state(enum cpuhp_state state,
  2171. const char *name, bool invoke,
  2172. int (*startup)(unsigned int cpu),
  2173. int (*teardown)(unsigned int cpu),
  2174. bool multi_instance)
  2175. {
  2176. int ret;
  2177. cpus_read_lock();
  2178. ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
  2179. teardown, multi_instance);
  2180. cpus_read_unlock();
  2181. return ret;
  2182. }
  2183. EXPORT_SYMBOL(__cpuhp_setup_state);
  2184. int __cpuhp_state_remove_instance(enum cpuhp_state state,
  2185. struct hlist_node *node, bool invoke)
  2186. {
  2187. struct cpuhp_step *sp = cpuhp_get_step(state);
  2188. int cpu;
  2189. BUG_ON(cpuhp_cb_check(state));
  2190. if (!sp->multi_instance)
  2191. return -EINVAL;
  2192. cpus_read_lock();
  2193. mutex_lock(&cpuhp_state_mutex);
  2194. if (!invoke || !cpuhp_get_teardown_cb(state))
  2195. goto remove;
  2196. /*
  2197. * Call the teardown callback for each present cpu depending
  2198. * on the hotplug state of the cpu. This function is not
  2199. * allowed to fail currently!
  2200. */
  2201. for_each_present_cpu(cpu) {
  2202. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  2203. int cpustate = st->state;
  2204. if (cpustate >= state)
  2205. cpuhp_issue_call(cpu, state, false, node);
  2206. }
  2207. remove:
  2208. hlist_del(node);
  2209. mutex_unlock(&cpuhp_state_mutex);
  2210. cpus_read_unlock();
  2211. return 0;
  2212. }
  2213. EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
  2214. /**
  2215. * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
  2216. * @state: The state to remove
  2217. * @invoke: If true, the teardown function is invoked for cpus where
  2218. * cpu state >= @state
  2219. *
  2220. * The caller needs to hold cpus read locked while calling this function.
  2221. * The teardown callback is currently not allowed to fail. Think
  2222. * about module removal!
  2223. */
  2224. void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
  2225. {
  2226. struct cpuhp_step *sp = cpuhp_get_step(state);
  2227. int cpu;
  2228. BUG_ON(cpuhp_cb_check(state));
  2229. lockdep_assert_cpus_held();
  2230. mutex_lock(&cpuhp_state_mutex);
  2231. if (sp->multi_instance) {
  2232. WARN(!hlist_empty(&sp->list),
  2233. "Error: Removing state %d which has instances left.\n",
  2234. state);
  2235. goto remove;
  2236. }
  2237. if (!invoke || !cpuhp_get_teardown_cb(state))
  2238. goto remove;
  2239. /*
  2240. * Call the teardown callback for each present cpu depending
  2241. * on the hotplug state of the cpu. This function is not
  2242. * allowed to fail currently!
  2243. */
  2244. for_each_present_cpu(cpu) {
  2245. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  2246. int cpustate = st->state;
  2247. if (cpustate >= state)
  2248. cpuhp_issue_call(cpu, state, false, NULL);
  2249. }
  2250. remove:
  2251. cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
  2252. mutex_unlock(&cpuhp_state_mutex);
  2253. }
  2254. EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
  2255. void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
  2256. {
  2257. cpus_read_lock();
  2258. __cpuhp_remove_state_cpuslocked(state, invoke);
  2259. cpus_read_unlock();
  2260. }
  2261. EXPORT_SYMBOL(__cpuhp_remove_state);
  2262. #ifdef CONFIG_HOTPLUG_SMT
  2263. static void cpuhp_offline_cpu_device(unsigned int cpu)
  2264. {
  2265. struct device *dev = get_cpu_device(cpu);
  2266. dev->offline = true;
  2267. /* Tell user space about the state change */
  2268. kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  2269. }
  2270. static void cpuhp_online_cpu_device(unsigned int cpu)
  2271. {
  2272. struct device *dev = get_cpu_device(cpu);
  2273. dev->offline = false;
  2274. /* Tell user space about the state change */
  2275. kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  2276. }
  2277. int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
  2278. {
  2279. int cpu, ret = 0;
  2280. cpu_maps_update_begin();
  2281. for_each_online_cpu(cpu) {
  2282. if (topology_is_primary_thread(cpu))
  2283. continue;
  2284. /*
  2285. * Disable can be called with CPU_SMT_ENABLED when changing
  2286. * from a higher to lower number of SMT threads per core.
  2287. */
  2288. if (ctrlval == CPU_SMT_ENABLED && cpu_smt_thread_allowed(cpu))
  2289. continue;
  2290. ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
  2291. if (ret)
  2292. break;
  2293. /*
  2294. * As this needs to hold the cpu maps lock it's impossible
  2295. * to call device_offline() because that ends up calling
  2296. * cpu_down() which takes cpu maps lock. cpu maps lock
  2297. * needs to be held as this might race against in kernel
  2298. * abusers of the hotplug machinery (thermal management).
  2299. *
  2300. * So nothing would update device:offline state. That would
  2301. * leave the sysfs entry stale and prevent onlining after
  2302. * smt control has been changed to 'off' again. This is
  2303. * called under the sysfs hotplug lock, so it is properly
  2304. * serialized against the regular offline usage.
  2305. */
  2306. cpuhp_offline_cpu_device(cpu);
  2307. }
  2308. if (!ret)
  2309. cpu_smt_control = ctrlval;
  2310. cpu_maps_update_done();
  2311. return ret;
  2312. }
  2313. /* Check if the core a CPU belongs to is online */
  2314. #if !defined(topology_is_core_online)
  2315. static inline bool topology_is_core_online(unsigned int cpu)
  2316. {
  2317. return true;
  2318. }
  2319. #endif
  2320. int cpuhp_smt_enable(void)
  2321. {
  2322. int cpu, ret = 0;
  2323. cpu_maps_update_begin();
  2324. cpu_smt_control = CPU_SMT_ENABLED;
  2325. for_each_present_cpu(cpu) {
  2326. /* Skip online CPUs and CPUs on offline nodes */
  2327. if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
  2328. continue;
  2329. if (!cpu_smt_thread_allowed(cpu) || !topology_is_core_online(cpu))
  2330. continue;
  2331. ret = _cpu_up(cpu, 0, CPUHP_ONLINE);
  2332. if (ret)
  2333. break;
  2334. /* See comment in cpuhp_smt_disable() */
  2335. cpuhp_online_cpu_device(cpu);
  2336. }
  2337. cpu_maps_update_done();
  2338. return ret;
  2339. }
  2340. #endif
  2341. #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
  2342. static ssize_t state_show(struct device *dev,
  2343. struct device_attribute *attr, char *buf)
  2344. {
  2345. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  2346. return sprintf(buf, "%d\n", st->state);
  2347. }
  2348. static DEVICE_ATTR_RO(state);
  2349. static ssize_t target_store(struct device *dev, struct device_attribute *attr,
  2350. const char *buf, size_t count)
  2351. {
  2352. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  2353. struct cpuhp_step *sp;
  2354. int target, ret;
  2355. ret = kstrtoint(buf, 10, &target);
  2356. if (ret)
  2357. return ret;
  2358. #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
  2359. if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
  2360. return -EINVAL;
  2361. #else
  2362. if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
  2363. return -EINVAL;
  2364. #endif
  2365. ret = lock_device_hotplug_sysfs();
  2366. if (ret)
  2367. return ret;
  2368. mutex_lock(&cpuhp_state_mutex);
  2369. sp = cpuhp_get_step(target);
  2370. ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
  2371. mutex_unlock(&cpuhp_state_mutex);
  2372. if (ret)
  2373. goto out;
  2374. if (st->state < target)
  2375. ret = cpu_up(dev->id, target);
  2376. else if (st->state > target)
  2377. ret = cpu_down(dev->id, target);
  2378. else if (WARN_ON(st->target != target))
  2379. st->target = target;
  2380. out:
  2381. unlock_device_hotplug();
  2382. return ret ? ret : count;
  2383. }
  2384. static ssize_t target_show(struct device *dev,
  2385. struct device_attribute *attr, char *buf)
  2386. {
  2387. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  2388. return sprintf(buf, "%d\n", st->target);
  2389. }
  2390. static DEVICE_ATTR_RW(target);
  2391. static ssize_t fail_store(struct device *dev, struct device_attribute *attr,
  2392. const char *buf, size_t count)
  2393. {
  2394. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  2395. struct cpuhp_step *sp;
  2396. int fail, ret;
  2397. ret = kstrtoint(buf, 10, &fail);
  2398. if (ret)
  2399. return ret;
  2400. if (fail == CPUHP_INVALID) {
  2401. st->fail = fail;
  2402. return count;
  2403. }
  2404. if (fail < CPUHP_OFFLINE || fail > CPUHP_ONLINE)
  2405. return -EINVAL;
  2406. /*
  2407. * Cannot fail STARTING/DYING callbacks.
  2408. */
  2409. if (cpuhp_is_atomic_state(fail))
  2410. return -EINVAL;
  2411. /*
  2412. * DEAD callbacks cannot fail...
  2413. * ... neither can CPUHP_BRINGUP_CPU during hotunplug. The latter
  2414. * triggering STARTING callbacks, a failure in this state would
  2415. * hinder rollback.
  2416. */
  2417. if (fail <= CPUHP_BRINGUP_CPU && st->state > CPUHP_BRINGUP_CPU)
  2418. return -EINVAL;
  2419. /*
  2420. * Cannot fail anything that doesn't have callbacks.
  2421. */
  2422. mutex_lock(&cpuhp_state_mutex);
  2423. sp = cpuhp_get_step(fail);
  2424. if (!sp->startup.single && !sp->teardown.single)
  2425. ret = -EINVAL;
  2426. mutex_unlock(&cpuhp_state_mutex);
  2427. if (ret)
  2428. return ret;
  2429. st->fail = fail;
  2430. return count;
  2431. }
  2432. static ssize_t fail_show(struct device *dev,
  2433. struct device_attribute *attr, char *buf)
  2434. {
  2435. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  2436. return sprintf(buf, "%d\n", st->fail);
  2437. }
  2438. static DEVICE_ATTR_RW(fail);
  2439. static struct attribute *cpuhp_cpu_attrs[] = {
  2440. &dev_attr_state.attr,
  2441. &dev_attr_target.attr,
  2442. &dev_attr_fail.attr,
  2443. NULL
  2444. };
  2445. static const struct attribute_group cpuhp_cpu_attr_group = {
  2446. .attrs = cpuhp_cpu_attrs,
  2447. .name = "hotplug",
  2448. NULL
  2449. };
  2450. static ssize_t states_show(struct device *dev,
  2451. struct device_attribute *attr, char *buf)
  2452. {
  2453. ssize_t cur, res = 0;
  2454. int i;
  2455. mutex_lock(&cpuhp_state_mutex);
  2456. for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
  2457. struct cpuhp_step *sp = cpuhp_get_step(i);
  2458. if (sp->name) {
  2459. cur = sprintf(buf, "%3d: %s\n", i, sp->name);
  2460. buf += cur;
  2461. res += cur;
  2462. }
  2463. }
  2464. mutex_unlock(&cpuhp_state_mutex);
  2465. return res;
  2466. }
  2467. static DEVICE_ATTR_RO(states);
  2468. static struct attribute *cpuhp_cpu_root_attrs[] = {
  2469. &dev_attr_states.attr,
  2470. NULL
  2471. };
  2472. static const struct attribute_group cpuhp_cpu_root_attr_group = {
  2473. .attrs = cpuhp_cpu_root_attrs,
  2474. .name = "hotplug",
  2475. NULL
  2476. };
  2477. #ifdef CONFIG_HOTPLUG_SMT
  2478. static bool cpu_smt_num_threads_valid(unsigned int threads)
  2479. {
  2480. if (IS_ENABLED(CONFIG_SMT_NUM_THREADS_DYNAMIC))
  2481. return threads >= 1 && threads <= cpu_smt_max_threads;
  2482. return threads == 1 || threads == cpu_smt_max_threads;
  2483. }
  2484. static ssize_t
  2485. __store_smt_control(struct device *dev, struct device_attribute *attr,
  2486. const char *buf, size_t count)
  2487. {
  2488. int ctrlval, ret, num_threads, orig_threads;
  2489. bool force_off;
  2490. if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
  2491. return -EPERM;
  2492. if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
  2493. return -ENODEV;
  2494. if (sysfs_streq(buf, "on")) {
  2495. ctrlval = CPU_SMT_ENABLED;
  2496. num_threads = cpu_smt_max_threads;
  2497. } else if (sysfs_streq(buf, "off")) {
  2498. ctrlval = CPU_SMT_DISABLED;
  2499. num_threads = 1;
  2500. } else if (sysfs_streq(buf, "forceoff")) {
  2501. ctrlval = CPU_SMT_FORCE_DISABLED;
  2502. num_threads = 1;
  2503. } else if (kstrtoint(buf, 10, &num_threads) == 0) {
  2504. if (num_threads == 1)
  2505. ctrlval = CPU_SMT_DISABLED;
  2506. else if (cpu_smt_num_threads_valid(num_threads))
  2507. ctrlval = CPU_SMT_ENABLED;
  2508. else
  2509. return -EINVAL;
  2510. } else {
  2511. return -EINVAL;
  2512. }
  2513. ret = lock_device_hotplug_sysfs();
  2514. if (ret)
  2515. return ret;
  2516. orig_threads = cpu_smt_num_threads;
  2517. cpu_smt_num_threads = num_threads;
  2518. force_off = ctrlval != cpu_smt_control && ctrlval == CPU_SMT_FORCE_DISABLED;
  2519. if (num_threads > orig_threads)
  2520. ret = cpuhp_smt_enable();
  2521. else if (num_threads < orig_threads || force_off)
  2522. ret = cpuhp_smt_disable(ctrlval);
  2523. unlock_device_hotplug();
  2524. return ret ? ret : count;
  2525. }
  2526. #else /* !CONFIG_HOTPLUG_SMT */
  2527. static ssize_t
  2528. __store_smt_control(struct device *dev, struct device_attribute *attr,
  2529. const char *buf, size_t count)
  2530. {
  2531. return -ENODEV;
  2532. }
  2533. #endif /* CONFIG_HOTPLUG_SMT */
  2534. static const char *smt_states[] = {
  2535. [CPU_SMT_ENABLED] = "on",
  2536. [CPU_SMT_DISABLED] = "off",
  2537. [CPU_SMT_FORCE_DISABLED] = "forceoff",
  2538. [CPU_SMT_NOT_SUPPORTED] = "notsupported",
  2539. [CPU_SMT_NOT_IMPLEMENTED] = "notimplemented",
  2540. };
  2541. static ssize_t control_show(struct device *dev,
  2542. struct device_attribute *attr, char *buf)
  2543. {
  2544. const char *state = smt_states[cpu_smt_control];
  2545. #ifdef CONFIG_HOTPLUG_SMT
  2546. /*
  2547. * If SMT is enabled but not all threads are enabled then show the
  2548. * number of threads. If all threads are enabled show "on". Otherwise
  2549. * show the state name.
  2550. */
  2551. if (cpu_smt_control == CPU_SMT_ENABLED &&
  2552. cpu_smt_num_threads != cpu_smt_max_threads)
  2553. return sysfs_emit(buf, "%d\n", cpu_smt_num_threads);
  2554. #endif
  2555. return sysfs_emit(buf, "%s\n", state);
  2556. }
  2557. static ssize_t control_store(struct device *dev, struct device_attribute *attr,
  2558. const char *buf, size_t count)
  2559. {
  2560. return __store_smt_control(dev, attr, buf, count);
  2561. }
  2562. static DEVICE_ATTR_RW(control);
  2563. static ssize_t active_show(struct device *dev,
  2564. struct device_attribute *attr, char *buf)
  2565. {
  2566. return sysfs_emit(buf, "%d\n", sched_smt_active());
  2567. }
  2568. static DEVICE_ATTR_RO(active);
  2569. static struct attribute *cpuhp_smt_attrs[] = {
  2570. &dev_attr_control.attr,
  2571. &dev_attr_active.attr,
  2572. NULL
  2573. };
  2574. static const struct attribute_group cpuhp_smt_attr_group = {
  2575. .attrs = cpuhp_smt_attrs,
  2576. .name = "smt",
  2577. NULL
  2578. };
  2579. static int __init cpu_smt_sysfs_init(void)
  2580. {
  2581. struct device *dev_root;
  2582. int ret = -ENODEV;
  2583. dev_root = bus_get_dev_root(&cpu_subsys);
  2584. if (dev_root) {
  2585. ret = sysfs_create_group(&dev_root->kobj, &cpuhp_smt_attr_group);
  2586. put_device(dev_root);
  2587. }
  2588. return ret;
  2589. }
  2590. static int __init cpuhp_sysfs_init(void)
  2591. {
  2592. struct device *dev_root;
  2593. int cpu, ret;
  2594. ret = cpu_smt_sysfs_init();
  2595. if (ret)
  2596. return ret;
  2597. dev_root = bus_get_dev_root(&cpu_subsys);
  2598. if (dev_root) {
  2599. ret = sysfs_create_group(&dev_root->kobj, &cpuhp_cpu_root_attr_group);
  2600. put_device(dev_root);
  2601. if (ret)
  2602. return ret;
  2603. }
  2604. for_each_possible_cpu(cpu) {
  2605. struct device *dev = get_cpu_device(cpu);
  2606. if (!dev)
  2607. continue;
  2608. ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
  2609. if (ret)
  2610. return ret;
  2611. }
  2612. return 0;
  2613. }
  2614. device_initcall(cpuhp_sysfs_init);
  2615. #endif /* CONFIG_SYSFS && CONFIG_HOTPLUG_CPU */
  2616. /*
  2617. * cpu_bit_bitmap[] is a special, "compressed" data structure that
  2618. * represents all NR_CPUS bits binary values of 1<<nr.
  2619. *
  2620. * It is used by cpumask_of() to get a constant address to a CPU
  2621. * mask value that has a single bit set only.
  2622. */
  2623. /* cpu_bit_bitmap[0] is empty - so we can back into it */
  2624. #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
  2625. #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
  2626. #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
  2627. #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
  2628. const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
  2629. MASK_DECLARE_8(0), MASK_DECLARE_8(8),
  2630. MASK_DECLARE_8(16), MASK_DECLARE_8(24),
  2631. #if BITS_PER_LONG > 32
  2632. MASK_DECLARE_8(32), MASK_DECLARE_8(40),
  2633. MASK_DECLARE_8(48), MASK_DECLARE_8(56),
  2634. #endif
  2635. };
  2636. EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
  2637. const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  2638. EXPORT_SYMBOL(cpu_all_bits);
  2639. #ifdef CONFIG_INIT_ALL_POSSIBLE
  2640. struct cpumask __cpu_possible_mask __ro_after_init
  2641. = {CPU_BITS_ALL};
  2642. #else
  2643. struct cpumask __cpu_possible_mask __ro_after_init;
  2644. #endif
  2645. EXPORT_SYMBOL(__cpu_possible_mask);
  2646. struct cpumask __cpu_online_mask __read_mostly;
  2647. EXPORT_SYMBOL(__cpu_online_mask);
  2648. struct cpumask __cpu_enabled_mask __read_mostly;
  2649. EXPORT_SYMBOL(__cpu_enabled_mask);
  2650. struct cpumask __cpu_present_mask __read_mostly;
  2651. EXPORT_SYMBOL(__cpu_present_mask);
  2652. struct cpumask __cpu_active_mask __read_mostly;
  2653. EXPORT_SYMBOL(__cpu_active_mask);
  2654. struct cpumask __cpu_dying_mask __read_mostly;
  2655. EXPORT_SYMBOL(__cpu_dying_mask);
  2656. atomic_t __num_online_cpus __read_mostly;
  2657. EXPORT_SYMBOL(__num_online_cpus);
  2658. void init_cpu_present(const struct cpumask *src)
  2659. {
  2660. cpumask_copy(&__cpu_present_mask, src);
  2661. }
  2662. void init_cpu_possible(const struct cpumask *src)
  2663. {
  2664. cpumask_copy(&__cpu_possible_mask, src);
  2665. }
  2666. void init_cpu_online(const struct cpumask *src)
  2667. {
  2668. cpumask_copy(&__cpu_online_mask, src);
  2669. }
  2670. void set_cpu_online(unsigned int cpu, bool online)
  2671. {
  2672. /*
  2673. * atomic_inc/dec() is required to handle the horrid abuse of this
  2674. * function by the reboot and kexec code which invoke it from
  2675. * IPI/NMI broadcasts when shutting down CPUs. Invocation from
  2676. * regular CPU hotplug is properly serialized.
  2677. *
  2678. * Note, that the fact that __num_online_cpus is of type atomic_t
  2679. * does not protect readers which are not serialized against
  2680. * concurrent hotplug operations.
  2681. */
  2682. if (online) {
  2683. if (!cpumask_test_and_set_cpu(cpu, &__cpu_online_mask))
  2684. atomic_inc(&__num_online_cpus);
  2685. } else {
  2686. if (cpumask_test_and_clear_cpu(cpu, &__cpu_online_mask))
  2687. atomic_dec(&__num_online_cpus);
  2688. }
  2689. }
  2690. /*
  2691. * Activate the first processor.
  2692. */
  2693. void __init boot_cpu_init(void)
  2694. {
  2695. int cpu = smp_processor_id();
  2696. /* Mark the boot cpu "present", "online" etc for SMP and UP case */
  2697. set_cpu_online(cpu, true);
  2698. set_cpu_active(cpu, true);
  2699. set_cpu_present(cpu, true);
  2700. set_cpu_possible(cpu, true);
  2701. #ifdef CONFIG_SMP
  2702. __boot_cpu_id = cpu;
  2703. #endif
  2704. }
  2705. /*
  2706. * Must be called _AFTER_ setting up the per_cpu areas
  2707. */
  2708. void __init boot_cpu_hotplug_init(void)
  2709. {
  2710. #ifdef CONFIG_SMP
  2711. cpumask_set_cpu(smp_processor_id(), &cpus_booted_once_mask);
  2712. atomic_set(this_cpu_ptr(&cpuhp_state.ap_sync_state), SYNC_STATE_ONLINE);
  2713. #endif
  2714. this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
  2715. this_cpu_write(cpuhp_state.target, CPUHP_ONLINE);
  2716. }
  2717. #ifdef CONFIG_CPU_MITIGATIONS
  2718. /*
  2719. * These are used for a global "mitigations=" cmdline option for toggling
  2720. * optional CPU mitigations.
  2721. */
  2722. enum cpu_mitigations {
  2723. CPU_MITIGATIONS_OFF,
  2724. CPU_MITIGATIONS_AUTO,
  2725. CPU_MITIGATIONS_AUTO_NOSMT,
  2726. };
  2727. static enum cpu_mitigations cpu_mitigations __ro_after_init = CPU_MITIGATIONS_AUTO;
  2728. static int __init mitigations_parse_cmdline(char *arg)
  2729. {
  2730. if (!strcmp(arg, "off"))
  2731. cpu_mitigations = CPU_MITIGATIONS_OFF;
  2732. else if (!strcmp(arg, "auto"))
  2733. cpu_mitigations = CPU_MITIGATIONS_AUTO;
  2734. else if (!strcmp(arg, "auto,nosmt"))
  2735. cpu_mitigations = CPU_MITIGATIONS_AUTO_NOSMT;
  2736. else
  2737. pr_crit("Unsupported mitigations=%s, system may still be vulnerable\n",
  2738. arg);
  2739. return 0;
  2740. }
  2741. /* mitigations=off */
  2742. bool cpu_mitigations_off(void)
  2743. {
  2744. return cpu_mitigations == CPU_MITIGATIONS_OFF;
  2745. }
  2746. EXPORT_SYMBOL_GPL(cpu_mitigations_off);
  2747. /* mitigations=auto,nosmt */
  2748. bool cpu_mitigations_auto_nosmt(void)
  2749. {
  2750. return cpu_mitigations == CPU_MITIGATIONS_AUTO_NOSMT;
  2751. }
  2752. EXPORT_SYMBOL_GPL(cpu_mitigations_auto_nosmt);
  2753. #else
  2754. static int __init mitigations_parse_cmdline(char *arg)
  2755. {
  2756. pr_crit("Kernel compiled without mitigations, ignoring 'mitigations'; system may still be vulnerable\n");
  2757. return 0;
  2758. }
  2759. #endif
  2760. early_param("mitigations", mitigations_parse_cmdline);