Skip to contentSkip to conversation

kref: prefer atomic_inc_not_zero to atomic_add_unless

PATCH In review Jason A. Donenfeld 4 participants 12 messages Oct 10, 2015 – Dec 16, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kref: prefer atomic_inc_not_zero to atomic_add_unless

On most platforms, there exists this ifdef:

#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)

This makes this patch functionally useless. However, on PPC, there is actually an explicit definition of atomic_inc_not_zero with its own assembly that is slightly more optimized than atomic_add_unless. So, this patch changes kref to use atomic_inc_not_zero instead, for PPC and any future platforms that might provide an explicit implementation.

This also puts this usage of kref more in line with a verbatim reading of the examples in Paul McKenney's paper [1] in the section titled "2.4 Atomic Counting With Check and Release Memory Barrier", which uses atomic_inc_not_zero.

[1] http://open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2167.pdf

---
 include/linux/kref.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kref.h +1 -1
index 484604d..83d1f94 100644
@@ -166,6 +166,6 @@ static inline int kref_put_mutex(struct kref *kref,
  */
 static inline int __must_check kref_get_unless_zero(struct kref *kref)
 {
-	return atomic_add_unless(&kref->refcount, 1, 0);
+	return atomic_inc_not_zero(&kref->refcount);
 }
 #endif /* _KREF_H_ */
Signature · 1 line
2.6.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 34 lines · Jason A. Donenfeld

On most platforms, there exists this ifdef:

#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)

This makes this patch functionally useless. However, on PPC, there is actually an explicit definition of atomic_inc_not_zero with its own assembly that is slightly more optimized than atomic_add_unless. So, this patch changes kref to use atomic_inc_not_zero instead, for PPC and any future platforms that might provide an explicit implementation.

This also puts this usage of kref more in line with a verbatim reading of the examples in Paul McKenney's paper [1] in the section titled "2.4 Atomic Counting With Check and Release Memory Barrier", which uses atomic_inc_not_zero.

[1] https://urldefense.proofpoint.com/v2/url?u=http-3A__open-2Dstd.org_jtc1_sc22_wg21_docs_papers_2007_n2167.pdf&d=BQIBAg&c=Sqcl0Ez6M0X8aeM67LKIiDJAXVeAw-YihVMNtXt-uEs&r=vpukPkBtpoNQp2IUKuFviOmPNYWVKmen3Jeeu55zmEA&m=z5Nd9sYiJMKiphNjyZp6XT5CbayXMBlcb903f260pDY&s=HEHX3CuXRs2GRRQWuC4Vef6iJMwdilKVRkiZgJpjEpA&e=

---
 include/linux/kref.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kref.h +1 -1
index 484604d..83d1f94 100644
@@ -166,6 +166,6 @@ static inline int kref_put_mutex(struct kref *kref,
  */
 static inline int __must_check kref_get_unless_zero(struct kref *kref)
 {
-	return atomic_add_unless(&kref->refcount, 1, 0);
+	return atomic_inc_not_zero(&kref->refcount);
 }
 #endif /* _KREF_H_ */
#2 lore raw

This was positively reviewed but never picked up. Can someone queue this for rc3?

Thanks,
Jason

Quoted 39 lines · Thomas Hellstrom
Quoted 34 lines · Jason A. Donenfeld

On most platforms, there exists this ifdef:

#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)

This makes this patch functionally useless. However, on PPC, there is actually an explicit definition of atomic_inc_not_zero with its own assembly that is slightly more optimized than atomic_add_unless. So, this patch changes kref to use atomic_inc_not_zero instead, for PPC and any future platforms that might provide an explicit implementation.

This also puts this usage of kref more in line with a verbatim reading of the examples in Paul McKenney's paper [1] in the section titled "2.4 Atomic Counting With Check and Release Memory Barrier", which uses atomic_inc_not_zero.

[1] https://urldefense.proofpoint.com/v2/url?u=http-3A__open-2Dstd.org_jtc1_sc22_wg21_docs_papers_2007_n2167.pdf&d=BQIBAg&c=Sqcl0Ez6M0X8aeM67LKIiDJAXVeAw-YihVMNtXt-uEs&r=vpukPkBtpoNQp2IUKuFviOmPNYWVKmen3Jeeu55zmEA&m=z5Nd9sYiJMKiphNjyZp6XT5CbayXMBlcb903f260pDY&s=HEHX3CuXRs2GRRQWuC4Vef6iJMwdilKVRkiZgJpjEpA&e=

---
 include/linux/kref.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kref.h +1 -1
index 484604d..83d1f94 100644
@@ -166,6 +166,6 @@ static inline int kref_put_mutex(struct kref *kref,
  */
 static inline int __must_check kref_get_unless_zero(struct kref *kref)
 {
-     return atomic_add_unless(&kref->refcount, 1, 0);
+     return atomic_inc_not_zero(&kref->refcount);
 }
 #endif /* _KREF_H_ */
#3 lore raw

This was positively reviewed by maintainers but never picked up. Can someone queue this for 4.7 or 4.8?

Thanks,
Jason

Quoted 46 lines · Jason A. Donenfeld

This was positively reviewed but never picked up. Can someone queue this for rc3?

Thanks,
Jason

Quoted 39 lines · Thomas Hellstrom
Quoted 34 lines · Jason A. Donenfeld

On most platforms, there exists this ifdef:

#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)

This makes this patch functionally useless. However, on PPC, there is actually an explicit definition of atomic_inc_not_zero with its own assembly that is slightly more optimized than atomic_add_unless. So, this patch changes kref to use atomic_inc_not_zero instead, for PPC and any future platforms that might provide an explicit implementation.

This also puts this usage of kref more in line with a verbatim reading of the examples in Paul McKenney's paper [1] in the section titled "2.4 Atomic Counting With Check and Release Memory Barrier", which uses atomic_inc_not_zero.

[1] https://urldefense.proofpoint.com/v2/url?u=http-3A__open-2Dstd.org_jtc1_sc22_wg21_docs_papers_2007_n2167.pdf&d=BQIBAg&c=Sqcl0Ez6M0X8aeM67LKIiDJAXVeAw-YihVMNtXt-uEs&r=vpukPkBtpoNQp2IUKuFviOmPNYWVKmen3Jeeu55zmEA&m=z5Nd9sYiJMKiphNjyZp6XT5CbayXMBlcb903f260pDY&s=HEHX3CuXRs2GRRQWuC4Vef6iJMwdilKVRkiZgJpjEpA&e=

---
 include/linux/kref.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kref.h +1 -1
index 484604d..83d1f94 100644
@@ -166,6 +166,6 @@ static inline int kref_put_mutex(struct kref *kref,
  */
 static inline int __must_check kref_get_unless_zero(struct kref *kref)
 {
-     return atomic_add_unless(&kref->refcount, 1, 0);
+     return atomic_inc_not_zero(&kref->refcount);
 }
 #endif /* _KREF_H_ */
#4 lore raw

Patch for drm-next WAS Re: [PATCH] kref: prefer atomic_inc_not_zero to atomic_add_unless

Dave,

Since kref_get_unless_zero() was brought in by drm, could we add this to drm-next?

Thanks,
Thomas

Quoted 40 lines · Jason A. Donenfeld

This was positively reviewed by maintainers but never picked up. Can someone queue this for 4.7 or 4.8?

Thanks,
Jason

Quoted 33 lines · Jason A. Donenfeld

This was positively reviewed but never picked up. Can someone queue this for rc3?

Thanks,
Jason

Quoted 26 lines · Thomas Hellstrom
Quoted 22 lines · Jason A. Donenfeld

On most platforms, there exists this ifdef:

#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)

This makes this patch functionally useless. However, on PPC, there is actually an explicit definition of atomic_inc_not_zero with its own assembly that is slightly more optimized than atomic_add_unless. So, this patch changes kref to use atomic_inc_not_zero instead, for PPC and any future platforms that might provide an explicit implementation.

This also puts this usage of kref more in line with a verbatim reading of the examples in Paul McKenney's paper [1] in the section titled "2.4 Atomic Counting With Check and Release Memory Barrier", which uses atomic_inc_not_zero.

[1] https://urldefense.proofpoint.com/v2/url?u=http-3A__open-2Dstd.org_jtc1_sc22_wg21_docs_papers_2007_n2167.pdf&d=BQIBAg&c=Sqcl0Ez6M0X8aeM67LKIiDJAXVeAw-YihVMNtXt-uEs&r=vpukPkBtpoNQp2IUKuFviOmPNYWVKmen3Jeeu55zmEA&m=z5Nd9sYiJMKiphNjyZp6XT5CbayXMBlcb903f260pDY&s=HEHX3CuXRs2GRRQWuC4Vef6iJMwdilKVRkiZgJpjEpA&e=

---
 include/linux/kref.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
include/linux/kref.h +0 -0
index 484604d..83d1f94 100644
Quoted 8 lines
Quoted 8 lines

include/linux/kref.h static inline int kref_put_mutex(struct kref *kref, · line 171

Quoted 8 lines
include/linux/kref.h +1 -1
@@ -166,6 +166,6 @@ static inline int kref_put_mutex(struct kref *kref,
  */
 static inline int __must_check kref_get_unless_zero(struct kref *kref)
 {
-     return atomic_add_unless(&kref->refcount, 1, 0);
+     return atomic_inc_not_zero(&kref->refcount);
 }
 #endif /* _KREF_H_ */

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw

Re: Patch for drm-next WAS Re: [PATCH] kref: prefer atomic_inc_not_zero to atomic_add_unless

Quoted 4 lines · Thomas Hellstrom

Dave,

Since kref_get_unless_zero() was brought in by drm, could we add this to drm-next?

Sure can do, but I can't find the raw patch anywhere (I suck, I know). Care to resend?

Thanks, Daniel

Quoted 64 lines

Thanks,
Thomas

Quoted 40 lines · Jason A. Donenfeld

This was positively reviewed by maintainers but never picked up. Can someone queue this for 4.7 or 4.8?

Thanks,
Jason

Quoted 33 lines · Jason A. Donenfeld

This was positively reviewed but never picked up. Can someone queue this for rc3?

Thanks,
Jason

Quoted 26 lines · Thomas Hellstrom
Quoted 22 lines · Jason A. Donenfeld

On most platforms, there exists this ifdef:

#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)

This makes this patch functionally useless. However, on PPC, there is actually an explicit definition of atomic_inc_not_zero with its own assembly that is slightly more optimized than atomic_add_unless. So, this patch changes kref to use atomic_inc_not_zero instead, for PPC and any future platforms that might provide an explicit implementation.

This also puts this usage of kref more in line with a verbatim reading of the examples in Paul McKenney's paper [1] in the section titled "2.4 Atomic Counting With Check and Release Memory Barrier", which uses atomic_inc_not_zero.

[1] https://urldefense.proofpoint.com/v2/url?u=http-3A__open-2Dstd.org_jtc1_sc22_wg21_docs_papers_2007_n2167.pdf&d=BQIBAg&c=Sqcl0Ez6M0X8aeM67LKIiDJAXVeAw-YihVMNtXt-uEs&r=vpukPkBtpoNQp2IUKuFviOmPNYWVKmen3Jeeu55zmEA&m=z5Nd9sYiJMKiphNjyZp6XT5CbayXMBlcb903f260pDY&s=HEHX3CuXRs2GRRQWuC4Vef6iJMwdilKVRkiZgJpjEpA&e=

---
 include/linux/kref.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
include/linux/kref.h +0 -0
index 484604d..83d1f94 100644
Quoted 8 lines
Quoted 8 lines

include/linux/kref.h static inline int kref_put_mutex(struct kref *kref, · line 171

Quoted 8 lines
include/linux/kref.h +1 -1
@@ -166,6 +166,6 @@ static inline int kref_put_mutex(struct kref *kref,
  */
 static inline int __must_check kref_get_unless_zero(struct kref *kref)
 {
-     return atomic_add_unless(&kref->refcount, 1, 0);
+     return atomic_inc_not_zero(&kref->refcount);
 }
 #endif /* _KREF_H_ */

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#6 lore raw

Re: Patch for drm-next WAS Re: [PATCH] kref: prefer atomic_inc_not_zero to atomic_add_unless

Quoted 2 lines · Daniel Vetter

Sure can do, but I can't find the raw patch anywhere (I suck, I know). Care to resend?

Hey sorry I missed this email requesting the actual patch. I reposted it here:

https://lkml.org/lkml/2016/12/14/814

#7 lore raw

PATCH 1/1 kref: prefer atomic_inc_not_zero to atomic_add_unless

In reply to Daniel Vetter

On most platforms, there exists this ifdef:

#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)

This makes this patch functionally useless. However, on PPC, there is actually an explicit definition of atomic_inc_not_zero with its own assembly that is slightly more optimized than atomic_add_unless. So, this patch changes kref to use atomic_inc_not_zero instead, for PPC and any future platforms that might provide an explicit implementation.

This also puts this usage of kref more in line with a verbatim reading of the examples in Paul McKenney's paper [1] in the section titled "2.4 Atomic Counting With Check and Release Memory Barrier", which uses atomic_inc_not_zero.

[1] http://open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2167.pdf

---
This was reviewed favorably 14 months ago but never picked up. I'm resubmitting it now in hopes that you can finally queue it up for 4.10.

 include/linux/kref.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kref.h +1 -1
index e15828fd71f1..62f0a84ae94e 100644
@@ -133,6 +133,6 @@ static inline int kref_put_mutex(struct kref *kref,
  */
 static inline int __must_check kref_get_unless_zero(struct kref *kref)
 {
-	return atomic_add_unless(&kref->refcount, 1, 0);
+	return atomic_inc_not_zero(&kref->refcount);
 }
 #endif /* _KREF_H_ */
Signature · 6 lines
2.11.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#8 lore raw
Quoted 15 lines · Jason A. Donenfeld

On most platforms, there exists this ifdef:

#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)

This makes this patch functionally useless. However, on PPC, there is actually an explicit definition of atomic_inc_not_zero with its own assembly that is slightly more optimized than atomic_add_unless. So, this patch changes kref to use atomic_inc_not_zero instead, for PPC and any future platforms that might provide an explicit implementation.

This also puts this usage of kref more in line with a verbatim reading of the examples in Paul McKenney's paper [1] in the section titled "2.4 Atomic Counting With Check and Release Memory Barrier", which uses atomic_inc_not_zero.

Quoted 4 lines

[1] http://open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2167.pdf

Applied to drm-misc, but for 4.11 since 4.10 is bugfixes only already.

Thanks, Daniel

Quoted 27 lines

---
This was reviewed favorably 14 months ago but never picked up. I'm resubmitting it now in hopes that you can finally queue it up for 4.10.

 include/linux/kref.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kref.h +1 -1
index e15828fd71f1..62f0a84ae94e 100644
@@ -133,6 +133,6 @@ static inline int kref_put_mutex(struct kref *kref,
  */
 static inline int __must_check kref_get_unless_zero(struct kref *kref)
 {
-	return atomic_add_unless(&kref->refcount, 1, 0);
+	return atomic_inc_not_zero(&kref->refcount);
 }
 #endif /* _KREF_H_ */
Signature · 6 lines
2.11.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#9 lore raw

PATCH 1/1 kref: prefer atomic_inc_not_zero to atomic_add_unless

In reply to Thomas Hellstrom

On most platforms, there exists this ifdef:

#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)

This makes this patch functionally useless. However, on PPC, there is actually an explicit definition of atomic_inc_not_zero with its own assembly that is slightly more optimized than atomic_add_unless. So, this patch changes kref to use atomic_inc_not_zero instead, for PPC and any future platforms that might provide an explicit implementation.

This also puts this usage of kref more in line with a verbatim reading of the examples in Paul McKenney's paper [1] in the section titled "2.4 Atomic Counting With Check and Release Memory Barrier", which uses atomic_inc_not_zero.

[1] http://open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2167.pdf

---
This was reviewed favorably 14 months ago but never picked up. I'm resubmitting it now in hopes that you can finally queue it up for 4.10.

 include/linux/kref.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kref.h +1 -1
index e15828fd71f1..62f0a84ae94e 100644
@@ -133,6 +133,6 @@ static inline int kref_put_mutex(struct kref *kref,
  */
 static inline int __must_check kref_get_unless_zero(struct kref *kref)
 {
-	return atomic_add_unless(&kref->refcount, 1, 0);
+	return atomic_inc_not_zero(&kref->refcount);
 }
 #endif /* _KREF_H_ */
Signature · 1 line
2.11.0
#11 lore raw

PATCH 1/1 kref: prefer atomic_inc_not_zero to atomic_add_unless

In reply to Jason A. Donenfeld

By request forwarded patch

This is also

/Thomas

-------- Forwarded Message --------
Subject: [PATCH] kref: prefer atomic_inc_not_zero to atomic_add_unless
Date: Sat, 10 Oct 2015 12:56:34 +0200
From: Jason A. Donenfeld <[email protected]>
To: Dave Airlie <[email protected]>, Thomas Hellstrom
<[email protected]>, [email protected]
CC: Jason A. Donenfeld <[email protected]>

On most platforms, there exists this ifdef:

#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)

This makes this patch functionally useless. However, on PPC, there is actually an explicit definition of atomic_inc_not_zero with its own assembly that is slightly more optimized than atomic_add_unless. So, this patch changes kref to use atomic_inc_not_zero instead, for PPC and any future platforms that might provide an explicit implementation.

This also puts this usage of kref more in line with a verbatim reading of the examples in Paul McKenney's paper [1] in the section titled "2.4 Atomic Counting With Check and Release Memory Barrier", which uses atomic_inc_not_zero.

[1] https://urldefense.proofpoint.com/v2/url?u=http-3A__open-2Dstd.org_jtc1_sc22_wg21_docs_papers_2007_n2167.pdf&d=BQIBAg&c=Sqcl0Ez6M0X8aeM67LKIiDJAXVeAw-YihVMNtXt-uEs&r=vpukPkBtpoNQp2IUKuFviOmPNYWVKmen3Jeeu55zmEA&m=z5Nd9sYiJMKiphNjyZp6XT5CbayXMBlcb903f260pDY&s=HEHX3CuXRs2GRRQWuC4Vef6iJMwdilKVRkiZgJpjEpA&e=

---
 include/linux/kref.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kref.h +1 -1
index 484604d..83d1f94 100644
@@ -166,6 +166,6 @@ static inline int kref_put_mutex(struct kref *kref,
  */
 static inline int __must_check kref_get_unless_zero(struct kref *kref)
 {
-	return atomic_add_unless(&kref->refcount, 1, 0);
+	return atomic_inc_not_zero(&kref->refcount);
 }
 #endif /* _KREF_H_ */
Signature · 6 lines
2.6.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel