sched discussion
[PATCH] sched/fair: Remove dead throttled check in pick_task_fair()
LLM 分析
sched/fair:清理 pick_task_fair() 中遗留的 throttled 死代码
系列概况
| 字段 | 内容 |
|---|---|
| 标题 | [PATCH] sched/fair: Remove dead throttled check in pick_task_fair() |
| 作者 | fangqiurong fangqiurong@kylinos.cn |
| 回复 | K Prateek Nayak kprateek.nayak@amd.com |
| 版本 | v1(单 patch) |
| 规模 | 1 文件,新增 0 行,删除 5 行 |
| 修改文件 | kernel/sched/fair.c |
| Message-ID | 20260805013949.151089-1-fangqiurong@kylinos.cn |
| 完整性 | 完整(diff + commit message + Signed-off-by) |
补丁目的
本 patch 是一段 cleanup 类改动,目标是把 pick_task_fair() 中因为历史重构而失去作用的死代码清理掉。
在 commit f666241e6bd5("sched/fair: Unify cfs_rq throttling via account_cfs_rq_runtime()")合入后,CFS runqueue 的节流被统一收敛到 account_cfs_rq_runtime() 中,原本在 pick_task_fair() 沿 cfs_rq 链路遍历时给局部变量 throttled 赋值的逻辑被移除,但变量声明、初始化以及函数尾部基于该变量判断的 task_throttle_setup_work() 调用还残留在源码里。
补丁要把这段已无功能语义、无人写也无人读的代码清掉,让 pick 路径行为完全不变。
旧流程的问题
旧版 pick_task_fair() 中存在以下三处与 throttled 相关的代码:
- 函数顶部声明局部变量
bool throttled;; - 进入遍历循环前执行
throttled = false;初始化; - 循环结束后用
if (unlikely(throttled)) task_throttle_setup_work(p);把被节流任务挂上 throttle work。
重构之后步骤 2 对应的"赋值"已不再发生,该变量始终为 false,步骤 3 变成永远走不到的分支。结果是变量名、初始化、调用点都还活在源码里,读者却找不到任何路径会让它们真正生效,造成阅读干扰与维护负担。
新流程
删除三处与 throttled 相关的代码:
- 局部变量声明
bool throttled; - 遍历前的
throttled = false;初始化 - 遍历结束后
if (unlikely(throttled)) task_throttle_setup_work(p);整段
其余 pick 路径——again 标签、cfs_rq->nr_queued 判断、do { ... } while (cfs_rq) 遍历、task_of(se) 返回以及 idle 标签——一字不动。
Patch 概览
| 文件 | 修改位置 | 行为 |
|---|---|---|
| kernel/sched/fair.c | pick_task_fair() | 删除 throttled 局部变量、其初始化以及基于该变量调用的 task_throttle_setup_work() |
无函数签名变化,无新增调用方影响,纯文本删除。
关键实现
清理后 pick_task_fair() 的相关骨架可表示为:
struct task_struct *pick_task_fair(struct rq *rq, struct rq_flags *rf)
{
struct sched_entity *se;
struct cfs_rq *cfs_rq;
struct task_struct *p;
int new_tasks;
again:
if (!cfs_rq->nr_queued)
goto idle;
do {
/* Might not have done put_prev_entity() */
if (cfs_rq->curr && cfs_rq->curr->on_rq)
...
} while (cfs_rq);
p = task_of(se);
return p;
idle:
...
}
被删除的 5 行对应:
- bool throttled;
- throttled = false;
-
- if (unlikely(throttled))
- task_throttle_setup_work(p);
类比
把 pick_task_fair() 想象成快递分拣中心:以前墙上挂着一块"今日限流"小牌子(变量 throttled),每个包裹批次过完后如果牌子翻起,就会按呼叫按钮通知调度(task_throttle_setup_work)。后来调度公司改用集中式调度平台 account_cfs_rq_runtime(),再也没人去翻那块牌子,但牌子还挂在墙上、按钮还在桌上,搬货的人路过都会迟疑一下。补丁做的事情就是把没用的牌子和按钮一起拆掉,让分拣流程干净利落。
pick_task_fair() cleanup timeline
--------------------------------
patch v1 (this thread) tip:sched/core (85570f10a4c6)
-------------------------- ---------------------------------
bool throttled; (no throttled variable)
throttled = false; (no task_throttle_setup_work call)
if (unlikely(throttled)) (cleanup already merged)
task_throttle_setup_work(p);
| |
v v
+-------------------+ +-----------------------+
| Dead code after | | Upstream tip already |
| f666241 removed | --f666241e6bd5--> | removed same code as |
| the only write to | assignment gone | part of eevdf single |
| throttled | -> variable dead | runqueue migration |
+-------------------+ +-----------------------+
| |
+--> author should rebase or withdraw <-------+
Highlight:风险与注意点
- 上游已清理:回复者 K Prateek Nayak 指出,在
tip:sched/core上 Peter Zijlstra 已经通过 commit85570f10a4c6("sched/eevdf: Move to a single runqueue")一并完成了同样的清理。 - 建议动作:作者需要 rebase 自己工作的分支;如果基线已经包含
85570f10a4c6,本 patch 合入时大概率会变成空 diff 甚至需要直接撤回。 - 验证方法:合并前用
git log --oneline f666241e6bd5..85570f10a4c6 -- kernel/sched/fair.c确认pick_task_fair()当前形态,避免重复提交。 - 行为风险:极低,纯删除一个
bool与一行 if,无功能变化,但仍应保证CONFIG_CFS_BANDWIDTH/CONFIG_SMP相关 throttle 测试通过。
版本变化
本系列只有 v1,无版本演进;唯一线索是回复者指向上游 85570f10a4c6,提示作者 rebase 或撤回。
一句话总结
本 patch 删除 pick_task_fair() 中自 f666241e6bd5 起已成死代码的 throttled 变量与 task_throttle_setup_work() 调用;方向正确,但已被 tip:sched/core 的 85570f10a4c6(eevdf 单 runqueue 改造)一并清理,作者需要 rebase 或撤回。