Skip to content

Commit

Permalink
pcpu_ref: add percpu_ref_tryget_many()
Browse files Browse the repository at this point in the history
Add percpu_ref_tryget_many(), which works the same way as
percpu_ref_tryget(), but grabs specified number of refs.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Dennis Zhou <dennis@kernel.org>
Cc: Christoph Lameter <cl@linux.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
  • Loading branch information
Pavel Begunkov authored and Jens Axboe committed Jan 21, 2020
1 parent c1ca757 commit 4e5ef02
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions include/linux/percpu-refcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,33 +210,49 @@ static inline void percpu_ref_get(struct percpu_ref *ref)
}

/**
* percpu_ref_tryget - try to increment a percpu refcount
* percpu_ref_tryget_many - try to increment a percpu refcount
* @ref: percpu_ref to try-get
* @nr: number of references to get
*
* Increment a percpu refcount unless its count already reached zero.
* Increment a percpu refcount by @nr unless its count already reached zero.
* Returns %true on success; %false on failure.
*
* This function is safe to call as long as @ref is between init and exit.
*/
static inline bool percpu_ref_tryget(struct percpu_ref *ref)
static inline bool percpu_ref_tryget_many(struct percpu_ref *ref,
unsigned long nr)
{
unsigned long __percpu *percpu_count;
bool ret;

rcu_read_lock();

if (__ref_is_percpu(ref, &percpu_count)) {
this_cpu_inc(*percpu_count);
this_cpu_add(*percpu_count, nr);
ret = true;
} else {
ret = atomic_long_inc_not_zero(&ref->count);
ret = atomic_long_add_unless(&ref->count, nr, 0);
}

rcu_read_unlock();

return ret;
}

/**
* percpu_ref_tryget - try to increment a percpu refcount
* @ref: percpu_ref to try-get
*
* Increment a percpu refcount unless its count already reached zero.
* Returns %true on success; %false on failure.
*
* This function is safe to call as long as @ref is between init and exit.
*/
static inline bool percpu_ref_tryget(struct percpu_ref *ref)
{
return percpu_ref_tryget_many(ref, 1);
}

/**
* percpu_ref_tryget_live - try to increment a live percpu refcount
* @ref: percpu_ref to try-get
Expand Down

0 comments on commit 4e5ef02

Please sign in to comment.