From patchwork Mon May 2 19:12:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12834638 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 005DFC433EF for ; Mon, 2 May 2022 19:13:58 +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=bE3r/cfP1IAcZw8daz+3u0KCVIuiQ5qOlEnU7OzJwro=; b=kNq+4Xfdt0OOem Nk3Eiku7j+c9+vpLRFX3fuBoUZPAjb5JIw2uRxRoQWShOZCYRC33kYQN9Zbt/QlE1vcR+otLZ9A4e G30LpNakW5yvbzsXkPAnogz5qPPBhVkDAM3G5EZ5qMI/jduvlswm6VEkGEnI/xcngZi3vUfyuqnui AiyggnLqF+LZ5L1yKV2RuFOauD/6OZiUWBKl7q/T2TxJj6RHDePjQCb0qcKYzLp3sJwjajGoqwghQ 2byag9XlppiJTEwbiY5QwkQxW/UGt128mUksz5Z/3IGKC0EOMcLOPUr5oqC+UGehje0481GigEf2J HOibOQj/O1ApIoPAX0rA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nlbTc-002Jrc-0e; Mon, 02 May 2022 19:12:56 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nlbTZ-002Jr6-6S for linux-arm-kernel@lists.infradead.org; Mon, 02 May 2022 19:12:54 +0000 Received: by mail-yb1-xb49.google.com with SMTP id b6-20020a253406000000b006484c081280so13909088yba.5 for ; Mon, 02 May 2022 12:12:51 -0700 (PDT) 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=SbkfFHwtj6LDfXcNEKyNCOiQxOwKu8gDiCPd3B2piLM=; b=EDJR3DTAX2eHb+n+nXs3NyHOQJ5iRpUPyz9W4sVPdLTKifwIuceSm9nqbUt+EAUjs8 Uy7YZCVBjsQZMuZZcYGoCTs3O2Q/0PWU1nemxTVCkyltR+Pfraxqcm/Mij0yMMW/vQpX 7o+MND69tDg8pttIB2MbNlqW61RbtG5ybH/CaNOoZlcGKOazY5whaBWWiNA+qS9nBW32 lGox+qsH97d8T7BvUlXqz4iiarm8kZeWQnrhNR/mPh4JMvWU2LVL4KaXwOnrglgIsYjJ 4umG8AwCaxD8GgaeNHACi79kPhW7xzf/d4gjHIFvgZyqSBDCJJIazbd++y+qCRLqv5Go POdw== 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=SbkfFHwtj6LDfXcNEKyNCOiQxOwKu8gDiCPd3B2piLM=; b=GynM6m4wtn7bM+Zor/6QT3fsZb69ziVevYhX91E3xJ07qc94twLKx+045d54Oenv9p WRWoOLXpqll3OnKUMHqHJXtiLfGRKiSpuIBe8CwVJpEGtblQTgYEeUJi///UdHHSIIVH D0iO4cghlvOL8FdOuSBV//yJFgaWEIJ4vnTcIRwSZTS5eZ5BHvIuAOS6saznQAIiw0hW qvlBqhXfYJjDAXBZYXxhfaNb/i2rMFTKM7Sh/a8S241gfm5noSWuZnFLO85PwkIECf5d bGyIr9ofnKiUwsPZDNqhxHodW5FxX/xhVIyAltkfEaQOo6Lwm7jg+jkeDj44vkpN7OTc p7eg== X-Gm-Message-State: AOAM531KRuRXNMou4XhXjuCtAWhnCdtjKzhNiMqvHFtQRVjlxfSKbP4u jDqcYcgKQZzKiXgb0b+qApu0h8CIJSy9Fv2nuw== X-Google-Smtp-Source: ABdhPJxP2xIlP7KifAzH0zbmoPkFGlaCnGx7XBRgqtsRS8QtrsuRrH/CX2j863LkhO0OA3feG1oXuqpQ/Sp0DoMkEA== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:55c:7548:7073:c14f]) (user=kaleshsingh job=sendgmr) by 2002:a0d:e8c7:0:b0:2f4:cd95:76d8 with SMTP id r190-20020a0de8c7000000b002f4cd9576d8mr12025072ywe.55.1651518771017; Mon, 02 May 2022 12:12:51 -0700 (PDT) Date: Mon, 2 May 2022 12:12:01 -0700 In-Reply-To: <20220502191222.4192768-1-kaleshsingh@google.com> Message-Id: <20220502191222.4192768-2-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220502191222.4192768-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.36.0.464.gb9c8b46e94-goog Subject: [PATCH v2 1/5] KVM: arm64: Factor out common stack unwinding logic From: Kalesh Singh Cc: mark.rutland@arm.com, broonie@kernel.org, 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 , Masami Hiramatsu , Peter Collingbourne , Alexei Starovoitov , "Madhavan T. Venkataraman" , Andrew Jones , Keir Fraser , Marco Elver , Kefeng Wang , Zenghui Yu , 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-20220502_121253_276761_F62EDEEB X-CRM114-Status: GOOD ( 16.56 ) 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 Factor out the stack unwinding logic common to both the host kernel and the nVHE hypersivor into __unwind_next(). This allows for reuse in the nVHE hypervisor stack unwinding (later in this series). Signed-off-by: Kalesh Singh Reviewed-by: Mark Brown --- arch/arm64/kernel/stacktrace.c | 36 +++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 0467cb79f080..ee60c279511c 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -81,23 +81,19 @@ NOKPROBE_SYMBOL(unwind_init); * records (e.g. a cycle), determined based on the location and fp value of A * and the location (but not the fp value) of B. */ -static int notrace unwind_next(struct task_struct *tsk, - struct unwind_state *state) +static int notrace __unwind_next(struct task_struct *tsk, + struct unwind_state *state, + struct stack_info *info) { unsigned long fp = state->fp; - struct stack_info info; - - /* Final frame; nothing to unwind */ - if (fp == (unsigned long)task_pt_regs(tsk)->stackframe) - return -ENOENT; if (fp & 0x7) return -EINVAL; - if (!on_accessible_stack(tsk, fp, 16, &info)) + if (!on_accessible_stack(tsk, fp, 16, info)) return -EINVAL; - if (test_bit(info.type, state->stacks_done)) + if (test_bit(info->type, state->stacks_done)) return -EINVAL; /* @@ -113,7 +109,7 @@ static int notrace unwind_next(struct task_struct *tsk, * stack to another, it's never valid to unwind back to that first * stack. */ - if (info.type == state->prev_type) { + if (info->type == state->prev_type) { if (fp <= state->prev_fp) return -EINVAL; } else { @@ -127,7 +123,25 @@ static int notrace unwind_next(struct task_struct *tsk, state->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp)); state->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8)); state->prev_fp = fp; - state->prev_type = info.type; + state->prev_type = info->type; + + return 0; +} +NOKPROBE_SYMBOL(__unwind_next); + +static int notrace unwind_next(struct task_struct *tsk, + struct unwind_state *state) +{ + struct stack_info info; + int err; + + /* Final frame; nothing to unwind */ + if (state->fp == (unsigned long)task_pt_regs(tsk)->stackframe) + return -ENOENT; + + err = __unwind_next(tsk, state, &info); + if (err) + return err; state->pc = ptrauth_strip_insn_pac(state->pc); From patchwork Mon May 2 19:12:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12834639 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 9C728C433F5 for ; Mon, 2 May 2022 19:14:24 +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=G/liNB5SIDdIf6uvRk+kmyY4u8sCLMTiXDWWwBgzhCc=; b=uBVYCS01fwavYa K7fmXQylLH77EZDuiQSmHPM0AZFbzT5WXNVm6irhYRATiCG+nFGsPrQ1VmqaSVZWliKLKBDQMzY6c QN7BnKFGh19qpVBR0MoRCjPFv9HefQpHvXPeZcDsCopA4HQv3paHBnKhCTyKMpNtAE4OE5AFcQUwC OV6rKGliC9U8g8wHkl2Qk1bTHr2fzJZJNQ7TDewtjjSyXeEQ6f81yx8EjSJAR+7CsHzBVIQbLqUZT LyhFEsFFiOlXSqG5JuxD6MYkRI/qc/PinnReF3O0N2J65zJJNka7D7p2X/5ZytmEF4hCuLLcEKs4H R/UTzGZxtpnrmmE+ZACA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nlbU5-002JwW-IX; Mon, 02 May 2022 19:13:25 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nlbU2-002Jvn-2e for linux-arm-kernel@lists.infradead.org; Mon, 02 May 2022 19:13:23 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2dc7bdd666fso142509987b3.7 for ; Mon, 02 May 2022 12:13:21 -0700 (PDT) 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=/bI0RHeA8Y22AxlgXD6e7A4C/dpyc69T8IyS6sPGv90=; b=Sin22pZTsc6T28/VmcgGpYyXlwVEiagPVH8gGRdS/r6ZVMcYq/l2DNUbd/UH3/QkMs p/+gTtCY/niBNLAhJoOefVSsFKMM/c88SL89BSA63IuZ3cfwvFI08P1OuK2RdvCjzLBr qUF5qcGV6Jm4e/6av+f1gHU3a/JMtMVlOJq7Pwez5ECfb3fFshKATFmMwKNSeqhaIqih GGyUCCT+tOf9J+gG8j+J9OY5cZ6Ar1kGTxdP/i+tJ8C9N14Pz00FT+93lrTfUmI2IwA1 bcCqJVJLvVrn/IbXgTc9dXoasshF+XmU0MAULG1/ZyGr4YYiQrdw3idt7UIQ/kHY2hGo MhwQ== 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=/bI0RHeA8Y22AxlgXD6e7A4C/dpyc69T8IyS6sPGv90=; b=hpYNa6ytFnwCz1dLQ0N0m+ixEShCx56xuu08d55P5H8EP95rh6p3d3//RImBdF89h9 WyqnPYS3tNn65HgYcKTjdxSQ4IQ/mMnIofUxcYQCicgy8S1c7577jQVwHMQEaaPU4TsK jczMPD9Ip5d2lQRH2XPJbxnmGQdGo4a2jdlnHunylgPyDfUxqek4AZoEXXdip5VlvvwY jOJ5745dj9d8DxZH7+wDi2bRvzpyLgaDx8cxPpXMIcnWaosiPSijI9r6wb6K95AhjQob CRjW34BpdhEfZygm99RMPplpJYWKXkX4/HuLBSUTCkuCpx8okbQAmWTNX8RqNrsZ856e zYbg== X-Gm-Message-State: AOAM53259/hkcYRPnMEfezhn1C0ax9NcxNRfcGNGxhdzMoq+HI48qlBo 0e+ChuRPq7u2ZYSDwnp85isU3JHItupld8gRlA== X-Google-Smtp-Source: ABdhPJwG8Jc3ZMgFFAnwpm1gcq8vn2y4rF+jHDc6argwAd5erYSjH3fDQtoG+c1wfL+lxMconIh1ekRrY/pl2Qm63w== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:55c:7548:7073:c14f]) (user=kaleshsingh job=sendgmr) by 2002:a25:e792:0:b0:645:7ddb:b5eb with SMTP id e140-20020a25e792000000b006457ddbb5ebmr11745947ybh.482.1651518800615; Mon, 02 May 2022 12:13:20 -0700 (PDT) Date: Mon, 2 May 2022 12:12:02 -0700 In-Reply-To: <20220502191222.4192768-1-kaleshsingh@google.com> Message-Id: <20220502191222.4192768-3-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220502191222.4192768-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.36.0.464.gb9c8b46e94-goog Subject: [PATCH v2 2/5] KVM: arm64: Compile stacktrace.nvhe.o From: Kalesh Singh Cc: mark.rutland@arm.com, broonie@kernel.org, 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 , Masami Hiramatsu , Peter Collingbourne , Alexei Starovoitov , "Madhavan T. Venkataraman" , Andrew Jones , Marco Elver , Kefeng Wang , Keir Fraser , Zenghui Yu , 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-20220502_121322_164778_31C6468A X-CRM114-Status: GOOD ( 15.88 ) 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 Recompile stack unwinding code for use with the nVHE hypervisor. This is a preparatory patch that will allow reusing most of the kernel unwinding logic in the nVHE hypervisor. Suggested-by: Mark Rutland Signed-off-by: Kalesh Singh --- Changes in v2: - Split out refactoring of common unwinding logic into a separate patch, per Mark Brown arch/arm64/include/asm/stacktrace.h | 18 +++++++++----- arch/arm64/kernel/stacktrace.c | 37 ++++++++++++++++------------- arch/arm64/kvm/hyp/nvhe/Makefile | 3 ++- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace.h b/arch/arm64/include/asm/stacktrace.h index aec9315bf156..f5af9a94c5a6 100644 --- a/arch/arm64/include/asm/stacktrace.h +++ b/arch/arm64/include/asm/stacktrace.h @@ -16,12 +16,14 @@ #include enum stack_type { - STACK_TYPE_UNKNOWN, +#ifndef __KVM_NVHE_HYPERVISOR__ STACK_TYPE_TASK, STACK_TYPE_IRQ, STACK_TYPE_OVERFLOW, STACK_TYPE_SDEI_NORMAL, STACK_TYPE_SDEI_CRITICAL, +#endif /* !__KVM_NVHE_HYPERVISOR__ */ + STACK_TYPE_UNKNOWN, __NR_STACK_TYPES }; @@ -31,11 +33,6 @@ struct stack_info { enum stack_type type; }; -extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, - const char *loglvl); - -DECLARE_PER_CPU(unsigned long *, irq_stack_ptr); - static inline bool on_stack(unsigned long sp, unsigned long size, unsigned long low, unsigned long high, enum stack_type type, struct stack_info *info) @@ -54,6 +51,12 @@ static inline bool on_stack(unsigned long sp, unsigned long size, return true; } +#ifndef __KVM_NVHE_HYPERVISOR__ +extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, + const char *loglvl); + +DECLARE_PER_CPU(unsigned long *, irq_stack_ptr); + static inline bool on_irq_stack(unsigned long sp, unsigned long size, struct stack_info *info) { @@ -88,6 +91,7 @@ static inline bool on_overflow_stack(unsigned long sp, unsigned long size, static inline bool on_overflow_stack(unsigned long sp, unsigned long size, struct stack_info *info) { return false; } #endif +#endif /* !__KVM_NVHE_HYPERVISOR__ */ /* @@ -101,6 +105,7 @@ static inline bool on_accessible_stack(const struct task_struct *tsk, if (info) info->type = STACK_TYPE_UNKNOWN; +#ifndef __KVM_NVHE_HYPERVISOR__ if (on_task_stack(tsk, sp, size, info)) return true; if (tsk != current || preemptible()) @@ -111,6 +116,7 @@ static inline bool on_accessible_stack(const struct task_struct *tsk, return true; if (on_sdei_stack(sp, size, info)) return true; +#endif /* !__KVM_NVHE_HYPERVISOR__ */ return false; } diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index ee60c279511c..a84e38d41d38 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -129,6 +129,26 @@ static int notrace __unwind_next(struct task_struct *tsk, } NOKPROBE_SYMBOL(__unwind_next); +static int notrace unwind_next(struct task_struct *tsk, + struct unwind_state *state); + +static void notrace unwind(struct task_struct *tsk, + struct unwind_state *state, + stack_trace_consume_fn consume_entry, void *cookie) +{ + while (1) { + int ret; + + if (!consume_entry(cookie, state->pc)) + break; + ret = unwind_next(tsk, state); + if (ret < 0) + break; + } +} +NOKPROBE_SYMBOL(unwind); + +#ifndef __KVM_NVHE_HYPERVISOR__ static int notrace unwind_next(struct task_struct *tsk, struct unwind_state *state) { @@ -171,22 +191,6 @@ static int notrace unwind_next(struct task_struct *tsk, } NOKPROBE_SYMBOL(unwind_next); -static void notrace unwind(struct task_struct *tsk, - struct unwind_state *state, - stack_trace_consume_fn consume_entry, void *cookie) -{ - while (1) { - int ret; - - if (!consume_entry(cookie, state->pc)) - break; - ret = unwind_next(tsk, state); - if (ret < 0) - break; - } -} -NOKPROBE_SYMBOL(unwind); - static bool dump_backtrace_entry(void *arg, unsigned long where) { char *loglvl = arg; @@ -238,3 +242,4 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, unwind(task, &state, consume_entry, cookie); } +#endif /* !__KVM_NVHE_HYPERVISOR__ */ diff --git a/arch/arm64/kvm/hyp/nvhe/Makefile b/arch/arm64/kvm/hyp/nvhe/Makefile index f9fe4dc21b1f..c0ff0d6fc403 100644 --- a/arch/arm64/kvm/hyp/nvhe/Makefile +++ b/arch/arm64/kvm/hyp/nvhe/Makefile @@ -14,7 +14,8 @@ lib-objs := $(addprefix ../../../lib/, $(lib-objs)) obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ hyp-main.o hyp-smp.o psci-relay.o early_alloc.o page_alloc.o \ - cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o + cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o \ + ../../../kernel/stacktrace.o obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o obj-$(CONFIG_DEBUG_LIST) += list_debug.o From patchwork Mon May 2 19:12:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12834640 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 79CC4C433F5 for ; Mon, 2 May 2022 19:14:58 +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=4kyUFSzDLCQ8pNthV2xh6lvsIbDncXvMfPmjPjiLSIg=; b=1IMAiw74i5qAW/ nqx8r0YktElv+lBO1hw1ZLSejKHWoUlb+4LWTUsY3JbCYs1CDPeZuM4nZIubsSHtMELIik/ltdQbw 2WwP1WOULbfc+IM9J/n79gn3kQ2n6JRDz2mTktApxBvUfn/IscE8hjVbsSpoFPuwWtiF1+6qvxdVH kWRMYYlL0nJ05tFQmosongOazEspCGOwSMsJEckF0Hfb1l2Ig3cKbKwlyf137b+E4QfZ5UdHxhKNZ E8Qgq7ZNlLfZM787KsZv6kTAeWDfIiwdOBaOWn5aCwOEcm+FzIChQUmG2BIU3tqbptcHTamBVWI/8 r/wKVlCXVsnDG1kWYhWQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nlbUW-002K3S-DH; Mon, 02 May 2022 19:13:52 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nlbUT-002K2N-Nm for linux-arm-kernel@lists.infradead.org; Mon, 02 May 2022 19:13:51 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2f8bbaa6b16so81713667b3.14 for ; Mon, 02 May 2022 12:13:48 -0700 (PDT) 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=NecAjaKo6a145Hd4bQjyaTWZfIjyHs70zkocfLAKQSs=; b=QVB6MW5tJEeXEtBnaqj+68WPwqmSC0IDheiS7ttyTWBXJheFxYJ7C/cvodMmjalP5S vgZ+25BOK1Vt3B+UC80FJgGZdJxmILirm2qIWWMzqQF9btkic9cKsZ4U90LlRJKgOIQb OrTHHTjrX2SGtENn6w3FoHexOsjv2+FfLK1KiMDV/DFHYvlaEcbXAMk9Uha4wZsMlT57 r3Qm38iqI4B/e8OLYePe0sEmEzTlEV4o1W4+PTuBcWIQ9QCFi9NQMtRyR9gXalxyZAsd L8fbI+dfQxvDWPmAQbezRSSCqwvyOACkyZSwVL6lKIEY19fxQPrqDUQjvTJeSh4cKII1 /WCg== 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=NecAjaKo6a145Hd4bQjyaTWZfIjyHs70zkocfLAKQSs=; b=iE23OuQZCDbtCXd2oA2kX2dCkb0RqNxs4VhyEAzS8MEPOxMLgztBpZw3q948B3HKva DFHenWlwhlvIy6/K7shPnnWIEeGfNzSLNTbu3yczty75jcc9Q/Jh7zeacogySmgUZFKC kTLSuD69Sxa/NlgTdhBG2MGNTg6NJeFSq56bdgrRHlnsB+qzQYHz/1P24fRFQ/pbf7wP n+8Aeq9FpmxH2nJKVZdE0qwzvtKUPD3sotFUBIBv+Fi1pCiUbw3GC62R77X5B2YKJSwe bcItweZXWrcxZKc9E/V8Qex+FwOcqQ10VxKNvmnjoZ67ew8IeteowPl6FsK2JUjzQJHt gHjQ== X-Gm-Message-State: AOAM530/mAEcj7mBy70IBRL/yMcytwv4B7OzabMLDRkTR9dp6pgCF52c 3V3XlK1bkf/QEDNHc9zBRWJ5ldte/q1IJgO/BQ== X-Google-Smtp-Source: ABdhPJx8M1O6z1nhK+MdMqqpss40uQyyOjlwfPNkVGWLfomVVPIxF8Rydp8wjn3kM3vkNvKVXWoWNEL5hggFgOkzAg== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:55c:7548:7073:c14f]) (user=kaleshsingh job=sendgmr) by 2002:a25:7243:0:b0:649:1be0:5061 with SMTP id n64-20020a257243000000b006491be05061mr11146766ybc.597.1651518828253; Mon, 02 May 2022 12:13:48 -0700 (PDT) Date: Mon, 2 May 2022 12:12:03 -0700 In-Reply-To: <20220502191222.4192768-1-kaleshsingh@google.com> Message-Id: <20220502191222.4192768-4-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220502191222.4192768-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.36.0.464.gb9c8b46e94-goog Subject: [PATCH v2 3/5] KVM: arm64: Add hypervisor overflow stack From: Kalesh Singh Cc: mark.rutland@arm.com, broonie@kernel.org, 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 , Masami Hiramatsu , Peter Collingbourne , Alexei Starovoitov , "Madhavan T. Venkataraman" , Andrew Jones , Marco Elver , Kefeng Wang , Keir Fraser , Zenghui Yu , 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-20220502_121349_838533_AEB80633 X-CRM114-Status: GOOD ( 13.84 ) 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 Allocate and switch to 16-byte aligned secondary stack on overflow. This provides us stack space to better handle overflows; and is used in a subsequent patch to dump the hypervisor stacktrace. Signed-off-by: Kalesh Singh --- arch/arm64/kernel/stacktrace.c | 3 +++ arch/arm64/kvm/hyp/nvhe/host.S | 9 ++------- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index a84e38d41d38..f346b4c66f1c 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -242,4 +242,7 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, unwind(task, &state, consume_entry, cookie); } +#else /* __KVM_NVHE_HYPERVISOR__ */ +DEFINE_PER_CPU(unsigned long [PAGE_SIZE/sizeof(long)], overflow_stack) + __aligned(16); #endif /* !__KVM_NVHE_HYPERVISOR__ */ diff --git a/arch/arm64/kvm/hyp/nvhe/host.S b/arch/arm64/kvm/hyp/nvhe/host.S index 09b5254fb497..1cd2de4f039e 100644 --- a/arch/arm64/kvm/hyp/nvhe/host.S +++ b/arch/arm64/kvm/hyp/nvhe/host.S @@ -179,13 +179,8 @@ SYM_FUNC_END(__host_hvc) b hyp_panic .L__hyp_sp_overflow\@: - /* - * Reset SP to the top of the stack, to allow handling the hyp_panic. - * This corrupts the stack but is ok, since we won't be attempting - * any unwinding here. - */ - ldr_this_cpu x0, kvm_init_params + NVHE_INIT_STACK_HYP_VA, x1 - mov sp, x0 + /* Switch to the overflow stack */ + adr_this_cpu sp, overflow_stack + PAGE_SIZE, x0 b hyp_panic_bad_stack ASM_BUG() From patchwork Mon May 2 19:12:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12834641 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 9528DC433F5 for ; Mon, 2 May 2022 19:15:15 +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=SDkVuo6YQmjpYNMz4IXt/oQopkrVpzNjNDCRX5YfsEk=; b=aNL9c0G8BN15Sy vd38zMPwOrQud6XVGwXhLMvik3usAd7BXJRYjCCnSGwlPwpyLYIQKhwSSUcsc7wUw4dtxMyIPgbua iPwOqffnYEf3qagW9+1eMsIWvzouV2felAUR445C7+yeiWCljbgpFWGN4Bf58u20hCaeNmR3qJ+j2 pGpdt5nx3VrUrhWFs0o402YrvF1QbY14YKoK8/moK6H5CGMmT5gVhknI+k2RgoG8+Hqmj3ogDQhl1 WFGkUB3sm43CZSqFHdR9kUA0NK4lhnz9kvGK9dankC0AoXn/140WPEJ8LGTaQ42iF+rNRnf0V/MVF gwC5zHrB4xIW9+ySokzg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nlbUt-002KCV-BR; Mon, 02 May 2022 19:14:15 +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 1nlbUq-002KB9-BP for linux-arm-kernel@lists.infradead.org; Mon, 02 May 2022 19:14:14 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id z14-20020a5b0b0e000000b0064848b628cfso13914839ybp.0 for ; Mon, 02 May 2022 12:14:10 -0700 (PDT) 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=kctQu0Q+rBx9IOI1jVYX1kCsToTQ9FXaErspFG8A+BM=; b=PUaFQEUXTVeO3j8UqhdilVFBoS8MQyXXLOdKDTrAxE3OYq/faQPkSJWo8e7me1GEfx 6PsNj1xxLGPhdzYlihe1rWIfDUcGXvJXU10tkvtxr6B/1FN7NalgVecXju0dhY3zvC3z Wn9cP13pNuedWp+KZQejkmkLKJWMyilvnVCZCeigqzu7y0HNaezqXPUdmoMOtc+bcbZg iGVOT37xPO8vnZ857v/bBUTDGk0QN0BZCLo8QSwtH1V7yUaedGw7RwWpenSonKMyFTwi MaX/+nPpNpve5ilD2cuRr96xbl7NBZ2cJyokJ9Cl3NLX6dlXkrvqfAJBFp8SZMNQPpEZ jtEw== 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=kctQu0Q+rBx9IOI1jVYX1kCsToTQ9FXaErspFG8A+BM=; b=A6fbfAaBqUEDETSQ5wAgTM1u3QlFwNAq7Se5ItUrqEcmKl7EH9kPBS1elcGVVHZG/2 boZUqKymK6OwcqH1reZCvAt4hA9Aw03HBuu2MJxDGOVKm4EcVSttf7bzTGAkjcvh3FhW z3ZvQWZYi07SjZm/Y40NQwIrmcVT2YnfbLJnxEZrWrIeCtjm2A+mfNQBiKd+OulbHNXq Ucjuo0Iku1sFu7u5nx0t8OzhkblXDZHJxkx6Q26xNnSHBvCxvY64REtwV/oUCI5L1in2 PDIkx4+IV97ZoW5rAXS2DGzjZxy/k2gnNIJwaaXW1txuYlHuV3moKBxDCep+J0F5Evew hc6g== X-Gm-Message-State: AOAM5302x1RQSa84vBUj1eDvxYZ3FHkauTOpc8DnDDWy+t69gpomQ+zT j/SrHAnvHpyyHSX98oFFfSUmgOx8V52y47nHlA== X-Google-Smtp-Source: ABdhPJxJbanbmKIqaVzeagTqMg3TVUzh6s0fqdiC3NI5Unq1sq+s6VeUa3qJVINa7kZWA+10cnCzYiMEVqhMJfYwMg== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:55c:7548:7073:c14f]) (user=kaleshsingh job=sendgmr) by 2002:a05:6902:50c:b0:648:67e5:f160 with SMTP id x12-20020a056902050c00b0064867e5f160mr11569415ybs.22.1651518850044; Mon, 02 May 2022 12:14:10 -0700 (PDT) Date: Mon, 2 May 2022 12:12:04 -0700 In-Reply-To: <20220502191222.4192768-1-kaleshsingh@google.com> Message-Id: <20220502191222.4192768-5-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220502191222.4192768-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.36.0.464.gb9c8b46e94-goog Subject: [PATCH v2 4/5] KVM: arm64: Allocate shared stacktrace pages From: Kalesh Singh Cc: mark.rutland@arm.com, broonie@kernel.org, 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 , Masami Hiramatsu , Peter Collingbourne , Alexei Starovoitov , "Madhavan T. Venkataraman" , Andrew Jones , Marco Elver , Zenghui Yu , Kefeng Wang , Keir Fraser , 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-20220502_121412_428831_9576C6BC X-CRM114-Status: GOOD ( 17.36 ) 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 The nVHE hypervisor can use this shared area to dump its stacktrace addresses on hyp_panic(). Symbolization and printing the stacktrace can then be handled by the host in EL1 (done in a later patch in this series). Signed-off-by: Kalesh Singh --- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/arm.c | 34 ++++++++++++++++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/setup.c | 11 +++++++++++ 3 files changed, 46 insertions(+) diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h index 2e277f2ed671..ad31ac68264f 100644 --- a/arch/arm64/include/asm/kvm_asm.h +++ b/arch/arm64/include/asm/kvm_asm.h @@ -174,6 +174,7 @@ struct kvm_nvhe_init_params { unsigned long hcr_el2; unsigned long vttbr; unsigned long vtcr; + unsigned long stacktrace_hyp_va; }; /* Translate a kernel address @ptr into its equivalent linear mapping */ diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index d300def44f5c..26005182da20 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -50,6 +50,7 @@ DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized); DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page); +DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stacktrace_page); unsigned long kvm_arm_hyp_percpu_base[NR_CPUS]; DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params); @@ -1484,6 +1485,7 @@ static void cpu_prepare_hyp_mode(int cpu) tcr |= (idmap_t0sz & GENMASK(TCR_TxSZ_WIDTH - 1, 0)) << TCR_T0SZ_OFFSET; params->tcr_el2 = tcr; + params->stacktrace_hyp_va = kern_hyp_va(per_cpu(kvm_arm_hyp_stacktrace_page, cpu)); params->pgd_pa = kvm_mmu_get_httbr(); if (is_protected_kvm_enabled()) params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS; @@ -1777,6 +1779,7 @@ static void teardown_hyp_mode(void) free_hyp_pgds(); for_each_possible_cpu(cpu) { free_page(per_cpu(kvm_arm_hyp_stack_page, cpu)); + free_page(per_cpu(kvm_arm_hyp_stacktrace_page, cpu)); free_pages(kvm_arm_hyp_percpu_base[cpu], nvhe_percpu_order()); } } @@ -1868,6 +1871,23 @@ static int init_hyp_mode(void) per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page; } + /* + * Allocate stacktrace pages for Hypervisor-mode. + * This is used by the hypervisor to share its stacktrace + * with the host on a hyp_panic(). + */ + for_each_possible_cpu(cpu) { + unsigned long stacktrace_page; + + stacktrace_page = __get_free_page(GFP_KERNEL); + if (!stacktrace_page) { + err = -ENOMEM; + goto out_err; + } + + per_cpu(kvm_arm_hyp_stacktrace_page, cpu) = stacktrace_page; + } + /* * Allocate and initialize pages for Hypervisor-mode percpu regions. */ @@ -1975,6 +1995,20 @@ static int init_hyp_mode(void) params->stack_hyp_va = hyp_addr + (2 * PAGE_SIZE); } + /* + * Map the hyp stacktrace pages. + */ + for_each_possible_cpu(cpu) { + char *stacktrace_page = (char *)per_cpu(kvm_arm_hyp_stacktrace_page, cpu); + + err = create_hyp_mappings(stacktrace_page, stacktrace_page + PAGE_SIZE, + PAGE_HYP); + if (err) { + kvm_err("Cannot map hyp stacktrace page\n"); + goto out_err; + } + } + for_each_possible_cpu(cpu) { char *percpu_begin = (char *)kvm_arm_hyp_percpu_base[cpu]; char *percpu_end = percpu_begin + nvhe_percpu_size(); diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c index e8d4ea2fcfa0..9b81bf2d40d7 100644 --- a/arch/arm64/kvm/hyp/nvhe/setup.c +++ b/arch/arm64/kvm/hyp/nvhe/setup.c @@ -135,6 +135,17 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size, /* Update stack_hyp_va to end of the stack's private VA range */ params->stack_hyp_va = hyp_addr + (2 * PAGE_SIZE); + + /* + * Map the stacktrace pages as shared and transfer ownership to + * the hypervisor. + */ + prot = pkvm_mkstate(PAGE_HYP, PKVM_PAGE_SHARED_OWNED); + start = (void *)params->stacktrace_hyp_va; + end = start + PAGE_SIZE; + ret = pkvm_create_mappings(start, end, prot); + if (ret) + return ret; } /* From patchwork Mon May 2 19:12:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12834642 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 13F99C433F5 for ; Mon, 2 May 2022 19:15:40 +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=ZCLMzO+PQL+XbJUX6q6XsGnKh0ruKyY2hD1nZUhCiH4=; b=yJ0HxKUrRm8xOt 42HrjaXJ8LFyPNBWYkmXJoO3aLZNdw4cRrJM6MHDTfwq3JokAyyySFPqboNUKJuj+zB9qPth5yf+A UFt1g5+zWflenFPvxWh4DQtVMmwGrASyyK/rHBoHaEgkgbRRcqaDMVO73+x4tI0ApwYzraJ24yQD6 H6NUYyaM41ney2Ji7ltOKY1Jv0Tl+Q0pEOGQeLnPOiIO0AiffbgOZ3gR2RDaUT6O8fi4ZGlafW3do ZkzYNxd7qezJ5vwExYXRxRWBL9F/7OMPNy2kqkylOsxyhy9Cl7TmIzzRjuWcZMkNYhaNsJ9VUcFH0 Dh1p9I5xJ2cXDJqHg2bA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nlbVK-002KR8-RU; Mon, 02 May 2022 19:14:43 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nlbVG-002KOp-AM for linux-arm-kernel@lists.infradead.org; Mon, 02 May 2022 19:14:40 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2f84aeb403fso135651817b3.22 for ; Mon, 02 May 2022 12:14:37 -0700 (PDT) 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=ipd0BefF9+t+n+BDA9qzK3ST29aGb9VMcXpg3CY250w=; b=fVIpIWNh7zPAAtchL34lpjR7VmL/rbh4QOlccErxfDjE0KYgyhgo2f9JKaAV/B3tWD MxCiCYhVyv8F63UsENToArfPZvAslNNo/Qu68rLtSkLkLg9ua9HMGyv2Nwe9/7AASh5i BobIpUh/UB0odGf+AoWdO8myMUZ0IUbReLTbxNOv5+D/GCt/2m92U7fGxLg18Ez67ZQb 4BfiJuZV3qHm+pzU441EVuhASuGcXposOq/D+cF3Ta0bcF5i+xaTZbMzyo2ZpZzCyc/i RaFq8+0+xm8tJblNw9lckgnQdTdMhtsMg1l2UKHbKKSdrNqXCz/AvtrbKkQhuMv1NF1i USPA== 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=ipd0BefF9+t+n+BDA9qzK3ST29aGb9VMcXpg3CY250w=; b=FbHbJU0HS9z7L+B1rIpfNH5pb+vZabakE8HLTtSWdlzPNnodMnLI0ORIjkckqGhpIN 0/btfCKSvDyEb4uoAXGRY5mtHQaR7S6tZ4KuHJW7A7K7oqksdh10S57ibko0vgENk8Vx ShH5ScdBBO/cXlwarPV7ZAKl+ENZGvHM7+p6XNgN4gOsFuT9/LvHIKG8X9rG5MRccIoi BXdZH9H7ypEXTaEeP/iR6RKFSgVVqZZLGXbJiTSiwAIx1FZOgqjVB7yVCrUepgGguIku PtGzwAYRovSGRl9OQvrTMQceCyzxh+NHqMCl1TOYMfnXdUyrVpMxubaofwAZpuDZ7m/l ZTqA== X-Gm-Message-State: AOAM53247JIZ9/S4S6qUoEmwTyDTyaSTLhyzsxaEHWk/mxR/f+vYrYqm nkfY4uCAskStcArpbzl6wQp5KtoMGzrv0MH1EQ== X-Google-Smtp-Source: ABdhPJzHCwIdJBjgxBAhHOPW83nydzx6a6Ghl99nsHDS8GeBKfnj+2gJ4yayUqDCoRfzGDmOaLGKU5KAvcjqU3rRPw== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:55c:7548:7073:c14f]) (user=kaleshsingh job=sendgmr) by 2002:a81:2492:0:b0:2eb:250d:9cd8 with SMTP id k140-20020a812492000000b002eb250d9cd8mr12136698ywk.238.1651518876776; Mon, 02 May 2022 12:14:36 -0700 (PDT) Date: Mon, 2 May 2022 12:12:05 -0700 In-Reply-To: <20220502191222.4192768-1-kaleshsingh@google.com> Message-Id: <20220502191222.4192768-6-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220502191222.4192768-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.36.0.464.gb9c8b46e94-goog Subject: [PATCH v2 5/5] KVM: arm64: Unwind and dump nVHE hypervisor stacktrace From: Kalesh Singh Cc: mark.rutland@arm.com, broonie@kernel.org, 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 , Masami Hiramatsu , Alexei Starovoitov , Peter Collingbourne , "Madhavan T. Venkataraman" , Andrew Jones , Kefeng Wang , Keir Fraser , Zenghui Yu , 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-20220502_121438_435549_854DD9BA X-CRM114-Status: GOOD ( 21.93 ) 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 On hyp_panic(), the hypervisor dumps the addresses for its stacktrace entries to a page shared with the host. The host then symbolizes and prints the hyp stacktrace before panicking itself. Example stacktrace: [ 122.051187] kvm [380]: Invalid host exception to nVHE hyp! [ 122.052467] kvm [380]: nVHE HYP call trace: [ 122.052814] kvm [380]: [] __kvm_nvhe___pkvm_vcpu_init_traps+0x1f0/0x1f0 [ 122.053865] kvm [380]: [] __kvm_nvhe_hyp_panic+0x130/0x1c0 [ 122.054367] kvm [380]: [] __kvm_nvhe___kvm_vcpu_run+0x10/0x10 [ 122.054878] kvm [380]: [] __kvm_nvhe_handle___kvm_vcpu_run+0x30/0x50 [ 122.055412] kvm [380]: [] __kvm_nvhe_handle_trap+0xbc/0x160 [ 122.055911] kvm [380]: [] __kvm_nvhe___host_exit+0x64/0x64 [ 122.056417] kvm [380]: ---- end of nVHE HYP call trace ---- Signed-off-by: Kalesh Singh Reviewed-by: Mark Brown --- Changes in v2: - Add Mark's Reviewed-by tag arch/arm64/include/asm/stacktrace.h | 42 ++++++++++++++-- arch/arm64/kernel/stacktrace.c | 75 +++++++++++++++++++++++++++++ arch/arm64/kvm/handle_exit.c | 4 ++ arch/arm64/kvm/hyp/nvhe/switch.c | 4 ++ 4 files changed, 121 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace.h b/arch/arm64/include/asm/stacktrace.h index f5af9a94c5a6..3063912107b0 100644 --- a/arch/arm64/include/asm/stacktrace.h +++ b/arch/arm64/include/asm/stacktrace.h @@ -5,6 +5,7 @@ #ifndef __ASM_STACKTRACE_H #define __ASM_STACKTRACE_H +#include #include #include #include @@ -19,10 +20,12 @@ enum stack_type { #ifndef __KVM_NVHE_HYPERVISOR__ STACK_TYPE_TASK, STACK_TYPE_IRQ, - STACK_TYPE_OVERFLOW, STACK_TYPE_SDEI_NORMAL, STACK_TYPE_SDEI_CRITICAL, +#else /* __KVM_NVHE_HYPERVISOR__ */ + STACK_TYPE_HYP, #endif /* !__KVM_NVHE_HYPERVISOR__ */ + STACK_TYPE_OVERFLOW, STACK_TYPE_UNKNOWN, __NR_STACK_TYPES }; @@ -55,6 +58,9 @@ static inline bool on_stack(unsigned long sp, unsigned long size, extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, const char *loglvl); +extern void hyp_dump_backtrace(unsigned long hyp_offset); + +DECLARE_PER_CPU(unsigned long, kvm_arm_hyp_stacktrace_page); DECLARE_PER_CPU(unsigned long *, irq_stack_ptr); static inline bool on_irq_stack(unsigned long sp, unsigned long size, @@ -91,8 +97,32 @@ static inline bool on_overflow_stack(unsigned long sp, unsigned long size, static inline bool on_overflow_stack(unsigned long sp, unsigned long size, struct stack_info *info) { return false; } #endif -#endif /* !__KVM_NVHE_HYPERVISOR__ */ +#else /* __KVM_NVHE_HYPERVISOR__ */ + +extern void hyp_save_backtrace(void); + +DECLARE_PER_CPU(unsigned long [PAGE_SIZE/sizeof(long)], overflow_stack); +DECLARE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params); + +static inline bool on_overflow_stack(unsigned long sp, unsigned long size, + struct stack_info *info) +{ + unsigned long low = (unsigned long)this_cpu_ptr(overflow_stack); + unsigned long high = low + PAGE_SIZE; + + return on_stack(sp, size, low, high, STACK_TYPE_OVERFLOW, info); +} + +static inline bool on_hyp_stack(unsigned long sp, unsigned long size, + struct stack_info *info) +{ + struct kvm_nvhe_init_params *params = this_cpu_ptr(&kvm_init_params); + unsigned long high = params->stack_hyp_va; + unsigned long low = high - PAGE_SIZE; + return on_stack(sp, size, low, high, STACK_TYPE_HYP, info); +} +#endif /* !__KVM_NVHE_HYPERVISOR__ */ /* * We can only safely access per-cpu stacks from current in a non-preemptible @@ -105,6 +135,9 @@ static inline bool on_accessible_stack(const struct task_struct *tsk, if (info) info->type = STACK_TYPE_UNKNOWN; + if (on_overflow_stack(sp, size, info)) + return true; + #ifndef __KVM_NVHE_HYPERVISOR__ if (on_task_stack(tsk, sp, size, info)) return true; @@ -112,10 +145,11 @@ static inline bool on_accessible_stack(const struct task_struct *tsk, return false; if (on_irq_stack(sp, size, info)) return true; - if (on_overflow_stack(sp, size, info)) - return true; if (on_sdei_stack(sp, size, info)) return true; +#else /* __KVM_NVHE_HYPERVISOR__ */ + if (on_hyp_stack(sp, size, info)) + return true; #endif /* !__KVM_NVHE_HYPERVISOR__ */ return false; diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index f346b4c66f1c..c81dea9760ac 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -104,6 +104,7 @@ static int notrace __unwind_next(struct task_struct *tsk, * * TASK -> IRQ -> OVERFLOW -> SDEI_NORMAL * TASK -> SDEI_NORMAL -> SDEI_CRITICAL -> OVERFLOW + * HYP -> OVERFLOW * * ... but the nesting itself is strict. Once we transition from one * stack to another, it's never valid to unwind back to that first @@ -242,7 +243,81 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, unwind(task, &state, consume_entry, cookie); } + +/** + * Symbolizes and dumps the hypervisor backtrace from the shared + * stacktrace page. + */ +noinline notrace void hyp_dump_backtrace(unsigned long hyp_offset) +{ + unsigned long *stacktrace_pos = + (unsigned long *)*this_cpu_ptr(&kvm_arm_hyp_stacktrace_page); + unsigned long va_mask = GENMASK_ULL(vabits_actual - 1, 0); + unsigned long pc = *stacktrace_pos++; + + kvm_err("nVHE HYP call trace:\n"); + + while (pc) { + pc &= va_mask; /* Mask tags */ + pc += hyp_offset; /* Convert to kern addr */ + kvm_err("[<%016lx>] %pB\n", pc, (void *)pc); + pc = *stacktrace_pos++; + } + + kvm_err("---- end of nVHE HYP call trace ----\n"); +} #else /* __KVM_NVHE_HYPERVISOR__ */ DEFINE_PER_CPU(unsigned long [PAGE_SIZE/sizeof(long)], overflow_stack) __aligned(16); + +static int notrace unwind_next(struct task_struct *tsk, + struct unwind_state *state) +{ + struct stack_info info; + + return __unwind_next(tsk, state, &info); +} + +/** + * Saves a hypervisor stacktrace entry (address) to the shared stacktrace page. + */ +static bool hyp_save_backtrace_entry(void *arg, unsigned long where) +{ + struct kvm_nvhe_init_params *params = this_cpu_ptr(&kvm_init_params); + unsigned long **stacktrace_pos = (unsigned long **)arg; + unsigned long stacktrace_start, stacktrace_end; + + stacktrace_start = (unsigned long)params->stacktrace_hyp_va; + stacktrace_end = stacktrace_start + PAGE_SIZE - (2 * sizeof(long)); + + if ((unsigned long) *stacktrace_pos > stacktrace_end) + return false; + + /* Save the entry to the current pos in stacktrace page */ + **stacktrace_pos = where; + + /* A zero entry delimits the end of the stacktrace. */ + *(*stacktrace_pos + 1) = 0UL; + + /* Increment the current pos */ + ++*stacktrace_pos; + + return true; +} + +/** + * Saves hypervisor stacktrace to the shared stacktrace page. + */ +noinline notrace void hyp_save_backtrace(void) +{ + struct kvm_nvhe_init_params *params = this_cpu_ptr(&kvm_init_params); + void *stacktrace_start = (void *)params->stacktrace_hyp_va; + struct unwind_state state; + + unwind_init(&state, (unsigned long)__builtin_frame_address(0), + _THIS_IP_); + + unwind(NULL, &state, hyp_save_backtrace_entry, &stacktrace_start); +} + #endif /* !__KVM_NVHE_HYPERVISOR__ */ diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index 2e61a987b0d5..f1a6b556ec32 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -325,6 +326,9 @@ void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr, (void *)panic_addr); } + /* Dump the hypervisor stacktrace */ + hyp_dump_backtrace(hyp_offset); + /* * Hyp has panicked and we're going to handle that by panicking the * kernel. The kernel offset will be revealed in the panic so we're diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c index 978f1b94fb25..95d810e86c7d 100644 --- a/arch/arm64/kvm/hyp/nvhe/switch.c +++ b/arch/arm64/kvm/hyp/nvhe/switch.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -395,6 +396,9 @@ asmlinkage void __noreturn hyp_panic(void) __sysreg_restore_state_nvhe(host_ctxt); } + /* Save the hypervisor stacktrace */ + hyp_save_backtrace(); + __hyp_do_panic(host_ctxt, spsr, elr, par); unreachable(); }