Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp30779ybt; Tue, 16 Jun 2020 15:39:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxdHXNiXbdTqgwYC2Yxoeo1splLAH19HlazbQFxgY3ay82IfKkW92uepqryU8pUxpuQw8oZ X-Received: by 2002:a17:906:95d6:: with SMTP id n22mr4824851ejy.138.1592347141699; Tue, 16 Jun 2020 15:39:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592347141; cv=none; d=google.com; s=arc-20160816; b=kHDRsvBk5dH8lSTEO46J+iII3S8wFM+t1sR2+xC5kMv8ww+HkInZhJTS1EtRcRf/jZ KaZxFsY2G1GXqPw4H254PzQ+muwLtsoTxdSyLgJpTf3zo64tsjPySMhVR74vHLfkItHC z+NC9qKFkKOL3n6Ru66m7iqQC0aA3BWMdfKyMg7WXwgsmucSkxhdTmdY3b4U7f5OO1QP ZAglTPi6bVoqxC8sw18o5Qx61UJBgKG9zST1mB/mD/MHVQKzXMmPOkeeNHmlX/uv/XT8 mVj+vp5DoDLRLIxJxZNNR9Jd9OQH5OBfxubxE26fvoLujKSn8/K0NW4mT8dzEu+Oj+3j 2+YQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=cHSpMAjXfyGfwJeb9KkrKwdJRoh7Aq/xdZj4zd+a3E4=; b=OVVgEWUEy1TpB1wmEThktPQMZWFS2FBhnZICql+csTi0B2RW0N6G2WIhBxN78OV2fn E3TXbOnY+km0FEK7JD3pL56DXuiIOyRIhY7ZPYbgzgFElhGIr5BTHdnJJR8pKkCtgZSO RNNPP6p1z1QUoNNH4atLJUdjWyosXh+Fq6TQa5B6PcApIhhWB74PVj7aG62EUU1EjGiJ 8BCN3LomjMBD9vo765HAyrOczLCiTj70p8tzC3YlenyiiLJbFIr/RfnSLBug+RA+zOzB eOLOr2V5BldOzhugdMCh9K6b9NTPNiNc14JVOE5sgC3arBWB9s9b0r+O0hUIIXtJnvBu R93A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=HheB+LyC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l2si11686414ejd.191.2020.06.16.15.38.39; Tue, 16 Jun 2020 15:39:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=HheB+LyC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726468AbgFPWg4 (ORCPT + 99 others); Tue, 16 Jun 2020 18:36:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725849AbgFPWg4 (ORCPT ); Tue, 16 Jun 2020 18:36:56 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85FE4C061573 for ; Tue, 16 Jun 2020 15:36:54 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id a188so230478ybg.20 for ; Tue, 16 Jun 2020 15:36:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=cHSpMAjXfyGfwJeb9KkrKwdJRoh7Aq/xdZj4zd+a3E4=; b=HheB+LyCA6QLhPuMgTXI9D4bM/sjHW46HrcniylyQpWYVvVz0VvbgMrMwmtT/VTMSn ozdxEbd4FiuD1Vz2/vxIo70UD+65D7QAeN3ds6hUUgEgYyIkK+Pj1QgwXUx6QytmEvLt DT+PNGSewXZi9ezwE2rkJRb3fvvRV8MpWXByslv2PBIba6elzAF0EoAUwGexIfUqUksw 3PAPYtIzAHEWJ0hrQcDp9/La/txp56hLNX0CufNivY+1H9y093X05ti4+rGg08OsiWEK rUbmcjGkW8rDJ/PCllYvS/jtXtiMv2tSdKhTcN9BDVdfFlEaABGDXNcskFfD6tY/O02Q 4fnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=cHSpMAjXfyGfwJeb9KkrKwdJRoh7Aq/xdZj4zd+a3E4=; b=JXbQejI+DOjVvlVyBGRRgumUAg8UDxeJvNMGcbe8cKsp8LvegMKdmhhGey/fYtsAAv Qf7DrMQi8KY41qULizddERo4GGKUCZxciLF2WJpgsnLKC1ayMrKxlDM1cZrgc89v7Ps7 xk+eK8pkNCGnadKPv+DowYtQWnFBUYtkBhxXT1Mj6kdWSJ1zNIB30v4T0a0VAO5zkG9L vj+U4vyoolAVVjw7kfKcQGUBhwoL3xa7pGW1ZZvK6RWg+KdPpbFO/SyPGLVGa9j7xqNm qm04f8Zf2YXsdewksC03ZXpNTuCtOHWm7en5XoxalHsrxIp3ZubF5F7EoAwTpcGNw91k w+7w== X-Gm-Message-State: AOAM531gm6wsTzXQGkiiPtY4bHekNSqDhfsGK45eh9DYE4k00FHoeLEc SifE8+ZdItsnxyUr+EWB0znIq7DysQ== X-Received: by 2002:a25:d8cd:: with SMTP id p196mr8176207ybg.55.1592347012784; Tue, 16 Jun 2020 15:36:52 -0700 (PDT) Date: Tue, 16 Jun 2020 22:36:33 +0000 Message-Id: <20200616223633.73971-1-nhuck@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.27.0.290.gba653c62da-goog Subject: [PATCH] Fix unwind_frame for clang-built kernels From: Nathan Huckleberry To: linux@armlinux.org.uk, vincent.whitchurch@axis.com Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, clang-built-linux@googlegroups.com, Nathan Huckleberry Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Since clang does not push pc and sp in function prologues, the current implementation of unwind_frame does not work. By using the previous frame's lr/fp instead of saved pc/sp we get valid unwinds on clang-built kernels. The bounds check on next frame pointer must be changed as well since there are 8 less bytes between frames. This fixes /proc//stack. Link: https://github.com/ClangBuiltLinux/linux/issues/912 Signed-off-by: Nathan Huckleberry --- arch/arm/kernel/stacktrace.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/arch/arm/kernel/stacktrace.c b/arch/arm/kernel/stacktrace.c index cc726afea023..76ea4178a55c 100644 --- a/arch/arm/kernel/stacktrace.c +++ b/arch/arm/kernel/stacktrace.c @@ -22,6 +22,19 @@ * A simple function epilogue looks like this: * ldm sp, {fp, sp, pc} * + * When compiled with clang, pc and sp are not pushed. A simple function + * prologue looks like this when built with clang: + * + * stmdb {..., fp, lr} + * add fp, sp, #x + * sub sp, sp, #y + * + * A simple function epilogue looks like this when built with clang: + * + * sub sp, fp, #x + * ldm {..., fp, pc} + * + * * Note that with framepointer enabled, even the leaf functions have the same * prologue and epilogue, therefore we can ignore the LR value in this case. */ @@ -34,6 +47,16 @@ int notrace unwind_frame(struct stackframe *frame) low = frame->sp; high = ALIGN(low, THREAD_SIZE); +#ifdef CONFIG_CC_IS_CLANG + /* check current frame pointer is within bounds */ + if (fp < low + 4 || fp > high - 4) + return -EINVAL; + + frame->sp = frame->fp; + frame->fp = *(unsigned long *)(fp); + frame->pc = frame->lr; + frame->lr = *(unsigned long *)(fp + 4); +#else /* check current frame pointer is within bounds */ if (fp < low + 12 || fp > high - 4) return -EINVAL; @@ -42,6 +65,7 @@ int notrace unwind_frame(struct stackframe *frame) frame->fp = *(unsigned long *)(fp - 12); frame->sp = *(unsigned long *)(fp - 8); frame->pc = *(unsigned long *)(fp - 4); +#endif return 0; } -- 2.27.0.290.gba653c62da-goog