mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
*  [PATCH linux-next RESEND] sched/core: Add WARN() to checks in migrate_disable()
@ 2024-07-02 11:33 xu.xin16
  2024-07-02 16:43 ` Steven Rostedt
  0 siblings, 1 reply; 2+ messages in thread
From: xu.xin16 @ 2024-07-02 11:33 UTC (permalink / raw)
  To: mingo, peterz, juri.lelli, vincent.guittot, ietmar.eggemann,
	ostedt, bsegall, mgorman, bristot
  Cc: he.peilin, yang.yang29, tu.qiang35, jiang.kun2, xu.xin16,
	zhang.yunkai, liu.chun2, fan.yu9, linux-kernel

From: Peilin He <he.peilin@zte.com.cn>

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() to check if 'migration_disabled' is upper overflow can help
developers identify the issue quickly.

Signed-off-by: Peilin He<he.peilin@zte.com.cn>
Reviewed-by: xu xin <xu.xin16@zte.com.cn>
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>
Cc: Yang Yang <yang.yang29@zte.com.cn>
Cc: Liu Chun <liu.chun2@zte.com.cn>
---
 kernel/sched/core.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 8cc4975d6b2b..14671291564c 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2259,6 +2259,8 @@ void migrate_disable(void)
 	struct task_struct *p = current;

 	if (p->migration_disabled) {
+		if (p->migration_disabled == USHRT_MAX)
+			WARN(1, "migration_disabled has encountered an overflow.\n");
 		p->migration_disabled++;
 		return;
 	}
-- 
2.17.1

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re:  [PATCH linux-next RESEND] sched/core: Add WARN() to checks in migrate_disable()
  2024-07-02 11:33 [PATCH linux-next RESEND] sched/core: Add WARN() to checks in migrate_disable() xu.xin16
@ 2024-07-02 16:43 ` Steven Rostedt
  0 siblings, 0 replies; 2+ messages in thread
From: Steven Rostedt @ 2024-07-02 16:43 UTC (permalink / raw)
  To: xu.xin16
  Cc: mingo, peterz, juri.lelli, vincent.guittot, ietmar.eggemann,
	ostedt, bsegall, mgorman, bristot, he.peilin, yang.yang29,
	tu.qiang35, jiang.kun2, zhang.yunkai, liu.chun2, fan.yu9,
	linux-kernel

On Tue, 2 Jul 2024 19:33:59 +0800 (CST)
<xu.xin16@zte.com.cn> wrote:

> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -2259,6 +2259,8 @@ void migrate_disable(void)
>  	struct task_struct *p = current;
> 
>  	if (p->migration_disabled) {
> +		if (p->migration_disabled == USHRT_MAX)
> +			WARN(1, "migration_disabled has encountered an overflow.\n");

This is redundant, you want:

		WARN(p->migration_disabled == USHRT_MAX, "migration_disabled has encountered an overflow.");

I'm not sure the '\n' is needed, but it seems like 50% of WARN() has
them, and 50% do not.

-- Steve


>  		p->migration_disabled++;
>  		return;
>  	}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-07-02 16:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-02 11:33 [PATCH linux-next RESEND] sched/core: Add WARN() to checks in migrate_disable() xu.xin16
2024-07-02 16:43 ` Steven Rostedt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®