1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a818e526SJoe Perches #define pr_fmt(fmt) "%s: " fmt, __func__
3215e262fSKent Overstreet
4215e262fSKent Overstreet #include <linux/kernel.h>
5490c79a6STejun Heo #include <linux/sched.h>
6490c79a6STejun Heo #include <linux/wait.h>
72b0d3d3eSMing Lei #include <linux/slab.h>
83375efedSPaul E. McKenney #include <linux/mm.h>
9215e262fSKent Overstreet #include <linux/percpu-refcount.h>
10215e262fSKent Overstreet
11215e262fSKent Overstreet /*
12215e262fSKent Overstreet * Initially, a percpu refcount is just a set of percpu counters. Initially, we
13215e262fSKent Overstreet * don't try to detect the ref hitting 0 - which means that get/put can just
14215e262fSKent Overstreet * increment or decrement the local counter. Note that the counter on a
15215e262fSKent Overstreet * particular cpu can (and will) wrap - this is fine, when we go to shutdown the
16215e262fSKent Overstreet * percpu counters will all sum to the correct value
17215e262fSKent Overstreet *
18bdb428c8SBogdan Sikora * (More precisely: because modular arithmetic is commutative the sum of all the
19eecc16baSTejun Heo * percpu_count vars will be equal to what it would have been if all the gets
20eecc16baSTejun Heo * and puts were done to a single integer, even if some of the percpu integers
21215e262fSKent Overstreet * overflow or underflow).
22215e262fSKent Overstreet *
23215e262fSKent Overstreet * The real trick to implementing percpu refcounts is shutdown. We can't detect
24215e262fSKent Overstreet * the ref hitting 0 on every put - this would require global synchronization
25215e262fSKent Overstreet * and defeat the whole purpose of using percpu refs.
26215e262fSKent Overstreet *
27215e262fSKent Overstreet * What we do is require the user to keep track of the initial refcount; we know
28215e262fSKent Overstreet * the ref can't hit 0 before the user drops the initial ref, so as long as we
29215e262fSKent Overstreet * convert to non percpu mode before the initial ref is dropped everything
30215e262fSKent Overstreet * works.
31215e262fSKent Overstreet *
32215e262fSKent Overstreet * Converting to non percpu mode is done with some RCUish stuff in
33e625305bSTejun Heo * percpu_ref_kill. Additionally, we need a bias value so that the
34e625305bSTejun Heo * atomic_long_t can't hit 0 before we've added up all the percpu refs.
35215e262fSKent Overstreet */
36215e262fSKent Overstreet
37eecc16baSTejun Heo #define PERCPU_COUNT_BIAS (1LU << (BITS_PER_LONG - 1))
38215e262fSKent Overstreet
3933e465ceSTejun Heo static DEFINE_SPINLOCK(percpu_ref_switch_lock);
40490c79a6STejun Heo static DECLARE_WAIT_QUEUE_HEAD(percpu_ref_switch_waitq);
41490c79a6STejun Heo
percpu_count_ptr(struct percpu_ref * ref)42eecc16baSTejun Heo static unsigned long __percpu *percpu_count_ptr(struct percpu_ref *ref)
43eae7975dSTejun Heo {
44eecc16baSTejun Heo return (unsigned long __percpu *)
4527344a90STejun Heo (ref->percpu_count_ptr & ~__PERCPU_REF_ATOMIC_DEAD);
46eae7975dSTejun Heo }
47eae7975dSTejun Heo
48215e262fSKent Overstreet /**
49215e262fSKent Overstreet * percpu_ref_init - initialize a percpu refcount
50ac899061STejun Heo * @ref: percpu_ref to initialize
51215e262fSKent Overstreet * @release: function which will be called when refcount hits 0
522aad2a86STejun Heo * @flags: PERCPU_REF_INIT_* flags
53a34375efSTejun Heo * @gfp: allocation mask to use
54215e262fSKent Overstreet *
5515617dffSIra Weiny * Initializes @ref. @ref starts out in percpu mode with a refcount of 1 unless
5615617dffSIra Weiny * @flags contains PERCPU_REF_INIT_ATOMIC or PERCPU_REF_INIT_DEAD. These flags
5715617dffSIra Weiny * change the start state to atomic with the latter setting the initial refcount
5815617dffSIra Weiny * to 0. See the definitions of PERCPU_REF_INIT_* flags for flag behaviors.
59215e262fSKent Overstreet *
60215e262fSKent Overstreet * Note that @release must not sleep - it may potentially be called from RCU
61215e262fSKent Overstreet * callback context by percpu_ref_kill().
62215e262fSKent Overstreet */
percpu_ref_init(struct percpu_ref * ref,percpu_ref_func_t * release,unsigned int flags,gfp_t gfp)63a34375efSTejun Heo int percpu_ref_init(struct percpu_ref *ref, percpu_ref_func_t *release,
642aad2a86STejun Heo unsigned int flags, gfp_t gfp)
65215e262fSKent Overstreet {
6627344a90STejun Heo size_t align = max_t(size_t, 1 << __PERCPU_REF_FLAG_BITS,
6727344a90STejun Heo __alignof__(unsigned long));
682aad2a86STejun Heo unsigned long start_count = 0;
692b0d3d3eSMing Lei struct percpu_ref_data *data;
70215e262fSKent Overstreet
7127344a90STejun Heo ref->percpu_count_ptr = (unsigned long)
7227344a90STejun Heo __alloc_percpu_gfp(sizeof(unsigned long), align, gfp);
73eecc16baSTejun Heo if (!ref->percpu_count_ptr)
74215e262fSKent Overstreet return -ENOMEM;
75215e262fSKent Overstreet
762b0d3d3eSMing Lei data = kzalloc(sizeof(*ref->data), gfp);
772b0d3d3eSMing Lei if (!data) {
782b0d3d3eSMing Lei free_percpu((void __percpu *)ref->percpu_count_ptr);
79a9171431SAl Viro ref->percpu_count_ptr = 0;
802b0d3d3eSMing Lei return -ENOMEM;
812b0d3d3eSMing Lei }
822b0d3d3eSMing Lei
832b0d3d3eSMing Lei data->force_atomic = flags & PERCPU_REF_INIT_ATOMIC;
842b0d3d3eSMing Lei data->allow_reinit = flags & PERCPU_REF_ALLOW_REINIT;
851cae13e7STejun Heo
867d9ab9b6SRoman Gushchin if (flags & (PERCPU_REF_INIT_ATOMIC | PERCPU_REF_INIT_DEAD)) {
872aad2a86STejun Heo ref->percpu_count_ptr |= __PERCPU_REF_ATOMIC;
882b0d3d3eSMing Lei data->allow_reinit = true;
897d9ab9b6SRoman Gushchin } else {
902aad2a86STejun Heo start_count += PERCPU_COUNT_BIAS;
917d9ab9b6SRoman Gushchin }
922aad2a86STejun Heo
932aad2a86STejun Heo if (flags & PERCPU_REF_INIT_DEAD)
942aad2a86STejun Heo ref->percpu_count_ptr |= __PERCPU_REF_DEAD;
952aad2a86STejun Heo else
962aad2a86STejun Heo start_count++;
972aad2a86STejun Heo
982b0d3d3eSMing Lei atomic_long_set(&data->count, start_count);
992aad2a86STejun Heo
1002b0d3d3eSMing Lei data->release = release;
1012b0d3d3eSMing Lei data->confirm_switch = NULL;
1022b0d3d3eSMing Lei data->ref = ref;
1032b0d3d3eSMing Lei ref->data = data;
104215e262fSKent Overstreet return 0;
105215e262fSKent Overstreet }
1065e9dd373SMatias Bjorling EXPORT_SYMBOL_GPL(percpu_ref_init);
107215e262fSKent Overstreet
__percpu_ref_exit(struct percpu_ref * ref)1082b0d3d3eSMing Lei static void __percpu_ref_exit(struct percpu_ref *ref)
1092b0d3d3eSMing Lei {
1102b0d3d3eSMing Lei unsigned long __percpu *percpu_count = percpu_count_ptr(ref);
1112b0d3d3eSMing Lei
1122b0d3d3eSMing Lei if (percpu_count) {
1132b0d3d3eSMing Lei /* non-NULL confirm_switch indicates switching in progress */
1147ea6bf2eSMing Lei WARN_ON_ONCE(ref->data && ref->data->confirm_switch);
1152b0d3d3eSMing Lei free_percpu(percpu_count);
1162b0d3d3eSMing Lei ref->percpu_count_ptr = __PERCPU_REF_ATOMIC_DEAD;
1172b0d3d3eSMing Lei }
1182b0d3d3eSMing Lei }
1192b0d3d3eSMing Lei
120bc497bd3STejun Heo /**
1219a1049daSTejun Heo * percpu_ref_exit - undo percpu_ref_init()
1229a1049daSTejun Heo * @ref: percpu_ref to exit
123bc497bd3STejun Heo *
1249a1049daSTejun Heo * This function exits @ref. The caller is responsible for ensuring that
1259a1049daSTejun Heo * @ref is no longer in active use. The usual places to invoke this
1269a1049daSTejun Heo * function from are the @ref->release() callback or in init failure path
1279a1049daSTejun Heo * where percpu_ref_init() succeeded but other parts of the initialization
1289a1049daSTejun Heo * of the embedding object failed.
129bc497bd3STejun Heo */
percpu_ref_exit(struct percpu_ref * ref)1309a1049daSTejun Heo void percpu_ref_exit(struct percpu_ref *ref)
131bc497bd3STejun Heo {
1322b0d3d3eSMing Lei struct percpu_ref_data *data = ref->data;
1332b0d3d3eSMing Lei unsigned long flags;
134bc497bd3STejun Heo
1352b0d3d3eSMing Lei __percpu_ref_exit(ref);
1362b0d3d3eSMing Lei
1372b0d3d3eSMing Lei if (!data)
1382b0d3d3eSMing Lei return;
1392b0d3d3eSMing Lei
1402b0d3d3eSMing Lei spin_lock_irqsave(&percpu_ref_switch_lock, flags);
1412b0d3d3eSMing Lei ref->percpu_count_ptr |= atomic_long_read(&ref->data->count) <<
1422b0d3d3eSMing Lei __PERCPU_REF_FLAG_BITS;
1432b0d3d3eSMing Lei ref->data = NULL;
1442b0d3d3eSMing Lei spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
1452b0d3d3eSMing Lei
1462b0d3d3eSMing Lei kfree(data);
147bc497bd3STejun Heo }
1489a1049daSTejun Heo EXPORT_SYMBOL_GPL(percpu_ref_exit);
149bc497bd3STejun Heo
percpu_ref_call_confirm_rcu(struct rcu_head * rcu)150490c79a6STejun Heo static void percpu_ref_call_confirm_rcu(struct rcu_head *rcu)
151490c79a6STejun Heo {
1522b0d3d3eSMing Lei struct percpu_ref_data *data = container_of(rcu,
1532b0d3d3eSMing Lei struct percpu_ref_data, rcu);
1542b0d3d3eSMing Lei struct percpu_ref *ref = data->ref;
155490c79a6STejun Heo
1562b0d3d3eSMing Lei data->confirm_switch(ref);
1572b0d3d3eSMing Lei data->confirm_switch = NULL;
158490c79a6STejun Heo wake_up_all(&percpu_ref_switch_waitq);
159490c79a6STejun Heo
1602b0d3d3eSMing Lei if (!data->allow_reinit)
1612b0d3d3eSMing Lei __percpu_ref_exit(ref);
1627d9ab9b6SRoman Gushchin
163490c79a6STejun Heo /* drop ref from percpu_ref_switch_to_atomic() */
164490c79a6STejun Heo percpu_ref_put(ref);
165490c79a6STejun Heo }
166490c79a6STejun Heo
percpu_ref_switch_to_atomic_rcu(struct rcu_head * rcu)167490c79a6STejun Heo static void percpu_ref_switch_to_atomic_rcu(struct rcu_head *rcu)
168215e262fSKent Overstreet {
1692b0d3d3eSMing Lei struct percpu_ref_data *data = container_of(rcu,
1702b0d3d3eSMing Lei struct percpu_ref_data, rcu);
1712b0d3d3eSMing Lei struct percpu_ref *ref = data->ref;
172eecc16baSTejun Heo unsigned long __percpu *percpu_count = percpu_count_ptr(ref);
1733375efedSPaul E. McKenney static atomic_t underflows;
174e625305bSTejun Heo unsigned long count = 0;
175215e262fSKent Overstreet int cpu;
176215e262fSKent Overstreet
177215e262fSKent Overstreet for_each_possible_cpu(cpu)
178eecc16baSTejun Heo count += *per_cpu_ptr(percpu_count, cpu);
179215e262fSKent Overstreet
180a818e526SJoe Perches pr_debug("global %lu percpu %lu\n",
1812b0d3d3eSMing Lei atomic_long_read(&data->count), count);
182215e262fSKent Overstreet
183215e262fSKent Overstreet /*
184215e262fSKent Overstreet * It's crucial that we sum the percpu counters _before_ adding the sum
185215e262fSKent Overstreet * to &ref->count; since gets could be happening on one cpu while puts
186215e262fSKent Overstreet * happen on another, adding a single cpu's count could cause
187215e262fSKent Overstreet * @ref->count to hit 0 before we've got a consistent value - but the
188215e262fSKent Overstreet * sum of all the counts will be consistent and correct.
189215e262fSKent Overstreet *
190215e262fSKent Overstreet * Subtracting the bias value then has to happen _after_ adding count to
191215e262fSKent Overstreet * &ref->count; we need the bias value to prevent &ref->count from
192215e262fSKent Overstreet * reaching 0 before we add the percpu counts. But doing it at the same
193215e262fSKent Overstreet * time is equivalent and saves us atomic operations:
194215e262fSKent Overstreet */
1952b0d3d3eSMing Lei atomic_long_add((long)count - PERCPU_COUNT_BIAS, &data->count);
196215e262fSKent Overstreet
1973375efedSPaul E. McKenney if (WARN_ONCE(atomic_long_read(&data->count) <= 0,
198d75f773cSSakari Ailus "percpu ref (%ps) <= 0 (%ld) after switching to atomic",
1993375efedSPaul E. McKenney data->release, atomic_long_read(&data->count)) &&
2003375efedSPaul E. McKenney atomic_inc_return(&underflows) < 4) {
2013375efedSPaul E. McKenney pr_err("%s(): percpu_ref underflow", __func__);
2023375efedSPaul E. McKenney mem_dump_obj(data);
2033375efedSPaul E. McKenney }
204687b0ad2SKent Overstreet
205490c79a6STejun Heo /* @ref is viewed as dead on all CPUs, send out switch confirmation */
206490c79a6STejun Heo percpu_ref_call_confirm_rcu(rcu);
207490c79a6STejun Heo }
208490c79a6STejun Heo
percpu_ref_noop_confirm_switch(struct percpu_ref * ref)209490c79a6STejun Heo static void percpu_ref_noop_confirm_switch(struct percpu_ref *ref)
210490c79a6STejun Heo {
211490c79a6STejun Heo }
212490c79a6STejun Heo
__percpu_ref_switch_to_atomic(struct percpu_ref * ref,percpu_ref_func_t * confirm_switch)213490c79a6STejun Heo static void __percpu_ref_switch_to_atomic(struct percpu_ref *ref,
214490c79a6STejun Heo percpu_ref_func_t *confirm_switch)
215490c79a6STejun Heo {
21618808354STejun Heo if (ref->percpu_count_ptr & __PERCPU_REF_ATOMIC) {
21718808354STejun Heo if (confirm_switch)
218a2f5630cSTejun Heo confirm_switch(ref);
219b2302c7fSTejun Heo return;
220b2302c7fSTejun Heo }
221b2302c7fSTejun Heo
222b2302c7fSTejun Heo /* switching from percpu to atomic */
223b2302c7fSTejun Heo ref->percpu_count_ptr |= __PERCPU_REF_ATOMIC;
224b2302c7fSTejun Heo
225b2302c7fSTejun Heo /*
226b2302c7fSTejun Heo * Non-NULL ->confirm_switch is used to indicate that switching is
227b2302c7fSTejun Heo * in progress. Use noop one if unspecified.
228b2302c7fSTejun Heo */
2292b0d3d3eSMing Lei ref->data->confirm_switch = confirm_switch ?:
2302b0d3d3eSMing Lei percpu_ref_noop_confirm_switch;
231b2302c7fSTejun Heo
232b2302c7fSTejun Heo percpu_ref_get(ref); /* put after confirmation */
233*343a72e5SJoel Fernandes (Google) call_rcu_hurry(&ref->data->rcu,
234*343a72e5SJoel Fernandes (Google) percpu_ref_switch_to_atomic_rcu);
235b2302c7fSTejun Heo }
236b2302c7fSTejun Heo
__percpu_ref_switch_to_percpu(struct percpu_ref * ref)237b2302c7fSTejun Heo static void __percpu_ref_switch_to_percpu(struct percpu_ref *ref)
238b2302c7fSTejun Heo {
239b2302c7fSTejun Heo unsigned long __percpu *percpu_count = percpu_count_ptr(ref);
240b2302c7fSTejun Heo int cpu;
241b2302c7fSTejun Heo
242b2302c7fSTejun Heo BUG_ON(!percpu_count);
243b2302c7fSTejun Heo
244b2302c7fSTejun Heo if (!(ref->percpu_count_ptr & __PERCPU_REF_ATOMIC))
245b2302c7fSTejun Heo return;
246b2302c7fSTejun Heo
2472b0d3d3eSMing Lei if (WARN_ON_ONCE(!ref->data->allow_reinit))
2487d9ab9b6SRoman Gushchin return;
2497d9ab9b6SRoman Gushchin
2502b0d3d3eSMing Lei atomic_long_add(PERCPU_COUNT_BIAS, &ref->data->count);
251b2302c7fSTejun Heo
252b2302c7fSTejun Heo /*
253b393e8b3SPaul E. McKenney * Restore per-cpu operation. smp_store_release() is paired
254b393e8b3SPaul E. McKenney * with READ_ONCE() in __ref_is_percpu() and guarantees that the
255b393e8b3SPaul E. McKenney * zeroing is visible to all percpu accesses which can see the
256b393e8b3SPaul E. McKenney * following __PERCPU_REF_ATOMIC clearing.
257b2302c7fSTejun Heo */
258b2302c7fSTejun Heo for_each_possible_cpu(cpu)
259b2302c7fSTejun Heo *per_cpu_ptr(percpu_count, cpu) = 0;
260b2302c7fSTejun Heo
261b2302c7fSTejun Heo smp_store_release(&ref->percpu_count_ptr,
262b2302c7fSTejun Heo ref->percpu_count_ptr & ~__PERCPU_REF_ATOMIC);
263215e262fSKent Overstreet }
264215e262fSKent Overstreet
__percpu_ref_switch_mode(struct percpu_ref * ref,percpu_ref_func_t * confirm_switch)2653f49bdd9STejun Heo static void __percpu_ref_switch_mode(struct percpu_ref *ref,
2663f49bdd9STejun Heo percpu_ref_func_t *confirm_switch)
2673f49bdd9STejun Heo {
2682b0d3d3eSMing Lei struct percpu_ref_data *data = ref->data;
2692b0d3d3eSMing Lei
27033e465ceSTejun Heo lockdep_assert_held(&percpu_ref_switch_lock);
27133e465ceSTejun Heo
2723f49bdd9STejun Heo /*
2733f49bdd9STejun Heo * If the previous ATOMIC switching hasn't finished yet, wait for
2743f49bdd9STejun Heo * its completion. If the caller ensures that ATOMIC switching
2753f49bdd9STejun Heo * isn't in progress, this function can be called from any context.
2763f49bdd9STejun Heo */
2772b0d3d3eSMing Lei wait_event_lock_irq(percpu_ref_switch_waitq, !data->confirm_switch,
27833e465ceSTejun Heo percpu_ref_switch_lock);
2793f49bdd9STejun Heo
2809e9da02aSNikolay Borisov if (data->force_atomic || percpu_ref_is_dying(ref))
2813f49bdd9STejun Heo __percpu_ref_switch_to_atomic(ref, confirm_switch);
2823f49bdd9STejun Heo else
2833f49bdd9STejun Heo __percpu_ref_switch_to_percpu(ref);
2843f49bdd9STejun Heo }
2853f49bdd9STejun Heo
286215e262fSKent Overstreet /**
287490c79a6STejun Heo * percpu_ref_switch_to_atomic - switch a percpu_ref to atomic mode
288490c79a6STejun Heo * @ref: percpu_ref to switch to atomic mode
289490c79a6STejun Heo * @confirm_switch: optional confirmation callback
290215e262fSKent Overstreet *
291490c79a6STejun Heo * There's no reason to use this function for the usual reference counting.
292490c79a6STejun Heo * Use percpu_ref_kill[_and_confirm]().
293215e262fSKent Overstreet *
294490c79a6STejun Heo * Schedule switching of @ref to atomic mode. All its percpu counts will
295490c79a6STejun Heo * be collected to the main atomic counter. On completion, when all CPUs
296490c79a6STejun Heo * are guaraneed to be in atomic mode, @confirm_switch, which may not
297490c79a6STejun Heo * block, is invoked. This function may be invoked concurrently with all
298490c79a6STejun Heo * the get/put operations and can safely be mixed with kill and reinit
2991cae13e7STejun Heo * operations. Note that @ref will stay in atomic mode across kill/reinit
3001cae13e7STejun Heo * cycles until percpu_ref_switch_to_percpu() is called.
301490c79a6STejun Heo *
3023f49bdd9STejun Heo * This function may block if @ref is in the process of switching to atomic
3033f49bdd9STejun Heo * mode. If the caller ensures that @ref is not in the process of
3043f49bdd9STejun Heo * switching to atomic mode, this function can be called from any context.
305215e262fSKent Overstreet */
percpu_ref_switch_to_atomic(struct percpu_ref * ref,percpu_ref_func_t * confirm_switch)306490c79a6STejun Heo void percpu_ref_switch_to_atomic(struct percpu_ref *ref,
307490c79a6STejun Heo percpu_ref_func_t *confirm_switch)
308215e262fSKent Overstreet {
30933e465ceSTejun Heo unsigned long flags;
31033e465ceSTejun Heo
31133e465ceSTejun Heo spin_lock_irqsave(&percpu_ref_switch_lock, flags);
31233e465ceSTejun Heo
3132b0d3d3eSMing Lei ref->data->force_atomic = true;
3143f49bdd9STejun Heo __percpu_ref_switch_mode(ref, confirm_switch);
31533e465ceSTejun Heo
31633e465ceSTejun Heo spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
317215e262fSKent Overstreet }
318210f7cdcSNeilBrown EXPORT_SYMBOL_GPL(percpu_ref_switch_to_atomic);
319210f7cdcSNeilBrown
320210f7cdcSNeilBrown /**
321210f7cdcSNeilBrown * percpu_ref_switch_to_atomic_sync - switch a percpu_ref to atomic mode
322210f7cdcSNeilBrown * @ref: percpu_ref to switch to atomic mode
323210f7cdcSNeilBrown *
324210f7cdcSNeilBrown * Schedule switching the ref to atomic mode, and wait for the
325210f7cdcSNeilBrown * switch to complete. Caller must ensure that no other thread
326210f7cdcSNeilBrown * will switch back to percpu mode.
327210f7cdcSNeilBrown */
percpu_ref_switch_to_atomic_sync(struct percpu_ref * ref)328210f7cdcSNeilBrown void percpu_ref_switch_to_atomic_sync(struct percpu_ref *ref)
329210f7cdcSNeilBrown {
330210f7cdcSNeilBrown percpu_ref_switch_to_atomic(ref, NULL);
3312b0d3d3eSMing Lei wait_event(percpu_ref_switch_waitq, !ref->data->confirm_switch);
332210f7cdcSNeilBrown }
333210f7cdcSNeilBrown EXPORT_SYMBOL_GPL(percpu_ref_switch_to_atomic_sync);
334a2237370STejun Heo
335f47ad457STejun Heo /**
336f47ad457STejun Heo * percpu_ref_switch_to_percpu - switch a percpu_ref to percpu mode
337f47ad457STejun Heo * @ref: percpu_ref to switch to percpu mode
338f47ad457STejun Heo *
339f47ad457STejun Heo * There's no reason to use this function for the usual reference counting.
340f47ad457STejun Heo * To re-use an expired ref, use percpu_ref_reinit().
341f47ad457STejun Heo *
342f47ad457STejun Heo * Switch @ref to percpu mode. This function may be invoked concurrently
343f47ad457STejun Heo * with all the get/put operations and can safely be mixed with kill and
3441cae13e7STejun Heo * reinit operations. This function reverses the sticky atomic state set
3451cae13e7STejun Heo * by PERCPU_REF_INIT_ATOMIC or percpu_ref_switch_to_atomic(). If @ref is
3461cae13e7STejun Heo * dying or dead, the actual switching takes place on the following
3471cae13e7STejun Heo * percpu_ref_reinit().
348f47ad457STejun Heo *
3493f49bdd9STejun Heo * This function may block if @ref is in the process of switching to atomic
3503f49bdd9STejun Heo * mode. If the caller ensures that @ref is not in the process of
3513f49bdd9STejun Heo * switching to atomic mode, this function can be called from any context.
352f47ad457STejun Heo */
percpu_ref_switch_to_percpu(struct percpu_ref * ref)353f47ad457STejun Heo void percpu_ref_switch_to_percpu(struct percpu_ref *ref)
354f47ad457STejun Heo {
35533e465ceSTejun Heo unsigned long flags;
35633e465ceSTejun Heo
35733e465ceSTejun Heo spin_lock_irqsave(&percpu_ref_switch_lock, flags);
35833e465ceSTejun Heo
3592b0d3d3eSMing Lei ref->data->force_atomic = false;
3603f49bdd9STejun Heo __percpu_ref_switch_mode(ref, NULL);
36133e465ceSTejun Heo
36233e465ceSTejun Heo spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
363f47ad457STejun Heo }
364210f7cdcSNeilBrown EXPORT_SYMBOL_GPL(percpu_ref_switch_to_percpu);
365490c79a6STejun Heo
366490c79a6STejun Heo /**
367490c79a6STejun Heo * percpu_ref_kill_and_confirm - drop the initial ref and schedule confirmation
368490c79a6STejun Heo * @ref: percpu_ref to kill
369490c79a6STejun Heo * @confirm_kill: optional confirmation callback
370490c79a6STejun Heo *
371490c79a6STejun Heo * Equivalent to percpu_ref_kill() but also schedules kill confirmation if
372490c79a6STejun Heo * @confirm_kill is not NULL. @confirm_kill, which may not block, will be
373490c79a6STejun Heo * called after @ref is seen as dead from all CPUs at which point all
374490c79a6STejun Heo * further invocations of percpu_ref_tryget_live() will fail. See
375490c79a6STejun Heo * percpu_ref_tryget_live() for details.
376490c79a6STejun Heo *
377490c79a6STejun Heo * This function normally doesn't block and can be called from any context
378f47ad457STejun Heo * but it may block if @confirm_kill is specified and @ref is in the
379a2f5630cSTejun Heo * process of switching to atomic mode by percpu_ref_switch_to_atomic().
380b3a5d111STejun Heo *
381b3a5d111STejun Heo * There are no implied RCU grace periods between kill and release.
382490c79a6STejun Heo */
percpu_ref_kill_and_confirm(struct percpu_ref * ref,percpu_ref_func_t * confirm_kill)383490c79a6STejun Heo void percpu_ref_kill_and_confirm(struct percpu_ref *ref,
384490c79a6STejun Heo percpu_ref_func_t *confirm_kill)
385490c79a6STejun Heo {
38633e465ceSTejun Heo unsigned long flags;
38733e465ceSTejun Heo
38833e465ceSTejun Heo spin_lock_irqsave(&percpu_ref_switch_lock, flags);
38933e465ceSTejun Heo
3909e9da02aSNikolay Borisov WARN_ONCE(percpu_ref_is_dying(ref),
3912b0d3d3eSMing Lei "%s called more than once on %ps!", __func__,
3922b0d3d3eSMing Lei ref->data->release);
393490c79a6STejun Heo
394490c79a6STejun Heo ref->percpu_count_ptr |= __PERCPU_REF_DEAD;
3953f49bdd9STejun Heo __percpu_ref_switch_mode(ref, confirm_kill);
396490c79a6STejun Heo percpu_ref_put(ref);
39733e465ceSTejun Heo
39833e465ceSTejun Heo spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
399490c79a6STejun Heo }
400490c79a6STejun Heo EXPORT_SYMBOL_GPL(percpu_ref_kill_and_confirm);
401f47ad457STejun Heo
402f47ad457STejun Heo /**
4032b0d3d3eSMing Lei * percpu_ref_is_zero - test whether a percpu refcount reached zero
4042b0d3d3eSMing Lei * @ref: percpu_ref to test
4052b0d3d3eSMing Lei *
4062b0d3d3eSMing Lei * Returns %true if @ref reached zero.
4072b0d3d3eSMing Lei *
4082b0d3d3eSMing Lei * This function is safe to call as long as @ref is between init and exit.
4092b0d3d3eSMing Lei */
percpu_ref_is_zero(struct percpu_ref * ref)4102b0d3d3eSMing Lei bool percpu_ref_is_zero(struct percpu_ref *ref)
4112b0d3d3eSMing Lei {
4122b0d3d3eSMing Lei unsigned long __percpu *percpu_count;
4132b0d3d3eSMing Lei unsigned long count, flags;
4142b0d3d3eSMing Lei
4152b0d3d3eSMing Lei if (__ref_is_percpu(ref, &percpu_count))
4162b0d3d3eSMing Lei return false;
4172b0d3d3eSMing Lei
4182b0d3d3eSMing Lei /* protect us from being destroyed */
4192b0d3d3eSMing Lei spin_lock_irqsave(&percpu_ref_switch_lock, flags);
4202b0d3d3eSMing Lei if (ref->data)
4212b0d3d3eSMing Lei count = atomic_long_read(&ref->data->count);
4222b0d3d3eSMing Lei else
4232b0d3d3eSMing Lei count = ref->percpu_count_ptr >> __PERCPU_REF_FLAG_BITS;
4242b0d3d3eSMing Lei spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
4252b0d3d3eSMing Lei
4262b0d3d3eSMing Lei return count == 0;
4272b0d3d3eSMing Lei }
4282b0d3d3eSMing Lei EXPORT_SYMBOL_GPL(percpu_ref_is_zero);
4292b0d3d3eSMing Lei
4302b0d3d3eSMing Lei /**
431f47ad457STejun Heo * percpu_ref_reinit - re-initialize a percpu refcount
432f47ad457STejun Heo * @ref: perpcu_ref to re-initialize
433f47ad457STejun Heo *
434f47ad457STejun Heo * Re-initialize @ref so that it's in the same state as when it finished
4351cae13e7STejun Heo * percpu_ref_init() ignoring %PERCPU_REF_INIT_DEAD. @ref must have been
4361cae13e7STejun Heo * initialized successfully and reached 0 but not exited.
437f47ad457STejun Heo *
438f47ad457STejun Heo * Note that percpu_ref_tryget[_live]() are safe to perform on @ref while
439f47ad457STejun Heo * this function is in progress.
440f47ad457STejun Heo */
percpu_ref_reinit(struct percpu_ref * ref)441f47ad457STejun Heo void percpu_ref_reinit(struct percpu_ref *ref)
442f47ad457STejun Heo {
44318c9a6bbSBart Van Assche WARN_ON_ONCE(!percpu_ref_is_zero(ref));
44418c9a6bbSBart Van Assche
44518c9a6bbSBart Van Assche percpu_ref_resurrect(ref);
44618c9a6bbSBart Van Assche }
44718c9a6bbSBart Van Assche EXPORT_SYMBOL_GPL(percpu_ref_reinit);
44818c9a6bbSBart Van Assche
44918c9a6bbSBart Van Assche /**
45018c9a6bbSBart Van Assche * percpu_ref_resurrect - modify a percpu refcount from dead to live
45118c9a6bbSBart Van Assche * @ref: perpcu_ref to resurrect
45218c9a6bbSBart Van Assche *
45318c9a6bbSBart Van Assche * Modify @ref so that it's in the same state as before percpu_ref_kill() was
45418c9a6bbSBart Van Assche * called. @ref must be dead but must not yet have exited.
45518c9a6bbSBart Van Assche *
45618c9a6bbSBart Van Assche * If @ref->release() frees @ref then the caller is responsible for
45718c9a6bbSBart Van Assche * guaranteeing that @ref->release() does not get called while this
45818c9a6bbSBart Van Assche * function is in progress.
45918c9a6bbSBart Van Assche *
46018c9a6bbSBart Van Assche * Note that percpu_ref_tryget[_live]() are safe to perform on @ref while
46118c9a6bbSBart Van Assche * this function is in progress.
46218c9a6bbSBart Van Assche */
percpu_ref_resurrect(struct percpu_ref * ref)46318c9a6bbSBart Van Assche void percpu_ref_resurrect(struct percpu_ref *ref)
46418c9a6bbSBart Van Assche {
46518c9a6bbSBart Van Assche unsigned long __percpu *percpu_count;
46633e465ceSTejun Heo unsigned long flags;
46733e465ceSTejun Heo
46833e465ceSTejun Heo spin_lock_irqsave(&percpu_ref_switch_lock, flags);
46933e465ceSTejun Heo
4709e9da02aSNikolay Borisov WARN_ON_ONCE(!percpu_ref_is_dying(ref));
47118c9a6bbSBart Van Assche WARN_ON_ONCE(__ref_is_percpu(ref, &percpu_count));
472f47ad457STejun Heo
473f47ad457STejun Heo ref->percpu_count_ptr &= ~__PERCPU_REF_DEAD;
474f47ad457STejun Heo percpu_ref_get(ref);
4753f49bdd9STejun Heo __percpu_ref_switch_mode(ref, NULL);
47633e465ceSTejun Heo
47733e465ceSTejun Heo spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
478f47ad457STejun Heo }
47918c9a6bbSBart Van Assche EXPORT_SYMBOL_GPL(percpu_ref_resurrect);
480