1 // SPDX-License-Identifier: GPL-2.0-only
3 * Infrastructure for migratable timers
5 * Copyright(C) 2022 linutronix GmbH
7 #include <linux/cpuhotplug.h>
8 #include <linux/slab.h>
10 #include <linux/spinlock.h>
11 #include <linux/timerqueue.h>
12 #include <trace/events/ipi.h>
14 #include "timer_migration.h"
15 #include "tick-internal.h"
17 #define CREATE_TRACE_POINTS
18 #include <trace/events/timer_migration.h>
21 * The timer migration mechanism is built on a hierarchy of groups. The
22 * lowest level group contains CPUs, the next level groups of CPU groups
23 * and so forth. The CPU groups are kept per node so for the normal case
24 * lock contention won't happen across nodes. Depending on the number of
25 * CPUs per node even the next level might be kept as groups of CPU groups
26 * per node and only the levels above cross the node topology.
28 * Example topology for a two node system with 24 CPUs each.
33 * LVL 1 [GRP1:0] [GRP1:1]
34 * GRP0:0 - GRP0:2 GRP0:3 - GRP0:5
36 * LVL 0 [GRP0:0] [GRP0:1] [GRP0:2] [GRP0:3] [GRP0:4] [GRP0:5]
37 * CPUS 0-7 8-15 16-23 24-31 32-39 40-47
39 * The groups hold a timer queue of events sorted by expiry time. These
40 * queues are updated when CPUs go in idle. When they come out of idle
41 * ignore flag of events is set.
43 * Each group has a designated migrator CPU/group as long as a CPU/group is
44 * active in the group. This designated role is necessary to avoid that all
45 * active CPUs in a group try to migrate expired timers from other CPUs,
46 * which would result in massive lock bouncing.
48 * When a CPU is awake, it checks in it's own timer tick the group
49 * hierarchy up to the point where it is assigned the migrator role or if
50 * no CPU is active, it also checks the groups where no migrator is set
53 * If it finds expired timers in one of the group queues it pulls them over
54 * from the idle CPU and runs the timer function. After that it updates the
55 * group and the parent groups if required.
57 * CPUs which go idle arm their CPU local timer hardware for the next local
58 * (pinned) timer event. If the next migratable timer expires after the
59 * next local timer or the CPU has no migratable timer pending then the
60 * CPU does not queue an event in the LVL0 group. If the next migratable
61 * timer expires before the next local timer then the CPU queues that timer
62 * in the LVL0 group. In both cases the CPU marks itself idle in the LVL0
65 * When CPU comes out of idle and when a group has at least a single active
66 * child, the ignore flag of the tmigr_event is set. This indicates, that
67 * the event is ignored even if it is still enqueued in the parent groups
68 * timer queue. It will be removed when touching the timer queue the next
69 * time. This spares locking in active path as the lock protects (after
70 * setup) only event information. For more information about locking,
71 * please read the section "Locking rules".
73 * If the CPU is the migrator of the group then it delegates that role to
74 * the next active CPU in the group or sets migrator to TMIGR_NONE when
75 * there is no active CPU in the group. This delegation needs to be
76 * propagated up the hierarchy so hand over from other leaves can happen at
77 * all hierarchy levels w/o doing a search.
79 * When the last CPU in the system goes idle, then it drops all migrator
80 * duties up to the top level of the hierarchy (LVL2 in the example). It
81 * then has to make sure, that it arms it's own local hardware timer for
82 * the earliest event in the system.
88 * The groups are built up at init time or when CPUs come online. They are
89 * not destroyed when a group becomes empty due to offlining. The group
90 * just won't participate in the hierarchy management anymore. Destroying
91 * groups would result in interesting race conditions which would just make
92 * the whole mechanism slow and complex.
98 * For setting up new groups and handling events it's required to lock both
99 * child and parent group. The lock ordering is always bottom up. This also
100 * includes the per CPU locks in struct tmigr_cpu. For updating the migrator and
101 * active CPU/group information atomic_try_cmpxchg() is used instead and only
102 * the per CPU tmigr_cpu->lock is held.
104 * During the setup of groups tmigr_level_list is required. It is protected by
107 * When @timer_base->lock as well as tmigr related locks are required, the lock
108 * ordering is: first @timer_base->lock, afterwards tmigr related locks.
111 * Protection of the tmigr group state information:
112 * ------------------------------------------------
114 * The state information with the list of active children and migrator needs to
115 * be protected by a sequence counter. It prevents a race when updates in child
116 * groups are propagated in changed order. The state update is performed
117 * lockless and group wise. The following scenario describes what happens
118 * without updating the sequence counter:
120 * Therefore, let's take three groups and four CPUs (CPU2 and CPU3 as well
121 * as GRP0:1 will not change during the scenario):
125 * active = GRP0:0, GRP0:1
127 * LVL 0 [GRP0:0] [GRP0:1]
128 * migrator = CPU0 migrator = CPU2
129 * active = CPU0 active = CPU2
132 * active idle active idle
135 * 1. CPU0 goes idle. As the update is performed group wise, in the first step
136 * only GRP0:0 is updated. The update of GRP1:0 is pending as CPU0 has to
137 * walk the hierarchy.
141 * active = GRP0:0, GRP0:1
143 * LVL 0 [GRP0:0] [GRP0:1]
144 * --> migrator = TMIGR_NONE migrator = CPU2
145 * --> active = active = CPU2
148 * --> idle idle active idle
150 * 2. While CPU0 goes idle and continues to update the state, CPU1 comes out of
151 * idle. CPU1 updates GRP0:0. The update for GRP1:0 is pending as CPU1 also
152 * has to walk the hierarchy. Both CPUs (CPU0 and CPU1) now walk the
153 * hierarchy to perform the needed update from their point of view. The
154 * currently visible state looks the following:
158 * active = GRP0:0, GRP0:1
160 * LVL 0 [GRP0:0] [GRP0:1]
161 * --> migrator = CPU1 migrator = CPU2
162 * --> active = CPU1 active = CPU2
165 * idle --> active active idle
167 * 3. Here is the race condition: CPU1 managed to propagate its changes (from
168 * step 2) through the hierarchy to GRP1:0 before CPU0 (step 1) did. The
169 * active members of GRP1:0 remain unchanged after the update since it is
170 * still valid from CPU1 current point of view:
173 * --> migrator = GRP0:1
174 * --> active = GRP0:0, GRP0:1
176 * LVL 0 [GRP0:0] [GRP0:1]
177 * migrator = CPU1 migrator = CPU2
178 * active = CPU1 active = CPU2
181 * idle active active idle
183 * 4. Now CPU0 finally propagates its changes (from step 1) to GRP1:0.
186 * --> migrator = GRP0:1
187 * --> active = GRP0:1
189 * LVL 0 [GRP0:0] [GRP0:1]
190 * migrator = CPU1 migrator = CPU2
191 * active = CPU1 active = CPU2
194 * idle active active idle
197 * The race of CPU0 vs. CPU1 led to an inconsistent state in GRP1:0. CPU1 is
198 * active and is correctly listed as active in GRP0:0. However GRP1:0 does not
199 * have GRP0:0 listed as active, which is wrong. The sequence counter has been
200 * added to avoid inconsistent states during updates. The state is updated
201 * atomically only if all members, including the sequence counter, match the
202 * expected value (compare-and-exchange).
204 * Looking back at the previous example with the addition of the sequence
205 * counter: The update as performed by CPU0 in step 4 will fail. CPU1 changed
206 * the sequence number during the update in step 3 so the expected old value (as
207 * seen by CPU0 before starting the walk) does not match.
209 * Prevent race between new event and last CPU going inactive
210 * ----------------------------------------------------------
212 * When the last CPU is going idle and there is a concurrent update of a new
213 * first global timer of an idle CPU, the group and child states have to be read
214 * while holding the lock in tmigr_update_events(). The following scenario shows
215 * what happens, when this is not done.
217 * 1. Only CPU2 is active:
222 * next_expiry = KTIME_MAX
224 * LVL 0 [GRP0:0] [GRP0:1]
225 * migrator = TMIGR_NONE migrator = CPU2
226 * active = active = CPU2
227 * next_expiry = KTIME_MAX next_expiry = KTIME_MAX
230 * idle idle active idle
232 * 2. Now CPU 2 goes idle (and has no global timer, that has to be handled) and
233 * propagates that to GRP0:1:
238 * next_expiry = KTIME_MAX
240 * LVL 0 [GRP0:0] [GRP0:1]
241 * migrator = TMIGR_NONE --> migrator = TMIGR_NONE
242 * active = --> active =
243 * next_expiry = KTIME_MAX next_expiry = KTIME_MAX
246 * idle idle --> idle idle
248 * 3. Now the idle state is propagated up to GRP1:0. As this is now the last
249 * child going idle in top level group, the expiry of the next group event
250 * has to be handed back to make sure no event is lost. As there is no event
251 * enqueued, KTIME_MAX is handed back to CPU2.
254 * --> migrator = TMIGR_NONE
256 * next_expiry = KTIME_MAX
258 * LVL 0 [GRP0:0] [GRP0:1]
259 * migrator = TMIGR_NONE migrator = TMIGR_NONE
261 * next_expiry = KTIME_MAX next_expiry = KTIME_MAX
264 * idle idle --> idle idle
266 * 4. CPU 0 has a new timer queued from idle and it expires at TIMER0. CPU0
267 * propagates that to GRP0:0:
270 * migrator = TMIGR_NONE
272 * next_expiry = KTIME_MAX
274 * LVL 0 [GRP0:0] [GRP0:1]
275 * migrator = TMIGR_NONE migrator = TMIGR_NONE
277 * --> next_expiry = TIMER0 next_expiry = KTIME_MAX
280 * idle idle idle idle
282 * 5. GRP0:0 is not active, so the new timer has to be propagated to
283 * GRP1:0. Therefore the GRP1:0 state has to be read. When the stalled value
284 * (from step 2) is read, the timer is enqueued into GRP1:0, but nothing is
285 * handed back to CPU0, as it seems that there is still an active child in
289 * migrator = TMIGR_NONE
291 * --> next_expiry = TIMER0
293 * LVL 0 [GRP0:0] [GRP0:1]
294 * migrator = TMIGR_NONE migrator = TMIGR_NONE
296 * next_expiry = TIMER0 next_expiry = KTIME_MAX
299 * idle idle idle idle
301 * This is prevented by reading the state when holding the lock (when a new
302 * timer has to be propagated from idle path)::
304 * CPU2 (tmigr_inactive_up()) CPU0 (tmigr_new_timer_up())
305 * -------------------------- ---------------------------
307 * cmpxchg(&GRP1:0->state);
308 * tmigr_update_events() {
309 * spin_lock(&GRP1:0->lock);
310 * // ... update events ...
311 * // hand back first expiry when GRP1:0 is idle
312 * spin_unlock(&GRP1:0->lock);
313 * // ^^^ release state modification
315 * tmigr_update_events() {
316 * spin_lock(&GRP1:0->lock)
317 * // ^^^ acquire state modification
318 * group_state = atomic_read(&GRP1:0->state)
319 * // .... update events ...
320 * // hand back first expiry when GRP1:0 is idle
321 * spin_unlock(&GRP1:0->lock) <3>
322 * // ^^^ makes state visible for other
323 * // callers of tmigr_new_timer_up()
326 * When CPU0 grabs the lock directly after cmpxchg, the first timer is reported
327 * back to CPU0 and also later on to CPU2. So no timer is missed. A concurrent
328 * update of the group state from active path is no problem, as the upcoming CPU
329 * will take care of the group events.
331 * Required event and timerqueue update after a remote expiry:
332 * -----------------------------------------------------------
334 * After expiring timers of a remote CPU, a walk through the hierarchy and
335 * update of events and timerqueues is required. It is obviously needed if there
336 * is a 'new' global timer but also if there is no new global timer but the
337 * remote CPU is still idle.
339 * 1. CPU0 and CPU1 are idle and have both a global timer expiring at the same
340 * time. So both have an event enqueued in the timerqueue of GRP0:0. CPU3 is
341 * also idle and has no global timer pending. CPU2 is the only active CPU and
342 * thus also the migrator:
347 * --> timerqueue = evt-GRP0:0
349 * LVL 0 [GRP0:0] [GRP0:1]
350 * migrator = TMIGR_NONE migrator = CPU2
351 * active = active = CPU2
352 * groupevt.ignore = false groupevt.ignore = true
353 * groupevt.cpu = CPU0 groupevt.cpu =
354 * timerqueue = evt-CPU0, timerqueue =
358 * idle idle active idle
360 * 2. CPU2 starts to expire remote timers. It starts with LVL0 group
361 * GRP0:1. There is no event queued in the timerqueue, so CPU2 continues with
362 * the parent of GRP0:1: GRP1:0. In GRP1:0 it dequeues the first event. It
363 * looks at tmigr_event::cpu struct member and expires the pending timer(s)
371 * LVL 0 [GRP0:0] [GRP0:1]
372 * migrator = TMIGR_NONE migrator = CPU2
373 * active = active = CPU2
374 * groupevt.ignore = false groupevt.ignore = true
375 * --> groupevt.cpu = CPU0 groupevt.cpu =
376 * timerqueue = evt-CPU0, timerqueue =
380 * idle idle active idle
382 * 3. Some work has to be done after expiring the timers of CPU0. If we stop
383 * here, then CPU1's pending global timer(s) will not expire in time and the
384 * timerqueue of GRP0:0 has still an event for CPU0 enqueued which has just
385 * been processed. So it is required to walk the hierarchy from CPU0's point
386 * of view and update it accordingly. CPU0's event will be removed from the
387 * timerqueue because it has no pending timer. If CPU0 would have a timer
388 * pending then it has to expire after CPU1's first timer because all timers
389 * from this period were just expired. Either way CPU1's event will be first
390 * in GRP0:0's timerqueue and therefore set in the CPU field of the group
391 * event which is then enqueued in GRP1:0's timerqueue as GRP0:0 is still not
397 * --> timerqueue = evt-GRP0:0
399 * LVL 0 [GRP0:0] [GRP0:1]
400 * migrator = TMIGR_NONE migrator = CPU2
401 * active = active = CPU2
402 * groupevt.ignore = false groupevt.ignore = true
403 * --> groupevt.cpu = CPU1 groupevt.cpu =
404 * --> timerqueue = evt-CPU1 timerqueue =
407 * idle idle active idle
409 * Now CPU2 (migrator) will continue step 2 at GRP1:0 and will expire the
412 * The hierarchy walk in step 3 can be skipped if the migrator notices that a
413 * CPU of GRP0:0 is active again. The CPU will mark GRP0:0 active and take care
414 * of the group as migrator and any needed updates within the hierarchy.
417 static DEFINE_MUTEX(tmigr_mutex
);
418 static struct list_head
*tmigr_level_list __read_mostly
;
420 static unsigned int tmigr_hierarchy_levels __read_mostly
;
421 static unsigned int tmigr_crossnode_level __read_mostly
;
423 static DEFINE_PER_CPU(struct tmigr_cpu
, tmigr_cpu
);
425 #define TMIGR_NONE 0xFF
428 static inline bool tmigr_is_not_available(struct tmigr_cpu
*tmc
)
430 return !(tmc
->tmgroup
&& tmc
->online
);
434 * Returns true, when @childmask corresponds to the group migrator or when the
435 * group is not active - so no migrator is set.
437 static bool tmigr_check_migrator(struct tmigr_group
*group
, u8 childmask
)
441 s
.state
= atomic_read(&group
->migr_state
);
443 if ((s
.migrator
== childmask
) || (s
.migrator
== TMIGR_NONE
))
449 static bool tmigr_check_migrator_and_lonely(struct tmigr_group
*group
, u8 childmask
)
451 bool lonely
, migrator
= false;
452 unsigned long active
;
455 s
.state
= atomic_read(&group
->migr_state
);
457 if ((s
.migrator
== childmask
) || (s
.migrator
== TMIGR_NONE
))
461 lonely
= bitmap_weight(&active
, BIT_CNT
) <= 1;
463 return (migrator
&& lonely
);
466 static bool tmigr_check_lonely(struct tmigr_group
*group
)
468 unsigned long active
;
471 s
.state
= atomic_read(&group
->migr_state
);
475 return bitmap_weight(&active
, BIT_CNT
) <= 1;
479 * struct tmigr_walk - data required for walking the hierarchy
480 * @nextexp: Next CPU event expiry information which is handed into
481 * the timer migration code by the timer code
482 * (get_next_timer_interrupt())
483 * @firstexp: Contains the first event expiry information when
484 * hierarchy is completely idle. When CPU itself was the
485 * last going idle, information makes sure, that CPU will
486 * be back in time. When using this value in the remote
487 * expiry case, firstexp is stored in the per CPU tmigr_cpu
488 * struct of CPU which expires remote timers. It is updated
489 * in top level group only. Be aware, there could occur a
490 * new top level of the hierarchy between the 'top level
491 * call' in tmigr_update_events() and the check for the
492 * parent group in walk_groups(). Then @firstexp might
493 * contain a value != KTIME_MAX even if it was not the
494 * final top level. This is not a problem, as the worst
495 * outcome is a CPU which might wake up a little early.
496 * @evt: Pointer to tmigr_event which needs to be queued (of idle
498 * @childmask: groupmask of child group
499 * @remote: Is set, when the new timer path is executed in
500 * tmigr_handle_remote_cpu()
501 * @basej: timer base in jiffies
502 * @now: timer base monotonic
503 * @check: is set if there is the need to handle remote timers;
504 * required in tmigr_requires_handle_remote() only
505 * @tmc_active: this flag indicates, whether the CPU which triggers
506 * the hierarchy walk is !idle in the timer migration
507 * hierarchy. When the CPU is idle and the whole hierarchy is
508 * idle, only the first event of the top level has to be
514 struct tmigr_event
*evt
;
523 typedef bool (*up_f
)(struct tmigr_group
*, struct tmigr_group
*, struct tmigr_walk
*);
525 static void __walk_groups(up_f up
, struct tmigr_walk
*data
,
526 struct tmigr_cpu
*tmc
)
528 struct tmigr_group
*child
= NULL
, *group
= tmc
->tmgroup
;
531 WARN_ON_ONCE(group
->level
>= tmigr_hierarchy_levels
);
533 if (up(group
, child
, data
))
537 group
= group
->parent
;
538 data
->childmask
= child
->groupmask
;
542 static void walk_groups(up_f up
, struct tmigr_walk
*data
, struct tmigr_cpu
*tmc
)
544 lockdep_assert_held(&tmc
->lock
);
546 __walk_groups(up
, data
, tmc
);
550 * Returns the next event of the timerqueue @group->events
552 * Removes timers with ignore flag and update next_expiry of the group. Values
553 * of the group event are updated in tmigr_update_events() only.
555 static struct tmigr_event
*tmigr_next_groupevt(struct tmigr_group
*group
)
557 struct timerqueue_node
*node
= NULL
;
558 struct tmigr_event
*evt
= NULL
;
560 lockdep_assert_held(&group
->lock
);
562 WRITE_ONCE(group
->next_expiry
, KTIME_MAX
);
564 while ((node
= timerqueue_getnext(&group
->events
))) {
565 evt
= container_of(node
, struct tmigr_event
, nextevt
);
568 WRITE_ONCE(group
->next_expiry
, evt
->nextevt
.expires
);
573 * Remove next timers with ignore flag, because the group lock
576 if (!timerqueue_del(&group
->events
, node
))
584 * Return the next event (with the expiry equal or before @now)
586 * Event, which is returned, is also removed from the queue.
588 static struct tmigr_event
*tmigr_next_expired_groupevt(struct tmigr_group
*group
,
591 struct tmigr_event
*evt
= tmigr_next_groupevt(group
);
593 if (!evt
|| now
< evt
->nextevt
.expires
)
597 * The event is ready to expire. Remove it and update next group event.
599 timerqueue_del(&group
->events
, &evt
->nextevt
);
600 tmigr_next_groupevt(group
);
605 static u64
tmigr_next_groupevt_expires(struct tmigr_group
*group
)
607 struct tmigr_event
*evt
;
609 evt
= tmigr_next_groupevt(group
);
614 return evt
->nextevt
.expires
;
617 static bool tmigr_active_up(struct tmigr_group
*group
,
618 struct tmigr_group
*child
,
619 struct tmigr_walk
*data
)
621 union tmigr_state curstate
, newstate
;
625 childmask
= data
->childmask
;
627 * No memory barrier is required here in contrast to
628 * tmigr_inactive_up(), as the group state change does not depend on the
631 curstate
.state
= atomic_read(&group
->migr_state
);
637 if (newstate
.migrator
== TMIGR_NONE
) {
638 newstate
.migrator
= childmask
;
640 /* Changes need to be propagated */
644 newstate
.active
|= childmask
;
647 } while (!atomic_try_cmpxchg(&group
->migr_state
, &curstate
.state
, newstate
.state
));
649 trace_tmigr_group_set_cpu_active(group
, newstate
, childmask
);
652 * The group is active (again). The group event might be still queued
653 * into the parent group's timerqueue but can now be handled by the
654 * migrator of this group. Therefore the ignore flag for the group event
655 * is updated to reflect this.
657 * The update of the ignore flag in the active path is done lockless. In
658 * worst case the migrator of the parent group observes the change too
659 * late and expires remotely all events belonging to this group. The
660 * lock is held while updating the ignore flag in idle path. So this
661 * state change will not be lost.
663 group
->groupevt
.ignore
= true;
668 static void __tmigr_cpu_activate(struct tmigr_cpu
*tmc
)
670 struct tmigr_walk data
;
672 data
.childmask
= tmc
->groupmask
;
674 trace_tmigr_cpu_active(tmc
);
676 tmc
->cpuevt
.ignore
= true;
677 WRITE_ONCE(tmc
->wakeup
, KTIME_MAX
);
679 walk_groups(&tmigr_active_up
, &data
, tmc
);
683 * tmigr_cpu_activate() - set this CPU active in timer migration hierarchy
685 * Call site timer_clear_idle() is called with interrupts disabled.
687 void tmigr_cpu_activate(void)
689 struct tmigr_cpu
*tmc
= this_cpu_ptr(&tmigr_cpu
);
691 if (tmigr_is_not_available(tmc
))
694 if (WARN_ON_ONCE(!tmc
->idle
))
697 raw_spin_lock(&tmc
->lock
);
699 __tmigr_cpu_activate(tmc
);
700 raw_spin_unlock(&tmc
->lock
);
704 * Returns true, if there is nothing to be propagated to the next level
706 * @data->firstexp is set to expiry of first gobal event of the (top level of
707 * the) hierarchy, but only when hierarchy is completely idle.
709 * The child and group states need to be read under the lock, to prevent a race
710 * against a concurrent tmigr_inactive_up() run when the last CPU goes idle. See
711 * also section "Prevent race between new event and last CPU going inactive" in
712 * the documentation at the top.
714 * This is the only place where the group event expiry value is set.
717 bool tmigr_update_events(struct tmigr_group
*group
, struct tmigr_group
*child
,
718 struct tmigr_walk
*data
)
720 struct tmigr_event
*evt
, *first_childevt
;
721 union tmigr_state childstate
, groupstate
;
722 bool remote
= data
->remote
;
723 bool walk_done
= false;
727 raw_spin_lock(&child
->lock
);
728 raw_spin_lock_nested(&group
->lock
, SINGLE_DEPTH_NESTING
);
730 childstate
.state
= atomic_read(&child
->migr_state
);
731 groupstate
.state
= atomic_read(&group
->migr_state
);
733 if (childstate
.active
) {
738 first_childevt
= tmigr_next_groupevt(child
);
739 nextexp
= child
->next_expiry
;
740 evt
= &child
->groupevt
;
742 evt
->ignore
= (nextexp
== KTIME_MAX
) ? true : false;
744 nextexp
= data
->nextexp
;
746 first_childevt
= evt
= data
->evt
;
749 * Walking the hierarchy is required in any case when a
750 * remote expiry was done before. This ensures to not lose
751 * already queued events in non active groups (see section
752 * "Required event and timerqueue update after a remote
753 * expiry" in the documentation at the top).
755 * The two call sites which are executed without a remote expiry
756 * before, are not prevented from propagating changes through
757 * the hierarchy by the return:
758 * - When entering this path by tmigr_new_timer(), @evt->ignore
760 * - tmigr_inactive_up() takes care of the propagation by
761 * itself and ignores the return value. But an immediate
762 * return is possible if there is a parent, sparing group
763 * locking at this level, because the upper walking call to
764 * the parent will take care about removing this event from
765 * within the group and update next_expiry accordingly.
767 * However if there is no parent, ie: the hierarchy has only a
768 * single level so @group is the top level group, make sure the
769 * first event information of the group is updated properly and
770 * also handled properly, so skip this fast return path.
772 if (evt
->ignore
&& !remote
&& group
->parent
)
775 raw_spin_lock(&group
->lock
);
777 childstate
.state
= 0;
778 groupstate
.state
= atomic_read(&group
->migr_state
);
782 * If the child event is already queued in the group, remove it from the
783 * queue when the expiry time changed only or when it could be ignored.
785 if (timerqueue_node_queued(&evt
->nextevt
)) {
786 if ((evt
->nextevt
.expires
== nextexp
) && !evt
->ignore
) {
787 /* Make sure not to miss a new CPU event with the same expiry */
788 evt
->cpu
= first_childevt
->cpu
;
792 if (!timerqueue_del(&group
->events
, &evt
->nextevt
))
793 WRITE_ONCE(group
->next_expiry
, KTIME_MAX
);
798 * When the next child event could be ignored (nextexp is
799 * KTIME_MAX) and there was no remote timer handling before or
800 * the group is already active, there is no need to walk the
801 * hierarchy even if there is a parent group.
803 * The other way round: even if the event could be ignored, but
804 * if a remote timer handling was executed before and the group
805 * is not active, walking the hierarchy is required to not miss
806 * an enqueued timer in the non active group. The enqueued timer
807 * of the group needs to be propagated to a higher level to
808 * ensure it is handled.
810 if (!remote
|| groupstate
.active
)
813 evt
->nextevt
.expires
= nextexp
;
814 evt
->cpu
= first_childevt
->cpu
;
816 if (timerqueue_add(&group
->events
, &evt
->nextevt
))
817 WRITE_ONCE(group
->next_expiry
, nextexp
);
821 if (!group
->parent
&& (groupstate
.migrator
== TMIGR_NONE
)) {
825 * Nothing to do when update was done during remote timer
826 * handling. First timer in top level group which needs to be
827 * handled when top level group is not active, is calculated
828 * directly in tmigr_handle_remote_up().
834 * The top level group is idle and it has to be ensured the
835 * global timers are handled in time. (This could be optimized
836 * by keeping track of the last global scheduled event and only
837 * arming it on the CPU if the new event is earlier. Not sure if
838 * its worth the complexity.)
840 data
->firstexp
= tmigr_next_groupevt_expires(group
);
843 trace_tmigr_update_events(child
, group
, childstate
, groupstate
,
847 raw_spin_unlock(&group
->lock
);
850 raw_spin_unlock(&child
->lock
);
855 static bool tmigr_new_timer_up(struct tmigr_group
*group
,
856 struct tmigr_group
*child
,
857 struct tmigr_walk
*data
)
859 return tmigr_update_events(group
, child
, data
);
863 * Returns the expiry of the next timer that needs to be handled. KTIME_MAX is
864 * returned, if an active CPU will handle all the timer migration hierarchy
867 static u64
tmigr_new_timer(struct tmigr_cpu
*tmc
, u64 nextexp
)
869 struct tmigr_walk data
= { .nextexp
= nextexp
,
870 .firstexp
= KTIME_MAX
,
871 .evt
= &tmc
->cpuevt
};
873 lockdep_assert_held(&tmc
->lock
);
878 trace_tmigr_cpu_new_timer(tmc
);
880 tmc
->cpuevt
.ignore
= false;
883 walk_groups(&tmigr_new_timer_up
, &data
, tmc
);
885 /* If there is a new first global event, make sure it is handled */
886 return data
.firstexp
;
889 static void tmigr_handle_remote_cpu(unsigned int cpu
, u64 now
,
892 struct timer_events tevt
;
893 struct tmigr_walk data
;
894 struct tmigr_cpu
*tmc
;
896 tmc
= per_cpu_ptr(&tmigr_cpu
, cpu
);
898 raw_spin_lock_irq(&tmc
->lock
);
901 * If the remote CPU is offline then the timers have been migrated to
904 * If tmigr_cpu::remote is set, at the moment another CPU already
905 * expires the timers of the remote CPU.
907 * If tmigr_event::ignore is set, then the CPU returns from idle and
908 * takes care of its timers.
910 * If the next event expires in the future, then the event has been
911 * updated and there are no timers to expire right now. The CPU which
912 * updated the event takes care when hierarchy is completely
913 * idle. Otherwise the migrator does it as the event is enqueued.
915 if (!tmc
->online
|| tmc
->remote
|| tmc
->cpuevt
.ignore
||
916 now
< tmc
->cpuevt
.nextevt
.expires
) {
917 raw_spin_unlock_irq(&tmc
->lock
);
921 trace_tmigr_handle_remote_cpu(tmc
);
924 WRITE_ONCE(tmc
->wakeup
, KTIME_MAX
);
926 /* Drop the lock to allow the remote CPU to exit idle */
927 raw_spin_unlock_irq(&tmc
->lock
);
929 if (cpu
!= smp_processor_id())
930 timer_expire_remote(cpu
);
933 * Lock ordering needs to be preserved - timer_base locks before tmigr
934 * related locks (see section "Locking rules" in the documentation at
935 * the top). During fetching the next timer interrupt, also tmc->lock
936 * needs to be held. Otherwise there is a possible race window against
937 * the CPU itself when it comes out of idle, updates the first timer in
938 * the hierarchy and goes back to idle.
940 * timer base locks are dropped as fast as possible: After checking
941 * whether the remote CPU went offline in the meantime and after
942 * fetching the next remote timer interrupt. Dropping the locks as fast
943 * as possible keeps the locking region small and prevents holding
944 * several (unnecessary) locks during walking the hierarchy for updating
945 * the timerqueue and group events.
948 timer_lock_remote_bases(cpu
);
949 raw_spin_lock(&tmc
->lock
);
952 * When the CPU went offline in the meantime, no hierarchy walk has to
953 * be done for updating the queued events, because the walk was
954 * already done during marking the CPU offline in the hierarchy.
956 * When the CPU is no longer idle, the CPU takes care of the timers and
957 * also of the timers in the hierarchy.
959 * (See also section "Required event and timerqueue update after a
960 * remote expiry" in the documentation at the top)
962 if (!tmc
->online
|| !tmc
->idle
) {
963 timer_unlock_remote_bases(cpu
);
967 /* next event of CPU */
968 fetch_next_timer_interrupt_remote(jif
, now
, &tevt
, cpu
);
969 timer_unlock_remote_bases(cpu
);
971 data
.nextexp
= tevt
.global
;
972 data
.firstexp
= KTIME_MAX
;
973 data
.evt
= &tmc
->cpuevt
;
977 * The update is done even when there is no 'new' global timer pending
978 * on the remote CPU (see section "Required event and timerqueue update
979 * after a remote expiry" in the documentation at the top)
981 walk_groups(&tmigr_new_timer_up
, &data
, tmc
);
985 raw_spin_unlock_irq(&tmc
->lock
);
988 static bool tmigr_handle_remote_up(struct tmigr_group
*group
,
989 struct tmigr_group
*child
,
990 struct tmigr_walk
*data
)
992 struct tmigr_event
*evt
;
1000 childmask
= data
->childmask
;
1002 trace_tmigr_handle_remote(group
);
1005 * Handle the group only if @childmask is the migrator or if the
1006 * group has no migrator. Otherwise the group is active and is
1007 * handled by its own migrator.
1009 if (!tmigr_check_migrator(group
, childmask
))
1012 raw_spin_lock_irq(&group
->lock
);
1014 evt
= tmigr_next_expired_groupevt(group
, now
);
1017 unsigned int remote_cpu
= evt
->cpu
;
1019 raw_spin_unlock_irq(&group
->lock
);
1021 tmigr_handle_remote_cpu(remote_cpu
, now
, jif
);
1023 /* check if there is another event, that needs to be handled */
1028 * Keep track of the expiry of the first event that needs to be handled
1029 * (group->next_expiry was updated by tmigr_next_expired_groupevt(),
1030 * next was set by tmigr_handle_remote_cpu()).
1032 data
->firstexp
= group
->next_expiry
;
1034 raw_spin_unlock_irq(&group
->lock
);
1040 * tmigr_handle_remote() - Handle global timers of remote idle CPUs
1042 * Called from the timer soft interrupt with interrupts enabled.
1044 void tmigr_handle_remote(void)
1046 struct tmigr_cpu
*tmc
= this_cpu_ptr(&tmigr_cpu
);
1047 struct tmigr_walk data
;
1049 if (tmigr_is_not_available(tmc
))
1052 data
.childmask
= tmc
->groupmask
;
1053 data
.firstexp
= KTIME_MAX
;
1056 * NOTE: This is a doubled check because the migrator test will be done
1057 * in tmigr_handle_remote_up() anyway. Keep this check to speed up the
1058 * return when nothing has to be done.
1060 if (!tmigr_check_migrator(tmc
->tmgroup
, tmc
->groupmask
)) {
1062 * If this CPU was an idle migrator, make sure to clear its wakeup
1063 * value so it won't chase timers that have already expired elsewhere.
1064 * This avoids endless requeue from tmigr_new_timer().
1066 if (READ_ONCE(tmc
->wakeup
) == KTIME_MAX
)
1070 data
.now
= get_jiffies_update(&data
.basej
);
1073 * Update @tmc->wakeup only at the end and do not reset @tmc->wakeup to
1074 * KTIME_MAX. Even if tmc->lock is not held during the whole remote
1075 * handling, tmc->wakeup is fine to be stale as it is called in
1076 * interrupt context and tick_nohz_next_event() is executed in interrupt
1077 * exit path only after processing the last pending interrupt.
1080 __walk_groups(&tmigr_handle_remote_up
, &data
, tmc
);
1082 raw_spin_lock_irq(&tmc
->lock
);
1083 WRITE_ONCE(tmc
->wakeup
, data
.firstexp
);
1084 raw_spin_unlock_irq(&tmc
->lock
);
1087 static bool tmigr_requires_handle_remote_up(struct tmigr_group
*group
,
1088 struct tmigr_group
*child
,
1089 struct tmigr_walk
*data
)
1093 childmask
= data
->childmask
;
1096 * Handle the group only if the child is the migrator or if the group
1097 * has no migrator. Otherwise the group is active and is handled by its
1100 if (!tmigr_check_migrator(group
, childmask
))
1104 * When there is a parent group and the CPU which triggered the
1105 * hierarchy walk is not active, proceed the walk to reach the top level
1106 * group before reading the next_expiry value.
1108 if (group
->parent
&& !data
->tmc_active
)
1112 * The lock is required on 32bit architectures to read the variable
1113 * consistently with a concurrent writer. On 64bit the lock is not
1114 * required because the read operation is not split and so it is always
1117 if (IS_ENABLED(CONFIG_64BIT
)) {
1118 data
->firstexp
= READ_ONCE(group
->next_expiry
);
1119 if (data
->now
>= data
->firstexp
) {
1124 raw_spin_lock(&group
->lock
);
1125 data
->firstexp
= group
->next_expiry
;
1126 if (data
->now
>= group
->next_expiry
) {
1128 raw_spin_unlock(&group
->lock
);
1131 raw_spin_unlock(&group
->lock
);
1138 * tmigr_requires_handle_remote() - Check the need of remote timer handling
1140 * Must be called with interrupts disabled.
1142 bool tmigr_requires_handle_remote(void)
1144 struct tmigr_cpu
*tmc
= this_cpu_ptr(&tmigr_cpu
);
1145 struct tmigr_walk data
;
1149 if (tmigr_is_not_available(tmc
))
1152 data
.now
= get_jiffies_update(&jif
);
1153 data
.childmask
= tmc
->groupmask
;
1154 data
.firstexp
= KTIME_MAX
;
1155 data
.tmc_active
= !tmc
->idle
;
1159 * If the CPU is active, walk the hierarchy to check whether a remote
1160 * expiry is required.
1162 * Check is done lockless as interrupts are disabled and @tmc->idle is
1163 * set only by the local CPU.
1166 __walk_groups(&tmigr_requires_handle_remote_up
, &data
, tmc
);
1172 * When the CPU is idle, compare @tmc->wakeup with @data.now. The lock
1173 * is required on 32bit architectures to read the variable consistently
1174 * with a concurrent writer. On 64bit the lock is not required because
1175 * the read operation is not split and so it is always consistent.
1177 if (IS_ENABLED(CONFIG_64BIT
)) {
1178 if (data
.now
>= READ_ONCE(tmc
->wakeup
))
1181 raw_spin_lock(&tmc
->lock
);
1182 if (data
.now
>= tmc
->wakeup
)
1184 raw_spin_unlock(&tmc
->lock
);
1191 * tmigr_cpu_new_timer() - enqueue next global timer into hierarchy (idle tmc)
1192 * @nextexp: Next expiry of global timer (or KTIME_MAX if not)
1194 * The CPU is already deactivated in the timer migration
1195 * hierarchy. tick_nohz_get_sleep_length() calls tick_nohz_next_event()
1196 * and thereby the timer idle path is executed once more. @tmc->wakeup
1197 * holds the first timer, when the timer migration hierarchy is
1200 * Returns the first timer that needs to be handled by this CPU or KTIME_MAX if
1201 * nothing needs to be done.
1203 u64
tmigr_cpu_new_timer(u64 nextexp
)
1205 struct tmigr_cpu
*tmc
= this_cpu_ptr(&tmigr_cpu
);
1208 if (tmigr_is_not_available(tmc
))
1211 raw_spin_lock(&tmc
->lock
);
1213 ret
= READ_ONCE(tmc
->wakeup
);
1214 if (nextexp
!= KTIME_MAX
) {
1215 if (nextexp
!= tmc
->cpuevt
.nextevt
.expires
||
1216 tmc
->cpuevt
.ignore
) {
1217 ret
= tmigr_new_timer(tmc
, nextexp
);
1219 * Make sure the reevaluation of timers in idle path
1220 * will not miss an event.
1222 WRITE_ONCE(tmc
->wakeup
, ret
);
1225 trace_tmigr_cpu_new_timer_idle(tmc
, nextexp
);
1226 raw_spin_unlock(&tmc
->lock
);
1230 static bool tmigr_inactive_up(struct tmigr_group
*group
,
1231 struct tmigr_group
*child
,
1232 struct tmigr_walk
*data
)
1234 union tmigr_state curstate
, newstate
, childstate
;
1238 childmask
= data
->childmask
;
1239 childstate
.state
= 0;
1242 * The memory barrier is paired with the cmpxchg() in tmigr_active_up()
1243 * to make sure the updates of child and group states are ordered. The
1244 * ordering is mandatory, as the group state change depends on the child
1247 curstate
.state
= atomic_read_acquire(&group
->migr_state
);
1251 childstate
.state
= atomic_read(&child
->migr_state
);
1253 newstate
= curstate
;
1256 /* Reset active bit when the child is no longer active */
1257 if (!childstate
.active
)
1258 newstate
.active
&= ~childmask
;
1260 if (newstate
.migrator
== childmask
) {
1262 * Find a new migrator for the group, because the child
1265 if (!childstate
.active
) {
1266 unsigned long new_migr_bit
, active
= newstate
.active
;
1268 new_migr_bit
= find_first_bit(&active
, BIT_CNT
);
1270 if (new_migr_bit
!= BIT_CNT
) {
1271 newstate
.migrator
= BIT(new_migr_bit
);
1273 newstate
.migrator
= TMIGR_NONE
;
1275 /* Changes need to be propagated */
1283 WARN_ON_ONCE((newstate
.migrator
!= TMIGR_NONE
) && !(newstate
.active
));
1285 if (atomic_try_cmpxchg(&group
->migr_state
, &curstate
.state
, newstate
.state
)) {
1286 trace_tmigr_group_set_cpu_inactive(group
, newstate
, childmask
);
1291 * The memory barrier is paired with the cmpxchg() in
1292 * tmigr_active_up() to make sure the updates of child and group
1293 * states are ordered. It is required only when the above
1294 * try_cmpxchg() fails.
1296 smp_mb__after_atomic();
1299 data
->remote
= false;
1301 /* Event Handling */
1302 tmigr_update_events(group
, child
, data
);
1307 static u64
__tmigr_cpu_deactivate(struct tmigr_cpu
*tmc
, u64 nextexp
)
1309 struct tmigr_walk data
= { .nextexp
= nextexp
,
1310 .firstexp
= KTIME_MAX
,
1311 .evt
= &tmc
->cpuevt
,
1312 .childmask
= tmc
->groupmask
};
1315 * If nextexp is KTIME_MAX, the CPU event will be ignored because the
1316 * local timer expires before the global timer, no global timer is set
1317 * or CPU goes offline.
1319 if (nextexp
!= KTIME_MAX
)
1320 tmc
->cpuevt
.ignore
= false;
1322 walk_groups(&tmigr_inactive_up
, &data
, tmc
);
1323 return data
.firstexp
;
1327 * tmigr_cpu_deactivate() - Put current CPU into inactive state
1328 * @nextexp: The next global timer expiry of the current CPU
1330 * Must be called with interrupts disabled.
1332 * Return: the next event expiry of the current CPU or the next event expiry
1333 * from the hierarchy if this CPU is the top level migrator or the hierarchy is
1336 u64
tmigr_cpu_deactivate(u64 nextexp
)
1338 struct tmigr_cpu
*tmc
= this_cpu_ptr(&tmigr_cpu
);
1341 if (tmigr_is_not_available(tmc
))
1344 raw_spin_lock(&tmc
->lock
);
1346 ret
= __tmigr_cpu_deactivate(tmc
, nextexp
);
1351 * Make sure the reevaluation of timers in idle path will not miss an
1354 WRITE_ONCE(tmc
->wakeup
, ret
);
1356 trace_tmigr_cpu_idle(tmc
, nextexp
);
1357 raw_spin_unlock(&tmc
->lock
);
1362 * tmigr_quick_check() - Quick forecast of next tmigr event when CPU wants to
1364 * @nextevt: The next global timer expiry of the current CPU
1367 * * KTIME_MAX - when it is probable that nothing has to be done (not
1368 * the only one in the level 0 group; and if it is the
1369 * only one in level 0 group, but there are more than a
1370 * single group active on the way to top level)
1371 * * nextevt - when CPU is offline and has to handle timer on its own
1372 * or when on the way to top in every group only a single
1373 * child is active but @nextevt is before the lowest
1374 * next_expiry encountered while walking up to top level.
1375 * * next_expiry - value of lowest expiry encountered while walking groups
1376 * if only a single child is active on each and @nextevt
1377 * is after this lowest expiry.
1379 u64
tmigr_quick_check(u64 nextevt
)
1381 struct tmigr_cpu
*tmc
= this_cpu_ptr(&tmigr_cpu
);
1382 struct tmigr_group
*group
= tmc
->tmgroup
;
1384 if (tmigr_is_not_available(tmc
))
1387 if (WARN_ON_ONCE(tmc
->idle
))
1390 if (!tmigr_check_migrator_and_lonely(tmc
->tmgroup
, tmc
->groupmask
))
1394 if (!tmigr_check_lonely(group
)) {
1398 * Since current CPU is active, events may not be sorted
1399 * from bottom to the top because the CPU's event is ignored
1400 * up to the top and its sibling's events not propagated upwards.
1401 * Thus keep track of the lowest observed expiry.
1403 nextevt
= min_t(u64
, nextevt
, READ_ONCE(group
->next_expiry
));
1407 group
= group
->parent
;
1414 * tmigr_trigger_active() - trigger a CPU to become active again
1416 * This function is executed on a CPU which is part of cpu_online_mask, when the
1417 * last active CPU in the hierarchy is offlining. With this, it is ensured that
1418 * the other CPU is active and takes over the migrator duty.
1420 static long tmigr_trigger_active(void *unused
)
1422 struct tmigr_cpu
*tmc
= this_cpu_ptr(&tmigr_cpu
);
1424 WARN_ON_ONCE(!tmc
->online
|| tmc
->idle
);
1429 static int tmigr_cpu_offline(unsigned int cpu
)
1431 struct tmigr_cpu
*tmc
= this_cpu_ptr(&tmigr_cpu
);
1435 raw_spin_lock_irq(&tmc
->lock
);
1436 tmc
->online
= false;
1437 WRITE_ONCE(tmc
->wakeup
, KTIME_MAX
);
1440 * CPU has to handle the local events on his own, when on the way to
1441 * offline; Therefore nextevt value is set to KTIME_MAX
1443 firstexp
= __tmigr_cpu_deactivate(tmc
, KTIME_MAX
);
1444 trace_tmigr_cpu_offline(tmc
);
1445 raw_spin_unlock_irq(&tmc
->lock
);
1447 if (firstexp
!= KTIME_MAX
) {
1448 migrator
= cpumask_any_but(cpu_online_mask
, cpu
);
1449 work_on_cpu(migrator
, tmigr_trigger_active
, NULL
);
1455 static int tmigr_cpu_online(unsigned int cpu
)
1457 struct tmigr_cpu
*tmc
= this_cpu_ptr(&tmigr_cpu
);
1459 /* Check whether CPU data was successfully initialized */
1460 if (WARN_ON_ONCE(!tmc
->tmgroup
))
1463 raw_spin_lock_irq(&tmc
->lock
);
1464 trace_tmigr_cpu_online(tmc
);
1465 tmc
->idle
= timer_base_is_idle();
1467 __tmigr_cpu_activate(tmc
);
1469 raw_spin_unlock_irq(&tmc
->lock
);
1473 static void tmigr_init_group(struct tmigr_group
*group
, unsigned int lvl
,
1476 union tmigr_state s
;
1478 raw_spin_lock_init(&group
->lock
);
1481 group
->numa_node
= lvl
< tmigr_crossnode_level
? node
: NUMA_NO_NODE
;
1483 group
->num_children
= 0;
1485 s
.migrator
= TMIGR_NONE
;
1488 atomic_set(&group
->migr_state
, s
.state
);
1490 timerqueue_init_head(&group
->events
);
1491 timerqueue_init(&group
->groupevt
.nextevt
);
1492 group
->groupevt
.nextevt
.expires
= KTIME_MAX
;
1493 WRITE_ONCE(group
->next_expiry
, KTIME_MAX
);
1494 group
->groupevt
.ignore
= true;
1497 static struct tmigr_group
*tmigr_get_group(unsigned int cpu
, int node
,
1500 struct tmigr_group
*tmp
, *group
= NULL
;
1502 lockdep_assert_held(&tmigr_mutex
);
1504 /* Try to attach to an existing group first */
1505 list_for_each_entry(tmp
, &tmigr_level_list
[lvl
], list
) {
1507 * If @lvl is below the cross NUMA node level, check whether
1508 * this group belongs to the same NUMA node.
1510 if (lvl
< tmigr_crossnode_level
&& tmp
->numa_node
!= node
)
1513 /* Capacity left? */
1514 if (tmp
->num_children
>= TMIGR_CHILDREN_PER_GROUP
)
1518 * TODO: A possible further improvement: Make sure that all CPU
1519 * siblings end up in the same group of the lowest level of the
1520 * hierarchy. Rely on the topology sibling mask would be a
1521 * reasonable solution.
1531 /* Allocate and set up a new group */
1532 group
= kzalloc_node(sizeof(*group
), GFP_KERNEL
, node
);
1534 return ERR_PTR(-ENOMEM
);
1536 tmigr_init_group(group
, lvl
, node
);
1538 /* Setup successful. Add it to the hierarchy */
1539 list_add(&group
->list
, &tmigr_level_list
[lvl
]);
1540 trace_tmigr_group_set(group
);
1544 static void tmigr_connect_child_parent(struct tmigr_group
*child
,
1545 struct tmigr_group
*parent
,
1548 struct tmigr_walk data
;
1550 raw_spin_lock_irq(&child
->lock
);
1551 raw_spin_lock_nested(&parent
->lock
, SINGLE_DEPTH_NESTING
);
1553 child
->parent
= parent
;
1554 child
->groupmask
= BIT(parent
->num_children
++);
1556 raw_spin_unlock(&parent
->lock
);
1557 raw_spin_unlock_irq(&child
->lock
);
1559 trace_tmigr_connect_child_parent(child
);
1565 * To prevent inconsistent states, active children need to be active in
1566 * the new parent as well. Inactive children are already marked inactive
1567 * in the parent group:
1569 * * When new groups were created by tmigr_setup_groups() starting from
1570 * the lowest level (and not higher then one level below the current
1571 * top level), then they are not active. They will be set active when
1572 * the new online CPU comes active.
1574 * * But if a new group above the current top level is required, it is
1575 * mandatory to propagate the active state of the already existing
1576 * child to the new parent. So tmigr_connect_child_parent() is
1577 * executed with the formerly top level group (child) and the newly
1578 * created group (parent).
1580 * * It is ensured that the child is active, as this setup path is
1581 * executed in hotplug prepare callback. This is exectued by an
1582 * already connected and !idle CPU. Even if all other CPUs go idle,
1583 * the CPU executing the setup will be responsible up to current top
1584 * level group. And the next time it goes inactive, it will release
1585 * the new childmask and parent to subsequent walkers through this
1586 * @child. Therefore propagate active state unconditionally.
1588 data
.childmask
= child
->groupmask
;
1591 * There is only one new level per time (which is protected by
1592 * tmigr_mutex). When connecting the child and the parent and set the
1593 * child active when the parent is inactive, the parent needs to be the
1594 * uppermost level. Otherwise there went something wrong!
1596 WARN_ON(!tmigr_active_up(parent
, child
, &data
) && parent
->parent
);
1599 static int tmigr_setup_groups(unsigned int cpu
, unsigned int node
)
1601 struct tmigr_group
*group
, *child
, **stack
;
1602 int top
= 0, err
= 0, i
= 0;
1603 struct list_head
*lvllist
;
1605 stack
= kcalloc(tmigr_hierarchy_levels
, sizeof(*stack
), GFP_KERNEL
);
1610 group
= tmigr_get_group(cpu
, node
, i
);
1611 if (IS_ERR(group
)) {
1612 err
= PTR_ERR(group
);
1620 * When booting only less CPUs of a system than CPUs are
1621 * available, not all calculated hierarchy levels are required.
1623 * The loop is aborted as soon as the highest level, which might
1624 * be different from tmigr_hierarchy_levels, contains only a
1627 if (group
->parent
|| i
== tmigr_hierarchy_levels
||
1628 (list_empty(&tmigr_level_list
[i
]) &&
1629 list_is_singular(&tmigr_level_list
[i
- 1])))
1632 } while (i
< tmigr_hierarchy_levels
);
1638 list_del(&group
->list
);
1643 WARN_ON_ONCE(i
!= group
->level
);
1646 * Update tmc -> group / child -> group connection
1649 struct tmigr_cpu
*tmc
= per_cpu_ptr(&tmigr_cpu
, cpu
);
1651 raw_spin_lock_irq(&group
->lock
);
1653 tmc
->tmgroup
= group
;
1654 tmc
->groupmask
= BIT(group
->num_children
++);
1656 raw_spin_unlock_irq(&group
->lock
);
1658 trace_tmigr_connect_cpu_parent(tmc
);
1660 /* There are no children that need to be connected */
1663 child
= stack
[i
- 1];
1664 /* Will be activated at online time */
1665 tmigr_connect_child_parent(child
, group
, false);
1668 /* check if uppermost level was newly created */
1672 WARN_ON_ONCE(top
== 0);
1674 lvllist
= &tmigr_level_list
[top
];
1675 if (group
->num_children
== 1 && list_is_singular(lvllist
)) {
1677 * The target CPU must never do the prepare work, except
1678 * on early boot when the boot CPU is the target. Otherwise
1679 * it may spuriously activate the old top level group inside
1680 * the new one (nevertheless whether old top level group is
1681 * active or not) and/or release an uninitialized childmask.
1683 WARN_ON_ONCE(cpu
== raw_smp_processor_id());
1685 lvllist
= &tmigr_level_list
[top
- 1];
1686 list_for_each_entry(child
, lvllist
, list
) {
1690 tmigr_connect_child_parent(child
, group
, true);
1700 static int tmigr_add_cpu(unsigned int cpu
)
1702 int node
= cpu_to_node(cpu
);
1705 mutex_lock(&tmigr_mutex
);
1706 ret
= tmigr_setup_groups(cpu
, node
);
1707 mutex_unlock(&tmigr_mutex
);
1712 static int tmigr_cpu_prepare(unsigned int cpu
)
1714 struct tmigr_cpu
*tmc
= per_cpu_ptr(&tmigr_cpu
, cpu
);
1717 /* Not first online attempt? */
1721 raw_spin_lock_init(&tmc
->lock
);
1722 timerqueue_init(&tmc
->cpuevt
.nextevt
);
1723 tmc
->cpuevt
.nextevt
.expires
= KTIME_MAX
;
1724 tmc
->cpuevt
.ignore
= true;
1725 tmc
->cpuevt
.cpu
= cpu
;
1726 tmc
->remote
= false;
1727 WRITE_ONCE(tmc
->wakeup
, KTIME_MAX
);
1729 ret
= tmigr_add_cpu(cpu
);
1733 if (tmc
->groupmask
== 0)
1739 static int __init
tmigr_init(void)
1741 unsigned int cpulvl
, nodelvl
, cpus_per_node
, i
;
1742 unsigned int nnodes
= num_possible_nodes();
1743 unsigned int ncpus
= num_possible_cpus();
1746 BUILD_BUG_ON_NOT_POWER_OF_2(TMIGR_CHILDREN_PER_GROUP
);
1748 /* Nothing to do if running on UP */
1753 * Calculate the required hierarchy levels. Unfortunately there is no
1754 * reliable information available, unless all possible CPUs have been
1755 * brought up and all NUMA nodes are populated.
1757 * Estimate the number of levels with the number of possible nodes and
1758 * the number of possible CPUs. Assume CPUs are spread evenly across
1759 * nodes. We cannot rely on cpumask_of_node() because it only works for
1762 cpus_per_node
= DIV_ROUND_UP(ncpus
, nnodes
);
1764 /* Calc the hierarchy levels required to hold the CPUs of a node */
1765 cpulvl
= DIV_ROUND_UP(order_base_2(cpus_per_node
),
1766 ilog2(TMIGR_CHILDREN_PER_GROUP
));
1768 /* Calculate the extra levels to connect all nodes */
1769 nodelvl
= DIV_ROUND_UP(order_base_2(nnodes
),
1770 ilog2(TMIGR_CHILDREN_PER_GROUP
));
1772 tmigr_hierarchy_levels
= cpulvl
+ nodelvl
;
1775 * If a NUMA node spawns more than one CPU level group then the next
1776 * level(s) of the hierarchy contains groups which handle all CPU groups
1777 * of the same NUMA node. The level above goes across NUMA nodes. Store
1778 * this information for the setup code to decide in which level node
1779 * matching is no longer required.
1781 tmigr_crossnode_level
= cpulvl
;
1783 tmigr_level_list
= kcalloc(tmigr_hierarchy_levels
, sizeof(struct list_head
), GFP_KERNEL
);
1784 if (!tmigr_level_list
)
1787 for (i
= 0; i
< tmigr_hierarchy_levels
; i
++)
1788 INIT_LIST_HEAD(&tmigr_level_list
[i
]);
1790 pr_info("Timer migration: %d hierarchy levels; %d children per group;"
1791 " %d crossnode level\n",
1792 tmigr_hierarchy_levels
, TMIGR_CHILDREN_PER_GROUP
,
1793 tmigr_crossnode_level
);
1795 ret
= cpuhp_setup_state(CPUHP_TMIGR_PREPARE
, "tmigr:prepare",
1796 tmigr_cpu_prepare
, NULL
);
1800 ret
= cpuhp_setup_state(CPUHP_AP_TMIGR_ONLINE
, "tmigr:online",
1801 tmigr_cpu_online
, tmigr_cpu_offline
);
1808 pr_err("Timer migration setup failed\n");
1811 early_initcall(tmigr_init
);