Skip to content

Commit

Permalink
Revert "locking/refcounts: Use atomic_try_cmpxchg()"
Browse files Browse the repository at this point in the history
This reverts commit 75a38ebaed3a7f8cd7a6d2a1032d1d2e23d4f744.
  • Loading branch information
THEBOSS619 committed Nov 11, 2019
1 parent bbdba83 commit e27fba9
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions lib/refcount.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
*/
bool refcount_add_not_zero(unsigned int i, refcount_t *r)
{
unsigned int new, val = atomic_read(&r->refs);
unsigned int old, new, val = atomic_read(&r->refs);

do {
for (;;) {
if (!val)
return false;

Expand All @@ -71,8 +71,12 @@ bool refcount_add_not_zero(unsigned int i, refcount_t *r)
new = val + i;
if (new < val)
new = UINT_MAX;
old = atomic_cmpxchg_relaxed(&r->refs, val, new);
if (old == val)
break;

} while (!atomic_try_cmpxchg_relaxed(&r->refs, &val, new));
val = old;
}

WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");

Expand Down Expand Up @@ -116,9 +120,9 @@ EXPORT_SYMBOL_GPL(refcount_add);
*/
bool refcount_inc_not_zero(refcount_t *r)
{
unsigned int new, val = atomic_read(&r->refs);
unsigned int old, new, val = atomic_read(&r->refs);

do {
for (;;) {
new = val + 1;

if (!val)
Expand All @@ -127,7 +131,12 @@ bool refcount_inc_not_zero(refcount_t *r)
if (unlikely(!new))
return true;

} while (!atomic_try_cmpxchg_relaxed(&r->refs, &val, new));
old = atomic_cmpxchg_relaxed(&r->refs, val, new);
if (old == val)
break;

val = old;
}

WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n");

Expand Down Expand Up @@ -175,9 +184,9 @@ EXPORT_SYMBOL_GPL(refcount_inc);
*/
bool refcount_sub_and_test(unsigned int i, refcount_t *r)
{
unsigned int new, val = atomic_read(&r->refs);
unsigned int old, new, val = atomic_read(&r->refs);

do {
for (;;) {
if (unlikely(val == UINT_MAX))
return false;

Expand All @@ -187,7 +196,12 @@ bool refcount_sub_and_test(unsigned int i, refcount_t *r)
return false;
}

} while (!atomic_try_cmpxchg_release(&r->refs, &val, new));
old = atomic_cmpxchg_release(&r->refs, val, new);
if (old == val)
break;

val = old;
}

return !new;
}
Expand Down Expand Up @@ -247,9 +261,7 @@ EXPORT_SYMBOL(refcount_dec);
*/
bool refcount_dec_if_one(refcount_t *r)
{
int val = 1;

return atomic_try_cmpxchg_release(&r->refs, &val, 0);
return atomic_cmpxchg_release(&r->refs, 1, 0) == 1;
}
EXPORT_SYMBOL_GPL(refcount_dec_if_one);

Expand All @@ -266,9 +278,9 @@ EXPORT_SYMBOL_GPL(refcount_dec_if_one);
*/
bool refcount_dec_not_one(refcount_t *r)
{
unsigned int new, val = atomic_read(&r->refs);
unsigned int old, new, val = atomic_read(&r->refs);

do {
for (;;) {
if (unlikely(val == UINT_MAX))
return true;

Expand All @@ -281,7 +293,12 @@ bool refcount_dec_not_one(refcount_t *r)
return true;
}

} while (!atomic_try_cmpxchg_release(&r->refs, &val, new));
old = atomic_cmpxchg_release(&r->refs, val, new);
if (old == val)
break;

val = old;
}

return true;
}
Expand Down

0 comments on commit e27fba9

Please sign in to comment.