Skip to contentSkip to conversation

locking/atomic: import atomic_dec_not_zero()

PATCH In review Fabian Frederick 3 participants 8 messages Jan 30, 2017 – Feb 1, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/14 locking/atomic: import atomic_dec_not_zero()

complementary definition to atomic_inc_not_zero() featured in lib/fault-inject.c

---
 include/linux/atomic.h | 2 ++
 lib/fault-inject.c     | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/atomic.h +2 -0
index e71835b..d8e6551 100644
@@ -517,6 +517,8 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)
 #define atomic_inc_not_zero(v)		atomic_add_unless((v), 1, 0)
 #endif
 
+#define atomic_dec_not_zero(v)          atomic_add_unless((v), -1, 0)
+
 #ifndef atomic_andnot
 static inline void atomic_andnot(int i, atomic_t *v)
 {
lib/fault-inject.c +0 -2
index 6a823a5..4ad5dcc 100644
@@ -52,8 +52,6 @@ static void fail_dump(struct fault_attr *attr)
 	}
 }
 
-#define atomic_dec_not_zero(v)		atomic_add_unless((v), -1, 0)
-
 static bool fail_task(struct fault_attr *attr, struct task_struct *task)
 {
 	return !in_interrupt() && task->make_it_fail;
Signature · 1 line
2.9.3
#1 lore raw
Quoted 27 lines · Fabian Frederick

complementary definition to atomic_inc_not_zero() featured in lib/fault-inject.c

---
 include/linux/atomic.h | 2 ++
 lib/fault-inject.c     | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/atomic.h +2 -0
index e71835b..d8e6551 100644
@@ -517,6 +517,8 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)
 #define atomic_inc_not_zero(v)		atomic_add_unless((v), 1, 0)
 #endif
 
+#define atomic_dec_not_zero(v)          atomic_add_unless((v), -1, 0)
+
 #ifndef atomic_andnot
 static inline void atomic_andnot(int i, atomic_t *v)
 {
lib/fault-inject.c +0 -0
index 6a823a5..4ad5dcc 100644

lib/fault-inject.c static void fail_dump(struct fault_attr *attr) · line 56

Quoted 5 lines
lib/fault-inject.c +0 -2
@@ -52,8 +52,6 @@ static void fail_dump(struct fault_attr *attr)
 	}
 }
 
-#define atomic_dec_not_zero(v)		atomic_add_unless((v), -1, 0)
-

Why did you convert the tabs to spaces?

Thanks,

Ingo

#3 lore raw
Quoted 10 lines · On 31 January 2017 at 11:41 Peter Zijlstra <[email protected]> wrote:
Quoted 2 lines · Fabian Frederick

complementary definition to atomic_inc_not_zero() featured in lib/fault-inject.c

Why?

Maybe this commit message should be ok ?

complementary definition to atomic_inc_not_zero() featured in lib/fault-inject.c and is more readable than atomic_add_unless((v), -1, 0) used in different places.

Andrew, do I have to submit this patch again replacing spaces with tabs in code line ?

Regards,
Fabian

maintainer
#4 lore raw
Quoted 9 lines · Fabian Frederick
Quoted 9 lines · On 31 January 2017 at 11:41 Peter Zijlstra <[email protected]> wrote:
Quoted 2 lines · Fabian Frederick

complementary definition to atomic_inc_not_zero() featured in lib/fault-inject.c

Quoted 7 lines
Quoted 1 line

Why?

Maybe this commit message should be ok ?

complementary definition to atomic_inc_not_zero() featured in lib/fault-inject.c and is more readable than atomic_add_unless((v), -1, 0) used in different places.

I still don't see why such a primitive makes sense. Yes there's a few usage sites, but from them I don't see a sensible pattern.

What sane pattern desires this?

#5 lore raw
Quoted 24 lines · On 31 January 2017 at 20:17 Peter Zijlstra <[email protected]> wrote:
Quoted 9 lines · Fabian Frederick
Quoted 9 lines · On 31 January 2017 at 11:41 Peter Zijlstra <[email protected]> wrote:
Quoted 2 lines · Fabian Frederick

complementary definition to atomic_inc_not_zero() featured in lib/fault-inject.c

Quoted 8 lines
Quoted 1 line

Why?

Maybe this commit message should be ok ?

complementary definition to atomic_inc_not_zero() featured in lib/fault-inject.c
and is more readable than atomic_add_unless((v), -1, 0) used in different places.

Quoted 4 lines

I still don't see why such a primitive makes sense. Yes there's a few usage sites, but from them I don't see a sensible pattern.

What sane pattern desires this?

Once again it's just about readability:

"add -1 unless value is zero" looks more complex in code than "dec not zero" but maybe it's just a matter of taste :) It it's not the case why would there be more sense about having
atomic_inc_not_zero() globally ?

Regards,
Fabian

maintainer
#6 lore raw
Quoted 1 line · Fabian Frederick

Once again it's just about readability:

I feel APIs should be about common use-cases, not about sporadic weird cases.

Quoted 4 lines

"add -1 unless value is zero" looks more complex in code than "dec not zero" but maybe it's just a matter of taste :) It it's not the case why would there be more sense about having
atomic_inc_not_zero() globally ?

inc_not_zero() has a very strong use-case, its for lockless refcount increment. Incrementing a 0 reference count is bad because the object will be freed and you'll have a use-after-free.

Arguably, once we move reference counting over to its own type, it would make sense to remove it from atomic, specifically to discourage that use case.

#7 lore raw
Quoted 16 lines · On 01 February 2017 at 10:25 Peter Zijlstra <[email protected]> wrote:
Quoted 1 line · Fabian Frederick

Once again it's just about readability:

I feel APIs should be about common use-cases, not about sporadic weird cases.

Quoted 5 lines

"add -1 unless value is zero" looks more complex in code than "dec not zero" but maybe it's just a matter of taste :) It it's not the case why would there be
more sense about having
atomic_inc_not_zero() globally ?

Quoted 7 lines

inc_not_zero() has a very strong use-case, its for lockless refcount increment. Incrementing a 0 reference count is bad because the object will be freed and you'll have a use-after-free.

Arguably, once we move reference counting over to its own type, it would make sense to remove it from atomic, specifically to discourage that use case.

Do you know about some commit doing such conversion or could you give me 2 distinct models
so I could compare ? btw atomic_inc_not_zero() is also adviced in Documentation/RCU/rcuref.txt

Regards,
Fabian