Skip to contentSkip to conversation

sysfs: use atomic_inc_unless_negative in sysfs_get_active

PATCH In review Maarten Lankhorst 3 participants 3 messages Mar 8, 2013 – Mar 19, 2013 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sysfs: use atomic_inc_unless_negative in sysfs_get_active

It seems that sysfs has an interesting way of doing the same thing. This removes the cpu_relax unfortunately, but if it's really needed, it would be better to add this to include/linux/atomic.h to benefit all atomic ops users.

---

fs/sysfs/dir.c +2 -15
index 2fbdff6..7f968ed 100644
@@ -165,21 +165,8 @@ struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
 	if (unlikely(!sd))
 		return NULL;
 
-	while (1) {
-		int v, t;
-
-		v = atomic_read(&sd->s_active);
-		if (unlikely(v < 0))
-			return NULL;
-
-		t = atomic_cmpxchg(&sd->s_active, v, v + 1);
-		if (likely(t == v))
-			break;
-		if (t < 0)
-			return NULL;
-
-		cpu_relax();
-	}
+	if (!atomic_inc_unless_negative(&sd->s_active))
+		return NULL;
 
 	if (likely(!ignore_lockdep(sd)))
 		rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_);
maintainer
#1 lore raw
Quoted 7 lines · Maarten Lankhorst

It seems that sysfs has an interesting way of doing the same thing. This removes the cpu_relax unfortunately, but if it's really needed, it would be better to add this to include/linux/atomic.h to benefit all atomic ops users.

---

Tejun wrote this code originally, so I'll defer to him as to if the patch below is a valid cleanup or not.

Tejun?

Quoted 29 lines
fs/sysfs/dir.c +2 -15
index 2fbdff6..7f968ed 100644
@@ -165,21 +165,8 @@ struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
 	if (unlikely(!sd))
 		return NULL;
 
-	while (1) {
-		int v, t;
-
-		v = atomic_read(&sd->s_active);
-		if (unlikely(v < 0))
-			return NULL;
-
-		t = atomic_cmpxchg(&sd->s_active, v, v + 1);
-		if (likely(t == v))
-			break;
-		if (t < 0)
-			return NULL;
-
-		cpu_relax();
-	}
+	if (!atomic_inc_unless_negative(&sd->s_active))
+		return NULL;
 
 	if (likely(!ignore_lockdep(sd)))
 		rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_);
Acked-by
#2 lore raw
Quoted 11 lines · Greg KH
Quoted 7 lines · Maarten Lankhorst

It seems that sysfs has an interesting way of doing the same thing. This removes the cpu_relax unfortunately, but if it's really needed, it would be better to add this to include/linux/atomic.h to benefit all atomic ops users.

---

Tejun wrote this code originally, so I'll defer to him as to if the patch below is a valid cleanup or not.

Yeah, I think that code predates atomic_inc_unless_negative().

Quoted 17 lines
Quoted 17 lines
fs/sysfs/dir.c +0 -9
index 2fbdff6..7f968ed 100644
@@ -165,21 +165,8 @@ struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
 	if (unlikely(!sd))
 		return NULL;
 
-	while (1) {
-		int v, t;
-
-		v = atomic_read(&sd->s_active);
-		if (unlikely(v < 0))
-			return NULL;
-
-		t = atomic_cmpxchg(&sd->s_active, v, v + 1);
-		if (likely(t == v))
Quoted 8 lines

fs/sysfs/dir.c struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd) · line 169

Quoted 8 lines
fs/sysfs/dir.c +2 -6
-			break;
-		if (t < 0)
-			return NULL;
-
-		cpu_relax();
-	}
+	if (!atomic_inc_unless_negative(&sd->s_active))
+		return NULL;

Looks good to me.

Thanks.

Signature · 1 line
tejun