From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796179174; b=JUytaUMDVeCgq1BkAQaU+PAe4xPp2quVidjehGuNmRhop5RhSnFwPs+feMKenAVocM+Z DtWu6yv+YhHZf10T2fBraU0/LRfc2yokQ0YBBOjZstH3ZBsfpZ99F4Inc3VpEC0T55ruw 67LDLL+PCcWZViHB8JNBUXPBnc4rdmMRwi6J+/v4NZShQPzrqWyRq7TDhIfqptwwWaVns tXA7JJtjboJhrKe4SleJqBQ4AWyGH6ZxGbUmZB2i+vV0pbn0VRKHaunntHrNaDf0n1mTK LJZwzVDDP0GlzX9ncyuEBUE1KVebXkn5LP4RV6OkhTnHfqJavRq4SnqkE9yZsEcBigQ== 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=rZSqEjtKwmpL7gh5Zztjj+EkXNM3rhvlDQAa48cUey1FMJ99Ig2nzGKS2XroE+FS7r+J EIf00+YqnEs+48luKtml5vt8ZMK5gJUZdcK7FI71UYATcey43nHIZi4jzVFu8HC1+Vn4w jMb6Y4qQY7btQikPhRT40q+TJwLaqfL/zY6mTlp9dzEVIs0IOmzwgfsgZ9MDue9qpIxsZ Ea7fqgeJniOr1k4pl+UAtqMKtj2/1lDgrOXrFq3JkyECSbNBoS/5aJCSyVwIlF9Hmir2Z pGoCYQLWJK69yWupLhj4XWk/lliPHWSbbjCBpH+iSwZDrqR6V8ikqtWl5nwUFFdOlBA== 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.198 Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) (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 40BC7606A32E for ; Wed, 2 Dec 2026 02:39:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 40BC7606A32E 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-f198.google.com with SMTP id d9443c01a7336-2f1426c0ee8so39335435ad.2 for ; Tue, 01 Dec 2026 18:39:33 -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=KoLktU4y2TwbrvJBgqKcKcc5QD8IH6IC6gKQphHfoo2ULFXBUN3ixSqxMVgPlplv/w B8G96DJifnX7u3YbDAiA8cZFyyduHkzYInAztUvA/iFHLJWFO5SFQpp9cqr9XvdSG3+/ jZsBCACLxZ+daVJ8L7POqR5jnojGbE8AZU/b/vfJ4jAK5Wl0PlYlSTLmrsOCLdcVBeyX FmGkoASNQQ+SfHd9smMoG6gffydrNHcBhwJxv97ql8SJJx4ZvtKV46PBz6u7wZ4uGr+Q 3u6G/96inIQGdDbdfBk/eGHg327MkvHSqnYHCCYc5M4dIj7PcHUdO2DHTFRM9VFThGX/ XH8A== X-Forwarded-Encrypted: i=1; AL+hErFfhJN9ndeGLK4Xf4LGVQ4af309AotV8xxhBt6A3dRophD0rPE5aasLY8yhw7xsuj03sgtz9uMEZ/KWhg==@vger.kernel.org X-Gm-Message-State: APuU4Z829vY22VxEh0vr6TH+MkqpIbGbUXBFmtLJraIeISI5V0f2ZykL Pr7SzLoCMj1ViICZlF7I/Yx49pPMp/iRJrLSABJievkxojZZhqvgLujzRI/BeW/s8z97GlNk/p9 ARxkeV8GfQvHVKCe2eYASDg== 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-block@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