From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796179173; b=01Z//bG1E6cMpI0OFHEiYdAaf/eMuN/mZGo5X1V7QHbuCpOzUmo1nrRat0HYshgr1y2v PCtUdJ7af3rHXtK9Wtg5lkIHWfBZDtloiBLqr/hrsukmHcX6JWVyjah+Jc2hMJlAgHkXS dDZDlidK6BVBv9lklPBnhhTka48EcF1gUz4fQu+oCoHIj+wbr9pQFcNjQdxMKx9mfBbWe 8jBOtM6ykbrbP2kwqgKlCaPguq4mmz0DaZgMh9Tz82BUqNrwKiitQlUh3UIK5rDHRNiAJ tsTYVYiC6yKj1clejcpXRioAg13W5a0L0rpsFTBPjJysb6hn3xMUl8CqnTiCXVPejDQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796179173; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=07DNobY33VZaSbTxdp80jhubPI2w45YNR/JgVYA6NIs=; b=iN9bObr17psVRh8TBeXDmapsLIom0La9wEbD4qo+PN/RUQ8hZ6N309Xp9CXZqczsfHl1 c1hce84YvlUWR31MQWraoVErOkJmubc5gUup5y1pTG2tTT1b87Md8xuVvLPn4mNUcU8aT K/Me3m3/dOYGa6CdQShFWXQKwsExxlN0SBH0tCJ7e0Xq2Gs63FHIzDw6ROS1HjPNe/Ths 4pQbFDCO0WCBd0Z2mi87CSMcnElcIZlLA4igLooNlvMxVZi7mX2cusso2axOSyWSSg6mH Z2TF7O9ZNFByTZFpGkHSvSwNBl4bAUIv7GOZvieSBbdXotIW4W+B/lMV5xIAR04smfA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=F2mqJLFT; arc=none smtp.remote-ip=209.85.214.199 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C638060A383D for ; Wed, 2 Dec 2026 02:39:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C638060A383D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=F2mqJLFT Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2f05b674e3eso101044135ad.0 for ; Tue, 01 Dec 2026 18:39:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796179172; x=1796783972; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=07DNobY33VZaSbTxdp80jhubPI2w45YNR/JgVYA6NIs=; b=F2mqJLFTlBi+RiHdOoy22Na4C6dw8beg6+nk7+kLd5lhsl0rxIj2yOODTEQIRFsEzQ EzbiE/xSrV0qQ8R84LdJtgjPK4q6OQjXwaVxLuZw7buYbTF8lZwcjh40EVyLPRSFEprr GSlEEuxERIM0zga69+hS65t4KphGHFAbv34dYhn27p4BN8nPIaYwTlYclhJabW8BZqk+ zxHjgiu/BqCz68Fje7gOASfuZ3fJ2D0vJsfQNcECEljyBwgqtCQDhJtcmI3OEkv708pi a5nIzQGBsv+oJh/sLSJ00jf9DAFPkEDNSZIkWyKKsLH8yawW95ZO1jcn77XmR0ZHzhmU oM2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796179172; x=1796783972; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=07DNobY33VZaSbTxdp80jhubPI2w45YNR/JgVYA6NIs=; b=FGj02/p7s45g7wXA2HDRBTJIHsaFah+DH5Ij/UuO/CV6Z9dVUd71VmNWNQkV0aEb/o DU6IX5gRA9A+R9TD5keOIxhPtwVueU55+7ow+XJFZLmE/cWj4jKxE0pyynYWz/q/+etq bpoREPaHaCF28rlr5cUN1GK9SCvfw/N7cL4YovT4+Ib/qNXe88FFnnLac9+RNvUm4TSI AJBR10HuOvwcvqjAaIkBaiNdHCli6IsIf2MVhORGpQjVqQfJFPmJ+6E/uAPkR8Kl5/Gh rhXybNLsKVLC5utYwaxb3rT8oIBEVtjWXoR9l323J+HfWsT+VqGiQ1CRe5pb7u4nrFHo DjmA== X-Forwarded-Encrypted: i=1; AL+hErFxRuaiy+I6D7b3dX4q8iOms/2abmF+Iq3MhWrUFf/fMhlnSqxhwarKfnygr2VM44DcaXKgg7sg3Hzz@vger.kernel.org X-Gm-Message-State: APuU4Z/m1y1f91MFh+Yc+aCsAANc5vrhHJs3BQj0WjYbRDoB8nLL1rX8 kIBVPzPNYWRJNYgHz9VKg09cYIIE2eR6YPZqgT1RpPmG2/CWJRXAQq0JmAHgcxFzpMv51yWoaqY FdRsEYg0juPFGO1JDlR69fw== X-Received: from plbaz1.prod.google.com ([2002:a17:902:a581:b0:2ef:d0d7:afa3]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c942:b0:2ed:4456:fb2a with SMTP id d9443c01a7336-2f03f1bfa64mr384086535ad.36.1796179171595; Tue, 01 Dec 2026 18:39:31 -0800 (PST) Date: Wed, 2 Dec 2026 02:39:11 +0000 In-Reply-To: <20261202023920.595207-1-jthoughton@google.com> X-Mailing-List: linux-arch@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202023920.595207-1-jthoughton@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202023920.595207-5-jthoughton@google.com> Subject: [PATCH v2 04/12] stacktrace: Don't unwind the user stack without an mm From: James Houghton To: Andrew Morton , Alexander Viro , Arnd Bergmann Cc: Sean Christopherson , Paolo Bonzini , Jim Mattson , Jens Axboe , David Hildenbrand , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Catalin Marinas , Will Deacon , Paul Walmsley , Palmer Dabbelt , Albert Ou , Madhavan Srinivasan , Chris Zankel , Max Filippov , Dinh Nguyen , Simon Schuster , David Sterba , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , KP Singh , Matt Bobrowski , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Steven Rostedt , Masami Hiramatsu , linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-btrfs@vger.kernel.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-riscv@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, io-uring@vger.kernel.org, James Houghton Content-Type: text/plain; charset="UTF-8" stack_trace_save_user() only refuses to unwind the user stack of tasks with PF_KTHREAD set. It used to check for !current->mm, but commit 7e8e6816c649 ("stacktrace: Use PF_KTHREAD to check for kernel threads") switched to PF_KTHREAD, as kernel threads can temporarily use a user mm. However, that doesn't cover all tasks without an mm: - A user task that has already gone through exit_mm(), e.g. when an event fires from exit_files(), exit_task_work() or the final context switch in do_exit(). - PID 1 before it execs init, and usermode helper children before they exec, which are created with user_mode_thread() and don't have PF_KTHREAD set. E.g. booting with trace_options=userstacktrace and an event enabled with trace_event= unwinds PID 1's "user stack" during initcalls. With the ftrace userstacktrace option enabled, the architecture unwinders then read the user stack without an mm. That access does not reliably fail: the user half of the address space belongs to whatever mm happens to be lazily loaded, if any, so the unwinder may read another task's memory. Use is_user_task() instead, which also checks current->mm, as perf already does for the same reason since commit 76ed27608f7d ("perf: sched: Fix perf crash with new is_user_task() helper"). This also stops unwinding the user stack of PF_USER_WORKER threads, which never run in user space, matching perf. Fixes: 7e8e6816c649 ("stacktrace: Use PF_KTHREAD to check for kernel threads") Signed-off-by: James Houghton --- kernel/stacktrace.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kernel/stacktrace.c b/kernel/stacktrace.c index afb3c116da91..f96a5ec12f6c 100644 --- a/kernel/stacktrace.c +++ b/kernel/stacktrace.c @@ -228,8 +228,12 @@ unsigned int stack_trace_save_user(unsigned long *store, unsigned int size) .size = size, }; - /* Trace user stack if not a kernel thread */ - if (current->flags & PF_KTHREAD) + /* + * Trace user stack only if this is a user task that still has an mm, + * i.e. not a kernel thread or user worker, and not a task that has + * already gone through exit_mm(). + */ + if (!is_user_task(current)) return 0; arch_stack_walk_user(consume_entry, &c, task_pt_regs(current)); -- 2.56.1.911.g883d8bcb20-goog From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 57C2C606A68C for ; Wed, 2 Dec 2026 02:39:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=foKMU+ao; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=NtIPYAVS DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Unsubscribe-Post:List-Owner; bh=jM8DqQoMEkXBgh38MJf4mxWR3/Tp62HRvnBZRVp6m2k=; b=foKMU+aoVGlGMh0ueGpW1xSaxX TzigEqF4ktJGxNn6nba9YAx2V4Ns9pH0Xh14QNG9wmV0etwO/gsr0NR6bppThIwNI0Gt9czSkJl22 fbvoY46PvNIAoeYB91A7s8oaJx6GZpAmdxzfNDX6BoKDYOwrHIceLU7RUgF7u+kRdj0xDydVsbEJZ Mejy5gxxardQTNe99rxevIZPuGJoWXRiJtAVFUvvf7oRhJ7GM8S6doxxwiwt2/oO2Pfx8LbdSF5zB teZGcdvHkDwRtELiEYRQFLcgm+KABrP6UzUpD6RlcYD/zcWFvY2tPtVK59atYNdwUdYvPdYDEKKow 0frpxj/w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYaFx-00000009yeK-01cQ; Wed, 02 Dec 2026 02:39:41 +0000 Received: from mail-pl1-x647.google.com ([2607:f8b0:4864:20::647]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYaFu-00000009ya8-218A for linux-riscv@lists.infradead.org; Wed, 02 Dec 2026 02:39:39 +0000 Received: by mail-pl1-x647.google.com with SMTP id d9443c01a7336-2f14314e32eso22229745ad.1 for ; Tue, 01 Dec 2026 18:39:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796179172; x=1796783972; darn=lists.infradead.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=07DNobY33VZaSbTxdp80jhubPI2w45YNR/JgVYA6NIs=; b=NtIPYAVSVfM/RDxm596WAVZc4RGpszwAAkRhMp2l0C8bUPOcXhrR8y36xQxuoiUyDy 2EkD7h35Q6NYRhzKLBKiPYl5cUNN7aiaUHNcpCzGwq/OYkaNuRiZ65q6zsvViI98iRT3 j88VxvoxVm6n3uVifeV16lpifbqtoXXJej+moSzKcCP+HwYs4CLRVXeCV7eFff9p2Lo7 6gbiGiln4zHEmgeNsRwKrTOBkrq1L6YLqs757JkErE1srMQHyWWeRHAdmxo7vpiRLyvW TdPFbWY+XItw6N8iGU7ca0MODYO64bjPpGxNrdXV+MeOuCICL+4wUlrW5eF9Txr0wFDk sfXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796179172; x=1796783972; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=07DNobY33VZaSbTxdp80jhubPI2w45YNR/JgVYA6NIs=; b=eFCZJDzluSZeFHUnUyl/M0f8oS3for6hpQ5co3KliOlp74+qlcYtYFQm7mOTdiGuXl FRqQrws8oFWvLoPg3p0IoCW7GEq3GtxIIhWQU/e1maelCRRegRMvJOibzI6B6zjXNdRj m8JZ1T29SVzkXb1+FUj49qr+n975XO8sfm6CJJGCgg0ZuRv19utdA+cMk3fRxddwwu62 LqmItMrlZ49EoewKaGrswsyczJDVczOHdwp/0ID2+DdvGJ0bAIVK2SuTfUkZzRP19rhd gYSy4Ls/ipHsn9uGJi4sT5UzXuC4SpxnxcHXrz5UGgrE0PyTn7x5XbgXVJ4Q+xGQXO6G A6uw== X-Forwarded-Encrypted: i=1; AL+hErEYslm+l/eRm6Wy8mK/dxduDA1chFoehp0m/jMr3u5ifxYymnYiLhHHRuu0SuttFcqOHpFjtBBZad5CbQ==@lists.infradead.org X-Gm-Message-State: APuU4Z9qG7pguAjqHxLRZiZ2XSNME5dRvi8O/QuqaVMyNonnznlj6ro3 1DpdIwhbeRUlAqG8aECkJ/N1HEMY2E7g51MWGaDHLT4IYzDn3iCkB10rOxoGrB4xtingiOYybCO HzHZdpIl6Sl2ok22IG70clQ== X-Received: from plbaz1.prod.google.com ([2002:a17:902:a581:b0:2ef:d0d7:afa3]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c942:b0:2ed:4456:fb2a with SMTP id d9443c01a7336-2f03f1bfa64mr384086535ad.36.1796179171595; Tue, 01 Dec 2026 18:39:31 -0800 (PST) Date: Wed, 2 Dec 2026 02:39:11 +0000 In-Reply-To: <20261202023920.595207-1-jthoughton@google.com> X-Archived-By: Mime-Version: 1.0 References: <20261202023920.595207-1-jthoughton@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202023920.595207-5-jthoughton@google.com> Subject: [PATCH v2 04/12] stacktrace: Don't unwind the user stack without an mm From: James Houghton To: Andrew Morton , Alexander Viro , Arnd Bergmann Cc: Sean Christopherson , Paolo Bonzini , Jim Mattson , Jens Axboe , David Hildenbrand , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Catalin Marinas , Will Deacon , Paul Walmsley , Palmer Dabbelt , Albert Ou , Madhavan Srinivasan , Chris Zankel , Max Filippov , Dinh Nguyen , Simon Schuster , David Sterba , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , KP Singh , Matt Bobrowski , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Steven Rostedt , Masami Hiramatsu , linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-btrfs@vger.kernel.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-riscv@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, io-uring@vger.kernel.org, James Houghton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261201_183938_641504_89A558D4 X-CRM114-Status: GOOD ( 18.01 ) X-Spam-Score: -9.5 (---------) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" X-Spam-Status: No, score=-16.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 stack_trace_save_user() only refuses to unwind the user stack of tasks with PF_KTHREAD set. It used to check for !current->mm, but commit 7e8e6816c649 ("stacktrace: Use PF_KTHREAD to check for kernel threads") switched to PF_KTHREAD, as kernel threads can temporarily use a user mm. However, that doesn't cover all tasks without an mm: - A user task that has already gone through exit_mm(), e.g. when an event fires from exit_files(), exit_task_work() or the final context switch in do_exit(). - PID 1 before it execs init, and usermode helper children before they exec, which are created with user_mode_thread() and don't have PF_KTHREAD set. E.g. booting with trace_options=userstacktrace and an event enabled with trace_event= unwinds PID 1's "user stack" during initcalls. With the ftrace userstacktrace option enabled, the architecture unwinders then read the user stack without an mm. That access does not reliably fail: the user half of the address space belongs to whatever mm happens to be lazily loaded, if any, so the unwinder may read another task's memory. Use is_user_task() instead, which also checks current->mm, as perf already does for the same reason since commit 76ed27608f7d ("perf: sched: Fix perf crash with new is_user_task() helper"). This also stops unwinding the user stack of PF_USER_WORKER threads, which never run in user space, matching perf. Fixes: 7e8e6816c649 ("stacktrace: Use PF_KTHREAD to check for kernel threads") Signed-off-by: James Houghton --- kernel/stacktrace.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kernel/stacktrace.c b/kernel/stacktrace.c index afb3c116da91..f96a5ec12f6c 100644 --- a/kernel/stacktrace.c +++ b/kernel/stacktrace.c @@ -228,8 +228,12 @@ unsigned int stack_trace_save_user(unsigned long *store, unsigned int size) .size = size, }; - /* Trace user stack if not a kernel thread */ - if (current->flags & PF_KTHREAD) + /* + * Trace user stack only if this is a user task that still has an mm, + * i.e. not a kernel thread or user worker, and not a task that has + * already gone through exit_mm(). + */ + if (!is_user_task(current)) return 0; arch_stack_walk_user(consume_entry, &c, task_pt_regs(current)); -- 2.56.1.911.g883d8bcb20-goog _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv