mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: "Rafael B. Dias" <rafaelbttdias@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	security@kernel.org
Subject: Re: [PATCH] perf/core: unaccount mmap pages from the charged owners
Date: Tue, 17 Nov 2026 16:57:20 +0100	[thread overview]
Message-ID: <20261117155720.GB621672@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20260722060353.24-1-rafaelbttdias@gmail.com>

On Wed, Jul 22, 2026 at 03:03:53AM -0300, Rafael B. Dias wrote:
> perf mmap accounting splits pages between user_struct::locked_vm and
> mm_struct::pinned_vm. The first mapping that allocates a data ring stores
> the charged user and numeric pinned amount in perf_buffer, but it does not
> store the mm that received the pinned charge.
> 
> A perf event fd can be mapped by more than one process. If a different mm
> owns the final VMA close, perf_mmap_unaccount() subtracts mmap_locked from
> that final mm instead of the mm charged at allocation. Repeating this
> across short-lived donor processes creates a negative pinned_vm balance
> that can fund later perf mappings beyond RLIMIT_MEMLOCK.
> 
> AUX has the same last-VMA assumption. It also subtracts locked_vm from the
> data ring's mmap_user even when a different user created the AUX mapping.
> 
> Retain references to the mm charged for the data ring and to the user and
> mm charged for AUX. Use those stored owners during final unaccounting and
> release the references after the counters are restored.
> 
> On a two-CPU 6.12.94 KASAN guest, an unprivileged receiver with

(that's an old kernel, as in *very* old)

> RLIMIT_MEMLOCK=0 accumulated 16,385 pages of cross-mm credit and mapped
> 65,540 KiB on the vulnerable kernel. The same reproducer returned EPERM
> with this ownership fix. No KASAN report or warning occurred. The patch
> also builds kernel/events/core.o with x86_64_defconfig on the same base.
> 
> Fixes: 26cb63ad11e0 ("perf: Fix perf mmap bugs")
> Fixes: 36b3db03b474 ("perf/core: Fix the mlock accounting, again")
> Cc: stable@vger.kernel.org
> Assisted-by: OpenAI-Codex:gpt-5.6-sol
> Signed-off-by: Rafael B. Dias <rafaelbttdias@gmail.com>

I don't like allowing the AUX stuff to be accounted separately from the
regular stuff. But also, I think you missed perf_mmap_calc_limits().

What about something like the below?

I am also tempted to add a clause where 'vma->vm_mm != rb->mmap_mm'
would completely dis-allow the operation.

---
diff --git a/kernel/events/core.c b/kernel/events/core.c
index e870f12c3ad8..b920680aad50 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -7046,7 +7046,7 @@ static void perf_mmap_open(struct vm_area_struct *vma)
 }
 
 static void perf_pmu_output_stop(struct perf_event *event);
-static void perf_mmap_unaccount(struct vm_area_struct *vma, struct perf_buffer *rb);
+static void perf_mmap_unaccount(struct perf_buffer *rb, long user_extra, long extra);
 
 /*
  * A buffer can be mmap()ed multiple times; either directly through the same
@@ -7082,8 +7082,7 @@ static void perf_mmap_close(struct vm_area_struct *vma)
 		perf_pmu_output_stop(event);
 
 		/* now it's safe to free the pages */
-		atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->perf_mlock);
-		atomic64_sub(rb->aux_mmap_locked, &vma->vm_mm->pinned_vm);
+		perf_mmap_unaccount(rb, rb->aux_nr_pages, rb->aux_mmap_locked);
 
 		/* this has to be the last one */
 		rb_free_aux(rb);
@@ -7156,7 +7155,7 @@ static void perf_mmap_close(struct vm_area_struct *vma)
 	 * Aside from that, this buffer is 'fully' detached and unmapped,
 	 * undo the VM accounting.
 	 */
-	perf_mmap_unaccount(vma, rb);
+	perf_mmap_unaccount(rb, data_page_nr(rb) + 1, rb->mmap_locked);
 
 out_put:
 	ring_buffer_put(rb); /* could be last */
@@ -7256,10 +7255,10 @@ static int map_range(struct perf_buffer *rb, struct vm_area_struct *vma)
 	return err;
 }
 
-static bool perf_mmap_calc_limits(struct vm_area_struct *vma, long *user_extra, long *extra)
+static bool __perf_mmap_calc_limits(struct user_struct *user, struct mm_struct *mm,
+				    long *user_extra, long *extra)
 {
 	unsigned long user_locked, user_lock_limit, locked, lock_limit;
-	struct user_struct *user = current_user();
 
 	user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
 	/* Increase the limit linearly with more CPUs */
@@ -7281,31 +7280,36 @@ static bool perf_mmap_calc_limits(struct vm_area_struct *vma, long *user_extra,
 		 * charge the rest from pinned_vm
 		 */
 		*extra = user_locked - user_lock_limit;
-		*user_extra -= *extra;
 	}
 
 	lock_limit = rlimit(RLIMIT_MEMLOCK);
 	lock_limit >>= PAGE_SHIFT;
-	locked = atomic64_read(&vma->vm_mm->pinned_vm) + *extra;
+	locked = atomic64_read(&mm->pinned_vm) + *extra;
 
 	return locked <= lock_limit || !perf_is_paranoid() || capable(CAP_IPC_LOCK);
 }
 
-static void perf_mmap_account(struct vm_area_struct *vma, long user_extra, long extra)
+static bool perf_mmap_calc_limits(struct perf_buffer *rb, long *user_extra, long *extra)
 {
-	struct user_struct *user = current_user();
-
-	atomic_long_add(user_extra, &user->perf_mlock);
-	atomic64_add(extra, &vma->vm_mm->pinned_vm);
+	return __perf_mmap_calc_limits(rb->mmap_user, rb->mmap_mm, user_extra, extra);
 }
 
-static void perf_mmap_unaccount(struct vm_area_struct *vma, struct perf_buffer *rb)
+static void perf_mmap_account(struct perf_buffer *rb, long user_extra, long extra)
 {
 	struct user_struct *user = rb->mmap_user;
+	struct mm_struct *mm = rb->mmap_mm;
 
-	atomic_long_sub((perf_data_size(rb) >> PAGE_SHIFT) + 1 - rb->mmap_locked,
-			&user->perf_mlock);
-	atomic64_sub(rb->mmap_locked, &vma->vm_mm->pinned_vm);
+	atomic_long_add(user_extra - extra, &user->perf_mlock);
+	atomic64_add(extra, &mm->pinned_vm);
+}
+
+static void perf_mmap_unaccount(struct perf_buffer *rb, long user_extra, long extra)
+{
+	struct user_struct *user = rb->mmap_user;
+	struct mm_struct *mm = rb->mmap_mm;
+
+	atomic_long_sub(user_extra - extra, &user->perf_mlock);
+	atomic64_sub(extra, &mm->pinned_vm);
 }
 
 static int perf_mmap_rb(struct vm_area_struct *vma, struct perf_event *event,
@@ -7344,7 +7348,7 @@ static int perf_mmap_rb(struct vm_area_struct *vma, struct perf_event *event,
 			 * Success -- managed to mmap() the same buffer
 			 * multiple times.
 			 */
-			perf_mmap_account(vma, user_extra, extra);
+			perf_mmap_account(event->rb, user_extra, extra);
 			refcount_inc(&event->mmap_count);
 			return 0;
 		}
@@ -7357,7 +7361,7 @@ static int perf_mmap_rb(struct vm_area_struct *vma, struct perf_event *event,
 		ring_buffer_attach(event, NULL);
 	}
 
-	if (!perf_mmap_calc_limits(vma, &user_extra, &extra))
+	if (!__perf_mmap_calc_limits(current_user(), vma->vm_mm, &user_extra, &extra))
 		return -EPERM;
 
 	if (vma->vm_flags & VM_WRITE)
@@ -7370,6 +7374,7 @@ static int perf_mmap_rb(struct vm_area_struct *vma, struct perf_event *event,
 	if (!rb)
 		return -ENOMEM;
 
+	rb->mmap_mm = ({ mmgrab(vma->vm_mm); vma->vm_mm; });
 	rb->mmap_locked = extra;
 
 	ring_buffer_attach(event, rb);
@@ -7378,7 +7383,7 @@ static int perf_mmap_rb(struct vm_area_struct *vma, struct perf_event *event,
 	perf_event_init_userpage(event);
 	perf_event_update_userpage(event);
 
-	perf_mmap_account(vma, user_extra, extra);
+	perf_mmap_account(rb, user_extra, extra);
 	refcount_set(&event->mmap_count, 1);
 
 	return 0;
@@ -7434,7 +7439,7 @@ static int perf_mmap_aux(struct vm_area_struct *vma, struct perf_event *event,
 		refcount_inc(&rb->aux_mmap_count);
 
 	} else {
-		if (!perf_mmap_calc_limits(vma, &user_extra, &extra)) {
+		if (!perf_mmap_calc_limits(rb, &user_extra, &extra)) {
 			refcount_dec(&rb->mmap_count);
 			return -EPERM;
 		}
@@ -7455,7 +7460,7 @@ static int perf_mmap_aux(struct vm_area_struct *vma, struct perf_event *event,
 		rb->aux_mmap_locked = extra;
 	}
 
-	perf_mmap_account(vma, user_extra, extra);
+	perf_mmap_account(rb, user_extra, extra);
 	refcount_inc(&event->mmap_count);
 
 	return 0;
@@ -7547,6 +7552,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
 		 * attempt use this failed buffer.
 		 */
 		if (refcount_read(&event->mmap_count) == 1) {
+			struct perf_buffer *rb = event->rb;
 			/*
 			 * Minimal perf_mmap_close(); there can't be AUX or
 			 * other events on account of this being the first.
@@ -7554,7 +7560,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
 			mapped = get_mapped(event, event_unmapped);
 			if (mapped)
 				mapped(event, vma->vm_mm);
-			perf_mmap_unaccount(vma, event->rb);
+			perf_mmap_unaccount(rb, data_page_nr(rb) + 1, rb->mmap_locked);
 			ring_buffer_attach(event, NULL);	/* drops last rb->refcount */
 			refcount_set(&event->mmap_count, 0);
 			return ret;
diff --git a/kernel/events/internal.h b/kernel/events/internal.h
index c03c4f2eea57..8a6c620ae91e 100644
--- a/kernel/events/internal.h
+++ b/kernel/events/internal.h
@@ -5,6 +5,9 @@
 #include <linux/hardirq.h>
 #include <linux/uaccess.h>
 #include <linux/refcount.h>
+#include <linux/sched/user.h>
+#include <linux/perf_event.h>
+#include <asm/local.h>
 
 /* Buffer handling */
 
@@ -38,6 +41,7 @@ struct perf_buffer {
 	refcount_t			mmap_count;
 	unsigned long			mmap_locked;
 	struct user_struct		*mmap_user;
+	struct mm_struct		*mmap_mm;
 
 	/* AUX area */
 	struct mutex			aux_mutex;
@@ -68,6 +72,7 @@ static inline void rb_free_rcu(struct rcu_head *rcu_head)
 
 	rb = container_of(rcu_head, struct perf_buffer, rcu_head);
 	free_uid(rb->mmap_user);
+	mmdrop(rb->mmap_mm);
 	rb_free(rb);
 }
 

      reply	other threads:[~2026-11-17 15:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-22  5:59 [BUG] perf: cross-mm mmap teardown unaccounts pinned memory from the wrong mm Rafael B. Dias
2026-07-22  6:03 ` [PATCH] perf/core: unaccount mmap pages from the charged owners Rafael B. Dias
2026-11-17 15:57   ` Peter Zijlstra [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117155720.GB621672@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=rafaelbttdias@gmail.com \
    --cc=security@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®