linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "tip-bot2 for Peter Zijlstra" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: Thomas Gleixner <tglx@linutronix.de>,
	"Peter Zijlstra (Intel)" <peterz@infradead.org>,
	Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: perf/core] perf: Split out the AUX buffer allocation
Date: Mon, 18 Aug 2025 10:22:25 -0000	[thread overview]
Message-ID: <175551254598.1420.9411382396633457171.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20250812104019.494205648@infradead.org>

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     2aee37682391332d26c01e703170e0d9358c7252
Gitweb:        https://git.kernel.org/tip/2aee37682391332d26c01e703170e0d9358c7252
Author:        Peter Zijlstra <peterz@infradead.org>
AuthorDate:    Tue, 12 Aug 2025 12:39:08 +02:00
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Fri, 15 Aug 2025 13:13:00 +02:00

perf: Split out the AUX buffer allocation

Move the AUX buffer allocation branch into its own function.

Originally-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Link: https://lore.kernel.org/r/20250812104019.494205648@infradead.org
---
 kernel/events/core.c | 144 ++++++++++++++++++++++--------------------
 1 file changed, 77 insertions(+), 67 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 5bbea81..e76afd9 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -6970,6 +6970,82 @@ static void perf_mmap_account(struct vm_area_struct *vma, long user_extra, long 
 	atomic64_add(extra, &vma->vm_mm->pinned_vm);
 }
 
+static int perf_mmap_aux(struct vm_area_struct *vma, struct perf_event *event,
+			 unsigned long nr_pages)
+{
+	long extra = 0, user_extra = nr_pages;
+	u64 aux_offset, aux_size;
+	struct perf_buffer *rb;
+	int ret, rb_flags = 0;
+
+	rb = event->rb;
+	if (!rb)
+		return -EINVAL;
+
+	guard(mutex)(&rb->aux_mutex);
+
+	/*
+	 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
+	 * mapped, all subsequent mappings should have the same size
+	 * and offset. Must be above the normal perf buffer.
+	 */
+	aux_offset = READ_ONCE(rb->user_page->aux_offset);
+	aux_size = READ_ONCE(rb->user_page->aux_size);
+
+	if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
+		return -EINVAL;
+
+	if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
+		return -EINVAL;
+
+	/* already mapped with a different offset */
+	if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
+		return -EINVAL;
+
+	if (aux_size != nr_pages * PAGE_SIZE)
+		return -EINVAL;
+
+	/* already mapped with a different size */
+	if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
+		return -EINVAL;
+
+	if (!is_power_of_2(nr_pages))
+		return -EINVAL;
+
+	if (!atomic_inc_not_zero(&rb->mmap_count))
+		return -EINVAL;
+
+	if (rb_has_aux(rb)) {
+		atomic_inc(&rb->aux_mmap_count);
+
+	} else {
+		if (!perf_mmap_calc_limits(vma, &user_extra, &extra)) {
+			atomic_dec(&rb->mmap_count);
+			return -EPERM;
+		}
+
+		WARN_ON(!rb && event->rb);
+
+		if (vma->vm_flags & VM_WRITE)
+			rb_flags |= RING_BUFFER_WRITABLE;
+
+		ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
+				   event->attr.aux_watermark, rb_flags);
+		if (ret) {
+			atomic_dec(&rb->mmap_count);
+			return ret;
+		}
+
+		atomic_set(&rb->aux_mmap_count, 1);
+		rb->aux_mmap_locked = extra;
+	}
+
+	perf_mmap_account(vma, user_extra, extra);
+	atomic_inc(&event->mmap_count);
+
+	return 0;
+}
+
 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
 {
 	struct perf_event *event = file->private_data;
@@ -7088,73 +7164,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
 		perf_mmap_account(vma, user_extra, extra);
 		atomic_inc(&event->mmap_count);
 	} else {
-		/*
-		 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
-		 * mapped, all subsequent mappings should have the same size
-		 * and offset. Must be above the normal perf buffer.
-		 */
-		u64 aux_offset, aux_size;
-
-		rb = event->rb;
-		if (!rb)
-			goto unlock;
-
-		guard(mutex)(&rb->aux_mutex);
-
-		aux_offset = READ_ONCE(rb->user_page->aux_offset);
-		aux_size = READ_ONCE(rb->user_page->aux_size);
-
-		if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
-			goto unlock;
-
-		if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
-			goto unlock;
-
-		/* already mapped with a different offset */
-		if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
-			goto unlock;
-
-		if (aux_size != nr_pages * PAGE_SIZE)
-			goto unlock;
-
-		/* already mapped with a different size */
-		if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
-			goto unlock;
-
-		if (!is_power_of_2(nr_pages))
-			goto unlock;
-
-		if (!atomic_inc_not_zero(&rb->mmap_count))
-			goto unlock;
-
-		if (rb_has_aux(rb)) {
-			atomic_inc(&rb->aux_mmap_count);
-			ret = 0;
-
-		} else {
-			if (!perf_mmap_calc_limits(vma, &user_extra, &extra)) {
-				ret = -EPERM;
-				atomic_dec(&rb->mmap_count);
-				goto unlock;
-			}
-
-			WARN_ON(!rb && event->rb);
-
-			if (vma->vm_flags & VM_WRITE)
-				flags |= RING_BUFFER_WRITABLE;
-
-			ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
-					   event->attr.aux_watermark, flags);
-			if (ret) {
-				atomic_dec(&rb->mmap_count);
-				goto unlock;
-			}
-
-			atomic_set(&rb->aux_mmap_count, 1);
-			rb->aux_mmap_locked = extra;
-		}
-		perf_mmap_account(vma, user_extra, extra);
-		atomic_inc(&event->mmap_count);
+		ret = perf_mmap_aux(vma, event, nr_pages);
 	}
 
 unlock:

  parent reply	other threads:[~2025-08-18 10:22 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-12 10:38 [PATCH v3 00/15] perf: Convert mmap() related reference counts to refcount_t Peter Zijlstra
2025-08-12 10:38 ` [PATCH v3 01/15] perf: Remove redundant condition for AUX buffer size Peter Zijlstra
2025-08-13  5:35   ` Lorenzo Stoakes
2025-08-18 10:23   ` [tip: perf/core] " tip-bot2 for Thomas Gleixner
2025-08-12 10:39 ` [PATCH v3 02/15] perf: Split out mlock limit handling Peter Zijlstra
2025-08-13  5:36   ` Lorenzo Stoakes
2025-08-18 10:22   ` [tip: perf/core] " tip-bot2 for Thomas Gleixner
2025-08-12 10:39 ` [PATCH v3 03/15] perf: Split out VM accounting Peter Zijlstra
2025-08-13  5:37   ` Lorenzo Stoakes
2025-08-18 10:22   ` [tip: perf/core] " tip-bot2 for Thomas Gleixner
2025-08-12 10:39 ` [PATCH v3 04/15] perf: Move perf_mmap_calc_limits() into both rb and aux branches Peter Zijlstra
2025-08-13  5:28   ` Lorenzo Stoakes
2025-08-18 10:22   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-08-12 10:39 ` [PATCH v3 05/15] perf: Merge consecutive conditionals in perf_mmap() Peter Zijlstra
2025-08-13  5:41   ` Lorenzo Stoakes
2025-08-18 10:22   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-08-12 10:39 ` [PATCH v3 06/15] perf: Move common code into both rb and aux branches Peter Zijlstra
2025-08-13  5:55   ` Lorenzo Stoakes
2025-08-13  8:25     ` Peter Zijlstra
2025-08-13  8:27       ` Lorenzo Stoakes
2025-08-18 10:22   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-08-12 10:39 ` [PATCH v3 07/15] perf: Remove redundant aux_unlock label Peter Zijlstra
2025-08-13  5:56   ` Lorenzo Stoakes
2025-08-18 10:22   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-08-12 10:39 ` [PATCH v3 08/15] perf: Use guard() for aux_mutex in perf_mmap() Peter Zijlstra
2025-08-13  6:00   ` Lorenzo Stoakes
2025-08-18 10:22   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-08-12 10:39 ` [PATCH v3 09/15] perf: Reflow to get rid of aux_success label Peter Zijlstra
2025-08-13  6:03   ` Lorenzo Stoakes
2025-08-13  8:29     ` Peter Zijlstra
2025-08-18 10:22   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-08-12 10:39 ` [PATCH v3 10/15] perf: Split out the AUX buffer allocation Peter Zijlstra
2025-08-13  6:10   ` Lorenzo Stoakes
2025-08-18 10:22   ` tip-bot2 for Peter Zijlstra [this message]
2025-08-12 10:39 ` [PATCH v3 11/15] perf: Make RB allocation branch self sufficient Peter Zijlstra
2025-08-13  6:24   ` Lorenzo Stoakes
2025-08-18 10:22   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-08-12 10:39 ` [PATCH v3 12/15] perf: Split out the RB allocation Peter Zijlstra
2025-08-13  6:35   ` Lorenzo Stoakes
2025-08-18 10:22   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-08-12 10:39 ` [PATCH v3 13/15] perf: Use scoped_guard() for mmap_mutex in perf_mmap() Peter Zijlstra
2025-08-13  6:42   ` Lorenzo Stoakes
2025-08-13  8:32     ` Peter Zijlstra
2025-08-13  8:44       ` Peter Zijlstra
2025-08-13  8:52         ` Lorenzo Stoakes
2025-08-18 10:22   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-08-12 10:39 ` [PATCH v3 14/15] perf: Identify the 0->1 transition for event::mmap_count Peter Zijlstra
2025-08-12 11:15   ` Peter Zijlstra
2025-08-13  6:53     ` Lorenzo Stoakes
2025-08-18 10:22   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-08-12 10:39 ` [PATCH v3 15/15] perf: Convert mmap() refcounts to refcount_t Peter Zijlstra
2025-08-13  7:09   ` Lorenzo Stoakes
2025-08-18 10:22   ` [tip: perf/core] " tip-bot2 for Thomas Gleixner
2025-08-13  7:11 ` [PATCH v3 00/15] perf: Convert mmap() related reference counts " Lorenzo Stoakes

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=175551254598.1420.9411382396633457171.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=x86@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;
as well as URLs for NNTP newsgroup(s).