From nobody Wed Jan 22 11:56:41 2025 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 56B9E1EEA3C; Wed, 22 Jan 2025 02:32:22 +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=1737513142; cv=none; b=VSeqn8A0clec+LDHFQHVeyydCNknn9cch/uQGwuAUb6b6/YkRmye5Esczu7SNoo7u6ALZHy3PMDK5jbRS6kYipT+VRenYE60ywaYGGdFgzkjynrudYBlFHp7yN0fijcqNYlGJvityGqkfR16YV3SxgUi1ALODczxW3IzlnC/x9E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737513142; c=relaxed/simple; bh=GD6lqqKJhzejsQBKB5JdzkZAwfNOtyH5yqbgfZgTnVg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RWlAhiOGvVB+4Yq67pgU1emMMxhV66JKkyKBWozUN8EIdFqjmVEwz02nYmfA9FahdQCSPl8ScCaCJp+Zc68hLCc/GMpyQsrr2kigt2OtqNe0EgxQPenOKgtQ8P5M0MKCZ8M0PKZDATuOWGmPK1qR9cYAVfwuZ755lhxRlTeECCc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=byQelMJ8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="byQelMJ8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5DE48C4CEE5; Wed, 22 Jan 2025 02:32:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737513142; bh=GD6lqqKJhzejsQBKB5JdzkZAwfNOtyH5yqbgfZgTnVg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=byQelMJ8J6c1cMq41ntcn63Eir36brW/eyI4TAb+TjIH6oLbgnflsZTs/60GXulKs 5yarE7zAP/sHT8J/MjdtdTFyrIR1Vw5/BusCBQE3RV6ePLTbkhd6TVDF6wUMNR2JkA 4R2xn/0/vyxpY09EJfy1I7Cj8kPYjsaCRbHiBTnGUIj6cJHMWjC2az2SDrZv0xa7zI bKCbwjMqMGgqUMpRVuhtH7rp/vSOBvcAhgltuR3WhdSea042vvJGHN31AyrAWBGBFd +NiXA+RjK+tSlxHBIx/eN+UfjkKjVpBZblHYfAYQGWPhPsbKT/fAW/aV7w7P/xsZMG 9X3/grvMFDIBg== From: Josh Poimboeuf To: x86@kernel.org Cc: Peter Zijlstra , Steven Rostedt , Ingo Molnar , Arnaldo Carvalho de Melo , linux-kernel@vger.kernel.org, Indu Bhagat , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, Mark Brown , linux-toolchains@vger.kernel.org, Jordan Rome , Sam James , linux-trace-kernel@vger.kernel.org, Andrii Nakryiko , Jens Remus , Mathieu Desnoyers , Florian Weimer , Andy Lutomirski , Masami Hiramatsu , Weinan Liu Subject: [PATCH v4 29/39] unwind_user/deferred: Add unwind cache Date: Tue, 21 Jan 2025 18:31:21 -0800 Message-ID: <51855c0902486060cd6e1ccc6b22fd092a2e676d.1737511963.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: References: 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" Cache the results of the unwind to ensure the unwind is only performed once, even when called by multiple tracers. Signed-off-by: Josh Poimboeuf --- include/linux/unwind_deferred_types.h | 8 +++++++- kernel/unwind/deferred.c | 26 ++++++++++++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/include/linux/unwind_deferred_types.h b/include/linux/unwind_d= eferred_types.h index 9749824aea09..6f71a06329fb 100644 --- a/include/linux/unwind_deferred_types.h +++ b/include/linux/unwind_deferred_types.h @@ -2,8 +2,14 @@ #ifndef _LINUX_UNWIND_USER_DEFERRED_TYPES_H #define _LINUX_UNWIND_USER_DEFERRED_TYPES_H =20 -struct unwind_task_info { +struct unwind_cache { unsigned long *entries; + unsigned int nr_entries; + u64 cookie; +}; + +struct unwind_task_info { + struct unwind_cache cache; u64 cookie; }; =20 diff --git a/kernel/unwind/deferred.c b/kernel/unwind/deferred.c index f0dbe4069247..2f38055cce48 100644 --- a/kernel/unwind/deferred.c +++ b/kernel/unwind/deferred.c @@ -56,6 +56,7 @@ static void unwind_deferred_task_work(struct callback_hea= d *head) { struct unwind_work *work =3D container_of(head, struct unwind_work, work); struct unwind_task_info *info =3D ¤t->unwind_info; + struct unwind_cache *cache =3D &info->cache; struct unwind_stacktrace trace; u64 cookie; =20 @@ -73,17 +74,30 @@ static void unwind_deferred_task_work(struct callback_h= ead *head) if (!current->mm) goto do_callback; =20 - if (!info->entries) { - info->entries =3D kmalloc(UNWIND_MAX_ENTRIES * sizeof(long), - GFP_KERNEL); - if (!info->entries) + if (!cache->entries) { + cache->entries =3D kmalloc(UNWIND_MAX_ENTRIES * sizeof(long), + GFP_KERNEL); + if (!cache->entries) goto do_callback; } =20 - trace.entries =3D info->entries; + trace.entries =3D cache->entries; + + if (cookie =3D=3D cache->cookie) { + /* + * The user stack has already been previously unwound in this + * entry context. Skip the unwind and use the cache. + */ + trace.nr =3D cache->nr_entries; + goto do_callback; + } + trace.nr =3D 0; unwind_user(&trace, UNWIND_MAX_ENTRIES); =20 + cache->cookie =3D cookie; + cache->nr_entries =3D trace.nr; + do_callback: work->func(work, &trace, cookie); work->pending =3D 0; @@ -174,5 +188,5 @@ void unwind_task_free(struct task_struct *task) { struct unwind_task_info *info =3D &task->unwind_info; =20 - kfree(info->entries); + kfree(info->cache.entries); } --=20 2.48.1