Re: [PATCH] sched: replace if (cond) BUG() with BUG_ON()
On Wed, Mar 17, 2021 at 9:05 PM Ingo Molnar wrote: > * Arnd Bergmann wrote: > > On Wed, Mar 17, 2021 at 9:45 AM Ingo Molnar wrote: > > > > > > 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. > > 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
Re: [PATCH] sched: replace if (cond) BUG() with BUG_ON()
* Arnd Bergmann wrote: > On Wed, Mar 17, 2021 at 9:45 AM Ingo Molnar wrote: > > * Ingo Molnar wrote: > > > * Jiapeng Chong wrote: > > > > > > > Fix the following coccicheck warnings: > > > > > > > > ./kernel/sched/core.c:8039:2-5: WARNING: Use BUG_ON instead of if > > > > condition followed by BUG. > > > > > > > > Reported-by: Abaci Robot > > > > Signed-off-by: Jiapeng Chong > > > > --- > > > > kernel/sched/core.c | 3 +-- > > > > 1 file changed, 1 insertion(+), 2 deletions(-) > > > > > > > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > > > > index 9819121..7392bc0 100644 > > > > --- a/kernel/sched/core.c > > > > +++ b/kernel/sched/core.c > > > > @@ -8035,8 +8035,7 @@ void __init sched_init_smp(void) > > > > mutex_unlock(_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... > > > > 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. Dunno, I still think it's not a particularly clean pattern to 'hide' significant side effects within a BUG_ON(). Thanks, Ingo
Re: [PATCH] sched: replace if (cond) BUG() with BUG_ON()
On Wed, Mar 17, 2021 at 9:45 AM Ingo Molnar wrote: > * Ingo Molnar wrote: > > * Jiapeng Chong wrote: > > > > > Fix the following coccicheck warnings: > > > > > > ./kernel/sched/core.c:8039:2-5: WARNING: Use BUG_ON instead of if > > > condition followed by BUG. > > > > > > Reported-by: Abaci Robot > > > Signed-off-by: Jiapeng Chong > > > --- > > > kernel/sched/core.c | 3 +-- > > > 1 file changed, 1 insertion(+), 2 deletions(-) > > > > > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > > > index 9819121..7392bc0 100644 > > > --- a/kernel/sched/core.c > > > +++ b/kernel/sched/core.c > > > @@ -8035,8 +8035,7 @@ void __init sched_init_smp(void) > > > mutex_unlock(_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... > > 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
Re: [PATCH] sched: replace if (cond) BUG() with BUG_ON()
* Ingo Molnar wrote: > > * Jiapeng Chong wrote: > > > Fix the following coccicheck warnings: > > > > ./kernel/sched/core.c:8039:2-5: WARNING: Use BUG_ON instead of if > > condition followed by BUG. > > > > Reported-by: Abaci Robot > > Signed-off-by: Jiapeng Chong > > --- > > kernel/sched/core.c | 3 +-- > > 1 file changed, 1 insertion(+), 2 deletions(-) > > > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > > index 9819121..7392bc0 100644 > > --- a/kernel/sched/core.c > > +++ b/kernel/sched/core.c > > @@ -8035,8 +8035,7 @@ void __init sched_init_smp(void) > > mutex_unlock(_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... 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
Re: [PATCH] sched: replace if (cond) BUG() with BUG_ON()
* Jiapeng Chong wrote: > Fix the following coccicheck warnings: > > ./kernel/sched/core.c:8039:2-5: WARNING: Use BUG_ON instead of if > condition followed by BUG. > > Reported-by: Abaci Robot > Signed-off-by: Jiapeng Chong > --- > kernel/sched/core.c | 3 +-- > 1 file changed, 1 insertion(+), 2 deletions(-) > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index 9819121..7392bc0 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -8035,8 +8035,7 @@ void __init sched_init_smp(void) > mutex_unlock(_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
Re: [PATCH] sched: replace if (cond) BUG() with BUG_ON()
Le 17/03/2021 à 07:45, Jiapeng Chong a écrit : Fix the following coccicheck warnings: ./arch/powerpc/platforms/cell/spufs/sched.c:908:2-5: WARNING: Use BUG_ON instead of if condition followed by BUG. Consider using WARN_ON() instead of BUG_ON() if relevant. If not, explain in the commit message why we need to keep a BUG_ON(). See https://www.kernel.org/doc/html/latest/process/deprecated.html#bug-and-bug-on Reported-by: Abaci Robot Signed-off-by: Jiapeng Chong --- arch/powerpc/platforms/cell/spufs/sched.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/powerpc/platforms/cell/spufs/sched.c b/arch/powerpc/platforms/cell/spufs/sched.c index 3692064..139a6ec 100644 --- a/arch/powerpc/platforms/cell/spufs/sched.c +++ b/arch/powerpc/platforms/cell/spufs/sched.c @@ -904,8 +904,7 @@ static noinline void spusched_tick(struct spu_context *ctx) struct spu_context *new = NULL; struct spu *spu = NULL; - if (spu_acquire(ctx)) - BUG(); /* a kernel thread never has signals pending */ + BUG_ON(spu_acquire(ctx)); /* a kernel thread never has signals pending */ if (ctx->state != SPU_STATE_RUNNABLE) goto out;