Re: [PATCH V5 06/11] entry: Prevent DEBUG_PREEMPT warning

Peter Zijlstra
2022-09-19T11:59:29Z

On Sun, Sep 18, 2022 at 11:52:41AM -0400, guoren@kernel.org wrote:
> From: Guo Ren <guoren@linux.alibaba.com>
>
> When DEBUG_PREEMPT=y,
> exit_to_user_mode_prepare
> ->tick_nohz_user_enter_prepare
> ->tick_nohz_full_cpu(smp_processor_id())
> ->smp_processor_id()
> ->debug_smp_processor_id()
> ->check preempt_count() then:
>
> [ 5.717610] BUG: using smp_processor_id() in preemptible [00000000]
> code: S20urandom/94
> [ 5.718111] caller is debug_smp_processor_id+0x24/0x38
> [ 5.718417] CPU: 1 PID: 94 Comm: S20urandom Not tainted
> 6.0.0-rc3-00010-gfd0a0d619c63-dirty #238
> [ 5.718886] Hardware name: riscv-virtio,qemu (DT)
> [ 5.719136] Call Trace:
> [ 5.719281] [<ffffffff800055fc>] dump_backtrace+0x2c/0x3c
> [ 5.719566] [<ffffffff80ae6cb0>] show_stack+0x44/0x5c
> [ 5.720023] [<ffffffff80aee870>] dump_stack_lvl+0x74/0xa4
> [ 5.720557] [<ffffffff80aee8bc>] dump_stack+0x1c/0x2c
> [ 5.721033] [<ffffffff80af65c0>]
> check_preemption_disabled+0x104/0x108
> [ 5.721538] [<ffffffff80af65e8>] debug_smp_processor_id+0x24/0x38
> [ 5.722001] [<ffffffff800aee30>] exit_to_user_mode_prepare+0x48/0x178
> [ 5.722355] [<ffffffff80af5bf4>] irqentry_exit_to_user_mode+0x18/0x30
> [ 5.722685] [<ffffffff80af5c70>] irqentry_exit+0x64/0xa4
> [ 5.722953] [<ffffffff80af52f4>] do_page_fault+0x1d8/0x544
> [ 5.723291] [<ffffffff80003310>] ret_from_exception+0x0/0xb8
>
> (Above is found in riscv platform with generic_entry)
>
> The smp_processor_id() needs irqs disable or preempt_disable, so use
> preempt dis/in protecting the tick_nohz_user_enter_prepare().
>
> Signed-off-by: Guo Ren <guoren@linux.alibaba.com>
> Signed-off-by: Guo Ren <guoren@kernel.org>
> ---
> kernel/entry/common.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/kernel/entry/common.c b/kernel/entry/common.c
> index 063068a9ea9b..36e4cd50531c 100644
> --- a/kernel/entry/common.c
> +++ b/kernel/entry/common.c
> @@ -194,8 +194,10 @@ static void exit_to_user_mode_prepare(struct pt_regs *regs)
>
> lockdep_assert_irqs_disabled();

Observe ^^^^

>
> + preempt_disable();
> /* Flush pending rcuog wakeup before the last need_resched() check */
> tick_nohz_user_enter_prepare();
> + preempt_enable();

This makes no sense; if IRQs are disabled, check_preemption_disabled()
should bail early per:

if (irqs_disabled())
goto out;