]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
percpu_ref: unify staggered atomic switching wait behavior
authorTejun Heo <tj@kernel.org>
Tue, 29 Sep 2015 21:47:18 +0000 (17:47 -0400)
committerChuck Anderson <chuck.anderson@oracle.com>
Sun, 18 Jun 2017 21:04:08 +0000 (14:04 -0700)
When an atomic or percpu switching starts before the previous atomic
switching finishes, the taken behaviors are

* If the new atomic switching has confirmation callback, it waits
  for the previous atomic switching to complete.

* If the new percpu switching is the first percpu switching following
  the previous atomic switching, it waits the previous atomic
  switching to complete.

No percpu_ref user depends on these subtleties.  The only meaningful
part is that, if the caller ensures that atomic switching isn't in
progress, mode switching operations can be issued from any context.

This patch pulls the wait logic to the top of both switching functions
so that they always wait for the previous atomic switching to
complete.  This makes the behavior simpler and consistent for both
directions and will help allowing concurrent invocations of mode
switching functions.

Signed-off-by: Tejun Heo <tj@kernel.org>
(cherry picked from commit 18808354b79622ed11857e41f9044ba17aec5b01)

Orabug: 26254388

Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
Reviewed-by: Ashok Vairavan <ashok.vairavan@oracle.com>
Signed-off-by: Brian Maly <brian.maly@oracle.com>
lib/percpu-refcount.c

index 622e93b18672d0fd73f62b095a8eb5389e511a4d..6a36597c129f36f122e9aee0a600aeb71b09a79f 100644 (file)
@@ -161,15 +161,19 @@ static void percpu_ref_noop_confirm_switch(struct percpu_ref *ref)
 static void __percpu_ref_switch_to_atomic(struct percpu_ref *ref,
                                          percpu_ref_func_t *confirm_switch)
 {
+       /*
+        * If the previous ATOMIC switching hasn't finished yet, wait for
+        * its completion.  If the caller ensures that ATOMIC switching
+        * isn't in progress, this function can be called from any context.
+        * Do an extra confirm_switch test to circumvent the unconditional
+        * might_sleep() in wait_event().
+        */
+       if (ref->confirm_switch)
+               wait_event(percpu_ref_switch_waitq, !ref->confirm_switch);
+
        if (ref->percpu_count_ptr & __PERCPU_REF_ATOMIC) {
-               if (confirm_switch) {
-                       /*
-                        * Somebody else already set ATOMIC.  Wait for its
-                        * completion and invoke @confirm_switch() directly.
-                        */
-                       wait_event(percpu_ref_switch_waitq, !ref->confirm_switch);
+               if (confirm_switch)
                        confirm_switch(ref);
-               }
                return;
        }
 
@@ -180,7 +184,6 @@ static void __percpu_ref_switch_to_atomic(struct percpu_ref *ref,
         * Non-NULL ->confirm_switch is used to indicate that switching is
         * in progress.  Use noop one if unspecified.
         */
-       WARN_ON_ONCE(ref->confirm_switch);
        ref->confirm_switch = confirm_switch ?: percpu_ref_noop_confirm_switch;
 
        percpu_ref_get(ref);    /* put after confirmation */
@@ -192,13 +195,21 @@ static void __percpu_ref_switch_to_percpu(struct percpu_ref *ref)
        unsigned long __percpu *percpu_count = percpu_count_ptr(ref);
        int cpu;
 
+       /*
+        * If the previous ATOMIC switching hasn't finished yet, wait for
+        * its completion.  If the caller ensures that ATOMIC switching
+        * isn't in progress, this function can be called from any context.
+        * Do an extra confirm_switch test to circumvent the unconditional
+        * might_sleep() in wait_event().
+        */
+       if (ref->confirm_switch)
+               wait_event(percpu_ref_switch_waitq, !ref->confirm_switch);
+
        BUG_ON(!percpu_count);
 
        if (!(ref->percpu_count_ptr & __PERCPU_REF_ATOMIC))
                return;
 
-       wait_event(percpu_ref_switch_waitq, !ref->confirm_switch);
-
        atomic_long_add(PERCPU_COUNT_BIAS, &ref->count);
 
        /*