cpu.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225
  1. /* CPU control.
  2. * (C) 2001, 2002, 2003, 2004 Rusty Russell
  3. *
  4. * This code is licenced under the GPL.
  5. */
  6. #include <linux/proc_fs.h>
  7. #include <linux/smp.h>
  8. #include <linux/init.h>
  9. #include <linux/notifier.h>
  10. #include <linux/sched.h>
  11. #include <linux/unistd.h>
  12. #include <linux/cpu.h>
  13. #include <linux/oom.h>
  14. #include <linux/rcupdate.h>
  15. #include <linux/export.h>
  16. #include <linux/bug.h>
  17. #include <linux/kthread.h>
  18. #include <linux/stop_machine.h>
  19. #include <linux/mutex.h>
  20. #include <linux/gfp.h>
  21. #include <linux/suspend.h>
  22. #include <linux/lockdep.h>
  23. #include <linux/tick.h>
  24. #include <linux/irq.h>
  25. #include <linux/smpboot.h>
  26. #include <linux/relay.h>
  27. #include <linux/slab.h>
  28. #include <trace/events/power.h>
  29. #define CREATE_TRACE_POINTS
  30. #include <trace/events/cpuhp.h>
  31. #include "smpboot.h"
  32. /**
  33. * cpuhp_cpu_state - Per cpu hotplug state storage
  34. * @state: The current cpu state
  35. * @target: The target state
  36. * @thread: Pointer to the hotplug thread
  37. * @should_run: Thread should execute
  38. * @rollback: Perform a rollback
  39. * @single: Single callback invocation
  40. * @bringup: Single callback bringup or teardown selector
  41. * @cb_state: The state for a single callback (install/uninstall)
  42. * @result: Result of the operation
  43. * @done: Signal completion to the issuer of the task
  44. */
  45. struct cpuhp_cpu_state {
  46. enum cpuhp_state state;
  47. enum cpuhp_state target;
  48. #ifdef CONFIG_SMP
  49. struct task_struct *thread;
  50. bool should_run;
  51. bool rollback;
  52. bool single;
  53. bool bringup;
  54. bool booted_once;
  55. struct hlist_node *node;
  56. enum cpuhp_state cb_state;
  57. int result;
  58. struct completion done;
  59. #endif
  60. };
  61. static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state);
  62. #if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
  63. static struct lock_class_key cpuhp_state_key;
  64. static struct lockdep_map cpuhp_state_lock_map =
  65. STATIC_LOCKDEP_MAP_INIT("cpuhp_state", &cpuhp_state_key);
  66. #endif
  67. /**
  68. * cpuhp_step - Hotplug state machine step
  69. * @name: Name of the step
  70. * @startup: Startup function of the step
  71. * @teardown: Teardown function of the step
  72. * @skip_onerr: Do not invoke the functions on error rollback
  73. * Will go away once the notifiers are gone
  74. * @cant_stop: Bringup/teardown can't be stopped at this step
  75. */
  76. struct cpuhp_step {
  77. const char *name;
  78. union {
  79. int (*single)(unsigned int cpu);
  80. int (*multi)(unsigned int cpu,
  81. struct hlist_node *node);
  82. } startup;
  83. union {
  84. int (*single)(unsigned int cpu);
  85. int (*multi)(unsigned int cpu,
  86. struct hlist_node *node);
  87. } teardown;
  88. struct hlist_head list;
  89. bool skip_onerr;
  90. bool cant_stop;
  91. bool multi_instance;
  92. };
  93. static DEFINE_MUTEX(cpuhp_state_mutex);
  94. static struct cpuhp_step cpuhp_bp_states[];
  95. static struct cpuhp_step cpuhp_ap_states[];
  96. static bool cpuhp_is_ap_state(enum cpuhp_state state)
  97. {
  98. /*
  99. * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
  100. * purposes as that state is handled explicitly in cpu_down.
  101. */
  102. return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
  103. }
  104. static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
  105. {
  106. struct cpuhp_step *sp;
  107. sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states;
  108. return sp + state;
  109. }
  110. /**
  111. * cpuhp_invoke_callback _ Invoke the callbacks for a given state
  112. * @cpu: The cpu for which the callback should be invoked
  113. * @step: The step in the state machine
  114. * @bringup: True if the bringup callback should be invoked
  115. *
  116. * Called from cpu hotplug and from the state register machinery.
  117. */
  118. static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
  119. bool bringup, struct hlist_node *node)
  120. {
  121. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  122. struct cpuhp_step *step = cpuhp_get_step(state);
  123. int (*cbm)(unsigned int cpu, struct hlist_node *node);
  124. int (*cb)(unsigned int cpu);
  125. int ret, cnt;
  126. if (!step->multi_instance) {
  127. cb = bringup ? step->startup.single : step->teardown.single;
  128. if (!cb)
  129. return 0;
  130. trace_cpuhp_enter(cpu, st->target, state, cb);
  131. ret = cb(cpu);
  132. trace_cpuhp_exit(cpu, st->state, state, ret);
  133. return ret;
  134. }
  135. cbm = bringup ? step->startup.multi : step->teardown.multi;
  136. if (!cbm)
  137. return 0;
  138. /* Single invocation for instance add/remove */
  139. if (node) {
  140. trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
  141. ret = cbm(cpu, node);
  142. trace_cpuhp_exit(cpu, st->state, state, ret);
  143. return ret;
  144. }
  145. /* State transition. Invoke on all instances */
  146. cnt = 0;
  147. hlist_for_each(node, &step->list) {
  148. trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
  149. ret = cbm(cpu, node);
  150. trace_cpuhp_exit(cpu, st->state, state, ret);
  151. if (ret)
  152. goto err;
  153. cnt++;
  154. }
  155. return 0;
  156. err:
  157. /* Rollback the instances if one failed */
  158. cbm = !bringup ? step->startup.multi : step->teardown.multi;
  159. if (!cbm)
  160. return ret;
  161. hlist_for_each(node, &step->list) {
  162. if (!cnt--)
  163. break;
  164. cbm(cpu, node);
  165. }
  166. return ret;
  167. }
  168. #ifdef CONFIG_SMP
  169. /* Serializes the updates to cpu_online_mask, cpu_present_mask */
  170. static DEFINE_MUTEX(cpu_add_remove_lock);
  171. bool cpuhp_tasks_frozen;
  172. EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
  173. /*
  174. * The following two APIs (cpu_maps_update_begin/done) must be used when
  175. * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
  176. * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
  177. * hotplug callback (un)registration performed using __register_cpu_notifier()
  178. * or __unregister_cpu_notifier().
  179. */
  180. void cpu_maps_update_begin(void)
  181. {
  182. mutex_lock(&cpu_add_remove_lock);
  183. }
  184. EXPORT_SYMBOL(cpu_notifier_register_begin);
  185. void cpu_maps_update_done(void)
  186. {
  187. mutex_unlock(&cpu_add_remove_lock);
  188. }
  189. EXPORT_SYMBOL(cpu_notifier_register_done);
  190. static RAW_NOTIFIER_HEAD(cpu_chain);
  191. /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
  192. * Should always be manipulated under cpu_add_remove_lock
  193. */
  194. static int cpu_hotplug_disabled;
  195. #ifdef CONFIG_HOTPLUG_CPU
  196. static struct {
  197. struct task_struct *active_writer;
  198. /* wait queue to wake up the active_writer */
  199. wait_queue_head_t wq;
  200. /* verifies that no writer will get active while readers are active */
  201. struct mutex lock;
  202. /*
  203. * Also blocks the new readers during
  204. * an ongoing cpu hotplug operation.
  205. */
  206. atomic_t refcount;
  207. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  208. struct lockdep_map dep_map;
  209. #endif
  210. } cpu_hotplug = {
  211. .active_writer = NULL,
  212. .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  213. .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
  214. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  215. .dep_map = STATIC_LOCKDEP_MAP_INIT("cpu_hotplug.dep_map", &cpu_hotplug.dep_map),
  216. #endif
  217. };
  218. /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
  219. #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
  220. #define cpuhp_lock_acquire_tryread() \
  221. lock_map_acquire_tryread(&cpu_hotplug.dep_map)
  222. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  223. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  224. void get_online_cpus(void)
  225. {
  226. might_sleep();
  227. if (cpu_hotplug.active_writer == current)
  228. return;
  229. cpuhp_lock_acquire_read();
  230. mutex_lock(&cpu_hotplug.lock);
  231. atomic_inc(&cpu_hotplug.refcount);
  232. mutex_unlock(&cpu_hotplug.lock);
  233. }
  234. EXPORT_SYMBOL_GPL(get_online_cpus);
  235. void put_online_cpus(void)
  236. {
  237. int refcount;
  238. if (cpu_hotplug.active_writer == current)
  239. return;
  240. refcount = atomic_dec_return(&cpu_hotplug.refcount);
  241. if (WARN_ON(refcount < 0)) /* try to fix things up */
  242. atomic_inc(&cpu_hotplug.refcount);
  243. if (refcount <= 0 && waitqueue_active(&cpu_hotplug.wq))
  244. wake_up(&cpu_hotplug.wq);
  245. cpuhp_lock_release();
  246. }
  247. EXPORT_SYMBOL_GPL(put_online_cpus);
  248. /*
  249. * This ensures that the hotplug operation can begin only when the
  250. * refcount goes to zero.
  251. *
  252. * Note that during a cpu-hotplug operation, the new readers, if any,
  253. * will be blocked by the cpu_hotplug.lock
  254. *
  255. * Since cpu_hotplug_begin() is always called after invoking
  256. * cpu_maps_update_begin(), we can be sure that only one writer is active.
  257. *
  258. * Note that theoretically, there is a possibility of a livelock:
  259. * - Refcount goes to zero, last reader wakes up the sleeping
  260. * writer.
  261. * - Last reader unlocks the cpu_hotplug.lock.
  262. * - A new reader arrives at this moment, bumps up the refcount.
  263. * - The writer acquires the cpu_hotplug.lock finds the refcount
  264. * non zero and goes to sleep again.
  265. *
  266. * However, this is very difficult to achieve in practice since
  267. * get_online_cpus() not an api which is called all that often.
  268. *
  269. */
  270. void cpu_hotplug_begin(void)
  271. {
  272. DEFINE_WAIT(wait);
  273. cpu_hotplug.active_writer = current;
  274. cpuhp_lock_acquire();
  275. for (;;) {
  276. mutex_lock(&cpu_hotplug.lock);
  277. prepare_to_wait(&cpu_hotplug.wq, &wait, TASK_UNINTERRUPTIBLE);
  278. if (likely(!atomic_read(&cpu_hotplug.refcount)))
  279. break;
  280. mutex_unlock(&cpu_hotplug.lock);
  281. schedule();
  282. }
  283. finish_wait(&cpu_hotplug.wq, &wait);
  284. }
  285. void cpu_hotplug_done(void)
  286. {
  287. cpu_hotplug.active_writer = NULL;
  288. mutex_unlock(&cpu_hotplug.lock);
  289. cpuhp_lock_release();
  290. }
  291. /*
  292. * Wait for currently running CPU hotplug operations to complete (if any) and
  293. * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
  294. * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
  295. * hotplug path before performing hotplug operations. So acquiring that lock
  296. * guarantees mutual exclusion from any currently running hotplug operations.
  297. */
  298. void cpu_hotplug_disable(void)
  299. {
  300. cpu_maps_update_begin();
  301. cpu_hotplug_disabled++;
  302. cpu_maps_update_done();
  303. }
  304. EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
  305. static void __cpu_hotplug_enable(void)
  306. {
  307. if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
  308. return;
  309. cpu_hotplug_disabled--;
  310. }
  311. void cpu_hotplug_enable(void)
  312. {
  313. cpu_maps_update_begin();
  314. __cpu_hotplug_enable();
  315. cpu_maps_update_done();
  316. }
  317. EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
  318. #endif /* CONFIG_HOTPLUG_CPU */
  319. #ifdef CONFIG_HOTPLUG_SMT
  320. enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
  321. EXPORT_SYMBOL_GPL(cpu_smt_control);
  322. static bool cpu_smt_available __read_mostly;
  323. void __init cpu_smt_disable(bool force)
  324. {
  325. if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
  326. cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
  327. return;
  328. if (force) {
  329. pr_info("SMT: Force disabled\n");
  330. cpu_smt_control = CPU_SMT_FORCE_DISABLED;
  331. } else {
  332. cpu_smt_control = CPU_SMT_DISABLED;
  333. }
  334. }
  335. /*
  336. * The decision whether SMT is supported can only be done after the full
  337. * CPU identification. Called from architecture code before non boot CPUs
  338. * are brought up.
  339. */
  340. void __init cpu_smt_check_topology_early(void)
  341. {
  342. if (!topology_smt_supported())
  343. cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
  344. }
  345. /*
  346. * If SMT was disabled by BIOS, detect it here, after the CPUs have been
  347. * brought online. This ensures the smt/l1tf sysfs entries are consistent
  348. * with reality. cpu_smt_available is set to true during the bringup of non
  349. * boot CPUs when a SMT sibling is detected. Note, this may overwrite
  350. * cpu_smt_control's previous setting.
  351. */
  352. void __init cpu_smt_check_topology(void)
  353. {
  354. if (!cpu_smt_available)
  355. cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
  356. }
  357. static int __init smt_cmdline_disable(char *str)
  358. {
  359. cpu_smt_disable(str && !strcmp(str, "force"));
  360. return 0;
  361. }
  362. early_param("nosmt", smt_cmdline_disable);
  363. static inline bool cpu_smt_allowed(unsigned int cpu)
  364. {
  365. if (topology_is_primary_thread(cpu))
  366. return true;
  367. /*
  368. * If the CPU is not a 'primary' thread and the booted_once bit is
  369. * set then the processor has SMT support. Store this information
  370. * for the late check of SMT support in cpu_smt_check_topology().
  371. */
  372. if (per_cpu(cpuhp_state, cpu).booted_once)
  373. cpu_smt_available = true;
  374. if (cpu_smt_control == CPU_SMT_ENABLED)
  375. return true;
  376. /*
  377. * On x86 it's required to boot all logical CPUs at least once so
  378. * that the init code can get a chance to set CR4.MCE on each
  379. * CPU. Otherwise, a broadacasted MCE observing CR4.MCE=0b on any
  380. * core will shutdown the machine.
  381. */
  382. return !per_cpu(cpuhp_state, cpu).booted_once;
  383. }
  384. #else
  385. static inline bool cpu_smt_allowed(unsigned int cpu) { return true; }
  386. #endif
  387. /* Need to know about CPUs going up/down? */
  388. int register_cpu_notifier(struct notifier_block *nb)
  389. {
  390. int ret;
  391. cpu_maps_update_begin();
  392. ret = raw_notifier_chain_register(&cpu_chain, nb);
  393. cpu_maps_update_done();
  394. return ret;
  395. }
  396. int __register_cpu_notifier(struct notifier_block *nb)
  397. {
  398. return raw_notifier_chain_register(&cpu_chain, nb);
  399. }
  400. static int __cpu_notify(unsigned long val, unsigned int cpu, int nr_to_call,
  401. int *nr_calls)
  402. {
  403. unsigned long mod = cpuhp_tasks_frozen ? CPU_TASKS_FROZEN : 0;
  404. void *hcpu = (void *)(long)cpu;
  405. int ret;
  406. ret = __raw_notifier_call_chain(&cpu_chain, val | mod, hcpu, nr_to_call,
  407. nr_calls);
  408. return notifier_to_errno(ret);
  409. }
  410. static int cpu_notify(unsigned long val, unsigned int cpu)
  411. {
  412. return __cpu_notify(val, cpu, -1, NULL);
  413. }
  414. static void cpu_notify_nofail(unsigned long val, unsigned int cpu)
  415. {
  416. BUG_ON(cpu_notify(val, cpu));
  417. }
  418. /* Notifier wrappers for transitioning to state machine */
  419. static int notify_prepare(unsigned int cpu)
  420. {
  421. int nr_calls = 0;
  422. int ret;
  423. ret = __cpu_notify(CPU_UP_PREPARE, cpu, -1, &nr_calls);
  424. if (ret) {
  425. nr_calls--;
  426. printk(KERN_WARNING "%s: attempt to bring up CPU %u failed\n",
  427. __func__, cpu);
  428. __cpu_notify(CPU_UP_CANCELED, cpu, nr_calls, NULL);
  429. }
  430. return ret;
  431. }
  432. static int notify_online(unsigned int cpu)
  433. {
  434. cpu_notify(CPU_ONLINE, cpu);
  435. return 0;
  436. }
  437. static void __cpuhp_kick_ap_work(struct cpuhp_cpu_state *st);
  438. static int bringup_wait_for_ap(unsigned int cpu)
  439. {
  440. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  441. /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
  442. wait_for_completion(&st->done);
  443. if (WARN_ON_ONCE((!cpu_online(cpu))))
  444. return -ECANCELED;
  445. /* Unpark the stopper thread and the hotplug thread of the target cpu */
  446. stop_machine_unpark(cpu);
  447. kthread_unpark(st->thread);
  448. /*
  449. * SMT soft disabling on X86 requires to bring the CPU out of the
  450. * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit. The
  451. * CPU marked itself as booted_once in cpu_notify_starting() so the
  452. * cpu_smt_allowed() check will now return false if this is not the
  453. * primary sibling.
  454. */
  455. if (!cpu_smt_allowed(cpu))
  456. return -ECANCELED;
  457. /* Should we go further up ? */
  458. if (st->target > CPUHP_AP_ONLINE_IDLE) {
  459. __cpuhp_kick_ap_work(st);
  460. wait_for_completion(&st->done);
  461. }
  462. return st->result;
  463. }
  464. static int bringup_cpu(unsigned int cpu)
  465. {
  466. struct task_struct *idle = idle_thread_get(cpu);
  467. int ret;
  468. /*
  469. * Some architectures have to walk the irq descriptors to
  470. * setup the vector space for the cpu which comes online.
  471. * Prevent irq alloc/free across the bringup.
  472. */
  473. irq_lock_sparse();
  474. /* Arch-specific enabling code. */
  475. ret = __cpu_up(cpu, idle);
  476. irq_unlock_sparse();
  477. if (ret) {
  478. cpu_notify(CPU_UP_CANCELED, cpu);
  479. return ret;
  480. }
  481. return bringup_wait_for_ap(cpu);
  482. }
  483. /*
  484. * Hotplug state machine related functions
  485. */
  486. static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
  487. {
  488. for (st->state++; st->state < st->target; st->state++) {
  489. struct cpuhp_step *step = cpuhp_get_step(st->state);
  490. if (!step->skip_onerr)
  491. cpuhp_invoke_callback(cpu, st->state, true, NULL);
  492. }
  493. }
  494. static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
  495. enum cpuhp_state target)
  496. {
  497. enum cpuhp_state prev_state = st->state;
  498. int ret = 0;
  499. for (; st->state > target; st->state--) {
  500. ret = cpuhp_invoke_callback(cpu, st->state, false, NULL);
  501. if (ret) {
  502. st->target = prev_state;
  503. undo_cpu_down(cpu, st);
  504. break;
  505. }
  506. }
  507. return ret;
  508. }
  509. static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
  510. {
  511. for (st->state--; st->state > st->target; st->state--) {
  512. struct cpuhp_step *step = cpuhp_get_step(st->state);
  513. if (!step->skip_onerr)
  514. cpuhp_invoke_callback(cpu, st->state, false, NULL);
  515. }
  516. }
  517. static inline bool can_rollback_cpu(struct cpuhp_cpu_state *st)
  518. {
  519. if (IS_ENABLED(CONFIG_HOTPLUG_CPU))
  520. return true;
  521. /*
  522. * When CPU hotplug is disabled, then taking the CPU down is not
  523. * possible because takedown_cpu() and the architecture and
  524. * subsystem specific mechanisms are not available. So the CPU
  525. * which would be completely unplugged again needs to stay around
  526. * in the current state.
  527. */
  528. return st->state <= CPUHP_BRINGUP_CPU;
  529. }
  530. static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
  531. enum cpuhp_state target)
  532. {
  533. enum cpuhp_state prev_state = st->state;
  534. int ret = 0;
  535. while (st->state < target) {
  536. st->state++;
  537. ret = cpuhp_invoke_callback(cpu, st->state, true, NULL);
  538. if (ret) {
  539. if (can_rollback_cpu(st)) {
  540. st->target = prev_state;
  541. undo_cpu_up(cpu, st);
  542. }
  543. break;
  544. }
  545. }
  546. return ret;
  547. }
  548. /*
  549. * The cpu hotplug threads manage the bringup and teardown of the cpus
  550. */
  551. static void cpuhp_create(unsigned int cpu)
  552. {
  553. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  554. init_completion(&st->done);
  555. }
  556. static int cpuhp_should_run(unsigned int cpu)
  557. {
  558. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  559. return st->should_run;
  560. }
  561. /* Execute the teardown callbacks. Used to be CPU_DOWN_PREPARE */
  562. static int cpuhp_ap_offline(unsigned int cpu, struct cpuhp_cpu_state *st)
  563. {
  564. enum cpuhp_state target = max((int)st->target, CPUHP_TEARDOWN_CPU);
  565. return cpuhp_down_callbacks(cpu, st, target);
  566. }
  567. /* Execute the online startup callbacks. Used to be CPU_ONLINE */
  568. static int cpuhp_ap_online(unsigned int cpu, struct cpuhp_cpu_state *st)
  569. {
  570. return cpuhp_up_callbacks(cpu, st, st->target);
  571. }
  572. /*
  573. * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
  574. * callbacks when a state gets [un]installed at runtime.
  575. */
  576. static void cpuhp_thread_fun(unsigned int cpu)
  577. {
  578. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  579. int ret = 0;
  580. /*
  581. * Paired with the mb() in cpuhp_kick_ap_work and
  582. * cpuhp_invoke_ap_callback, so the work set is consistent visible.
  583. */
  584. smp_mb();
  585. if (!st->should_run)
  586. return;
  587. st->should_run = false;
  588. lock_map_acquire(&cpuhp_state_lock_map);
  589. /* Single callback invocation for [un]install ? */
  590. if (st->single) {
  591. if (st->cb_state < CPUHP_AP_ONLINE) {
  592. local_irq_disable();
  593. ret = cpuhp_invoke_callback(cpu, st->cb_state,
  594. st->bringup, st->node);
  595. local_irq_enable();
  596. } else {
  597. ret = cpuhp_invoke_callback(cpu, st->cb_state,
  598. st->bringup, st->node);
  599. }
  600. } else if (st->rollback) {
  601. BUG_ON(st->state < CPUHP_AP_ONLINE_IDLE);
  602. undo_cpu_down(cpu, st);
  603. /*
  604. * This is a momentary workaround to keep the notifier users
  605. * happy. Will go away once we got rid of the notifiers.
  606. */
  607. cpu_notify_nofail(CPU_DOWN_FAILED, cpu);
  608. st->rollback = false;
  609. } else {
  610. /* Cannot happen .... */
  611. BUG_ON(st->state < CPUHP_AP_ONLINE_IDLE);
  612. /* Regular hotplug work */
  613. if (st->state < st->target)
  614. ret = cpuhp_ap_online(cpu, st);
  615. else if (st->state > st->target)
  616. ret = cpuhp_ap_offline(cpu, st);
  617. }
  618. lock_map_release(&cpuhp_state_lock_map);
  619. st->result = ret;
  620. complete(&st->done);
  621. }
  622. /* Invoke a single callback on a remote cpu */
  623. static int
  624. cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
  625. struct hlist_node *node)
  626. {
  627. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  628. if (!cpu_online(cpu))
  629. return 0;
  630. lock_map_acquire(&cpuhp_state_lock_map);
  631. lock_map_release(&cpuhp_state_lock_map);
  632. /*
  633. * If we are up and running, use the hotplug thread. For early calls
  634. * we invoke the thread function directly.
  635. */
  636. if (!st->thread)
  637. return cpuhp_invoke_callback(cpu, state, bringup, node);
  638. st->cb_state = state;
  639. st->single = true;
  640. st->bringup = bringup;
  641. st->node = node;
  642. /*
  643. * Make sure the above stores are visible before should_run becomes
  644. * true. Paired with the mb() above in cpuhp_thread_fun()
  645. */
  646. smp_mb();
  647. st->should_run = true;
  648. wake_up_process(st->thread);
  649. wait_for_completion(&st->done);
  650. return st->result;
  651. }
  652. /* Regular hotplug invocation of the AP hotplug thread */
  653. static void __cpuhp_kick_ap_work(struct cpuhp_cpu_state *st)
  654. {
  655. st->result = 0;
  656. st->single = false;
  657. /*
  658. * Make sure the above stores are visible before should_run becomes
  659. * true. Paired with the mb() above in cpuhp_thread_fun()
  660. */
  661. smp_mb();
  662. st->should_run = true;
  663. wake_up_process(st->thread);
  664. }
  665. static int cpuhp_kick_ap_work(unsigned int cpu)
  666. {
  667. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  668. enum cpuhp_state state = st->state;
  669. trace_cpuhp_enter(cpu, st->target, state, cpuhp_kick_ap_work);
  670. lock_map_acquire(&cpuhp_state_lock_map);
  671. lock_map_release(&cpuhp_state_lock_map);
  672. __cpuhp_kick_ap_work(st);
  673. wait_for_completion(&st->done);
  674. trace_cpuhp_exit(cpu, st->state, state, st->result);
  675. return st->result;
  676. }
  677. static struct smp_hotplug_thread cpuhp_threads = {
  678. .store = &cpuhp_state.thread,
  679. .create = &cpuhp_create,
  680. .thread_should_run = cpuhp_should_run,
  681. .thread_fn = cpuhp_thread_fun,
  682. .thread_comm = "cpuhp/%u",
  683. .selfparking = true,
  684. };
  685. void __init cpuhp_threads_init(void)
  686. {
  687. BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
  688. kthread_unpark(this_cpu_read(cpuhp_state.thread));
  689. }
  690. EXPORT_SYMBOL(register_cpu_notifier);
  691. EXPORT_SYMBOL(__register_cpu_notifier);
  692. void unregister_cpu_notifier(struct notifier_block *nb)
  693. {
  694. cpu_maps_update_begin();
  695. raw_notifier_chain_unregister(&cpu_chain, nb);
  696. cpu_maps_update_done();
  697. }
  698. EXPORT_SYMBOL(unregister_cpu_notifier);
  699. void __unregister_cpu_notifier(struct notifier_block *nb)
  700. {
  701. raw_notifier_chain_unregister(&cpu_chain, nb);
  702. }
  703. EXPORT_SYMBOL(__unregister_cpu_notifier);
  704. #ifdef CONFIG_HOTPLUG_CPU
  705. /**
  706. * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
  707. * @cpu: a CPU id
  708. *
  709. * This function walks all processes, finds a valid mm struct for each one and
  710. * then clears a corresponding bit in mm's cpumask. While this all sounds
  711. * trivial, there are various non-obvious corner cases, which this function
  712. * tries to solve in a safe manner.
  713. *
  714. * Also note that the function uses a somewhat relaxed locking scheme, so it may
  715. * be called only for an already offlined CPU.
  716. */
  717. void clear_tasks_mm_cpumask(int cpu)
  718. {
  719. struct task_struct *p;
  720. /*
  721. * This function is called after the cpu is taken down and marked
  722. * offline, so its not like new tasks will ever get this cpu set in
  723. * their mm mask. -- Peter Zijlstra
  724. * Thus, we may use rcu_read_lock() here, instead of grabbing
  725. * full-fledged tasklist_lock.
  726. */
  727. WARN_ON(cpu_online(cpu));
  728. rcu_read_lock();
  729. for_each_process(p) {
  730. struct task_struct *t;
  731. /*
  732. * Main thread might exit, but other threads may still have
  733. * a valid mm. Find one.
  734. */
  735. t = find_lock_task_mm(p);
  736. if (!t)
  737. continue;
  738. cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
  739. task_unlock(t);
  740. }
  741. rcu_read_unlock();
  742. }
  743. static inline void check_for_tasks(int dead_cpu)
  744. {
  745. struct task_struct *g, *p;
  746. read_lock(&tasklist_lock);
  747. for_each_process_thread(g, p) {
  748. if (!p->on_rq)
  749. continue;
  750. /*
  751. * We do the check with unlocked task_rq(p)->lock.
  752. * Order the reading to do not warn about a task,
  753. * which was running on this cpu in the past, and
  754. * it's just been woken on another cpu.
  755. */
  756. rmb();
  757. if (task_cpu(p) != dead_cpu)
  758. continue;
  759. pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
  760. p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags);
  761. }
  762. read_unlock(&tasklist_lock);
  763. }
  764. static int notify_down_prepare(unsigned int cpu)
  765. {
  766. int err, nr_calls = 0;
  767. err = __cpu_notify(CPU_DOWN_PREPARE, cpu, -1, &nr_calls);
  768. if (err) {
  769. nr_calls--;
  770. __cpu_notify(CPU_DOWN_FAILED, cpu, nr_calls, NULL);
  771. pr_warn("%s: attempt to take down CPU %u failed\n",
  772. __func__, cpu);
  773. }
  774. return err;
  775. }
  776. /* Take this CPU down. */
  777. static int take_cpu_down(void *_param)
  778. {
  779. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  780. enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
  781. int err, cpu = smp_processor_id();
  782. /* Ensure this CPU doesn't handle any more interrupts. */
  783. err = __cpu_disable();
  784. if (err < 0)
  785. return err;
  786. /*
  787. * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
  788. * do this step again.
  789. */
  790. WARN_ON(st->state != CPUHP_TEARDOWN_CPU);
  791. st->state--;
  792. /* Invoke the former CPU_DYING callbacks */
  793. for (; st->state > target; st->state--)
  794. cpuhp_invoke_callback(cpu, st->state, false, NULL);
  795. /* Give up timekeeping duties */
  796. tick_handover_do_timer();
  797. /* Park the stopper thread */
  798. stop_machine_park(cpu);
  799. return 0;
  800. }
  801. static int takedown_cpu(unsigned int cpu)
  802. {
  803. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  804. int err;
  805. /* Park the smpboot threads */
  806. kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
  807. /*
  808. * Prevent irq alloc/free while the dying cpu reorganizes the
  809. * interrupt affinities.
  810. */
  811. irq_lock_sparse();
  812. /*
  813. * So now all preempt/rcu users must observe !cpu_active().
  814. */
  815. err = stop_machine(take_cpu_down, NULL, cpumask_of(cpu));
  816. if (err) {
  817. /* CPU refused to die */
  818. irq_unlock_sparse();
  819. /* Unpark the hotplug thread so we can rollback there */
  820. kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
  821. return err;
  822. }
  823. BUG_ON(cpu_online(cpu));
  824. /*
  825. * The CPUHP_AP_SCHED_MIGRATE_DYING callback will have removed all
  826. * runnable tasks from the cpu, there's only the idle task left now
  827. * that the migration thread is done doing the stop_machine thing.
  828. *
  829. * Wait for the stop thread to go away.
  830. */
  831. wait_for_completion(&st->done);
  832. BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
  833. /* Interrupts are moved away from the dying cpu, reenable alloc/free */
  834. irq_unlock_sparse();
  835. hotplug_cpu__broadcast_tick_pull(cpu);
  836. /* This actually kills the CPU. */
  837. __cpu_die(cpu);
  838. tick_cleanup_dead_cpu(cpu);
  839. return 0;
  840. }
  841. static int notify_dead(unsigned int cpu)
  842. {
  843. cpu_notify_nofail(CPU_DEAD, cpu);
  844. check_for_tasks(cpu);
  845. return 0;
  846. }
  847. static void cpuhp_complete_idle_dead(void *arg)
  848. {
  849. struct cpuhp_cpu_state *st = arg;
  850. complete(&st->done);
  851. }
  852. void cpuhp_report_idle_dead(void)
  853. {
  854. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  855. BUG_ON(st->state != CPUHP_AP_OFFLINE);
  856. rcu_report_dead(smp_processor_id());
  857. st->state = CPUHP_AP_IDLE_DEAD;
  858. /*
  859. * We cannot call complete after rcu_report_dead() so we delegate it
  860. * to an online cpu.
  861. */
  862. smp_call_function_single(cpumask_first(cpu_online_mask),
  863. cpuhp_complete_idle_dead, st, 0);
  864. }
  865. #else
  866. #define notify_down_prepare NULL
  867. #define takedown_cpu NULL
  868. #define notify_dead NULL
  869. #endif
  870. #ifdef CONFIG_HOTPLUG_CPU
  871. /* Requires cpu_add_remove_lock to be held */
  872. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
  873. enum cpuhp_state target)
  874. {
  875. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  876. int prev_state, ret = 0;
  877. bool hasdied = false;
  878. if (num_online_cpus() == 1)
  879. return -EBUSY;
  880. if (!cpu_present(cpu))
  881. return -EINVAL;
  882. cpu_hotplug_begin();
  883. cpuhp_tasks_frozen = tasks_frozen;
  884. prev_state = st->state;
  885. st->target = target;
  886. /*
  887. * If the current CPU state is in the range of the AP hotplug thread,
  888. * then we need to kick the thread.
  889. */
  890. if (st->state > CPUHP_TEARDOWN_CPU) {
  891. ret = cpuhp_kick_ap_work(cpu);
  892. /*
  893. * The AP side has done the error rollback already. Just
  894. * return the error code..
  895. */
  896. if (ret)
  897. goto out;
  898. /*
  899. * We might have stopped still in the range of the AP hotplug
  900. * thread. Nothing to do anymore.
  901. */
  902. if (st->state > CPUHP_TEARDOWN_CPU)
  903. goto out;
  904. }
  905. /*
  906. * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
  907. * to do the further cleanups.
  908. */
  909. ret = cpuhp_down_callbacks(cpu, st, target);
  910. if (ret && st->state > CPUHP_TEARDOWN_CPU && st->state < prev_state) {
  911. st->target = prev_state;
  912. st->rollback = true;
  913. cpuhp_kick_ap_work(cpu);
  914. }
  915. hasdied = prev_state != st->state && st->state == CPUHP_OFFLINE;
  916. out:
  917. cpu_hotplug_done();
  918. /* This post dead nonsense must die */
  919. if (!ret && hasdied)
  920. cpu_notify_nofail(CPU_POST_DEAD, cpu);
  921. return ret;
  922. }
  923. static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
  924. {
  925. if (cpu_hotplug_disabled)
  926. return -EBUSY;
  927. return _cpu_down(cpu, 0, target);
  928. }
  929. static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
  930. {
  931. int err;
  932. cpu_maps_update_begin();
  933. err = cpu_down_maps_locked(cpu, target);
  934. cpu_maps_update_done();
  935. return err;
  936. }
  937. int cpu_down(unsigned int cpu)
  938. {
  939. return do_cpu_down(cpu, CPUHP_OFFLINE);
  940. }
  941. EXPORT_SYMBOL(cpu_down);
  942. #endif /*CONFIG_HOTPLUG_CPU*/
  943. /**
  944. * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
  945. * @cpu: cpu that just started
  946. *
  947. * It must be called by the arch code on the new cpu, before the new cpu
  948. * enables interrupts and before the "boot" cpu returns from __cpu_up().
  949. */
  950. void notify_cpu_starting(unsigned int cpu)
  951. {
  952. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  953. enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
  954. rcu_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
  955. st->booted_once = true;
  956. while (st->state < target) {
  957. st->state++;
  958. cpuhp_invoke_callback(cpu, st->state, true, NULL);
  959. }
  960. }
  961. /*
  962. * Called from the idle task. Wake up the controlling task which brings the
  963. * stopper and the hotplug thread of the upcoming CPU up and then delegates
  964. * the rest of the online bringup to the hotplug thread.
  965. */
  966. void cpuhp_online_idle(enum cpuhp_state state)
  967. {
  968. struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
  969. /* Happens for the boot cpu */
  970. if (state != CPUHP_AP_ONLINE_IDLE)
  971. return;
  972. st->state = CPUHP_AP_ONLINE_IDLE;
  973. complete(&st->done);
  974. }
  975. /* Requires cpu_add_remove_lock to be held */
  976. static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
  977. {
  978. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  979. struct task_struct *idle;
  980. int ret = 0;
  981. cpu_hotplug_begin();
  982. if (!cpu_present(cpu)) {
  983. ret = -EINVAL;
  984. goto out;
  985. }
  986. /*
  987. * The caller of do_cpu_up might have raced with another
  988. * caller. Ignore it for now.
  989. */
  990. if (st->state >= target)
  991. goto out;
  992. if (st->state == CPUHP_OFFLINE) {
  993. /* Let it fail before we try to bring the cpu up */
  994. idle = idle_thread_get(cpu);
  995. if (IS_ERR(idle)) {
  996. ret = PTR_ERR(idle);
  997. goto out;
  998. }
  999. }
  1000. cpuhp_tasks_frozen = tasks_frozen;
  1001. st->target = target;
  1002. /*
  1003. * If the current CPU state is in the range of the AP hotplug thread,
  1004. * then we need to kick the thread once more.
  1005. */
  1006. if (st->state > CPUHP_BRINGUP_CPU) {
  1007. ret = cpuhp_kick_ap_work(cpu);
  1008. /*
  1009. * The AP side has done the error rollback already. Just
  1010. * return the error code..
  1011. */
  1012. if (ret)
  1013. goto out;
  1014. }
  1015. /*
  1016. * Try to reach the target state. We max out on the BP at
  1017. * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
  1018. * responsible for bringing it up to the target state.
  1019. */
  1020. target = min((int)target, CPUHP_BRINGUP_CPU);
  1021. ret = cpuhp_up_callbacks(cpu, st, target);
  1022. out:
  1023. cpu_hotplug_done();
  1024. return ret;
  1025. }
  1026. static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
  1027. {
  1028. int err = 0;
  1029. if (!cpu_possible(cpu)) {
  1030. pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
  1031. cpu);
  1032. #if defined(CONFIG_IA64)
  1033. pr_err("please check additional_cpus= boot parameter\n");
  1034. #endif
  1035. return -EINVAL;
  1036. }
  1037. err = try_online_node(cpu_to_node(cpu));
  1038. if (err)
  1039. return err;
  1040. cpu_maps_update_begin();
  1041. if (cpu_hotplug_disabled) {
  1042. err = -EBUSY;
  1043. goto out;
  1044. }
  1045. if (!cpu_smt_allowed(cpu)) {
  1046. err = -EPERM;
  1047. goto out;
  1048. }
  1049. err = _cpu_up(cpu, 0, target);
  1050. out:
  1051. cpu_maps_update_done();
  1052. return err;
  1053. }
  1054. int cpu_up(unsigned int cpu)
  1055. {
  1056. return do_cpu_up(cpu, CPUHP_ONLINE);
  1057. }
  1058. EXPORT_SYMBOL_GPL(cpu_up);
  1059. #ifdef CONFIG_PM_SLEEP_SMP
  1060. static cpumask_var_t frozen_cpus;
  1061. int freeze_secondary_cpus(int primary)
  1062. {
  1063. int cpu, error = 0;
  1064. cpu_maps_update_begin();
  1065. if (!cpu_online(primary))
  1066. primary = cpumask_first(cpu_online_mask);
  1067. /*
  1068. * We take down all of the non-boot CPUs in one shot to avoid races
  1069. * with the userspace trying to use the CPU hotplug at the same time
  1070. */
  1071. cpumask_clear(frozen_cpus);
  1072. pr_info("Disabling non-boot CPUs ...\n");
  1073. for_each_online_cpu(cpu) {
  1074. if (cpu == primary)
  1075. continue;
  1076. trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
  1077. error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
  1078. trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
  1079. if (!error)
  1080. cpumask_set_cpu(cpu, frozen_cpus);
  1081. else {
  1082. pr_err("Error taking CPU%d down: %d\n", cpu, error);
  1083. break;
  1084. }
  1085. }
  1086. if (!error)
  1087. BUG_ON(num_online_cpus() > 1);
  1088. else
  1089. pr_err("Non-boot CPUs are not disabled\n");
  1090. /*
  1091. * Make sure the CPUs won't be enabled by someone else. We need to do
  1092. * this even in case of failure as all disable_nonboot_cpus() users are
  1093. * supposed to do enable_nonboot_cpus() on the failure path.
  1094. */
  1095. cpu_hotplug_disabled++;
  1096. cpu_maps_update_done();
  1097. return error;
  1098. }
  1099. void __weak arch_enable_nonboot_cpus_begin(void)
  1100. {
  1101. }
  1102. void __weak arch_enable_nonboot_cpus_end(void)
  1103. {
  1104. }
  1105. void enable_nonboot_cpus(void)
  1106. {
  1107. int cpu, error;
  1108. /* Allow everyone to use the CPU hotplug again */
  1109. cpu_maps_update_begin();
  1110. __cpu_hotplug_enable();
  1111. if (cpumask_empty(frozen_cpus))
  1112. goto out;
  1113. pr_info("Enabling non-boot CPUs ...\n");
  1114. arch_enable_nonboot_cpus_begin();
  1115. for_each_cpu(cpu, frozen_cpus) {
  1116. trace_suspend_resume(TPS("CPU_ON"), cpu, true);
  1117. error = _cpu_up(cpu, 1, CPUHP_ONLINE);
  1118. trace_suspend_resume(TPS("CPU_ON"), cpu, false);
  1119. if (!error) {
  1120. pr_info("CPU%d is up\n", cpu);
  1121. continue;
  1122. }
  1123. pr_warn("Error taking CPU%d up: %d\n", cpu, error);
  1124. }
  1125. arch_enable_nonboot_cpus_end();
  1126. cpumask_clear(frozen_cpus);
  1127. out:
  1128. cpu_maps_update_done();
  1129. }
  1130. static int __init alloc_frozen_cpus(void)
  1131. {
  1132. if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
  1133. return -ENOMEM;
  1134. return 0;
  1135. }
  1136. core_initcall(alloc_frozen_cpus);
  1137. /*
  1138. * When callbacks for CPU hotplug notifications are being executed, we must
  1139. * ensure that the state of the system with respect to the tasks being frozen
  1140. * or not, as reported by the notification, remains unchanged *throughout the
  1141. * duration* of the execution of the callbacks.
  1142. * Hence we need to prevent the freezer from racing with regular CPU hotplug.
  1143. *
  1144. * This synchronization is implemented by mutually excluding regular CPU
  1145. * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
  1146. * Hibernate notifications.
  1147. */
  1148. static int
  1149. cpu_hotplug_pm_callback(struct notifier_block *nb,
  1150. unsigned long action, void *ptr)
  1151. {
  1152. switch (action) {
  1153. case PM_SUSPEND_PREPARE:
  1154. case PM_HIBERNATION_PREPARE:
  1155. cpu_hotplug_disable();
  1156. break;
  1157. case PM_POST_SUSPEND:
  1158. case PM_POST_HIBERNATION:
  1159. cpu_hotplug_enable();
  1160. break;
  1161. default:
  1162. return NOTIFY_DONE;
  1163. }
  1164. return NOTIFY_OK;
  1165. }
  1166. static int __init cpu_hotplug_pm_sync_init(void)
  1167. {
  1168. /*
  1169. * cpu_hotplug_pm_callback has higher priority than x86
  1170. * bsp_pm_callback which depends on cpu_hotplug_pm_callback
  1171. * to disable cpu hotplug to avoid cpu hotplug race.
  1172. */
  1173. pm_notifier(cpu_hotplug_pm_callback, 0);
  1174. return 0;
  1175. }
  1176. core_initcall(cpu_hotplug_pm_sync_init);
  1177. #endif /* CONFIG_PM_SLEEP_SMP */
  1178. #endif /* CONFIG_SMP */
  1179. /* Boot processor state steps */
  1180. static struct cpuhp_step cpuhp_bp_states[] = {
  1181. [CPUHP_OFFLINE] = {
  1182. .name = "offline",
  1183. .startup.single = NULL,
  1184. .teardown.single = NULL,
  1185. },
  1186. #ifdef CONFIG_SMP
  1187. [CPUHP_CREATE_THREADS]= {
  1188. .name = "threads:prepare",
  1189. .startup.single = smpboot_create_threads,
  1190. .teardown.single = NULL,
  1191. .cant_stop = true,
  1192. },
  1193. [CPUHP_PERF_PREPARE] = {
  1194. .name = "perf:prepare",
  1195. .startup.single = perf_event_init_cpu,
  1196. .teardown.single = perf_event_exit_cpu,
  1197. },
  1198. [CPUHP_WORKQUEUE_PREP] = {
  1199. .name = "workqueue:prepare",
  1200. .startup.single = workqueue_prepare_cpu,
  1201. .teardown.single = NULL,
  1202. },
  1203. [CPUHP_HRTIMERS_PREPARE] = {
  1204. .name = "hrtimers:prepare",
  1205. .startup.single = hrtimers_prepare_cpu,
  1206. .teardown.single = hrtimers_dead_cpu,
  1207. },
  1208. [CPUHP_SMPCFD_PREPARE] = {
  1209. .name = "smpcfd:prepare",
  1210. .startup.single = smpcfd_prepare_cpu,
  1211. .teardown.single = smpcfd_dead_cpu,
  1212. },
  1213. [CPUHP_RELAY_PREPARE] = {
  1214. .name = "relay:prepare",
  1215. .startup.single = relay_prepare_cpu,
  1216. .teardown.single = NULL,
  1217. },
  1218. [CPUHP_SLAB_PREPARE] = {
  1219. .name = "slab:prepare",
  1220. .startup.single = slab_prepare_cpu,
  1221. .teardown.single = slab_dead_cpu,
  1222. },
  1223. [CPUHP_RCUTREE_PREP] = {
  1224. .name = "RCU/tree:prepare",
  1225. .startup.single = rcutree_prepare_cpu,
  1226. .teardown.single = rcutree_dead_cpu,
  1227. },
  1228. /*
  1229. * Preparatory and dead notifiers. Will be replaced once the notifiers
  1230. * are converted to states.
  1231. */
  1232. [CPUHP_NOTIFY_PREPARE] = {
  1233. .name = "notify:prepare",
  1234. .startup.single = notify_prepare,
  1235. .teardown.single = notify_dead,
  1236. .skip_onerr = true,
  1237. .cant_stop = true,
  1238. },
  1239. /*
  1240. * On the tear-down path, timers_dead_cpu() must be invoked
  1241. * before blk_mq_queue_reinit_notify() from notify_dead(),
  1242. * otherwise a RCU stall occurs.
  1243. */
  1244. [CPUHP_TIMERS_PREPARE] = {
  1245. .name = "timers:dead",
  1246. .startup.single = timers_prepare_cpu,
  1247. .teardown.single = timers_dead_cpu,
  1248. },
  1249. /* Kicks the plugged cpu into life */
  1250. [CPUHP_BRINGUP_CPU] = {
  1251. .name = "cpu:bringup",
  1252. .startup.single = bringup_cpu,
  1253. .teardown.single = NULL,
  1254. .cant_stop = true,
  1255. },
  1256. /*
  1257. * Handled on controll processor until the plugged processor manages
  1258. * this itself.
  1259. */
  1260. [CPUHP_TEARDOWN_CPU] = {
  1261. .name = "cpu:teardown",
  1262. .startup.single = NULL,
  1263. .teardown.single = takedown_cpu,
  1264. .cant_stop = true,
  1265. },
  1266. #else
  1267. [CPUHP_BRINGUP_CPU] = { },
  1268. #endif
  1269. };
  1270. /* Application processor state steps */
  1271. static struct cpuhp_step cpuhp_ap_states[] = {
  1272. #ifdef CONFIG_SMP
  1273. /* Final state before CPU kills itself */
  1274. [CPUHP_AP_IDLE_DEAD] = {
  1275. .name = "idle:dead",
  1276. },
  1277. /*
  1278. * Last state before CPU enters the idle loop to die. Transient state
  1279. * for synchronization.
  1280. */
  1281. [CPUHP_AP_OFFLINE] = {
  1282. .name = "ap:offline",
  1283. .cant_stop = true,
  1284. },
  1285. /* First state is scheduler control. Interrupts are disabled */
  1286. [CPUHP_AP_SCHED_STARTING] = {
  1287. .name = "sched:starting",
  1288. .startup.single = sched_cpu_starting,
  1289. .teardown.single = sched_cpu_dying,
  1290. },
  1291. [CPUHP_AP_RCUTREE_DYING] = {
  1292. .name = "RCU/tree:dying",
  1293. .startup.single = NULL,
  1294. .teardown.single = rcutree_dying_cpu,
  1295. },
  1296. [CPUHP_AP_SMPCFD_DYING] = {
  1297. .name = "smpcfd:dying",
  1298. .startup.single = NULL,
  1299. .teardown.single = smpcfd_dying_cpu,
  1300. },
  1301. /* Entry state on starting. Interrupts enabled from here on. Transient
  1302. * state for synchronsization */
  1303. [CPUHP_AP_ONLINE] = {
  1304. .name = "ap:online",
  1305. },
  1306. /* Handle smpboot threads park/unpark */
  1307. [CPUHP_AP_SMPBOOT_THREADS] = {
  1308. .name = "smpboot/threads:online",
  1309. .startup.single = smpboot_unpark_threads,
  1310. .teardown.single = smpboot_park_threads,
  1311. },
  1312. [CPUHP_AP_PERF_ONLINE] = {
  1313. .name = "perf:online",
  1314. .startup.single = perf_event_init_cpu,
  1315. .teardown.single = perf_event_exit_cpu,
  1316. },
  1317. [CPUHP_AP_WORKQUEUE_ONLINE] = {
  1318. .name = "workqueue:online",
  1319. .startup.single = workqueue_online_cpu,
  1320. .teardown.single = workqueue_offline_cpu,
  1321. },
  1322. [CPUHP_AP_RCUTREE_ONLINE] = {
  1323. .name = "RCU/tree:online",
  1324. .startup.single = rcutree_online_cpu,
  1325. .teardown.single = rcutree_offline_cpu,
  1326. },
  1327. /*
  1328. * Online/down_prepare notifiers. Will be removed once the notifiers
  1329. * are converted to states.
  1330. */
  1331. [CPUHP_AP_NOTIFY_ONLINE] = {
  1332. .name = "notify:online",
  1333. .startup.single = notify_online,
  1334. .teardown.single = notify_down_prepare,
  1335. .skip_onerr = true,
  1336. },
  1337. #endif
  1338. /*
  1339. * The dynamically registered state space is here
  1340. */
  1341. #ifdef CONFIG_SMP
  1342. /* Last state is scheduler control setting the cpu active */
  1343. [CPUHP_AP_ACTIVE] = {
  1344. .name = "sched:active",
  1345. .startup.single = sched_cpu_activate,
  1346. .teardown.single = sched_cpu_deactivate,
  1347. },
  1348. #endif
  1349. /* CPU is fully up and running. */
  1350. [CPUHP_ONLINE] = {
  1351. .name = "online",
  1352. .startup.single = NULL,
  1353. .teardown.single = NULL,
  1354. },
  1355. };
  1356. /* Sanity check for callbacks */
  1357. static int cpuhp_cb_check(enum cpuhp_state state)
  1358. {
  1359. if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
  1360. return -EINVAL;
  1361. return 0;
  1362. }
  1363. static void cpuhp_store_callbacks(enum cpuhp_state state,
  1364. const char *name,
  1365. int (*startup)(unsigned int cpu),
  1366. int (*teardown)(unsigned int cpu),
  1367. bool multi_instance)
  1368. {
  1369. /* (Un)Install the callbacks for further cpu hotplug operations */
  1370. struct cpuhp_step *sp;
  1371. sp = cpuhp_get_step(state);
  1372. sp->startup.single = startup;
  1373. sp->teardown.single = teardown;
  1374. sp->name = name;
  1375. sp->multi_instance = multi_instance;
  1376. INIT_HLIST_HEAD(&sp->list);
  1377. }
  1378. static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
  1379. {
  1380. return cpuhp_get_step(state)->teardown.single;
  1381. }
  1382. /*
  1383. * Call the startup/teardown function for a step either on the AP or
  1384. * on the current CPU.
  1385. */
  1386. static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
  1387. struct hlist_node *node)
  1388. {
  1389. struct cpuhp_step *sp = cpuhp_get_step(state);
  1390. int ret;
  1391. if ((bringup && !sp->startup.single) ||
  1392. (!bringup && !sp->teardown.single))
  1393. return 0;
  1394. /*
  1395. * The non AP bound callbacks can fail on bringup. On teardown
  1396. * e.g. module removal we crash for now.
  1397. */
  1398. #ifdef CONFIG_SMP
  1399. if (cpuhp_is_ap_state(state))
  1400. ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
  1401. else
  1402. ret = cpuhp_invoke_callback(cpu, state, bringup, node);
  1403. #else
  1404. ret = cpuhp_invoke_callback(cpu, state, bringup, node);
  1405. #endif
  1406. BUG_ON(ret && !bringup);
  1407. return ret;
  1408. }
  1409. /*
  1410. * Called from __cpuhp_setup_state on a recoverable failure.
  1411. *
  1412. * Note: The teardown callbacks for rollback are not allowed to fail!
  1413. */
  1414. static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
  1415. struct hlist_node *node)
  1416. {
  1417. int cpu;
  1418. /* Roll back the already executed steps on the other cpus */
  1419. for_each_present_cpu(cpu) {
  1420. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1421. int cpustate = st->state;
  1422. if (cpu >= failedcpu)
  1423. break;
  1424. /* Did we invoke the startup call on that cpu ? */
  1425. if (cpustate >= state)
  1426. cpuhp_issue_call(cpu, state, false, node);
  1427. }
  1428. }
  1429. /*
  1430. * Returns a free for dynamic slot assignment of the Online state. The states
  1431. * are protected by the cpuhp_slot_states mutex and an empty slot is identified
  1432. * by having no name assigned.
  1433. */
  1434. static int cpuhp_reserve_state(enum cpuhp_state state)
  1435. {
  1436. enum cpuhp_state i;
  1437. for (i = CPUHP_AP_ONLINE_DYN; i <= CPUHP_AP_ONLINE_DYN_END; i++) {
  1438. if (cpuhp_ap_states[i].name)
  1439. continue;
  1440. cpuhp_ap_states[i].name = "Reserved";
  1441. return i;
  1442. }
  1443. WARN(1, "No more dynamic states available for CPU hotplug\n");
  1444. return -ENOSPC;
  1445. }
  1446. int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
  1447. bool invoke)
  1448. {
  1449. struct cpuhp_step *sp;
  1450. int cpu;
  1451. int ret;
  1452. sp = cpuhp_get_step(state);
  1453. if (sp->multi_instance == false)
  1454. return -EINVAL;
  1455. get_online_cpus();
  1456. mutex_lock(&cpuhp_state_mutex);
  1457. if (!invoke || !sp->startup.multi)
  1458. goto add_node;
  1459. /*
  1460. * Try to call the startup callback for each present cpu
  1461. * depending on the hotplug state of the cpu.
  1462. */
  1463. for_each_present_cpu(cpu) {
  1464. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1465. int cpustate = st->state;
  1466. if (cpustate < state)
  1467. continue;
  1468. ret = cpuhp_issue_call(cpu, state, true, node);
  1469. if (ret) {
  1470. if (sp->teardown.multi)
  1471. cpuhp_rollback_install(cpu, state, node);
  1472. goto err;
  1473. }
  1474. }
  1475. add_node:
  1476. ret = 0;
  1477. hlist_add_head(node, &sp->list);
  1478. err:
  1479. mutex_unlock(&cpuhp_state_mutex);
  1480. put_online_cpus();
  1481. return ret;
  1482. }
  1483. EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
  1484. /**
  1485. * __cpuhp_setup_state - Setup the callbacks for an hotplug machine state
  1486. * @state: The state to setup
  1487. * @invoke: If true, the startup function is invoked for cpus where
  1488. * cpu state >= @state
  1489. * @startup: startup callback function
  1490. * @teardown: teardown callback function
  1491. *
  1492. * Returns 0 if successful, otherwise a proper error code
  1493. */
  1494. int __cpuhp_setup_state(enum cpuhp_state state,
  1495. const char *name, bool invoke,
  1496. int (*startup)(unsigned int cpu),
  1497. int (*teardown)(unsigned int cpu),
  1498. bool multi_instance)
  1499. {
  1500. int cpu, ret = 0;
  1501. int dyn_state = 0;
  1502. if (cpuhp_cb_check(state) || !name)
  1503. return -EINVAL;
  1504. get_online_cpus();
  1505. mutex_lock(&cpuhp_state_mutex);
  1506. /* currently assignments for the ONLINE state are possible */
  1507. if (state == CPUHP_AP_ONLINE_DYN) {
  1508. dyn_state = 1;
  1509. ret = cpuhp_reserve_state(state);
  1510. if (ret < 0)
  1511. goto out;
  1512. state = ret;
  1513. }
  1514. cpuhp_store_callbacks(state, name, startup, teardown, multi_instance);
  1515. if (!invoke || !startup)
  1516. goto out;
  1517. /*
  1518. * Try to call the startup callback for each present cpu
  1519. * depending on the hotplug state of the cpu.
  1520. */
  1521. for_each_present_cpu(cpu) {
  1522. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1523. int cpustate = st->state;
  1524. if (cpustate < state)
  1525. continue;
  1526. ret = cpuhp_issue_call(cpu, state, true, NULL);
  1527. if (ret) {
  1528. if (teardown)
  1529. cpuhp_rollback_install(cpu, state, NULL);
  1530. cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
  1531. goto out;
  1532. }
  1533. }
  1534. out:
  1535. mutex_unlock(&cpuhp_state_mutex);
  1536. put_online_cpus();
  1537. if (!ret && dyn_state)
  1538. return state;
  1539. return ret;
  1540. }
  1541. EXPORT_SYMBOL(__cpuhp_setup_state);
  1542. int __cpuhp_state_remove_instance(enum cpuhp_state state,
  1543. struct hlist_node *node, bool invoke)
  1544. {
  1545. struct cpuhp_step *sp = cpuhp_get_step(state);
  1546. int cpu;
  1547. BUG_ON(cpuhp_cb_check(state));
  1548. if (!sp->multi_instance)
  1549. return -EINVAL;
  1550. get_online_cpus();
  1551. mutex_lock(&cpuhp_state_mutex);
  1552. if (!invoke || !cpuhp_get_teardown_cb(state))
  1553. goto remove;
  1554. /*
  1555. * Call the teardown callback for each present cpu depending
  1556. * on the hotplug state of the cpu. This function is not
  1557. * allowed to fail currently!
  1558. */
  1559. for_each_present_cpu(cpu) {
  1560. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1561. int cpustate = st->state;
  1562. if (cpustate >= state)
  1563. cpuhp_issue_call(cpu, state, false, node);
  1564. }
  1565. remove:
  1566. hlist_del(node);
  1567. mutex_unlock(&cpuhp_state_mutex);
  1568. put_online_cpus();
  1569. return 0;
  1570. }
  1571. EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
  1572. /**
  1573. * __cpuhp_remove_state - Remove the callbacks for an hotplug machine state
  1574. * @state: The state to remove
  1575. * @invoke: If true, the teardown function is invoked for cpus where
  1576. * cpu state >= @state
  1577. *
  1578. * The teardown callback is currently not allowed to fail. Think
  1579. * about module removal!
  1580. */
  1581. void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
  1582. {
  1583. struct cpuhp_step *sp = cpuhp_get_step(state);
  1584. int cpu;
  1585. BUG_ON(cpuhp_cb_check(state));
  1586. get_online_cpus();
  1587. mutex_lock(&cpuhp_state_mutex);
  1588. if (sp->multi_instance) {
  1589. WARN(!hlist_empty(&sp->list),
  1590. "Error: Removing state %d which has instances left.\n",
  1591. state);
  1592. goto remove;
  1593. }
  1594. if (!invoke || !cpuhp_get_teardown_cb(state))
  1595. goto remove;
  1596. /*
  1597. * Call the teardown callback for each present cpu depending
  1598. * on the hotplug state of the cpu. This function is not
  1599. * allowed to fail currently!
  1600. */
  1601. for_each_present_cpu(cpu) {
  1602. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  1603. int cpustate = st->state;
  1604. if (cpustate >= state)
  1605. cpuhp_issue_call(cpu, state, false, NULL);
  1606. }
  1607. remove:
  1608. cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
  1609. mutex_unlock(&cpuhp_state_mutex);
  1610. put_online_cpus();
  1611. }
  1612. EXPORT_SYMBOL(__cpuhp_remove_state);
  1613. #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
  1614. static ssize_t show_cpuhp_state(struct device *dev,
  1615. struct device_attribute *attr, char *buf)
  1616. {
  1617. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1618. return sprintf(buf, "%d\n", st->state);
  1619. }
  1620. static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
  1621. static ssize_t write_cpuhp_target(struct device *dev,
  1622. struct device_attribute *attr,
  1623. const char *buf, size_t count)
  1624. {
  1625. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1626. struct cpuhp_step *sp;
  1627. int target, ret;
  1628. ret = kstrtoint(buf, 10, &target);
  1629. if (ret)
  1630. return ret;
  1631. #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
  1632. if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
  1633. return -EINVAL;
  1634. #else
  1635. if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
  1636. return -EINVAL;
  1637. #endif
  1638. ret = lock_device_hotplug_sysfs();
  1639. if (ret)
  1640. return ret;
  1641. mutex_lock(&cpuhp_state_mutex);
  1642. sp = cpuhp_get_step(target);
  1643. ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
  1644. mutex_unlock(&cpuhp_state_mutex);
  1645. if (ret)
  1646. goto out;
  1647. if (st->state < target)
  1648. ret = do_cpu_up(dev->id, target);
  1649. else
  1650. ret = do_cpu_down(dev->id, target);
  1651. out:
  1652. unlock_device_hotplug();
  1653. return ret ? ret : count;
  1654. }
  1655. static ssize_t show_cpuhp_target(struct device *dev,
  1656. struct device_attribute *attr, char *buf)
  1657. {
  1658. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
  1659. return sprintf(buf, "%d\n", st->target);
  1660. }
  1661. static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
  1662. static struct attribute *cpuhp_cpu_attrs[] = {
  1663. &dev_attr_state.attr,
  1664. &dev_attr_target.attr,
  1665. NULL
  1666. };
  1667. static struct attribute_group cpuhp_cpu_attr_group = {
  1668. .attrs = cpuhp_cpu_attrs,
  1669. .name = "hotplug",
  1670. NULL
  1671. };
  1672. static ssize_t show_cpuhp_states(struct device *dev,
  1673. struct device_attribute *attr, char *buf)
  1674. {
  1675. ssize_t cur, res = 0;
  1676. int i;
  1677. mutex_lock(&cpuhp_state_mutex);
  1678. for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
  1679. struct cpuhp_step *sp = cpuhp_get_step(i);
  1680. if (sp->name) {
  1681. cur = sprintf(buf, "%3d: %s\n", i, sp->name);
  1682. buf += cur;
  1683. res += cur;
  1684. }
  1685. }
  1686. mutex_unlock(&cpuhp_state_mutex);
  1687. return res;
  1688. }
  1689. static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
  1690. static struct attribute *cpuhp_cpu_root_attrs[] = {
  1691. &dev_attr_states.attr,
  1692. NULL
  1693. };
  1694. static struct attribute_group cpuhp_cpu_root_attr_group = {
  1695. .attrs = cpuhp_cpu_root_attrs,
  1696. .name = "hotplug",
  1697. NULL
  1698. };
  1699. #ifdef CONFIG_HOTPLUG_SMT
  1700. static const char *smt_states[] = {
  1701. [CPU_SMT_ENABLED] = "on",
  1702. [CPU_SMT_DISABLED] = "off",
  1703. [CPU_SMT_FORCE_DISABLED] = "forceoff",
  1704. [CPU_SMT_NOT_SUPPORTED] = "notsupported",
  1705. };
  1706. static ssize_t
  1707. show_smt_control(struct device *dev, struct device_attribute *attr, char *buf)
  1708. {
  1709. return snprintf(buf, PAGE_SIZE - 2, "%s\n", smt_states[cpu_smt_control]);
  1710. }
  1711. static void cpuhp_offline_cpu_device(unsigned int cpu)
  1712. {
  1713. struct device *dev = get_cpu_device(cpu);
  1714. dev->offline = true;
  1715. /* Tell user space about the state change */
  1716. kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  1717. }
  1718. static void cpuhp_online_cpu_device(unsigned int cpu)
  1719. {
  1720. struct device *dev = get_cpu_device(cpu);
  1721. dev->offline = false;
  1722. /* Tell user space about the state change */
  1723. kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  1724. }
  1725. static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
  1726. {
  1727. int cpu, ret = 0;
  1728. cpu_maps_update_begin();
  1729. for_each_online_cpu(cpu) {
  1730. if (topology_is_primary_thread(cpu))
  1731. continue;
  1732. ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
  1733. if (ret)
  1734. break;
  1735. /*
  1736. * As this needs to hold the cpu maps lock it's impossible
  1737. * to call device_offline() because that ends up calling
  1738. * cpu_down() which takes cpu maps lock. cpu maps lock
  1739. * needs to be held as this might race against in kernel
  1740. * abusers of the hotplug machinery (thermal management).
  1741. *
  1742. * So nothing would update device:offline state. That would
  1743. * leave the sysfs entry stale and prevent onlining after
  1744. * smt control has been changed to 'off' again. This is
  1745. * called under the sysfs hotplug lock, so it is properly
  1746. * serialized against the regular offline usage.
  1747. */
  1748. cpuhp_offline_cpu_device(cpu);
  1749. }
  1750. if (!ret)
  1751. cpu_smt_control = ctrlval;
  1752. cpu_maps_update_done();
  1753. return ret;
  1754. }
  1755. static int cpuhp_smt_enable(void)
  1756. {
  1757. int cpu, ret = 0;
  1758. cpu_maps_update_begin();
  1759. cpu_smt_control = CPU_SMT_ENABLED;
  1760. for_each_present_cpu(cpu) {
  1761. /* Skip online CPUs and CPUs on offline nodes */
  1762. if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
  1763. continue;
  1764. ret = _cpu_up(cpu, 0, CPUHP_ONLINE);
  1765. if (ret)
  1766. break;
  1767. /* See comment in cpuhp_smt_disable() */
  1768. cpuhp_online_cpu_device(cpu);
  1769. }
  1770. cpu_maps_update_done();
  1771. return ret;
  1772. }
  1773. static ssize_t
  1774. store_smt_control(struct device *dev, struct device_attribute *attr,
  1775. const char *buf, size_t count)
  1776. {
  1777. int ctrlval, ret;
  1778. if (sysfs_streq(buf, "on"))
  1779. ctrlval = CPU_SMT_ENABLED;
  1780. else if (sysfs_streq(buf, "off"))
  1781. ctrlval = CPU_SMT_DISABLED;
  1782. else if (sysfs_streq(buf, "forceoff"))
  1783. ctrlval = CPU_SMT_FORCE_DISABLED;
  1784. else
  1785. return -EINVAL;
  1786. if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
  1787. return -EPERM;
  1788. if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
  1789. return -ENODEV;
  1790. ret = lock_device_hotplug_sysfs();
  1791. if (ret)
  1792. return ret;
  1793. if (ctrlval != cpu_smt_control) {
  1794. switch (ctrlval) {
  1795. case CPU_SMT_ENABLED:
  1796. ret = cpuhp_smt_enable();
  1797. break;
  1798. case CPU_SMT_DISABLED:
  1799. case CPU_SMT_FORCE_DISABLED:
  1800. ret = cpuhp_smt_disable(ctrlval);
  1801. break;
  1802. }
  1803. }
  1804. unlock_device_hotplug();
  1805. return ret ? ret : count;
  1806. }
  1807. static DEVICE_ATTR(control, 0644, show_smt_control, store_smt_control);
  1808. static ssize_t
  1809. show_smt_active(struct device *dev, struct device_attribute *attr, char *buf)
  1810. {
  1811. bool active = topology_max_smt_threads() > 1;
  1812. return snprintf(buf, PAGE_SIZE - 2, "%d\n", active);
  1813. }
  1814. static DEVICE_ATTR(active, 0444, show_smt_active, NULL);
  1815. static struct attribute *cpuhp_smt_attrs[] = {
  1816. &dev_attr_control.attr,
  1817. &dev_attr_active.attr,
  1818. NULL
  1819. };
  1820. static const struct attribute_group cpuhp_smt_attr_group = {
  1821. .attrs = cpuhp_smt_attrs,
  1822. .name = "smt",
  1823. NULL
  1824. };
  1825. static int __init cpu_smt_state_init(void)
  1826. {
  1827. return sysfs_create_group(&cpu_subsys.dev_root->kobj,
  1828. &cpuhp_smt_attr_group);
  1829. }
  1830. #else
  1831. static inline int cpu_smt_state_init(void) { return 0; }
  1832. #endif
  1833. static int __init cpuhp_sysfs_init(void)
  1834. {
  1835. int cpu, ret;
  1836. ret = cpu_smt_state_init();
  1837. if (ret)
  1838. return ret;
  1839. ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
  1840. &cpuhp_cpu_root_attr_group);
  1841. if (ret)
  1842. return ret;
  1843. for_each_possible_cpu(cpu) {
  1844. struct device *dev = get_cpu_device(cpu);
  1845. if (!dev)
  1846. continue;
  1847. ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
  1848. if (ret)
  1849. return ret;
  1850. }
  1851. return 0;
  1852. }
  1853. device_initcall(cpuhp_sysfs_init);
  1854. #endif
  1855. /*
  1856. * cpu_bit_bitmap[] is a special, "compressed" data structure that
  1857. * represents all NR_CPUS bits binary values of 1<<nr.
  1858. *
  1859. * It is used by cpumask_of() to get a constant address to a CPU
  1860. * mask value that has a single bit set only.
  1861. */
  1862. /* cpu_bit_bitmap[0] is empty - so we can back into it */
  1863. #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
  1864. #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
  1865. #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
  1866. #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
  1867. const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
  1868. MASK_DECLARE_8(0), MASK_DECLARE_8(8),
  1869. MASK_DECLARE_8(16), MASK_DECLARE_8(24),
  1870. #if BITS_PER_LONG > 32
  1871. MASK_DECLARE_8(32), MASK_DECLARE_8(40),
  1872. MASK_DECLARE_8(48), MASK_DECLARE_8(56),
  1873. #endif
  1874. };
  1875. EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
  1876. const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  1877. EXPORT_SYMBOL(cpu_all_bits);
  1878. #ifdef CONFIG_INIT_ALL_POSSIBLE
  1879. struct cpumask __cpu_possible_mask __read_mostly
  1880. = {CPU_BITS_ALL};
  1881. #else
  1882. struct cpumask __cpu_possible_mask __read_mostly;
  1883. #endif
  1884. EXPORT_SYMBOL(__cpu_possible_mask);
  1885. struct cpumask __cpu_online_mask __read_mostly;
  1886. EXPORT_SYMBOL(__cpu_online_mask);
  1887. struct cpumask __cpu_present_mask __read_mostly;
  1888. EXPORT_SYMBOL(__cpu_present_mask);
  1889. struct cpumask __cpu_active_mask __read_mostly;
  1890. EXPORT_SYMBOL(__cpu_active_mask);
  1891. void init_cpu_present(const struct cpumask *src)
  1892. {
  1893. cpumask_copy(&__cpu_present_mask, src);
  1894. }
  1895. void init_cpu_possible(const struct cpumask *src)
  1896. {
  1897. cpumask_copy(&__cpu_possible_mask, src);
  1898. }
  1899. void init_cpu_online(const struct cpumask *src)
  1900. {
  1901. cpumask_copy(&__cpu_online_mask, src);
  1902. }
  1903. /*
  1904. * Activate the first processor.
  1905. */
  1906. void __init boot_cpu_init(void)
  1907. {
  1908. int cpu = smp_processor_id();
  1909. /* Mark the boot cpu "present", "online" etc for SMP and UP case */
  1910. set_cpu_online(cpu, true);
  1911. set_cpu_active(cpu, true);
  1912. set_cpu_present(cpu, true);
  1913. set_cpu_possible(cpu, true);
  1914. }
  1915. /*
  1916. * Must be called _AFTER_ setting up the per_cpu areas
  1917. */
  1918. void __init boot_cpu_hotplug_init(void)
  1919. {
  1920. #ifdef CONFIG_SMP
  1921. this_cpu_write(cpuhp_state.booted_once, true);
  1922. #endif
  1923. this_cpu_write(cpuhp_state.state, CPUHP_ONLINE);
  1924. }