[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAC_TJvdo2UwrZwKGhZUTnCF--CNsKJFKe+tZOQWgs0=BhpBF0g@mail.gmail.com>
Date: Tue, 22 Feb 2022 12:20:19 -0800
From: Kalesh Singh <kaleshsingh@...gle.com>
To: Mark Rutland <mark.rutland@....com>
Cc: Will Deacon <will@...nel.org>, Marc Zyngier <maz@...nel.org>,
Quentin Perret <qperret@...gle.com>,
Fuad Tabba <tabba@...gle.com>,
Suren Baghdasaryan <surenb@...gle.com>,
"Cc: Android Kernel" <kernel-team@...roid.com>,
Catalin Marinas <catalin.marinas@....com>,
James Morse <james.morse@....com>,
Alexandru Elisei <alexandru.elisei@....com>,
Suzuki K Poulose <suzuki.poulose@....com>,
Ard Biesheuvel <ardb@...nel.org>,
Pasha Tatashin <pasha.tatashin@...een.com>,
Joey Gouly <joey.gouly@....com>,
Peter Collingbourne <pcc@...gle.com>,
Andrew Scull <ascull@...gle.com>,
Paolo Bonzini <pbonzini@...hat.com>,
"moderated list:ARM64 PORT (AARCH64 ARCHITECTURE)"
<linux-arm-kernel@...ts.infradead.org>,
LKML <linux-kernel@...r.kernel.org>, kvmarm@...ts.cs.columbia.edu
Subject: Re: [PATCH v2 5/9] arm64: asm: Introduce test_sp_overflow macro
On Tue, Feb 22, 2022 at 10:32 AM Mark Rutland <mark.rutland@....com> wrote:
>
> On Tue, Feb 22, 2022 at 08:51:06AM -0800, Kalesh Singh wrote:
> > From: Quentin Perret <qperret@...gle.com>
> >
> > The asm entry code in the kernel uses a trick to check if VMAP'd stacks
> > have overflowed by aligning them at THREAD_SHIFT * 2 granularity and
> > checking the SP's THREAD_SHIFT bit.
> >
> > Protected KVM will soon make use of a similar trick to detect stack
> > overflows, so factor out the asm code in a re-usable macro.
> >
> > Signed-off-by: Quentin Perret <qperret@...gle.com>
> > [Kalesh - Resolve minor conflicts]
> > Signed-off-by: Kalesh Singh <kaleshsingh@...gle.com>
> > ---
> > arch/arm64/include/asm/assembler.h | 11 +++++++++++
> > arch/arm64/kernel/entry.S | 7 +------
> > 2 files changed, 12 insertions(+), 6 deletions(-)
> >
> > diff --git a/arch/arm64/include/asm/assembler.h b/arch/arm64/include/asm/assembler.h
> > index e8bd0af0141c..ad40eb0eee83 100644
> > --- a/arch/arm64/include/asm/assembler.h
> > +++ b/arch/arm64/include/asm/assembler.h
> > @@ -850,4 +850,15 @@ alternative_endif
> >
> > #endif /* GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT */
> >
> > +/*
> > + * Test whether the SP has overflowed, without corrupting a GPR.
> > + */
> > +.macro test_sp_overflow shift, label
> > + add sp, sp, x0 // sp' = sp + x0
> > + sub x0, sp, x0 // x0' = sp' - x0 = (sp + x0) - x0 = sp
> > + tbnz x0, #\shift, \label
> > + sub x0, sp, x0 // x0'' = sp' - x0' = (sp + x0) - sp = x0
> > + sub sp, sp, x0 // sp'' = sp' - x0 = (sp + x0) - x0 = sp
> > +.endm
>
> I'm a little unhappy about factoring this out, since it's not really
> self-contained and leaves sp and x0 partially-swapped when it branches
> to the label. You can't really make that clear with comments on the
> macro, and you need comments at each use-sire, so I'd ratehr we just
> open-coded a copy of this.
>
> > +
> > #endif /* __ASM_ASSEMBLER_H */
> > diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
> > index 772ec2ecf488..ce99ee30c77e 100644
> > --- a/arch/arm64/kernel/entry.S
> > +++ b/arch/arm64/kernel/entry.S
> > @@ -53,15 +53,10 @@ alternative_else_nop_endif
> > sub sp, sp, #PT_REGS_SIZE
> > #ifdef CONFIG_VMAP_STACK
> > /*
> > - * Test whether the SP has overflowed, without corrupting a GPR.
> > * Task and IRQ stacks are aligned so that SP & (1 << THREAD_SHIFT)
> > * should always be zero.
> > */
> > - add sp, sp, x0 // sp' = sp + x0
> > - sub x0, sp, x0 // x0' = sp' - x0 = (sp + x0) - x0 = sp
> > - tbnz x0, #THREAD_SHIFT, 0f
> > - sub x0, sp, x0 // x0'' = sp' - x0' = (sp + x0) - sp = x0
> > - sub sp, sp, x0 // sp'' = sp' - x0 = (sp + x0) - x0 = sp
> > + test_sp_overflow THREAD_SHIFT, 0f
> > b el\el\ht\()_\regsize\()_\label
> >
> > 0:
>
> Further to my comment above, immediately after this we have:
>
> /* Stash the original SP (minus PT_REGS_SIZE) in tpidr_el0. */
> msr tpidr_el0, x0
>
> /* Recover the original x0 value and stash it in tpidrro_el0 */
> sub x0, sp, x0
> msr tpidrro_el0, x0
>
> ... which is really surprising with the `test_sp_overflow` macro because
> it's not clear that modifies x0 and sp in this way.
Hi Mark,
I agree the macro hides the fact that sp and x0 are left in an
'corrupt' state if the branch happens. Not a problem in this case but
it could be misleading to new users. I'll remove this per your
suggestion in the next version.
Thanks,
Kalesh
>
> Thanks,
> Mark.
> ...
>
> > --
> > 2.35.1.473.g83b2b277ed-goog
> >
Powered by blists - more mailing lists