All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/1] percpu-refcount: init ->confirm_switch member properly
@ 2016-08-11 17:27 Roman Pen
  2016-08-11 20:25 ` Tejun Heo
  0 siblings, 1 reply; 2+ messages in thread
From: Roman Pen @ 2016-08-11 17:27 UTC (permalink / raw)
  Cc: Roman Pen, Tejun Heo, linux-kernel

This patch targets two things which are related to ->confirm_switch:

 1. Init ->confirm_switch pointer with NULL on percpu_ref_init() or
    kernel frightfully complains with WARN_ON_ONCE(ref->confirm_switch)
    at __percpu_ref_switch_to_atomic if memory chunk was not properly
    zeroed.

 2. Warn if RCU callback is still in progress on percpu_ref_exit().
    The race still exists, because percpu_ref_call_confirm_rcu()
    drops ->confirm_switch to NULL early, but that is only a warning
    and still the caller is responsible that ref is no longer in
    active use.  Hopefully that can help to catch incorrect usage
    of percpu-refcount.

Signed-off-by: Roman Pen <roman.penyaev@profitbricks.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: linux-kernel@vger.kernel.org
---
 lib/percpu-refcount.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c
index 6111bcb..9c110e0 100644
--- a/lib/percpu-refcount.c
+++ b/lib/percpu-refcount.c
@@ -82,6 +82,7 @@ int percpu_ref_init(struct percpu_ref *ref, percpu_ref_func_t *release,
 	atomic_long_set(&ref->count, start_count);
 
 	ref->release = release;
+	ref->confirm_switch = NULL;
 	return 0;
 }
 EXPORT_SYMBOL_GPL(percpu_ref_init);
@@ -101,6 +102,11 @@ void percpu_ref_exit(struct percpu_ref *ref)
 	unsigned long __percpu *percpu_count = percpu_count_ptr(ref);
 
 	if (percpu_count) {
+		/*
+		 * Non-NULL ->confirm_switch is used to indicate that
+		 * switching is in progress.
+		 */
+		WARN_ON_ONCE(ref->confirm_switch);
 		free_percpu(percpu_count);
 		ref->percpu_count_ptr = __PERCPU_REF_ATOMIC_DEAD;
 	}
-- 
2.9.0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 1/1] percpu-refcount: init ->confirm_switch member properly
  2016-08-11 17:27 [PATCH 1/1] percpu-refcount: init ->confirm_switch member properly Roman Pen
@ 2016-08-11 20:25 ` Tejun Heo
  0 siblings, 0 replies; 2+ messages in thread
From: Tejun Heo @ 2016-08-11 20:25 UTC (permalink / raw)
  To: Roman Pen; +Cc: linux-kernel

On Thu, Aug 11, 2016 at 07:27:09PM +0200, Roman Pen wrote:
> This patch targets two things which are related to ->confirm_switch:
> 
>  1. Init ->confirm_switch pointer with NULL on percpu_ref_init() or
>     kernel frightfully complains with WARN_ON_ONCE(ref->confirm_switch)
>     at __percpu_ref_switch_to_atomic if memory chunk was not properly
>     zeroed.
> 
>  2. Warn if RCU callback is still in progress on percpu_ref_exit().
>     The race still exists, because percpu_ref_call_confirm_rcu()
>     drops ->confirm_switch to NULL early, but that is only a warning
>     and still the caller is responsible that ref is no longer in
>     active use.  Hopefully that can help to catch incorrect usage
>     of percpu-refcount.
> 
> Signed-off-by: Roman Pen <roman.penyaev@profitbricks.com>
> Cc: Tejun Heo <tj@kernel.org>
> Cc: linux-kernel@vger.kernel.org

Applied to percpu/for-4.9.

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-08-11 20:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-11 17:27 [PATCH 1/1] percpu-refcount: init ->confirm_switch member properly Roman Pen
2016-08-11 20:25 ` Tejun Heo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.