Re: for_each_cpu() is buggy for UP kernel?

From: Linus Torvalds
Date: Tue May 15 2018 - 13:41:57 EST


On Mon, May 14, 2018 at 8:02 PM Dexuan Cui <decui@xxxxxxxxxxxxx> wrote:

> If you're OK with the below fix (not tested yet), I'll submit a patch for
it:

> --- a/kernel/time/tick-broadcast.c
> +++ b/kernel/time/tick-broadcast.c
> @@ -616,6 +616,10 @@ static void tick_handle_oneshot_broadcast(struct
clock_event_device *dev)
> now = ktime_get();
> /* Find all expired events */
> for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
> +#ifndef CONFIG_SMP
> + if (cpumask_empty(tick_broadcast_oneshot_mask))
> + break;
> +#endif

I'm certainly ok with this. It's hacky, but maybe being explicitly hacky is
good to "document" this gotcha.

And I really do agree that this special UP case is nasty nasty and much too
subtle, and I hope that some day we won't care about UP at all, and maybe
kill it, or maybe just make for_each_cpu() generate the extra code to have
the actual same semantics as the SMP case.

Linus