Skip to contentSkip to conversation

RT: Checks for cmpxchg in get_task_struct_rcu()

PATCH In review Daniel Walker 3 participants 11 messages Sep 21, 2005 – Sep 26, 2005 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RT: Checks for cmpxchg in get_task_struct_rcu()

Adds a check for cmpxchg in get_task_struct_rcu(), and implements the case when it doesn't exist.

include/linux/sched.h +9 -1
Index: linux-2.6.13/include/linux/sched.h
===================================================================
@@ -1026,13 +1026,21 @@ static inline int get_task_struct_rcu(st
 {
 	int oldusage;
 
+#ifdef __HAVE_ARCH_CMPXCHG
 	do {
 		oldusage = atomic_read(&t->usage);
 		if (oldusage == 0) {
 			return 0;
 		}
 	} while (cmpxchg(&t->usage.counter,
-		 oldusage, oldusage + 1) != oldusage);
+				oldusage, oldusage + 1) != oldusage);
+#else
+	oldusage = atomic_read(&t->usage);
+	if (oldusage == 0) {
+		return 0;
+	}
+	atomic_inc(&t->usage);
+#endif
 	return 1;
 }
 
#1 lore raw
Quoted 3 lines · Daniel Walker

Adds a check for cmpxchg in get_task_struct_rcu(), and implements the case when it doesn't exist.

I believe this is racy.

Quoted 14 lines

Index: linux-2.6.13/include/linux/sched.h
===================================================================

include/linux/sched.h +1 -0
@@ -1026,13 +1026,21 @@ static inline int get_task_struct_rcu(st
 {
 	int oldusage;
 
+#ifdef __HAVE_ARCH_CMPXCHG
 	do {
 		oldusage = atomic_read(&t->usage);
 		if (oldusage == 0) {

include/linux/sched.h static inline int get_task_struct_rcu(st · line 1041

Quoted 10 lines
include/linux/sched.h +6 -1
 			return 0;
 		}
 	} while (cmpxchg(&t->usage.counter,
-		 oldusage, oldusage + 1) != oldusage);
+				oldusage, oldusage + 1) != oldusage);
+#else
+	oldusage = atomic_read(&t->usage);
+	if (oldusage == 0) {
+		return 0;
+	}

What if t->usage becomes 0 here?

include/linux/sched.h static inline int get_task_struct_rcu(st · line 1046

Quoted 5 lines
include/linux/sched.h +2 -0
+	atomic_inc(&t->usage);
+#endif
 	return 1;
 }
 

You need my atomic_cmpxchg patches that provide an atomic_cmpxchg (and atomic_inc_not_zero) for all architectures.

Signature · 3 lines
SUSE Labs, Novell Inc.

Send instant messages to your online friends http://au.messenger.yahoo.com 
#2 lore raw
Quoted 3 lines · Nick Piggin

You need my atomic_cmpxchg patches that provide an atomic_cmpxchg (and atomic_inc_not_zero) for all architectures.

It is racy, but why not just disable preemption ..

Daniel

#3 lore raw
Quoted 10 lines · Daniel Walker
Quoted 3 lines · Nick Piggin

You need my atomic_cmpxchg patches that provide an atomic_cmpxchg (and atomic_inc_not_zero) for all architectures.

It is racy, but why not just disable preemption ..

Well you haven't, that's the point. You *introduce* racy operation.

Anyway, no matter, atomic_cmpxchg will take care of this nicely.

Signature · 3 lines
SUSE Labs, Novell Inc.

Send instant messages to your online friends http://au.messenger.yahoo.com 
#4 lore raw
In reply to Daniel Walker

Checks for cmpxchg in get_task_struct_rcu() . No race version.

include/linux/sched.h +12 -1
Index: linux-2.6.13/include/linux/sched.h
===================================================================
@@ -1026,13 +1026,24 @@ static inline int get_task_struct_rcu(st
 {
 	int oldusage;
 
+#ifdef __HAVE_ARCH_CMPXCHG
 	do {
 		oldusage = atomic_read(&t->usage);
 		if (oldusage == 0) {
 			return 0;
 		}
 	} while (cmpxchg(&t->usage.counter,
-		 oldusage, oldusage + 1) != oldusage);
+				oldusage, oldusage + 1) != oldusage);
+#else
+	raw_local_irq_disable();
+	oldusage = atomic_read(&t->usage);
+	if (oldusage == 0) {
+		raw_local_irq_enable();
+		return 0;
+	}
+	atomic_inc(&t->usage);
+	raw_local_irq_enable();
+#endif
 	return 1;
 }
 
#5 lore raw

include/linux/sched.h static inline int get_task_struct_rcu(st · line 1043

Quoted 7 lines · Daniel Walker
include/linux/sched.h +7 -0
+#else
+	raw_local_irq_disable();
+	oldusage = atomic_read(&t->usage);
+	if (oldusage == 0) {
+		raw_local_irq_enable();
+		return 0;
+	}

Isn't this still racy if another cpu drops the last reference to task struct here? Or can't that happen?

When you said "disable preempt", I had some silly idea that this function was only used in PREEMPT_RT mode, and that you would simply disallow architectures without cmpxchg from configuring PREEMPT_RT.

Quoted 3 lines
include/linux/sched.h +3 -0
+	atomic_inc(&t->usage);
+	raw_local_irq_enable();
+#endif
Signature · 3 lines
SUSE Labs, Novell Inc.

Send instant messages to your online friends http://au.messenger.yahoo.com 
#6 lore raw
In reply to Daniel Walker
Quoted 1 line · Daniel Walker

Checks for cmpxchg in get_task_struct_rcu() . No race version.

shouldnt we actually define a global, default cmpxchg() function, based on IRQ-disable - instead of open-coding one?

Ingo

#7 lore raw
Quoted 6 lines · Ingo Molnar
Quoted 1 line · Daniel Walker

Checks for cmpxchg in get_task_struct_rcu() . No race version.

shouldnt we actually define a global, default cmpxchg() function, based on IRQ-disable - instead of open-coding one?

I was thinking it should be restructures so it just needs an atomic_inc in this case. Considering that without cmpxchg() you must be on a UP machine .

Daniel

#8 lore raw
In reply to Nick Piggin
Quoted 2 lines · Nick Piggin

You need my atomic_cmpxchg patches that provide an atomic_cmpxchg (and atomic_inc_not_zero) for all architectures.

yeah. When will they be merged upstream?

Ingo

#9 lore raw
Quoted 7 lines · Ingo Molnar
Quoted 2 lines · Nick Piggin

You need my atomic_cmpxchg patches that provide an atomic_cmpxchg (and atomic_inc_not_zero) for all architectures.

yeah. When will they be merged upstream?

Well they're in -mm now, you can put them in your RT tree until they're in mainline... I guess realistically, 2.6.15. They should blow up fairly quickly if there are any problems with them, but they simply need a bit of testing on all architectures which I cannot do and I suspect even -mm isn't tested on at least half of them.

Signature · 5 lines
SUSE Labs, Novell Inc.



Send instant messages to your online friends http://au.messenger.yahoo.com 
#10 lore raw
Quoted 6 lines · Nick Piggin
Quoted 5 lines · Ingo Molnar
Quoted 2 lines · Nick Piggin

You need my atomic_cmpxchg patches that provide an atomic_cmpxchg (and atomic_inc_not_zero) for all architectures.

Quoted 8 lines
Quoted 2 lines

yeah. When will they be merged upstream?

Well they're in -mm now, you can put them in your RT tree until they're in mainline... I guess realistically, 2.6.15. They should blow up fairly quickly if there are any problems with them, but they simply need a bit of testing on all architectures which I cannot do and I suspect even -mm isn't tested on at least half of them.

very good - i'll put them into -rt.

Ingo