From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796179174; b=AINRTJxpDJLf9NM/n9i/CscLaID9xhGEz0Nr8U4/yq/mfkjfJK1ySkrmk0YDgd84lGvX 5CIQLEe5Uqz0myJYg/NZJ+5npr13faQ6KpQve5m9HM06poKC8AndBKImNOYLIZoJ3q3ea ODYix9CVSyQAfSo/pGE0p1HFA28d6xUPsbq/MBCSSuoQQ7Vf0tNqjuTE4ofzXcMnhNHZ6 ERNa4YdExOB5zRid2dIEFf+wpQHMighJlmUcv0wGHKSkHzELyOzNPDg2PpBQZr3hoSsUM tdJHkBRCXeu8AQaNZIUzN9aXgjfNmDhZcfcBjNGROu0T0lNU8tDGYEqKWUeHPty6J9Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796179174; 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=TMeReKoC4NvpZzCgrCHSVmIs0gx+dcl0dloObctf+IA=; b=QBexe3/ukmHODW4seMvS16aQPaKy40AH3cLYlGm2lNhvzdYeEejAsBoYw+lk8AuJcUF1 Kcr/DmR2isir+zvV3mETYVp8aebvyjL6JChQRQOnh2A2OQd7aEzaurxFtAvOn9oL+5k8k L9dE+g5Inqe601uPC++y/NEWkOoraCyV/RTkBWtKT04QtF67AZIEV++KpZ2TsULTgcoa1 ecmiQNBZRT4OA+JdZLp8Ws8OYzGBmJnv8eIAVQaipACCQMOsjzQQo8cluvzkpRomm+u1H CMjiZnRCf0+znLbjBn5IfZo1pQSpzNZT9YaPsecoTwf46cLROWFSXH1BxolA1jr5STA== 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="TgW1etZ/"; arc=none smtp.remote-ip=209.85.214.200 Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (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 32CB460A5BE8 for ; Wed, 2 Dec 2026 02:39:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 32CB460A5BE8 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=TgW1etZ/ Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2f0578f1d51so140381905ad.3 for ; Tue, 01 Dec 2026 18:39:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796179173; x=1796783973; 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=TMeReKoC4NvpZzCgrCHSVmIs0gx+dcl0dloObctf+IA=; b=TgW1etZ/dfUyPSVX1dKtiY/hpaQxZCgZRthgYbBjU/zCkNmiuEEFRveTkPh425bd+0 +iFm7Ioe5l1nd6U9U/TtPlyRH00Dc7MVFCGRD3WRDxnXpwh9L82Q7hSHqpa/YqTIM6lU heaHgv0KgpAfxaEOIuLPN2J5M37pfaTgaVsFEQ8ascfkK/dhaYM0nDunrYzUJucoOE+X lQPdYfNi3ihLK+sdNvX6YkKOjwqTUwSZvwmAYVkar3BwiDTK+ZmOF1w/PPRss5nT/MdY +rQCxvPeISfjsvkhZDMGR6lW60Fp2Za073bFK5EUf98kaVp3GFrnxWYqwSSoa0uGewMe 7YVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796179173; x=1796783973; 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=TMeReKoC4NvpZzCgrCHSVmIs0gx+dcl0dloObctf+IA=; b=rxHIfhHXT5gYbdKIHdY+CL1s73gYrr9kbuxMuRzwVJNHvdmnjjmmreO6SiAiElhis3 r12apqP9lvV8rZ8NcD1CuMvjk6Vj5ZVPGvMzChRAlgwiyHSo8vv05GTmAB9BQOJM9e+Q 8Xk/4I9aCpPkNajGbhVB2itkso6bPWKIn4N3eHQFc5zMq5RPycUyx+MvC2nAWtoWWy6G NF5r0mB9mJt9NogQYZO4Oezy3s5QlV38nK2z4KXTWc0SrMgpmgS8TuluePQrUm7SWUMt oJetpV3p9NyNC+17U2TQQFDbLckDI2vPWB8nLReS61kH8OgQiKRY5/LRvyJ5x4jmH9tH v+OA== X-Forwarded-Encrypted: i=1; AL+hErE4DRp/38AuVKWqYCicaoZdhyINPWCvaFwSGrZJaQFnQmJGFNHjfljyFg0OKqv1J3uckRlBepJ9h80v@vger.kernel.org X-Gm-Message-State: APuU4Z+SHR0tbCYuqpLU+vTOvElo1ISI91gLp9meN0wq+WkylczPUNjz Zj2Po5wgErVBlDRpsRvr95WnswNkcGO2bG4aU9JhsTAZYwaWH1g+9jrd7wMdsRMRAaQFpjQDITk kVHSSJr5aI+If3Q9GvHqwCw== X-Received: from plrc4.prod.google.com ([2002:a17:902:aa44:b0:2f0:43fa:d088]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:440c:b0:2f1:4efb:5636 with SMTP id d9443c01a7336-2f14efb56c4mr81598785ad.61.1796179172966; Tue, 01 Dec 2026 18:39:32 -0800 (PST) Date: Wed, 2 Dec 2026 02:39:12 +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-6-jthoughton@google.com> Subject: [PATCH v2 05/12] perf/core: 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" get_perf_callchain() only checks is_user_task(current) when the regs it is given aren't user mode regs. That's fine for perf events: a task's own events are torn down by perf_event_exit_task() before exit_mm(), and samples from CPU-wide events go through perf_callchain(), which already skips the user callchain if current isn't a user task with an mm. However, BPF's bpf_get_task_stack() (and bpf_get_task_stack_sleepable()) call get_perf_callchain() directly, passing task_pt_regs(current) when called on the current task. Those are user mode regs for a user task even after it has gone through exit_mm(), e.g. when the helper is called from a program attached to exit_files() or to the dying task's final sched_switch. perf_callchain_user() then unwinds 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. This affects e.g. arm64, riscv, loongarch and csky, whose perf_callchain_user() doesn't check current->mm itself. Check is_user_task(current) regardless of the regs' mode. Fixes: fa28dcb82a38 ("bpf: Introduce helper bpf_get_task_stack()") Signed-off-by: James Houghton --- kernel/events/callchain.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/kernel/events/callchain.c b/kernel/events/callchain.c index 9d24b6e0c91f..a451ca2a5573 100644 --- a/kernel/events/callchain.c +++ b/kernel/events/callchain.c @@ -245,11 +245,15 @@ get_perf_callchain(struct pt_regs *regs, bool kernel, bool user, } if (user && !crosstask) { - if (!user_mode(regs)) { - if (!is_user_task(current)) - goto exit_put; + /* + * Even if @regs are user mode regs, e.g. task_pt_regs(current) + * passed in by bpf_get_task_stack(), current may already have + * gone through exit_mm(). + */ + if (!is_user_task(current)) + goto exit_put; + if (!user_mode(regs)) regs = task_pt_regs(current); - } if (defer_cookie) { /* -- 2.56.1.911.g883d8bcb20-goog