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=iFQB9mWFosqyh97itUTzfvB/kL87YGBGUYErdLrrh+J/OBpR6svoFLtdTvdipR/oS3AT vInePTTWWAWdX/oz8lDFxjyIdORCbQikdYb7XkefQIyh26sZbC9Z0RMNKdExxKcY0MXA7 3Y/iLzgkobz/YxgBJDDwVgld5RhwbA8JfCXpA4DYuoEj4RuXyTDHUiinpMn5fLtESxG8W a7fDorTeSmE2ukq6oC58WJ2qwGKxA9zbPl1GiUiyYGBpCDQYpB77e9XfSl2iWYO31UULy sp8h2/V8kXvaPVWl2RPmsbI+33apYCnNXRiw5G/Hwz8HAeDG+wc6UzAuiTB3M9HpVjg== 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=YBmxiF2MvJD3EdHolHkip0Fk0fCSOyOS0m0n/ERTzmCFiftgEMEbGj3QwGUTO2tZ9CYB KFWJyvhB4Cw0eMQ+ruyfgt8kSD3A7zyc2l6hbd2dspefEhNywb1fmMJ9nX0jt+o5kT2xJ e1vZSexoVxEbEKhJsNoRNGqFSCk7n+6TS3CP09HmNQpyISCw13RUegOC1+9usVtrA2FBQ 6hKqqplyCg3LaN954sb2xgQkqFRii0+ZAlSJzVbyidXcMydYluK6A6j7E9CtWbxBTTjFh xpo3BsWKMiNLx88Bxj69nMIhizmpVuMfd2ZJsJqRlo6tbEdLdZO/tpD7PGYyzRRxzDA== 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 40C5E606A339 for ; Wed, 2 Dec 2026 02:39:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 40C5E606A339 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-2efa9aea3c0so167569685ad.1 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=bZEos0P3CIyPGx+vmknRGpIu4QZW4wWiyBg4CjuEDnzhAcI3ia2XUPu+Bwxylx9iaC Q99NKwF+tErfEB1KcgTcOfUcHStMe+d62UbuJ/ziP4XfhKN2wZNFBFFVsn50iPLZL+xh 54fo9gf2wGDOMTx/tbc/egBZ8Ay11aFEKVhianKVreBG4Ijuvp1T9qU4aMJo4CgFND/B 590GC6BN5krc43W4EeBL/0LS4XmgaRfAmGopM9ENZdHw/OEBx8gPHJDhbAgHlu40b/vk Rvn228Kp4zZyW1mSU8xQ1bFvpCvMsTUPsFMCZRZgjrUESrJ3CXtlz2jtUB9xprpWDh76 YX3Q== X-Forwarded-Encrypted: i=1; AL+hErGU7iYJH41smTn4gi9E8XNL+a7GYxg6YWp2DZI7mBOk4/jzIG+qopF6p/2SXvO8HUyE1o3M5jXXwA==@vger.kernel.org X-Gm-Message-State: APuU4Z8act5pq8mVQHShe9aeOr6vLFeIYTTcNJeM6kZ4tjjQBwY8rO6a ZFtjoya975fJ0ZK+rTYkKyQ7CsZ/AeotpNhLL2YITNcZqHVPCcdTkdZoEIm49upfhTzmcJAk81O 1n0oEYrwGEwwhNDCvfMxEgQ== 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: io-uring@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