Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp126327pxm; Tue, 22 Feb 2022 18:35:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJxNjHpXt9zQ2ka17y9xCD0JaNfsI7awIz/Q7Jor3a4joCFWz5xQbuc6hzDhEzd1BnF4gp59 X-Received: by 2002:a17:902:7e4f:b0:14f:dc56:95c9 with SMTP id a15-20020a1709027e4f00b0014fdc5695c9mr6168722pln.9.1645583710386; Tue, 22 Feb 2022 18:35:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645583710; cv=none; d=google.com; s=arc-20160816; b=jcy3BFL8m93NiHoX19Qn5kHA24mOLy8UJUUdrOuYoet54Kwiysvt+vdl2NpWxqki5z 2naaIEpCo0kjxs1o/6Uh2CpI+tPx8uh99lEKYJPgL6WAo/ItlNDPBFEjfZG0wyeqvGLn lSEctnx4K7cZFxM3CbSj6B6a1jCkfYzENnBVR9ymFxquY+5wa538kA5FFnqMkeV3jhjl UIBuxLoU+L6lgppgVUxfNGwRhU+Loi5LcFQFXas+4zRvPZaaHsEdlBrvL/37apP+i6tm XP4fSUMLy0SvSLg2riIuRY/R3Bsd2CTC2OpKE+D3i2UdoaRhTGiMF3P4KZuHen+aKX5g I3Gw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=B/YRXI4ec/Oe28dzVk9y2XZh8WDl1L+YDnsPXbLEPWg=; b=zyCrawABFt20CsNU/b/l2Vsu6oCqB+6VcsFar7hC9Qz/zkNfNF7nTkvf/o1XWq3Emn HScneBokVq5A6POUvQnfs6urThZx9Q0amYR5s6zqPXEQmMW+MZkVA8CNNfTNljMqHQY3 YTZs01CkETiJwX5txki34jB6G/KR6CwNaMRYvmZuWm3tcf7teNh+AI7YIna4wO0V1L8Q kAsIoFnL4E+mJ+g++wZSPpaEQOBVZtSX4ayLTGjYzwSHqk2MeRMMA8vw0uAs1wxrc6ad etmMmIi4L+5FHUOtkRlXMKilqcI4lB4pk4Ag//TalvbAjTK1ByH8ywrmkQkDcbs9A11w ALCQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ot8si1256430pjb.88.2022.02.22.18.34.54; Tue, 22 Feb 2022 18:35:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232893AbiBVSd1 (ORCPT + 99 others); Tue, 22 Feb 2022 13:33:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235050AbiBVSdD (ORCPT ); Tue, 22 Feb 2022 13:33:03 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 082137D03A for ; Tue, 22 Feb 2022 10:32:38 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C95D1139F; Tue, 22 Feb 2022 10:32:37 -0800 (PST) Received: from lakrids (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 61EB33F66F; Tue, 22 Feb 2022 10:32:35 -0800 (PST) Date: Tue, 22 Feb 2022 18:32:33 +0000 From: Mark Rutland To: Kalesh Singh Cc: will@kernel.org, maz@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, Catalin Marinas , James Morse , Alexandru Elisei , Suzuki K Poulose , Ard Biesheuvel , Pasha Tatashin , Joey Gouly , Peter Collingbourne , Andrew Scull , Paolo Bonzini , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvmarm@lists.cs.columbia.edu Subject: Re: [PATCH v2 5/9] arm64: asm: Introduce test_sp_overflow macro Message-ID: References: <20220222165212.2005066-1-kaleshsingh@google.com> <20220222165212.2005066-6-kaleshsingh@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220222165212.2005066-6-kaleshsingh@google.com> X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 22, 2022 at 08:51:06AM -0800, Kalesh Singh wrote: > From: Quentin Perret > > 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 > [Kalesh - Resolve minor conflicts] > Signed-off-by: Kalesh Singh > --- > 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. Thanks, Mark. ... > -- > 2.35.1.473.g83b2b277ed-goog >