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()
78 ref->percpu_count_ptr = 0; in percpu_ref_init()
86 ref->percpu_count_ptr |= __PERCPU_REF_ATOMIC; in percpu_ref_init()
93 ref->percpu_count_ptr |= __PERCPU_REF_DEAD; in percpu_ref_init()
101 data->ref = ref; in percpu_ref_init()
102 ref->data = data; in percpu_ref_init()
107 static void __percpu_ref_exit(struct percpu_ref *ref) in __percpu_ref_exit() argument
109 unsigned long __percpu *percpu_count = percpu_count_ptr(ref); in __percpu_ref_exit()
113 WARN_ON_ONCE(ref->data && ref->data->confirm_switch); in __percpu_ref_exit()
115 ref->percpu_count_ptr = __PERCPU_REF_ATOMIC_DEAD; in __percpu_ref_exit()
121 * @ref: percpu_ref to exit
123 * This function exits @ref. The caller is responsible for ensuring that
124 * @ref is no longer in active use. The usual places to invoke this
125 * function from are the @ref->release() callback or in init failure path
129 void percpu_ref_exit(struct percpu_ref *ref) in percpu_ref_exit() argument
131 struct percpu_ref_data *data = ref->data; in percpu_ref_exit()
134 __percpu_ref_exit(ref); in percpu_ref_exit()
140 ref->percpu_count_ptr |= atomic_long_read(&ref->data->count) << in percpu_ref_exit()
142 ref->data = NULL; in percpu_ref_exit()
153 struct percpu_ref *ref = data->ref; in percpu_ref_call_confirm_rcu() local
155 data->confirm_switch(ref); in percpu_ref_call_confirm_rcu()
160 __percpu_ref_exit(ref); in percpu_ref_call_confirm_rcu()
162 /* drop ref from percpu_ref_switch_to_atomic() */ in percpu_ref_call_confirm_rcu()
163 percpu_ref_put(ref); in percpu_ref_call_confirm_rcu()
170 struct percpu_ref *ref = data->ref; in percpu_ref_switch_to_atomic_rcu() local
171 unsigned long __percpu *percpu_count = percpu_count_ptr(ref); in percpu_ref_switch_to_atomic_rcu()
183 * to &ref->count; since gets could be happening on one cpu while puts in percpu_ref_switch_to_atomic_rcu()
185 * @ref->count to hit 0 before we've got a consistent value - but the in percpu_ref_switch_to_atomic_rcu()
189 * &ref->count; we need the bias value to prevent &ref->count from in percpu_ref_switch_to_atomic_rcu()
196 "percpu ref (%ps) <= 0 (%ld) after switching to atomic", in percpu_ref_switch_to_atomic_rcu()
199 /* @ref is viewed as dead on all CPUs, send out switch confirmation */ in percpu_ref_switch_to_atomic_rcu()
203 static void percpu_ref_noop_confirm_switch(struct percpu_ref *ref) in percpu_ref_noop_confirm_switch() argument
207 static void __percpu_ref_switch_to_atomic(struct percpu_ref *ref, in __percpu_ref_switch_to_atomic() argument
210 if (ref->percpu_count_ptr & __PERCPU_REF_ATOMIC) { in __percpu_ref_switch_to_atomic()
212 confirm_switch(ref); in __percpu_ref_switch_to_atomic()
217 ref->percpu_count_ptr |= __PERCPU_REF_ATOMIC; in __percpu_ref_switch_to_atomic()
223 ref->data->confirm_switch = confirm_switch ?: in __percpu_ref_switch_to_atomic()
226 percpu_ref_get(ref); /* put after confirmation */ in __percpu_ref_switch_to_atomic()
227 call_rcu(&ref->data->rcu, percpu_ref_switch_to_atomic_rcu); in __percpu_ref_switch_to_atomic()
230 static void __percpu_ref_switch_to_percpu(struct percpu_ref *ref) in __percpu_ref_switch_to_percpu() argument
232 unsigned long __percpu *percpu_count = percpu_count_ptr(ref); in __percpu_ref_switch_to_percpu()
237 if (!(ref->percpu_count_ptr & __PERCPU_REF_ATOMIC)) in __percpu_ref_switch_to_percpu()
240 if (WARN_ON_ONCE(!ref->data->allow_reinit)) in __percpu_ref_switch_to_percpu()
243 atomic_long_add(PERCPU_COUNT_BIAS, &ref->data->count); in __percpu_ref_switch_to_percpu()
254 smp_store_release(&ref->percpu_count_ptr, in __percpu_ref_switch_to_percpu()
255 ref->percpu_count_ptr & ~__PERCPU_REF_ATOMIC); in __percpu_ref_switch_to_percpu()
258 static void __percpu_ref_switch_mode(struct percpu_ref *ref, in __percpu_ref_switch_mode() argument
261 struct percpu_ref_data *data = ref->data; in __percpu_ref_switch_mode()
273 if (data->force_atomic || (ref->percpu_count_ptr & __PERCPU_REF_DEAD)) in __percpu_ref_switch_mode()
274 __percpu_ref_switch_to_atomic(ref, confirm_switch); in __percpu_ref_switch_mode()
276 __percpu_ref_switch_to_percpu(ref); in __percpu_ref_switch_mode()
281 * @ref: percpu_ref to switch to atomic mode
287 * Schedule switching of @ref to atomic mode. All its percpu counts will
292 * operations. Note that @ref will stay in atomic mode across kill/reinit
295 * This function may block if @ref is in the process of switching to atomic
296 * mode. If the caller ensures that @ref is not in the process of
299 void percpu_ref_switch_to_atomic(struct percpu_ref *ref, in percpu_ref_switch_to_atomic() argument
306 ref->data->force_atomic = true; in percpu_ref_switch_to_atomic()
307 __percpu_ref_switch_mode(ref, confirm_switch); in percpu_ref_switch_to_atomic()
315 * @ref: percpu_ref to switch to atomic mode
317 * Schedule switching the ref to atomic mode, and wait for the
321 void percpu_ref_switch_to_atomic_sync(struct percpu_ref *ref) in percpu_ref_switch_to_atomic_sync() argument
323 percpu_ref_switch_to_atomic(ref, NULL); in percpu_ref_switch_to_atomic_sync()
324 wait_event(percpu_ref_switch_waitq, !ref->data->confirm_switch); in percpu_ref_switch_to_atomic_sync()
330 * @ref: percpu_ref to switch to percpu mode
333 * To re-use an expired ref, use percpu_ref_reinit().
335 * Switch @ref to percpu mode. This function may be invoked concurrently
338 * by PERCPU_REF_INIT_ATOMIC or percpu_ref_switch_to_atomic(). If @ref is
342 * This function may block if @ref is in the process of switching to atomic
343 * mode. If the caller ensures that @ref is not in the process of
346 void percpu_ref_switch_to_percpu(struct percpu_ref *ref) in percpu_ref_switch_to_percpu() argument
352 ref->data->force_atomic = false; in percpu_ref_switch_to_percpu()
353 __percpu_ref_switch_mode(ref, NULL); in percpu_ref_switch_to_percpu()
360 * percpu_ref_kill_and_confirm - drop the initial ref and schedule confirmation
361 * @ref: percpu_ref to kill
366 * called after @ref is seen as dead from all CPUs at which point all
371 * but it may block if @confirm_kill is specified and @ref is in the
376 void percpu_ref_kill_and_confirm(struct percpu_ref *ref, in percpu_ref_kill_and_confirm() argument
383 WARN_ONCE(ref->percpu_count_ptr & __PERCPU_REF_DEAD, in percpu_ref_kill_and_confirm()
385 ref->data->release); in percpu_ref_kill_and_confirm()
387 ref->percpu_count_ptr |= __PERCPU_REF_DEAD; in percpu_ref_kill_and_confirm()
388 __percpu_ref_switch_mode(ref, confirm_kill); in percpu_ref_kill_and_confirm()
389 percpu_ref_put(ref); in percpu_ref_kill_and_confirm()
397 * @ref: percpu_ref to test
399 * Returns %true if @ref reached zero.
401 * This function is safe to call as long as @ref is between init and exit.
403 bool percpu_ref_is_zero(struct percpu_ref *ref) in percpu_ref_is_zero() argument
408 if (__ref_is_percpu(ref, &percpu_count)) in percpu_ref_is_zero()
413 if (ref->data) in percpu_ref_is_zero()
414 count = atomic_long_read(&ref->data->count); in percpu_ref_is_zero()
416 count = ref->percpu_count_ptr >> __PERCPU_REF_FLAG_BITS; in percpu_ref_is_zero()
425 * @ref: perpcu_ref to re-initialize
427 * Re-initialize @ref so that it's in the same state as when it finished
428 * percpu_ref_init() ignoring %PERCPU_REF_INIT_DEAD. @ref must have been
431 * Note that percpu_ref_tryget[_live]() are safe to perform on @ref while
434 void percpu_ref_reinit(struct percpu_ref *ref) in percpu_ref_reinit() argument
436 WARN_ON_ONCE(!percpu_ref_is_zero(ref)); in percpu_ref_reinit()
438 percpu_ref_resurrect(ref); in percpu_ref_reinit()
444 * @ref: perpcu_ref to resurrect
446 * Modify @ref so that it's in the same state as before percpu_ref_kill() was
447 * called. @ref must be dead but must not yet have exited.
449 * If @ref->release() frees @ref then the caller is responsible for
450 * guaranteeing that @ref->release() does not get called while this
453 * Note that percpu_ref_tryget[_live]() are safe to perform on @ref while
456 void percpu_ref_resurrect(struct percpu_ref *ref) in percpu_ref_resurrect() argument
463 WARN_ON_ONCE(!(ref->percpu_count_ptr & __PERCPU_REF_DEAD)); in percpu_ref_resurrect()
464 WARN_ON_ONCE(__ref_is_percpu(ref, &percpu_count)); in percpu_ref_resurrect()
466 ref->percpu_count_ptr &= ~__PERCPU_REF_DEAD; in percpu_ref_resurrect()
467 percpu_ref_get(ref); in percpu_ref_resurrect()
468 __percpu_ref_switch_mode(ref, NULL); in percpu_ref_resurrect()