Skip to contentSkip to conversation

sched: replace if (cond) BUG() with BUG_ON()

PATCH In review Jiapeng Chong 3 participants 6 messages Mar 17, 2021 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched: replace if (cond) BUG() with BUG_ON()

Fix the following coccicheck warnings:

./kernel/sched/core.c:8039:2-5: WARNING: Use BUG_ON instead of if condition followed by BUG.

---
 kernel/sched/core.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/sched/core.c +1 -2
index 9819121..7392bc0 100644
@@ -8035,8 +8035,7 @@ void __init sched_init_smp(void)
 	mutex_unlock(&sched_domains_mutex);
 
 	/* Move init over to a non-isolated CPU */
-	if (set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0)
-		BUG();
+	BUG(set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0);
 	sched_init_granularity();
 
 	init_sched_rt_class();
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 16 lines · Jiapeng Chong

Fix the following coccicheck warnings:

./kernel/sched/core.c:8039:2-5: WARNING: Use BUG_ON instead of if condition followed by BUG.

---
 kernel/sched/core.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/sched/core.c +0 -0
index 9819121..7392bc0 100644

kernel/sched/core.c void __init sched_init_smp(void) · line 8038

Quoted 6 lines
kernel/sched/core.c +1 -2
@@ -8035,8 +8035,7 @@ void __init sched_init_smp(void)
 	mutex_unlock(&sched_domains_mutex);
 
 	/* Move init over to a non-isolated CPU */
-	if (set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0)
-		BUG();
+	BUG(set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0);

The patch doesn't quite do what the title & changelog claims...

Thanks,

Ingo

#2 lore raw
Quoted 21 lines · Ingo Molnar
Quoted 18 lines · Jiapeng Chong

Fix the following coccicheck warnings:

./kernel/sched/core.c:8039:2-5: WARNING: Use BUG_ON instead of if condition followed by BUG.

---
 kernel/sched/core.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/sched/core.c +0 -0
index 9819121..7392bc0 100644
@@ -8035,8 +8035,7 @@ void __init sched_init_smp(void)
 	mutex_unlock(&sched_domains_mutex);
 
Quoted 6 lines

kernel/sched/core.c void __init sched_init_smp(void) · line 8038

Quoted 4 lines
kernel/sched/core.c +1 -2
 	/* Move init over to a non-isolated CPU */
-	if (set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0)
-		BUG();
+	BUG(set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0);

The patch doesn't quite do what the title & changelog claims...

More importantly, we use this pattern when we don't want !CONFIG_BUG to remove the 'condition'.

I.e. the "side effect" here is important scheduler logic. It must never be optimized out.

Thanks,

Ingo

#3 lore raw
Quoted 28 lines · Ingo Molnar
Quoted 20 lines · Ingo Molnar
Quoted 18 lines · Jiapeng Chong

Fix the following coccicheck warnings:

./kernel/sched/core.c:8039:2-5: WARNING: Use BUG_ON instead of if condition followed by BUG.

---
 kernel/sched/core.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/sched/core.c +0 -0
index 9819121..7392bc0 100644
@@ -8035,8 +8035,7 @@ void __init sched_init_smp(void)
    mutex_unlock(&sched_domains_mutex);
 
Quoted 6 lines

kernel/sched/core.c void __init sched_init_smp(void) · line 8038

Quoted 4 lines
kernel/sched/core.c +1 -2
    /* Move init over to a non-isolated CPU */
-   if (set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0)
-           BUG();
+   BUG(set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0);

The patch doesn't quite do what the title & changelog claims...

Quoted 5 lines

More importantly, we use this pattern when we don't want !CONFIG_BUG to remove the 'condition'.

I.e. the "side effect" here is important scheduler logic. It must never be optimized out.

This behavior for !CONFIG_BUG has changed a while ago, it is now safe to rely on the side-effect of the BUG_ON() condition regardless of CONFIG_BUG. When that option is disabled, running into the condition just ends up in a "do {} while (1)" loop.

Arnd

#4 lore raw
Quoted 32 lines · Arnd Bergmann
Quoted 31 lines · Ingo Molnar
Quoted 20 lines · Ingo Molnar
Quoted 18 lines · Jiapeng Chong

Fix the following coccicheck warnings:

./kernel/sched/core.c:8039:2-5: WARNING: Use BUG_ON instead of if condition followed by BUG.

---
 kernel/sched/core.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/sched/core.c +0 -0
index 9819121..7392bc0 100644
@@ -8035,8 +8035,7 @@ void __init sched_init_smp(void)
    mutex_unlock(&sched_domains_mutex);
 
Quoted 6 lines

kernel/sched/core.c void __init sched_init_smp(void) · line 8038

Quoted 4 lines
kernel/sched/core.c +1 -2
    /* Move init over to a non-isolated CPU */
-   if (set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0)
-           BUG();
+   BUG(set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0);

The patch doesn't quite do what the title & changelog claims...

More importantly, we use this pattern when we don't want !CONFIG_BUG to remove the 'condition'.

Quoted 7 lines
Quoted 2 lines

I.e. the "side effect" here is important scheduler logic. It must never be optimized out.

This behavior for !CONFIG_BUG has changed a while ago, it is now safe to rely on the side-effect of the BUG_ON() condition regardless of CONFIG_BUG. When that option is disabled, running into the condition just ends up in a "do {} while (1)" loop.

Dunno, I still think it's not a particularly clean pattern to 'hide' significant side effects within a BUG_ON().

Thanks,

Ingo

#5 lore raw
Quoted 9 lines · Ingo Molnar
Quoted 8 lines · Arnd Bergmann
Quoted 6 lines · Ingo Molnar

More importantly, we use this pattern when we don't want !CONFIG_BUG to remove the 'condition'.

I.e. the "side effect" here is important scheduler logic. It must never be optimized out.

Quoted 7 lines
Quoted 4 lines

This behavior for !CONFIG_BUG has changed a while ago, it is now safe to rely on the side-effect of the BUG_ON() condition regardless of CONFIG_BUG. When that option is disabled, running into the condition just ends up in a "do {} while (1)" loop.

Dunno, I still think it's not a particularly clean pattern to 'hide' significant side effects within a BUG_ON().

Fair enough. Readability really is the key here, and I agree the current version is easier to understand. The only architectures that even define an optimized BUG_ON() are mips and powerpc, and saving a few cycles is barely worth it in a fast path, which this is clearly not.

Arnd