From nobody Thu Dec 18 13:33:11 2025 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6E132BE639 for ; Mon, 15 Dec 2025 03:42:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765770166; cv=none; b=pR72oPSlIW1HqHQqwPPKWG1zGeMwa4JgGLGZy+F9D8DAyPI5Z2SRFymA8KdZlrQBYrBzrbVsDYvDqcF8aZ7XyvQe9DsMGTEQAvh9GjcYaGVwArpZpS1T7WFMAVbLXzrGqvXbQONpKwpNs5AYDNS29nmoMYmJqZ9kW1s6qzWHgGg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765770166; c=relaxed/simple; bh=yGNIMta8W5fCJfVCRLduvS9JRYsFtLAV7Z8MNBPkCso=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kGN5OzCbiUrvtqdbuCD9v2zaPDVZledcr1R5SaEIMDSACSAHnAjDwrtt9t3S31EhiUqmpd/fPJvUljG1cRQtHUQim5Frd1xvy84U9juldIZeVOMU0aE2qFKLpexRrKKVKiyEqki4wRe0LZER6bipEfvYqPfYccCDqfJHcNA/Uac= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EAHQGox8; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EAHQGox8" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2a0d5c365ceso10405965ad.3 for ; Sun, 14 Dec 2025 19:42:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765770164; x=1766374964; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=p5UBV2dmchl80Cv5GOle8EKJ8orIpqARGfyikGjXPIY=; b=EAHQGox8fuOksrR6LDr9tAsS1HTRn0yac52jvD7dN/HO7278i4eSlh2uoXpRPuaacy c4pNPIEZ+wR4x/L5LP6tnKvnyQPHbyZx5jBg3n/AH/Tk5wuZtZ6K5UZXuH3F+6NOMj6Q 5/lKLCxaeUiKlo5bnBFAz06vDbQKMQzWs27j+IVsKzlwQgUG4i/P8mYomk0Ah6LipwnG o03fSshtnJnJ8VIJ4UWb9Aas6T6OpLvG+NODO/lZShdGPIV87uzaBGYueJ+BbWUSi2mA 0EcFz0xCUvHtBGYdw290qZPVxAuOTe0b3E7HoF9D1Pl5cltIt4dYPPfpJav/ReMugX7n PDWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765770164; x=1766374964; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=p5UBV2dmchl80Cv5GOle8EKJ8orIpqARGfyikGjXPIY=; b=gLx+8FIE3VR9YY8ADlYtN+Reng7pk8g74eK+8Q+77aob8UY2YINBVqY6owS1zUHqBh iqcDP/7XXHYE4UVXLvtYqVg9kleV+Kx4rne9KsrYhs90KtoPYce2nVRaxh6ubdZNIXse CfOKsH5xAqokAuYAyI5kkITBpt1SWaYnY+IcEQawVDxUrRvmEPSSZa2qdD+uCv4H8KmC 1KA/Wxz++Ck767sJF2eR6S5uEUF3OcLfT/eC7bYgEb0sYtcdWZMzaddiXU6WBTmJRVV3 EGPt4AsBsd+PfHedJ4Y/ciPpW+pf5MfxlNHpEX0zg5xda8R1L+zPiG2PpItlWPD3Hr45 zYIg== X-Forwarded-Encrypted: i=1; AJvYcCV3BE1yGLz8o20EuPivkA7kYeC7PkFMg3PRM+suELfkpwIaVNvGnoJE0cAY4NVXlqUIxkgN7TpcB9n5PX4=@vger.kernel.org X-Gm-Message-State: AOJu0YzDHtrqcxXm5P0k9GHwlY6qDXkbevSN2thKTtv4ZILoPHXjSjxF fL5GdS9GZsBmFyImofsTNmQMHiHeS6wBLVGUChEXO5otdriDifN4HLAM X-Gm-Gg: AY/fxX4OsPhuvRnab5TyUGidnCpd3zj8QC/wujzkg4jaxosVC2kWEXIt11z8QWlsmEq RACJqsdecNkoKdcov+3KJ23VcxZlngBqPj3NddT9XQKTOTEvyBFkeQu6Bg5z36LXASstqsg6XR8 +TbPkNnS5IG2GJwi9I59idMpnZPxLxjIb1GCzoR1nAsYkXMFlkqcAGKPNSa2MNvi4R7z1MLaS0O CqvwOGHfOcQc5iphHp6YrKniDZmCGfXkg+H+S8FZ479C3bGcBjzMsVcO9hCh9XrUFKzSty5NtmN paealTslD8TYeR41H0DNAEF6dzwA5+FtSJiLBTYAN3dvoD81+D45c6eKdDH2kvH5dOkT2N3+ig8 +KkuwDjtEYw7BvpkJrQLDFPmQwPQPz2MXlcPmLAR12aOG+Ed3KHEQaEwagfMeB3s26ayRlmgjN5 ulZ+mXqviLgrqywLQAjJtwFLffEHQ= X-Google-Smtp-Source: AGHT+IEv/SYjvRWH1owJzIiSvcEbD/HWRjgkyxil80rYRGJmI57RoXZa6jI2N8QcNELLK7oHytfmhw== X-Received: by 2002:a17:902:d2c7:b0:2a0:e94e:5df6 with SMTP id d9443c01a7336-2a0e94e5ebemr19780285ad.50.1765770164019; Sun, 14 Dec 2025 19:42:44 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-34abe23a207sm3420562a91.1.2025.12.14.19.42.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Dec 2025 19:42:42 -0800 (PST) From: Donglin Peng To: rostedt@goodmis.org Cc: mhiramat@kernel.org, linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, pengdonglin , Xiaoqin Zhang Subject: [PATCH v4 1/3] ftrace: Build trace_btf.c when CONFIG_DEBUG_INFO_BTF is enabled Date: Mon, 15 Dec 2025 11:41:51 +0800 Message-Id: <20251215034153.2367756-2-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251215034153.2367756-1-dolinux.peng@gmail.com> References: <20251215034153.2367756-1-dolinux.peng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: pengdonglin The trace_btf.c file provides BTF helper functions used by the ftrace subsystem. This change makes its compilation solely dependent on CONFIG_DEBUG_INFO_BTF, allowing features like funcgraph-retval to also utilize these helpers. Additionally, the redundant dependency on CONFIG_PROBE_EVENTS_BTF_ARGS is removed, as CONFIG_DEBUG_INFO_BTF already depends on CONFIG_BPF_SYSCALL. Cc: Steven Rostedt (Google) Cc: Masami Hiramatsu Cc: Xiaoqin Zhang Signed-off-by: pengdonglin --- kernel/trace/Kconfig | 2 +- kernel/trace/Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index e1214b9dc990..653c1fcefa4c 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -755,7 +755,7 @@ config FPROBE_EVENTS config PROBE_EVENTS_BTF_ARGS depends on HAVE_FUNCTION_ARG_ACCESS_API depends on FPROBE_EVENTS || KPROBE_EVENTS - depends on DEBUG_INFO_BTF && BPF_SYSCALL + depends on DEBUG_INFO_BTF bool "Support BTF function arguments for probe events" default y help diff --git a/kernel/trace/Makefile b/kernel/trace/Makefile index fc5dcc888e13..6c4bf5a6c4f3 100644 --- a/kernel/trace/Makefile +++ b/kernel/trace/Makefile @@ -116,7 +116,7 @@ obj-$(CONFIG_KGDB_KDB) +=3D trace_kdb.o endif obj-$(CONFIG_DYNAMIC_EVENTS) +=3D trace_dynevent.o obj-$(CONFIG_PROBE_EVENTS) +=3D trace_probe.o -obj-$(CONFIG_PROBE_EVENTS_BTF_ARGS) +=3D trace_btf.o +obj-$(CONFIG_DEBUG_INFO_BTF) +=3D trace_btf.o obj-$(CONFIG_UPROBE_EVENTS) +=3D trace_uprobe.o obj-$(CONFIG_BOOTTIME_TRACING) +=3D trace_boot.o obj-$(CONFIG_FTRACE_RECORD_RECURSION) +=3D trace_recursion_record.o --=20 2.34.1 From nobody Thu Dec 18 13:33:11 2025 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A9BDC29E114 for ; Mon, 15 Dec 2025 03:42:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765770169; cv=none; b=qp2Y6rhy9RLO3g7YCmo6yg4bMk3qdgP5chYrnlTQMGEuPNN0gDR2xRi7n4Z2nVU5sZPrHdlP5aNI6IYqm0/CGtUhY1OqTsb+N+xOkV13AfY0niLfeucAq3WzXotespnBA1x/gm4ZxZrU+9JYDkjuu1pNAgqgPCcSfFZDjmWYrsE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765770169; c=relaxed/simple; bh=x1EZDXwpBtHEAJwiUMWKxwGt4FVIKlZiGWUwN6Sc5P8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JNlzsFXaQn+LFQt15UjZT/ygwLVDSk06B0yvQH+urvsS2MIHUaDkPlXoLNf8wm2u79XEmf8pJKfR828B/clZ9sJrWExRS/JiDQjvrf+SeqIhbNKRFq5goHOX+uGpP+q7z5A3gSt/wH9Sq6ckGTwjZh7dQy1JKUN9zB30ZRYmu44= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=YhR+R9aq; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YhR+R9aq" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-34c21417781so1977872a91.3 for ; Sun, 14 Dec 2025 19:42:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765770167; x=1766374967; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xd9WY+m6+WX7Pur4QkVKIOZEntT/jaxLPiAk2vWokIQ=; b=YhR+R9aqtgUfLjGkxgRTG7DPOOLpHV40plmzAtZV3ana3IVYK39KV/JNS4y3mb+MBI uHdQ187XCA0HOhR3eBMlKbT7m2SC2bhO+uocHElkoN+YJCMvYUHfuz5nxFJgU9mYQAsK TwOYw9mX/JXvb0zswLw+e770OiXRKDkRQp3Pbbup0emOlVjx90UAB4mt8ylTyvKb9t2o Q2WPZtiTGVJf7hJS1RI3Vq6P1M6zUS65KDPnH/Lpg0B0GtjJaoEeD+25BAVXhMSHVCur UsJPHIj3Ce0LE3lKd1StaHxiqm3pnsmcxcCwWbp7oiHge42ADaOUEnUCpyhYGDwcWsu2 jakw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765770167; x=1766374967; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=xd9WY+m6+WX7Pur4QkVKIOZEntT/jaxLPiAk2vWokIQ=; b=MvPPe5uLngCG9igg4fr/NxQM3rmWPPE7zeRuUeR/TkGMhA6xd96BKPXiDmrToeJp6s e8gZVpiAXu/nbaqYmS9StxRH6FbjqkNm5P+mA142//TaiPe0NcdvAxkn7JJhnoqFZRMz KPNCExnYNZSdp8cm7E++cb/iFGyBUuAYnRmQNvm+kGER9HTgiAy4Z1cDXgppm2wPqY+P zSQiN+Kw+42NoB6oxYZPn0tu8RFO5dAqekhPjxmZBaj5A0ArDhVrg54jXSNmYn0HxydZ JirqacjxNjLYUdbV+s34ZTHdCz6AsukfhSoMkDorK2f0oG/X+PLTbEGt5Ne30hXLkA/L Uxgg== X-Forwarded-Encrypted: i=1; AJvYcCU0qEO4+P3DuX8LKIu7W79dK/v949OYEaAbGT9HGwY7/PwmKjFoq3RqOLBDmiZEuuuk/JkU3NuphDU3xcM=@vger.kernel.org X-Gm-Message-State: AOJu0YxZxXQJ14Shae2eb7gIQVl+RKnWkUTTDfXkWs0l08HEKY8aI/0P PEQ4o+CJQ14K33DkpJ/eyfpL60rGxG9FBVRgruGt5gaQpcFCn+lZjzLL X-Gm-Gg: AY/fxX4dSASAuSE63MJkVvood7uRV4dn7kYf2V+Ng3W5wdkCPVdmk6ZPvvjbVxmRCQz RLxhoKJHpR2GUlJslg3U9rrb2t5lbKYmV1VsWRUJJ4tiMHcf6M92+zzEhZuqJiWSFQASGJKZAVM DnetuzwaZBTDfRYkSily0lCSkZAxvy5mXFYi/XlU5KKoHybXhzeOme2uG3wzvxAh521PLcY6nd2 LmpIUTe40fsTAKFPe0dcbWZ5Ak6MLigqTJ8M8yRuey0cCU38GT1EMAzfQJcCyFvT5/3zRSetz/u UQryb0mwcxS2jLCdrS7GuDp0fH82YG1PaqRRv00sIngqR93UNR1HrTUzXNrVh84ucihmwVHPNzq JyV/nTdFlOs8puWHp72qZizt2U8GWDUEmjlSYkrAobEb4xcLZA+WsH7JKfTQGZR/Pjwoj4FKwET /4wLf5gVM3N27+Yhn6mOokHyEJiJc= X-Google-Smtp-Source: AGHT+IFEKMAm2xwpvhAT31FkoiRa3afcZbkdDGilaT6v6Av0cg8Jp7vcjHFm1RUYNGN5u6kMK6k+cA== X-Received: by 2002:a17:90b:2d4b:b0:349:2936:7f4 with SMTP id 98e67ed59e1d1-34abd786b6amr7848522a91.32.1765770166936; Sun, 14 Dec 2025 19:42:46 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-34abe23a207sm3420562a91.1.2025.12.14.19.42.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Dec 2025 19:42:45 -0800 (PST) From: Donglin Peng To: rostedt@goodmis.org Cc: mhiramat@kernel.org, linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, pengdonglin , Xiaoqin Zhang Subject: [PATCH v4 2/3] fgraph: Enhance funcgraph-retval with BTF-based type-aware output Date: Mon, 15 Dec 2025 11:41:52 +0800 Message-Id: <20251215034153.2367756-3-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251215034153.2367756-1-dolinux.peng@gmail.com> References: <20251215034153.2367756-1-dolinux.peng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: pengdonglin The current funcgraph-retval implementation suffers from two accuracy issues: 1. Void-returning functions still print a return value, creating misleading noise in the trace output. 2. For functions returning narrower types (e.g., char, short), the displayed value can be incorrect because high bits of the register may contain undefined data. This patch addresses both problems by leveraging BTF to obtain the exact return type of each traced kernel function. The key changes are: 1. Void function filtering: Functions with void return type no longer display any return value in the trace output, eliminating unnecessary clutter. 2. Type-aware value formatting: The return value is now properly truncated to match the actual width of the return type before being displayed. Additionally, the value is formatted according to its type for better human readability. Here is an output comparison: Before: # perf ftrace -G vfs_read --graph-opts retval ... 1) | touch_atime() { 1) | atime_needs_update() { 1) 0.069 us | make_vfsuid(); /* ret=3D0x0 */ 1) 0.067 us | make_vfsgid(); /* ret=3D0x0 */ 1) | current_time() { 1) 0.197 us | ktime_get_coarse_real_ts64_mg(); /* ret=3D0x187= f886aec3ed6f5 */ 1) 0.352 us | } /* current_time ret=3D0x69380753 */ 1) 0.792 us | } /* atime_needs_update ret=3D0x0 */ 1) 0.937 us | } /* touch_atime ret=3D0x0 */ After: # perf ftrace -G vfs_read --graph-opts retval ... 2) | touch_atime() { 2) | atime_needs_update() { 2) 0.070 us | make_vfsuid(); /* ret=3D0x0 */ 2) 0.070 us | make_vfsgid(); /* ret=3D0x0 */ 2) | current_time() { 2) 0.162 us | ktime_get_coarse_real_ts64_mg(); 2) 0.312 us | } /* current_time ret=3D0x69380649(trunc) */ 2) 0.753 us | } /* atime_needs_update ret=3Dfalse */ 2) 0.899 us | } /* touch_atime */ Cc: Steven Rostedt (Google) Cc: Masami Hiramatsu Cc: Xiaoqin Zhang Signed-off-by: pengdonglin --- kernel/trace/trace_functions_graph.c | 124 ++++++++++++++++++++++++--- 1 file changed, 111 insertions(+), 13 deletions(-) diff --git a/kernel/trace/trace_functions_graph.c b/kernel/trace/trace_func= tions_graph.c index 17c75cf2348e..46b66b1cfc16 100644 --- a/kernel/trace/trace_functions_graph.c +++ b/kernel/trace/trace_functions_graph.c @@ -15,6 +15,7 @@ =20 #include "trace.h" #include "trace_output.h" +#include "trace_btf.h" =20 /* When set, irq functions might be ignored */ static int ftrace_graph_skip_irqs; @@ -120,6 +121,13 @@ enum { FLAGS_FILL_END =3D 3 << TRACE_GRAPH_PRINT_FILL_SHIFT, }; =20 +enum { + RETVAL_FMT_HEX =3D BIT(0), + RETVAL_FMT_DEC =3D BIT(1), + RETVAL_FMT_BOOL =3D BIT(2), + RETVAL_FMT_TRUNC =3D BIT(3), +}; + static void print_graph_duration(struct trace_array *tr, unsigned long long duration, struct trace_seq *s, u32 flags); @@ -865,6 +873,73 @@ static void print_graph_retaddr(struct trace_seq *s, s= truct fgraph_retaddr_ent_e =20 #if defined(CONFIG_FUNCTION_GRAPH_RETVAL) || defined(CONFIG_FUNCTION_GRAPH= _RETADDR) =20 +static void trim_retval(unsigned long func, unsigned long *retval, bool *p= rint_retval, + int *fmt) +{ + const struct btf_type *t; + char name[KSYM_NAME_LEN]; + struct btf *btf; + u32 v, msb; + int kind; + + if (!IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) + return; + + if (lookup_symbol_name(func, name)) + return; + + t =3D btf_find_func_proto(name, &btf); + if (IS_ERR_OR_NULL(t)) + return; + + t =3D btf_type_skip_modifiers(btf, t->type, NULL); + kind =3D t ? BTF_INFO_KIND(t->info) : BTF_KIND_UNKN; + switch (kind) { + case BTF_KIND_UNKN: + *print_retval =3D false; + break; + case BTF_KIND_STRUCT: + case BTF_KIND_UNION: + case BTF_KIND_ENUM: + case BTF_KIND_ENUM64: + if (kind =3D=3D BTF_KIND_STRUCT || kind =3D=3D BTF_KIND_UNION) + *fmt =3D RETVAL_FMT_HEX; + else + *fmt =3D RETVAL_FMT_DEC; + + if (t->size > sizeof(unsigned long)) { + *fmt |=3D RETVAL_FMT_TRUNC; + } else { + msb =3D BITS_PER_BYTE * t->size - 1; + *retval &=3D GENMASK(msb, 0); + } + break; + case BTF_KIND_INT: + v =3D *(u32 *)(t + 1); + if (BTF_INT_ENCODING(v) =3D=3D BTF_INT_BOOL) { + *fmt =3D RETVAL_FMT_BOOL; + msb =3D 0; + } else { + if (BTF_INT_ENCODING(v) =3D=3D BTF_INT_SIGNED) + *fmt =3D RETVAL_FMT_DEC; + else + *fmt =3D RETVAL_FMT_HEX; + + if (t->size > sizeof(unsigned long)) { + *fmt |=3D RETVAL_FMT_TRUNC; + msb =3D BITS_PER_LONG - 1; + } else { + msb =3D BTF_INT_BITS(v) - 1; + } + } + *retval &=3D GENMASK(msb, 0); + break; + default: + *fmt =3D RETVAL_FMT_HEX; + break; + } +} + static void print_graph_retval(struct trace_seq *s, struct ftrace_graph_en= t_entry *entry, struct ftrace_graph_ret *graph_ret, void *func, u32 opt_flags, u32 trace_flags, int args_size) @@ -873,7 +948,7 @@ static void print_graph_retval(struct trace_seq *s, str= uct ftrace_graph_ent_entr unsigned long retval =3D 0; bool print_retaddr =3D false; bool print_retval =3D false; - bool hex_format =3D !!(opt_flags & TRACE_GRAPH_PRINT_RETVAL_HEX); + int retval_fmt =3D 0; =20 #ifdef CONFIG_FUNCTION_GRAPH_RETVAL retval =3D graph_ret->retval; @@ -884,17 +959,35 @@ static void print_graph_retval(struct trace_seq *s, s= truct ftrace_graph_ent_entr print_retaddr =3D !!(opt_flags & TRACE_GRAPH_PRINT_RETADDR); #endif =20 - if (print_retval && retval && !hex_format) { - /* Check if the return value matches the negative format */ - if (IS_ENABLED(CONFIG_64BIT) && (retval & BIT(31)) && - (((u64)retval) >> 32) =3D=3D 0) { - err_code =3D sign_extend64(retval, 31); - } else { - err_code =3D retval; + if (print_retval) { + int fmt =3D RETVAL_FMT_HEX; + + trim_retval((unsigned long)func, &retval, &print_retval, &fmt); + if (print_retval) { + if (opt_flags & TRACE_GRAPH_PRINT_RETVAL_HEX) + retval_fmt =3D RETVAL_FMT_HEX; + + if (retval && retval_fmt !=3D RETVAL_FMT_HEX) { + /* Check if the return value matches the negative format */ + if (IS_ENABLED(CONFIG_64BIT) && (retval & BIT(31)) && + (((u64)retval) >> 32) =3D=3D 0) { + err_code =3D sign_extend64(retval, 31); + } else { + err_code =3D retval; + } + + if (!IS_ERR_VALUE(err_code)) + err_code =3D 0; + } + + if (retval_fmt =3D=3D RETVAL_FMT_HEX) { + retval_fmt |=3D (fmt & RETVAL_FMT_TRUNC); + } else { + if (err_code && fmt & RETVAL_FMT_HEX) + fmt =3D (fmt & ~RETVAL_FMT_HEX) | RETVAL_FMT_DEC; + retval_fmt =3D fmt; + } } - - if (!IS_ERR_VALUE(err_code)) - err_code =3D 0; } =20 if (entry) { @@ -921,10 +1014,15 @@ static void print_graph_retval(struct trace_seq *s, = struct ftrace_graph_ent_entr trace_flags, false); =20 if (print_retval) { - if (hex_format || (err_code =3D=3D 0)) + if (retval_fmt & RETVAL_FMT_HEX) trace_seq_printf(s, " ret=3D0x%lx", retval); + else if (retval_fmt & RETVAL_FMT_BOOL) + trace_seq_printf(s, " ret=3D%s", retval ? "true" : "false"); else - trace_seq_printf(s, " ret=3D%ld", err_code); + trace_seq_printf(s, " ret=3D%ld", err_code ?: retval); + + if (retval_fmt & RETVAL_FMT_TRUNC) + trace_seq_printf(s, "(trunc)"); } =20 if (!entry || print_retval || print_retaddr) --=20 2.34.1 From nobody Thu Dec 18 13:33:11 2025 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5D4C8299AAC for ; Mon, 15 Dec 2025 03:42:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765770174; cv=none; b=QrV5M9jFKu6XEx7saSPmgxm+k6uBYylfCZAArJ3hB8kDKDnDLmuOjLoYatuNgFTFlyGXfAX3w9j1jF5KDncRm19G52yTYXYf2mXz3UdOlhYU81FaRO3V3tDcQ9/0IRzHktnC2a0vyXHTU6v0E4Os/ncwoVgorWcW6e4wGXHdSIk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765770174; c=relaxed/simple; bh=l38H3vsBTdrEgWUMkEZiNVCzoEObSKrQ0jGhEvBuLdE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XKqDnFLVy0WmaTXuV8BFueU87XsQnfDx0eh7AZWQ+3F+ZVMsTEaN+jOadepOH5ejrBZy9tb+RPitQ+YPldOT+J4MX0cDzy3ys8BxHuoFA4jZ7BrcX9TO112EZCs8DYnuQWxgHtlDFdm+VfiHvKDvfpCR22Goh9oaUQvvDpyJxLY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=aIAfC/8w; arc=none smtp.client-ip=209.85.216.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aIAfC/8w" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-34c708702dfso709420a91.1 for ; Sun, 14 Dec 2025 19:42:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765770171; x=1766374971; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zodsawp9MT0zFDRA0UB83iWzeQZFAUerG70qGUpiRMU=; b=aIAfC/8w4T/cAr7zcvewSyDbm3lxXGhQbtW3U2NuXS6TqiZ7A248Rde2tl40fliGPh CKvYNGCPUrgUvh+DbWEsW9mBPj2iutjpOazoqlRHPuy54znqfxQuhZffZFYQTfywVlWP OhBYcAh10DXsU2SOLlHd0g5PnXOTc7yyQQSJ6nHvNW9PCX5tVacq2shA17kIPUJtvzhI ftZLeY1/SoeUq/ptlJoT4/1dBhU0MJDyEM8Z2gpdWiP3GyQKTXUzmcpUK8Sfc3DU/sja aS6jyaI9IWu6d8SEkfNsbsTj0NP/5qIxMHZwDr7FDp/rIPmv5wvi3+Jv+sjQXD3yQ1z8 o1MA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765770171; x=1766374971; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=zodsawp9MT0zFDRA0UB83iWzeQZFAUerG70qGUpiRMU=; b=Gt+Wo8Kqhd39kpsyRrUtw3n+c2j0PvAzEYB7CEMEgFv5OuCepzVPRD5XZ2vRDoE6ZI gzhCj0u+jyZW3R7HXs3zXezhkYpQol+Ofs4usE4ClsBrxT3+2pN0GtC64RU22mZDxdbG /yUFQCJe7mm9t+QNJDheTBMJrE7/PrfEe9jaI8hkqyLaGsj2/uOrmf00moGd6yArwgC6 iQQ4IdeTZYf3+SvtsGhdpgZ48WaftWjTRu7/c31/XLoad7nybkGpjUhi7K/46tCMDPxR mdB+6JiJzFjycCC/0G9vHv4gsdmKGf11CGMA+f6D7kd2ZcU76O8bKZA2s5iXYtsCpLI/ s+xA== X-Forwarded-Encrypted: i=1; AJvYcCXYZJYHJw5bWYeiSnej0TJ+1uN0oA0s22KQ+ZaWybGRZPbxSItgBYtVE2z8rE2fqq3JEKTKqGoUmjY+UbQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyM9ovYKt4ER+RPh1prA2UxRMYxPVhjoNU3bkoRfXpwWsUb2hEm x7FDXrmE2Ye1TZijqQhou8+g/QMidnknrc73u3Zjgz6OXlJA3mGfvHAj X-Gm-Gg: AY/fxX4ONa5SzvjqBVrSvR2CpSefqjbKOwahYsSBT/5sz+L046f1liWoqhBCHCAmuS1 DvDEloQpcE42Nn3XRWKVsqYz4UZa9JEh52/nznK2vGewe5JJumIH1PinJGEspghK9AFabQ7JB8Y HV7oCZvZHMe8GA/LQQqYTYdblRJTDlfhamkA92JpoPoeoKy+fj2PNkyK4G+9FKKjhrxIZdTMKcA yxU5Akl0HtrhG8A9tXONQMJzAxD4NKdlThGPy7P+BfWJTZXLdtU0pa4usN/2cZi8uzpjiwv4Hqn dYxmqIDNhbGEZhSj20di9+8bDZVAzxdEu0TvPBOBeSN6QtpjKfUf1k7zXRGLAtdtMvcpCTUweoa VJdCBBbXSuxS0zkEbhEDLsiCObD/Zasp3YjsHj/r/1y+6Jh45O5RW23rGTeUsGsyXMt0PuaFQgm AdUw7TCOaB5k5PPTdG4kuRhQKntFw= X-Google-Smtp-Source: AGHT+IFV0mk8C23ZddCTFMGBb6EW5vJkKKe/Id/7ooCo0G5hzTqwU+bujzLDeHuF/5cfsXslbYpfpg== X-Received: by 2002:a17:90b:3c4f:b0:340:c179:3666 with SMTP id 98e67ed59e1d1-34abd6c02cfmr7561102a91.8.1765770169907; Sun, 14 Dec 2025 19:42:49 -0800 (PST) Received: from pengdl-pc.mioffice.cn ([43.224.245.249]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-34abe23a207sm3420562a91.1.2025.12.14.19.42.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Dec 2025 19:42:48 -0800 (PST) From: Donglin Peng To: rostedt@goodmis.org Cc: mhiramat@kernel.org, linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, pengdonglin , Xiaoqin Zhang Subject: [PATCH v4 3/3] tracing: Update funcgraph-retval documentation Date: Mon, 15 Dec 2025 11:41:53 +0800 Message-Id: <20251215034153.2367756-4-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251215034153.2367756-1-dolinux.peng@gmail.com> References: <20251215034153.2367756-1-dolinux.peng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: pengdonglin The existing documentation for funcgraph-retval is outdated and partially incorrect, as it describes limitations that have now been resolved. Recent changes (e.g., using BTF to obtain function return types) have addressed key issues: 1. Return values are now printed only for non-void functions. 2. Values are trimmed to the correct width of the return type, avoiding garbage data from high bits. Cc: Steven Rostedt (Google) Cc: Masami Hiramatsu Cc: Xiaoqin Zhang Signed-off-by: pengdonglin --- Documentation/trace/ftrace.rst | 88 +++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 38 deletions(-) diff --git a/Documentation/trace/ftrace.rst b/Documentation/trace/ftrace.rst index d1f313a5f4ad..b231e80e6a4f 100644 --- a/Documentation/trace/ftrace.rst +++ b/Documentation/trace/ftrace.rst @@ -1454,6 +1454,10 @@ Options for function_graph tracer: printed in hexadecimal format. By default, this option is off. =20 + funcgraph-retaddr + When set, the return address will always be printed. + By default, this option is off. + sleep-time When running function graph tracer, to include the time a task schedules out in its function. @@ -2800,7 +2804,7 @@ It is default disabled. 0) 2.861 us | } /* putname() */ =20 The return value of each traced function can be displayed after -an equal sign "=3D". When encountering system call failures, it +an equal sign "ret =3D". When encountering system call failures, it can be very helpful to quickly locate the function that first returns an error code. =20 @@ -2810,16 +2814,16 @@ returns an error code. Example with funcgraph-retval:: =20 1) | cgroup_migrate() { - 1) 0.651 us | cgroup_migrate_add_task(); /* =3D 0xffff93fcfd= 346c00 */ + 1) 0.651 us | cgroup_migrate_add_task(); /* ret=3D0xffff93fc= fd346c00 */ 1) | cgroup_migrate_execute() { 1) | cpu_cgroup_can_attach() { 1) | cgroup_taskset_first() { - 1) 0.732 us | cgroup_taskset_next(); /* =3D 0xffff93fc= 8fb20000 */ - 1) 1.232 us | } /* cgroup_taskset_first =3D 0xffff93fc8f= b20000 */ - 1) 0.380 us | sched_rt_can_attach(); /* =3D 0x0 */ - 1) 2.335 us | } /* cpu_cgroup_can_attach =3D -22 */ - 1) 4.369 us | } /* cgroup_migrate_execute =3D -22 */ - 1) 7.143 us | } /* cgroup_migrate =3D -22 */ + 1) 0.732 us | cgroup_taskset_next(); /* ret=3D0xffff93= fc8fb20000 */ + 1) 1.232 us | } /* cgroup_taskset_first ret=3D0xffff93fc= 8fb20000 */ + 1) 0.380 us | sched_rt_can_attach(); /* ret=3D0x0 */ + 1) 2.335 us | } /* cpu_cgroup_can_attach ret=3D-22 */ + 1) 4.369 us | } /* cgroup_migrate_execute ret=3D-22 */ + 1) 7.143 us | } /* cgroup_migrate ret=3D-22 */ =20 The above example shows that the function cpu_cgroup_can_attach returned the error code -22 firstly, then we can read the code @@ -2836,37 +2840,41 @@ printed in hexadecimal format. Example with funcgraph-retval-hex:: =20 1) | cgroup_migrate() { - 1) 0.651 us | cgroup_migrate_add_task(); /* =3D 0xffff93fc= fd346c00 */ + 1) 0.651 us | cgroup_migrate_add_task(); /* ret=3D0xffff93= fcfd346c00 */ 1) | cgroup_migrate_execute() { 1) | cpu_cgroup_can_attach() { 1) | cgroup_taskset_first() { - 1) 0.732 us | cgroup_taskset_next(); /* =3D 0xffff93= fc8fb20000 */ - 1) 1.232 us | } /* cgroup_taskset_first =3D 0xffff93fc= 8fb20000 */ - 1) 0.380 us | sched_rt_can_attach(); /* =3D 0x0 */ - 1) 2.335 us | } /* cpu_cgroup_can_attach =3D 0xffffffea = */ - 1) 4.369 us | } /* cgroup_migrate_execute =3D 0xffffffea */ - 1) 7.143 us | } /* cgroup_migrate =3D 0xffffffea */ - -At present, there are some limitations when using the funcgraph-retval -option, and these limitations will be eliminated in the future: - -- Even if the function return type is void, a return value will still - be printed, and you can just ignore it. - -- Even if return values are stored in multiple registers, only the - value contained in the first register will be recorded and printed. - To illustrate, in the x86 architecture, eax and edx are used to store - a 64-bit return value, with the lower 32 bits saved in eax and the - upper 32 bits saved in edx. However, only the value stored in eax - will be recorded and printed. - -- In certain procedure call standards, such as arm64's AAPCS64, when a - type is smaller than a GPR, it is the responsibility of the consumer - to perform the narrowing, and the upper bits may contain UNKNOWN values. - Therefore, it is advisable to check the code for such cases. For instanc= e, - when using a u8 in a 64-bit GPR, bits [63:8] may contain arbitrary value= s, - especially when larger types are truncated, whether explicitly or implic= itly. - Here are some specific cases to illustrate this point: + 1) 0.732 us | cgroup_taskset_next(); /* ret=3D0xffff= 93fc8fb20000 */ + 1) 1.232 us | } /* cgroup_taskset_first ret=3D0xffff93= fc8fb20000 */ + 1) 0.380 us | sched_rt_can_attach(); /* ret=3D0x0 */ + 1) 2.335 us | } /* cpu_cgroup_can_attach ret=3D0xffffffe= a */ + 1) 4.369 us | } /* cgroup_migrate_execute ret=3D0xffffffea= */ + 1) 7.143 us | } /* cgroup_migrate ret=3D0xffffffea */ + +Note that there are some limitations when using the funcgraph-retval +option: + +- If CONFIG_DEBUG_INFO_BTF is disabled (n), a return value is printed even= for + functions with a void return type. When CONFIG_DEBUG_INFO_BTF is enabled= (y), + the return value is printed only for non-void functions. + +- If a return value occupies multiple registers, only the value in the fir= st + register is recorded and printed. For example, on the x86 architecture, a + 64-bit return value is stored across eax (lower 32 bits) and edx (upper = 32 bits), + but only the contents of eax are captured. If CONFIG_DEBUG_INFO_BTF is e= nabled, + the suffix "(trunc)" is appended to the printed value to indicate that t= he + output may be truncated because high-order register contents are omitted. + +- Under certain procedure-call standards (e.g., arm64's AAPCS64), when the= return + type is smaller than a general-purpose register (GPR), the caller is res= ponsible + for narrowing the value; the upper bits of the register may contain unde= fined data. + For instance, when a u8 is returned in 64-bit GPR, bits [63:8] can hold = arbitrary + values, especially when larger types are truncated (explicitly or implic= itly). It + is therefore advisable to inspect the code in such cases. If CONFIG_DEBU= G_INFO_BTF + is enabled (y), the return value is automatically trimmed to the width o= f the return + type. + + The following examples illustrate the behavior: =20 **Case One**: =20 @@ -2885,7 +2893,9 @@ option, and these limitations will be eliminated in t= he future: RET =20 If you pass 0x123456789abcdef to this function and want to narrow it, - it may be recorded as 0x123456789abcdef instead of 0xef. + it may be recorded as 0x123456789abcdef instead of 0xef. When + CONFIG_DEBUG_INFO_BTF is enabled, the value will be correctly truncated + to 0xef based on the size constraints of the u8 type. =20 **Case Two**: =20 @@ -2910,7 +2920,9 @@ option, and these limitations will be eliminated in t= he future: RET =20 When passing 0x2_0000_0000 to it, the return value may be recorded as - 0x2_0000_0000 instead of 0. + 0x2_0000_0000 instead of 0. When CONFIG_DEBUG_INFO_BTF is enabled, the + value will be correctly truncated to 0 based on the size constraints of + the int type. =20 You can put some comments on specific functions by using trace_printk() For example, if you want to put a comment inside --=20 2.34.1