Skip to contentSkip to conversation

drivers/block/rbd.c: atomic_inc_return_safe() & atomic_dec_return_safe()

DISCUSSION Shuah Khan 2 participants 3 messages Oct 2, 2020

lore
Share & tools
lore

Shuah Khan

author
#0 lore raw

All,

I came across these atomic_inc_return_safe() & atomic_dec_return_safe() functions that hold the counters at safe values.

atomic_inc_return_safe()

If the counter is already 0 it will not be incremented. If the counter is already at its maximum value returns -EINVAL without updating it.

atomic_dec_return_safe()

Decrement the counter. Return the resulting value, or -EINVAL

These two routines are static and only used in rbd.c.

Can these become part of atomic_t ops?

thanks,
-- Shuah

#1 lore raw
Quoted 11 lines · Shuah Khan

All,

I came across these atomic_inc_return_safe() & atomic_dec_return_safe() functions that hold the counters at safe values.

atomic_inc_return_safe()

If the counter is already 0 it will not be incremented. If the counter is already at its maximum value returns -EINVAL without updating it.

Quoted 7 lines

atomic_dec_return_safe()

Decrement the counter. Return the resulting value, or -EINVAL

These two routines are static and only used in rbd.c.

Can these become part of atomic_t ops?

I think you just want to use refcount_t for this use case. They have safe guards for under/overflow.

Signature · 1 line
Jens Axboe
author
#2 lore raw
Quoted 14 lines · Jens Axboe
Quoted 13 lines · Shuah Khan

All,

I came across these atomic_inc_return_safe() & atomic_dec_return_safe() functions that hold the counters at safe values.

atomic_inc_return_safe()

If the counter is already 0 it will not be incremented. If the counter is already at its maximum value returns -EINVAL without updating it.

atomic_dec_return_safe()

Quoted 9 lines
Quoted 5 lines

Decrement the counter. Return the resulting value, or -EINVAL

These two routines are static and only used in rbd.c.

Can these become part of atomic_t ops?

I think you just want to use refcount_t for this use case. They have safe guards for under/overflow.

Makes sense. Guess these came before refcount_t.

I will track this for refcount_t conversion.

thanks,
-- Shuah