1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2215e262fSKent Overstreet /*
3215e262fSKent Overstreet  * Percpu refcounts:
4215e262fSKent Overstreet  * (C) 2012 Google, Inc.
5215e262fSKent Overstreet  * Author: Kent Overstreet <[email protected]>
6215e262fSKent Overstreet  *
7215e262fSKent Overstreet  * This implements a refcount with similar semantics to atomic_t - atomic_inc(),
8215e262fSKent Overstreet  * atomic_dec_and_test() - but percpu.
9215e262fSKent Overstreet  *
10215e262fSKent Overstreet  * There's one important difference between percpu refs and normal atomic_t
11215e262fSKent Overstreet  * refcounts; you have to keep track of your initial refcount, and then when you
12215e262fSKent Overstreet  * start shutting down you call percpu_ref_kill() _before_ dropping the initial
13215e262fSKent Overstreet  * refcount.
14215e262fSKent Overstreet  *
15215e262fSKent Overstreet  * The refcount will have a range of 0 to ((1U << 31) - 1), i.e. one bit less
16215e262fSKent Overstreet  * than an atomic_t - this is because of the way shutdown works, see
17eecc16baSTejun Heo  * percpu_ref_kill()/PERCPU_COUNT_BIAS.
18215e262fSKent Overstreet  *
19215e262fSKent Overstreet  * Before you call percpu_ref_kill(), percpu_ref_put() does not check for the
20215e262fSKent Overstreet  * refcount hitting 0 - it can't, if it was in percpu mode. percpu_ref_kill()
21215e262fSKent Overstreet  * puts the ref back in single atomic_t mode, collecting the per cpu refs and
22215e262fSKent Overstreet  * issuing the appropriate barriers, and then marks the ref as shutting down so
23215e262fSKent Overstreet  * that percpu_ref_put() will check for the ref hitting 0.  After it returns,
24215e262fSKent Overstreet  * it's safe to drop the initial ref.
25215e262fSKent Overstreet  *
26215e262fSKent Overstreet  * USAGE:
27215e262fSKent Overstreet  *
28215e262fSKent Overstreet  * See fs/aio.c for some example usage; it's used there for struct kioctx, which
29215e262fSKent Overstreet  * is created when userspaces calls io_setup(), and destroyed when userspace
30215e262fSKent Overstreet  * calls io_destroy() or the process exits.
31215e262fSKent Overstreet  *
32215e262fSKent Overstreet  * In the aio code, kill_ioctx() is called when we wish to destroy a kioctx; it
33b3a5d111STejun Heo  * removes the kioctx from the proccess's table of kioctxs and kills percpu_ref.
34b3a5d111STejun Heo  * After that, there can't be any new users of the kioctx (from lookup_ioctx())
35b3a5d111STejun Heo  * and it's then safe to drop the initial ref with percpu_ref_put().
36b3a5d111STejun Heo  *
37b3a5d111STejun Heo  * Note that the free path, free_ioctx(), needs to go through explicit call_rcu()
38b3a5d111STejun Heo  * to synchronize with RCU protected lookup_ioctx().  percpu_ref operations don't
39b3a5d111STejun Heo  * imply RCU grace periods of any kind and if a user wants to combine percpu_ref
40b3a5d111STejun Heo  * with RCU protection, it must be done explicitly.
41215e262fSKent Overstreet  *
42215e262fSKent Overstreet  * Code that does a two stage shutdown like this often needs some kind of
43215e262fSKent Overstreet  * explicit synchronization to ensure the initial refcount can only be dropped
44215e262fSKent Overstreet  * once - percpu_ref_kill() does this for you, it returns true once and false if
45215e262fSKent Overstreet  * someone else already called it. The aio code uses it this way, but it's not
46215e262fSKent Overstreet  * necessary if the code has some other mechanism to synchronize teardown.
47215e262fSKent Overstreet  * around.
48215e262fSKent Overstreet  */
49215e262fSKent Overstreet 
50215e262fSKent Overstreet #ifndef _LINUX_PERCPU_REFCOUNT_H
51215e262fSKent Overstreet #define _LINUX_PERCPU_REFCOUNT_H
52215e262fSKent Overstreet 
53215e262fSKent Overstreet #include <linux/atomic.h>
54215e262fSKent Overstreet #include <linux/percpu.h>
55215e262fSKent Overstreet #include <linux/rcupdate.h>
56*a4f1192cSAndy Shevchenko #include <linux/types.h>
57a34375efSTejun Heo #include <linux/gfp.h>
58215e262fSKent Overstreet 
59215e262fSKent Overstreet struct percpu_ref;
60ac899061STejun Heo typedef void (percpu_ref_func_t)(struct percpu_ref *);
61215e262fSKent Overstreet 
629e804d1fSTejun Heo /* flags set in the lower bits of percpu_ref->percpu_count_ptr */
639e804d1fSTejun Heo enum {
649e804d1fSTejun Heo 	__PERCPU_REF_ATOMIC	= 1LU << 0,	/* operating in atomic mode */
6527344a90STejun Heo 	__PERCPU_REF_DEAD	= 1LU << 1,	/* (being) killed */
6627344a90STejun Heo 	__PERCPU_REF_ATOMIC_DEAD = __PERCPU_REF_ATOMIC | __PERCPU_REF_DEAD,
6727344a90STejun Heo 
6827344a90STejun Heo 	__PERCPU_REF_FLAG_BITS	= 2,
699e804d1fSTejun Heo };
709e804d1fSTejun Heo 
712aad2a86STejun Heo /* @flags for percpu_ref_init() */
722aad2a86STejun Heo enum {
732aad2a86STejun Heo 	/*
742aad2a86STejun Heo 	 * Start w/ ref == 1 in atomic mode.  Can be switched to percpu
751cae13e7STejun Heo 	 * operation using percpu_ref_switch_to_percpu().  If initialized
761cae13e7STejun Heo 	 * with this flag, the ref will stay in atomic mode until
771cae13e7STejun Heo 	 * percpu_ref_switch_to_percpu() is invoked on it.
7809ed79d6SRoman Gushchin 	 * Implies ALLOW_REINIT.
792aad2a86STejun Heo 	 */
802aad2a86STejun Heo 	PERCPU_REF_INIT_ATOMIC	= 1 << 0,
812aad2a86STejun Heo 
822aad2a86STejun Heo 	/*
832aad2a86STejun Heo 	 * Start dead w/ ref == 0 in atomic mode.  Must be revived with
8409ed79d6SRoman Gushchin 	 * percpu_ref_reinit() before used.  Implies INIT_ATOMIC and
8509ed79d6SRoman Gushchin 	 * ALLOW_REINIT.
862aad2a86STejun Heo 	 */
872aad2a86STejun Heo 	PERCPU_REF_INIT_DEAD	= 1 << 1,
8809ed79d6SRoman Gushchin 
8909ed79d6SRoman Gushchin 	/*
9009ed79d6SRoman Gushchin 	 * Allow switching from atomic mode to percpu mode.
9109ed79d6SRoman Gushchin 	 */
9209ed79d6SRoman Gushchin 	PERCPU_REF_ALLOW_REINIT	= 1 << 2,
932aad2a86STejun Heo };
942aad2a86STejun Heo 
952b0d3d3eSMing Lei struct percpu_ref_data {
96e625305bSTejun Heo 	atomic_long_t		count;
97ac899061STejun Heo 	percpu_ref_func_t	*release;
989e804d1fSTejun Heo 	percpu_ref_func_t	*confirm_switch;
991cae13e7STejun Heo 	bool			force_atomic:1;
1007d9ab9b6SRoman Gushchin 	bool			allow_reinit:1;
101215e262fSKent Overstreet 	struct rcu_head		rcu;
1022b0d3d3eSMing Lei 	struct percpu_ref	*ref;
1032b0d3d3eSMing Lei };
1042b0d3d3eSMing Lei 
1052b0d3d3eSMing Lei struct percpu_ref {
1062b0d3d3eSMing Lei 	/*
1072b0d3d3eSMing Lei 	 * The low bit of the pointer indicates whether the ref is in percpu
1082b0d3d3eSMing Lei 	 * mode; if set, then get/put will manipulate the atomic_t.
1092b0d3d3eSMing Lei 	 */
1102b0d3d3eSMing Lei 	unsigned long		percpu_count_ptr;
1112b0d3d3eSMing Lei 
1122b0d3d3eSMing Lei 	/*
1132b0d3d3eSMing Lei 	 * 'percpu_ref' is often embedded into user structure, and only
1142b0d3d3eSMing Lei 	 * 'percpu_count_ptr' is required in fast path, move other fields
1152b0d3d3eSMing Lei 	 * into 'percpu_ref_data', so we can reduce memory footprint in
1162b0d3d3eSMing Lei 	 * fast path.
1172b0d3d3eSMing Lei 	 */
1182b0d3d3eSMing Lei 	struct percpu_ref_data  *data;
119215e262fSKent Overstreet };
120215e262fSKent Overstreet 
121acac7883STejun Heo int __must_check percpu_ref_init(struct percpu_ref *ref,
1222aad2a86STejun Heo 				 percpu_ref_func_t *release, unsigned int flags,
1232aad2a86STejun Heo 				 gfp_t gfp);
1249a1049daSTejun Heo void percpu_ref_exit(struct percpu_ref *ref);
125490c79a6STejun Heo void percpu_ref_switch_to_atomic(struct percpu_ref *ref,
126490c79a6STejun Heo 				 percpu_ref_func_t *confirm_switch);
127210f7cdcSNeilBrown void percpu_ref_switch_to_atomic_sync(struct percpu_ref *ref);
128f47ad457STejun Heo void percpu_ref_switch_to_percpu(struct percpu_ref *ref);
129dbece3a0STejun Heo void percpu_ref_kill_and_confirm(struct percpu_ref *ref,
130dbece3a0STejun Heo 				 percpu_ref_func_t *confirm_kill);
13118c9a6bbSBart Van Assche void percpu_ref_resurrect(struct percpu_ref *ref);
132f47ad457STejun Heo void percpu_ref_reinit(struct percpu_ref *ref);
1332b0d3d3eSMing Lei bool percpu_ref_is_zero(struct percpu_ref *ref);
134dbece3a0STejun Heo 
135dbece3a0STejun Heo /**
136dbece3a0STejun Heo  * percpu_ref_kill - drop the initial ref
137dbece3a0STejun Heo  * @ref: percpu_ref to kill
138dbece3a0STejun Heo  *
139dbece3a0STejun Heo  * Must be used to drop the initial ref on a percpu refcount; must be called
140dbece3a0STejun Heo  * precisely once before shutdown.
141dbece3a0STejun Heo  *
142b3a5d111STejun Heo  * Switches @ref into atomic mode before gathering up the percpu counters
143b3a5d111STejun Heo  * and dropping the initial ref.
144b3a5d111STejun Heo  *
145b3a5d111STejun Heo  * There are no implied RCU grace periods between kill and release.
146dbece3a0STejun Heo  */
percpu_ref_kill(struct percpu_ref * ref)147dbece3a0STejun Heo static inline void percpu_ref_kill(struct percpu_ref *ref)
148dbece3a0STejun Heo {
1494d414269SGuillaume Gomez 	percpu_ref_kill_and_confirm(ref, NULL);
150dbece3a0STejun Heo }
151215e262fSKent Overstreet 
152eae7975dSTejun Heo /*
153eae7975dSTejun Heo  * Internal helper.  Don't use outside percpu-refcount proper.  The
154eae7975dSTejun Heo  * function doesn't return the pointer and let the caller test it for NULL
155eae7975dSTejun Heo  * because doing so forces the compiler to generate two conditional
156eecc16baSTejun Heo  * branches as it can't assume that @ref->percpu_count is not NULL.
157eae7975dSTejun Heo  */
__ref_is_percpu(struct percpu_ref * ref,unsigned long __percpu ** percpu_countp)1589e804d1fSTejun Heo static inline bool __ref_is_percpu(struct percpu_ref *ref,
159eecc16baSTejun Heo 					  unsigned long __percpu **percpu_countp)
160eae7975dSTejun Heo {
1616810e4a3STejun Heo 	unsigned long percpu_ptr;
1626810e4a3STejun Heo 
1636810e4a3STejun Heo 	/*
1646810e4a3STejun Heo 	 * The value of @ref->percpu_count_ptr is tested for
1656810e4a3STejun Heo 	 * !__PERCPU_REF_ATOMIC, which may be set asynchronously, and then
1666810e4a3STejun Heo 	 * used as a pointer.  If the compiler generates a separate fetch
1676810e4a3STejun Heo 	 * when using it as a pointer, __PERCPU_REF_ATOMIC may be set in
1686810e4a3STejun Heo 	 * between contaminating the pointer value, meaning that
169ed8ebd1dSTejun Heo 	 * READ_ONCE() is required when fetching it.
170b393e8b3SPaul E. McKenney 	 *
171c6cd2e01SWill Deacon 	 * The dependency ordering from the READ_ONCE() pairs
172b393e8b3SPaul E. McKenney 	 * with smp_store_release() in __percpu_ref_switch_to_percpu().
1736810e4a3STejun Heo 	 */
174ed8ebd1dSTejun Heo 	percpu_ptr = READ_ONCE(ref->percpu_count_ptr);
175ed8ebd1dSTejun Heo 
1764aab3b5bSTejun Heo 	/*
1774aab3b5bSTejun Heo 	 * Theoretically, the following could test just ATOMIC; however,
1784aab3b5bSTejun Heo 	 * then we'd have to mask off DEAD separately as DEAD may be
1794aab3b5bSTejun Heo 	 * visible without ATOMIC if we race with percpu_ref_kill().  DEAD
1804aab3b5bSTejun Heo 	 * implies ATOMIC anyway.  Test them together.
1814aab3b5bSTejun Heo 	 */
1824aab3b5bSTejun Heo 	if (unlikely(percpu_ptr & __PERCPU_REF_ATOMIC_DEAD))
183eae7975dSTejun Heo 		return false;
184eae7975dSTejun Heo 
185eecc16baSTejun Heo 	*percpu_countp = (unsigned long __percpu *)percpu_ptr;
186eae7975dSTejun Heo 	return true;
187eae7975dSTejun Heo }
188215e262fSKent Overstreet 
189215e262fSKent Overstreet /**
190e8ea14ccSJohannes Weiner  * percpu_ref_get_many - increment a percpu refcount
191e8ea14ccSJohannes Weiner  * @ref: percpu_ref to get
192e8ea14ccSJohannes Weiner  * @nr: number of references to get
193e8ea14ccSJohannes Weiner  *
194e8ea14ccSJohannes Weiner  * Analogous to atomic_long_add().
195e8ea14ccSJohannes Weiner  *
196e8ea14ccSJohannes Weiner  * This function is safe to call as long as @ref is between init and exit.
197e8ea14ccSJohannes Weiner  */
percpu_ref_get_many(struct percpu_ref * ref,unsigned long nr)198e8ea14ccSJohannes Weiner static inline void percpu_ref_get_many(struct percpu_ref *ref, unsigned long nr)
199e8ea14ccSJohannes Weiner {
200e8ea14ccSJohannes Weiner 	unsigned long __percpu *percpu_count;
201e8ea14ccSJohannes Weiner 
2029e8d42a0SSebastian Andrzej Siewior 	rcu_read_lock();
203e8ea14ccSJohannes Weiner 
204e8ea14ccSJohannes Weiner 	if (__ref_is_percpu(ref, &percpu_count))
205e8ea14ccSJohannes Weiner 		this_cpu_add(*percpu_count, nr);
206e8ea14ccSJohannes Weiner 	else
2072b0d3d3eSMing Lei 		atomic_long_add(nr, &ref->data->count);
208e8ea14ccSJohannes Weiner 
2099e8d42a0SSebastian Andrzej Siewior 	rcu_read_unlock();
210e8ea14ccSJohannes Weiner }
211e8ea14ccSJohannes Weiner 
212e8ea14ccSJohannes Weiner /**
213215e262fSKent Overstreet  * percpu_ref_get - increment a percpu refcount
214ac899061STejun Heo  * @ref: percpu_ref to get
215215e262fSKent Overstreet  *
216c23c8082SZhen Lei  * Analogous to atomic_long_inc().
2176251f997STejun Heo  *
2186251f997STejun Heo  * This function is safe to call as long as @ref is between init and exit.
219215e262fSKent Overstreet  */
percpu_ref_get(struct percpu_ref * ref)220215e262fSKent Overstreet static inline void percpu_ref_get(struct percpu_ref *ref)
221215e262fSKent Overstreet {
222e8ea14ccSJohannes Weiner 	percpu_ref_get_many(ref, 1);
223215e262fSKent Overstreet }
224215e262fSKent Overstreet 
225215e262fSKent Overstreet /**
2264e5ef023SPavel Begunkov  * percpu_ref_tryget_many - try to increment a percpu refcount
2274e5ef023SPavel Begunkov  * @ref: percpu_ref to try-get
2284e5ef023SPavel Begunkov  * @nr: number of references to get
2294e5ef023SPavel Begunkov  *
2304e5ef023SPavel Begunkov  * Increment a percpu refcount  by @nr unless its count already reached zero.
2314e5ef023SPavel Begunkov  * Returns %true on success; %false on failure.
2324e5ef023SPavel Begunkov  *
2334e5ef023SPavel Begunkov  * This function is safe to call as long as @ref is between init and exit.
2344e5ef023SPavel Begunkov  */
percpu_ref_tryget_many(struct percpu_ref * ref,unsigned long nr)2354e5ef023SPavel Begunkov static inline bool percpu_ref_tryget_many(struct percpu_ref *ref,
2364e5ef023SPavel Begunkov 					  unsigned long nr)
2374e5ef023SPavel Begunkov {
2384e5ef023SPavel Begunkov 	unsigned long __percpu *percpu_count;
2394e5ef023SPavel Begunkov 	bool ret;
2404e5ef023SPavel Begunkov 
2414e5ef023SPavel Begunkov 	rcu_read_lock();
2424e5ef023SPavel Begunkov 
2434e5ef023SPavel Begunkov 	if (__ref_is_percpu(ref, &percpu_count)) {
2444e5ef023SPavel Begunkov 		this_cpu_add(*percpu_count, nr);
2454e5ef023SPavel Begunkov 		ret = true;
2464e5ef023SPavel Begunkov 	} else {
2472b0d3d3eSMing Lei 		ret = atomic_long_add_unless(&ref->data->count, nr, 0);
2484e5ef023SPavel Begunkov 	}
2494e5ef023SPavel Begunkov 
2504e5ef023SPavel Begunkov 	rcu_read_unlock();
2514e5ef023SPavel Begunkov 
2524e5ef023SPavel Begunkov 	return ret;
2534e5ef023SPavel Begunkov }
2544e5ef023SPavel Begunkov 
2554e5ef023SPavel Begunkov /**
2564fb6e250STejun Heo  * percpu_ref_tryget - try to increment a percpu refcount
2574fb6e250STejun Heo  * @ref: percpu_ref to try-get
2584fb6e250STejun Heo  *
2594fb6e250STejun Heo  * Increment a percpu refcount unless its count already reached zero.
2604fb6e250STejun Heo  * Returns %true on success; %false on failure.
2614fb6e250STejun Heo  *
2626251f997STejun Heo  * This function is safe to call as long as @ref is between init and exit.
2634fb6e250STejun Heo  */
percpu_ref_tryget(struct percpu_ref * ref)2644fb6e250STejun Heo static inline bool percpu_ref_tryget(struct percpu_ref *ref)
2654fb6e250STejun Heo {
2664e5ef023SPavel Begunkov 	return percpu_ref_tryget_many(ref, 1);
2674fb6e250STejun Heo }
2684fb6e250STejun Heo 
2694fb6e250STejun Heo /**
2703b13c168SPavel Begunkov  * percpu_ref_tryget_live_rcu - same as percpu_ref_tryget_live() but the
2713b13c168SPavel Begunkov  * caller is responsible for taking RCU.
2723b13c168SPavel Begunkov  *
2733b13c168SPavel Begunkov  * This function is safe to call as long as @ref is between init and exit.
2743b13c168SPavel Begunkov  */
percpu_ref_tryget_live_rcu(struct percpu_ref * ref)2753b13c168SPavel Begunkov static inline bool percpu_ref_tryget_live_rcu(struct percpu_ref *ref)
2763b13c168SPavel Begunkov {
2773b13c168SPavel Begunkov 	unsigned long __percpu *percpu_count;
2783b13c168SPavel Begunkov 	bool ret = false;
2793b13c168SPavel Begunkov 
2803b13c168SPavel Begunkov 	WARN_ON_ONCE(!rcu_read_lock_held());
2813b13c168SPavel Begunkov 
2823b13c168SPavel Begunkov 	if (likely(__ref_is_percpu(ref, &percpu_count))) {
2833b13c168SPavel Begunkov 		this_cpu_inc(*percpu_count);
2843b13c168SPavel Begunkov 		ret = true;
2853b13c168SPavel Begunkov 	} else if (!(ref->percpu_count_ptr & __PERCPU_REF_DEAD)) {
2863b13c168SPavel Begunkov 		ret = atomic_long_inc_not_zero(&ref->data->count);
2873b13c168SPavel Begunkov 	}
2883b13c168SPavel Begunkov 	return ret;
2893b13c168SPavel Begunkov }
2903b13c168SPavel Begunkov 
2913b13c168SPavel Begunkov /**
2922070d50eSTejun Heo  * percpu_ref_tryget_live - try to increment a live percpu refcount
293dbece3a0STejun Heo  * @ref: percpu_ref to try-get
294dbece3a0STejun Heo  *
295dbece3a0STejun Heo  * Increment a percpu refcount unless it has already been killed.  Returns
296dbece3a0STejun Heo  * %true on success; %false on failure.
297dbece3a0STejun Heo  *
2986251f997STejun Heo  * Completion of percpu_ref_kill() in itself doesn't guarantee that this
2996251f997STejun Heo  * function will fail.  For such guarantee, percpu_ref_kill_and_confirm()
3006251f997STejun Heo  * should be used.  After the confirm_kill callback is invoked, it's
3016251f997STejun Heo  * guaranteed that no new reference will be given out by
3026251f997STejun Heo  * percpu_ref_tryget_live().
3034fb6e250STejun Heo  *
3046251f997STejun Heo  * This function is safe to call as long as @ref is between init and exit.
305dbece3a0STejun Heo  */
percpu_ref_tryget_live(struct percpu_ref * ref)3062070d50eSTejun Heo static inline bool percpu_ref_tryget_live(struct percpu_ref *ref)
307dbece3a0STejun Heo {
308966d2b04SDouglas Miller 	bool ret = false;
309dbece3a0STejun Heo 
3109e8d42a0SSebastian Andrzej Siewior 	rcu_read_lock();
3113b13c168SPavel Begunkov 	ret = percpu_ref_tryget_live_rcu(ref);
3129e8d42a0SSebastian Andrzej Siewior 	rcu_read_unlock();
313dbece3a0STejun Heo 	return ret;
314dbece3a0STejun Heo }
315dbece3a0STejun Heo 
316dbece3a0STejun Heo /**
317e8ea14ccSJohannes Weiner  * percpu_ref_put_many - decrement a percpu refcount
318e8ea14ccSJohannes Weiner  * @ref: percpu_ref to put
319e8ea14ccSJohannes Weiner  * @nr: number of references to put
320e8ea14ccSJohannes Weiner  *
321e8ea14ccSJohannes Weiner  * Decrement the refcount, and if 0, call the release function (which was passed
322e8ea14ccSJohannes Weiner  * to percpu_ref_init())
323e8ea14ccSJohannes Weiner  *
324e8ea14ccSJohannes Weiner  * This function is safe to call as long as @ref is between init and exit.
325e8ea14ccSJohannes Weiner  */
percpu_ref_put_many(struct percpu_ref * ref,unsigned long nr)326e8ea14ccSJohannes Weiner static inline void percpu_ref_put_many(struct percpu_ref *ref, unsigned long nr)
327e8ea14ccSJohannes Weiner {
328e8ea14ccSJohannes Weiner 	unsigned long __percpu *percpu_count;
329e8ea14ccSJohannes Weiner 
3309e8d42a0SSebastian Andrzej Siewior 	rcu_read_lock();
331e8ea14ccSJohannes Weiner 
332e8ea14ccSJohannes Weiner 	if (__ref_is_percpu(ref, &percpu_count))
333e8ea14ccSJohannes Weiner 		this_cpu_sub(*percpu_count, nr);
3342b0d3d3eSMing Lei 	else if (unlikely(atomic_long_sub_and_test(nr, &ref->data->count)))
3352b0d3d3eSMing Lei 		ref->data->release(ref);
336e8ea14ccSJohannes Weiner 
3379e8d42a0SSebastian Andrzej Siewior 	rcu_read_unlock();
338e8ea14ccSJohannes Weiner }
339e8ea14ccSJohannes Weiner 
340e8ea14ccSJohannes Weiner /**
341215e262fSKent Overstreet  * percpu_ref_put - decrement a percpu refcount
342ac899061STejun Heo  * @ref: percpu_ref to put
343215e262fSKent Overstreet  *
344215e262fSKent Overstreet  * Decrement the refcount, and if 0, call the release function (which was passed
345215e262fSKent Overstreet  * to percpu_ref_init())
3466251f997STejun Heo  *
3476251f997STejun Heo  * This function is safe to call as long as @ref is between init and exit.
348215e262fSKent Overstreet  */
percpu_ref_put(struct percpu_ref * ref)349215e262fSKent Overstreet static inline void percpu_ref_put(struct percpu_ref *ref)
350215e262fSKent Overstreet {
351e8ea14ccSJohannes Weiner 	percpu_ref_put_many(ref, 1);
352215e262fSKent Overstreet }
353215e262fSKent Overstreet 
3542d722782STejun Heo /**
3554c907bafSTejun Heo  * percpu_ref_is_dying - test whether a percpu refcount is dying or dead
3564c907bafSTejun Heo  * @ref: percpu_ref to test
3574c907bafSTejun Heo  *
3584c907bafSTejun Heo  * Returns %true if @ref is dying or dead.
3594c907bafSTejun Heo  *
3604c907bafSTejun Heo  * This function is safe to call as long as @ref is between init and exit
3614c907bafSTejun Heo  * and the caller is responsible for synchronizing against state changes.
3624c907bafSTejun Heo  */
percpu_ref_is_dying(struct percpu_ref * ref)3634c907bafSTejun Heo static inline bool percpu_ref_is_dying(struct percpu_ref *ref)
3644c907bafSTejun Heo {
3654c907bafSTejun Heo 	return ref->percpu_count_ptr & __PERCPU_REF_DEAD;
3664c907bafSTejun Heo }
3674c907bafSTejun Heo 
368215e262fSKent Overstreet #endif
369