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 15/15] perf: Convert mmap() refcounts to refcount_t
Date: Wed, 13 Aug 2025 08:09:54 +0100	[thread overview]
Message-ID: <aabd9668-bd22-44de-a4ef-e3bff9c48c28@lucifer.local> (raw)
In-Reply-To: <20250812104020.071507932@infradead.org>

On Tue, Aug 12, 2025 at 12:39:13PM +0200, Peter Zijlstra wrote:
> From: Thomas Gleixner <tglx@linutronix.de>
>
> The recently fixed reference count leaks could have been detected by using
> refcount_t and refcount_t would have mitigated the potential overflow at
> least.
>
> Now that the code is properly structured, convert the mmap() related
> mmap_count variants over to refcount_t.
>
> No functional change intended.
>
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>

All LGTM, checked the mmap test passes and no splat, the atomic_inc() ->
atomic_set() on &rb->mmap_count (which here becomes refcount_set()) sorted
that out.

This is a nice fix up as is the rest of the series, kudos to tglx and
additionally you Peter for adding further nice changes + fixing up the bit
we missed.

This is a _massive_ improvement :)

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

> Link: https://lkml.kernel.org/r/20250811070620.716309215@linutronix.de
> ---
>  include/linux/perf_event.h  |    2 +-
>  kernel/events/core.c        |   40 ++++++++++++++++++++--------------------
>  kernel/events/internal.h    |    4 ++--
>  kernel/events/ring_buffer.c |    2 +-
>  4 files changed, 24 insertions(+), 24 deletions(-)
>
> --- a/include/linux/perf_event.h
> +++ b/include/linux/perf_event.h
> @@ -859,7 +859,7 @@ struct perf_event {
>
>  	/* mmap bits */
>  	struct mutex			mmap_mutex;
> -	atomic_t			mmap_count;
> +	refcount_t			mmap_count;
>
>  	struct perf_buffer		*rb;
>  	struct list_head		rb_entry;
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -3968,7 +3968,7 @@ static noinline int visit_groups_merge(s
>   */
>  static inline bool event_update_userpage(struct perf_event *event)
>  {
> -	if (likely(!atomic_read(&event->mmap_count)))
> +	if (likely(!refcount_read(&event->mmap_count)))
>  		return false;
>
>  	perf_event_update_time(event);
> @@ -6704,11 +6704,11 @@ static void perf_mmap_open(struct vm_are
>  	struct perf_event *event = vma->vm_file->private_data;
>  	mapped_f mapped = get_mapped(event, event_mapped);
>
> -	atomic_inc(&event->mmap_count);
> -	atomic_inc(&event->rb->mmap_count);
> +	refcount_inc(&event->mmap_count);
> +	refcount_inc(&event->rb->mmap_count);
>
>  	if (vma->vm_pgoff)
> -		atomic_inc(&event->rb->aux_mmap_count);
> +		refcount_inc(&event->rb->aux_mmap_count);
>
>  	if (mapped)
>  		mapped(event, vma->vm_mm);
> @@ -6743,7 +6743,7 @@ static void perf_mmap_close(struct vm_ar
>  	 * to avoid complications.
>  	 */
>  	if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
> -	    atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &rb->aux_mutex)) {
> +	    refcount_dec_and_mutex_lock(&rb->aux_mmap_count, &rb->aux_mutex)) {
>  		/*
>  		 * Stop all AUX events that are writing to this buffer,
>  		 * so that we can free its AUX pages and corresponding PMU
> @@ -6763,10 +6763,10 @@ static void perf_mmap_close(struct vm_ar
>  		mutex_unlock(&rb->aux_mutex);
>  	}
>
> -	if (atomic_dec_and_test(&rb->mmap_count))
> +	if (refcount_dec_and_test(&rb->mmap_count))
>  		detach_rest = true;
>
> -	if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
> +	if (!refcount_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
>  		goto out_put;
>
>  	ring_buffer_attach(event, NULL);
> @@ -6992,19 +6992,19 @@ static int perf_mmap_rb(struct vm_area_s
>  		if (data_page_nr(event->rb) != nr_pages)
>  			return -EINVAL;
>
> -		if (atomic_inc_not_zero(&event->rb->mmap_count)) {
> +		if (refcount_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);
> +			refcount_inc(&event->mmap_count);
>  			return 0;
>  		}
>
>  		/*
>  		 * Raced against perf_mmap_close()'s
> -		 * atomic_dec_and_mutex_lock() remove the
> +		 * refcount_dec_and_mutex_lock() remove the
>  		 * event and continue as if !event->rb
>  		 */
>  		ring_buffer_attach(event, NULL);
> @@ -7023,7 +7023,7 @@ static int perf_mmap_rb(struct vm_area_s
>  	if (!rb)
>  		return -ENOMEM;
>
> -	atomic_set(&rb->mmap_count, 1);
> +	refcount_set(&rb->mmap_count, 1);
>  	rb->mmap_user = get_current_user();
>  	rb->mmap_locked = extra;
>
> @@ -7034,7 +7034,7 @@ static int perf_mmap_rb(struct vm_area_s
>  	perf_event_update_userpage(event);
>
>  	perf_mmap_account(vma, user_extra, extra);
> -	atomic_set(&event->mmap_count, 1);
> +	refcount_set(&event->mmap_count, 1);
>
>  	return 0;
>  }
> @@ -7081,15 +7081,15 @@ static int perf_mmap_aux(struct vm_area_
>  	if (!is_power_of_2(nr_pages))
>  		return -EINVAL;
>
> -	if (!atomic_inc_not_zero(&rb->mmap_count))
> +	if (!refcount_inc_not_zero(&rb->mmap_count))
>  		return -EINVAL;
>
>  	if (rb_has_aux(rb)) {
> -		atomic_inc(&rb->aux_mmap_count);
> +		refcount_inc(&rb->aux_mmap_count);
>
>  	} else {
>  		if (!perf_mmap_calc_limits(vma, &user_extra, &extra)) {
> -			atomic_dec(&rb->mmap_count);
> +			refcount_dec(&rb->mmap_count);
>  			return -EPERM;
>  		}
>
> @@ -7101,16 +7101,16 @@ static int perf_mmap_aux(struct vm_area_
>  		ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
>  				   event->attr.aux_watermark, rb_flags);
>  		if (ret) {
> -			atomic_dec(&rb->mmap_count);
> +			refcount_dec(&rb->mmap_count);
>  			return ret;
>  		}
>
> -		atomic_set(&rb->aux_mmap_count, 1);
> +		refcount_set(&rb->aux_mmap_count, 1);
>  		rb->aux_mmap_locked = extra;
>  	}
>
>  	perf_mmap_account(vma, user_extra, extra);
> -	atomic_inc(&event->mmap_count);
> +	refcount_inc(&event->mmap_count);
>
>  	return 0;
>  }
> @@ -13257,7 +13257,7 @@ perf_event_set_output(struct perf_event
>  	mutex_lock_double(&event->mmap_mutex, &output_event->mmap_mutex);
>  set:
>  	/* Can't redirect output if we've got an active mmap() */
> -	if (atomic_read(&event->mmap_count))
> +	if (refcount_read(&event->mmap_count))
>  		goto unlock;
>
>  	if (output_event) {
> @@ -13270,7 +13270,7 @@ perf_event_set_output(struct perf_event
>  			goto unlock;
>
>  		/* did we race against perf_mmap_close() */
> -		if (!atomic_read(&rb->mmap_count)) {
> +		if (!refcount_read(&rb->mmap_count)) {
>  			ring_buffer_put(rb);
>  			goto unlock;
>  		}
> --- a/kernel/events/internal.h
> +++ b/kernel/events/internal.h
> @@ -35,7 +35,7 @@ struct perf_buffer {
>  	spinlock_t			event_lock;
>  	struct list_head		event_list;
>
> -	atomic_t			mmap_count;
> +	refcount_t			mmap_count;
>  	unsigned long			mmap_locked;
>  	struct user_struct		*mmap_user;
>
> @@ -47,7 +47,7 @@ struct perf_buffer {
>  	unsigned long			aux_pgoff;
>  	int				aux_nr_pages;
>  	int				aux_overwrite;
> -	atomic_t			aux_mmap_count;
> +	refcount_t			aux_mmap_count;
>  	unsigned long			aux_mmap_locked;
>  	void				(*free_aux)(void *);
>  	refcount_t			aux_refcount;
> --- a/kernel/events/ring_buffer.c
> +++ b/kernel/events/ring_buffer.c
> @@ -400,7 +400,7 @@ void *perf_aux_output_begin(struct perf_
>  	 * the same order, see perf_mmap_close. Otherwise we end up freeing
>  	 * aux pages in this path, which is a bug, because in_atomic().
>  	 */
> -	if (!atomic_read(&rb->aux_mmap_count))
> +	if (!refcount_read(&rb->aux_mmap_count))
>  		goto err;
>
>  	if (!refcount_inc_not_zero(&rb->aux_refcount))
>
>

  reply	other threads:[~2025-08-13  7:10 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 " 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
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 [this message]
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=aabd9668-bd22-44de-a4ef-e3bff9c48c28@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®