Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Percpu refcounts:
0004  * (C) 2012 Google, Inc.
0005  * Author: Kent Overstreet <koverstreet@google.com>
0006  *
0007  * This implements a refcount with similar semantics to atomic_t - atomic_inc(),
0008  * atomic_dec_and_test() - but percpu.
0009  *
0010  * There's one important difference between percpu refs and normal atomic_t
0011  * refcounts; you have to keep track of your initial refcount, and then when you
0012  * start shutting down you call percpu_ref_kill() _before_ dropping the initial
0013  * refcount.
0014  *
0015  * The refcount will have a range of 0 to ((1U << 31) - 1), i.e. one bit less
0016  * than an atomic_t - this is because of the way shutdown works, see
0017  * percpu_ref_kill()/PERCPU_COUNT_BIAS.
0018  *
0019  * Before you call percpu_ref_kill(), percpu_ref_put() does not check for the
0020  * refcount hitting 0 - it can't, if it was in percpu mode. percpu_ref_kill()
0021  * puts the ref back in single atomic_t mode, collecting the per cpu refs and
0022  * issuing the appropriate barriers, and then marks the ref as shutting down so
0023  * that percpu_ref_put() will check for the ref hitting 0.  After it returns,
0024  * it's safe to drop the initial ref.
0025  *
0026  * USAGE:
0027  *
0028  * See fs/aio.c for some example usage; it's used there for struct kioctx, which
0029  * is created when userspaces calls io_setup(), and destroyed when userspace
0030  * calls io_destroy() or the process exits.
0031  *
0032  * In the aio code, kill_ioctx() is called when we wish to destroy a kioctx; it
0033  * removes the kioctx from the proccess's table of kioctxs and kills percpu_ref.
0034  * After that, there can't be any new users of the kioctx (from lookup_ioctx())
0035  * and it's then safe to drop the initial ref with percpu_ref_put().
0036  *
0037  * Note that the free path, free_ioctx(), needs to go through explicit call_rcu()
0038  * to synchronize with RCU protected lookup_ioctx().  percpu_ref operations don't
0039  * imply RCU grace periods of any kind and if a user wants to combine percpu_ref
0040  * with RCU protection, it must be done explicitly.
0041  *
0042  * Code that does a two stage shutdown like this often needs some kind of
0043  * explicit synchronization to ensure the initial refcount can only be dropped
0044  * once - percpu_ref_kill() does this for you, it returns true once and false if
0045  * someone else already called it. The aio code uses it this way, but it's not
0046  * necessary if the code has some other mechanism to synchronize teardown.
0047  * around.
0048  */
0049 
0050 #ifndef _LINUX_PERCPU_REFCOUNT_H
0051 #define _LINUX_PERCPU_REFCOUNT_H
0052 
0053 #include <linux/atomic.h>
0054 #include <linux/percpu.h>
0055 #include <linux/rcupdate.h>
0056 #include <linux/types.h>
0057 #include <linux/gfp.h>
0058 
0059 struct percpu_ref;
0060 typedef void (percpu_ref_func_t)(struct percpu_ref *);
0061 
0062 /* flags set in the lower bits of percpu_ref->percpu_count_ptr */
0063 enum {
0064     __PERCPU_REF_ATOMIC = 1LU << 0, /* operating in atomic mode */
0065     __PERCPU_REF_DEAD   = 1LU << 1, /* (being) killed */
0066     __PERCPU_REF_ATOMIC_DEAD = __PERCPU_REF_ATOMIC | __PERCPU_REF_DEAD,
0067 
0068     __PERCPU_REF_FLAG_BITS  = 2,
0069 };
0070 
0071 /* @flags for percpu_ref_init() */
0072 enum {
0073     /*
0074      * Start w/ ref == 1 in atomic mode.  Can be switched to percpu
0075      * operation using percpu_ref_switch_to_percpu().  If initialized
0076      * with this flag, the ref will stay in atomic mode until
0077      * percpu_ref_switch_to_percpu() is invoked on it.
0078      * Implies ALLOW_REINIT.
0079      */
0080     PERCPU_REF_INIT_ATOMIC  = 1 << 0,
0081 
0082     /*
0083      * Start dead w/ ref == 0 in atomic mode.  Must be revived with
0084      * percpu_ref_reinit() before used.  Implies INIT_ATOMIC and
0085      * ALLOW_REINIT.
0086      */
0087     PERCPU_REF_INIT_DEAD    = 1 << 1,
0088 
0089     /*
0090      * Allow switching from atomic mode to percpu mode.
0091      */
0092     PERCPU_REF_ALLOW_REINIT = 1 << 2,
0093 };
0094 
0095 struct percpu_ref_data {
0096     atomic_long_t       count;
0097     percpu_ref_func_t   *release;
0098     percpu_ref_func_t   *confirm_switch;
0099     bool            force_atomic:1;
0100     bool            allow_reinit:1;
0101     struct rcu_head     rcu;
0102     struct percpu_ref   *ref;
0103 };
0104 
0105 struct percpu_ref {
0106     /*
0107      * The low bit of the pointer indicates whether the ref is in percpu
0108      * mode; if set, then get/put will manipulate the atomic_t.
0109      */
0110     unsigned long       percpu_count_ptr;
0111 
0112     /*
0113      * 'percpu_ref' is often embedded into user structure, and only
0114      * 'percpu_count_ptr' is required in fast path, move other fields
0115      * into 'percpu_ref_data', so we can reduce memory footprint in
0116      * fast path.
0117      */
0118     struct percpu_ref_data  *data;
0119 };
0120 
0121 int __must_check percpu_ref_init(struct percpu_ref *ref,
0122                  percpu_ref_func_t *release, unsigned int flags,
0123                  gfp_t gfp);
0124 void percpu_ref_exit(struct percpu_ref *ref);
0125 void percpu_ref_switch_to_atomic(struct percpu_ref *ref,
0126                  percpu_ref_func_t *confirm_switch);
0127 void percpu_ref_switch_to_atomic_sync(struct percpu_ref *ref);
0128 void percpu_ref_switch_to_percpu(struct percpu_ref *ref);
0129 void percpu_ref_kill_and_confirm(struct percpu_ref *ref,
0130                  percpu_ref_func_t *confirm_kill);
0131 void percpu_ref_resurrect(struct percpu_ref *ref);
0132 void percpu_ref_reinit(struct percpu_ref *ref);
0133 bool percpu_ref_is_zero(struct percpu_ref *ref);
0134 
0135 /**
0136  * percpu_ref_kill - drop the initial ref
0137  * @ref: percpu_ref to kill
0138  *
0139  * Must be used to drop the initial ref on a percpu refcount; must be called
0140  * precisely once before shutdown.
0141  *
0142  * Switches @ref into atomic mode before gathering up the percpu counters
0143  * and dropping the initial ref.
0144  *
0145  * There are no implied RCU grace periods between kill and release.
0146  */
0147 static inline void percpu_ref_kill(struct percpu_ref *ref)
0148 {
0149     percpu_ref_kill_and_confirm(ref, NULL);
0150 }
0151 
0152 /*
0153  * Internal helper.  Don't use outside percpu-refcount proper.  The
0154  * function doesn't return the pointer and let the caller test it for NULL
0155  * because doing so forces the compiler to generate two conditional
0156  * branches as it can't assume that @ref->percpu_count is not NULL.
0157  */
0158 static inline bool __ref_is_percpu(struct percpu_ref *ref,
0159                       unsigned long __percpu **percpu_countp)
0160 {
0161     unsigned long percpu_ptr;
0162 
0163     /*
0164      * The value of @ref->percpu_count_ptr is tested for
0165      * !__PERCPU_REF_ATOMIC, which may be set asynchronously, and then
0166      * used as a pointer.  If the compiler generates a separate fetch
0167      * when using it as a pointer, __PERCPU_REF_ATOMIC may be set in
0168      * between contaminating the pointer value, meaning that
0169      * READ_ONCE() is required when fetching it.
0170      *
0171      * The dependency ordering from the READ_ONCE() pairs
0172      * with smp_store_release() in __percpu_ref_switch_to_percpu().
0173      */
0174     percpu_ptr = READ_ONCE(ref->percpu_count_ptr);
0175 
0176     /*
0177      * Theoretically, the following could test just ATOMIC; however,
0178      * then we'd have to mask off DEAD separately as DEAD may be
0179      * visible without ATOMIC if we race with percpu_ref_kill().  DEAD
0180      * implies ATOMIC anyway.  Test them together.
0181      */
0182     if (unlikely(percpu_ptr & __PERCPU_REF_ATOMIC_DEAD))
0183         return false;
0184 
0185     *percpu_countp = (unsigned long __percpu *)percpu_ptr;
0186     return true;
0187 }
0188 
0189 /**
0190  * percpu_ref_get_many - increment a percpu refcount
0191  * @ref: percpu_ref to get
0192  * @nr: number of references to get
0193  *
0194  * Analogous to atomic_long_add().
0195  *
0196  * This function is safe to call as long as @ref is between init and exit.
0197  */
0198 static inline void percpu_ref_get_many(struct percpu_ref *ref, unsigned long nr)
0199 {
0200     unsigned long __percpu *percpu_count;
0201 
0202     rcu_read_lock();
0203 
0204     if (__ref_is_percpu(ref, &percpu_count))
0205         this_cpu_add(*percpu_count, nr);
0206     else
0207         atomic_long_add(nr, &ref->data->count);
0208 
0209     rcu_read_unlock();
0210 }
0211 
0212 /**
0213  * percpu_ref_get - increment a percpu refcount
0214  * @ref: percpu_ref to get
0215  *
0216  * Analogous to atomic_long_inc().
0217  *
0218  * This function is safe to call as long as @ref is between init and exit.
0219  */
0220 static inline void percpu_ref_get(struct percpu_ref *ref)
0221 {
0222     percpu_ref_get_many(ref, 1);
0223 }
0224 
0225 /**
0226  * percpu_ref_tryget_many - try to increment a percpu refcount
0227  * @ref: percpu_ref to try-get
0228  * @nr: number of references to get
0229  *
0230  * Increment a percpu refcount  by @nr unless its count already reached zero.
0231  * Returns %true on success; %false on failure.
0232  *
0233  * This function is safe to call as long as @ref is between init and exit.
0234  */
0235 static inline bool percpu_ref_tryget_many(struct percpu_ref *ref,
0236                       unsigned long nr)
0237 {
0238     unsigned long __percpu *percpu_count;
0239     bool ret;
0240 
0241     rcu_read_lock();
0242 
0243     if (__ref_is_percpu(ref, &percpu_count)) {
0244         this_cpu_add(*percpu_count, nr);
0245         ret = true;
0246     } else {
0247         ret = atomic_long_add_unless(&ref->data->count, nr, 0);
0248     }
0249 
0250     rcu_read_unlock();
0251 
0252     return ret;
0253 }
0254 
0255 /**
0256  * percpu_ref_tryget - try to increment a percpu refcount
0257  * @ref: percpu_ref to try-get
0258  *
0259  * Increment a percpu refcount unless its count already reached zero.
0260  * Returns %true on success; %false on failure.
0261  *
0262  * This function is safe to call as long as @ref is between init and exit.
0263  */
0264 static inline bool percpu_ref_tryget(struct percpu_ref *ref)
0265 {
0266     return percpu_ref_tryget_many(ref, 1);
0267 }
0268 
0269 /**
0270  * percpu_ref_tryget_live_rcu - same as percpu_ref_tryget_live() but the
0271  * caller is responsible for taking RCU.
0272  *
0273  * This function is safe to call as long as @ref is between init and exit.
0274  */
0275 static inline bool percpu_ref_tryget_live_rcu(struct percpu_ref *ref)
0276 {
0277     unsigned long __percpu *percpu_count;
0278     bool ret = false;
0279 
0280     WARN_ON_ONCE(!rcu_read_lock_held());
0281 
0282     if (likely(__ref_is_percpu(ref, &percpu_count))) {
0283         this_cpu_inc(*percpu_count);
0284         ret = true;
0285     } else if (!(ref->percpu_count_ptr & __PERCPU_REF_DEAD)) {
0286         ret = atomic_long_inc_not_zero(&ref->data->count);
0287     }
0288     return ret;
0289 }
0290 
0291 /**
0292  * percpu_ref_tryget_live - try to increment a live percpu refcount
0293  * @ref: percpu_ref to try-get
0294  *
0295  * Increment a percpu refcount unless it has already been killed.  Returns
0296  * %true on success; %false on failure.
0297  *
0298  * Completion of percpu_ref_kill() in itself doesn't guarantee that this
0299  * function will fail.  For such guarantee, percpu_ref_kill_and_confirm()
0300  * should be used.  After the confirm_kill callback is invoked, it's
0301  * guaranteed that no new reference will be given out by
0302  * percpu_ref_tryget_live().
0303  *
0304  * This function is safe to call as long as @ref is between init and exit.
0305  */
0306 static inline bool percpu_ref_tryget_live(struct percpu_ref *ref)
0307 {
0308     bool ret = false;
0309 
0310     rcu_read_lock();
0311     ret = percpu_ref_tryget_live_rcu(ref);
0312     rcu_read_unlock();
0313     return ret;
0314 }
0315 
0316 /**
0317  * percpu_ref_put_many - decrement a percpu refcount
0318  * @ref: percpu_ref to put
0319  * @nr: number of references to put
0320  *
0321  * Decrement the refcount, and if 0, call the release function (which was passed
0322  * to percpu_ref_init())
0323  *
0324  * This function is safe to call as long as @ref is between init and exit.
0325  */
0326 static inline void percpu_ref_put_many(struct percpu_ref *ref, unsigned long nr)
0327 {
0328     unsigned long __percpu *percpu_count;
0329 
0330     rcu_read_lock();
0331 
0332     if (__ref_is_percpu(ref, &percpu_count))
0333         this_cpu_sub(*percpu_count, nr);
0334     else if (unlikely(atomic_long_sub_and_test(nr, &ref->data->count)))
0335         ref->data->release(ref);
0336 
0337     rcu_read_unlock();
0338 }
0339 
0340 /**
0341  * percpu_ref_put - decrement a percpu refcount
0342  * @ref: percpu_ref to put
0343  *
0344  * Decrement the refcount, and if 0, call the release function (which was passed
0345  * to percpu_ref_init())
0346  *
0347  * This function is safe to call as long as @ref is between init and exit.
0348  */
0349 static inline void percpu_ref_put(struct percpu_ref *ref)
0350 {
0351     percpu_ref_put_many(ref, 1);
0352 }
0353 
0354 /**
0355  * percpu_ref_is_dying - test whether a percpu refcount is dying or dead
0356  * @ref: percpu_ref to test
0357  *
0358  * Returns %true if @ref is dying or dead.
0359  *
0360  * This function is safe to call as long as @ref is between init and exit
0361  * and the caller is responsible for synchronizing against state changes.
0362  */
0363 static inline bool percpu_ref_is_dying(struct percpu_ref *ref)
0364 {
0365     return ref->percpu_count_ptr & __PERCPU_REF_DEAD;
0366 }
0367 
0368 #endif