Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp377828ybt; Mon, 6 Jul 2020 11:30:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxMgtGLcDFOJclbGEHAfy45uL0eCFbQRhsOrygpt4uDO/qn/gx+mqGdOvjNFPeIeKzW9h/R X-Received: by 2002:a17:906:a0c8:: with SMTP id bh8mr45805533ejb.190.1594060200542; Mon, 06 Jul 2020 11:30:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594060200; cv=none; d=google.com; s=arc-20160816; b=q4VpJHg47ydmYRIYv9xznem4gfPapXEhvuxl8OPj8YsG9JSn3aln9AFCvKEwlIkT3r 2ocgU8IeOYBMIEGLFO6+F69kjNSK4UMhXRJ4PvU3TKimjVWkkJBCnkXN7AmudPAYE5Pf FDJMy+ymfAquF+2jbKOFYPZiPa5dEOnPODowHiUvAZZodZ1Mqg+Nsytmovb3T7vz5G+l 0inAiSW9EaoDnCbmbfSAr8fogkyVOzLkhjC9UNqN4AXHtRmxNpLc1FYAw0ckvssSDzgX jP3NlL9oGGFD9baFpf2fJj5MYD9z4fWrf3GI9fsA78JwQSWzwbf9ka53y1/TpMGYCxab MEvg== 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:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=qqoupK5Oq6XKew6i6oyqJ6+pHqm95p/dfVqnR40LUaA=; b=EsTZR7Hvi/37VwEIACfFTjhOSA6kduS9uEXrPsE0dT8cIow7qPz/cRjRlPw/SLbURm US+kIObJ07gwUjWgXEfen7y+Iutvwsg3fJOBTKm2N+PH6aQl7yzTTlHSM48R7NZxRmdy i+pel2rQA9HvNaZVbFhrI9siDQ7Pn3jCZbhPD78TssQnGH3D4r7vkPhPf4Ivr/f2L/vr DAWanlFBcVuJcwFvS5Wwg1gpgFMrWyhOweiUZWj66FKWI35SbE3W8pc/9l+B+G+VR42i ZZYYS0Z8l9t6pHiwvUCO33wJjr/+RRmDmfGIOdSLfC87fSQSNVo2YahZdMrPkMfk5VHo j13w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Iqizygs0; 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 u25si4201334eje.33.2020.07.06.11.29.36; Mon, 06 Jul 2020 11:30:00 -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=Iqizygs0; 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 S1729657AbgGFS1Y (ORCPT + 99 others); Mon, 6 Jul 2020 14:27:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729632AbgGFS1Y (ORCPT ); Mon, 6 Jul 2020 14:27:24 -0400 Received: from mail-oi1-x249.google.com (mail-oi1-x249.google.com [IPv6:2607:f8b0:4864:20::249]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33950C061755 for ; Mon, 6 Jul 2020 11:27:24 -0700 (PDT) Received: by mail-oi1-x249.google.com with SMTP id d185so9741761oia.21 for ; Mon, 06 Jul 2020 11:27:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=qqoupK5Oq6XKew6i6oyqJ6+pHqm95p/dfVqnR40LUaA=; b=Iqizygs0Qd97GZDMVr5MpvwQmOfgjGa2UIYaRHiioHiJ0vLDD6W8swj+li7Hr5Svhx LO9/lPvsugaN0/6egSTnXgsy1EAyz4KJfFrZkFR3QfG5BCtd18pLxBtTzFA1CaLlqbCt jEQOGwpkEXPy06MIkntvurVJNu6a9JgL7UByfxIbHsVXnoEyOJIeT8aFVonyvm+CJ3aZ t+s1JZPyorXK4j2tSOLwFoLeEu6MA/Z9EYSoxsjont9Q0X1H4GaAcgtCMEvAPlLLJEuw RZuxLdkZO/u32IzVhZf6kw4qro5XTFTAe+CI7wDg0ODIHGUzHlAtbeIzRiLNK2m11JyB V8xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=qqoupK5Oq6XKew6i6oyqJ6+pHqm95p/dfVqnR40LUaA=; b=ASIebniY9fSj6XVPGGmxvTyzrvvLgsmqFIxWe+xl6jy01YVs0LkHg12HjkHPd0ipdo HiuLGQoK39Bz8nfAvLb62rMOmmyVcNjGisayG+J4KaM1MoC/1oDkWBhTCZYMxTO0V0Vf Hgkm+L5dG+bg5mApGKjijsMq5+yUNKxVBgTh7HSJR0HtlCkcJos0Cz5VPZvA1ZmC+IA4 xQGKLjCLVrFQty8b7xfMvAy7StKyKup6jLvlRxjABn9RU8PXm1bIubH9/XAsBIiEf77k qp45OHa91KWc6sCZZGhzwpenQZC0c34k4anfTqC5di4zrz55yF257O1FC+hcXk5j1Hc4 PoXA== X-Gm-Message-State: AOAM530H0PldT4Ykm+Pmlfm5fLmF1OtvWqEWuay/2yy7ovs+/huU0Brj 3fZVMfLFBYhjy+fd3cj0M/TAF7oXDg== X-Received: by 2002:aca:c683:: with SMTP id w125mr443202oif.37.1594060043510; Mon, 06 Jul 2020 11:27:23 -0700 (PDT) Date: Mon, 6 Jul 2020 18:27:13 +0000 In-Reply-To: <1592531704.23014.1.camel@mtkswgap22> Message-Id: <20200706182713.3693762-1-nhuck@google.com> Mime-Version: 1.0 References: <1592531704.23014.1.camel@mtkswgap22> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH v2] ARM: stacktrace: 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 , Miles Chen , stable@vger.kernel.org, Nick Desaulniers 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 Reported-by: Miles Chen Tested-by: Miles Chen Cc: stable@vger.kernel.org Reviewed-by: Nick Desaulniers 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.212.ge8ba1cc988-goog