前言
前段时间修了一个调度并发导致的bug,在此记录一下bug的根因,并梳理一下内核中的并发模型。
BUG梳理
IBM的Shrikanth Hegde向内核提交了一组补丁[1],这组补丁的意图是将宿主机上的虚机之间的vcpu调度竞争转化为每个虚机内部的调度竞争,从而提升宿主机上各虚机的性能表现。
更详细一点,这组补丁就是新增了一个叫作cpu_preferred_mask的cpumask,它被设定为cpu_active_mask的一个子集(没有开启steal governor的时候,cpu_preferred_mask等于cpu_active_mask)。任务的唤醒、负载均衡这些操作将限制在cpu_preferred_mask中。steal governor的delayed work每隔一段时间就扫描一下本虚机的steal time,以此计算出一个steal ratio,当它高于设定的高阈值时,就将一个core移出cpu_preferred_mask,当它低于设定的低阈值时,就将一个core移回cpu_preferred_mask。
steal governor的使用需要满足一些条件:steal governor运行在虚机上;虚机应该能够获取steal time;宿主机上的所有虚机应该一起使用steal governor(不然将有失公平)。
这组补丁中有如下代码,这就是tick的时候发现如果当前cpu不是preferred,就调用sched_push_current_non_preferred_cpu判断当前cpu上的任务是否可以push出去,如果可以的话,就通过stopper线程将任务push出去。
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 04400f934cc7..5049eff58fb7 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -5809,6 +5809,9 @@ void sched_tick(void)
unsigned long hw_pressure;
u64 resched_latency;
+ if (!cpu_preferred(cpu))
+ sched_push_current_non_preferred_cpu(rq);
+
if (housekeeping_cpu(cpu, HK_TYPE_KERNEL_NOISE))
arch_scale_freq_tick();
@@ -11201,3 +11204,83 @@ void sched_change_end(struct sched_change_ctx *ctx)
p->sched_class->prio_changed(rq, p, ctx->prio);
}
}
+
+#ifdef CONFIG_PREFERRED_CPU
+static DEFINE_PER_CPU(struct cpu_stop_work, npc_push_task_work);
+
+static int sched_non_preferred_cpu_push_stop(void *arg)
+{
+ struct task_struct *p = arg;
+ struct rq *rq = this_rq();
+ struct rq_flags rf;
+ int cpu;
+
+ if (cpu_preferred(rq->cpu)) {
+ scoped_guard(rq_lock_irqsave, rq)
+ rq->npc_push_work_pending = false;
+ put_task_struct(p);
+ return 0;
+ }
+
+ scoped_guard (raw_spinlock_irq, &p->pi_lock) {
+ /*
+ * select_fallback_rq() may acquire the rq lock in case of
+ * fallback. So call it before grabbing rq lock. If the task
+ * migrates to another CPU before the rq lock is acquired,
+ * subsequent validation of task's current rq will help to
+ * safely bail out.
+ */
+ cpu = select_fallback_rq(rq->cpu, p);
+ rq_lock(rq, &rf);
+ rq->npc_push_work_pending = false;
+ update_rq_clock(rq);
+ context_unsafe_alias(rq);
+
+ if (task_rq(p) == rq && task_on_rq_queued(p))
+ rq = __migrate_task(rq, &rf, p, cpu);
+ rq_unlock(rq, &rf);
+ }
+
+ put_task_struct(p);
+ return 0;
+}
+
+/*
+ * Push the current task running on non-preferred CPU(npc).
+ * Using this non preferred CPU will lead to more contention
+ * in the host. So it is better not to use this CPU.
+ *
+ * Since task is running, call a stopper to push the task out. This is
+ * similar to how task moves during hotplug. In select_fallback_rq() a
+ * preferred CPU will be chosen and henceforth task shouldn't come back to
+ * this CPU again.
+ *
+ * Works for FAIR class only.
+ *
+ * If task is affined only on non-preferred CPUs, no point in moving it out.
+ */
+void sched_push_current_non_preferred_cpu(struct rq *rq)
+{
+ struct task_struct *push_task = rq->curr;
+
+ scoped_guard(rq_lock, rq) {
+ /* Push the task if its explicit affinity allows */
+ if (!task_can_migrate_to_preferred(push_task, rq->cpu))
+ return;
+
+ /* There is already a stopper thread. Don't race with it. */
+ if (rq->npc_push_work_pending)
+ return;
+
+ if (is_migration_disabled(push_task))
+ return;
+
+ rq->npc_push_work_pending = true;
+ }
+
+ /* sched_tick runs with interrupts disabled. */
+ get_task_struct(push_task);
+ stop_one_cpu_nowait(rq->cpu, sched_non_preferred_cpu_push_stop,
+ push_task, this_cpu_ptr(&npc_push_task_work));
+}
+#endif
这段代码的逻辑其实是和cpu hotplug那块的balance_push那一段的逻辑差不多的,都是对当前任务做一些检查,比如是否能迁移之类的,然后通过stopper线程去实际迁移指定,理论上不会有什么问题。
但是这里与balance_push存在一处本质上的区别,那就是代码执行的上下文不同。
梳理一下balance_push,可以发现那些调用都是在task上下文中执行的,而且stop_one_cpu_nowait有关抢占->开抢占,开抢占之后是有调度点存在的,所以stopper可以抢占并运行,不会存在在balance_push中检查is_migration_disabled为false然后将__balance_push_cpu_stop入stopper队列,然后current继续运行设置migrate_disable,导致后面stopper执行的时候检查到is_migration_disabled为true,从而导致出现将不可迁移的任务进行迁移触发warn的情况。
而sched_push_current_non_preferred_cpu是在中断上下文中运行的(sched_tick是timer定时器的触发执行的,在timer的中断上下文中),因此就会出现以下情况:在sched_push_current_non_preferred_cpu函数中检查is_migration_disabled是false可以迁移,然后中断返回,当前的任务继续运行,可能执行migrate_disable禁止迁移,然后当stopper运行的时候尝试迁移这个任务,实际上就是去迁移migration_disabled的任务了,破坏了migration_disabled任务不可迁移的语义,触发warning。
cpu0
----------------------------------------------------------------
task context | irq context
----------------------------------------------------------------
is_migration_disabled{false}
migrate_disable()
那么这时候大家就肯定有个疑问,中断上下文中调用了stop_one_cpu_nowait,stop_one_cpu_nowait肯定会执行resched_curr设置可抢占。而中断返回是一个天然的调度点,这时候stopper不就抢占了嘛?根本不会给current去执行migrate_disable的机会吧?
这个是有一定的道理的,所以上面的bug只能发生在关抢占的时候,也就是在current运行的时候正处于关抢占的状态,这时候timer中断过来执行sched_push_current_non_preferred_cpu,中断返回后,因为preempt_count非0,所以自然不能触发抢占调度的逻辑,那么current就会继续运行。从current继续运行到stopper抢占运行的这段时间,如果正好执行了migrate_disable,就会触发上面的bug。
而我们看一下migrate_disable的函数体:最后是需要先关抢占,然后才设置p->migration_disabled = 1;,最后再开抢占。而is_migration_disabled其实就只是读p->migration_disabled的值并返回。
static inline void __migrate_disable(void)
{
struct task_struct *p = current;
if (p->migration_disabled) {
#ifdef CONFIG_DEBUG_PREEMPT
/*
*Warn about overflow half-way through the range.
*/
WARN_ON_ONCE((s16)p->migration_disabled < 0);
#endif
p->migration_disabled++;
return;
}
guard(preempt)();
this_rq_pinned()++;
p->migration_disabled = 1;
}
static inline bool is_migration_disabled(struct task_struct *p)
{
return p->migration_disabled;
}
所以这migrate_disable本身在这种情况下就会导致race,如果一个任务正在内核态执行migrate_disable函数,恰好在执行完preempt_disable之后执行p->migration_disabled=1之前被timer中断打断从而进入irq context去执行sched_push_current_non_preferred_cpu,这时候执行is_migration_disabled必然检测到任务是可以迁移的,然后就会将迁移任务queue到stopper中去,中断返回。因为中断返回的时候是关抢占的,所以stopper并不能抢占运行,所以current会继续运行完p->migration_disabled=1,然后开抢占被stopper抢占执行,然后stopper去执行迁移任务,但是这时候这个任务已经是不可迁移的了。
cpu0
----------------------------------------------------------------
task context | irq context
----------------------------------------------------------------
migrate_disable()
preempt_disable()
is_migration_disabled{false}
p->migration_disabled=1
preempt_enable()
内核并发模型
借上面这个调度bug,我们再梳理一下内核的并发模型。
很多人可能以为,并发只出现在smp系统上,比如CPU0和CPU1同时访问同一段代码段,这时候可能出现竞态。
但是其实,单核上或者说up系统上,也是存在并发问题的。
首先我们知道当前linux系统中是有四种上下文的,从下到上分别是:task context->softirq context->hardirq context->NMI context。
因此,单核/UP上也就会出现系统在某种上下文中运行的时候,被更高级的上下文打断,如果更高级的上下文中也同样执行到了与被打断的上下文相关的代码,就有可能引起race问题。
另外,单核上如果一个任务在访问某个数据的时候,被该cpu上另一个更高优先级的任务抢占执行,然后该任务也去访问这个数据,这也可能会产生race。
因此,其实并发是有三种情况的:
1.smp下的多核并发,多个cpu同时访问某一段代码造成的race
2.单核上的上下文打断导致的并发,当前运行的上下文被更高级的上下文打断时可能导致race
3.单核上的抢占导致的并发,当前运行的任务中被该rq中的其他任务抢占导致代码运行可能出现race

综上,考虑代码/数据是否有并发问题的时候,除了考虑多核间的cpu并发,还应该考虑单核上的抢占/软中断/硬中断/NMI等是否会触发race条件。
如何解决这几种并发呢?
smp多核并发,直接可以加锁;单核上下文打断导致的并发,可以通过关中断来解决;单核上任务抢占导致的并发,可以通过关抢占来解决。
当然,不想这么麻烦的话,加锁能直接解决绝大多数问题。
引用
https://lore.kernel.org/all/20260928053728.797539-1-sshegde@linux.ibm.com/
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 857879363@qq.com