Lines Matching full:ref

12  * don't try to detect the ref hitting 0 - which means that get/put can just
23 * the ref hitting 0 on every put - this would require global synchronization
27 * the ref can't hit 0 before the user drops the initial ref, so as long as we
28 * convert to non percpu mode before the initial ref is dropped everything
41 static unsigned long __percpu *percpu_count_ptr(struct percpu_ref *ref) in percpu_count_ptr() argument
44 (ref->percpu_count_ptr & ~__PERCPU_REF_ATOMIC_DEAD); in percpu_count_ptr()
49 * @ref: percpu_ref to initialize
54 * Initializes @ref. @ref starts out in percpu mode with a refcount of 1 unless
62 int percpu_ref_init(struct percpu_ref *ref, percpu_ref_func_t *release, in percpu_ref_init() argument
70 ref->percpu_count_ptr = (unsigned long) in percpu_ref_init()
72 if (!ref->percpu_count_ptr) in percpu_ref_init()
75 data = kzalloc(sizeof(*ref->data), gfp); in percpu_ref_init()
77 free_percpu((void __percpu *)ref->percpu_count_ptr); in percpu_ref_init()
85 ref->percpu_count_ptr |= __PERCPU_REF_ATOMIC; in percpu_ref_init()
92 ref->percpu_count_ptr |= __PERCPU_REF_DEAD; in percpu_ref_init()
100 data->ref = ref; in percpu_ref_init()
101 ref->data = data; in percpu_ref_init()
106 static void __percpu_ref_exit(struct percpu_ref *ref) in __percpu_ref_exit() argument
108 unsigned long __percpu *percpu_count = percpu_count_ptr(ref); in __percpu_ref_exit()
112 WARN_ON_ONCE(ref->data && ref->data->confirm_switch); in __percpu_ref_exit()
114 ref->percpu_count_ptr = __PERCPU_REF_ATOMIC_DEAD; in __percpu_ref_exit()
120 * @ref: percpu_ref to exit
122 * This function exits @ref. The caller is responsible for ensuring that
123 * @ref is no longer in active use. The usual places to invoke this
124 * function from are the @ref->release() callback or in init failure path
128 void percpu_ref_exit(struct percpu_ref *ref) in percpu_ref_exit() argument
130 struct percpu_ref_data *data = ref->data; in percpu_ref_exit()
133 __percpu_ref_exit(ref); in percpu_ref_exit()
139 ref->percpu_count_ptr |= atomic_long_read(&ref->data->count) << in percpu_ref_exit()
141 ref->data = NULL; in percpu_ref_exit()
152 struct percpu_ref *ref = data->ref; in percpu_ref_call_confirm_rcu() local
154 data->confirm_switch(ref); in percpu_ref_call_confirm_rcu()
159 __percpu_ref_exit(ref); in percpu_ref_call_confirm_rcu()
161 /* drop ref from percpu_ref_switch_to_atomic() */ in percpu_ref_call_confirm_rcu()
162 percpu_ref_put(ref); in percpu_ref_call_confirm_rcu()
169 struct percpu_ref *ref = data->ref; in percpu_ref_switch_to_atomic_rcu() local
170 unsigned long __percpu *percpu_count = percpu_count_ptr(ref); in percpu_ref_switch_to_atomic_rcu()
182 * to &ref->count; since gets could be happening on one cpu while puts in percpu_ref_switch_to_atomic_rcu()
184 * @ref->count to hit 0 before we've got a consistent value - but the in percpu_ref_switch_to_atomic_rcu()
188 * &ref->count; we need the bias value to prevent &ref->count from in percpu_ref_switch_to_atomic_rcu()
195 "percpu ref (%ps) <= 0 (%ld) after switching to atomic", in percpu_ref_switch_to_atomic_rcu()
198 /* @ref is viewed as dead on all CPUs, send out switch confirmation */ in percpu_ref_switch_to_atomic_rcu()
202 static void percpu_ref_noop_confirm_switch(struct percpu_ref *ref) in percpu_ref_noop_confirm_switch() argument
206 static void __percpu_ref_switch_to_atomic(struct percpu_ref *ref, in __percpu_ref_switch_to_atomic() argument
209 if (ref->percpu_count_ptr & __PERCPU_REF_ATOMIC) { in __percpu_ref_switch_to_atomic()
211 confirm_switch(ref); in __percpu_ref_switch_to_atomic()
216 ref->percpu_count_ptr |= __PERCPU_REF_ATOMIC; in __percpu_ref_switch_to_atomic()
222 ref->data->confirm_switch = confirm_switch ?: in __percpu_ref_switch_to_atomic()
225 percpu_ref_get(ref); /* put after confirmation */ in __percpu_ref_switch_to_atomic()
226 call_rcu(&ref->data->rcu, percpu_ref_switch_to_atomic_rcu); in __percpu_ref_switch_to_atomic()
229 static void __percpu_ref_switch_to_percpu(struct percpu_ref *ref) in __percpu_ref_switch_to_percpu() argument
231 unsigned long __percpu *percpu_count = percpu_count_ptr(ref); in __percpu_ref_switch_to_percpu()
236 if (!(ref->percpu_count_ptr & __PERCPU_REF_ATOMIC)) in __percpu_ref_switch_to_percpu()
239 if (WARN_ON_ONCE(!ref->data->allow_reinit)) in __percpu_ref_switch_to_percpu()
242 atomic_long_add(PERCPU_COUNT_BIAS, &ref->data->count); in __percpu_ref_switch_to_percpu()
253 smp_store_release(&ref->percpu_count_ptr, in __percpu_ref_switch_to_percpu()
254 ref->percpu_count_ptr & ~__PERCPU_REF_ATOMIC); in __percpu_ref_switch_to_percpu()
257 static void __percpu_ref_switch_mode(struct percpu_ref *ref, in __percpu_ref_switch_mode() argument
260 struct percpu_ref_data *data = ref->data; in __percpu_ref_switch_mode()
272 if (data->force_atomic || (ref->percpu_count_ptr & __PERCPU_REF_DEAD)) in __percpu_ref_switch_mode()
273 __percpu_ref_switch_to_atomic(ref, confirm_switch); in __percpu_ref_switch_mode()
275 __percpu_ref_switch_to_percpu(ref); in __percpu_ref_switch_mode()
280 * @ref: percpu_ref to switch to atomic mode
286 * Schedule switching of @ref to atomic mode. All its percpu counts will
291 * operations. Note that @ref will stay in atomic mode across kill/reinit
294 * This function may block if @ref is in the process of switching to atomic
295 * mode. If the caller ensures that @ref is not in the process of
298 void percpu_ref_switch_to_atomic(struct percpu_ref *ref, in percpu_ref_switch_to_atomic() argument
305 ref->data->force_atomic = true; in percpu_ref_switch_to_atomic()
306 __percpu_ref_switch_mode(ref, confirm_switch); in percpu_ref_switch_to_atomic()
314 * @ref: percpu_ref to switch to atomic mode
316 * Schedule switching the ref to atomic mode, and wait for the
320 void percpu_ref_switch_to_atomic_sync(struct percpu_ref *ref) in percpu_ref_switch_to_atomic_sync() argument
322 percpu_ref_switch_to_atomic(ref, NULL); in percpu_ref_switch_to_atomic_sync()
323 wait_event(percpu_ref_switch_waitq, !ref->data->confirm_switch); in percpu_ref_switch_to_atomic_sync()
329 * @ref: percpu_ref to switch to percpu mode
332 * To re-use an expired ref, use percpu_ref_reinit().
334 * Switch @ref to percpu mode. This function may be invoked concurrently
337 * by PERCPU_REF_INIT_ATOMIC or percpu_ref_switch_to_atomic(). If @ref is
341 * This function may block if @ref is in the process of switching to atomic
342 * mode. If the caller ensures that @ref is not in the process of
345 void percpu_ref_switch_to_percpu(struct percpu_ref *ref) in percpu_ref_switch_to_percpu() argument
351 ref->data->force_atomic = false; in percpu_ref_switch_to_percpu()
352 __percpu_ref_switch_mode(ref, NULL); in percpu_ref_switch_to_percpu()
359 * percpu_ref_kill_and_confirm - drop the initial ref and schedule confirmation
360 * @ref: percpu_ref to kill
365 * called after @ref is seen as dead from all CPUs at which point all
370 * but it may block if @confirm_kill is specified and @ref is in the
375 void percpu_ref_kill_and_confirm(struct percpu_ref *ref, in percpu_ref_kill_and_confirm() argument
382 WARN_ONCE(ref->percpu_count_ptr & __PERCPU_REF_DEAD, in percpu_ref_kill_and_confirm()
384 ref->data->release); in percpu_ref_kill_and_confirm()
386 ref->percpu_count_ptr |= __PERCPU_REF_DEAD; in percpu_ref_kill_and_confirm()
387 __percpu_ref_switch_mode(ref, confirm_kill); in percpu_ref_kill_and_confirm()
388 percpu_ref_put(ref); in percpu_ref_kill_and_confirm()
396 * @ref: percpu_ref to test
398 * Returns %true if @ref reached zero.
400 * This function is safe to call as long as @ref is between init and exit.
402 bool percpu_ref_is_zero(struct percpu_ref *ref) in percpu_ref_is_zero() argument
407 if (__ref_is_percpu(ref, &percpu_count)) in percpu_ref_is_zero()
412 if (ref->data) in percpu_ref_is_zero()
413 count = atomic_long_read(&ref->data->count); in percpu_ref_is_zero()
415 count = ref->percpu_count_ptr >> __PERCPU_REF_FLAG_BITS; in percpu_ref_is_zero()
424 * @ref: perpcu_ref to re-initialize
426 * Re-initialize @ref so that it's in the same state as when it finished
427 * percpu_ref_init() ignoring %PERCPU_REF_INIT_DEAD. @ref must have been
430 * Note that percpu_ref_tryget[_live]() are safe to perform on @ref while
433 void percpu_ref_reinit(struct percpu_ref *ref) in percpu_ref_reinit() argument
435 WARN_ON_ONCE(!percpu_ref_is_zero(ref)); in percpu_ref_reinit()
437 percpu_ref_resurrect(ref); in percpu_ref_reinit()
443 * @ref: perpcu_ref to resurrect
445 * Modify @ref so that it's in the same state as before percpu_ref_kill() was
446 * called. @ref must be dead but must not yet have exited.
448 * If @ref->release() frees @ref then the caller is responsible for
449 * guaranteeing that @ref->release() does not get called while this
452 * Note that percpu_ref_tryget[_live]() are safe to perform on @ref while
455 void percpu_ref_resurrect(struct percpu_ref *ref) in percpu_ref_resurrect() argument
462 WARN_ON_ONCE(!(ref->percpu_count_ptr & __PERCPU_REF_DEAD)); in percpu_ref_resurrect()
463 WARN_ON_ONCE(__ref_is_percpu(ref, &percpu_count)); in percpu_ref_resurrect()
465 ref->percpu_count_ptr &= ~__PERCPU_REF_DEAD; in percpu_ref_resurrect()
466 percpu_ref_get(ref); in percpu_ref_resurrect()
467 __percpu_ref_switch_mode(ref, NULL); in percpu_ref_resurrect()