do_timer() 是否应该只在 SMP 系统中的一个内核上调用?

Is do_timer() supposed to be called on only one core in SMP systems?

我了解到 do_timer 负责更新 jiffies 计数器。因此我的问题是,它可以在不同的内核上执行,还是始终在发生计时器滴答的同一内核上执行?

让我在谷歌搜索和阅读代码后回答我自己的问题。

do_timer() is supposed to be called on cpu with ID kept in tick_do_timer_cpu变量。

kernel/time/tick-common.c

/*
* tick_do_timer_cpu is a timer core internal variable which holds the CPU NR
* which is responsible for calling do_timer(), i.e. the timekeeping stuff.This
* variable has two functions:
*
* 1) Prevent a thundering herd issue of a gazillion of CPUs trying to grab the
* timekeeping lock all at once. Only the CPU which is assigned to do the
* update is handling it.
*
* 2) Hand off the duty in the NOHZ idle case by setting the value to
* TICK_DO_TIMER_NONE, i.e. a non existing CPU. So the next cpu which looks
* at it will take over and keep the time keeping alive. The handover
* procedure also covers cpu hotplug.
*/

tick_do_timer_cpu 根据 tick_periodic() or in tick_sched_do_timer() 中的当前 CPU ID 检查。如果当前 CPU 相同,则调用 do_timer(),否则不调用。

static void tick_periodic(int cpu)
 {
      if (tick_do_timer_cpu == cpu) {
              write_seqlock(&jiffies_lock);

              /* Keep track of the next tick event */
              tick_next_period = ktime_add(tick_next_period, tick_period);

              do_timer(1);
              write_sequnlock(&jiffies_lock);
              update_wall_time();
      }

      update_process_times(user_mode(get_irq_regs()));
      profile_tick(CPU_PROFILING);
  }`

这种方式的 jiffies 管理是在 SMP 系统中的一个核心上完成的。