From patchwork Tue Feb 23 18:12:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Madhavan T. Venkataraman" X-Patchwork-Id: 12100667 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1BD36C433E0 for ; Tue, 23 Feb 2021 18:14:19 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D5C4B64DD3 for ; Tue, 23 Feb 2021 18:14:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D5C4B64DD3 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe :List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:To:From: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=QxRvJW7pKSbwqIgTnAw+Nc11F6HFcuBOiB863Q1fnCU=; b=QDug4/EfCV5N1/qt5IoXq9g2VD DtiOuxaI2tlPbtJx8riEdgnt58uFAtktRS2Qpu8M9Q1NcN/rXREa50R+7jC4gCZ4F/CXU6xBwCo3a ieTDxk1rP49V/x/Xzi5ER6gtZY6cJoejdJ6MctwbzxXGWBoFzzDNMRgUDckrLvmRDzyQeQQ2DmCmr Cdmix1RYZPRv298EiPK+NroGuDHMQk99DkQcdjxj2V1gdorSo6KpJ5M+hT4W5DR7D/1VjCGYjJJyg 8qloFKnQt85Vj9vbdNRkOjm+yKwcGiqPqd7DprBw9W/t0tbqSGxwuPJEHWak/tnfM2qrPLlHwnyWL +J04ty7w==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1lEcB9-0004Uo-1L; Tue, 23 Feb 2021 18:12:59 +0000 Received: from linux.microsoft.com ([13.77.154.182]) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1lEcB4-0004Th-S9 for linux-arm-kernel@lists.infradead.org; Tue, 23 Feb 2021 18:12:57 +0000 Received: from x64host.home (unknown [47.187.194.202]) by linux.microsoft.com (Postfix) with ESMTPSA id 8F0A520B6C40; Tue, 23 Feb 2021 10:12:50 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 8F0A520B6C40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1614103971; bh=sy1CSkf4NPTHdPtm6j5T9s2Pe4lqTfBSvIKFA247V6w=; h=From:To:Subject:Date:In-Reply-To:References:From; b=K03r/VXlwk2jKpOAoakKfKTnk9GgZIaZ9RmcwGyE5/jK0n0yMqk9aGLaUFlg9nGS7 vh8SAXoOZ99GbSj9QKLNVoO+IqAjd5RBtUqtEt0bMUmbji47YgJB/vCTRpw/4CcHaU lsSYlbqTz0gCx991Ir1S5iazzjKggR2/3oJBmQEU= From: madvenka@linux.microsoft.com To: broonie@kernel.org, mark.rutland@arm.com, jpoimboe@redhat.com, jthierry@redhat.com, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [RFC PATCH v1 0/1] arm64: Unwinder enhancements for reliable stack trace Date: Tue, 23 Feb 2021 12:12:42 -0600 Message-Id: <20210223181243.6776-1-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210223_131255_197915_3CFDF00F X-CRM114-Status: GOOD ( 17.74 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 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 From: "Madhavan T. Venkataraman" I have made an attempt to add some enhancements to the stack trace code so it is a few steps closer to what is required for livepatch. Unwinder changes ================ Termination =========== Currently, the unwinder terminates when both the FP (frame pointer) and the PC (return address) of a frame are 0. But a frame could get corrupted and zeroed. There needs to be a better check. The following special terminating frame and function have been defined for this purpose: const u64 arm64_last_frame[2] __attribute__ ((aligned (16))); void arm64_last_func(void) { } In this patch, the FP is set to arm64_last_frame and the PC is set to arm64_last_func in the bottom most frame. Exception/Interrupt detection ============================= An EL1 exception renders the stack trace unreliable as it can happen anywhere including the frame pointer prolog and epilog. The unwinder needs to be able to detect the exception on the stack. Currently, the EL1 exception handler sets up pt_regs on the stack. pt_regs contains a stack frame field that can hold an FP and a PC. The exception handler chains this stack frame field along with other frames on the stack. In other words, the EL1 handler creates a synthetic exception frame. Currently, the unwinder does not know where this exception frame is in the stack trace. In this patch, the LSB of the exception frame FP is set. This is similar to what is done on X86. When the unwinder detects the frame with the LSB set, it needs to make sure that it is really an exception frame and not the result of any stack corruption. It can do this if the FP and PC are also recorded elsewhere in the pt_regs for comparison. Currently, the FP is also stored in regs->regs[29]. The PC is stored in regs->pc. However, regs->pc can be changed by lower level functions. So, the PC needs to be stored somewhere else as well. This patch defines a new field, pt_regs->orig_pc, and records the PC there. With this, the unwinder can validate the exception frame and set a flag so that the caller of the unwinder can know when an exception frame is encountered. Unwinder return value ===================== Currently, the unwinder returns -EINVAL for stack trace termination as well as stack trace error. In this patch, the unwinder returns -ENOENT for stack trace termination and -EINVAL for error. This idea has been plagiarized from Mark Brown. Reliable stack trace function ============================= arch_stack_walk_reliable() is implemented in this patch. It walks the stack like the existing stack trace functions with a couple of additional checks: Return address check -------------------- For each frame, the return address is checked to see if it is a proper kernel text address. If not, the stack walk fails. Exception frame check --------------------- Each frame is checked to see if it is an EL1 exception frame. If it is, the stack walk fails. Signed-off-by: Madhavan T. Venkataraman Madhavan T. Venkataraman (1): arm64: Unwinder enhancements for reliable stack trace arch/arm64/include/asm/processor.h | 2 + arch/arm64/include/asm/ptrace.h | 7 ++ arch/arm64/include/asm/stacktrace.h | 5 ++ arch/arm64/kernel/asm-offsets.c | 1 + arch/arm64/kernel/entry.S | 14 +++- arch/arm64/kernel/head.S | 8 +-- arch/arm64/kernel/process.c | 12 ++++ arch/arm64/kernel/stacktrace.c | 103 +++++++++++++++++++++++++--- 8 files changed, 137 insertions(+), 15 deletions(-) base-commit: e0756cfc7d7cd08c98a53b6009c091a3f6a50be6