From patchwork Fri Feb 25 03:34:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12759615 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3DE0FC433EF for ; Fri, 25 Feb 2022 03:44:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:From:Subject:References:Mime-Version :Message-Id:In-Reply-To:Date:Reply-To:To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Cvv5VvOF0OA9CflvuQ6l1+aCYq8DDYWAq9n3GTEH4Ts=; b=3llGQHdFvmEO69 kICT63vnq5FqGoCbArTU8zxx8lZ9CN73sOkg49VtaQpTdeGldlM5Xg2DL3ExX+mW5qINx0so5kVes fjUTgek9a0XVxT+2ML+m672U54yDgrnsFDPYjEEjqez372HT5/AmAurRloUbOeQ+QjMbH//fOsT5f h4BjdGKvcQCSoCjaD4E67o4QawDE41pLKgB5aU/NVqVeWoJvecsXc7s81nIvXZ6hqErmNCuPmKxFh /p/lqI3IZ03b2AGjYwCh30vKf5HmU5O7dKxGIGE9rknYN/5p/r5wGhYiZb2f3WPdlUFFEJR9NK0+Y IVpPLTcoKaH2VMOo4wfQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nNRVT-00385g-LJ; Fri, 25 Feb 2022 03:42:59 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nNRVP-00383l-0T for linux-arm-kernel@lists.infradead.org; Fri, 25 Feb 2022 03:42:56 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id k10-20020a056902070a00b0062469b00335so1989210ybt.14 for ; Thu, 24 Feb 2022 19:42:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:cc; bh=TYfrDDnCDZLgsaXn4hYzYqjwtRqrff+8WzYcoEgftOI=; b=q7666YkutUkL6RKXDHN6uW1bfeRRma2VbYbkjuziO8kN3Ez+AvP99CPA+crFQIMyGM u8V98l/kcyJmZrAMYC8lp0VZdsx7hfOnk0og8Iyu8/EPTcDyOrtXaylNfruGAYrBELML 0t4kJOa3EW5NNbIbFy4ls3stZzs7gzcL6saNGB+qXhJn3B64HD/5jMrLH5g2Qj/oJRUN C3NdGCf8ofRIATki5ntXpIdGQYYOMg+4xfPq2uDJ2UMg5PbTBm+zuybq7cU5vTAp9A5R OadOj9nn2g7Sk/FRIHG0Q30twQG0Zl6SxdUGzB8zR5q1zrX1sy32E1VtmvDod4V091/Q RyvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:cc; bh=TYfrDDnCDZLgsaXn4hYzYqjwtRqrff+8WzYcoEgftOI=; b=7JBkz5zAPdmB6Yt14tL//08Zh3Wr3KtxEIjmU15IBlBc9KjL5sTuZ2LvSPW0sdp31e HM2VqhuM6lL0Lg2dfJLMBKA/VFZ6/3ff8FGmTvKHJfx5bA9TUzU/1qpU0ci35wBYSmVg hG7ak8rz017QIGb1PpYf2mV7Pg1Uday1uEKYs9PG5iW/X0RtUolm4bb/V9izLTfPVuNi 8h761g7JURNJwDYXyVODoEiHtTA17St3nXGlbhaW4ntvpvm7tXHaJGqCOodGoPlHzEUF Ky7zVvQyaDAedhOBQxwbniHotXTPsohXDKveVscufe0qZlRerK/n0OL7tWaFAR8Has7y vmxQ== X-Gm-Message-State: AOAM531Mge+1gC7qy6bP8mVDis6NfURkiiMZpvsK7h17SD+HSd/8zkjF GA0dVpOMVL1/fKR+M5E6mpzOFG/IKxnwFk9YAw== X-Google-Smtp-Source: ABdhPJyuZ08nciNdihLr0dH67Rir4ZvyzFgkAlltso3KgjJwiOs2qrT2Nw+pxsQEIBxp93gHWjAaX4W2t6yMU0pQ5Q== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:3a50:90b2:e6a2:9060]) (user=kaleshsingh job=sendgmr) by 2002:a25:808f:0:b0:624:a5e8:9762 with SMTP id n15-20020a25808f000000b00624a5e89762mr5469937ybk.279.1645760573552; Thu, 24 Feb 2022 19:42:53 -0800 (PST) Date: Thu, 24 Feb 2022 19:34:49 -0800 In-Reply-To: <20220225033548.1912117-1-kaleshsingh@google.com> Message-Id: <20220225033548.1912117-5-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220225033548.1912117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.35.1.574.g5d30c73bfb-goog Subject: [PATCH v4 4/8] KVM: arm64: Add guard pages for pKVM (protected nVHE) hypervisor stack From: Kalesh Singh Cc: will@kernel.org, maz@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, Kalesh Singh , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Mark Rutland , Mark Brown , Masami Hiramatsu , Peter Collingbourne , "Madhavan T. Venkataraman" , Andrew Walbran , Andrew Scull , Ard Biesheuvel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220224_194255_087092_8C90E573 X-CRM114-Status: GOOD ( 13.87 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Maps the stack pages in the flexible private VA range and allocates guard pages below the stack as unbacked VA space. The stack is aligned to twice its size to aid overflow detection (implemented in a subsequent patch in the series). Signed-off-by: Kalesh Singh --- Changes in v4: - Replace IS_ERR_OR_NULL check with IS_ERR check now that pkvm_alloc_private_va_range() returns an error for null pointer, per Fuad Changes in v3: - Handle null ptr in IS_ERR_OR_NULL checks, per Mark arch/arm64/kvm/hyp/nvhe/setup.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c index 27af337f9fea..1b69a25c1861 100644 --- a/arch/arm64/kvm/hyp/nvhe/setup.c +++ b/arch/arm64/kvm/hyp/nvhe/setup.c @@ -105,11 +105,28 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, if (ret) return ret; - end = (void *)per_cpu_ptr(&kvm_init_params, i)->stack_hyp_va; + /* + * Private mappings are allocated upwards from __io_map_base + * so allocate the guard page first then the stack. + */ + start = (void *)pkvm_alloc_private_va_range(PAGE_SIZE, PAGE_SIZE); + if (IS_ERR(start)) + return PTR_ERR(start); + + /* + * The stack is aligned to twice its size to facilitate overflow + * detection. + */ + end = (void *)per_cpu_ptr(&kvm_init_params, i)->stack_pa; start = end - PAGE_SIZE; - ret = pkvm_create_mappings(start, end, PAGE_HYP); - if (ret) - return ret; + start = (void *)__pkvm_create_private_mapping((phys_addr_t)start, + PAGE_SIZE, PAGE_SIZE * 2, PAGE_HYP); + if (IS_ERR(start)) + return PTR_ERR(start); + end = start + PAGE_SIZE; + + /* Update stack_hyp_va to end of the stack's private VA range */ + per_cpu_ptr(&kvm_init_params, i)->stack_hyp_va = (unsigned long) end; } /*