mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: tglx@linutronix.de, linux-kernel@vger.kernel.org,
	torvalds@linuxfoundation.org, mingo@kernel.org,
	namhyung@kernel.org, acme@redhat.com, kees@kernel.org
Subject: Re: [PATCH v3 12/15] perf: Split out the RB allocation
Date: Wed, 13 Aug 2025 07:35:03 +0100	[thread overview]
Message-ID: <35b09c4e-0305-443f-9c17-c8d46d1792b0@lucifer.local> (raw)
In-Reply-To: <20250812104019.722214699@infradead.org>

On Tue, Aug 12, 2025 at 12:39:10PM +0200, Peter Zijlstra wrote:
> Move the RB buffer allocation branch into its own function.
>
> Originally-by: Thomas Gleixner <tglx@linutronix.de>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>

Ah yes of course, the very next patch :)

One nit below about ret assignment, though I wonder if you address later?

Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

> ---
>  kernel/events/core.c |  145 +++++++++++++++++++++++++--------------------------
>  1 file changed, 73 insertions(+), 72 deletions(-)
>
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -6970,6 +6970,75 @@ static void perf_mmap_account(struct vm_
>  	atomic64_add(extra, &vma->vm_mm->pinned_vm);
>  }
>
> +static int perf_mmap_rb(struct vm_area_struct *vma, struct perf_event *event,
> +			unsigned long nr_pages)
> +{
> +	long extra = 0, user_extra = nr_pages;
> +	struct perf_buffer *rb;
> +	int rb_flags = 0;
> +
> +	nr_pages -= 1;
> +
> +	/*
> +	 * If we have rb pages ensure they're a power-of-two number, so we
> +	 * can do bitmasks instead of modulo.
> +	 */
> +	if (nr_pages != 0 && !is_power_of_2(nr_pages))
> +		return -EINVAL;
> +
> +	WARN_ON_ONCE(event->ctx->parent_ctx);
> +
> +	if (event->rb) {
> +		if (data_page_nr(event->rb) != nr_pages)
> +			return -EINVAL;
> +
> +		if (atomic_inc_not_zero(&event->rb->mmap_count)) {
> +			/*
> +			 * Success -- managed to mmap() the same buffer
> +			 * multiple times.
> +			 */
> +			perf_mmap_account(vma, user_extra, extra);
> +			atomic_inc(&event->mmap_count);
> +			return 0;
> +		}
> +
> +		/*
> +		 * Raced against perf_mmap_close()'s
> +		 * atomic_dec_and_mutex_lock() remove the
> +		 * event and continue as if !event->rb
> +		 */
> +		ring_buffer_attach(event, NULL);
> +	}
> +
> +	if (!perf_mmap_calc_limits(vma, &user_extra, &extra))
> +		return -EPERM;
> +
> +	if (vma->vm_flags & VM_WRITE)
> +		rb_flags |= RING_BUFFER_WRITABLE;
> +

I hadn't noticed before actually, but it's really nice that we only assign
rb from below for the newly allocated rb, and refer to the prior one via
event->rb above.

Which neatly addressed my prior review feedback on this, very nice, cheers!

> +	rb = rb_alloc(nr_pages,
> +		      event->attr.watermark ? event->attr.wakeup_watermark : 0,
> +		      event->cpu, rb_flags);
> +
> +	if (!rb)
> +		return -ENOMEM;
> +
> +	atomic_set(&rb->mmap_count, 1);
> +	rb->mmap_user = get_current_user();
> +	rb->mmap_locked = extra;
> +
> +	ring_buffer_attach(event, rb);
> +
> +	perf_event_update_time(event);
> +	perf_event_init_userpage(event);
> +	perf_event_update_userpage(event);
> +
> +	perf_mmap_account(vma, user_extra, extra);
> +	atomic_inc(&event->mmap_count);
> +
> +	return 0;
> +}
> +
>  static int perf_mmap_aux(struct vm_area_struct *vma, struct perf_event *event,
>  			 unsigned long nr_pages)
>  {
> @@ -7050,10 +7119,8 @@ static int perf_mmap(struct file *file,
>  {
>  	struct perf_event *event = file->private_data;
>  	unsigned long vma_size, nr_pages;
> -	long user_extra = 0, extra = 0;
> -	struct perf_buffer *rb = NULL;
> -	int ret, flags = 0;
>  	mapped_f mapped;
> +	int ret;
>
>  	/*
>  	 * Don't allow mmap() of inherited per-task counters. This would
> @@ -7079,8 +7146,6 @@ static int perf_mmap(struct file *file,
>  	if (vma_size != PAGE_SIZE * nr_pages)
>  		return -EINVAL;
>
> -	user_extra = nr_pages;
> -
>  	mutex_lock(&event->mmap_mutex);
>  	ret = -EINVAL;

This ret assignment is redundant now, since in all cases you assign ret
below.

I wonder if you get rid of this in a later patch though?

>
> @@ -7094,74 +7159,10 @@ static int perf_mmap(struct file *file,
>  		goto unlock;
>  	}
>
> -	if (vma->vm_pgoff == 0) {
> -		nr_pages -= 1;
> -
> -		/*
> -		 * If we have rb pages ensure they're a power-of-two number, so we
> -		 * can do bitmasks instead of modulo.
> -		 */
> -		if (nr_pages != 0 && !is_power_of_2(nr_pages))
> -			goto unlock;
> -
> -		WARN_ON_ONCE(event->ctx->parent_ctx);
> -
> -		if (event->rb) {
> -			if (data_page_nr(event->rb) != nr_pages)
> -				goto unlock;
> -
> -			if (atomic_inc_not_zero(&event->rb->mmap_count)) {
> -				/*
> -				 * Success -- managed to mmap() the same buffer
> -				 * multiple times.
> -				 */
> -				ret = 0;
> -				perf_mmap_account(vma, user_extra, extra);
> -				atomic_inc(&event->mmap_count);
> -				goto unlock;
> -			}
> -
> -			/*
> -			 * Raced against perf_mmap_close()'s
> -			 * atomic_dec_and_mutex_lock() remove the
> -			 * event and continue as if !event->rb
> -			 */
> -			ring_buffer_attach(event, NULL);
> -		}
> -
> -		if (!perf_mmap_calc_limits(vma, &user_extra, &extra)) {
> -			ret = -EPERM;
> -			goto unlock;
> -		}
> -
> -		if (vma->vm_flags & VM_WRITE)
> -			flags |= RING_BUFFER_WRITABLE;
> -
> -		rb = rb_alloc(nr_pages,
> -			      event->attr.watermark ? event->attr.wakeup_watermark : 0,
> -			      event->cpu, flags);
> -
> -		if (!rb) {
> -			ret = -ENOMEM;
> -			goto unlock;
> -		}
> -
> -		atomic_set(&rb->mmap_count, 1);
> -		rb->mmap_user = get_current_user();
> -		rb->mmap_locked = extra;
> -
> -		ring_buffer_attach(event, rb);
> -
> -		perf_event_update_time(event);
> -		perf_event_init_userpage(event);
> -		perf_event_update_userpage(event);
> -		ret = 0;
> -
> -		perf_mmap_account(vma, user_extra, extra);
> -		atomic_inc(&event->mmap_count);
> -	} else {
> +	if (vma->vm_pgoff == 0)
> +		ret = perf_mmap_rb(vma, event, nr_pages);
> +	else
>  		ret = perf_mmap_aux(vma, event, nr_pages);
> -	}
>
>  unlock:
>  	mutex_unlock(&event->mmap_mutex);
>
>

  reply	other threads:[~2025-08-13  6:35 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: perf/core] " tip-bot2 for Peter Zijlstra
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 [this message]
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=35b09c4e-0305-443f-9c17-c8d46d1792b0@lucifer.local \
    --to=lorenzo.stoakes@oracle.com \
    --cc=acme@redhat.com \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linuxfoundation.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®