From patchwork Tue Jul 26 07:37:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928975 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 2B016C433EF for ; Tue, 26 Jul 2022 07:39:19 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=NFx0kSHInnsvzY8Tc0rkOYDG9lKceYzRAWC4hfAy17k=; b=kZXe1sY6fV6o9qhE4Q+jcQ7aoo cRR6rZ+nII/2CV/nDcOdFmdbqe2Ww4sAtBzgfBMAQFoXrMlGXbVbkDKRe0bAl7uO4vj4yIG5821Za PYkALm2RMWFJI51d2/igbxg0obT0vrF7LrNldtRJ7vqIDaS3Y1a9lX+i67QprNsUFrzt9QM4p5Lky 7SMaSy4660CjObYDLzWVj6JqMIgl8PCd/YYFlry3NNgf5qP4sTR9Ou5ArrY4lX2x5ECyU+PRoYdzd SniH0EM3Sv3qWAOAkvrImPHtArxkq48xFVYhzUN5XpEor/QbKGEF1K0rUwNYg4YofPlhiO9tAFnbK P+BGpgZg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGF8v-009VkM-6n; Tue, 26 Jul 2022 07:38:13 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGF8g-009VbY-Vt for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:00 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-31cdce3ed04so105963627b3.13 for ; Tue, 26 Jul 2022 00:37:58 -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:to :cc; bh=hkI8SHFxOWpuHqFEDFQkJgQIHtTw/fiCoKas4HBdchQ=; b=M+Hejj5F7RFOdoRRpj6jtjp+qBRc41n5pQh31rmQMfVXSELVe/V/WfozQRtnDJnawL xmxQvp8H/TNEYTS6v8gm+aWOLZCB8A+bdIqE4bdksQoex0Y1Sim3av4m6IBwa4j1eNu+ lLJHoMOsHZt+AwUxCJ8eKmq6jMXwdASNVsi6fxwWNIBBNJZ5pPsxrAci2Xi+DjpGgbyN EIZfXSc20kRide0V1fjhZTnheB1Mw3DPofZqBbcMR6eMeOfO9pEubkxsLjHxzQGwjxa/ DJMu/iSA4pZNsUwhOQBlOg611kRKrsSkfM0Qs6syuSluVsK+Px75QuH0UVwxWTnI1zkA C5BA== 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:to:cc; bh=hkI8SHFxOWpuHqFEDFQkJgQIHtTw/fiCoKas4HBdchQ=; b=ERT1cGyRdN4ToQ+wTUBuCc+WG8X5UsyBBXsZsspUEoAD3cyMGkQ/O8WB9QmHMGlfSd wCJeAGQ5JOyGK0+zAJUzq/jDdRi+MeiI3PP86982LpmWg73buBiBJGfTmBbn0P8r8KKJ gqjhR9XJacVKVVhemhzGTIlV0Z4yXwiNWJa75AEDiCF2y1bU22AZMDY6MkWPdZJ0Om37 3Ws9W0o4IwZfhirs9g02jZLUjQrXA9XUW+ErDpuDYDbgO7+YG7hlqKLbpoa2XLpRrgKj f1EXGx8f0UNHyEoH6KnY0HSTgxQfarCcY5GkQZ7C6Jz3LTOsyBii+Mr6HaCuiNYRwX8o PTvQ== X-Gm-Message-State: AJIora/7ReYW6b9RbC0yo9BOm7+Y3FyiKUXtvGbLKRTWPdLs219nD39C UWLAocdmNX6GgksejufDAWREDps5BfWQ6IGSiw== X-Google-Smtp-Source: AGRyM1uWETDHd3ZdTJkDIeLlRJqRRBUNqKJBfrB+brbF37PlJGFXG/GVaoixFtkKUTmpD9G+OeM4yiAKZY3RNJrpRQ== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a25:a423:0:b0:671:6dc0:bbc9 with SMTP id f32-20020a25a423000000b006716dc0bbc9mr2071615ybi.223.1658821077611; Tue, 26 Jul 2022 00:37:57 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:34 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-2-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 01/17] arm64: stacktrace: Add shared header for common stack unwinding code From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003759_049862_F5D4F1A2 X-CRM114-Status: GOOD ( 25.61 ) 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 In order to reuse the arm64 stack unwinding logic for the nVHE hypervisor stack, move the common code to a shared header (arch/arm64/include/asm/stacktrace/common.h). The nVHE hypervisor cannot safely link against kernel code, so we make use of the shared header to avoid duplicated logic later in this series. Signed-off-by: Kalesh Singh Reviewed-by: Mark Brown Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- Changes in v6: - Add Fuad's Tested-by tag Changes in v5: - Add Reviewed-by tags from Mark Brown and Fuad arch/arm64/include/asm/stacktrace.h | 35 +------ arch/arm64/include/asm/stacktrace/common.h | 105 +++++++++++++++++++++ arch/arm64/kernel/stacktrace.c | 57 ----------- 3 files changed, 106 insertions(+), 91 deletions(-) create mode 100644 arch/arm64/include/asm/stacktrace/common.h diff --git a/arch/arm64/include/asm/stacktrace.h b/arch/arm64/include/asm/stacktrace.h index aec9315bf156..79f455b37c84 100644 --- a/arch/arm64/include/asm/stacktrace.h +++ b/arch/arm64/include/asm/stacktrace.h @@ -8,52 +8,19 @@ #include #include #include -#include #include #include #include #include -enum stack_type { - STACK_TYPE_UNKNOWN, - STACK_TYPE_TASK, - STACK_TYPE_IRQ, - STACK_TYPE_OVERFLOW, - STACK_TYPE_SDEI_NORMAL, - STACK_TYPE_SDEI_CRITICAL, - __NR_STACK_TYPES -}; - -struct stack_info { - unsigned long low; - unsigned long high; - enum stack_type type; -}; +#include 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) -{ - if (!low) - return false; - - if (sp < low || sp + size < sp || sp + size > high) - return false; - - if (info) { - info->low = low; - info->high = high; - info->type = type; - } - return true; -} - static inline bool on_irq_stack(unsigned long sp, unsigned long size, struct stack_info *info) { diff --git a/arch/arm64/include/asm/stacktrace/common.h b/arch/arm64/include/asm/stacktrace/common.h new file mode 100644 index 000000000000..64ae4f6b06fe --- /dev/null +++ b/arch/arm64/include/asm/stacktrace/common.h @@ -0,0 +1,105 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Common arm64 stack unwinder code. + * + * Copyright (C) 2012 ARM Ltd. + */ +#ifndef __ASM_STACKTRACE_COMMON_H +#define __ASM_STACKTRACE_COMMON_H + +#include +#include +#include + +enum stack_type { + STACK_TYPE_UNKNOWN, + STACK_TYPE_TASK, + STACK_TYPE_IRQ, + STACK_TYPE_OVERFLOW, + STACK_TYPE_SDEI_NORMAL, + STACK_TYPE_SDEI_CRITICAL, + __NR_STACK_TYPES +}; + +struct stack_info { + unsigned long low; + unsigned long high; + enum stack_type type; +}; + +/* + * A snapshot of a frame record or fp/lr register values, along with some + * accounting information necessary for robust unwinding. + * + * @fp: The fp value in the frame record (or the real fp) + * @pc: The lr value in the frame record (or the real lr) + * + * @stacks_done: Stacks which have been entirely unwound, for which it is no + * longer valid to unwind to. + * + * @prev_fp: The fp that pointed to this frame record, or a synthetic value + * of 0. This is used to ensure that within a stack, each + * subsequent frame record is at an increasing address. + * @prev_type: The type of stack this frame record was on, or a synthetic + * value of STACK_TYPE_UNKNOWN. This is used to detect a + * transition from one stack to another. + * + * @kr_cur: When KRETPROBES is selected, holds the kretprobe instance + * associated with the most recently encountered replacement lr + * value. + * + * @task: The task being unwound. + */ +struct unwind_state { + unsigned long fp; + unsigned long pc; + DECLARE_BITMAP(stacks_done, __NR_STACK_TYPES); + unsigned long prev_fp; + enum stack_type prev_type; +#ifdef CONFIG_KRETPROBES + struct llist_node *kr_cur; +#endif + struct task_struct *task; +}; + +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) +{ + if (!low) + return false; + + if (sp < low || sp + size < sp || sp + size > high) + return false; + + if (info) { + info->low = low; + info->high = high; + info->type = type; + } + return true; +} + +static inline void unwind_init_common(struct unwind_state *state, + struct task_struct *task) +{ + state->task = task; +#ifdef CONFIG_KRETPROBES + state->kr_cur = NULL; +#endif + + /* + * Prime the first unwind. + * + * In unwind_next() we'll check that the FP points to a valid stack, + * which can't be STACK_TYPE_UNKNOWN, and the first unwind will be + * treated as a transition to whichever stack that happens to be. The + * prev_fp value won't be used, but we set it to 0 such that it is + * definitely not an accessible stack address. + */ + bitmap_zero(state->stacks_done, __NR_STACK_TYPES); + state->prev_fp = 0; + state->prev_type = STACK_TYPE_UNKNOWN; +} + +#endif /* __ASM_STACKTRACE_COMMON_H */ diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index fcaa151b81f1..94a5dd2ab8fd 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -18,63 +18,6 @@ #include #include -/* - * A snapshot of a frame record or fp/lr register values, along with some - * accounting information necessary for robust unwinding. - * - * @fp: The fp value in the frame record (or the real fp) - * @pc: The lr value in the frame record (or the real lr) - * - * @stacks_done: Stacks which have been entirely unwound, for which it is no - * longer valid to unwind to. - * - * @prev_fp: The fp that pointed to this frame record, or a synthetic value - * of 0. This is used to ensure that within a stack, each - * subsequent frame record is at an increasing address. - * @prev_type: The type of stack this frame record was on, or a synthetic - * value of STACK_TYPE_UNKNOWN. This is used to detect a - * transition from one stack to another. - * - * @kr_cur: When KRETPROBES is selected, holds the kretprobe instance - * associated with the most recently encountered replacement lr - * value. - * - * @task: The task being unwound. - */ -struct unwind_state { - unsigned long fp; - unsigned long pc; - DECLARE_BITMAP(stacks_done, __NR_STACK_TYPES); - unsigned long prev_fp; - enum stack_type prev_type; -#ifdef CONFIG_KRETPROBES - struct llist_node *kr_cur; -#endif - struct task_struct *task; -}; - -static void unwind_init_common(struct unwind_state *state, - struct task_struct *task) -{ - state->task = task; -#ifdef CONFIG_KRETPROBES - state->kr_cur = NULL; -#endif - - /* - * Prime the first unwind. - * - * In unwind_next() we'll check that the FP points to a valid stack, - * which can't be STACK_TYPE_UNKNOWN, and the first unwind will be - * treated as a transition to whichever stack that happens to be. The - * prev_fp value won't be used, but we set it to 0 such that it is - * definitely not an accessible stack address. - */ - bitmap_zero(state->stacks_done, __NR_STACK_TYPES); - state->prev_fp = 0; - state->prev_type = STACK_TYPE_UNKNOWN; -} - /* * Start an unwind from a pt_regs. * From patchwork Tue Jul 26 07:37:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928977 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 E812DCCA489 for ; Tue, 26 Jul 2022 07:39:20 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=j0lO4YhpHehymt6pZtEne90WHSrJ7rPiYp3YwaXSO8E=; b=W/tEdxZPDHYgoDn6Zx0rErKFxO /W3+pLu20YOWCFTdqGP2rclcdGQAE4EBirfJwwQmue40R1GO6WY4I8xRszfioiOJGd14Ntq3f1G72 H+BHg63ulgQhzWnDY85h4SRsgXPnBxwfZHNalXIO5w5zKeAq8X3vxKcyqn20op3dD7BIzGhdM792y cN0audi8D4gl+mkO0Vuqq3dY8ALBi1uxpk9J6O9a2P7DfkoauD4TkIM7+LrTuHiPOuFVWp/jeD8EB RMvOKz5sos+sGe8L/nri/1gjaINknoEZY3myNIXuP1bLGKNry2lkLfNFBV0FaGx+9LCY7LgkfdZch Bc9CPxpA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGF94-009VpD-SA; Tue, 26 Jul 2022 07:38:23 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGF8j-009Vd3-7z for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:02 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-31e619dcbbaso104751897b3.14 for ; Tue, 26 Jul 2022 00:38:00 -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:to :cc; bh=HJlaI56zFqTbz4E9QM1JRki26p1CmhrJ0ldLZAGD5Gc=; b=Hg/ba+twU69xuQeTauHfGh1xd/IG5mrk63mhJ8YbvAnkIYauk5mYLezBlA1CBAAwo5 iudwXyj1NFecmdhOgDc713Mpd26tUbphRGBTx9y6x3SGfN+p2OKUD22cbjv1uK0Zs2iK 7n537VLpecnFLR32XUyFwgB3FNRhMtF/cc8+8rqGN3vGNoImYNblyhe1sxYMZIG5lzi1 sx9avbYpCLnTFekr6sLBtneCRMNiIRO9FkeJ79AvkazD6dHzX0TGFnDTV7OdwpiJ+6TE YS8w/xFCmTCZs1aYynybx790r1vSYJn2pkRV1zejr+IyokyYFn7uf1IPyeW8bkGwewTz uyvQ== 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:to:cc; bh=HJlaI56zFqTbz4E9QM1JRki26p1CmhrJ0ldLZAGD5Gc=; b=In/pVLiomZh3A/lG0CDfvIPZ+jVJoWOAbwVOGd6uGFSKXtu9CD7GJId/l6cb+gO2ey 6OltkJeS9rwwFaBY/oKoFzrWodhgodAFjPMc42ZMpbBi8f7pvkoOD+YfqpceNXhD8oP0 46HgBgzS8+5AcmbZnkAHLtU0rcotMGSvsyJ8RgQaZ6q3MyAf+2b98Tv9dkIIJZmWD6n0 EjCQO//31/MSQ4XmP/HWky+oAdrTXjWEAY9+oxOK/8vbsyLEDaargTKsMPTACmo1TYyB yyaVQqspblJjJ9ICVb//5HtfCPXD2U0gpd4Y8Nq5EivKZSoLtzI/116TEm+6BtESRskU tokA== X-Gm-Message-State: AJIora/g648LW6F+jJlV4vK5n826Mk4n8EkiH7VIKilwhVSad0fozn9e p0UVNdGmqsK6JXvyXWWSwjpeNaQAxw3uzKYS2g== X-Google-Smtp-Source: AGRyM1sij8eAETksDM9PJQAcvuFaOFZtWgM4fTTWZ/PJWmBJUpgq02OLuiPRM+zMspwuXb4dIPzCV3Wazxh9bcMfGw== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a25:80d3:0:b0:66f:5da5:204f with SMTP id c19-20020a2580d3000000b0066f5da5204fmr12492060ybm.30.1658821079950; Tue, 26 Jul 2022 00:37:59 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:35 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-3-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 02/17] arm64: stacktrace: Factor out on_accessible_stack_common() From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003801_326224_3364A181 X-CRM114-Status: GOOD ( 13.83 ) 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 Move common on_accessible_stack checks to stacktrace/common.h. This is used in the implementation of the nVHE hypervisor unwinder later in this series. Signed-off-by: Kalesh Singh Reviewed-by: Fuad Tabba Reviewed-by: Mark Brown Tested-by: Fuad Tabba --- Changes in v6: - Add Fuad's Tested-by tag Changes in v5: - Add Reviewed-by tags from Mark Brown and Fuad - Remove random whitespace change, per Mark Brown arch/arm64/include/asm/stacktrace.h | 6 ++---- arch/arm64/include/asm/stacktrace/common.h | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace.h b/arch/arm64/include/asm/stacktrace.h index 79f455b37c84..43f4b4a6d383 100644 --- a/arch/arm64/include/asm/stacktrace.h +++ b/arch/arm64/include/asm/stacktrace.h @@ -65,8 +65,8 @@ static inline bool on_accessible_stack(const struct task_struct *tsk, unsigned long sp, unsigned long size, struct stack_info *info) { - if (info) - info->type = STACK_TYPE_UNKNOWN; + if (on_accessible_stack_common(tsk, sp, size, info)) + return true; if (on_task_stack(tsk, sp, size, info)) return true; @@ -74,8 +74,6 @@ 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; diff --git a/arch/arm64/include/asm/stacktrace/common.h b/arch/arm64/include/asm/stacktrace/common.h index 64ae4f6b06fe..f58b786460d3 100644 --- a/arch/arm64/include/asm/stacktrace/common.h +++ b/arch/arm64/include/asm/stacktrace/common.h @@ -62,6 +62,9 @@ struct unwind_state { struct task_struct *task; }; +static inline bool on_overflow_stack(unsigned long sp, unsigned long size, + struct stack_info *info); + 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) @@ -80,6 +83,21 @@ static inline bool on_stack(unsigned long sp, unsigned long size, return true; } +static inline bool on_accessible_stack_common(const struct task_struct *tsk, + unsigned long sp, + unsigned long size, + struct stack_info *info) +{ + if (info) + info->type = STACK_TYPE_UNKNOWN; + + /* + * Both the kernel and nvhe hypervisor make use of + * an overflow_stack + */ + return on_overflow_stack(sp, size, info); +} + static inline void unwind_init_common(struct unwind_state *state, struct task_struct *task) { From patchwork Tue Jul 26 07:37:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928978 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 56D19C43334 for ; Tue, 26 Jul 2022 07:39:43 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=BzNE/LBVCpnKT5T4t+vtM1VW2hOQ80XZ4SS0shUuIVA=; b=VdOvLmd8M4IqzZZt/8ouqDq7Cy 5Fe42cr6cm/AhME7M2hZ5Q4kI4eD+0cSR1yYy1ja7ZQjgIhGOXqcDKrrvyJixEnFJcYDDqXl0UGCy 2jNgX2IqPgwRsj5DFsqhE5MWE3QtQrAJanpfuk6R1f1mouT8EiDHvDAk/VWtvQXmf05zW2KAiMXaq fNoEJvSsm2WmKiSsGTa9O6gvXwBdZ5O3GGFoUUyD6JCZZEbgRRrAwLOyB3vr+/fmbK72DR3pbd5Ae sRHA0PG9UuRWUKTHbQTdGS77nLW6dM4Z8Q/y7stqAXlPVnTxcU+v1pASxCWyAZH+Ry0VKjRWkwp5b 1QMum1Ig==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGF9G-009Vwl-MA; Tue, 26 Jul 2022 07:38:34 +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 1oGF8o-009Vfo-8A for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:07 +0000 Received: by mail-yb1-xb49.google.com with SMTP id m11-20020a5b040b000000b0066fcc60d1a0so10506853ybp.19 for ; Tue, 26 Jul 2022 00:38:03 -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:to :cc; bh=5LzygoxYBmX52V1I8A5Hvt/OcxOdlEMYc0Y930yUiBQ=; b=lxba6zPS8bNj3T4SQ4NgtR3spXvbVhLqKPcx1Hc782Lm9/xnxi+CiSSlzu4yrB+6Ob +nmlTtMJekStORm3jx3MPu7T8RpB2I3EFhmAMg67K2+TOyWtvgD8ceXd2DZEz98cp+tB AHbCkifdg/jPtMKpNDl63zHG7jkWkO4I6Mrp++lQyYGmqJhbb8+2J+8O3jJTXddAXJze hzHbAAZxokWC+22wtYsQGG0iXfr3iYegpLoWC3Za44kxt61yZtjlHL5W74mLLRp4HBVo 2PURIJR3vLCK3lvSW66LOdSgAoZIvwAkopq9F1Qr9I8F1VaYKVLQYLvCnqkmvISyVhlB 8OpA== 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:to:cc; bh=5LzygoxYBmX52V1I8A5Hvt/OcxOdlEMYc0Y930yUiBQ=; b=4aXPSKAq7lj8/5c60jRMfuAT8R5ow3NmtHfmPOrS8IC3JnXClWn1JqH1a6gJcB5UdN eKP7EHx5BGoCVjpaIP5kUefrnWlwemdRCZAPRZaCFwXeSAackV4o5qWH8Eqwdpqk2BPk Umm8jOZJJtLQZbUtstNIYzEnVAeb85owCPIQsRlHeQOD2O7Dqz/E7v2/+5qyEVRnV2gn hkROt1E3lWIbwNVwB4o8C2p/iKWcj5oVjNzW9AknBysoMFJdxtF4da3+0qChu9IxEUvT aVSnO2HQ1YnMycgi7/pXg54Cqd5U5/zgrilYGzgNo8QPczm5K/giqRieZhpyik7uDyad sC1Q== X-Gm-Message-State: AJIora9RHdbzePc88Rza4CWw/CVscaMB2tRcgP8cNQieAomvwPgy5+xp RQuLKpG6/3q+juTR7UcEO00xGRb6J44R1bLVcw== X-Google-Smtp-Source: AGRyM1vLCGxSpACfvit1h9qHAotszBcN5RiBC0/VBQZAO/C3DHxhNjsQIs2RUsZ7LNGl/jujBDMD9xrWPH2wZ/0Q7A== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a25:4c9:0:b0:671:6f60:eed8 with SMTP id 192-20020a2504c9000000b006716f60eed8mr1683033ybe.243.1658821082719; Tue, 26 Jul 2022 00:38:02 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:36 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-4-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 03/17] arm64: stacktrace: Factor out unwind_next_common() From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003806_332779_77148F4B X-CRM114-Status: GOOD ( 19.06 ) 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 Move common unwind_next logic to stacktrace/common.h. This allows reusing the code in the implementation the nVHE hypervisor stack unwinder, later in this series. Signed-off-by: Kalesh Singh Reviewed-by: Fuad Tabba Reviewed-by: Mark Brown Tested-by: Fuad Tabba --- Changes in v6: - Add Fuad's Tested-by tag Changes in v5: - Add Reviewed-by tags from Mark Brown and Fuad arch/arm64/include/asm/stacktrace/common.h | 50 ++++++++++++++++++++++ arch/arm64/kernel/stacktrace.c | 41 ++---------------- 2 files changed, 54 insertions(+), 37 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace/common.h b/arch/arm64/include/asm/stacktrace/common.h index f58b786460d3..0c5cbfdb56b5 100644 --- a/arch/arm64/include/asm/stacktrace/common.h +++ b/arch/arm64/include/asm/stacktrace/common.h @@ -65,6 +65,10 @@ struct unwind_state { static inline bool on_overflow_stack(unsigned long sp, unsigned long size, struct stack_info *info); +static inline bool on_accessible_stack(const struct task_struct *tsk, + unsigned long sp, unsigned long size, + struct stack_info *info); + 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) @@ -120,4 +124,50 @@ static inline void unwind_init_common(struct unwind_state *state, state->prev_type = STACK_TYPE_UNKNOWN; } +static inline int unwind_next_common(struct unwind_state *state, + struct stack_info *info) +{ + struct task_struct *tsk = state->task; + unsigned long fp = state->fp; + + if (fp & 0x7) + return -EINVAL; + + if (!on_accessible_stack(tsk, fp, 16, info)) + return -EINVAL; + + if (test_bit(info->type, state->stacks_done)) + return -EINVAL; + + /* + * As stacks grow downward, any valid record on the same stack must be + * at a strictly higher address than the prior record. + * + * Stacks can nest in several valid orders, e.g. + * + * TASK -> IRQ -> OVERFLOW -> SDEI_NORMAL + * TASK -> SDEI_NORMAL -> SDEI_CRITICAL -> 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 + * stack. + */ + if (info->type == state->prev_type) { + if (fp <= state->prev_fp) + return -EINVAL; + } else { + __set_bit(state->prev_type, state->stacks_done); + } + + /* + * Record this frame record's values and location. The prev_fp and + * prev_type are only meaningful to the next unwind_next() invocation. + */ + state->fp = READ_ONCE(*(unsigned long *)(fp)); + state->pc = READ_ONCE(*(unsigned long *)(fp + 8)); + state->prev_fp = fp; + state->prev_type = info->type; + + return 0; +} #endif /* __ASM_STACKTRACE_COMMON_H */ diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 94a5dd2ab8fd..834851939364 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -81,48 +81,15 @@ static int notrace unwind_next(struct unwind_state *state) struct task_struct *tsk = state->task; unsigned long fp = state->fp; struct stack_info info; + int err; /* 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)) - return -EINVAL; - - if (test_bit(info.type, state->stacks_done)) - return -EINVAL; - - /* - * As stacks grow downward, any valid record on the same stack must be - * at a strictly higher address than the prior record. - * - * Stacks can nest in several valid orders, e.g. - * - * TASK -> IRQ -> OVERFLOW -> SDEI_NORMAL - * TASK -> SDEI_NORMAL -> SDEI_CRITICAL -> 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 - * stack. - */ - if (info.type == state->prev_type) { - if (fp <= state->prev_fp) - return -EINVAL; - } else { - __set_bit(state->prev_type, state->stacks_done); - } - - /* - * Record this frame record's values and location. The prev_fp and - * prev_type are only meaningful to the next unwind_next() invocation. - */ - state->fp = READ_ONCE(*(unsigned long *)(fp)); - state->pc = READ_ONCE(*(unsigned long *)(fp + 8)); - state->prev_fp = fp; - state->prev_type = info.type; + err = unwind_next_common(state, &info); + if (err) + return err; state->pc = ptrauth_strip_insn_pac(state->pc); From patchwork Tue Jul 26 07:37:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928979 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 66375C43334 for ; Tue, 26 Jul 2022 07:39:53 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=pXj23wDojfDdgQ+M6LbKSsISO1eqc+RiGhyXOu7oF2g=; b=vC3j0S32U/CGiCO8XfCkxUiJtJ 6Wl8XE97IXNiX2ADQueTshzIbPkQ0CNMvlN1qAKA6cg8TVjZHe+zyXzwOC8KiJIPk/4kLbUpPdvGu Rtd9M4eOVbvhofC9fXSafwTd164dx4ZuP7K+A8ZkE0GFO1MTotsq4yz9CpYiEbcV/y4CIF9HuvLGw IXqrA+V0HxGcbbYpHA6+SiP5z7UlWr4ujmTbgF+UNqam13NtMLNYzZkFhS6NVjxp90pAUBRDcPpmB euet3TlWVTRovENUCMmcSTW8msOy6OpIXqepHzIsHSr62guLLy0V8HXj0js8nofZyHUKFdPCl9+m/ isG9QrcA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGF9T-009W5v-0d; Tue, 26 Jul 2022 07:38:47 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGF8o-009Vgi-MG for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:08 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-31f49eea05fso4815657b3.15 for ; Tue, 26 Jul 2022 00:38:05 -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:to :cc:content-transfer-encoding; bh=KintJzFkc3fGk0PZThBnEs9+mVIkzS8MGNi7+4hHocM=; b=F99U/GyUMm86I5hhUPAEh3i37Efs41f+NjVsXrba2/WKPQPyfrqe2tZkqcM8WLdn4N tjoyQVaWZBceiGc+IiYE68nlfbCinUJe03/UFDa39xJ3UpbZdD9kbzr1fXgF94y52jEV rS8Lbe7khCJnAGE3uuqPQYZcwfmKGiiahXSgT5hnhAxspgLgUZwx2XcFjsYvNHIlG4TO Kz3pafuhE8xSvKeQKTkBbd6h7vha4xx8mVypWM60J3ynB/E+WPGvUVSThWgp4tikyd+6 meIxa9eGPMfqTbQzFurHAAv657oZE/ifKVL3Y5P3LVWtokWYf79sEL7M95vb7JRmvBTK wWkA== 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:to:cc:content-transfer-encoding; bh=KintJzFkc3fGk0PZThBnEs9+mVIkzS8MGNi7+4hHocM=; b=uMehfuHIFtD8VNqjilrsu3xEbFii8r5tTqCyx7jb4jJEMWx8s/FPZ37I2rByrFLpH+ sdNaesrLpYsjc4Uxm5jyiHq9DCuYaWQIcF5PfFG9CaBkeYxD+2E4Ij27xlkZzRPwXuBf aoPutoRDwLqN9hrZdHs+nSRnD+CWC9ba4X0BW3+zZ06Yfw3q93bFqA/PjtQkGWFWz6PJ D02wLJ3WetzDDlMXdIy3xaytz7lJJ5j2XZkx1Itk5usXYrI5Jmc/rAaX0KYMB+2JFRDE EAO3s5fgIfyv0xWp9csN6vlRJN5FcH1aZ9nGHZuB59DwiwpRMvkC0p3mh1rPy29KqsBa +fNQ== X-Gm-Message-State: AJIora8RXxaZ6LyXdLVq4r+mdvIuxaVsaOScc1BE1VJg9SsEOD5scTZT 9gFTgQgvdcARdDCHFd1goQCMNEvN8q6Rsp5LbA== X-Google-Smtp-Source: AGRyM1tViIgbjsnsd0F2wwzY/mHFi+V+awQaQAAf7taOX+okajo7NRVMTiuzJzRWy3+FHjR02RHuf/ccxOBvmI+klQ== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a25:d796:0:b0:671:6696:d824 with SMTP id o144-20020a25d796000000b006716696d824mr3624037ybg.52.1658821085312; Tue, 26 Jul 2022 00:38:05 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:37 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-5-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 04/17] arm64: stacktrace: Handle frame pointer from different address spaces From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003806_750033_2DBA1F6A X-CRM114-Status: GOOD ( 19.34 ) 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 unwinder code is made reusable so that it can be used to unwind various types of stacks. One usecase is unwinding the nVHE hyp stack from the host (EL1) in non-protected mode. This means that the unwinder must be able to translate HYP stack addresses to kernel addresses. Add a callback (stack_trace_translate_fp_fn) to allow specifying the translation function. Signed-off-by: Kalesh Singh Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba Reviewed-by: Mark Brown --- Changes in v6: - Fix typo in comment, per Fuad - Add Fuad’s Reviewed-by tag and Tested-by tags Changes in v5: - Fix typo in commit text, per Fuad - Update unwind_next_common() to not have side effects on failure, per Fuad - Use regular comment instead of doc comments, per Fuad arch/arm64/include/asm/stacktrace/common.h | 29 +++++++++++++++++++--- arch/arm64/kernel/stacktrace.c | 2 +- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace/common.h b/arch/arm64/include/asm/stacktrace/common.h index 0c5cbfdb56b5..b241edba5c76 100644 --- a/arch/arm64/include/asm/stacktrace/common.h +++ b/arch/arm64/include/asm/stacktrace/common.h @@ -124,11 +124,25 @@ static inline void unwind_init_common(struct unwind_state *state, state->prev_type = STACK_TYPE_UNKNOWN; } +/* + * stack_trace_translate_fp_fn() - Translates a non-kernel frame pointer to + * a kernel address. + * + * @fp: the frame pointer to be updated to its kernel address. + * @type: the stack type associated with frame pointer @fp + * + * Returns true and success and @fp is updated to the corresponding + * kernel virtual address; otherwise returns false. + */ +typedef bool (*stack_trace_translate_fp_fn)(unsigned long *fp, + enum stack_type type); + static inline int unwind_next_common(struct unwind_state *state, - struct stack_info *info) + struct stack_info *info, + stack_trace_translate_fp_fn translate_fp) { + unsigned long fp = state->fp, kern_fp = fp; struct task_struct *tsk = state->task; - unsigned long fp = state->fp; if (fp & 0x7) return -EINVAL; @@ -139,6 +153,13 @@ static inline int unwind_next_common(struct unwind_state *state, if (test_bit(info->type, state->stacks_done)) return -EINVAL; + /* + * If fp is not from the current address space perform the necessary + * translation before dereferencing it to get the next fp. + */ + if (translate_fp && !translate_fp(&kern_fp, info->type)) + return -EINVAL; + /* * As stacks grow downward, any valid record on the same stack must be * at a strictly higher address than the prior record. @@ -163,8 +184,8 @@ static inline int unwind_next_common(struct unwind_state *state, * Record this frame record's values and location. The prev_fp and * prev_type are only meaningful to the next unwind_next() invocation. */ - state->fp = READ_ONCE(*(unsigned long *)(fp)); - state->pc = READ_ONCE(*(unsigned long *)(fp + 8)); + state->fp = READ_ONCE(*(unsigned long *)(kern_fp)); + state->pc = READ_ONCE(*(unsigned long *)(kern_fp + 8)); state->prev_fp = fp; state->prev_type = info->type; diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 834851939364..eef3cf6bf2d7 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -87,7 +87,7 @@ static int notrace unwind_next(struct unwind_state *state) if (fp == (unsigned long)task_pt_regs(tsk)->stackframe) return -ENOENT; - err = unwind_next_common(state, &info); + err = unwind_next_common(state, &info, NULL); if (err) return err; From patchwork Tue Jul 26 07:37:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928981 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 240B9C43334 for ; Tue, 26 Jul 2022 07:40:46 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=WZGna5fPLOoE6Qy6JJ9oH3Pl8d3BGQPznArKLuf+110=; b=4dOjA/2n9n15+mAAZ1m1h7Txbl HdjI9joIX9xL72oM8oe6X/O/ecT6z+h4oKWmP5e1Ok9Du6szBOs+PFRx43t78Lgs8zldiTKFkAcLK NDTybtDtL+EYI+mPdAnKHEKqnLQbpeXmQ7GyZvyxU/NwjDidCY5JT8a82VFFtfIGWE8phI5zmV/3Y PlAz6VZ2Js4cEFXYZ06RBYWMEIobsOlYRZjCEJprw3uBUXKQws+CY5U2o3JGk15H9hvhCAzsXKaNi 0PCq6Nh/KBYsXdyJVC58kg1UA/AvBDWMdyUK5Njg7oNxAQWCLAUT+f981TkZrK8MI6Q884YqGD8Ww NPY3SNxA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGFAK-009Wi4-1N; Tue, 26 Jul 2022 07:39:40 +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 1oGF8t-009Vi6-C8 for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:13 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id b18-20020a25bb52000000b0067128e66131so4355512ybk.18 for ; Tue, 26 Jul 2022 00:38:08 -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:to :cc:content-transfer-encoding; bh=oKsO05P5VhcOhgxPaMS6HDnukgyf9AnFQ5c8xI8uER0=; b=RWdGAWr1wiMKMYmt1Q0zaOtg01Gpkj8XyrBSlfbuJSLqsyPs2tLYDKHtBSOwf48Shy DkYgX60VLBIuDQouUNeq5KQTYkgOdpKW44mgBBJ+LicBqyV0Z1NWi5StUYyqdAVS1ll5 F568b6yHoHL1tWf/TUQCFWcSuioO8OfEun/UrNi2xeOZf9PZVBW192GSWJrSrCJ3W9PY /Po1ewQ0PS3T1o4JmjsyyYeOfzZfsWEVS/4QdzhlNKOPA29XaZqAVU0WP0ixV2f7Isti P0KEbN70EUnCLoPljXREc1wrcu5VQV3ElufIlRi4JXKUyOzY/0wnxsVuLcaVlVjQ/ff3 iPsA== 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:to:cc:content-transfer-encoding; bh=oKsO05P5VhcOhgxPaMS6HDnukgyf9AnFQ5c8xI8uER0=; b=XQ0q2cyNlzMgUEEnH/dPrM3XzZdI4X/6KpxFMQQKgsAR7lDsgYZ7jYyK9fTv0i5y1w NC7haMrLLsRFAqS+l3wdYN98SZU80IB4a+r78Tiu4IO/CrYn0LnuM9INbRci5e62Qkzq +u+LBC3S7EokolupoMREtINaFET9LRALK4Tp0z9N+Xs9sqNHjp38hCSJLZj79kqSMA4+ CxxpvmmbNTiY5G6Vro5/gt+8BGbCroHlpmavcBHtZI18QhGim0Binu7aBwVZDwEeHkj9 DSzUznIfeAzD+IA/lliThMYng7zxTCy3LOmOZxDo1okpgRzTHF64/yqObLSbP38ByyiE clPA== X-Gm-Message-State: AJIora9+gJggKIE/WrU0E2owngyI/vuepnS3cG+LyQXNyTJq9QMMEZNA 99iDceZI91Ae605XKPOvuhvvBPC8Ln5fu55n8Q== X-Google-Smtp-Source: AGRyM1tkUVtSxVSYagZ3IM0myL/MmHggfXa+9W+OWUGgjocjbTQ1+SnKGqSf50Z5nLH4MLcVuHH3XYxWyaCFV7DDXg== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a05:6902:10c1:b0:66f:1f9f:9c00 with SMTP id w1-20020a05690210c100b0066f1f9f9c00mr11818088ybu.583.1658821087741; Tue, 26 Jul 2022 00:38:07 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:38 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-6-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 05/17] arm64: stacktrace: Factor out common unwind() From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003811_442935_93779C26 X-CRM114-Status: GOOD ( 18.53 ) 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 Move unwind() to stacktrace/common.h, and as a result the kernel unwind_next() to asm/stacktrace.h. This allow reusing unwind() in the implementation of the nVHE HYP stack unwinder, later in the series. Signed-off-by: Kalesh Singh Reviewed-by: Fuad Tabba Reviewed-by: Mark Brown Tested-by: Fuad Tabba --- Changes in v6: - Add Mark Brown’s Reviewed-by tag - Add Fuad's Tested-by tag Changes in v5: - Add Reviewed-by tag from Fuad arch/arm64/include/asm/stacktrace.h | 51 ++++++++++++++++ arch/arm64/include/asm/stacktrace/common.h | 19 ++++++ arch/arm64/kernel/stacktrace.c | 67 ---------------------- 3 files changed, 70 insertions(+), 67 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace.h b/arch/arm64/include/asm/stacktrace.h index 43f4b4a6d383..ea828579a98b 100644 --- a/arch/arm64/include/asm/stacktrace.h +++ b/arch/arm64/include/asm/stacktrace.h @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -80,4 +81,54 @@ static inline bool on_accessible_stack(const struct task_struct *tsk, return false; } +/* + * Unwind from one frame record (A) to the next frame record (B). + * + * We terminate early if the location of B indicates a malformed chain of frame + * 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 inline int notrace unwind_next(struct unwind_state *state) +{ + struct task_struct *tsk = state->task; + unsigned long fp = state->fp; + struct stack_info info; + int err; + + /* Final frame; nothing to unwind */ + if (fp == (unsigned long)task_pt_regs(tsk)->stackframe) + return -ENOENT; + + err = unwind_next_common(state, &info, NULL); + if (err) + return err; + + state->pc = ptrauth_strip_insn_pac(state->pc); + +#ifdef CONFIG_FUNCTION_GRAPH_TRACER + if (tsk->ret_stack && + (state->pc == (unsigned long)return_to_handler)) { + unsigned long orig_pc; + /* + * This is a case where function graph tracer has + * modified a return address (LR) in a stack frame + * to hook a function return. + * So replace it to an original value. + */ + orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc, + (void *)state->fp); + if (WARN_ON_ONCE(state->pc == orig_pc)) + return -EINVAL; + state->pc = orig_pc; + } +#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ +#ifdef CONFIG_KRETPROBES + if (is_kretprobe_trampoline(state->pc)) + state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur); +#endif + + return 0; +} +NOKPROBE_SYMBOL(unwind_next); + #endif /* __ASM_STACKTRACE_H */ diff --git a/arch/arm64/include/asm/stacktrace/common.h b/arch/arm64/include/asm/stacktrace/common.h index b241edba5c76..4b632141d91c 100644 --- a/arch/arm64/include/asm/stacktrace/common.h +++ b/arch/arm64/include/asm/stacktrace/common.h @@ -9,6 +9,7 @@ #include #include +#include #include enum stack_type { @@ -69,6 +70,8 @@ static inline bool on_accessible_stack(const struct task_struct *tsk, unsigned long sp, unsigned long size, struct stack_info *info); +static inline int unwind_next(struct unwind_state *state); + 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) @@ -191,4 +194,20 @@ static inline int unwind_next_common(struct unwind_state *state, return 0; } + +static inline void notrace unwind(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(state); + if (ret < 0) + break; + } +} +NOKPROBE_SYMBOL(unwind); #endif /* __ASM_STACKTRACE_COMMON_H */ diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index eef3cf6bf2d7..9fa60ee48499 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -7,14 +7,12 @@ #include #include #include -#include #include #include #include #include #include -#include #include #include @@ -69,71 +67,6 @@ static inline void unwind_init_from_task(struct unwind_state *state, state->pc = thread_saved_pc(task); } -/* - * Unwind from one frame record (A) to the next frame record (B). - * - * We terminate early if the location of B indicates a malformed chain of frame - * 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 unwind_state *state) -{ - struct task_struct *tsk = state->task; - unsigned long fp = state->fp; - struct stack_info info; - int err; - - /* Final frame; nothing to unwind */ - if (fp == (unsigned long)task_pt_regs(tsk)->stackframe) - return -ENOENT; - - err = unwind_next_common(state, &info, NULL); - if (err) - return err; - - state->pc = ptrauth_strip_insn_pac(state->pc); - -#ifdef CONFIG_FUNCTION_GRAPH_TRACER - if (tsk->ret_stack && - (state->pc == (unsigned long)return_to_handler)) { - unsigned long orig_pc; - /* - * This is a case where function graph tracer has - * modified a return address (LR) in a stack frame - * to hook a function return. - * So replace it to an original value. - */ - orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc, - (void *)state->fp); - if (WARN_ON_ONCE(state->pc == orig_pc)) - return -EINVAL; - state->pc = orig_pc; - } -#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ -#ifdef CONFIG_KRETPROBES - if (is_kretprobe_trampoline(state->pc)) - state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur); -#endif - - return 0; -} -NOKPROBE_SYMBOL(unwind_next); - -static void notrace unwind(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(state); - if (ret < 0) - break; - } -} -NOKPROBE_SYMBOL(unwind); - static bool dump_backtrace_entry(void *arg, unsigned long where) { char *loglvl = arg; From patchwork Tue Jul 26 07:37:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928980 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 CA306C433EF for ; Tue, 26 Jul 2022 07:40:23 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=Qa18ZE262pnQ+Wur8XpWS0ykR5KMxB2XqiBONKeV0/E=; b=I+Zmi1cDcONnjnb3eriJw21C6l EkkMXMzaWSNuIt6AHCJlAk7cXX3GXjRgGyBtwc8vz9PDEbtHU/HTxFI9dRXwC8l+ydqdcx03dPjWp jLnob0ZFi5RIQwC9LHRtAjzOK0vQM8sPSIo7wxiXoop5yjpL18jDbRb7zaS/ARcVsNurUgF5dG3hx GYYaUm2fpsXYEOYWt3X/aFmJqKHtWf09NfRXKUlqwr8lidofIkE7/itenMHta67zw6EyH/j+tmwM2 LL+idVXLHiS/27T0ixngGvKITupQ0WJ/6Lz8AHj1TK7ylrfhwVd6uKWT9iY0Mf8yxioXPrAf6EC0j /jGD6dfQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGF9t-009WQ9-JS; Tue, 26 Jul 2022 07:39:14 +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 1oGF8t-009VjG-Gt for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:12 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 83-20020a250156000000b006707fbc22ddso10435706ybb.3 for ; Tue, 26 Jul 2022 00:38: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:to :cc:content-transfer-encoding; bh=VavrLtXbRNR5iYY6DuWJsiPM2oJzRiMbR593lo0d1Rw=; b=j96IaEX62h71DWb6L/1x3UIPVuOe11L4SskRUNLfEpqgu2vu4jVoT7hkbCAryRObVn Zi9PUjFc3msAp6dAeAxLeAvs5ursb0jnWMfa66uohrRUKiYMKU6skuOaJAlmHKMEnNXn osaNYizMUsQjs8peiYd/0YDjLYH7izn6TGf8Vejoqckt5OSYyCR7wAWcWajSZnntZDeQ G8qLCUQzdjzgvYlwgCnfYmJjBnkmOh5UZMKCFOUAbItZYA5Di/+5IJKFfMmR5lxLeFg+ 6H5z7pSY/XdyftcagV0boM4TJERzjbvtdEnrnWDxPwe3kwN/CDSKVtd3hu6+6QLzA8Z7 0jDw== 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:to:cc:content-transfer-encoding; bh=VavrLtXbRNR5iYY6DuWJsiPM2oJzRiMbR593lo0d1Rw=; b=x+inj669gpd9lxeC7bNKxGdTCEusq4fPW/r4NicmxV24YgWZXVwhMJJNcr4rhYeIVg 62KbE3IqdozlU28cWkaQ7WCbYKlZgyvXswnwz3fzizQr6vlPQmdwNdCqB7YbRuM/gBqN 8xGAJavfAk85UsBogk8InfEDTKoph1frD/fjWcrIVi5+kHjLRmEwZJGh3CX3KH3uDsCZ xTTri33KKeHAKBcskeEvT0Jt+EEDorB2Z6aF6IhFrCb1rHzT4/S+PxueodTHS4X0ykT0 0WM5JSbvgaVS0shALr18ObI3u+d3JdwKH6ZVNNsrIan7mI+8dgZWlc6HSjkf2EmxZRLe yjfw== X-Gm-Message-State: AJIora+OT+bTC1AmNX0ZE1LOfSqFb1okBCJ5+UdEK144F0kJr7wo3mLG BWFT6Hd3yp/LbkTjqRV2qHvPsMnPAnspSRr0UQ== X-Google-Smtp-Source: AGRyM1uLngP1y5rfZv7qcR91UDrsOk5KX5o+Z2nFXgrbRkoOxYN5kjOvh7dIZvVEJTbG63rkQ3WbCIEv9Qf04bN+kA== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a81:1b97:0:b0:2db:640f:49d8 with SMTP id b145-20020a811b97000000b002db640f49d8mr13021154ywb.326.1658821090210; Tue, 26 Jul 2022 00:38:10 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:39 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-7-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 06/17] arm64: stacktrace: Add description of stacktrace/common.h From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003811_591317_E08DF244 X-CRM114-Status: UNSURE ( 9.72 ) X-CRM114-Notice: Please train this message. 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 Add brief description on how to use stacktrace/common.h to implement a stack unwinder. Signed-off-by: Kalesh Singh Reviewed-by: Fuad Tabba Reviewed-by: Mark Brown --- Changes in v6: - Add Fuad’s Reviewed-by tag Changes in v5: - Add short description of each required function, per Fuad and Marc arch/arm64/include/asm/stacktrace/common.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/arch/arm64/include/asm/stacktrace/common.h b/arch/arm64/include/asm/stacktrace/common.h index 4b632141d91c..45474b383630 100644 --- a/arch/arm64/include/asm/stacktrace/common.h +++ b/arch/arm64/include/asm/stacktrace/common.h @@ -2,6 +2,21 @@ /* * Common arm64 stack unwinder code. * + * To implement a new arm64 stack unwinder: + * 1) Include this header + * + * 2) Provide implementations for the following functions: + * on_overflow_stack(): Returns true if SP is on the overflow + * stack. + * on_accessible_stack(): Returns true is SP is on any accessible + * stack. + * unwind_next(): Performs validation checks on the frame + * pointer, and transitions unwind_state + * to the next frame. + * + * See: arch/arm64/include/asm/stacktrace.h for reference + * implementations. + * * Copyright (C) 2012 ARM Ltd. */ #ifndef __ASM_STACKTRACE_COMMON_H From patchwork Tue Jul 26 07:37:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928982 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 06A50C433EF for ; Tue, 26 Jul 2022 07:41:43 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=oU/C4AIIZ12yaj1tt3wIohsVre3bPSJruqMmjN4Edz0=; b=nbCszMXYnHjPq1psmkw488iOla +mtxPA2ESxDrm7VvQfVwSQE6b4UXTXPDfN4C7HH9eLeAgIrSvIOB8+6WT49yczYf3PFoYraZ/B/7t zYKr13myD5NVY+rXKZU1fTNn4fwQJ8EK7UmMJG4U9qlnmPJ167ViLnQSIH/0u9HQqsxU1OU1j1Jgf IZwvXH0iLLuPIg1/ujdsE+lST4qEHLwN2LDRcG/UHamIun2l1NzWeDSEUowY1T805NT5lqW2rMFAk vTCx4HyA0CRj9IJNa02JgkJtCzvMnOwyHS3zOx2VEOZGyEkun7HQWMMvxHSAraXF13Tm+IZCN4Zxc fINCCrug==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGFB3-009XI1-Ns; Tue, 26 Jul 2022 07:40:25 +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 1oGF8v-009VkQ-W4 for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:16 +0000 Received: by mail-yb1-xb49.google.com with SMTP id z6-20020a056902054600b00670e3c8b43fso8388756ybs.23 for ; Tue, 26 Jul 2022 00:38:13 -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:to :cc; bh=jVgz/szeSDEehPkBGFOtiG6TH4cWoSnAE1JEiuFeF4M=; b=C8Yf1l5/uTkII4CdB1cNpCvsxEc+28PsYTXUIba+64kfS7ozSMTCfLNCiyBvwQNoYx v4tLxHFGn1NugmrjBRhPGqOdAZyJXo6plyN8kY2kxqrqmVq3LsXugD6jJievh78HX1KU 8SV94Q4buvRguuD8HdNpI5/LLT4QrCi5nivUYVxFdpMWinuWqv8Wjo2CCaEzyts5qj2l xJc8atY4LJrRDy1iJjqOnQ7dmFBLYGMRuagGcaTc0e7clYvr/Z70nvyA4TtGSrPQOrH8 Y5fQN/ePeHrKVVNVIg/c62DaamTj1boYjZ/CNnJsQw/brcmyDLOOKu7s7UrMm8NQL9ED RE3A== 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:to:cc; bh=jVgz/szeSDEehPkBGFOtiG6TH4cWoSnAE1JEiuFeF4M=; b=UlV5/NIRx6FftvdrPYq7D0qu+PzFVwbzmZgd1yNn095e+EHcRqKUAYwM9X1IPgd3V6 /RPslCTmwm0NVrUnZK4FrpTvv2GubmB5b/BOfH7Ab5dKBI5s0rxO5Cnmsn9lXVh2H6bb x+na5CZVXK9Dy6gBHyHEiszbWaefzYhtOoEm2VwUC+olIsoNl6ztQxHnLyyliv2DPbau zUA7GBADHMD5Ef4eFWizr+H6zUjaB6Qd/WGHrcBSKmaVGV1Kj2Y5mSxK15tU8D5xNEGe kUMzCvjpuqBzxTQPmcU5Cu867LN6kisKxlu1a4PLhWnjO1pgWzH8mqceOK57amBs+v7g LKpw== X-Gm-Message-State: AJIora8oaEbCA5Ii+bjwg+BX+Vd0J/QOZXqOaU/7FJuU7/xPrObUWhH2 EzalbcsQGzorC6+0Nn9ZSc+hHJbxSc39IkofWg== X-Google-Smtp-Source: AGRyM1u/sM09rRaGMw6Yf5N3cXgpcmHXKZVyWJE18QaDwOtW5yaQPATPB7V5gHdj9ore7niZ9XvnVO65RXiiNAWyfg== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a05:6902:1613:b0:671:2e0c:dda1 with SMTP id bw19-20020a056902161300b006712e0cdda1mr7585688ybb.514.1658821092633; Tue, 26 Jul 2022 00:38:12 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:40 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-8-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 07/17] KVM: arm64: On stack overflow switch to hyp overflow_stack From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003814_061178_0572C7E0 X-CRM114-Status: GOOD ( 16.21 ) 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 stack overflow switch to 16-byte aligned secondary stack. 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 Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- Changes in v6: - Add Fuad's Tested-by tag Changes in v5: - Add Reviewed-by tag from Fuad arch/arm64/kvm/hyp/nvhe/Makefile | 2 +- arch/arm64/kvm/hyp/nvhe/host.S | 9 ++------- arch/arm64/kvm/hyp/nvhe/stacktrace.c | 11 +++++++++++ 3 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 arch/arm64/kvm/hyp/nvhe/stacktrace.c diff --git a/arch/arm64/kvm/hyp/nvhe/Makefile b/arch/arm64/kvm/hyp/nvhe/Makefile index f9fe4dc21b1f..524e7dad5739 100644 --- a/arch/arm64/kvm/hyp/nvhe/Makefile +++ b/arch/arm64/kvm/hyp/nvhe/Makefile @@ -14,7 +14,7 @@ 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 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 diff --git a/arch/arm64/kvm/hyp/nvhe/host.S b/arch/arm64/kvm/hyp/nvhe/host.S index ea6a397b64a6..b6c0188c4b35 100644 --- a/arch/arm64/kvm/hyp/nvhe/host.S +++ b/arch/arm64/kvm/hyp/nvhe/host.S @@ -177,13 +177,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 + OVERFLOW_STACK_SIZE, x0 b hyp_panic_bad_stack ASM_BUG() diff --git a/arch/arm64/kvm/hyp/nvhe/stacktrace.c b/arch/arm64/kvm/hyp/nvhe/stacktrace.c new file mode 100644 index 000000000000..a3d5b34e1249 --- /dev/null +++ b/arch/arm64/kvm/hyp/nvhe/stacktrace.c @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * KVM nVHE hypervisor stack tracing support. + * + * Copyright (C) 2022 Google LLC + */ +#include +#include + +DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack) + __aligned(16); From patchwork Tue Jul 26 07:37:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928983 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 40C39C43334 for ; Tue, 26 Jul 2022 07:42:02 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=LVACHgbd16rXhADPYqHYLxXVexCDcG/lPwn6wGey0E4=; b=sOsvauJ44w7QChhvt5K1Jt6Vfu 6GI9PRLsGnvI9W7vaA+hWaYkCdzJYw4FE018dix9v0WPYQ2TJgyfJXsthyGEUUrHfMMxSJW5vWVgY Hn8gpMdOtfcBMjmtyT7scWKTdI1iWFTFZT+nY9skYRt7K9NxwoHlDmB3rPeXFGzVTZ3HpY2j8vXq7 RZZbk6qqZo3tdHtD0tPD28Au1HKgox4wol9xh53q62/ZSIfQai6E6Kvf9ahr1fFcWJAO0pxPlDdZm foV/3qPNV9UTjxyp6F0M6N2u6sKOIBVaGn2MSXmxlwW77H3/oTepo1wXUuFkn6P+HC1Lm6/1y2+Cj UCz0dxvg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGFBQ-009XcV-0A; Tue, 26 Jul 2022 07:40:48 +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 1oGF8y-009Vlm-0W for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:17 +0000 Received: by mail-yb1-xb49.google.com with SMTP id y13-20020a5b09cd000000b0067114eb5b50so5462779ybq.17 for ; Tue, 26 Jul 2022 00:38:15 -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:to :cc:content-transfer-encoding; bh=i3lgJjiT46mrfMrlOUzdDB8/4iw1XwFuVsxnh7Km3KU=; b=WOkwBJsq6ZjTSMovFjTZSwD8sMa0HEZuNKHEgdh0voOyB5tJv9hkeQ+fZDL1k9LQcl HdmKNrGKCbOT5bEqpnEt7rwEAZDZ+0zTSWbaLPiU0AqMqsipdiozcfCUa9u6bPIBOatS vWPrP5aG+wSpIvX8sr/94a4xqITU92dSLf2AYfmTEoqKskq1JhQN2gcbTTrtvANOu5eO 1vVBWRdSsgYX2zoljGCoDswSlE7tA3EoGUGyC5jpvxoGAWjlU43CU5+5obQRNcLBOQGL H7NE9oMLvMbY89vNyCOO9gqAaCApSjv9H1wQVr4qEtQwGbYboXkRvHJugniaI+3iDPfl O31w== 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:to:cc:content-transfer-encoding; bh=i3lgJjiT46mrfMrlOUzdDB8/4iw1XwFuVsxnh7Km3KU=; b=fnIx+u/Mgo2218Ub7Rr/z3q8yi1gXJYQTbdunWCp9HDWiOCsLg7KwWfjE6oewQrSmq trWWcsBd80noNWn2IOOKZ5FvjErGTFXu2JhPKz/3iuZDcpGB2GbSCzP/1h9NQCM0Zfzv fEwOZ7wSrIDFwAeHr2NId5K9j/2vusa78OHaKiSLicdX828pCPM35jRZnA6D6RnAhyJd be8kaZoPTwmG/dNtD644Psb8Czck5gSH6BEQgP75Bp7btqN158ZJ2uE5O5ibnoEK+jfU BJeG0aHG76wgIuZMDPRH/MFRynVwVn+WUrA2yD5TaQM9JeNvBJfnlqP0Cts4t6Ol2yCJ amVA== X-Gm-Message-State: AJIora/N2AEYSWU7ykR2oHV1s8VQ+JOSVZ7SKhwZQ4BfSwXiQxuHtuzm sNP4tjvGYbtrQ8cq0h4bUHslAGNW2O4HtMCHpQ== X-Google-Smtp-Source: AGRyM1uSyW7sYTMSgC+uaW3gfAmuo2IndnCXRVAcmDi9a0epcfogUXoXeu1EIc1/i1iJusQo8hJnHjXzlwV/bKaYCw== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a25:9e92:0:b0:66e:c03e:c632 with SMTP id p18-20020a259e92000000b0066ec03ec632mr12231031ybq.279.1658821094784; Tue, 26 Jul 2022 00:38:14 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:41 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-9-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 08/17] KVM: arm64: Stub implementation of non-protected nVHE HYP stack unwinder From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003816_090564_74EB2940 X-CRM114-Status: GOOD ( 13.70 ) 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 Add stub implementations of non-protected nVHE stack unwinder, for building. These are implemented later in this series. Signed-off-by: Kalesh Singh Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- Changes in v6: - Add Fuad’s Reviewed-by and Tested-by tags Changes in v5: - Mark unwind_next() as inline, per Marc - Comment !__KVM_NVHE_HYPERVISOR__ unwinder path, per Marc arch/arm64/include/asm/stacktrace/nvhe.h | 47 ++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 arch/arm64/include/asm/stacktrace/nvhe.h diff --git a/arch/arm64/include/asm/stacktrace/nvhe.h b/arch/arm64/include/asm/stacktrace/nvhe.h new file mode 100644 index 000000000000..1192ae0f80c1 --- /dev/null +++ b/arch/arm64/include/asm/stacktrace/nvhe.h @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * KVM nVHE hypervisor stack tracing support. + * + * The unwinder implementation depends on the nVHE mode: + * + * 1) Non-protected nVHE mode - the host can directly access the + * HYP stack pages and unwind the HYP stack in EL1. This saves having + * to allocate shared buffers for the host to read the unwinded + * stacktrace. + * + * Copyright (C) 2022 Google LLC + */ +#ifndef __ASM_STACKTRACE_NVHE_H +#define __ASM_STACKTRACE_NVHE_H + +#include + +static inline bool on_accessible_stack(const struct task_struct *tsk, + unsigned long sp, unsigned long size, + struct stack_info *info) +{ + return false; +} + +#ifndef __KVM_NVHE_HYPERVISOR__ +/* + * Conventional (non-protected) nVHE HYP stack unwinder + * + * In non-protected mode, the unwinding is done from kernel proper context + * (by the host in EL1). + */ + +static inline bool on_overflow_stack(unsigned long sp, unsigned long size, + struct stack_info *info) +{ + return false; +} + +static inline int notrace unwind_next(struct unwind_state *state) +{ + return 0; +} +NOKPROBE_SYMBOL(unwind_next); + +#endif /* !__KVM_NVHE_HYPERVISOR__ */ +#endif /* __ASM_STACKTRACE_NVHE_H */ From patchwork Tue Jul 26 07:37:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928984 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 EAC73C433EF for ; Tue, 26 Jul 2022 07:42:37 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=7d3clnXJvpO4m72yF3Ogmbsy0FYD6/AHGaLhcJIjZ34=; b=Q4mfQmaRqV/W5W5uMLen7uTlfr IlpfgRbP+I8/pLDU9CieNBFJZQ5/b5JerJHqs877Ya0Vdy6i+qq3o/jOAIS3AD1/T/27/4gu+Xl+D oPYqs6Klf4tc88Dq82sjvW0yFyjawg1fH1Z/AGlVuTqLPvDI487IFPqFElu+kfaYEiPdjdflSn5MR +uAZxm3+cF1qwazB3yxOR6ebj5dFsYFQzSOpuuJ1YCbTr/PixRM1CQDgIJedtjJWmba+hw88/FDD6 tRJ2LZgoAAEv29SvlPP4gs2pvSNSxm+DYh/9HW1WmR4fyJN8GsnoemH7RMCxjjOonCDzy5MU89DrA JqDtWOlg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGFC6-009YBL-8y; Tue, 26 Jul 2022 07:41:30 +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 1oGF92-009Vn5-Tj for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:22 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-31e89d6bea7so101349167b3.10 for ; Tue, 26 Jul 2022 00:38:17 -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:to :cc:content-transfer-encoding; bh=9m8IvwBOrk2IG7YkQAmQznPKKCvMCFPD8HJb+zFgtK8=; b=VhKEEavk6g8DUisEYJ5fAHdMTvKq1n+wB7+56Ad5f4Tt+AZAOv7GuRys7Kgb+3+KOG p03FaqATet8gRqAPFppMF9Kfbj/aDmzrkJudV1qME6MnQKWKFtW9pdbRTtwAn+OYEWy9 B7NVUpEbguxv4nUZgDaWFKufpopj+7xZULmeUF3mYeIBkoglfz3pXlVJNkApttUvBYbv rf6vSs4cE7BLt6NNMWawOgPLMDkK1hTQ1GT2YS9ZHmApphd+I6th/NhszMgZ5+elz7pb C/8gi3GqQXgiVyNuV4OmLVw58XuRoDb52Hqoazg89l/+gnlHqCoQ8xlq2Oe6EZ9y9f0p VBuw== 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:to:cc:content-transfer-encoding; bh=9m8IvwBOrk2IG7YkQAmQznPKKCvMCFPD8HJb+zFgtK8=; b=N59QyOw355nLSoDS2gdGZ6o9tCpqBLSOXcBUX/R1UGWlosPYTJt2GrPXGAaz47/eN8 E8dtXfdOX/bjjBo8IHv68fhxhG7av8u/yKr6btpovWBH0O1wxZz8THFwNA5kClsxtpso MOz0mWmopCvEL/EbWqSUe10D/vXz5GwGDjCcEKjhF+PrTAz7L4a8WoyFpZK8O3mIO1Dk Pmo2B8qC3UNDR01R/J1JOxLe0hAA7ZTDVPWLzRLpmhPb7DuGW63/IPjGtyZU9RcmMY50 4igRnia7XrGR2uwjSewK/v3SiJS434KpwPgJj3gFDQvmKYoE6s5EUJRkAHqnxNYSReHX SwbQ== X-Gm-Message-State: AJIora8EXF5GEyvWr7R82qUc/p8gbwThPHwkePeKpWWTh1D+1MJHdZjz Xl4p9owyTDExotf1TkLUyPZzljFoaAXXxWAejQ== X-Google-Smtp-Source: AGRyM1tZibhQ2sQ3XGV5gsfb456P6K4Ka3kCr7dAdskf5tEREIiKvgCRKyyCGjYAT/M3AHlYk45u/sv5vdP+iu+2/Q== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a25:d6cc:0:b0:671:6e7e:d5d9 with SMTP id n195-20020a25d6cc000000b006716e7ed5d9mr1909023ybg.14.1658821097301; Tue, 26 Jul 2022 00:38:17 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:42 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-10-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 09/17] KVM: arm64: Prepare non-protected nVHE hypervisor stacktrace From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003820_987236_0331B6B7 X-CRM114-Status: GOOD ( 17.31 ) 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 In non-protected nVHE mode (non-pKVM) the host can directly access hypervisor memory; and unwinding of the hypervisor stacktrace is done from EL1 to save on memory for shared buffers. To unwind the hypervisor stack from EL1 the host needs to know the starting point for the unwind and information that will allow it to translate hypervisor stack addresses to the corresponding kernel addresses. This patch sets up this book keeping. It is made use of later in the series. Signed-off-by: Kalesh Singh Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- Changes in v6: - Add Fuad’s Reviewed-by and Tested-by tags Changes in v5: - Use regular comments instead of doc comments, per Fuad arch/arm64/include/asm/kvm_asm.h | 16 +++++++++++ arch/arm64/kvm/hyp/nvhe/stacktrace.c | 41 ++++++++++++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/switch.c | 6 ++++ 3 files changed, 63 insertions(+) diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h index 2e277f2ed671..53035763e48e 100644 --- a/arch/arm64/include/asm/kvm_asm.h +++ b/arch/arm64/include/asm/kvm_asm.h @@ -176,6 +176,22 @@ struct kvm_nvhe_init_params { unsigned long vtcr; }; +/* + * Used by the host in EL1 to dump the nVHE hypervisor backtrace on + * hyp_panic() in non-protected mode. + * + * @stack_base: hyp VA of the hyp_stack base. + * @overflow_stack_base: hyp VA of the hyp_overflow_stack base. + * @fp: hyp FP where the backtrace begins. + * @pc: hyp PC where the backtrace begins. + */ +struct kvm_nvhe_stacktrace_info { + unsigned long stack_base; + unsigned long overflow_stack_base; + unsigned long fp; + unsigned long pc; +}; + /* Translate a kernel address @ptr into its equivalent linear mapping */ #define kvm_ksym_ref(ptr) \ ({ \ diff --git a/arch/arm64/kvm/hyp/nvhe/stacktrace.c b/arch/arm64/kvm/hyp/nvhe/stacktrace.c index a3d5b34e1249..b8a280aa026a 100644 --- a/arch/arm64/kvm/hyp/nvhe/stacktrace.c +++ b/arch/arm64/kvm/hyp/nvhe/stacktrace.c @@ -4,8 +4,49 @@ * * Copyright (C) 2022 Google LLC */ +#include +#include #include #include DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack) __aligned(16); + +DEFINE_PER_CPU(struct kvm_nvhe_stacktrace_info, kvm_stacktrace_info); + +/* + * hyp_prepare_backtrace - Prepare non-protected nVHE backtrace. + * + * @fp : frame pointer at which to start the unwinding. + * @pc : program counter at which to start the unwinding. + * + * Save the information needed by the host to unwind the non-protected + * nVHE hypervisor stack in EL1. + */ +static void hyp_prepare_backtrace(unsigned long fp, unsigned long pc) +{ + struct kvm_nvhe_stacktrace_info *stacktrace_info = this_cpu_ptr(&kvm_stacktrace_info); + struct kvm_nvhe_init_params *params = this_cpu_ptr(&kvm_init_params); + + stacktrace_info->stack_base = (unsigned long)(params->stack_hyp_va - PAGE_SIZE); + stacktrace_info->overflow_stack_base = (unsigned long)this_cpu_ptr(overflow_stack); + stacktrace_info->fp = fp; + stacktrace_info->pc = pc; +} + +/* + * kvm_nvhe_prepare_backtrace - prepare to dump the nVHE backtrace + * + * @fp : frame pointer at which to start the unwinding. + * @pc : program counter at which to start the unwinding. + * + * Saves the information needed by the host to dump the nVHE hypervisor + * backtrace. + */ +void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc) +{ + if (is_protected_kvm_enabled()) + return; + else + hyp_prepare_backtrace(fp, pc); +} diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c index 6db801db8f27..64e13445d0d9 100644 --- a/arch/arm64/kvm/hyp/nvhe/switch.c +++ b/arch/arm64/kvm/hyp/nvhe/switch.c @@ -34,6 +34,8 @@ DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data); DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt); DEFINE_PER_CPU(unsigned long, kvm_hyp_vector); +extern void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc); + static void __activate_traps(struct kvm_vcpu *vcpu) { u64 val; @@ -375,6 +377,10 @@ asmlinkage void __noreturn hyp_panic(void) __sysreg_restore_state_nvhe(host_ctxt); } + /* Prepare to dump kvm nvhe hyp stacktrace */ + kvm_nvhe_prepare_backtrace((unsigned long)__builtin_frame_address(0), + _THIS_IP_); + __hyp_do_panic(host_ctxt, spsr, elr, par); unreachable(); } From patchwork Tue Jul 26 07:37:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928985 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 6B6F1C433EF for ; Tue, 26 Jul 2022 07:43:14 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=DHIGTF/WTNeRwbqY0gOWxpWD5fS0HHjptF3BwWMnHRU=; b=D0HKHvNpS2SAd9pV8Sep4idVO4 qxU58sb78wubdB7EuYCTyyxxc6fdxpzbcIWRo9DDD7AX2pKt9JO6F6kqn3CzXu3EiBkNjW/k8WDuP Jj8X5o4SIqbNw9ebYIzCBnQp7yV4q/jg0hOBpS5tOiAbHrike+Jd16rdcGRyprPGMrVREuqWUbVXb 8vrZ1Db/r3MfoPJ9UzkeIZzGg4rfS11RCShBmMCv+Au7ozFJ7fjJiTlP2FZztiRWtwQ7oM+2cxvle T/lLllpBEqjTjrRYTHOdlHoVFT40wKWbV0AHMO4wRJDzQwg1GGXJUBs6xOU4KULGSTJA/ETxcoRY6 mFrHEQxQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGFCe-009Yf3-Hm; Tue, 26 Jul 2022 07:42:04 +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 1oGF93-009Vny-Dm for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:23 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id s186-20020a255ec3000000b0067162ed1bd3so2184164ybb.8 for ; Tue, 26 Jul 2022 00:38:20 -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:to :cc:content-transfer-encoding; bh=C05IvefY3yHCSicvywO05OO7/p0cJlxJJw9NBtAG/BI=; b=ob0fpBg8on7i2VT4Z634POHg6172v4VbWQBgZgfbUIfl3zaaqjd2K1fDRy9359PVjb 4x4V7vuhKpQ2qxl2xXXTkZqOksr1K6xehND+jYxPnqLLL4sGxuAYqcaGIzzLq/EQSV32 4XNIGl6pZxohnm3t8lNmwRsOcAm+H3FZRMvxLWyeW3uMAZF7pf3Q9GP2Fa1ueJk0x2Ux tpQPP6PzohSzZi9bbBhmNzb8WpHg8N43FnQ4ZwhcEcbQtltUBDAKI3HhFKP5hQT3YEVu PlAzKvYGDWLsYcb3P4TPbRyvpZgZMXxlQitgwIycWRd++/l8WatzmXjknkpPNNNxUIfh JyEg== 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:to:cc:content-transfer-encoding; bh=C05IvefY3yHCSicvywO05OO7/p0cJlxJJw9NBtAG/BI=; b=Mk9orVOSMFGgqjw37hxhlHGfhfy6azEMZNS6XptzjXLothB2mQuQmChCyw70lZmvFz V0dPqUiS89BYBtMmDL+Q5fvH40T8xRYO30yNpbHHZYw3IttcuIrUr4cETywByOxEmJlq SLBr4d9kStmLymEN0TLiLPuAX8E4D/hamM5Y7qFq0OjYxRG3OkFTm2fHPIerSxgjvYGV /u99fzfeUeHP15Dm1dSY9MzzmQAwn9IZnNVc2Di68K3ugZf5ZithdQgXy14SAm8SYqTa z0SVh/QbkXvhxA9XGPveQHa3rZndSh4dleu6nFzNinFb31KSoxzgge8NaI/bEhkF3KGK M+CQ== X-Gm-Message-State: AJIora9ePATAgV9tL644135Dxkoual9uSI0OKTQtd1lQ3aPzLAlE7rCB YitDD7ivSrobCQ3rV0RNrzwcYf55Ufs+0GoHuQ== X-Google-Smtp-Source: AGRyM1s6pLQ/wRx5BpTGjEP9aAkuHSQDACPu3UJRYg3QKP09PzT9y5R0O/ZIhB0UfRXFksJMnzxnUqHKNhS8Z1k5qQ== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a25:50c5:0:b0:670:394a:a2a with SMTP id e188-20020a2550c5000000b00670394a0a2amr12895264ybb.294.1658821099711; Tue, 26 Jul 2022 00:38:19 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:43 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-11-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 10/17] KVM: arm64: Implement non-protected nVHE hyp stack unwinder From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003821_498248_C8F2DDF4 X-CRM114-Status: GOOD ( 16.30 ) 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 Implements the common framework necessary for unwind() to work for non-protected nVHE mode: - on_accessible_stack() - on_overflow_stack() - unwind_next() Non-protected nVHE unwind() is used to unwind and dump the hypervisor stacktrace by the host in EL1 Signed-off-by: Kalesh Singh Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- Changes in v6: - Add Fuad’s Reviewed-by and Tested-by tags Changes in v5: - Use regular comments instead of doc comments, per Fuad arch/arm64/include/asm/stacktrace/common.h | 2 + arch/arm64/include/asm/stacktrace/nvhe.h | 76 +++++++++++++++++++++- arch/arm64/kvm/arm.c | 2 +- 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace/common.h b/arch/arm64/include/asm/stacktrace/common.h index 45474b383630..3ebb69ea374a 100644 --- a/arch/arm64/include/asm/stacktrace/common.h +++ b/arch/arm64/include/asm/stacktrace/common.h @@ -34,6 +34,7 @@ enum stack_type { STACK_TYPE_OVERFLOW, STACK_TYPE_SDEI_NORMAL, STACK_TYPE_SDEI_CRITICAL, + STACK_TYPE_HYP, __NR_STACK_TYPES }; @@ -186,6 +187,7 @@ static inline int unwind_next_common(struct unwind_state *state, * * 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 diff --git a/arch/arm64/include/asm/stacktrace/nvhe.h b/arch/arm64/include/asm/stacktrace/nvhe.h index 1192ae0f80c1..21082fd4a0b7 100644 --- a/arch/arm64/include/asm/stacktrace/nvhe.h +++ b/arch/arm64/include/asm/stacktrace/nvhe.h @@ -16,10 +16,19 @@ #include +static inline bool on_hyp_stack(unsigned long sp, unsigned long size, + struct stack_info *info); + static inline bool on_accessible_stack(const struct task_struct *tsk, unsigned long sp, unsigned long size, struct stack_info *info) { + if (on_accessible_stack_common(tsk, sp, size, info)) + return true; + + if (on_hyp_stack(sp, size, info)) + return true; + return false; } @@ -31,15 +40,78 @@ static inline bool on_accessible_stack(const struct task_struct *tsk, * (by the host in EL1). */ +DECLARE_KVM_NVHE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack); +DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_stacktrace_info, kvm_stacktrace_info); +DECLARE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page); + +/* + * kvm_nvhe_stack_kern_va - Convert KVM nVHE HYP stack addresses to a kernel VAs + * + * The nVHE hypervisor stack is mapped in the flexible 'private' VA range, to + * allow for guard pages below the stack. Consequently, the fixed offset address + * translation macros won't work here. + * + * The kernel VA is calculated as an offset from the kernel VA of the hypervisor + * stack base. + * + * Returns true on success and updates @addr to its corresponding kernel VA; + * otherwise returns false. + */ +static inline bool kvm_nvhe_stack_kern_va(unsigned long *addr, + enum stack_type type) +{ + struct kvm_nvhe_stacktrace_info *stacktrace_info; + unsigned long hyp_base, kern_base, hyp_offset; + + stacktrace_info = this_cpu_ptr_nvhe_sym(kvm_stacktrace_info); + + switch (type) { + case STACK_TYPE_HYP: + kern_base = (unsigned long)*this_cpu_ptr(&kvm_arm_hyp_stack_page); + hyp_base = (unsigned long)stacktrace_info->stack_base; + break; + case STACK_TYPE_OVERFLOW: + kern_base = (unsigned long)this_cpu_ptr_nvhe_sym(overflow_stack); + hyp_base = (unsigned long)stacktrace_info->overflow_stack_base; + break; + default: + return false; + } + + hyp_offset = *addr - hyp_base; + + *addr = kern_base + hyp_offset; + + return true; +} + static inline bool on_overflow_stack(unsigned long sp, unsigned long size, struct stack_info *info) { - return false; + struct kvm_nvhe_stacktrace_info *stacktrace_info + = this_cpu_ptr_nvhe_sym(kvm_stacktrace_info); + unsigned long low = (unsigned long)stacktrace_info->overflow_stack_base; + unsigned long high = low + OVERFLOW_STACK_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_stacktrace_info *stacktrace_info + = this_cpu_ptr_nvhe_sym(kvm_stacktrace_info); + unsigned long low = (unsigned long)stacktrace_info->stack_base; + unsigned long high = low + PAGE_SIZE; + + return on_stack(sp, size, low, high, STACK_TYPE_HYP, info); } static inline int notrace unwind_next(struct unwind_state *state) { - return 0; + struct stack_info info; + + return unwind_next_common(state, &info, kvm_nvhe_stack_kern_va); } NOKPROBE_SYMBOL(unwind_next); diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index a0188144a122..6a64293108c5 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -49,7 +49,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_stack_page); unsigned long kvm_arm_hyp_percpu_base[NR_CPUS]; DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params); From patchwork Tue Jul 26 07:37:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928987 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 4E194C433EF for ; Tue, 26 Jul 2022 07:43:51 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=qm+4MvXxpVk5PyeKhjI9/FORkU5xkqPW37AK/y0sUvA=; b=2Aa7S+lUMBDOXQKY05p4+YfwXn I6qf7oAvxqDS9wQc/fcW7oFKji8zJg/PlKd94f+Vfa2OAJ6aFuRrhTNouODvYleuGLI/f69brN5Kk 7zsyR8jmEjtQwqHP2GpvvjrA5EmoM4PizaaNVn2QQy03OxrxFm8fiVL0BhjeoP35WHPcKg3ErSrXq sIsT8CrMnox12tEcfGaIZotRfSjOqv5jdRvtjh62eE186xQRBh5iO4vjsRTPhpLzP+fBqaNyM4QK+ 7EwNWGb7aIK25PxBQv1BJnvjdiuV8w/Jhxr4UMryr1poOIpZhvajJ508fDVUJFdBDDd8zDi0m262K 5eYrHqcw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGFDG-009Z7z-E9; Tue, 26 Jul 2022 07:42:43 +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 1oGF95-009VpS-TH for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:25 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id f3-20020a5b0d43000000b00671194e39abso5258721ybr.4 for ; Tue, 26 Jul 2022 00:38:23 -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:to :cc; bh=kL7jS2TEgfQABS/KABalQqubKEd+bS4PTw3OsgUmZrg=; b=J9LlcMs4kVl5Jw7qgJTzTVfwP5zRI1SRtnFCZ3KUg847ET+lDBzps4QPRrt7QF7/Ks xKq7LVs7tkEhFsKhBHqbpvefr6T7tgS96BykDlhtcK2A654oU74O77GoXM85LKr2zv7L BvGEAiGlvkxvl7cCjh/9fvLea6FoUmnPKuR/JoLvKMPSe+Tjr4avfVymE3VSL2Rgpy+E KDH/TPvXhSmO5fPs8LEiUE2W6ShIs6W5zYRVei7zZB5A7wh6165l6SX9QLewO0iT+iww 9QLYk9i77CFCRiaAW71AInn6lyy+wNhBpEcxgvjKVY+YfZuk19SXuheN4itHR3CuRIzk rO2g== 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:to:cc; bh=kL7jS2TEgfQABS/KABalQqubKEd+bS4PTw3OsgUmZrg=; b=E7cqXtuGy+5HpQhXkUzS5kapdZ8E7Mzm9rbrnEhpoxxTR0pBNfyNV3PxGrZJBuTJwh eneT2cE5H+OiEkTr98+DrR3gaBj2IqhUvfntS2gEzB7uxQryEToFy/xZsgTOhd/VlYb+ X1GQkADbJVTQb844JaAgq4W66ns54uDxdajcYv6Jg4ccl+5OzZASjoU0R+2yqhPHDjRS gXdqqWdHqf1DLO8QHswDcvihluY1wyhabWQJuiNmEQB+QB4oxj4SbmewUboz4mJdVupg iL0i9mB5OfhoylBxTNgMyp7OLPT0ObHHLMFJW9sDuaxd8UcoI/FBZAGb8RANX709c5Cg Wjyw== X-Gm-Message-State: AJIora9hf4WYX1UasUqLhfbN+qRKRnzth7nIjdhUjAcGZio9hrOyaJTT 4U4mO55WxfKpJmtzAUjqG4hgJzpEFLDP0u+J9w== X-Google-Smtp-Source: AGRyM1saaVsEuZn7ZnRBCM6GpPqB26BLjzUSPJbF2c+S3D5RHjCzUgbqIv/13TpNkFfjwqhEHzjPbXseN9GPJLOPcQ== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a25:264b:0:b0:66e:cc95:50e with SMTP id m72-20020a25264b000000b0066ecc95050emr12499491ybm.173.1658821102498; Tue, 26 Jul 2022 00:38:22 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:44 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-12-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 11/17] KVM: arm64: Introduce hyp_dump_backtrace() From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003824_019116_CD6B4092 X-CRM114-Status: GOOD ( 16.07 ) 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 In non-protected nVHE mode, unwinds and dumps the hypervisor backtrace from EL1. This is possible beacause the host can directly access the hypervisor stack pages in non-protected mode. The nVHE backtrace is dumped on hyp_panic(), before panicking the host. [ 101.498183] kvm [377]: nVHE call trace: [ 101.498363] kvm [377]: [] __kvm_nvhe_hyp_panic+0xac/0xf8 [ 101.499045] kvm [377]: [] __kvm_nvhe_hyp_panic_bad_stack+0x10/0x10 [ 101.499498] kvm [377]: [] __kvm_nvhe_recursive_death+0x24/0x34 . . . [ 101.524929] kvm [377]: [] __kvm_nvhe_recursive_death+0x24/0x34 [ 101.525062] kvm [377]: [] __kvm_nvhe_recursive_death+0x24/0x34 [ 101.525195] kvm [377]: [] __kvm_nvhe___kvm_vcpu_run+0x30/0x40c [ 101.525333] kvm [377]: [] __kvm_nvhe_handle___kvm_vcpu_run+0x30/0x48 [ 101.525468] kvm [377]: [] __kvm_nvhe_handle_trap+0xc4/0x128 [ 101.525602] kvm [377]: [] __kvm_nvhe___host_exit+0x64/0x64 [ 101.525745] kvm [377]: ---[ end nVHE call trace ]--- Signed-off-by: Kalesh Singh --- Changes in v6: - Fix some typos in commit text and comments, per Fuad - Remove kvm_nvhe_print_backtrace_entry(), per Oliver - To make nVHE call trace delimiters consistent between protected and non-protected mode, factor it out into helpers, per Oliver - Change end delimiter to more match that of arm64 stacktrace (---[ end nVHE call trace ]---), per Oliver Changes in v5: - Move code out from nvhe.h header to handle_exit.c, per Marc - Fix stacktrace symbolization when CONFIG_RAMDOMIZE_BASE is enabled, per Fuad - Use regular comments instead of doc comments, per Fuad arch/arm64/include/asm/stacktrace/nvhe.h | 17 ++++++ arch/arm64/kvm/handle_exit.c | 69 ++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/arch/arm64/include/asm/stacktrace/nvhe.h b/arch/arm64/include/asm/stacktrace/nvhe.h index 21082fd4a0b7..170fe7459f7c 100644 --- a/arch/arm64/include/asm/stacktrace/nvhe.h +++ b/arch/arm64/include/asm/stacktrace/nvhe.h @@ -16,6 +16,23 @@ #include +/* + * kvm_nvhe_unwind_init - Start an unwind from the given nVHE HYP fp and pc + * + * @state : unwind_state to initialize + * @fp : frame pointer at which to start the unwinding. + * @pc : program counter at which to start the unwinding. + */ +static inline void kvm_nvhe_unwind_init(struct unwind_state *state, + unsigned long fp, + unsigned long pc) +{ + unwind_init_common(state, NULL); + + state->fp = fp; + state->pc = pc; +} + static inline bool on_hyp_stack(unsigned long sp, unsigned long size, struct stack_info *info); diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index f66c0142b335..e83e6f735100 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 @@ -318,6 +319,71 @@ void handle_exit_early(struct kvm_vcpu *vcpu, int exception_index) kvm_handle_guest_serror(vcpu, kvm_vcpu_get_esr(vcpu)); } +/* + * kvm_nvhe_dump_backtrace_entry - Symbolize and print an nVHE backtrace entry + * + * @arg : the hypervisor offset, used for address translation + * @where : the program counter corresponding to the stack frame + */ +static bool kvm_nvhe_dump_backtrace_entry(void *arg, unsigned long where) +{ + unsigned long va_mask = GENMASK_ULL(vabits_actual - 1, 0); + unsigned long hyp_offset = (unsigned long)arg; + + /* Mask tags and convert to kern addr */ + where = (where & va_mask) + hyp_offset; + kvm_err(" [<%016lx>] %pB\n", where, (void *)(where + kaslr_offset())); + + return true; +} + +static inline void kvm_nvhe_dump_backtrace_start(void) +{ + kvm_err("nVHE call trace:\n"); +} + +static inline void kvm_nvhe_dump_backtrace_end(void) +{ + kvm_err("---[ end nVHE call trace ]---\n"); +} + +/* + * hyp_dump_backtrace - Dump the non-protected nVHE backtrace. + * + * @hyp_offset: hypervisor offset, used for address translation. + * + * The host can directly access HYP stack pages in non-protected + * mode, so the unwinding is done directly from EL1. This removes + * the need for shared buffers between host and hypervisor for + * the stacktrace. + */ +static void hyp_dump_backtrace(unsigned long hyp_offset) +{ + struct kvm_nvhe_stacktrace_info *stacktrace_info; + struct unwind_state state; + + stacktrace_info = this_cpu_ptr_nvhe_sym(kvm_stacktrace_info); + + kvm_nvhe_unwind_init(&state, stacktrace_info->fp, stacktrace_info->pc); + + kvm_nvhe_dump_backtrace_start(); + unwind(&state, kvm_nvhe_dump_backtrace_entry, (void *)hyp_offset); + kvm_nvhe_dump_backtrace_end(); +} + +/* + * kvm_nvhe_dump_backtrace - Dump KVM nVHE hypervisor backtrace. + * + * @hyp_offset: hypervisor offset, used for address translation. + */ +static void kvm_nvhe_dump_backtrace(unsigned long hyp_offset) +{ + if (is_protected_kvm_enabled()) + return; + else + hyp_dump_backtrace(hyp_offset); +} + void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr, u64 elr_virt, u64 elr_phys, u64 par, uintptr_t vcpu, @@ -353,6 +419,9 @@ void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr, (void *)panic_addr); } + /* Dump the nVHE hypervisor backtrace */ + kvm_nvhe_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 From patchwork Tue Jul 26 07:37:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928988 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 DA49BC43334 for ; Tue, 26 Jul 2022 07:44:33 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=rC/A/CSHxH1a6yHvKW8PsHTFqZ1nkfGRWwv2Hfgiovk=; b=T3vjt7TnidDMRgAHT30db3VaBK 2W0g45nMbYg1Pe4wE4TkcpfMm1ZhJ0AiC/sx8F2MCAGxXPgEYGLDHTKkE7CDJFGiCjG1TxOC4YyeZ G01XowqGXanpYm/ipKwzuNO32mphaFsO6baRMzS3NrMZzEZJ7H/Za3yF4m1Z+Rzop1c482nN8Y4+T LXlkAyfXmKmKqK5fzWlQC+hUO1kN8MqH76bPTOmvtboyEQM3QQDdxz1GBDHDpc4K294stvT49wROC bY7R1PemD3+3ihTgJ+UA2gEC/g1db9BiUjp71c6g1K3QRjX97zP1dFLu6aD6UE1dy7qGaE0rkBLs9 SbNgRDyw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGFDv-009Zhs-QM; Tue, 26 Jul 2022 07:43:25 +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 1oGF98-009Vi6-AF for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:27 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id b18-20020a25bb52000000b0067128e66131so4355954ybk.18 for ; Tue, 26 Jul 2022 00:38:25 -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:to :cc:content-transfer-encoding; bh=9z1geK001Lgo4aQzTlFwh7+mdMHid79qtYIr9n0hiZw=; b=akqv8KPpQa1VNcDlRwBAf0WpbIAk4vEtYIJMvicQmBFeAaugRk9kLN6l+vafdsELTk NtqHPyYUML5CJ4AjDiY3dTaY+cSkOpg128D4ZkUQUG9Fz38Uro8ofjsPN0WzDtIet4WQ SZYEv+uBqfLl3qepTZ0OG6h7bYpZ3eDokb5EaIFJXHoiXBj3RK1eGwGvMWUb7qW6iO7g nyY0uYsGfoAPSXbog85aTEium1GWJ1Xhh3lcJ8Yp8ebpVrv98qa30n06S4cvxkTePaJB VUVCyyYygnDOUIC/8x/7m9q/JVzqjGe8IUGeLpvtVWbUFSdClA40lQjVHGIkuc85K3fB gYVw== 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:to:cc:content-transfer-encoding; bh=9z1geK001Lgo4aQzTlFwh7+mdMHid79qtYIr9n0hiZw=; b=HDHkx6QYOzPFHjRpNamkbnAfT549daCpMcWWagqReHLmQsoGLWEzvH0VKLa3ObNJYy 6rhdQIWV7PgiT6Hc2PcehSIxsX6kHrapmUFy5L4MgP0tt0CubKdSWkRezWjmTMzIdqab zx2MI1U80yoDrqQjLnEm6OeD0CZw4uyVUUA01uqql2MHQaLWOOd3eafAfG2P39XLIgdS HAkhMdDRYpbCnLUIT/h0Y2EBQpatijU71BHnFIUadWnyiiLErVzS1XJljhYML6mVU1p8 fSXFLt3vNbajJggsXyCokELOuWdsBYXMu43STohtTWK4TNFRfOkugi8TB1OmsLwdhE0d RBnw== X-Gm-Message-State: AJIora8zVlg89t8aFcxwY5072xH9tGRkwg/XVUzQa7W44sys4tUSkW0F udrXfBXUHyzgLHXUpL8CzsBaLTsSPXTOsh/tAw== X-Google-Smtp-Source: AGRyM1u5wMrg3Dj62VDc19qLELQ21pNgDjr7RG8JmDND2ovfsQwBtAh8lTkmGF1a/0WVMU+4RWrNFRXG8A0elKLu0w== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a25:ccca:0:b0:66e:c109:a884 with SMTP id l193-20020a25ccca000000b0066ec109a884mr12597907ybf.161.1658821105018; Tue, 26 Jul 2022 00:38:25 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:45 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-13-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 12/17] KVM: arm64: Add PROTECTED_NVHE_STACKTRACE Kconfig From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003826_391269_F996874B X-CRM114-Status: GOOD ( 10.50 ) 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 This can be used to disable stacktrace for the protected KVM nVHE hypervisor, in order to save on the associated memory usage. This option is disabled by default, since protected KVM is not widely used on platforms other than Android currently. Signed-off-by: Kalesh Singh Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- Changes in v6: - Add Fuad’s Reviewed-by and Tested-by tags Changes in v5: - Make PROTECTED_NVHE_STACKTRACE depend on NVHE_EL2_DEBUG, per Marc arch/arm64/kvm/Kconfig | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 8a5fbbf084df..09c995869916 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -46,6 +46,21 @@ menuconfig KVM If unsure, say N. +config PROTECTED_NVHE_STACKTRACE + bool "Protected KVM hypervisor stacktraces" + depends on NVHE_EL2_DEBUG + default n + help + Say Y here to enable pKVM hypervisor stacktraces on hyp_panic() + + If you are not using protected nVHE (pKVM), say N. + + If using protected nVHE mode, but cannot afford the associated + memory cost (less than 0.75 page per CPU) of pKVM stacktraces, + say N. + + If unsure, say N. + config NVHE_EL2_DEBUG bool "Debug mode for non-VHE EL2 object" depends on KVM From patchwork Tue Jul 26 07:37:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928989 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 E263EC43334 for ; Tue, 26 Jul 2022 07:45:20 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=l231WRekC12MPud9D7eZzmhGv6ug1fV5i3kzf1BzyOM=; b=lsE1kdoI4ssZtR2RXf/OQqB8eL Z4toGMtCS6wVq0t6YDqQpnYHli/suJBU8Rfx4pfgC/6oo+wIA0ZBuECgnDoecoEd0psKh7G9G9p4W AhjDPo3wN6T1moF2rHyz40c1WIYeDVyiinrpSwK39ZuhNZx36z3GoxVQ7+CBK5U0XIjQxOU87Bc+j BMWo7fHMtK0hOClW+Thllyav6coLaZkw3x6uBTLIA/3gSy/8o3L1jpbJQk9Rc7WkIhqjFmBT/4nn+ 5OIoTAptbD0w9BJWcCrw7t9/e1gXcSP7ezHzPDuu4oBoo+WyUWbSPf4V5CG88pgB1PVMU6izv2QYE cfCV0R6A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGFEd-009aEl-RQ; Tue, 26 Jul 2022 07:44:08 +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 1oGF9A-009Vt1-GV for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:29 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id b18-20020a25bb52000000b0067128e66131so4356007ybk.18 for ; Tue, 26 Jul 2022 00:38:27 -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:to :cc:content-transfer-encoding; bh=Yr2SeyZfI0iUSRRhOZVPZMhkgZCPOBYcwpq2LIzTDZU=; b=ld594KmaC+x5hDg3TfDCFLBo3mJAZfZadHtvCv4LxhROGpYJjZU5u3nKSiZtJGEeDg IIuDeTXXeGCm/m63A/vVxNjaLgs/Y/a/sPcp5QPfaHOHooP7/BenwvBsnpWGgpYKdke2 NSEaG4+gALZAkhG6CrQW2qi5i3MnwMxygSzrgKO+y48x97DWG5b6cKMHnWq5gI3k7I8o aF84buGLeVeeiDm2loKcvHAEv/qJorx273cBmBrFBtvuFayq67ts1t/eZYcPdJiP1OrJ 37UDImjN43qQLenhSw1pgI8Q7IZ/5+VyJ8X+qvF10PD0zs4JSX97+y7BUrreFoc3hEaK +Rtw== 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:to:cc:content-transfer-encoding; bh=Yr2SeyZfI0iUSRRhOZVPZMhkgZCPOBYcwpq2LIzTDZU=; b=Be0uogZ9WmrnRJbZxL+H23PvgOZGOA+2vaTti2+Gv8zPPj2ay7iXKT8seWh5DYiQc9 0BXIhMcHe06NtnlkfTwx4YeQui8eoodMdSpaEUHEy36zjMt7ceVtW/PSp3hvHkAnj0v0 Gp4UeQ7p+cqZ12bz+eALdJJsvpaf0RhhV5yv9Sz/lFk8JaWHiss+VWoqcMoEbp9X0WJv tNYt3mbsPBznQNnyRkMgspgxtkBEb2+GiZL9oYCbcLCcoxz5bTAn9/A79Mfu/YpMGLF+ Yp6Q6jEvm2/o2FEfX+t2l3wn+IXBf2Zh6raSJYSMWXEHCvjXl/+UTecpM89vZkkfgjRD ElyA== X-Gm-Message-State: AJIora9IZayXAdsiVm3d9/ks1VTjBITfOI4aLzM4dCoNWXoJaOxsH14K TZnZGSrWDeWZIfp5127wJFo57v6HivTYJXT4tg== X-Google-Smtp-Source: AGRyM1uUV8LVmqLvK9zIG/LZdwQHtzL5gI4YShW6qDDsHeG4grPXlFeu6lQVvyPm5rCyBP47PIGIWuZltzrJGPwwYA== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a81:911:0:b0:31f:4906:80a2 with SMTP id 17-20020a810911000000b0031f490680a2mr1032907ywj.351.1658821107235; Tue, 26 Jul 2022 00:38:27 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:46 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-14-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 13/17] KVM: arm64: Allocate shared pKVM hyp stacktrace buffers From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003828_571791_EB75EA2A X-CRM114-Status: GOOD ( 14.59 ) 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 In protected nVHE mode the host cannot directly access hypervisor memory, so we will dump the hypervisor stacktrace to a shared buffer with the host. The minimum size for the buffer required, assuming the min frame size of [x29, x30] (2 * sizeof(long)), is half the combined size of the hypervisor and overflow stacks plus an additional entry to delimit the end of the stacktrace. The stacktrace buffers are used later in the series to dump the nVHE hypervisor stacktrace when using protected-mode. Signed-off-by: Kalesh Singh Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- Changes in v6: - Fix typo in commit text, per Fuad - Add Fuad’s Reviewed-by and Tested-by tags Changes in v5: - Fix typo in commit text, per Marc arch/arm64/include/asm/memory.h | 8 ++++++++ arch/arm64/kvm/hyp/nvhe/stacktrace.c | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h index 0af70d9abede..cab80a9a4086 100644 --- a/arch/arm64/include/asm/memory.h +++ b/arch/arm64/include/asm/memory.h @@ -113,6 +113,14 @@ #define OVERFLOW_STACK_SIZE SZ_4K +/* + * With the minimum frame size of [x29, x30], exactly half the combined + * sizes of the hyp and overflow stacks is the maximum size needed to + * save the unwinded stacktrace; plus an additional entry to delimit the + * end. + */ +#define NVHE_STACKTRACE_SIZE ((OVERFLOW_STACK_SIZE + PAGE_SIZE) / 2 + sizeof(long)) + /* * Alignment of kernel segments (e.g. .text, .data). * diff --git a/arch/arm64/kvm/hyp/nvhe/stacktrace.c b/arch/arm64/kvm/hyp/nvhe/stacktrace.c index b8a280aa026a..e2edda92a108 100644 --- a/arch/arm64/kvm/hyp/nvhe/stacktrace.c +++ b/arch/arm64/kvm/hyp/nvhe/stacktrace.c @@ -34,6 +34,10 @@ static void hyp_prepare_backtrace(unsigned long fp, unsigned long pc) stacktrace_info->pc = pc; } +#ifdef CONFIG_PROTECTED_NVHE_STACKTRACE +DEFINE_PER_CPU(unsigned long [NVHE_STACKTRACE_SIZE/sizeof(long)], pkvm_stacktrace); +#endif /* CONFIG_PROTECTED_NVHE_STACKTRACE */ + /* * kvm_nvhe_prepare_backtrace - prepare to dump the nVHE backtrace * From patchwork Tue Jul 26 07:37:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928990 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 6A2D2C433EF for ; Tue, 26 Jul 2022 07:46:06 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=7gi64s6Z9GFi4ZxNyv0tjQ+B+tfeQJ07VL3a0NJ1uoo=; b=QTOD/9U00COWFsIWFRKgNrc0/i meOkZQzPUIbg1BZ+zczAvsAlLEZ1QQFeqNK+F0keWr5rq2uEYEOKIRJ8gn0GjwnOfpXJg2Td2+YTy Td+XUngTWaBuuD+gakih6e34bprwZM/C9j8QfROZ+TyoLiV9iKBywOrumNpUdcww39QXe0Y5kOyI/ 1/FgmxakRmLGjDbZDwQPGlIn4sE84b2vEM/iC6jq52Nmf4gbP4P35Zn4KKKruHnl/Tp4+RUvyQzEb MkXH8HVd6D2QJt0nt0tr6I4vDZpwTC8XTMhoDAmF7msOzu4bqmEhEa6lhNHsBB/Jo1XXWmceymQ4u i8UUavKQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGFFQ-009anp-Ik; Tue, 26 Jul 2022 07:44: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 1oGF9D-009Vux-0b for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:32 +0000 Received: by mail-yb1-xb49.google.com with SMTP id m5-20020a2598c5000000b0066faab590c5so10337353ybo.7 for ; Tue, 26 Jul 2022 00:38:30 -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:to :cc:content-transfer-encoding; bh=xrCZ3JDmoRPruDDfTE2MIoljy3aPp6Mt1Jsyf41HvKw=; b=m6IsAPXeraH4sveiwup4g2zagtiCTkNBAzqv8fgM0IoJl7hrB2JzWnIspy1lIY7osA NzAq3F6sO2d8uqWMdVFrS63sBkmLaYviVNj7S5bFoNxZW4aM3kSxz16b2fsJs1wHVodj XYTsyXv2EdLxUTOBL2TRNlpw2hv5izPXRxd/Z8NCB05ZO1KoHvjIeAKzQ/wCMrPfIjkF CwDZjSNE4xTOp8O6+44f6VUzoPj0PPtSHzUMylIR6Z8gIUsJG12CccrP3jvNWY5L6ovS GpZRrij++WgXDPzUdF+AuJ2mJkEU3GESYnoDG1tN06qjgOizHU6Q+SpIz3jEcMDcit9j EyAQ== 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:to:cc:content-transfer-encoding; bh=xrCZ3JDmoRPruDDfTE2MIoljy3aPp6Mt1Jsyf41HvKw=; b=HraOPyBuyApTkh8Yc3+0WZJr23jwKgk0xnDUx43ZedVGI6zckNbPf32+zT25ejhx0Z 5wlY8lGjz7f5yxyE+hw7Av81rOt0xf8bzhpFjNBc84NuJN3VeoyNyNwgmgiltSFEenmT nOzCAz0S2AUsgR5mt/aq3KUAgvuPujFr3mqCE2Wcly2sAa11wh7tNk1rQB7wEs7p24NZ xWGZBa4xNejmPyCuzaOE5/r6DPJ2+QJ8YoU8v2pTA5OrUEg051x5vmJvAKOhy36dyZ82 d51aG0y+3qJZ/dtFHRaYa/r5IpEGiqQUBT3pssLZv7p9O5aYvEB+q9e1AeAk/0OjAS6N GqXA== X-Gm-Message-State: AJIora9IGBhINMGh09MmyK1V3GSoQ93rriQy4Mt/Y3RU0FRk/Yle/Y/E S5a0n905XzAvXz+IykFrG0EuDfNXLMaWQxjU2g== X-Google-Smtp-Source: AGRyM1tNxOOKtnk6pZWo0eI8iYyyinuUNbf8mMV5eXM2bUjJGrThxUTbi7FD+57/XnORNw3Rkfp420sRl8CYsBg42g== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a05:6902:1542:b0:66e:e116:95b8 with SMTP id r2-20020a056902154200b0066ee11695b8mr12799119ybu.218.1658821109572; Tue, 26 Jul 2022 00:38:29 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:47 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-15-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 14/17] KVM: arm64: Stub implementation of pKVM HYP stack unwinder From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003831_084191_0B6C01A6 X-CRM114-Status: GOOD ( 13.20 ) 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 Add some stub implementations of protected nVHE stack unwinder, for building. These are implemented later in this series. Signed-off-by: Kalesh Singh Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- Changes in v6: - Add Fuad’s Reviewed-by and Tested-by tags Changes in v5: - Mark unwind_next() as inline, per Marc arch/arm64/include/asm/stacktrace/nvhe.h | 35 ++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace/nvhe.h b/arch/arm64/include/asm/stacktrace/nvhe.h index 170fe7459f7c..2ce59c058806 100644 --- a/arch/arm64/include/asm/stacktrace/nvhe.h +++ b/arch/arm64/include/asm/stacktrace/nvhe.h @@ -9,6 +9,10 @@ * to allocate shared buffers for the host to read the unwinded * stacktrace. * + * 2) pKVM (protected nVHE) mode - the host cannot directly access + * the HYP memory. The stack is unwinded in EL2 and dumped to a shared + * buffer where the host can read and print the stacktrace. + * * Copyright (C) 2022 Google LLC */ #ifndef __ASM_STACKTRACE_NVHE_H @@ -49,7 +53,34 @@ static inline bool on_accessible_stack(const struct task_struct *tsk, return false; } -#ifndef __KVM_NVHE_HYPERVISOR__ +#ifdef __KVM_NVHE_HYPERVISOR__ +/* + * Protected nVHE HYP stack unwinder + * + * In protected mode, the unwinding is done by the hypervisor in EL2. + */ + +#ifdef CONFIG_PROTECTED_NVHE_STACKTRACE +static inline bool on_overflow_stack(unsigned long sp, unsigned long size, + struct stack_info *info) +{ + return false; +} + +static inline bool on_hyp_stack(unsigned long sp, unsigned long size, + struct stack_info *info) +{ + return false; +} + +static inline int notrace unwind_next(struct unwind_state *state) +{ + return 0; +} +NOKPROBE_SYMBOL(unwind_next); +#endif /* CONFIG_PROTECTED_NVHE_STACKTRACE */ + +#else /* !__KVM_NVHE_HYPERVISOR__ */ /* * Conventional (non-protected) nVHE HYP stack unwinder * @@ -132,5 +163,5 @@ static inline int notrace unwind_next(struct unwind_state *state) } NOKPROBE_SYMBOL(unwind_next); -#endif /* !__KVM_NVHE_HYPERVISOR__ */ +#endif /* __KVM_NVHE_HYPERVISOR__ */ #endif /* __ASM_STACKTRACE_NVHE_H */ From patchwork Tue Jul 26 07:37:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928991 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 DE75FC43334 for ; Tue, 26 Jul 2022 07:46: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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=rw/DVMpYBTWoz623uN2K1ZiENgQzv4D+lzsLUIaag3Q=; b=fe9QcQz2sbjzPc6VE1UXLdtKyF CewtW5svlJDFWU4RWTgmPHtK6mx/6kNuh9d0iOPr+l6bXSeV+7j+5f3iwE/O43+KxiLGVdkE1Js6A VS013UT28U2EUVv/nMJmcWzClTgb7SdrLT/YXZ7ulFT2yXvYfRtN5PNuuYcHZ6F0+kgshbWd0k8v8 3OVHOZ42f5ZLkYhnWd6b0Xi9UrNKD2IYUcYUxZg32FQpmNFHoRUhxvWzNEtmMBoB/HHAOmgd4UDK3 JHV9edew8SYVQmaceG7zWXWQRW3p2GX7Xpl8h9zP7n0sDIOhP7bGqugu+1LFzYfV0hrGIcQ9SXffH +5+k6WXA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGFG9-009bKo-L3; Tue, 26 Jul 2022 07:45:43 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGF9F-009VwP-AN for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:34 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-31e9b1be83eso87098147b3.8 for ; Tue, 26 Jul 2022 00:38:32 -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:to :cc; bh=xzH/6M+SU0X5hOX4FZRvYLC0KzgDSzoTGm2ET8Rq+o0=; b=DETaP7rF9wWbO6Msex1IjbIPOs5qcFopnknde2KDuKfQtGfBHQQZeSttvtn1M31WnG YP9SmoTcjSiBVkQMa36IbuQEGimRV0R+nOmXct3SY4oULsKy7C54mnSHLgxbuyhdBcn5 kc9VIPOtYstVwH+wHBNdY1bHzvfVqak5AfqO5t5aHq6xwBNPcc5ZWHhSUSXM+E5dhfkf Ig+GVFWSBaY3UZVU8Po82d30sbS/TuoWAuUoCDKCXCNT+IUA8CjzhJO0oTyEwNNyc6RG MBN6CwskgCB1Yhqk5Ni52rnn47r/JS90lh+HxJt3TannLGJVTmQaK1SXj7Iua9xSEMdz Suzg== 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:to:cc; bh=xzH/6M+SU0X5hOX4FZRvYLC0KzgDSzoTGm2ET8Rq+o0=; b=VOuyJR/Ih6bvxH9yclo2HtQTCRfkIzYub1zB6hNKis7Me7YPIV1rHtYkon9pJ7iB9K SlauJO+L9WIfJkt56n3b/gZ0DD5G0yX/QBhpLE/ACBG5p3s5PJcaYQlq8K1rd0+dcLIS L8zBOd1OKU5k6SyVBtgDsNSEJmDheKOgIc88J4BPN52KU0WUd/2NcV0Xwlu/3/9Xi3mR w0aHDvq1GAbRr1f7FoSfCSN4pXCf3g5+ylqycDzp0KSN4zHzojiQer2mxNZ5xnF3sT3D rDfMkz8h5p/ipWB2Niw/IvsdsUskFUcvX5JalJ7B4AZfTTT/8hlwlanmlUvqAkrt5NTc Q7iA== X-Gm-Message-State: AJIora/N5v9afQD2hpXlOg9GZ4L1C2h8KZqPKJ5PtT2TrZJpfg2X5h5X C7XYDEpcbWGcNRI4YOojS4FINr0lNhAuhpQyOw== X-Google-Smtp-Source: AGRyM1s9MRRJnFJpaGAtK6mRh1YvUHLp08b9Vp1bPDczimM0hZ7elFoj8DoT6FWrVuc+bm+K4KVS8zylnMHhGD9GeQ== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a5b:f41:0:b0:671:6ce6:f71 with SMTP id y1-20020a5b0f41000000b006716ce60f71mr2391473ybr.506.1658821112045; Tue, 26 Jul 2022 00:38:32 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:48 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-16-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 15/17] KVM: arm64: Save protected-nVHE (pKVM) hyp stacktrace From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003833_420163_87047B33 X-CRM114-Status: GOOD ( 18.68 ) 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 In protected nVHE mode, the host cannot access private owned hypervisor memory. Also the hypervisor aims to remains simple to reduce the attack surface and does not provide any printk support. For the above reasons, the approach taken to provide hypervisor stacktraces in protected mode is: 1) Unwind and save the hyp stack addresses in EL2 to a shared buffer with the host (done in this patch). 2) Delegate the dumping and symbolization of the addresses to the host in EL1 (later patch in the series). On hyp_panic(), the hypervisor prepares the stacktrace before returning to the host. Signed-off-by: Kalesh Singh --- Changes in v6: - Simplify pkvm_save_backtrace_entry() using array semantics instead of the pointer arithmetic, per Oliver. Changes in v5: - Comment/clarify pkvm_save_backtrace_entry(), per Fuad - kvm_nvhe_unwind_init(), doesn't need to be always inline, make it inline instead to avoid linking issues, per Marc - Use regular comments instead of doc comments, per Fuad arch/arm64/kvm/hyp/nvhe/stacktrace.c | 55 +++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/nvhe/stacktrace.c b/arch/arm64/kvm/hyp/nvhe/stacktrace.c index e2edda92a108..900324b7a08f 100644 --- a/arch/arm64/kvm/hyp/nvhe/stacktrace.c +++ b/arch/arm64/kvm/hyp/nvhe/stacktrace.c @@ -35,7 +35,60 @@ static void hyp_prepare_backtrace(unsigned long fp, unsigned long pc) } #ifdef CONFIG_PROTECTED_NVHE_STACKTRACE +#include + DEFINE_PER_CPU(unsigned long [NVHE_STACKTRACE_SIZE/sizeof(long)], pkvm_stacktrace); + +/* + * pkvm_save_backtrace_entry - Saves a protected nVHE HYP stacktrace entry + * + * @arg : index of the entry in the stacktrace buffer + * @where : the program counter corresponding to the stack frame + * + * Save the return address of a stack frame to the shared stacktrace buffer. + * The host can access this shared buffer from EL1 to dump the backtrace. + */ +static bool pkvm_save_backtrace_entry(void *arg, unsigned long where) +{ + unsigned long *stacktrace = this_cpu_ptr(pkvm_stacktrace); + int size = NVHE_STACKTRACE_SIZE / sizeof(long); + int *idx = (int *)arg; + + /* + * Need 2 free slots: 1 for current entry and 1 for the + * delimiter. + */ + if (*idx > size - 2) + return false; + + stacktrace[*idx] = where; + stacktrace[++*idx] = 0UL; + + return true; +} + +/* + * pkvm_save_backtrace - Saves the protected nVHE HYP stacktrace + * + * @fp : frame pointer at which to start the unwinding. + * @pc : program counter at which to start the unwinding. + * + * Save the unwinded stack addresses to the shared stacktrace buffer. + * The host can access this shared buffer from EL1 to dump the backtrace. + */ +static void pkvm_save_backtrace(unsigned long fp, unsigned long pc) +{ + struct unwind_state state; + int idx = 0; + + kvm_nvhe_unwind_init(&state, fp, pc); + + unwind(&state, pkvm_save_backtrace_entry, &idx); +} +#else /* !CONFIG_PROTECTED_NVHE_STACKTRACE */ +static void pkvm_save_backtrace(unsigned long fp, unsigned long pc) +{ +} #endif /* CONFIG_PROTECTED_NVHE_STACKTRACE */ /* @@ -50,7 +103,7 @@ DEFINE_PER_CPU(unsigned long [NVHE_STACKTRACE_SIZE/sizeof(long)], pkvm_stacktrac void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc) { if (is_protected_kvm_enabled()) - return; + pkvm_save_backtrace(fp, pc); else hyp_prepare_backtrace(fp, pc); } From patchwork Tue Jul 26 07:37:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928992 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 3ED8CC433EF for ; Tue, 26 Jul 2022 07:47: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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=uD7WDHUoa7mQMr7UbAmJqYJo7i4JiqNaNpGefUWdmBU=; b=R1JM7js8XkUtbBvj5empAc0rca kkrHUo011hjauorF0zhbbjIEFdCTy8bNv80J8SJyIz/TevF6A4O7vHYp4MMHB5ttidq2sm/zTKH/O pUBrnjqhS9SHhgV3olYHL95oVF/2BgglRq9xr/2Yr8JB8KpnhP5CUeI+l7sRbKW0srLD5xHVQs7C+ +g/+GiWVNgcSiZGlPhKmtrwB+sTCbhEuYZgsX5CPLEYO6v7WJxqbmOZTl11lreez3DXNXl3O5OFaE hTh9D60YBnudHkrs95MZhkC4lYQbOkgkwnJNyVU+2wQ+JGS3vOUKD9St1AxlJwGEeiMYzl0weLygs THtbUebQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGFHC-009bx2-3I; Tue, 26 Jul 2022 07:46:47 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGF9H-009Vxy-RR for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:37 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-31cb93cadf2so105179947b3.11 for ; Tue, 26 Jul 2022 00:38:35 -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:to :cc:content-transfer-encoding; bh=4z4Kz5jKP4bI+UOP9DxU3eg2/YP+oU3Smm0rFqPcsng=; b=Z1P1ClfItoh79aQ1b/7uN6+YG8sXdaRGbHmm2Km3lpRCBNERxYfC45wuUnV22xDrKr C7P7sOnzNuqntBLF3F+6jM28iGWBmjuZh/onAiogWA6nkl+pQMh/hkru3m8yOoDZtCiz YUeV/q7yT+eI6gJO0z3Jk1qzCMVZ8PnFyCNSgqN/dWdmju6ronRybvKXvcqMM82Etntz YLHXZQ84XUWvCivtFflxyQq6fJhEpglvw3cFqVdjfvpGM1UsagOmmxxMaKcMmpN6dr59 S8axA/CrLXVHIAUBmzb2I9dpWg+mkbpuBU7UpOLEQXfqv2pHnmn4tY3D3DSea9NPICrl o61w== 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:to:cc:content-transfer-encoding; bh=4z4Kz5jKP4bI+UOP9DxU3eg2/YP+oU3Smm0rFqPcsng=; b=NumdVWZkraK2zquwgnAlvMn5nUHfZ1gLthkBINdMGLi5ppeH49ILxxv1Z0CUhrcSPH 0H5+SfLMa7ZJwJ1Kkn3YtTAQg1B1jJLm6cchirdtHXNLzvc0+7WxMp6UFK4kdkNjkNXw o3hfHPdPD+PvfgaTAOy5OIUs3AYJlN9650Esm341QLN3KOjWIuW5folvu49mIm3Y+UY4 FiUmJH/h1dcoXquJBXVrafIZOqHY9pqbApeCtINYDxU+LMlp3tvUuEzTkibHb2Gy5zWt BPcA+HSxUJhtfJE6TLJKrE6dy0U1Vl2WXIrwVtrRNOb7p5Xkx/07X47G+A2zTEbbTB4D 2F/g== X-Gm-Message-State: AJIora+7TtzKIipJUwf9VRoE7BB98eDaEbaUKsx5RbPLbaYFRXA8hSrx LpOFHS0flSCpsUQv6BvNIX2OA0j9urFFaRqN+g== X-Google-Smtp-Source: AGRyM1s+nZYFqneaUWZhKYAd3zQF7L0Cwai3ur8Rv5PlGmwq+xCSduNXnxJSHkCKOQUTBjw9Sv3BBmCbw3HEDJGUpA== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a81:1e49:0:b0:31e:5b13:a187 with SMTP id e70-20020a811e49000000b0031e5b13a187mr13741284ywe.129.1658821114535; Tue, 26 Jul 2022 00:38:34 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:49 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-17-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 16/17] KVM: arm64: Implement protected nVHE hyp stack unwinder From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003835_906197_01926E40 X-CRM114-Status: GOOD ( 12.32 ) 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 Implements the common framework necessary for unwind() to work in the protected nVHE context: - on_accessible_stack() - on_overflow_stack() - unwind_next() Protected nVHE unwind() is used to unwind and save the hyp stack addresses to the shared stacktrace buffer. The host reads the entries in this buffer, symbolizes and dumps the stacktrace (later patch in the series). Signed-off-by: Kalesh Singh Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- Changes in v6: - Add Fuad’s Reviewed-by and Tested-by tags arch/arm64/include/asm/stacktrace/nvhe.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace/nvhe.h b/arch/arm64/include/asm/stacktrace/nvhe.h index 2ce59c058806..600dbc2220b6 100644 --- a/arch/arm64/include/asm/stacktrace/nvhe.h +++ b/arch/arm64/include/asm/stacktrace/nvhe.h @@ -64,18 +64,27 @@ static inline bool on_accessible_stack(const struct task_struct *tsk, static inline bool on_overflow_stack(unsigned long sp, unsigned long size, struct stack_info *info) { - return false; + unsigned long low = (unsigned long)this_cpu_ptr(overflow_stack); + unsigned long high = low + OVERFLOW_STACK_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) { - return false; + 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); } static inline int notrace unwind_next(struct unwind_state *state) { - return 0; + struct stack_info info; + + return unwind_next_common(state, &info, NULL); } NOKPROBE_SYMBOL(unwind_next); #endif /* CONFIG_PROTECTED_NVHE_STACKTRACE */ From patchwork Tue Jul 26 07:37:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kalesh Singh X-Patchwork-Id: 12928995 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 F32AAC433EF for ; Tue, 26 Jul 2022 07:49:13 +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:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=NfKozxRL5UIrpGi4mp4B62wjDHB2dZujeiOUwjTjKzM=; b=qXP2lz38qFv4tVzFX10svR2TRk 73vttXR3B+QDZwrwKulEPWUwVkqk/iAJlRrUGTmO8lkKTjHW9dpjgldguShmr33N74f30qke48pVc RI0xGcFTOENREEWsAEIX4S6d7CJadwwPINJeSpMxbmWGglzU2G9cmXDlJlWuycteIFiOa23sxbgkz L3hHnu6d1sZ6R9XoGvHJf0lYu7tFpIu/r6/2DpTT5mV515MdQwryIxOwLVIZMPtBNQTG0gWjz2L9T Qs4wsE2Y1UBRdOs2Tq6kySBrC9wIVQMN7r0VkFMLN5Tq49dPom/9PDYzHikVSbEGv2IWkvkE1QGHV ZEPEMcJw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oGFIF-009cdv-28; Tue, 26 Jul 2022 07:47:51 +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 1oGF9K-009Vn5-2e for linux-arm-kernel@lists.infradead.org; Tue, 26 Jul 2022 07:38:39 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-31e89d6bea7so101355087b3.10 for ; Tue, 26 Jul 2022 00:38: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:to :cc; bh=jqHv3wNh5seyZnCSMu1rVV5xcStpMLXUKBtcU3Y1j6g=; b=ocN5AR1zWFMJEdlfQYlUgBVvqdAFbwAuvDwmF8/WQF7flrQZy/d2lABpWZ90I1DGwj uBDOTwVB3d0mowFCuMBvFItzze3IgDYvVeY7MF+0YCht4BAY9N6Rvg3LgAdPwNAO7Zqa UWsoXpemhN+pJ+qvZZlKIlMkVeXNiyHkkrkksywHf4jDn9qkfULtI3MYl337w8Z1pIi0 hGesxH4lpG5OYmornCWfc/StZhbhdxa0VAvVvGMJ8tJ7mP8FUQKCMgedSz5c27wL4aN1 Y/7E4OfVr6mEd/aTfmMEtKFSKRG5k/Q6b/oS8yrnS7U3v1pA8SbmNiuNjfXvoeXZ3KFi sMjw== 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:to:cc; bh=jqHv3wNh5seyZnCSMu1rVV5xcStpMLXUKBtcU3Y1j6g=; b=DjUlv9s6tEIoyuoqJcd73GCrDHYL7GvVY9jknyEHkMwAo99cGDJAuE1VSeUpyCvPlv weULkViitv4xq1A/PnhRwD8tkRx29KeQyZb1+KEpkHdLqhb2UQ9cwJcNjB7mgQPYBeSq cG7AQWRUi4RiInsS0bwmMT3ndn9rjScuF8CB7AzAIwpK40L+di6VyGIO86QJ6YiLVVIb 357XQnOvNAK0phjR8JE2kIBFLYdSzHloFih2rwxaU7VLYcK2Di3tECe70mX5EKkfVagv Ez7noFJ3OYXuRqZw6GLFqyfMvs84GpNwbD02rPPXV5PC9zLkLD8fROD6NdVDG6/8EF4O A0Eg== X-Gm-Message-State: AJIora/Thr6FXYm8cqNo+QM142GNkfM7w7qfRB+eXpmpIeuREcDAya1S 28UI8xr/Wfd2nQAFIr00UTlCGMgz1ic64mSgKg== X-Google-Smtp-Source: AGRyM1topRzJUtRGNFO9xj3XVL9vGf78Qw66LXXmx8qTxzqFbDxaG+0vvCO+D0Dnu6p0fFCleQQnl3iTvjkdyPhgxw== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:4f77:3b64:736a:394e]) (user=kaleshsingh job=sendgmr) by 2002:a25:640a:0:b0:671:3386:f860 with SMTP id y10-20020a25640a000000b006713386f860mr6983680ybb.423.1658821116945; Tue, 26 Jul 2022 00:38:36 -0700 (PDT) Date: Tue, 26 Jul 2022 00:37:50 -0700 In-Reply-To: <20220726073750.3219117-1-kaleshsingh@google.com> Message-Id: <20220726073750.3219117-18-kaleshsingh@google.com> Mime-Version: 1.0 References: <20220726073750.3219117-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v6 17/17] KVM: arm64: Introduce pkvm_dump_backtrace() From: Kalesh Singh To: maz@kernel.org, mark.rutland@arm.com, broonie@kernel.org, madvenka@linux.microsoft.com, tabba@google.com, oliver.upton@linux.dev Cc: will@kernel.org, qperret@google.com, kaleshsingh@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, mhiramat@kernel.org, ast@kernel.org, wangkefeng.wang@huawei.com, elver@google.com, keirf@google.com, yuzenghui@huawei.com, ardb@kernel.org, oupton@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, android-mm@google.com, kernel-team@android.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220726_003838_156119_AA97B923 X-CRM114-Status: GOOD ( 14.27 ) 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 Dumps the pKVM hypervisor backtrace from EL1 by reading the unwinded addresses from the shared stacktrace buffer. The nVHE hyp backtrace is dumped on hyp_panic(), before panicking the host. [ 111.623091] kvm [367]: nVHE call trace: [ 111.623215] kvm [367]: [] __kvm_nvhe_hyp_panic+0xac/0xf8 [ 111.623448] kvm [367]: [] __kvm_nvhe_hyp_panic_bad_stack+0x10/0x10 [ 111.623642] kvm [367]: [] __kvm_nvhe_recursive_death+0x24/0x34 . . . [ 111.640366] kvm [367]: [] __kvm_nvhe_recursive_death+0x24/0x34 [ 111.640467] kvm [367]: [] __kvm_nvhe_recursive_death+0x24/0x34 [ 111.640574] kvm [367]: [] __kvm_nvhe___kvm_vcpu_run+0x30/0x40c [ 111.640676] kvm [367]: [] __kvm_nvhe_handle___kvm_vcpu_run+0x30/0x48 [ 111.640778] kvm [367]: [] __kvm_nvhe_handle_trap+0xc4/0x128 [ 111.640880] kvm [367]: [] __kvm_nvhe___host_exit+0x64/0x64 [ 111.640996] kvm [367]: ---[ end nVHE call trace ]--- Signed-off-by: Kalesh Singh --- Changes in v6: - And range check when dumping pkvm stacktrace, per Oliver - Use consistent nVHE call trace delimiters between protected and non-protected mode, per Oliver - Fix typo in comment, per Fuad Changes in v5: - Move code out from nvhe.h header to handle_exit.c, per Marc - Fix stacktrace symbolization when CONFIG_RAMDOMIZE_BASE is enabled, per Fuad - Use regular comments instead of doc comments, per Fuad arch/arm64/kvm/handle_exit.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index e83e6f735100..c14fc4ba4422 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -371,6 +371,39 @@ static void hyp_dump_backtrace(unsigned long hyp_offset) kvm_nvhe_dump_backtrace_end(); } +#ifdef CONFIG_PROTECTED_NVHE_STACKTRACE +DECLARE_KVM_NVHE_PER_CPU(unsigned long [NVHE_STACKTRACE_SIZE/sizeof(long)], + pkvm_stacktrace); + +/* + * pkvm_dump_backtrace - Dump the protected nVHE HYP backtrace. + * + * @hyp_offset: hypervisor offset, used for address translation. + * + * Dumping of the pKVM HYP backtrace is done by reading the + * stack addresses from the shared stacktrace buffer, since the + * host cannot directly access hypervisor memory in protected + * mode. + */ +static void pkvm_dump_backtrace(unsigned long hyp_offset) +{ + unsigned long *stacktrace + = (unsigned long *) this_cpu_ptr_nvhe_sym(pkvm_stacktrace); + int i, size = NVHE_STACKTRACE_SIZE / sizeof(long); + + kvm_nvhe_dump_backtrace_start(); + /* The saved stacktrace is terminated by a null entry */ + for (i = 0; i < size && stacktrace[i]; i++) + kvm_nvhe_dump_backtrace_entry((void *)hyp_offset, stacktrace[i]); + kvm_nvhe_dump_backtrace_end(); +} +#else /* !CONFIG_PROTECTED_NVHE_STACKTRACE */ +static void pkvm_dump_backtrace(unsigned long hyp_offset) +{ + kvm_err("Cannot dump pKVM nVHE stacktrace: !CONFIG_PROTECTED_NVHE_STACKTRACE\n"); +} +#endif /* CONFIG_PROTECTED_NVHE_STACKTRACE */ + /* * kvm_nvhe_dump_backtrace - Dump KVM nVHE hypervisor backtrace. * @@ -379,7 +412,7 @@ static void hyp_dump_backtrace(unsigned long hyp_offset) static void kvm_nvhe_dump_backtrace(unsigned long hyp_offset) { if (is_protected_kvm_enabled()) - return; + pkvm_dump_backtrace(hyp_offset); else hyp_dump_backtrace(hyp_offset); }