Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp2073705lqz; Tue, 2 Apr 2024 06:40:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUF4jNf02WI90545VfYHk7kiyRxYEyqYVLH7hsz4xO5siFxv2m/fM3Fmh0Hlt2wdDbODN0bmi1/k0soxheXM46q2JPb8k3J8Nc9rhNIJQ== X-Google-Smtp-Source: AGHT+IEZ8gIjzGnko4PFZ2L1GDTbEoTkvcJt777Jq6C/sXr2Wfq+cN7NdJ6Vp7zFFhHETVd+N//1 X-Received: by 2002:a05:6a20:2448:b0:1a7:2452:808f with SMTP id t8-20020a056a20244800b001a72452808fmr1300123pzc.53.1712065223664; Tue, 02 Apr 2024 06:40:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712065223; cv=pass; d=google.com; s=arc-20160816; b=Ntj4oYr0CjjR8FDBXQ5GhHizcX0q05tu3R87isauU35GzYOZmUlz18RPTRHEWtZMtg mcLX3IDzxnJE0oTeBDV04f2XSduyy9sP4bbl/o+w6kSTdAqcb6nSWdHOoAf4eh0v8R44 dYdVr/3wS2asf5l3AIb96erm4NGgP//3xVi7o4V4yRxSk0C6q9yCYGPZtUY+7ZVZYkCV 2FKjxGRufoBFYArYJjSPEd8u21KzIok7BvWxe2S8UsAMdV7QnyCTiEMrH8ugABh3lF7l NTGpg7tL5wx/E/286rKBPtE2s5wTFO7p9okouYijKSe2mVs3m2XFnLM+LDikW0LRhUHr Yn6w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=dL71pYsXVRfN/6e7YgE9JD6Z27i3D6UhmX+KIRgqWYU=; fh=ATlQKU0OtD3nSxm1+UNPgFmqz72BHKUX/W+ZlbqL0+0=; b=jxK8BNLAQZDtmB/+PRVRX7BFG5u0CHvug9baRyvm1PSxbZLlq1qA0o8OIuG2mbxX3i r6QTnXYXW1mVus3gBRqo65pyMaYxduBFrxt0/HjvG/smqN7KF8YRgYuznEM6AqVYq50G U2EMpjsQoEJA6enP6wUWIyOKJSM6mk8QyMDj8TT8lC3wIqMVbwqKzeBi2n6G6HGCfFn0 oV0SX1xzEG5W2En2dhrSOIdSEIMFboZhaOlUPug3VelroKp1d0vqomYhu6Dc/FDHH1Fz wSDdNqagxub3P3NzWbbkzbW35uC9dVZG/nxBoHosHaiWm5bgJqmbEmogzNJTv2WC+ATw U3bw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dZFQ3Dsk; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-128028-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-128028-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id lm15-20020a056a003c8f00b006eac82adfd4si11501553pfb.152.2024.04.02.06.40.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Apr 2024 06:40:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-128028-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dZFQ3Dsk; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-128028-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-128028-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id EB19128706D for ; Tue, 2 Apr 2024 13:20:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3A4E382C60; Tue, 2 Apr 2024 13:20:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dZFQ3Dsk" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4653260DE9 for ; Tue, 2 Apr 2024 13:20:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712064005; cv=none; b=scxwwuRyhvGqB1te/dqKLOIzz2txiADg43/VjZW2+eWxkburzR5ubO3Q4zSIgIjdJ0oAalgeu8jUCU+c6FX0EElqPZeL5UuTp7Lu4ZOGWigtgRgHrabM27bju5MSSl6B1d2FZRieljdJK/QtnfTfvULloXxR13G3UI81MlPxu6g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712064005; c=relaxed/simple; bh=uKsjU8/PIj0LW7ebecEKaDRjpVVEPAPWOJqhLvwjBdY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=COx+TQalKCY5W5Ty7cmXQMQ+HA0r4tKx6uikn+t2mLSquNGA/C3ifrq61q9zvq0Ifo5EwekVLvZrtmvbnabZ/fBPtdJPZAMmwRx0YGOCn4+bTACA7MTVFo1iPpN0326XuHJQnexz6UmfBf6spI49noiZmAqgZdFU6/vNBjkFXDM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dZFQ3Dsk; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 57347C433F1; Tue, 2 Apr 2024 13:20:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712064004; bh=uKsjU8/PIj0LW7ebecEKaDRjpVVEPAPWOJqhLvwjBdY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=dZFQ3DskOEKCJJ0pjg1btHzSeMqU5YkkvRkMb7PmvR6XoRk5BdnGWP1ZPPnBsNgfM Av1CEuprICBAeX0wAlhkCPjXl8uRUhu1CZDEIG4674budbSyQlFgEnmDjMAFAMP14P nqqxnetxZv0/0OAny6ddaerzAQQYnEl2pKa8OGzFuCPDf4Oe4FTphJVpSGL17g6oWV Nq7BU1lN+wk/l/6x4ApmMsojReAskjExzqutQ25+eUyZR3mlHf4Z967feBBcCSEHmJ 5dYTA121fm+zgKKJ48sHACOTTYAg8Y5Pr/SwKmJJPZltypnS/iqDCc4xlhq63+JB7r +N2z1Y/YT041A== From: =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= To: Puranjay Mohan , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= Cc: puranjay12@gmail.com Subject: Re: [PATCH 2/2] riscv: stacktrace: make walk_stackframe() more robust In-Reply-To: <20240328184020.34278-3-puranjay12@gmail.com> References: <20240328184020.34278-1-puranjay12@gmail.com> <20240328184020.34278-3-puranjay12@gmail.com> Date: Tue, 02 Apr 2024 15:20:01 +0200 Message-ID: <87v84zrjem.fsf@all.your.base.are.belong.to.us> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Puranjay Mohan writes: > Currently walk_stackframe() provides only a cookie and the PC to the > consume_entry function. This doesn't allow the implementation of > advanced stack walkers that need access to SP and FP as well. > > Change walk_stackframe to provide a struct unwind_state to the > consume_entry function. This unwind_state has all information that is > available to walk_stackframe. The information provided to the callback > will not always be live/useful, the callback would be aware of the > different configurations the information in unwind_state can be. > > For example: if CONFIG_FRAME_POINTER is not available, unwind_state->fp > will always be zero. > > This commit doesn't make any functional changes. > > Signed-off-by: Puranjay Mohan > --- > arch/riscv/kernel/stacktrace.c | 55 ++++++++++++++++++++++++++++++---- > 1 file changed, 49 insertions(+), 6 deletions(-) > > diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrac= e.c > index e28f7b2e4b6a6..92c41c87b267b 100644 > --- a/arch/riscv/kernel/stacktrace.c > +++ b/arch/riscv/kernel/stacktrace.c > @@ -14,15 +14,26 @@ >=20=20 > #include >=20=20 > +struct unwind_state { > + unsigned long fp; > + unsigned long sp; > + unsigned long pc; > + struct pt_regs *regs; > + struct task_struct *task; > +}; > + > +typedef bool (*unwind_consume_fn)(void *cookie, const struct unwind_stat= e *state); > + > #ifdef CONFIG_FRAME_POINTER >=20=20 > extern asmlinkage void ret_from_exception(void); >=20=20 > static __always_inline void > walk_stackframe(struct task_struct *task, struct pt_regs *regs, > - bool (*fn)(void *, unsigned long), void *arg) > + unwind_consume_fn fn, void *arg) > { > unsigned long fp, sp, pc; > + struct unwind_state state; > int level =3D 0; >=20=20 > if (regs) { > @@ -40,12 +51,17 @@ walk_stackframe(struct task_struct *task, struct pt_r= egs *regs, > sp =3D task->thread.sp; > pc =3D task->thread.ra; > } > + state.task =3D task; > + state.regs =3D regs; >=20=20 > for (;;) { > unsigned long low, high; > struct stackframe *frame; >=20=20 > - if (unlikely(!__kernel_text_address(pc) || (level++ >=3D 0 && !fn(arg,= pc)))) > + state.sp =3D sp; > + state.fp =3D fp; > + state.pc =3D pc; > + if (unlikely(!__kernel_text_address(pc) || (level++ >=3D 0 && !fn(arg,= &state)))) > break; >=20=20 > /* Validate frame pointer */ > @@ -64,7 +80,10 @@ walk_stackframe(struct task_struct *task, struct pt_re= gs *regs, > pc =3D ftrace_graph_ret_addr(current, NULL, frame->ra, > &frame->ra); > if (pc =3D=3D (unsigned long)ret_from_exception) { > - if (unlikely(!__kernel_text_address(pc) || !fn(arg, pc))) > + state.sp =3D sp; > + state.fp =3D fp; > + state.pc =3D pc; > + if (unlikely(!__kernel_text_address(pc) || !fn(arg, &state))) > break; >=20=20 > pc =3D ((struct pt_regs *)sp)->epc; > @@ -79,9 +98,10 @@ walk_stackframe(struct task_struct *task, struct pt_re= gs *regs, >=20=20 > static __always_inline void > walk_stackframe(struct task_struct *task, struct pt_regs *regs, > - bool (*fn)(void *, unsigned long), void *arg) > + unwind_consume_fn fn, void *arg) > { > unsigned long sp, pc; > + struct unwind_state state; > unsigned long *ksp; >=20=20 > if (regs) { > @@ -99,9 +119,14 @@ walk_stackframe(struct task_struct *task, struct pt_r= egs *regs, > if (unlikely(sp & 0x7)) > return; >=20=20 > + state.task =3D task; > + state.regs =3D regs; > + state.sp =3D sp; > + state.fp =3D 0; > ksp =3D (unsigned long *)sp; > while (!kstack_end(ksp)) { > - if (__kernel_text_address(pc) && unlikely(!fn(arg, pc))) > + state.pc =3D pc; > + if (__kernel_text_address(pc) && unlikely(!fn(arg, &state))) > break; > pc =3D READ_ONCE_NOCHECK(*ksp++) - 0x4; > } > @@ -109,10 +134,28 @@ walk_stackframe(struct task_struct *task, struct pt= _regs *regs, >=20=20 > #endif /* CONFIG_FRAME_POINTER */ >=20=20 > +struct unwind_consume_entry_data { > + stack_trace_consume_fn consume_entry; > + void *cookie; > +}; > + > +static __always_inline bool > +arch_unwind_consume_entry(void *cookie, const struct unwind_state *state) Same comment as patch 1. Bj=C3=B6rn