Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp5868432yba; Thu, 11 Apr 2019 07:26:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqwcKiGpY1OJdFZaKuzd4/+MvB+E7zRp/lKTJH+kr1MHxsAvEbH0tB0/kPmq80dCJcEdNuKC X-Received: by 2002:aa7:8e55:: with SMTP id d21mr8711068pfr.62.1554992800422; Thu, 11 Apr 2019 07:26:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554992800; cv=none; d=google.com; s=arc-20160816; b=V9D8Km+Fn48yWMtlcrx3VSDEUXbVVnAGA03/2vrPVNQtQxvXkJgfXTb7RZDgQFaaH4 6pPGLZAtGIYfhyrjWC74B0WhsJ13Pp5Q7pC1/7WkPrBEmdJkbtZ8SOwj2S88A/5kkV8/ Frdy7uGpGNn5nmtGLcVPjSMhNRNBgN7kLQkYPQz8S/o7viR0S/VLtunXDSys+wCxAY9W CSE/rmsACsccwr7XTND2l3nwypi6A8Dg1PlVO4udaNHd3KzrIzHGyG9Vd8OE8t6MqBW6 8xch+5NdMnUP6NPaKnpDLIED9hVBUFs9JJb9Pu2cEk2/+ETsOq1AuZLeYZPkX+ckA1BR Ek9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=0PQ6u1lryoiBwiTNLXSuUsVuukUeEcpBs3lJidBSw3Q=; b=DO1zfMNxNpxluih/uYLm0U5udqh/kqhev4cOWkHt4rxwW51Qg7CYdcrcN2EpdQgPC+ MFxwUv5vYqJPi5dHlchLSRVRzYQM7E01kvkXP5fp7aGNmGVDqK/UHLVDOR6ruBM/+zW4 4hdowQ5bKChGtAE3tRZA7p4d0GmEYDX6KOU/UbMBXpg/tLQVB+ajioZOtBXgYSEn6ayb uuUAOcX8uuDD9ln7CbupdhjMxmgINYX8nkevKg0+nFvmgP9QfA07LU+iA+xXwI4NB1y3 SmJbHP7iJ+QrhySHv8KoW1eJ0u4Q0QccsNRgEByMMtnnmoNob9sB4fEp9yRhKwmUClXt YQ2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=YkZoFEdj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z3si34362069pfa.239.2019.04.11.07.26.24; Thu, 11 Apr 2019 07:26:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=YkZoFEdj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726706AbfDKOYd (ORCPT + 99 others); Thu, 11 Apr 2019 10:24:33 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:37666 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726121AbfDKOYd (ORCPT ); Thu, 11 Apr 2019 10:24:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=0PQ6u1lryoiBwiTNLXSuUsVuukUeEcpBs3lJidBSw3Q=; b=YkZoFEdj+mDuWhZR9lu1QPt7i 6Kmzt3Km4bC7+HUCiUzk3r37FaGfNsSg9Ctx9QheZfUEeXpIo/Mvsknm5NHITXgfdIubs8eP8E/zh h5a7LtGMbUmDhhR+x53KB4CiCTq6KmZEvnmo53dTypGeOXui1vvd7YWD1l5izNjZNKOyVqobe9e0n 3jrghxOzP++KF80XlRSkJxcmtiJFrSidFS8GJOE5bSuJdeyABOMfMI83cqpnddlUJS4PU7GOsgi1o mCrvxvs6ivXGxtpoWVwjIfaDMCUWzxR/DJXjyEV4J3zTbTqhK+eY/EYiWDly4HothfgUj+JUE3Otu 3LOIEPsvA==; Received: from hch by bombadil.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1hEacy-0006u7-LD; Thu, 11 Apr 2019 14:24:32 +0000 Date: Thu, 11 Apr 2019 07:24:32 -0700 From: Christoph Hellwig To: Mao Han Cc: linux-kernel@vger.kernel.org, Palmer Dabbelt Subject: Re: [PATCH 1/3] riscv: Add perf callchain support Message-ID: <20190411142432.GB8343@infradead.org> References: <195185ea63240ed396026505d96d1f6449963482.1554961908.git.han_mao@c-sky.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <195185ea63240ed396026505d96d1f6449963482.1554961908.git.han_mao@c-sky.com> User-Agent: Mutt/1.9.2 (2017-12-15) X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > --- /dev/null > +++ b/arch/riscv/kernel/perf_callchain.c > @@ -0,0 +1,122 @@ > +// SPDX-License-Identifier: GPL-2.0 > +// Copyright (C) 2019 Hangzhou C-SKY Microsystems co.,ltd. Please use normal /* */ Style comment for everything but the SPDX tags. > +static int unwind_frame_kernel(struct stackframe *frame) > +{ > + int graph = 0; > + > + /* 0x3 means misalignment */ > + if (!kstack_end((void *)frame->fp) && > + !((unsigned long)frame->fp & 0x3) && > + ((unsigned long)frame->fp >= TASK_SIZE)) { > + frame->ra = ((struct stackframe *)frame->fp - 1)->ra; > + frame->fp = ((struct stackframe *)frame->fp - 1)->fp; > + /* make sure CONFIG_FUNCTION_GRAPH_TRACER is turned on */ > + if (__kernel_text_address(frame->ra)) > + frame->ra = ftrace_graph_ret_addr(NULL, &graph, > + frame->ra, NULL); > + return 0; > + } else { > + return -EPERM; > + } Please do early exists for all the error conditions. Also no casts are needed for using ->fp as a scalar value, and we should probably just do a struct copy instead of copying both values individually. The function should look something like: static int unwind_frame_kernel(struct stackframe *frame) { if (!kstack_end((void *)frame->fp)) return -EPERM; if ((frame->fp & 0x3 || frame->fp >= TASK_SIZE) return -EPERM; *frame = *((struct stackframe *)frame->fp - 1); if (__kernel_text_address(frame->ra)) { int graph = 0; frame->ra = ftrace_graph_ret_addr(NULL, &graph, frame->ra, NULL); } return 0; } > +static void notrace walk_stackframe(struct stackframe *fr, > + struct perf_callchain_entry_ctx *entry) > +{ > + while (1) { > + int ret; > + > + perf_callchain_store(entry, fr->ra); > + > + ret = unwind_frame_kernel(fr); > + if (ret < 0) > + break; > + } > +} Why not: do { perf_callchain_store(entry, fr->ra); } while (unwind_frame_kernel(fr) >= 0); > +/* > + * Get the return address for a single stackframe and return a pointer to the > + * next frame tail. > + */ > +static unsigned long user_backtrace(struct perf_callchain_entry_ctx *entry, > + unsigned long fp, unsigned long reg_ra) > +{ > + struct stackframe buftail; > + unsigned long ra = 0; > + unsigned long *user_frame_tail = (unsigned long *)(fp - sizeof(struct stackframe)); Overly long line. > + fp = user_backtrace(entry, fp, regs->ra); > + while ((entry->nr < entry->max_stack) && > + fp && !((unsigned long)fp & 0x3)) > + fp = user_backtrace(entry, fp, 0); Please don't indent the condition continuation and the loop body by the same amount.