Skip to contentSkip to conversation

de-xchg fork.c

PATCH In review Rusty Russell 4 participants 5 messages Sep 23, 2002 – Sep 24, 2002

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

PATCH 1/1 de-xchg fork.c

Don't know who put this in for 2.5.38.

I realize that using xchg() makes you 'leet. But doing an atomic op where none is required is suboptimal and confusing.

Hey, at least it wasn't:

	if (likely(tsk = xchg(task_cache + cpu, tsk))) {

Cheers,
Rusty.

kernel/fork.c +2 -1
diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.38/kernel/fork.c working-2.5.38-unxchg/kernel/fork.c
@@ -64,11 +64,12 @@ void __put_task_struct(struct task_struc
 	} else {
 		int cpu = smp_processor_id();
 
-		tsk = xchg(task_cache + cpu, tsk);
+		tsk = task_cache[cpu];
 		if (tsk) {
 			free_thread_info(tsk->thread_info);
 			kmem_cache_free(task_struct_cachep,tsk);
 		}
+		task_cache[cpu] = current;
 	}
 }
 

--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.

#1 lore raw

Review

Quoted 2 lines · Rusty Russell
(quoted diff) +1 -1
-		tsk = xchg(task_cache + cpu, tsk);
+		tsk = task_cache[cpu];

this was me - i forgot that this are per-CPU fields. (hey and i wrote the original task_cache code so there's no excuse :-)

the attached patch (against BK-curr) fixes all xchg()'s and a preemption bug.

Ingo

kernel/fork.c renamed from kernel/fork.c.orig +6 -2
@@ -64,11 +64,12 @@
 	} else {
 		int cpu = smp_processor_id();
 
-		tsk = xchg(task_cache + cpu, tsk);
+		tsk = task_cache[cpu];
 		if (tsk) {
 			free_thread_info(tsk->thread_info);
 			kmem_cache_free(task_struct_cachep,tsk);
 		}
+		task_cache[cpu] = current;
 	}
 }
 
@@ -126,8 +127,11 @@
 {
 	struct task_struct *tsk;
 	struct thread_info *ti;
+	int cpu = get_cpu();
 
-	tsk = xchg(task_cache + smp_processor_id(), NULL);
+	tsk = task_cache[cpu];
+	task_cache[cpu] = NULL;
+	put_cpu();
 	if (!tsk) {
 		ti = alloc_thread_info();
 		if (!ti)
#2 lore raw
Quoted 2 lines · Ingo Molnar

the attached patch (against BK-curr) fixes all xchg()'s and a preemption bug.

Thanks, Ingo.

Quoted 5 lines
kernel/fork.c renamed from kernel/fork.c.orig +0 -0
@@ -64,11 +64,12 @@
 	} else {
 		int cpu = smp_processor_id();

kernel/fork.c line 74

Quoted 10 lines
kernel/fork.c renamed from kernel/fork.c.orig +2 -1
 
-		tsk = xchg(task_cache + cpu, tsk);
+		tsk = task_cache[cpu];
 		if (tsk) {
 			free_thread_info(tsk->thread_info);
 			kmem_cache_free(task_struct_cachep,tsk);
 		}
+		task_cache[cpu] = current;
 	}
 }

I think you need get/put_cpu() here, too?

Robert Love

#3 lore raw
Quoted 5 lines · Robert Love

Review

Quoted 3 lines
(quoted diff) +1 -0
+		task_cache[cpu] = current;
 	}
 }

I think you need get/put_cpu() here, too?

you are right - new patch attached.

Ingo

kernel/fork.c renamed from kernel/fork.c.orig +8 -3
@@ -62,13 +62,15 @@
 		free_thread_info(tsk->thread_info);
 		kmem_cache_free(task_struct_cachep,tsk);
 	} else {
-		int cpu = smp_processor_id();
+		int cpu = get_cpu();
 
-		tsk = xchg(task_cache + cpu, tsk);
+		tsk = task_cache[cpu];
 		if (tsk) {
 			free_thread_info(tsk->thread_info);
 			kmem_cache_free(task_struct_cachep,tsk);
 		}
+		task_cache[cpu] = current;
+		put_cpu();
 	}
 }
 
@@ -126,8 +128,11 @@
 {
 	struct task_struct *tsk;
 	struct thread_info *ti;
+	int cpu = get_cpu();
 
-	tsk = xchg(task_cache + smp_processor_id(), NULL);
+	tsk = task_cache[cpu];
+	task_cache[cpu] = NULL;
+	put_cpu();
 	if (!tsk) {
 		ti = alloc_thread_info();
 		if (!ti)
#4 lore raw
In reply to Rusty Russell

Hi!

Quoted 4 lines

Don't know who put this in for 2.5.38.

I realize that using xchg() makes you 'leet. But doing an atomic op where none is required is suboptimal and confusing.

Well, atomic op is also more expensive. i think ingo did this. Ingo, is patch below safe? It is faster *and* it works on 386.
Pavel

Quoted 16 lines

diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.38/kernel/fork.c working-2.5.38-unxchg/kernel/fork.c

kernel/fork.c +2 -1
@@ -64,11 +64,12 @@ void __put_task_struct(struct task_struc
 	} else {
 		int cpu = smp_processor_id();
 
-		tsk = xchg(task_cache + cpu, tsk);
+		tsk = task_cache[cpu];
 		if (tsk) {
 			free_thread_info(tsk->thread_info);
 			kmem_cache_free(task_struct_cachep,tsk);
 		}
+		task_cache[cpu] = current;
 	}
 }
Signature · 2 lines
Philips Velo 1: 1"x4"x8", 300gram, 60, 12MB, 40bogomips, linux, mutt,
details at http://atrey.karlin.mff.cuni.cz/~pavel/velo/index.html.