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=1796179174; b=xy7f7ced8+76p61YD8IdZuCsmwSfPS4LY8ouVNf0h+gW2T62RUqt4Ul/s17Q8S9u1DxT c3eZ3VNVtpa6NfpsKi3JNDM/XBGFwJL1WAaFGeKMF3ppfe2OWmuHpu/5jGeIyKGHQo/ro Zc+0AYI21aC4CBrE5w2W0wmYGb4PG8ch1U6/OI5+2ZhFvE4xKF4omiD4RxCGyiDTg2GPw UvdioRRhiDxzXq4V5lRZUWVUHVUhKlO/ySpjRcoWEA/lblNcwEn9KDgH8yBykI/54ZNnc k4nLZ3hKrIh+kcuwFCQs9ZmdSFvDTJ2BAI97Hu3ubHAVcQUquRQ2KkH22dp6vzCuOgg== 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=HhrQfLIKIMuu18Rds3TgnqpJis6vfcFkSsUh3JcqI0Jt5AeVDZOQuB5XAHgSe/myznF4 91WJtCUubgrd2isD0PQfJybUeYbbQnZ4wl9l3Nary10w8MW9Vv/z/1KdBCv/rlPR23f0O sypylGBnx5rMnIoiTh36FMp/Mq74R7NeRea9Lasc/vAa1xe5W+vmEiUf3KJ+xUbQ5dGk6 kMR+vkgNCxjiwagwkJsEbrvAstcT0SIrYj7nab/3nChGCVq9Ghq+JR45YYvVnGG/+Gzr+ RAQhG17Sgdy0wdU73EXVo1rDMSz9X+RWLstJo27wP6FCj578uYqoLOU56AL3TN+3oOg== 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.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 3DB1D60A6F41 for ; Wed, 2 Dec 2026 02:39:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3DB1D60A6F41 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-f199.google.com with SMTP id d9443c01a7336-2f1426c0ee8so39335495ad.2 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=h+CwgUg4u6Y2B6CgmT60QS1LJDWy+FbcGG44niXPKUS6diC0vCQ4yrhIyzYryWHm+S 6wUdl+TvGnFoi/w62jcEgQ3fRIK2LMdH4lisnmNeRohhUHV7p0KuXux2Vy3Wj8XoRbiF ckQcTH2f9WgU3R1efGuA83qoVRumUlcLM9xaaBySQT9Us0OF/pIOIcvwwTMIM/OngNwc CKx6lnmxVyz5PIB4iYlUE/mqvYWAnNfecbE0vtXw0exdms4oVTHpmtSYleTHdsEUW2Jv KOMaJ0TxAyF2b1i81qk2POtiA1U4Weym6UdSSfw24kLXUJtpNVzryMbgsgKUNLFUUXH6 OyQA== X-Forwarded-Encrypted: i=1; AL+hErGz3qa/9hUohznk89jwS62f7iIzNlWIk9VhBrYgEEVyR7C5w1567PWjFUYGL9QDhM6OJZo=@vger.kernel.org X-Gm-Message-State: APuU4Z8ant19B2HZ4VX4/5Jrw7Uf7aQT7I18t2mVmxKok6paXZLVqMsY sCNiDIIBmYaKdCGKFD52bn+JqX/BMmIW5nXDgPJ/ADjQXc4yzEdxByYeEiyH5ZQKm4YJiDFkyTT cFbM3Z67CV0N+GcIb2whs4Q== 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: bpf@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