123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- #define pr_fmt(fmt) "%s: " fmt "\n", __func__
- #include <linux/kernel.h>
- #include <linux/sched.h>
- #include <linux/wait.h>
- #include <linux/percpu-refcount.h>
- #define PERCPU_COUNT_BIAS (1LU << (BITS_PER_LONG - 1))
- static DEFINE_SPINLOCK(percpu_ref_switch_lock);
- static DECLARE_WAIT_QUEUE_HEAD(percpu_ref_switch_waitq);
- static unsigned long __percpu *percpu_count_ptr(struct percpu_ref *ref)
- {
- return (unsigned long __percpu *)
- (ref->percpu_count_ptr & ~__PERCPU_REF_ATOMIC_DEAD);
- }
- int percpu_ref_init(struct percpu_ref *ref, percpu_ref_func_t *release,
- unsigned int flags, gfp_t gfp)
- {
- size_t align = max_t(size_t, 1 << __PERCPU_REF_FLAG_BITS,
- __alignof__(unsigned long));
- unsigned long start_count = 0;
- ref->percpu_count_ptr = (unsigned long)
- __alloc_percpu_gfp(sizeof(unsigned long), align, gfp);
- if (!ref->percpu_count_ptr)
- return -ENOMEM;
- ref->force_atomic = flags & PERCPU_REF_INIT_ATOMIC;
- if (flags & (PERCPU_REF_INIT_ATOMIC | PERCPU_REF_INIT_DEAD))
- ref->percpu_count_ptr |= __PERCPU_REF_ATOMIC;
- else
- start_count += PERCPU_COUNT_BIAS;
- if (flags & PERCPU_REF_INIT_DEAD)
- ref->percpu_count_ptr |= __PERCPU_REF_DEAD;
- else
- start_count++;
- atomic_long_set(&ref->count, start_count);
- ref->release = release;
- ref->confirm_switch = NULL;
- return 0;
- }
- EXPORT_SYMBOL_GPL(percpu_ref_init);
- void percpu_ref_exit(struct percpu_ref *ref)
- {
- unsigned long __percpu *percpu_count = percpu_count_ptr(ref);
- if (percpu_count) {
-
- WARN_ON_ONCE(ref->confirm_switch);
- free_percpu(percpu_count);
- ref->percpu_count_ptr = __PERCPU_REF_ATOMIC_DEAD;
- }
- }
- EXPORT_SYMBOL_GPL(percpu_ref_exit);
- static void percpu_ref_call_confirm_rcu(struct rcu_head *rcu)
- {
- struct percpu_ref *ref = container_of(rcu, struct percpu_ref, rcu);
- ref->confirm_switch(ref);
- ref->confirm_switch = NULL;
- wake_up_all(&percpu_ref_switch_waitq);
-
- percpu_ref_put(ref);
- }
- static void percpu_ref_switch_to_atomic_rcu(struct rcu_head *rcu)
- {
- struct percpu_ref *ref = container_of(rcu, struct percpu_ref, rcu);
- unsigned long __percpu *percpu_count = percpu_count_ptr(ref);
- unsigned long count = 0;
- int cpu;
- for_each_possible_cpu(cpu)
- count += *per_cpu_ptr(percpu_count, cpu);
- pr_debug("global %ld percpu %ld",
- atomic_long_read(&ref->count), (long)count);
-
- atomic_long_add((long)count - PERCPU_COUNT_BIAS, &ref->count);
- WARN_ONCE(atomic_long_read(&ref->count) <= 0,
- "percpu ref (%pf) <= 0 (%ld) after switching to atomic",
- ref->release, atomic_long_read(&ref->count));
-
- percpu_ref_call_confirm_rcu(rcu);
- }
- static void percpu_ref_noop_confirm_switch(struct percpu_ref *ref)
- {
- }
- static void __percpu_ref_switch_to_atomic(struct percpu_ref *ref,
- percpu_ref_func_t *confirm_switch)
- {
- if (ref->percpu_count_ptr & __PERCPU_REF_ATOMIC) {
- if (confirm_switch)
- confirm_switch(ref);
- return;
- }
-
- ref->percpu_count_ptr |= __PERCPU_REF_ATOMIC;
-
- ref->confirm_switch = confirm_switch ?: percpu_ref_noop_confirm_switch;
- percpu_ref_get(ref);
- call_rcu_sched(&ref->rcu, percpu_ref_switch_to_atomic_rcu);
- }
- static void __percpu_ref_switch_to_percpu(struct percpu_ref *ref)
- {
- unsigned long __percpu *percpu_count = percpu_count_ptr(ref);
- int cpu;
- BUG_ON(!percpu_count);
- if (!(ref->percpu_count_ptr & __PERCPU_REF_ATOMIC))
- return;
- atomic_long_add(PERCPU_COUNT_BIAS, &ref->count);
-
- for_each_possible_cpu(cpu)
- *per_cpu_ptr(percpu_count, cpu) = 0;
- smp_store_release(&ref->percpu_count_ptr,
- ref->percpu_count_ptr & ~__PERCPU_REF_ATOMIC);
- }
- static void __percpu_ref_switch_mode(struct percpu_ref *ref,
- percpu_ref_func_t *confirm_switch)
- {
- lockdep_assert_held(&percpu_ref_switch_lock);
-
- wait_event_lock_irq(percpu_ref_switch_waitq, !ref->confirm_switch,
- percpu_ref_switch_lock);
- if (ref->force_atomic || (ref->percpu_count_ptr & __PERCPU_REF_DEAD))
- __percpu_ref_switch_to_atomic(ref, confirm_switch);
- else
- __percpu_ref_switch_to_percpu(ref);
- }
- void percpu_ref_switch_to_atomic(struct percpu_ref *ref,
- percpu_ref_func_t *confirm_switch)
- {
- unsigned long flags;
- spin_lock_irqsave(&percpu_ref_switch_lock, flags);
- ref->force_atomic = true;
- __percpu_ref_switch_mode(ref, confirm_switch);
- spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
- }
- void percpu_ref_switch_to_percpu(struct percpu_ref *ref)
- {
- unsigned long flags;
- spin_lock_irqsave(&percpu_ref_switch_lock, flags);
- ref->force_atomic = false;
- __percpu_ref_switch_mode(ref, NULL);
- spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
- }
- void percpu_ref_kill_and_confirm(struct percpu_ref *ref,
- percpu_ref_func_t *confirm_kill)
- {
- unsigned long flags;
- spin_lock_irqsave(&percpu_ref_switch_lock, flags);
- WARN_ONCE(ref->percpu_count_ptr & __PERCPU_REF_DEAD,
- "%s called more than once on %pf!", __func__, ref->release);
- ref->percpu_count_ptr |= __PERCPU_REF_DEAD;
- __percpu_ref_switch_mode(ref, confirm_kill);
- percpu_ref_put(ref);
- spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
- }
- EXPORT_SYMBOL_GPL(percpu_ref_kill_and_confirm);
- void percpu_ref_reinit(struct percpu_ref *ref)
- {
- unsigned long flags;
- spin_lock_irqsave(&percpu_ref_switch_lock, flags);
- WARN_ON_ONCE(!percpu_ref_is_zero(ref));
- ref->percpu_count_ptr &= ~__PERCPU_REF_DEAD;
- percpu_ref_get(ref);
- __percpu_ref_switch_mode(ref, NULL);
- spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
- }
- EXPORT_SYMBOL_GPL(percpu_ref_reinit);
|