cpu.c 70 KB

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