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=1796179175; b=IGaKS8rKLuVQqiPpfufIR/ZawQOXDFroX4OMEvrJZaIOzrP5spdvhm7hav1TPttwajrr Q/EYwH7E/V/QLhIRJ+Cmaphu5xzNsiCP5PbZTozzdCcRuswZbzRumoFyH6eqI4D1Cnas6 CRWqMa+pbunrW75UX8kQ6j7MaDhd2aCWXAM2O+jzIZL7Ban8ag9LFlBIxJDD74tm79R7z RNBS6OdWZRvW9BCAy2LHNgSorOSDopcaYwpLyqkssV00TB9b0Wv9ecIpKPjh8860lrx9S bZTK2mvYEymRL5hXo1LpeVllXXHlnLYVC/NjZ0K9WGC7dxmxjiq36X+yCtur6BfScHA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796179175; 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=uLMY6UqOF/D8gIcd9wp5oFPu4c5Affcr9FW3M9YCXaD2M2Qe1bY0U8kIaULq3Rqm3gIg 8ssPkV3jWEGpmdmwVHBsehrJveBR2ZEgCx/qwmzpYE2LOnKwHMwdCfzU8pxF82mUnl4ql lWf/14JDSD0qI/ZkX6g26JTDtfzt5qF6qr+SUoXesP4ebnvIGieaWoFlr/ygCUyLOtF4C SYPT/XW1rljiXm+4zGp6Ss0e4oboDS0VlJaSY2DS1NfJqLD94BualI4FlauN4K4YARUqK Ijv52EU7gkK6/xw1HYW/PxjAQ3rB0wha1wgfMawyXcFItycyzOjjcYKxNZoWZYQRXyQ== 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 2268160A3839 for ; Wed, 2 Dec 2026 02:39:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2268160A3839 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-2f0578f1d51so140381725ad.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=qvVQ8bFKMB8I359yBq8rtd/OHi2WfpEyZVidfblfwJBn+HCyMoKNy8AvFNRVIKQkxa /t2iuGxwglry+Fgo4nc2RUDK7YVWzu6p5khbWX5qzFZeP9PENpIsZMV50D/ERdanF1+3 7viKJxA5Ureh6mcg5uknJTuCHCMJULRVh9N3bMjKLEbL7nRuavRWY9QHhgPSCcEiEOfr 3GZovbJx89I31rsnor8JhjW5w7bYs78ELtno1dM1YTRJNuVKIOvqdPSuhSOz+IhD8K7f F2OOqbVpVevLMfhQLF8mqrsBHt4BDmUwff0H34FildEmEogrALy2/yUE41w2EvJbBdAP wO+A== X-Forwarded-Encrypted: i=1; AL+hErFrWKmFT/PRYixeEub9kM3LJXZfbB8acss4bASscRm7CclTb53zZo+4VVhW5R0+ZNXpx48mfj1y2pfE38s=@vger.kernel.org X-Gm-Message-State: APuU4Z+EQX5jBG5rAb+PKVOVOBDcGBlFvwKA+jmAUsbBr8E3hLf/mW4T gTAnp9wn51zuocDhYwPiOIQMx7DnqvU30orGtal33esRrmz83MgWUNW1ty40C5MP03qJPxk5lUj ozq9SaoHA3V9lW49NXxn4lw== 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-kernel@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