timer_migration.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Infrastructure for migratable timers
  4. *
  5. * Copyright(C) 2022 linutronix GmbH
  6. */
  7. #include <linux/cpuhotplug.h>
  8. #include <linux/slab.h>
  9. #include <linux/smp.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/timerqueue.h>
  12. #include <trace/events/ipi.h>
  13. #include "timer_migration.h"
  14. #include "tick-internal.h"
  15. #define CREATE_TRACE_POINTS
  16. #include <trace/events/timer_migration.h>
  17. /*
  18. * The timer migration mechanism is built on a hierarchy of groups. The
  19. * lowest level group contains CPUs, the next level groups of CPU groups
  20. * and so forth. The CPU groups are kept per node so for the normal case
  21. * lock contention won't happen across nodes. Depending on the number of
  22. * CPUs per node even the next level might be kept as groups of CPU groups
  23. * per node and only the levels above cross the node topology.
  24. *
  25. * Example topology for a two node system with 24 CPUs each.
  26. *
  27. * LVL 2 [GRP2:0]
  28. * GRP1:0 = GRP1:M
  29. *
  30. * LVL 1 [GRP1:0] [GRP1:1]
  31. * GRP0:0 - GRP0:2 GRP0:3 - GRP0:5
  32. *
  33. * LVL 0 [GRP0:0] [GRP0:1] [GRP0:2] [GRP0:3] [GRP0:4] [GRP0:5]
  34. * CPUS 0-7 8-15 16-23 24-31 32-39 40-47
  35. *
  36. * The groups hold a timer queue of events sorted by expiry time. These
  37. * queues are updated when CPUs go in idle. When they come out of idle
  38. * ignore flag of events is set.
  39. *
  40. * Each group has a designated migrator CPU/group as long as a CPU/group is
  41. * active in the group. This designated role is necessary to avoid that all
  42. * active CPUs in a group try to migrate expired timers from other CPUs,
  43. * which would result in massive lock bouncing.
  44. *
  45. * When a CPU is awake, it checks in it's own timer tick the group
  46. * hierarchy up to the point where it is assigned the migrator role or if
  47. * no CPU is active, it also checks the groups where no migrator is set
  48. * (TMIGR_NONE).
  49. *
  50. * If it finds expired timers in one of the group queues it pulls them over
  51. * from the idle CPU and runs the timer function. After that it updates the
  52. * group and the parent groups if required.
  53. *
  54. * CPUs which go idle arm their CPU local timer hardware for the next local
  55. * (pinned) timer event. If the next migratable timer expires after the
  56. * next local timer or the CPU has no migratable timer pending then the
  57. * CPU does not queue an event in the LVL0 group. If the next migratable
  58. * timer expires before the next local timer then the CPU queues that timer
  59. * in the LVL0 group. In both cases the CPU marks itself idle in the LVL0
  60. * group.
  61. *
  62. * When CPU comes out of idle and when a group has at least a single active
  63. * child, the ignore flag of the tmigr_event is set. This indicates, that
  64. * the event is ignored even if it is still enqueued in the parent groups
  65. * timer queue. It will be removed when touching the timer queue the next
  66. * time. This spares locking in active path as the lock protects (after
  67. * setup) only event information. For more information about locking,
  68. * please read the section "Locking rules".
  69. *
  70. * If the CPU is the migrator of the group then it delegates that role to
  71. * the next active CPU in the group or sets migrator to TMIGR_NONE when
  72. * there is no active CPU in the group. This delegation needs to be
  73. * propagated up the hierarchy so hand over from other leaves can happen at
  74. * all hierarchy levels w/o doing a search.
  75. *
  76. * When the last CPU in the system goes idle, then it drops all migrator
  77. * duties up to the top level of the hierarchy (LVL2 in the example). It
  78. * then has to make sure, that it arms it's own local hardware timer for
  79. * the earliest event in the system.
  80. *
  81. *
  82. * Lifetime rules:
  83. * ---------------
  84. *
  85. * The groups are built up at init time or when CPUs come online. They are
  86. * not destroyed when a group becomes empty due to offlining. The group
  87. * just won't participate in the hierarchy management anymore. Destroying
  88. * groups would result in interesting race conditions which would just make
  89. * the whole mechanism slow and complex.
  90. *
  91. *
  92. * Locking rules:
  93. * --------------
  94. *
  95. * For setting up new groups and handling events it's required to lock both
  96. * child and parent group. The lock ordering is always bottom up. This also
  97. * includes the per CPU locks in struct tmigr_cpu. For updating the migrator and
  98. * active CPU/group information atomic_try_cmpxchg() is used instead and only
  99. * the per CPU tmigr_cpu->lock is held.
  100. *
  101. * During the setup of groups tmigr_level_list is required. It is protected by
  102. * @tmigr_mutex.
  103. *
  104. * When @timer_base->lock as well as tmigr related locks are required, the lock
  105. * ordering is: first @timer_base->lock, afterwards tmigr related locks.
  106. *
  107. *
  108. * Protection of the tmigr group state information:
  109. * ------------------------------------------------
  110. *
  111. * The state information with the list of active children and migrator needs to
  112. * be protected by a sequence counter. It prevents a race when updates in child
  113. * groups are propagated in changed order. The state update is performed
  114. * lockless and group wise. The following scenario describes what happens
  115. * without updating the sequence counter:
  116. *
  117. * Therefore, let's take three groups and four CPUs (CPU2 and CPU3 as well
  118. * as GRP0:1 will not change during the scenario):
  119. *
  120. * LVL 1 [GRP1:0]
  121. * migrator = GRP0:1
  122. * active = GRP0:0, GRP0:1
  123. * / \
  124. * LVL 0 [GRP0:0] [GRP0:1]
  125. * migrator = CPU0 migrator = CPU2
  126. * active = CPU0 active = CPU2
  127. * / \ / \
  128. * CPUs 0 1 2 3
  129. * active idle active idle
  130. *
  131. *
  132. * 1. CPU0 goes idle. As the update is performed group wise, in the first step
  133. * only GRP0:0 is updated. The update of GRP1:0 is pending as CPU0 has to
  134. * walk the hierarchy.
  135. *
  136. * LVL 1 [GRP1:0]
  137. * migrator = GRP0:1
  138. * active = GRP0:0, GRP0:1
  139. * / \
  140. * LVL 0 [GRP0:0] [GRP0:1]
  141. * --> migrator = TMIGR_NONE migrator = CPU2
  142. * --> active = active = CPU2
  143. * / \ / \
  144. * CPUs 0 1 2 3
  145. * --> idle idle active idle
  146. *
  147. * 2. While CPU0 goes idle and continues to update the state, CPU1 comes out of
  148. * idle. CPU1 updates GRP0:0. The update for GRP1:0 is pending as CPU1 also
  149. * has to walk the hierarchy. Both CPUs (CPU0 and CPU1) now walk the
  150. * hierarchy to perform the needed update from their point of view. The
  151. * currently visible state looks the following:
  152. *
  153. * LVL 1 [GRP1:0]
  154. * migrator = GRP0:1
  155. * active = GRP0:0, GRP0:1
  156. * / \
  157. * LVL 0 [GRP0:0] [GRP0:1]
  158. * --> migrator = CPU1 migrator = CPU2
  159. * --> active = CPU1 active = CPU2
  160. * / \ / \
  161. * CPUs 0 1 2 3
  162. * idle --> active active idle
  163. *
  164. * 3. Here is the race condition: CPU1 managed to propagate its changes (from
  165. * step 2) through the hierarchy to GRP1:0 before CPU0 (step 1) did. The
  166. * active members of GRP1:0 remain unchanged after the update since it is
  167. * still valid from CPU1 current point of view:
  168. *
  169. * LVL 1 [GRP1:0]
  170. * --> migrator = GRP0:1
  171. * --> active = GRP0:0, GRP0:1
  172. * / \
  173. * LVL 0 [GRP0:0] [GRP0:1]
  174. * migrator = CPU1 migrator = CPU2
  175. * active = CPU1 active = CPU2
  176. * / \ / \
  177. * CPUs 0 1 2 3
  178. * idle active active idle
  179. *
  180. * 4. Now CPU0 finally propagates its changes (from step 1) to GRP1:0.
  181. *
  182. * LVL 1 [GRP1:0]
  183. * --> migrator = GRP0:1
  184. * --> active = GRP0:1
  185. * / \
  186. * LVL 0 [GRP0:0] [GRP0:1]
  187. * migrator = CPU1 migrator = CPU2
  188. * active = CPU1 active = CPU2
  189. * / \ / \
  190. * CPUs 0 1 2 3
  191. * idle active active idle
  192. *
  193. *
  194. * The race of CPU0 vs. CPU1 led to an inconsistent state in GRP1:0. CPU1 is
  195. * active and is correctly listed as active in GRP0:0. However GRP1:0 does not
  196. * have GRP0:0 listed as active, which is wrong. The sequence counter has been
  197. * added to avoid inconsistent states during updates. The state is updated
  198. * atomically only if all members, including the sequence counter, match the
  199. * expected value (compare-and-exchange).
  200. *
  201. * Looking back at the previous example with the addition of the sequence
  202. * counter: The update as performed by CPU0 in step 4 will fail. CPU1 changed
  203. * the sequence number during the update in step 3 so the expected old value (as
  204. * seen by CPU0 before starting the walk) does not match.
  205. *
  206. * Prevent race between new event and last CPU going inactive
  207. * ----------------------------------------------------------
  208. *
  209. * When the last CPU is going idle and there is a concurrent update of a new
  210. * first global timer of an idle CPU, the group and child states have to be read
  211. * while holding the lock in tmigr_update_events(). The following scenario shows
  212. * what happens, when this is not done.
  213. *
  214. * 1. Only CPU2 is active:
  215. *
  216. * LVL 1 [GRP1:0]
  217. * migrator = GRP0:1
  218. * active = GRP0:1
  219. * next_expiry = KTIME_MAX
  220. * / \
  221. * LVL 0 [GRP0:0] [GRP0:1]
  222. * migrator = TMIGR_NONE migrator = CPU2
  223. * active = active = CPU2
  224. * next_expiry = KTIME_MAX next_expiry = KTIME_MAX
  225. * / \ / \
  226. * CPUs 0 1 2 3
  227. * idle idle active idle
  228. *
  229. * 2. Now CPU 2 goes idle (and has no global timer, that has to be handled) and
  230. * propagates that to GRP0:1:
  231. *
  232. * LVL 1 [GRP1:0]
  233. * migrator = GRP0:1
  234. * active = GRP0:1
  235. * next_expiry = KTIME_MAX
  236. * / \
  237. * LVL 0 [GRP0:0] [GRP0:1]
  238. * migrator = TMIGR_NONE --> migrator = TMIGR_NONE
  239. * active = --> active =
  240. * next_expiry = KTIME_MAX next_expiry = KTIME_MAX
  241. * / \ / \
  242. * CPUs 0 1 2 3
  243. * idle idle --> idle idle
  244. *
  245. * 3. Now the idle state is propagated up to GRP1:0. As this is now the last
  246. * child going idle in top level group, the expiry of the next group event
  247. * has to be handed back to make sure no event is lost. As there is no event
  248. * enqueued, KTIME_MAX is handed back to CPU2.
  249. *
  250. * LVL 1 [GRP1:0]
  251. * --> migrator = TMIGR_NONE
  252. * --> active =
  253. * next_expiry = KTIME_MAX
  254. * / \
  255. * LVL 0 [GRP0:0] [GRP0:1]
  256. * migrator = TMIGR_NONE migrator = TMIGR_NONE
  257. * active = active =
  258. * next_expiry = KTIME_MAX next_expiry = KTIME_MAX
  259. * / \ / \
  260. * CPUs 0 1 2 3
  261. * idle idle --> idle idle
  262. *
  263. * 4. CPU 0 has a new timer queued from idle and it expires at TIMER0. CPU0
  264. * propagates that to GRP0:0:
  265. *
  266. * LVL 1 [GRP1:0]
  267. * migrator = TMIGR_NONE
  268. * active =
  269. * next_expiry = KTIME_MAX
  270. * / \
  271. * LVL 0 [GRP0:0] [GRP0:1]
  272. * migrator = TMIGR_NONE migrator = TMIGR_NONE
  273. * active = active =
  274. * --> next_expiry = TIMER0 next_expiry = KTIME_MAX
  275. * / \ / \
  276. * CPUs 0 1 2 3
  277. * idle idle idle idle
  278. *
  279. * 5. GRP0:0 is not active, so the new timer has to be propagated to
  280. * GRP1:0. Therefore the GRP1:0 state has to be read. When the stalled value
  281. * (from step 2) is read, the timer is enqueued into GRP1:0, but nothing is
  282. * handed back to CPU0, as it seems that there is still an active child in
  283. * top level group.
  284. *
  285. * LVL 1 [GRP1:0]
  286. * migrator = TMIGR_NONE
  287. * active =
  288. * --> next_expiry = TIMER0
  289. * / \
  290. * LVL 0 [GRP0:0] [GRP0:1]
  291. * migrator = TMIGR_NONE migrator = TMIGR_NONE
  292. * active = active =
  293. * next_expiry = TIMER0 next_expiry = KTIME_MAX
  294. * / \ / \
  295. * CPUs 0 1 2 3
  296. * idle idle idle idle
  297. *
  298. * This is prevented by reading the state when holding the lock (when a new
  299. * timer has to be propagated from idle path)::
  300. *
  301. * CPU2 (tmigr_inactive_up()) CPU0 (tmigr_new_timer_up())
  302. * -------------------------- ---------------------------
  303. * // step 3:
  304. * cmpxchg(&GRP1:0->state);
  305. * tmigr_update_events() {
  306. * spin_lock(&GRP1:0->lock);
  307. * // ... update events ...
  308. * // hand back first expiry when GRP1:0 is idle
  309. * spin_unlock(&GRP1:0->lock);
  310. * // ^^^ release state modification
  311. * }
  312. * tmigr_update_events() {
  313. * spin_lock(&GRP1:0->lock)
  314. * // ^^^ acquire state modification
  315. * group_state = atomic_read(&GRP1:0->state)
  316. * // .... update events ...
  317. * // hand back first expiry when GRP1:0 is idle
  318. * spin_unlock(&GRP1:0->lock) <3>
  319. * // ^^^ makes state visible for other
  320. * // callers of tmigr_new_timer_up()
  321. * }
  322. *
  323. * When CPU0 grabs the lock directly after cmpxchg, the first timer is reported
  324. * back to CPU0 and also later on to CPU2. So no timer is missed. A concurrent
  325. * update of the group state from active path is no problem, as the upcoming CPU
  326. * will take care of the group events.
  327. *
  328. * Required event and timerqueue update after a remote expiry:
  329. * -----------------------------------------------------------
  330. *
  331. * After expiring timers of a remote CPU, a walk through the hierarchy and
  332. * update of events and timerqueues is required. It is obviously needed if there
  333. * is a 'new' global timer but also if there is no new global timer but the
  334. * remote CPU is still idle.
  335. *
  336. * 1. CPU0 and CPU1 are idle and have both a global timer expiring at the same
  337. * time. So both have an event enqueued in the timerqueue of GRP0:0. CPU3 is
  338. * also idle and has no global timer pending. CPU2 is the only active CPU and
  339. * thus also the migrator:
  340. *
  341. * LVL 1 [GRP1:0]
  342. * migrator = GRP0:1
  343. * active = GRP0:1
  344. * --> timerqueue = evt-GRP0:0
  345. * / \
  346. * LVL 0 [GRP0:0] [GRP0:1]
  347. * migrator = TMIGR_NONE migrator = CPU2
  348. * active = active = CPU2
  349. * groupevt.ignore = false groupevt.ignore = true
  350. * groupevt.cpu = CPU0 groupevt.cpu =
  351. * timerqueue = evt-CPU0, timerqueue =
  352. * evt-CPU1
  353. * / \ / \
  354. * CPUs 0 1 2 3
  355. * idle idle active idle
  356. *
  357. * 2. CPU2 starts to expire remote timers. It starts with LVL0 group
  358. * GRP0:1. There is no event queued in the timerqueue, so CPU2 continues with
  359. * the parent of GRP0:1: GRP1:0. In GRP1:0 it dequeues the first event. It
  360. * looks at tmigr_event::cpu struct member and expires the pending timer(s)
  361. * of CPU0.
  362. *
  363. * LVL 1 [GRP1:0]
  364. * migrator = GRP0:1
  365. * active = GRP0:1
  366. * --> timerqueue =
  367. * / \
  368. * LVL 0 [GRP0:0] [GRP0:1]
  369. * migrator = TMIGR_NONE migrator = CPU2
  370. * active = active = CPU2
  371. * groupevt.ignore = false groupevt.ignore = true
  372. * --> groupevt.cpu = CPU0 groupevt.cpu =
  373. * timerqueue = evt-CPU0, timerqueue =
  374. * evt-CPU1
  375. * / \ / \
  376. * CPUs 0 1 2 3
  377. * idle idle active idle
  378. *
  379. * 3. Some work has to be done after expiring the timers of CPU0. If we stop
  380. * here, then CPU1's pending global timer(s) will not expire in time and the
  381. * timerqueue of GRP0:0 has still an event for CPU0 enqueued which has just
  382. * been processed. So it is required to walk the hierarchy from CPU0's point
  383. * of view and update it accordingly. CPU0's event will be removed from the
  384. * timerqueue because it has no pending timer. If CPU0 would have a timer
  385. * pending then it has to expire after CPU1's first timer because all timers
  386. * from this period were just expired. Either way CPU1's event will be first
  387. * in GRP0:0's timerqueue and therefore set in the CPU field of the group
  388. * event which is then enqueued in GRP1:0's timerqueue as GRP0:0 is still not
  389. * active:
  390. *
  391. * LVL 1 [GRP1:0]
  392. * migrator = GRP0:1
  393. * active = GRP0:1
  394. * --> timerqueue = evt-GRP0:0
  395. * / \
  396. * LVL 0 [GRP0:0] [GRP0:1]
  397. * migrator = TMIGR_NONE migrator = CPU2
  398. * active = active = CPU2
  399. * groupevt.ignore = false groupevt.ignore = true
  400. * --> groupevt.cpu = CPU1 groupevt.cpu =
  401. * --> timerqueue = evt-CPU1 timerqueue =
  402. * / \ / \
  403. * CPUs 0 1 2 3
  404. * idle idle active idle
  405. *
  406. * Now CPU2 (migrator) will continue step 2 at GRP1:0 and will expire the
  407. * timer(s) of CPU1.
  408. *
  409. * The hierarchy walk in step 3 can be skipped if the migrator notices that a
  410. * CPU of GRP0:0 is active again. The CPU will mark GRP0:0 active and take care
  411. * of the group as migrator and any needed updates within the hierarchy.
  412. */
  413. static DEFINE_MUTEX(tmigr_mutex);
  414. static struct list_head *tmigr_level_list __read_mostly;
  415. static unsigned int tmigr_hierarchy_levels __read_mostly;
  416. static unsigned int tmigr_crossnode_level __read_mostly;
  417. static DEFINE_PER_CPU(struct tmigr_cpu, tmigr_cpu);
  418. #define TMIGR_NONE 0xFF
  419. #define BIT_CNT 8
  420. static inline bool tmigr_is_not_available(struct tmigr_cpu *tmc)
  421. {
  422. return !(tmc->tmgroup && tmc->online);
  423. }
  424. /*
  425. * Returns true, when @childmask corresponds to the group migrator or when the
  426. * group is not active - so no migrator is set.
  427. */
  428. static bool tmigr_check_migrator(struct tmigr_group *group, u8 childmask)
  429. {
  430. union tmigr_state s;
  431. s.state = atomic_read(&group->migr_state);
  432. if ((s.migrator == childmask) || (s.migrator == TMIGR_NONE))
  433. return true;
  434. return false;
  435. }
  436. static bool tmigr_check_migrator_and_lonely(struct tmigr_group *group, u8 childmask)
  437. {
  438. bool lonely, migrator = false;
  439. unsigned long active;
  440. union tmigr_state s;
  441. s.state = atomic_read(&group->migr_state);
  442. if ((s.migrator == childmask) || (s.migrator == TMIGR_NONE))
  443. migrator = true;
  444. active = s.active;
  445. lonely = bitmap_weight(&active, BIT_CNT) <= 1;
  446. return (migrator && lonely);
  447. }
  448. static bool tmigr_check_lonely(struct tmigr_group *group)
  449. {
  450. unsigned long active;
  451. union tmigr_state s;
  452. s.state = atomic_read(&group->migr_state);
  453. active = s.active;
  454. return bitmap_weight(&active, BIT_CNT) <= 1;
  455. }
  456. /**
  457. * struct tmigr_walk - data required for walking the hierarchy
  458. * @nextexp: Next CPU event expiry information which is handed into
  459. * the timer migration code by the timer code
  460. * (get_next_timer_interrupt())
  461. * @firstexp: Contains the first event expiry information when
  462. * hierarchy is completely idle. When CPU itself was the
  463. * last going idle, information makes sure, that CPU will
  464. * be back in time. When using this value in the remote
  465. * expiry case, firstexp is stored in the per CPU tmigr_cpu
  466. * struct of CPU which expires remote timers. It is updated
  467. * in top level group only. Be aware, there could occur a
  468. * new top level of the hierarchy between the 'top level
  469. * call' in tmigr_update_events() and the check for the
  470. * parent group in walk_groups(). Then @firstexp might
  471. * contain a value != KTIME_MAX even if it was not the
  472. * final top level. This is not a problem, as the worst
  473. * outcome is a CPU which might wake up a little early.
  474. * @evt: Pointer to tmigr_event which needs to be queued (of idle
  475. * child group)
  476. * @childmask: groupmask of child group
  477. * @remote: Is set, when the new timer path is executed in
  478. * tmigr_handle_remote_cpu()
  479. * @basej: timer base in jiffies
  480. * @now: timer base monotonic
  481. * @check: is set if there is the need to handle remote timers;
  482. * required in tmigr_requires_handle_remote() only
  483. * @tmc_active: this flag indicates, whether the CPU which triggers
  484. * the hierarchy walk is !idle in the timer migration
  485. * hierarchy. When the CPU is idle and the whole hierarchy is
  486. * idle, only the first event of the top level has to be
  487. * considered.
  488. */
  489. struct tmigr_walk {
  490. u64 nextexp;
  491. u64 firstexp;
  492. struct tmigr_event *evt;
  493. u8 childmask;
  494. bool remote;
  495. unsigned long basej;
  496. u64 now;
  497. bool check;
  498. bool tmc_active;
  499. };
  500. typedef bool (*up_f)(struct tmigr_group *, struct tmigr_group *, struct tmigr_walk *);
  501. static void __walk_groups(up_f up, struct tmigr_walk *data,
  502. struct tmigr_cpu *tmc)
  503. {
  504. struct tmigr_group *child = NULL, *group = tmc->tmgroup;
  505. do {
  506. WARN_ON_ONCE(group->level >= tmigr_hierarchy_levels);
  507. if (up(group, child, data))
  508. break;
  509. child = group;
  510. /*
  511. * Pairs with the store release on group connection
  512. * to make sure group initialization is visible.
  513. */
  514. group = READ_ONCE(group->parent);
  515. data->childmask = child->groupmask;
  516. WARN_ON_ONCE(!data->childmask);
  517. } while (group);
  518. }
  519. static void walk_groups(up_f up, struct tmigr_walk *data, struct tmigr_cpu *tmc)
  520. {
  521. lockdep_assert_held(&tmc->lock);
  522. __walk_groups(up, data, tmc);
  523. }
  524. /*
  525. * Returns the next event of the timerqueue @group->events
  526. *
  527. * Removes timers with ignore flag and update next_expiry of the group. Values
  528. * of the group event are updated in tmigr_update_events() only.
  529. */
  530. static struct tmigr_event *tmigr_next_groupevt(struct tmigr_group *group)
  531. {
  532. struct timerqueue_node *node = NULL;
  533. struct tmigr_event *evt = NULL;
  534. lockdep_assert_held(&group->lock);
  535. WRITE_ONCE(group->next_expiry, KTIME_MAX);
  536. while ((node = timerqueue_getnext(&group->events))) {
  537. evt = container_of(node, struct tmigr_event, nextevt);
  538. if (!evt->ignore) {
  539. WRITE_ONCE(group->next_expiry, evt->nextevt.expires);
  540. return evt;
  541. }
  542. /*
  543. * Remove next timers with ignore flag, because the group lock
  544. * is held anyway
  545. */
  546. if (!timerqueue_del(&group->events, node))
  547. break;
  548. }
  549. return NULL;
  550. }
  551. /*
  552. * Return the next event (with the expiry equal or before @now)
  553. *
  554. * Event, which is returned, is also removed from the queue.
  555. */
  556. static struct tmigr_event *tmigr_next_expired_groupevt(struct tmigr_group *group,
  557. u64 now)
  558. {
  559. struct tmigr_event *evt = tmigr_next_groupevt(group);
  560. if (!evt || now < evt->nextevt.expires)
  561. return NULL;
  562. /*
  563. * The event is ready to expire. Remove it and update next group event.
  564. */
  565. timerqueue_del(&group->events, &evt->nextevt);
  566. tmigr_next_groupevt(group);
  567. return evt;
  568. }
  569. static u64 tmigr_next_groupevt_expires(struct tmigr_group *group)
  570. {
  571. struct tmigr_event *evt;
  572. evt = tmigr_next_groupevt(group);
  573. if (!evt)
  574. return KTIME_MAX;
  575. else
  576. return evt->nextevt.expires;
  577. }
  578. static bool tmigr_active_up(struct tmigr_group *group,
  579. struct tmigr_group *child,
  580. struct tmigr_walk *data)
  581. {
  582. union tmigr_state curstate, newstate;
  583. bool walk_done;
  584. u8 childmask;
  585. childmask = data->childmask;
  586. /*
  587. * No memory barrier is required here in contrast to
  588. * tmigr_inactive_up(), as the group state change does not depend on the
  589. * child state.
  590. */
  591. curstate.state = atomic_read(&group->migr_state);
  592. do {
  593. newstate = curstate;
  594. walk_done = true;
  595. if (newstate.migrator == TMIGR_NONE) {
  596. newstate.migrator = childmask;
  597. /* Changes need to be propagated */
  598. walk_done = false;
  599. }
  600. newstate.active |= childmask;
  601. newstate.seq++;
  602. } while (!atomic_try_cmpxchg(&group->migr_state, &curstate.state, newstate.state));
  603. trace_tmigr_group_set_cpu_active(group, newstate, childmask);
  604. /*
  605. * The group is active (again). The group event might be still queued
  606. * into the parent group's timerqueue but can now be handled by the
  607. * migrator of this group. Therefore the ignore flag for the group event
  608. * is updated to reflect this.
  609. *
  610. * The update of the ignore flag in the active path is done lockless. In
  611. * worst case the migrator of the parent group observes the change too
  612. * late and expires remotely all events belonging to this group. The
  613. * lock is held while updating the ignore flag in idle path. So this
  614. * state change will not be lost.
  615. */
  616. group->groupevt.ignore = true;
  617. return walk_done;
  618. }
  619. static void __tmigr_cpu_activate(struct tmigr_cpu *tmc)
  620. {
  621. struct tmigr_walk data;
  622. data.childmask = tmc->groupmask;
  623. trace_tmigr_cpu_active(tmc);
  624. tmc->cpuevt.ignore = true;
  625. WRITE_ONCE(tmc->wakeup, KTIME_MAX);
  626. walk_groups(&tmigr_active_up, &data, tmc);
  627. }
  628. /**
  629. * tmigr_cpu_activate() - set this CPU active in timer migration hierarchy
  630. *
  631. * Call site timer_clear_idle() is called with interrupts disabled.
  632. */
  633. void tmigr_cpu_activate(void)
  634. {
  635. struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
  636. if (tmigr_is_not_available(tmc))
  637. return;
  638. if (WARN_ON_ONCE(!tmc->idle))
  639. return;
  640. raw_spin_lock(&tmc->lock);
  641. tmc->idle = false;
  642. __tmigr_cpu_activate(tmc);
  643. raw_spin_unlock(&tmc->lock);
  644. }
  645. /*
  646. * Returns true, if there is nothing to be propagated to the next level
  647. *
  648. * @data->firstexp is set to expiry of first gobal event of the (top level of
  649. * the) hierarchy, but only when hierarchy is completely idle.
  650. *
  651. * The child and group states need to be read under the lock, to prevent a race
  652. * against a concurrent tmigr_inactive_up() run when the last CPU goes idle. See
  653. * also section "Prevent race between new event and last CPU going inactive" in
  654. * the documentation at the top.
  655. *
  656. * This is the only place where the group event expiry value is set.
  657. */
  658. static
  659. bool tmigr_update_events(struct tmigr_group *group, struct tmigr_group *child,
  660. struct tmigr_walk *data)
  661. {
  662. struct tmigr_event *evt, *first_childevt;
  663. union tmigr_state childstate, groupstate;
  664. bool remote = data->remote;
  665. bool walk_done = false;
  666. u64 nextexp;
  667. if (child) {
  668. raw_spin_lock(&child->lock);
  669. raw_spin_lock_nested(&group->lock, SINGLE_DEPTH_NESTING);
  670. childstate.state = atomic_read(&child->migr_state);
  671. groupstate.state = atomic_read(&group->migr_state);
  672. if (childstate.active) {
  673. walk_done = true;
  674. goto unlock;
  675. }
  676. first_childevt = tmigr_next_groupevt(child);
  677. nextexp = child->next_expiry;
  678. evt = &child->groupevt;
  679. evt->ignore = (nextexp == KTIME_MAX) ? true : false;
  680. } else {
  681. nextexp = data->nextexp;
  682. first_childevt = evt = data->evt;
  683. /*
  684. * Walking the hierarchy is required in any case when a
  685. * remote expiry was done before. This ensures to not lose
  686. * already queued events in non active groups (see section
  687. * "Required event and timerqueue update after a remote
  688. * expiry" in the documentation at the top).
  689. *
  690. * The two call sites which are executed without a remote expiry
  691. * before, are not prevented from propagating changes through
  692. * the hierarchy by the return:
  693. * - When entering this path by tmigr_new_timer(), @evt->ignore
  694. * is never set.
  695. * - tmigr_inactive_up() takes care of the propagation by
  696. * itself and ignores the return value. But an immediate
  697. * return is possible if there is a parent, sparing group
  698. * locking at this level, because the upper walking call to
  699. * the parent will take care about removing this event from
  700. * within the group and update next_expiry accordingly.
  701. *
  702. * However if there is no parent, ie: the hierarchy has only a
  703. * single level so @group is the top level group, make sure the
  704. * first event information of the group is updated properly and
  705. * also handled properly, so skip this fast return path.
  706. */
  707. if (evt->ignore && !remote && group->parent)
  708. return true;
  709. raw_spin_lock(&group->lock);
  710. childstate.state = 0;
  711. groupstate.state = atomic_read(&group->migr_state);
  712. }
  713. /*
  714. * If the child event is already queued in the group, remove it from the
  715. * queue when the expiry time changed only or when it could be ignored.
  716. */
  717. if (timerqueue_node_queued(&evt->nextevt)) {
  718. if ((evt->nextevt.expires == nextexp) && !evt->ignore) {
  719. /* Make sure not to miss a new CPU event with the same expiry */
  720. evt->cpu = first_childevt->cpu;
  721. goto check_toplvl;
  722. }
  723. if (!timerqueue_del(&group->events, &evt->nextevt))
  724. WRITE_ONCE(group->next_expiry, KTIME_MAX);
  725. }
  726. if (evt->ignore) {
  727. /*
  728. * When the next child event could be ignored (nextexp is
  729. * KTIME_MAX) and there was no remote timer handling before or
  730. * the group is already active, there is no need to walk the
  731. * hierarchy even if there is a parent group.
  732. *
  733. * The other way round: even if the event could be ignored, but
  734. * if a remote timer handling was executed before and the group
  735. * is not active, walking the hierarchy is required to not miss
  736. * an enqueued timer in the non active group. The enqueued timer
  737. * of the group needs to be propagated to a higher level to
  738. * ensure it is handled.
  739. */
  740. if (!remote || groupstate.active)
  741. walk_done = true;
  742. } else {
  743. evt->nextevt.expires = nextexp;
  744. evt->cpu = first_childevt->cpu;
  745. if (timerqueue_add(&group->events, &evt->nextevt))
  746. WRITE_ONCE(group->next_expiry, nextexp);
  747. }
  748. check_toplvl:
  749. if (!group->parent && (groupstate.migrator == TMIGR_NONE)) {
  750. walk_done = true;
  751. /*
  752. * Nothing to do when update was done during remote timer
  753. * handling. First timer in top level group which needs to be
  754. * handled when top level group is not active, is calculated
  755. * directly in tmigr_handle_remote_up().
  756. */
  757. if (remote)
  758. goto unlock;
  759. /*
  760. * The top level group is idle and it has to be ensured the
  761. * global timers are handled in time. (This could be optimized
  762. * by keeping track of the last global scheduled event and only
  763. * arming it on the CPU if the new event is earlier. Not sure if
  764. * its worth the complexity.)
  765. */
  766. data->firstexp = tmigr_next_groupevt_expires(group);
  767. }
  768. trace_tmigr_update_events(child, group, childstate, groupstate,
  769. nextexp);
  770. unlock:
  771. raw_spin_unlock(&group->lock);
  772. if (child)
  773. raw_spin_unlock(&child->lock);
  774. return walk_done;
  775. }
  776. static bool tmigr_new_timer_up(struct tmigr_group *group,
  777. struct tmigr_group *child,
  778. struct tmigr_walk *data)
  779. {
  780. return tmigr_update_events(group, child, data);
  781. }
  782. /*
  783. * Returns the expiry of the next timer that needs to be handled. KTIME_MAX is
  784. * returned, if an active CPU will handle all the timer migration hierarchy
  785. * timers.
  786. */
  787. static u64 tmigr_new_timer(struct tmigr_cpu *tmc, u64 nextexp)
  788. {
  789. struct tmigr_walk data = { .nextexp = nextexp,
  790. .firstexp = KTIME_MAX,
  791. .evt = &tmc->cpuevt };
  792. lockdep_assert_held(&tmc->lock);
  793. if (tmc->remote)
  794. return KTIME_MAX;
  795. trace_tmigr_cpu_new_timer(tmc);
  796. tmc->cpuevt.ignore = false;
  797. data.remote = false;
  798. walk_groups(&tmigr_new_timer_up, &data, tmc);
  799. /* If there is a new first global event, make sure it is handled */
  800. return data.firstexp;
  801. }
  802. static void tmigr_handle_remote_cpu(unsigned int cpu, u64 now,
  803. unsigned long jif)
  804. {
  805. struct timer_events tevt;
  806. struct tmigr_walk data;
  807. struct tmigr_cpu *tmc;
  808. tmc = per_cpu_ptr(&tmigr_cpu, cpu);
  809. raw_spin_lock_irq(&tmc->lock);
  810. /*
  811. * If the remote CPU is offline then the timers have been migrated to
  812. * another CPU.
  813. *
  814. * If tmigr_cpu::remote is set, at the moment another CPU already
  815. * expires the timers of the remote CPU.
  816. *
  817. * If tmigr_event::ignore is set, then the CPU returns from idle and
  818. * takes care of its timers.
  819. *
  820. * If the next event expires in the future, then the event has been
  821. * updated and there are no timers to expire right now. The CPU which
  822. * updated the event takes care when hierarchy is completely
  823. * idle. Otherwise the migrator does it as the event is enqueued.
  824. */
  825. if (!tmc->online || tmc->remote || tmc->cpuevt.ignore ||
  826. now < tmc->cpuevt.nextevt.expires) {
  827. raw_spin_unlock_irq(&tmc->lock);
  828. return;
  829. }
  830. trace_tmigr_handle_remote_cpu(tmc);
  831. tmc->remote = true;
  832. WRITE_ONCE(tmc->wakeup, KTIME_MAX);
  833. /* Drop the lock to allow the remote CPU to exit idle */
  834. raw_spin_unlock_irq(&tmc->lock);
  835. if (cpu != smp_processor_id())
  836. timer_expire_remote(cpu);
  837. /*
  838. * Lock ordering needs to be preserved - timer_base locks before tmigr
  839. * related locks (see section "Locking rules" in the documentation at
  840. * the top). During fetching the next timer interrupt, also tmc->lock
  841. * needs to be held. Otherwise there is a possible race window against
  842. * the CPU itself when it comes out of idle, updates the first timer in
  843. * the hierarchy and goes back to idle.
  844. *
  845. * timer base locks are dropped as fast as possible: After checking
  846. * whether the remote CPU went offline in the meantime and after
  847. * fetching the next remote timer interrupt. Dropping the locks as fast
  848. * as possible keeps the locking region small and prevents holding
  849. * several (unnecessary) locks during walking the hierarchy for updating
  850. * the timerqueue and group events.
  851. */
  852. local_irq_disable();
  853. timer_lock_remote_bases(cpu);
  854. raw_spin_lock(&tmc->lock);
  855. /*
  856. * When the CPU went offline in the meantime, no hierarchy walk has to
  857. * be done for updating the queued events, because the walk was
  858. * already done during marking the CPU offline in the hierarchy.
  859. *
  860. * When the CPU is no longer idle, the CPU takes care of the timers and
  861. * also of the timers in the hierarchy.
  862. *
  863. * (See also section "Required event and timerqueue update after a
  864. * remote expiry" in the documentation at the top)
  865. */
  866. if (!tmc->online || !tmc->idle) {
  867. timer_unlock_remote_bases(cpu);
  868. goto unlock;
  869. }
  870. /* next event of CPU */
  871. fetch_next_timer_interrupt_remote(jif, now, &tevt, cpu);
  872. timer_unlock_remote_bases(cpu);
  873. data.nextexp = tevt.global;
  874. data.firstexp = KTIME_MAX;
  875. data.evt = &tmc->cpuevt;
  876. data.remote = true;
  877. /*
  878. * The update is done even when there is no 'new' global timer pending
  879. * on the remote CPU (see section "Required event and timerqueue update
  880. * after a remote expiry" in the documentation at the top)
  881. */
  882. walk_groups(&tmigr_new_timer_up, &data, tmc);
  883. unlock:
  884. tmc->remote = false;
  885. raw_spin_unlock_irq(&tmc->lock);
  886. }
  887. static bool tmigr_handle_remote_up(struct tmigr_group *group,
  888. struct tmigr_group *child,
  889. struct tmigr_walk *data)
  890. {
  891. struct tmigr_event *evt;
  892. unsigned long jif;
  893. u8 childmask;
  894. u64 now;
  895. jif = data->basej;
  896. now = data->now;
  897. childmask = data->childmask;
  898. trace_tmigr_handle_remote(group);
  899. again:
  900. /*
  901. * Handle the group only if @childmask is the migrator or if the
  902. * group has no migrator. Otherwise the group is active and is
  903. * handled by its own migrator.
  904. */
  905. if (!tmigr_check_migrator(group, childmask))
  906. return true;
  907. raw_spin_lock_irq(&group->lock);
  908. evt = tmigr_next_expired_groupevt(group, now);
  909. if (evt) {
  910. unsigned int remote_cpu = evt->cpu;
  911. raw_spin_unlock_irq(&group->lock);
  912. tmigr_handle_remote_cpu(remote_cpu, now, jif);
  913. /* check if there is another event, that needs to be handled */
  914. goto again;
  915. }
  916. /*
  917. * Keep track of the expiry of the first event that needs to be handled
  918. * (group->next_expiry was updated by tmigr_next_expired_groupevt(),
  919. * next was set by tmigr_handle_remote_cpu()).
  920. */
  921. data->firstexp = group->next_expiry;
  922. raw_spin_unlock_irq(&group->lock);
  923. return false;
  924. }
  925. /**
  926. * tmigr_handle_remote() - Handle global timers of remote idle CPUs
  927. *
  928. * Called from the timer soft interrupt with interrupts enabled.
  929. */
  930. void tmigr_handle_remote(void)
  931. {
  932. struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
  933. struct tmigr_walk data;
  934. if (tmigr_is_not_available(tmc))
  935. return;
  936. data.childmask = tmc->groupmask;
  937. data.firstexp = KTIME_MAX;
  938. /*
  939. * NOTE: This is a doubled check because the migrator test will be done
  940. * in tmigr_handle_remote_up() anyway. Keep this check to speed up the
  941. * return when nothing has to be done.
  942. */
  943. if (!tmigr_check_migrator(tmc->tmgroup, tmc->groupmask)) {
  944. /*
  945. * If this CPU was an idle migrator, make sure to clear its wakeup
  946. * value so it won't chase timers that have already expired elsewhere.
  947. * This avoids endless requeue from tmigr_new_timer().
  948. */
  949. if (READ_ONCE(tmc->wakeup) == KTIME_MAX)
  950. return;
  951. }
  952. data.now = get_jiffies_update(&data.basej);
  953. /*
  954. * Update @tmc->wakeup only at the end and do not reset @tmc->wakeup to
  955. * KTIME_MAX. Even if tmc->lock is not held during the whole remote
  956. * handling, tmc->wakeup is fine to be stale as it is called in
  957. * interrupt context and tick_nohz_next_event() is executed in interrupt
  958. * exit path only after processing the last pending interrupt.
  959. */
  960. __walk_groups(&tmigr_handle_remote_up, &data, tmc);
  961. raw_spin_lock_irq(&tmc->lock);
  962. WRITE_ONCE(tmc->wakeup, data.firstexp);
  963. raw_spin_unlock_irq(&tmc->lock);
  964. }
  965. static bool tmigr_requires_handle_remote_up(struct tmigr_group *group,
  966. struct tmigr_group *child,
  967. struct tmigr_walk *data)
  968. {
  969. u8 childmask;
  970. childmask = data->childmask;
  971. /*
  972. * Handle the group only if the child is the migrator or if the group
  973. * has no migrator. Otherwise the group is active and is handled by its
  974. * own migrator.
  975. */
  976. if (!tmigr_check_migrator(group, childmask))
  977. return true;
  978. /*
  979. * When there is a parent group and the CPU which triggered the
  980. * hierarchy walk is not active, proceed the walk to reach the top level
  981. * group before reading the next_expiry value.
  982. */
  983. if (group->parent && !data->tmc_active)
  984. return false;
  985. /*
  986. * The lock is required on 32bit architectures to read the variable
  987. * consistently with a concurrent writer. On 64bit the lock is not
  988. * required because the read operation is not split and so it is always
  989. * consistent.
  990. */
  991. if (IS_ENABLED(CONFIG_64BIT)) {
  992. data->firstexp = READ_ONCE(group->next_expiry);
  993. if (data->now >= data->firstexp) {
  994. data->check = true;
  995. return true;
  996. }
  997. } else {
  998. raw_spin_lock(&group->lock);
  999. data->firstexp = group->next_expiry;
  1000. if (data->now >= group->next_expiry) {
  1001. data->check = true;
  1002. raw_spin_unlock(&group->lock);
  1003. return true;
  1004. }
  1005. raw_spin_unlock(&group->lock);
  1006. }
  1007. return false;
  1008. }
  1009. /**
  1010. * tmigr_requires_handle_remote() - Check the need of remote timer handling
  1011. *
  1012. * Must be called with interrupts disabled.
  1013. */
  1014. bool tmigr_requires_handle_remote(void)
  1015. {
  1016. struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
  1017. struct tmigr_walk data;
  1018. unsigned long jif;
  1019. bool ret = false;
  1020. if (tmigr_is_not_available(tmc))
  1021. return ret;
  1022. data.now = get_jiffies_update(&jif);
  1023. data.childmask = tmc->groupmask;
  1024. data.firstexp = KTIME_MAX;
  1025. data.tmc_active = !tmc->idle;
  1026. data.check = false;
  1027. /*
  1028. * If the CPU is active, walk the hierarchy to check whether a remote
  1029. * expiry is required.
  1030. *
  1031. * Check is done lockless as interrupts are disabled and @tmc->idle is
  1032. * set only by the local CPU.
  1033. */
  1034. if (!tmc->idle) {
  1035. __walk_groups(&tmigr_requires_handle_remote_up, &data, tmc);
  1036. return data.check;
  1037. }
  1038. /*
  1039. * When the CPU is idle, compare @tmc->wakeup with @data.now. The lock
  1040. * is required on 32bit architectures to read the variable consistently
  1041. * with a concurrent writer. On 64bit the lock is not required because
  1042. * the read operation is not split and so it is always consistent.
  1043. */
  1044. if (IS_ENABLED(CONFIG_64BIT)) {
  1045. if (data.now >= READ_ONCE(tmc->wakeup))
  1046. return true;
  1047. } else {
  1048. raw_spin_lock(&tmc->lock);
  1049. if (data.now >= tmc->wakeup)
  1050. ret = true;
  1051. raw_spin_unlock(&tmc->lock);
  1052. }
  1053. return ret;
  1054. }
  1055. /**
  1056. * tmigr_cpu_new_timer() - enqueue next global timer into hierarchy (idle tmc)
  1057. * @nextexp: Next expiry of global timer (or KTIME_MAX if not)
  1058. *
  1059. * The CPU is already deactivated in the timer migration
  1060. * hierarchy. tick_nohz_get_sleep_length() calls tick_nohz_next_event()
  1061. * and thereby the timer idle path is executed once more. @tmc->wakeup
  1062. * holds the first timer, when the timer migration hierarchy is
  1063. * completely idle.
  1064. *
  1065. * Returns the first timer that needs to be handled by this CPU or KTIME_MAX if
  1066. * nothing needs to be done.
  1067. */
  1068. u64 tmigr_cpu_new_timer(u64 nextexp)
  1069. {
  1070. struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
  1071. u64 ret;
  1072. if (tmigr_is_not_available(tmc))
  1073. return nextexp;
  1074. raw_spin_lock(&tmc->lock);
  1075. ret = READ_ONCE(tmc->wakeup);
  1076. if (nextexp != KTIME_MAX) {
  1077. if (nextexp != tmc->cpuevt.nextevt.expires ||
  1078. tmc->cpuevt.ignore) {
  1079. ret = tmigr_new_timer(tmc, nextexp);
  1080. /*
  1081. * Make sure the reevaluation of timers in idle path
  1082. * will not miss an event.
  1083. */
  1084. WRITE_ONCE(tmc->wakeup, ret);
  1085. }
  1086. }
  1087. trace_tmigr_cpu_new_timer_idle(tmc, nextexp);
  1088. raw_spin_unlock(&tmc->lock);
  1089. return ret;
  1090. }
  1091. static bool tmigr_inactive_up(struct tmigr_group *group,
  1092. struct tmigr_group *child,
  1093. struct tmigr_walk *data)
  1094. {
  1095. union tmigr_state curstate, newstate, childstate;
  1096. bool walk_done;
  1097. u8 childmask;
  1098. childmask = data->childmask;
  1099. childstate.state = 0;
  1100. /*
  1101. * The memory barrier is paired with the cmpxchg() in tmigr_active_up()
  1102. * to make sure the updates of child and group states are ordered. The
  1103. * ordering is mandatory, as the group state change depends on the child
  1104. * state.
  1105. */
  1106. curstate.state = atomic_read_acquire(&group->migr_state);
  1107. for (;;) {
  1108. if (child)
  1109. childstate.state = atomic_read(&child->migr_state);
  1110. newstate = curstate;
  1111. walk_done = true;
  1112. /* Reset active bit when the child is no longer active */
  1113. if (!childstate.active)
  1114. newstate.active &= ~childmask;
  1115. if (newstate.migrator == childmask) {
  1116. /*
  1117. * Find a new migrator for the group, because the child
  1118. * group is idle!
  1119. */
  1120. if (!childstate.active) {
  1121. unsigned long new_migr_bit, active = newstate.active;
  1122. new_migr_bit = find_first_bit(&active, BIT_CNT);
  1123. if (new_migr_bit != BIT_CNT) {
  1124. newstate.migrator = BIT(new_migr_bit);
  1125. } else {
  1126. newstate.migrator = TMIGR_NONE;
  1127. /* Changes need to be propagated */
  1128. walk_done = false;
  1129. }
  1130. }
  1131. }
  1132. newstate.seq++;
  1133. WARN_ON_ONCE((newstate.migrator != TMIGR_NONE) && !(newstate.active));
  1134. if (atomic_try_cmpxchg(&group->migr_state, &curstate.state, newstate.state)) {
  1135. trace_tmigr_group_set_cpu_inactive(group, newstate, childmask);
  1136. break;
  1137. }
  1138. /*
  1139. * The memory barrier is paired with the cmpxchg() in
  1140. * tmigr_active_up() to make sure the updates of child and group
  1141. * states are ordered. It is required only when the above
  1142. * try_cmpxchg() fails.
  1143. */
  1144. smp_mb__after_atomic();
  1145. }
  1146. data->remote = false;
  1147. /* Event Handling */
  1148. tmigr_update_events(group, child, data);
  1149. return walk_done;
  1150. }
  1151. static u64 __tmigr_cpu_deactivate(struct tmigr_cpu *tmc, u64 nextexp)
  1152. {
  1153. struct tmigr_walk data = { .nextexp = nextexp,
  1154. .firstexp = KTIME_MAX,
  1155. .evt = &tmc->cpuevt,
  1156. .childmask = tmc->groupmask };
  1157. /*
  1158. * If nextexp is KTIME_MAX, the CPU event will be ignored because the
  1159. * local timer expires before the global timer, no global timer is set
  1160. * or CPU goes offline.
  1161. */
  1162. if (nextexp != KTIME_MAX)
  1163. tmc->cpuevt.ignore = false;
  1164. walk_groups(&tmigr_inactive_up, &data, tmc);
  1165. return data.firstexp;
  1166. }
  1167. /**
  1168. * tmigr_cpu_deactivate() - Put current CPU into inactive state
  1169. * @nextexp: The next global timer expiry of the current CPU
  1170. *
  1171. * Must be called with interrupts disabled.
  1172. *
  1173. * Return: the next event expiry of the current CPU or the next event expiry
  1174. * from the hierarchy if this CPU is the top level migrator or the hierarchy is
  1175. * completely idle.
  1176. */
  1177. u64 tmigr_cpu_deactivate(u64 nextexp)
  1178. {
  1179. struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
  1180. u64 ret;
  1181. if (tmigr_is_not_available(tmc))
  1182. return nextexp;
  1183. raw_spin_lock(&tmc->lock);
  1184. ret = __tmigr_cpu_deactivate(tmc, nextexp);
  1185. tmc->idle = true;
  1186. /*
  1187. * Make sure the reevaluation of timers in idle path will not miss an
  1188. * event.
  1189. */
  1190. WRITE_ONCE(tmc->wakeup, ret);
  1191. trace_tmigr_cpu_idle(tmc, nextexp);
  1192. raw_spin_unlock(&tmc->lock);
  1193. return ret;
  1194. }
  1195. /**
  1196. * tmigr_quick_check() - Quick forecast of next tmigr event when CPU wants to
  1197. * go idle
  1198. * @nextevt: The next global timer expiry of the current CPU
  1199. *
  1200. * Return:
  1201. * * KTIME_MAX - when it is probable that nothing has to be done (not
  1202. * the only one in the level 0 group; and if it is the
  1203. * only one in level 0 group, but there are more than a
  1204. * single group active on the way to top level)
  1205. * * nextevt - when CPU is offline and has to handle timer on its own
  1206. * or when on the way to top in every group only a single
  1207. * child is active but @nextevt is before the lowest
  1208. * next_expiry encountered while walking up to top level.
  1209. * * next_expiry - value of lowest expiry encountered while walking groups
  1210. * if only a single child is active on each and @nextevt
  1211. * is after this lowest expiry.
  1212. */
  1213. u64 tmigr_quick_check(u64 nextevt)
  1214. {
  1215. struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
  1216. struct tmigr_group *group = tmc->tmgroup;
  1217. if (tmigr_is_not_available(tmc))
  1218. return nextevt;
  1219. if (WARN_ON_ONCE(tmc->idle))
  1220. return nextevt;
  1221. if (!tmigr_check_migrator_and_lonely(tmc->tmgroup, tmc->groupmask))
  1222. return KTIME_MAX;
  1223. do {
  1224. if (!tmigr_check_lonely(group)) {
  1225. return KTIME_MAX;
  1226. } else {
  1227. /*
  1228. * Since current CPU is active, events may not be sorted
  1229. * from bottom to the top because the CPU's event is ignored
  1230. * up to the top and its sibling's events not propagated upwards.
  1231. * Thus keep track of the lowest observed expiry.
  1232. */
  1233. nextevt = min_t(u64, nextevt, READ_ONCE(group->next_expiry));
  1234. if (!group->parent)
  1235. return nextevt;
  1236. }
  1237. group = group->parent;
  1238. } while (group);
  1239. return KTIME_MAX;
  1240. }
  1241. /*
  1242. * tmigr_trigger_active() - trigger a CPU to become active again
  1243. *
  1244. * This function is executed on a CPU which is part of cpu_online_mask, when the
  1245. * last active CPU in the hierarchy is offlining. With this, it is ensured that
  1246. * the other CPU is active and takes over the migrator duty.
  1247. */
  1248. static long tmigr_trigger_active(void *unused)
  1249. {
  1250. struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
  1251. WARN_ON_ONCE(!tmc->online || tmc->idle);
  1252. return 0;
  1253. }
  1254. static int tmigr_cpu_offline(unsigned int cpu)
  1255. {
  1256. struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
  1257. int migrator;
  1258. u64 firstexp;
  1259. raw_spin_lock_irq(&tmc->lock);
  1260. tmc->online = false;
  1261. WRITE_ONCE(tmc->wakeup, KTIME_MAX);
  1262. /*
  1263. * CPU has to handle the local events on his own, when on the way to
  1264. * offline; Therefore nextevt value is set to KTIME_MAX
  1265. */
  1266. firstexp = __tmigr_cpu_deactivate(tmc, KTIME_MAX);
  1267. trace_tmigr_cpu_offline(tmc);
  1268. raw_spin_unlock_irq(&tmc->lock);
  1269. if (firstexp != KTIME_MAX) {
  1270. migrator = cpumask_any_but(cpu_online_mask, cpu);
  1271. work_on_cpu(migrator, tmigr_trigger_active, NULL);
  1272. }
  1273. return 0;
  1274. }
  1275. static int tmigr_cpu_online(unsigned int cpu)
  1276. {
  1277. struct tmigr_cpu *tmc = this_cpu_ptr(&tmigr_cpu);
  1278. /* Check whether CPU data was successfully initialized */
  1279. if (WARN_ON_ONCE(!tmc->tmgroup))
  1280. return -EINVAL;
  1281. raw_spin_lock_irq(&tmc->lock);
  1282. trace_tmigr_cpu_online(tmc);
  1283. tmc->idle = timer_base_is_idle();
  1284. if (!tmc->idle)
  1285. __tmigr_cpu_activate(tmc);
  1286. tmc->online = true;
  1287. raw_spin_unlock_irq(&tmc->lock);
  1288. return 0;
  1289. }
  1290. static void tmigr_init_group(struct tmigr_group *group, unsigned int lvl,
  1291. int node)
  1292. {
  1293. union tmigr_state s;
  1294. raw_spin_lock_init(&group->lock);
  1295. group->level = lvl;
  1296. group->numa_node = lvl < tmigr_crossnode_level ? node : NUMA_NO_NODE;
  1297. group->num_children = 0;
  1298. s.migrator = TMIGR_NONE;
  1299. s.active = 0;
  1300. s.seq = 0;
  1301. atomic_set(&group->migr_state, s.state);
  1302. /*
  1303. * If this is a new top-level, prepare its groupmask in advance.
  1304. * This avoids accidents where yet another new top-level is
  1305. * created in the future and made visible before the current groupmask.
  1306. */
  1307. if (list_empty(&tmigr_level_list[lvl])) {
  1308. group->groupmask = BIT(0);
  1309. /*
  1310. * The previous top level has prepared its groupmask already,
  1311. * simply account it as the first child.
  1312. */
  1313. if (lvl > 0)
  1314. group->num_children = 1;
  1315. }
  1316. timerqueue_init_head(&group->events);
  1317. timerqueue_init(&group->groupevt.nextevt);
  1318. group->groupevt.nextevt.expires = KTIME_MAX;
  1319. WRITE_ONCE(group->next_expiry, KTIME_MAX);
  1320. group->groupevt.ignore = true;
  1321. }
  1322. static struct tmigr_group *tmigr_get_group(unsigned int cpu, int node,
  1323. unsigned int lvl)
  1324. {
  1325. struct tmigr_group *tmp, *group = NULL;
  1326. lockdep_assert_held(&tmigr_mutex);
  1327. /* Try to attach to an existing group first */
  1328. list_for_each_entry(tmp, &tmigr_level_list[lvl], list) {
  1329. /*
  1330. * If @lvl is below the cross NUMA node level, check whether
  1331. * this group belongs to the same NUMA node.
  1332. */
  1333. if (lvl < tmigr_crossnode_level && tmp->numa_node != node)
  1334. continue;
  1335. /* Capacity left? */
  1336. if (tmp->num_children >= TMIGR_CHILDREN_PER_GROUP)
  1337. continue;
  1338. /*
  1339. * TODO: A possible further improvement: Make sure that all CPU
  1340. * siblings end up in the same group of the lowest level of the
  1341. * hierarchy. Rely on the topology sibling mask would be a
  1342. * reasonable solution.
  1343. */
  1344. group = tmp;
  1345. break;
  1346. }
  1347. if (group)
  1348. return group;
  1349. /* Allocate and set up a new group */
  1350. group = kzalloc_node(sizeof(*group), GFP_KERNEL, node);
  1351. if (!group)
  1352. return ERR_PTR(-ENOMEM);
  1353. tmigr_init_group(group, lvl, node);
  1354. /* Setup successful. Add it to the hierarchy */
  1355. list_add(&group->list, &tmigr_level_list[lvl]);
  1356. trace_tmigr_group_set(group);
  1357. return group;
  1358. }
  1359. static void tmigr_connect_child_parent(struct tmigr_group *child,
  1360. struct tmigr_group *parent,
  1361. bool activate)
  1362. {
  1363. struct tmigr_walk data;
  1364. raw_spin_lock_irq(&child->lock);
  1365. raw_spin_lock_nested(&parent->lock, SINGLE_DEPTH_NESTING);
  1366. if (activate) {
  1367. /*
  1368. * @child is the old top and @parent the new one. In this
  1369. * case groupmask is pre-initialized and @child already
  1370. * accounted, along with its new sibling corresponding to the
  1371. * CPU going up.
  1372. */
  1373. WARN_ON_ONCE(child->groupmask != BIT(0) || parent->num_children != 2);
  1374. } else {
  1375. /* Adding @child for the CPU going up to @parent. */
  1376. child->groupmask = BIT(parent->num_children++);
  1377. }
  1378. /*
  1379. * Make sure parent initialization is visible before publishing it to a
  1380. * racing CPU entering/exiting idle. This RELEASE barrier enforces an
  1381. * address dependency that pairs with the READ_ONCE() in __walk_groups().
  1382. */
  1383. smp_store_release(&child->parent, parent);
  1384. raw_spin_unlock(&parent->lock);
  1385. raw_spin_unlock_irq(&child->lock);
  1386. trace_tmigr_connect_child_parent(child);
  1387. if (!activate)
  1388. return;
  1389. /*
  1390. * To prevent inconsistent states, active children need to be active in
  1391. * the new parent as well. Inactive children are already marked inactive
  1392. * in the parent group:
  1393. *
  1394. * * When new groups were created by tmigr_setup_groups() starting from
  1395. * the lowest level (and not higher then one level below the current
  1396. * top level), then they are not active. They will be set active when
  1397. * the new online CPU comes active.
  1398. *
  1399. * * But if a new group above the current top level is required, it is
  1400. * mandatory to propagate the active state of the already existing
  1401. * child to the new parent. So tmigr_connect_child_parent() is
  1402. * executed with the formerly top level group (child) and the newly
  1403. * created group (parent).
  1404. *
  1405. * * It is ensured that the child is active, as this setup path is
  1406. * executed in hotplug prepare callback. This is exectued by an
  1407. * already connected and !idle CPU. Even if all other CPUs go idle,
  1408. * the CPU executing the setup will be responsible up to current top
  1409. * level group. And the next time it goes inactive, it will release
  1410. * the new childmask and parent to subsequent walkers through this
  1411. * @child. Therefore propagate active state unconditionally.
  1412. */
  1413. data.childmask = child->groupmask;
  1414. /*
  1415. * There is only one new level per time (which is protected by
  1416. * tmigr_mutex). When connecting the child and the parent and set the
  1417. * child active when the parent is inactive, the parent needs to be the
  1418. * uppermost level. Otherwise there went something wrong!
  1419. */
  1420. WARN_ON(!tmigr_active_up(parent, child, &data) && parent->parent);
  1421. }
  1422. static int tmigr_setup_groups(unsigned int cpu, unsigned int node)
  1423. {
  1424. struct tmigr_group *group, *child, **stack;
  1425. int top = 0, err = 0, i = 0;
  1426. struct list_head *lvllist;
  1427. stack = kcalloc(tmigr_hierarchy_levels, sizeof(*stack), GFP_KERNEL);
  1428. if (!stack)
  1429. return -ENOMEM;
  1430. do {
  1431. group = tmigr_get_group(cpu, node, i);
  1432. if (IS_ERR(group)) {
  1433. err = PTR_ERR(group);
  1434. break;
  1435. }
  1436. top = i;
  1437. stack[i++] = group;
  1438. /*
  1439. * When booting only less CPUs of a system than CPUs are
  1440. * available, not all calculated hierarchy levels are required.
  1441. *
  1442. * The loop is aborted as soon as the highest level, which might
  1443. * be different from tmigr_hierarchy_levels, contains only a
  1444. * single group.
  1445. */
  1446. if (group->parent || i == tmigr_hierarchy_levels ||
  1447. (list_empty(&tmigr_level_list[i]) &&
  1448. list_is_singular(&tmigr_level_list[i - 1])))
  1449. break;
  1450. } while (i < tmigr_hierarchy_levels);
  1451. /* Assert single root */
  1452. WARN_ON_ONCE(!err && !group->parent && !list_is_singular(&tmigr_level_list[top]));
  1453. while (i > 0) {
  1454. group = stack[--i];
  1455. if (err < 0) {
  1456. list_del(&group->list);
  1457. kfree(group);
  1458. continue;
  1459. }
  1460. WARN_ON_ONCE(i != group->level);
  1461. /*
  1462. * Update tmc -> group / child -> group connection
  1463. */
  1464. if (i == 0) {
  1465. struct tmigr_cpu *tmc = per_cpu_ptr(&tmigr_cpu, cpu);
  1466. raw_spin_lock_irq(&group->lock);
  1467. tmc->tmgroup = group;
  1468. tmc->groupmask = BIT(group->num_children++);
  1469. raw_spin_unlock_irq(&group->lock);
  1470. trace_tmigr_connect_cpu_parent(tmc);
  1471. /* There are no children that need to be connected */
  1472. continue;
  1473. } else {
  1474. child = stack[i - 1];
  1475. /* Will be activated at online time */
  1476. tmigr_connect_child_parent(child, group, false);
  1477. }
  1478. /* check if uppermost level was newly created */
  1479. if (top != i)
  1480. continue;
  1481. WARN_ON_ONCE(top == 0);
  1482. lvllist = &tmigr_level_list[top];
  1483. /*
  1484. * Newly created root level should have accounted the upcoming
  1485. * CPU's child group and pre-accounted the old root.
  1486. */
  1487. if (group->num_children == 2 && list_is_singular(lvllist)) {
  1488. /*
  1489. * The target CPU must never do the prepare work, except
  1490. * on early boot when the boot CPU is the target. Otherwise
  1491. * it may spuriously activate the old top level group inside
  1492. * the new one (nevertheless whether old top level group is
  1493. * active or not) and/or release an uninitialized childmask.
  1494. */
  1495. WARN_ON_ONCE(cpu == raw_smp_processor_id());
  1496. lvllist = &tmigr_level_list[top - 1];
  1497. list_for_each_entry(child, lvllist, list) {
  1498. if (child->parent)
  1499. continue;
  1500. tmigr_connect_child_parent(child, group, true);
  1501. }
  1502. }
  1503. }
  1504. kfree(stack);
  1505. return err;
  1506. }
  1507. static int tmigr_add_cpu(unsigned int cpu)
  1508. {
  1509. int node = cpu_to_node(cpu);
  1510. int ret;
  1511. mutex_lock(&tmigr_mutex);
  1512. ret = tmigr_setup_groups(cpu, node);
  1513. mutex_unlock(&tmigr_mutex);
  1514. return ret;
  1515. }
  1516. static int tmigr_cpu_prepare(unsigned int cpu)
  1517. {
  1518. struct tmigr_cpu *tmc = per_cpu_ptr(&tmigr_cpu, cpu);
  1519. int ret = 0;
  1520. /* Not first online attempt? */
  1521. if (tmc->tmgroup)
  1522. return ret;
  1523. raw_spin_lock_init(&tmc->lock);
  1524. timerqueue_init(&tmc->cpuevt.nextevt);
  1525. tmc->cpuevt.nextevt.expires = KTIME_MAX;
  1526. tmc->cpuevt.ignore = true;
  1527. tmc->cpuevt.cpu = cpu;
  1528. tmc->remote = false;
  1529. WRITE_ONCE(tmc->wakeup, KTIME_MAX);
  1530. ret = tmigr_add_cpu(cpu);
  1531. if (ret < 0)
  1532. return ret;
  1533. if (tmc->groupmask == 0)
  1534. return -EINVAL;
  1535. return ret;
  1536. }
  1537. static int __init tmigr_init(void)
  1538. {
  1539. unsigned int cpulvl, nodelvl, cpus_per_node, i;
  1540. unsigned int nnodes = num_possible_nodes();
  1541. unsigned int ncpus = num_possible_cpus();
  1542. int ret = -ENOMEM;
  1543. BUILD_BUG_ON_NOT_POWER_OF_2(TMIGR_CHILDREN_PER_GROUP);
  1544. /* Nothing to do if running on UP */
  1545. if (ncpus == 1)
  1546. return 0;
  1547. /*
  1548. * Calculate the required hierarchy levels. Unfortunately there is no
  1549. * reliable information available, unless all possible CPUs have been
  1550. * brought up and all NUMA nodes are populated.
  1551. *
  1552. * Estimate the number of levels with the number of possible nodes and
  1553. * the number of possible CPUs. Assume CPUs are spread evenly across
  1554. * nodes. We cannot rely on cpumask_of_node() because it only works for
  1555. * online CPUs.
  1556. */
  1557. cpus_per_node = DIV_ROUND_UP(ncpus, nnodes);
  1558. /* Calc the hierarchy levels required to hold the CPUs of a node */
  1559. cpulvl = DIV_ROUND_UP(order_base_2(cpus_per_node),
  1560. ilog2(TMIGR_CHILDREN_PER_GROUP));
  1561. /* Calculate the extra levels to connect all nodes */
  1562. nodelvl = DIV_ROUND_UP(order_base_2(nnodes),
  1563. ilog2(TMIGR_CHILDREN_PER_GROUP));
  1564. tmigr_hierarchy_levels = cpulvl + nodelvl;
  1565. /*
  1566. * If a NUMA node spawns more than one CPU level group then the next
  1567. * level(s) of the hierarchy contains groups which handle all CPU groups
  1568. * of the same NUMA node. The level above goes across NUMA nodes. Store
  1569. * this information for the setup code to decide in which level node
  1570. * matching is no longer required.
  1571. */
  1572. tmigr_crossnode_level = cpulvl;
  1573. tmigr_level_list = kcalloc(tmigr_hierarchy_levels, sizeof(struct list_head), GFP_KERNEL);
  1574. if (!tmigr_level_list)
  1575. goto err;
  1576. for (i = 0; i < tmigr_hierarchy_levels; i++)
  1577. INIT_LIST_HEAD(&tmigr_level_list[i]);
  1578. pr_info("Timer migration: %d hierarchy levels; %d children per group;"
  1579. " %d crossnode level\n",
  1580. tmigr_hierarchy_levels, TMIGR_CHILDREN_PER_GROUP,
  1581. tmigr_crossnode_level);
  1582. ret = cpuhp_setup_state(CPUHP_TMIGR_PREPARE, "tmigr:prepare",
  1583. tmigr_cpu_prepare, NULL);
  1584. if (ret)
  1585. goto err;
  1586. ret = cpuhp_setup_state(CPUHP_AP_TMIGR_ONLINE, "tmigr:online",
  1587. tmigr_cpu_online, tmigr_cpu_offline);
  1588. if (ret)
  1589. goto err;
  1590. return 0;
  1591. err:
  1592. pr_err("Timer migration setup failed\n");
  1593. return ret;
  1594. }
  1595. early_initcall(tmigr_init);