Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

sched/core: Add WARN_ON_ONCE() to check overflow for migrate_disable()

Background
==========
When repeated migrate_disable() calls are made with missing the
corresponding migrate_enable() calls, there is a risk of
'migration_disabled' going upper overflow because
'migration_disabled' is a type of unsigned short whose max value is
65535.

In PREEMPT_RT kernel, if 'migration_disabled' goes upper overflow, it may
make the migrate_disable() ineffective within local_lock_irqsave(). This
is because, during the scheduling procedure, the value of
'migration_disabled' will be checked, which can trigger CPU migration.
Consequently, the count of 'rcu_read_lock_nesting' may leak due to
local_lock_irqsave() and local_unlock_irqrestore() occurring on different
CPUs.

Usecase
========
For example, When I developed a driver, I encountered a warning like
"WARNING: CPU: 4 PID: 260 at kernel/rcu/tree_plugin.h:315
rcu_note_context_switch+0xa8/0x4e8" warning. It took me half a month
to locate this issue. Ultimately, I discovered that the lack of upper
overflow detection mechanism in migrate_disable() was the root cause,
leading to a significant amount of time spent on problem localization.

If the upper overflow detection mechanism was added to migrate_disable(),
the root cause could be very quickly and easily identified.

Effect
======
Using WARN_ON_ONCE() to check if 'migration_disabled' is upper overflow
can help developers identify the issue quickly.

Suggested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Peilin He<he.peilin@zte.com.cn>
Signed-off-by: xu xin <xu.xin16@zte.com.cn>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Yunkai Zhang <zhang.yunkai@zte.com.cn>
Reviewed-by: Qiang Tu <tu.qiang35@zte.com.cn>
Reviewed-by: Kun Jiang <jiang.kun2@zte.com.cn>
Reviewed-by: Fan Yu <fan.yu9@zte.com.cn>
Link: https://lkml.kernel.org/r/20240716104244764N2jD8gnBpnsLjCDnQGQ8c@zte.com.cn

authored by

Peilin He and committed by
Peter Zijlstra
0ec8d5ae c40dd90a

+15 -3
+15 -3
kernel/sched/core.c
··· 2233 2233 struct task_struct *p = current; 2234 2234 2235 2235 if (p->migration_disabled) { 2236 + #ifdef CONFIG_DEBUG_PREEMPT 2237 + /* 2238 + *Warn about overflow half-way through the range. 2239 + */ 2240 + WARN_ON_ONCE((s16)p->migration_disabled < 0); 2241 + #endif 2236 2242 p->migration_disabled++; 2237 2243 return; 2238 2244 } ··· 2257 2251 .flags = SCA_MIGRATE_ENABLE, 2258 2252 }; 2259 2253 2254 + #ifdef CONFIG_DEBUG_PREEMPT 2255 + /* 2256 + * Check both overflow from migrate_disable() and superfluous 2257 + * migrate_enable(). 2258 + */ 2259 + if (WARN_ON_ONCE((s16)p->migration_disabled <= 0)) 2260 + return; 2261 + #endif 2262 + 2260 2263 if (p->migration_disabled > 1) { 2261 2264 p->migration_disabled--; 2262 2265 return; 2263 2266 } 2264 - 2265 - if (WARN_ON_ONCE(!p->migration_disabled)) 2266 - return; 2267 2267 2268 2268 /* 2269 2269 * Ensure stop_task runs either before or after this, and that