Re: [PATCH 2/4] arm64: Extract kprobes_save_local_irqflag() and kprobes_restore_local_irqflag()
From: liwei (GF)
Date: Sat May 16 2020 - 04:47:16 EST
Hi Douglas,
On 2020/5/14 8:21, Doug Anderson wrote:
(SNIP)
>> +/*
>> + * Interrupts need to be disabled before single-step mode is set, and not
>> + * reenabled until after single-step mode ends.
>> + * Without disabling interrupt on local CPU, there is a chance of
>> + * interrupt occurrence in the period of exception return and start of
>> + * out-of-line single-step, that result in wrongly single stepping
>> + * into the interrupt handler.
>> + */
>> +void kernel_prepare_single_step(unsigned long *flags,
>> + struct pt_regs *regs)
>> +{
>> + *flags = regs->pstate & DAIF_MASK;
>> + regs->pstate |= PSR_I_BIT;
>> + /* Unmask PSTATE.D for enabling software step exceptions. */
>> + regs->pstate &= ~PSR_D_BIT;
>> +}
>> +NOKPROBE_SYMBOL(kernel_prepare_single_step);
>
> nit: why not just return unsigned long rather than passing by reference?
Because i just extract this function from kprobes_save_local_irqflag(), i think
return unsigned long is fine.
>
>> +
>> +void kernel_cleanup_single_step(unsigned long flags,
>> + struct pt_regs *regs)
>> +{
>> + regs->pstate &= ~DAIF_MASK;
>> + regs->pstate |= flags;
>> +}
>> +NOKPROBE_SYMBOL(kernel_cleanup_single_step);
>> +
>> /* ptrace API */
>> void user_enable_single_step(struct task_struct *task)
>> {
>> diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c
>> index d1c95dcf1d78..c655b6b543e3 100644
>> --- a/arch/arm64/kernel/probes/kprobes.c
>> +++ b/arch/arm64/kernel/probes/kprobes.c
>> @@ -168,30 +168,6 @@ static void __kprobes set_current_kprobe(struct kprobe *p)
>> __this_cpu_write(current_kprobe, p);
>> }
>>
>> -/*
>> - * Interrupts need to be disabled before single-step mode is set, and not
>> - * reenabled until after single-step mode ends.
>> - * Without disabling interrupt on local CPU, there is a chance of
>> - * interrupt occurrence in the period of exception return and start of
>> - * out-of-line single-step, that result in wrongly single stepping
>> - * into the interrupt handler.
>> - */
>> -static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
>> - struct pt_regs *regs)
>> -{
>> - kcb->saved_irqflag = regs->pstate & DAIF_MASK;
>> - regs->pstate |= PSR_I_BIT;
>> - /* Unmask PSTATE.D for enabling software step exceptions. */
>> - regs->pstate &= ~PSR_D_BIT;
>> -}
>> -
>> -static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
>> - struct pt_regs *regs)
>> -{
>> - regs->pstate &= ~DAIF_MASK;
>> - regs->pstate |= kcb->saved_irqflag;
>> -}
>> -
>> static void __kprobes
>> set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr)
>> {
>> @@ -227,7 +203,7 @@ static void __kprobes setup_singlestep(struct kprobe *p,
>> set_ss_context(kcb, slot); /* mark pending ss */
>>
>> /* IRQs and single stepping do not mix well. */
>> - kprobes_save_local_irqflag(kcb, regs);
>> + kernel_prepare_single_step(&kcb->saved_irqflag, regs);
>
> Is there some reason to have two functions? It seems like every time
> you call kernel_enable_single_step() you'd want to call
> kernel_prepare_single_step(). ...and every time you call
> kernel_disable_single_step() you'd want to call
> kernel_cleanup_single_step().
>
> Maybe you can just add the flags parameter to
> kernel_enable_single_step() / kernel_disable_single_step() and put the
> code in there?
>
As kernel_enable_single_step() / kernel_disable_single_step() are also called in
breakpoint_handler() and watchpoint_handler(), i am not sure it's a right thing
to put the daif flag prepare/cleanup into them, especially we don't have a context
to save the flags.
Thanks,
Wei