linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] set_cpus_allowed() for 2.4
@ 2002-10-01 23:03 Robert Love
  2002-10-02 13:01 ` Christoph Hellwig
  2002-11-05  3:37 ` Christoph Hellwig
  0 siblings, 2 replies; 45+ messages in thread
From: Robert Love @ 2002-10-01 23:03 UTC (permalink / raw)
  To: jgarzik, hch; +Cc: linux-kernel

The following patch implements set_cpus_allowed() for stock 2.4 without
the O(1) scheduler.

The calling semantics and behavior remain the same as 2.5's method.

This is to provide a backward-compatible interface, specifically for
those interested in back-porting the new workqueue code to 2.4 -
set_cpus_allowed() seems to be the only nit preventing a straight
drop-in.

Patch is against 2.4.20-pre8 and untested but does compile.

	Robert Love

diff -urN linux-2.4.20-pre8/include/linux/sched.h linux/include/linux/sched.h
--- linux-2.4.20-pre8/include/linux/sched.h	Mon Sep 30 17:41:22 2002
+++ linux/include/linux/sched.h	Tue Oct  1 18:35:28 2002
@@ -163,6 +164,12 @@
 extern int start_context_thread(void);
 extern int current_is_keventd(void);
 
+#if CONFIG_SMP
+extern void set_cpus_allowed(struct task_struct *p, unsigned long new_mask);
+#else
+# define set_cpus_allowed(p, new_mask) do { } while (0)
+#endif
+
 /*
  * The default fd array needs to be at least BITS_PER_LONG,
  * as this is the granularity returned by copy_fdset().
diff -urN linux-2.4.20-pre8/kernel/ksyms.c linux/kernel/ksyms.c
--- linux-2.4.20-pre8/kernel/ksyms.c	Mon Sep 30 17:41:22 2002
+++ linux/kernel/ksyms.c	Tue Oct  1 18:34:41 2002
@@ -451,6 +451,9 @@
 EXPORT_SYMBOL(interruptible_sleep_on_timeout);
 EXPORT_SYMBOL(schedule);
 EXPORT_SYMBOL(schedule_timeout);
+#if CONFIG_SMP
+EXPORT_SYMBOL(set_cpus_allowed);
+#endif
 EXPORT_SYMBOL(yield);
 EXPORT_SYMBOL(__cond_resched);
 EXPORT_SYMBOL(jiffies);
diff -urN linux-2.4.20-pre8/kernel/sched.c linux/kernel/sched.c
--- linux-2.4.20-pre8/kernel/sched.c	Mon Sep 30 17:41:22 2002
+++ linux/kernel/sched.c	Tue Oct  1 18:54:49 2002
@@ -850,6 +850,46 @@
 
 void scheduling_functions_end_here(void) { }
 
+#if CONFIG_SMP
+
+/**
+ * set_cpus_allowed() - change a given task's processor affinity
+ * @p: task to bind
+ * @new_mask: bitmask of allowed processors
+ *
+ * Upon return, the task is running on a legal processor.  Note the caller
+ * must have a valid reference to the task: it must not exit() prematurely.
+ * This call can sleep; do not hold locks on call.
+ */
+void set_cpus_allowed(struct task_struct *p, unsigned long new_mask)
+{
+	new_mask &= cpu_online_map;
+	BUG_ON(!new_mask);
+
+	p->cpus_allowed = new_mask;
+
+	/*
+	 * If the task is on a no-longer-allowed processor, we need to move
+	 * it.  If the task is not current, then set need_resched and send
+	 * its processor an IPI to reschedule.
+	 */
+	if (!(p->cpus_runnable & p->cpus_allowed)) {
+		if (p != current) {
+			p->need_resched = 1;
+			smp_send_reschedule(p->processor);
+		}
+		/*
+		 * Wait until we are on a legal processor.  If the task is
+		 * current, then we should be on a legal processor the next
+		 * time we reschedule.  Otherwise, we need to wait for the IPI.
+		 */
+		while (!(p->cpus_runnable & p->cpus_allowed))
+			schedule();
+	}
+}
+
+#endif /* CONFIG_SMP */
+
 #ifndef __alpha__
 
 /*
diff -urN linux-2.4.20-pre8/kernel/softirq.c linux/kernel/softirq.c
--- linux-2.4.20-pre8/kernel/softirq.c	Mon Sep 30 17:41:22 2002
+++ linux/kernel/softirq.c	Tue Oct  1 18:53:01 2002
@@ -368,9 +368,8 @@
 	sigfillset(&current->blocked);
 
 	/* Migrate to the right CPU */
-	current->cpus_allowed = 1UL << cpu;
-	while (smp_processor_id() != cpu)
-		schedule();
+	set_cpus_allowed(current, 1UL << cpu);
+	BUG_ON(smp_processor_id() != cpu);
 
 	sprintf(current->comm, "ksoftirqd_CPU%d", bind_cpu);
 




^ permalink raw reply	[flat|nested] 45+ messages in thread
* [PATCH] set_cpus_allowed() for 2.4
@ 2002-12-03  0:26 Christoph Hellwig
  2002-12-02 17:24 ` Jeff Garzik
  2002-12-02 17:50 ` Martin J. Bligh
  0 siblings, 2 replies; 45+ messages in thread
From: Christoph Hellwig @ 2002-12-03  0:26 UTC (permalink / raw)
  To: marcelo, rml; +Cc: linux-kernel

now that all commercial vendors ship a backport of Ingo's O(1) scheduler
external projects like XFS have to track those projects in addition to the
mainline kernel.

Having the common new APIs available in mainline would be a very good thing
for those projects.  We already have a proper yield() in 2.4.20, but the
set_cpus_allowed() API as used e.g. for kernelthreads bound to CPUs is
still missing.

Any chance you could apply Robert Love's patch to add it for 2.4.21?  Note
that it does not change any existing code but just adds that interface.


diff -urN linux-2.4.20-pre8/include/linux/sched.h linux/include/linux/sched.h
--- linux-2.4.20-pre8/include/linux/sched.h	Mon Sep 30 17:41:22 2002
+++ linux/include/linux/sched.h	Tue Oct  1 18:35:28 2002
@@ -163,6 +164,12 @@
 extern int start_context_thread(void);
 extern int current_is_keventd(void);
 
+#if CONFIG_SMP
+extern void set_cpus_allowed(struct task_struct *p, unsigned long new_mask);
+#else
+# define set_cpus_allowed(p, new_mask) do { } while (0)
+#endif
+
 /*
  * The default fd array needs to be at least BITS_PER_LONG,
  * as this is the granularity returned by copy_fdset().
diff -urN linux-2.4.20-pre8/kernel/ksyms.c linux/kernel/ksyms.c
--- linux-2.4.20-pre8/kernel/ksyms.c	Mon Sep 30 17:41:22 2002
+++ linux/kernel/ksyms.c	Tue Oct  1 18:34:41 2002
@@ -451,6 +451,9 @@
 EXPORT_SYMBOL(interruptible_sleep_on_timeout);
 EXPORT_SYMBOL(schedule);
 EXPORT_SYMBOL(schedule_timeout);
+#if CONFIG_SMP
+EXPORT_SYMBOL(set_cpus_allowed);
+#endif
 EXPORT_SYMBOL(yield);
 EXPORT_SYMBOL(__cond_resched);
 EXPORT_SYMBOL(jiffies);
diff -urN linux-2.4.20-pre8/kernel/sched.c linux/kernel/sched.c
--- linux-2.4.20-pre8/kernel/sched.c	Mon Sep 30 17:41:22 2002
+++ linux/kernel/sched.c	Tue Oct  1 18:54:49 2002
@@ -850,6 +850,46 @@
 
 void scheduling_functions_end_here(void) { }
 
+#if CONFIG_SMP
+
+/**
+ * set_cpus_allowed() - change a given task's processor affinity
+ * @p: task to bind
+ * @new_mask: bitmask of allowed processors
+ *
+ * Upon return, the task is running on a legal processor.  Note the caller
+ * must have a valid reference to the task: it must not exit() prematurely.
+ * This call can sleep; do not hold locks on call.
+ */
+void set_cpus_allowed(struct task_struct *p, unsigned long new_mask)
+{
+	new_mask &= cpu_online_map;
+	BUG_ON(!new_mask);
+
+	p->cpus_allowed = new_mask;
+
+	/*
+	 * If the task is on a no-longer-allowed processor, we need to move
+	 * it.  If the task is not current, then set need_resched and send
+	 * its processor an IPI to reschedule.
+	 */
+	if (!(p->cpus_runnable & p->cpus_allowed)) {
+		if (p != current) {
+			p->need_resched = 1;
+			smp_send_reschedule(p->processor);
+		}
+		/*
+		 * Wait until we are on a legal processor.  If the task is
+		 * current, then we should be on a legal processor the next
+		 * time we reschedule.  Otherwise, we need to wait for the IPI.
+		 */
+		while (!(p->cpus_runnable & p->cpus_allowed))
+			schedule();
+	}
+}
+
+#endif /* CONFIG_SMP */
+
 #ifndef __alpha__
 
 /*

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Re: [PATCH] set_cpus_allowed() for 2.4
@ 2002-12-09  3:02 Jim Houston
  0 siblings, 0 replies; 45+ messages in thread
From: Jim Houston @ 2002-12-09  3:02 UTC (permalink / raw)
  To: linux-kernel, mingo


Hi Everyone,

I ran into a lockup with the O(1) scheduler back in August and
exchanged email with Andrea. I tried a couple of his patches.
They prevented the lockup but it was still easy to have the
X server exiled to the inactive array for seconds at a time.
This got me started working on a patch to make the schedule
more fair.

I posted a patch archive here:
http://marc.theaimsgroup.com/?l=linux-kernel&m=103508412423719&w=2

It fixes fairness but breaks nice(2). Rik van Riel has a
patch here which builds on my patch which fixes this:
http://marc.theaimsgroup.com/?l=linux-kernel&m=103651801424031&w=2

I tested the combination of these patches with linux-2.5.48 and
it seems well behaved:-)  

I found this problem with the LTP waitpid06 test.  It actually
produced a live-lock. See this mail:
http://marc.theaimsgroup.com/?l=linux-kernel&m=103133744217082&w=2

I have been distracted by Posix timers but I plan to get back
finish this.

Jim Houston - Concurrent Computer Corp.

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Re: [PATCH] set_cpus_allowed() for 2.4
@ 2002-12-09 20:19 kernel
  0 siblings, 0 replies; 45+ messages in thread
From: kernel @ 2002-12-09 20:19 UTC (permalink / raw)
  To: Ingo Molnar; +Cc: linux-kernel


I confirm that 2.5 is much much much better. On a dual Pentium pro@180MHz i had 48 lames encoding wave to mp3 and X where responding as nothing has happening....
With 2.4 kaboom! Even the mouse slows down!

Nice work Ingo ;)

George


On Sun, 8 Dec 2002, Andrew Morton wrote:

> Yes, thanks.  Will we also be seeing the "interactivity estimator" fixes
> in 2.5?

yes, but i'd like to clarify one more thing - worst-case O(1)  
interactivity indeed is indeed very jerky (eg. the fast window moving
thing you noticed), but the normal behavior is much better than the old
scheduler's. Just try compiling the kernel with make -j4 under stock 2.4
and _everything_ in X will be jerky. With the O(1) scheduler things are
just as smooth as on an idle system - as long as your application does not
get rated CPU-intensive. [which happens too fast in the case you
described.] So we do have something in 2.5 that is visibly better in a
number of cases, and i want to preserve that - while fixing the
corner-cases discussed here. I'm working on it.

	Ingo

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 45+ messages in thread
* Re: [PATCH] set_cpus_allowed() for 2.4
@ 2002-12-13 23:08 Christoph Hellwig
  2002-12-13 21:34 ` Adrian Bunk
  0 siblings, 1 reply; 45+ messages in thread
From: Christoph Hellwig @ 2002-12-13 23:08 UTC (permalink / raw)
  To: marcelo; +Cc: rml, linux-kernel

Hi Marcelo,

now that all vendors ship a backport of Ingo's O(1) scheduler, external
projects like XFS have to track those trees in addition to the mainline kernel.

Having the common new APIs available in mainline would be a very good thing
to have for us and others.  Now that 2.4.20 already has a working yield()
the biggest missing part is set_cpus_allowed() to limit (kernel-)threads
to a specific CPU or set of CPUs.

--- linux/include/linux/sched.h~	Mon Sep 30 17:41:22 2002
+++ linux/include/linux/sched.h	Tue Oct  1 18:35:28 2002
@@ -163,6 +164,12 @@
 extern int start_context_thread(void);
 extern int current_is_keventd(void);
 
+#if CONFIG_SMP
+extern void set_cpus_allowed(struct task_struct *p, unsigned long new_mask);
+#else
+# define set_cpus_allowed(p, new_mask) do { } while (0)
+#endif
+
 /*
  * The default fd array needs to be at least BITS_PER_LONG,
  * as this is the granularity returned by copy_fdset().
--- linux/kernel/ksyms.c~	Mon Sep 30 17:41:22 2002
+++ linux/kernel/ksyms.c	Tue Oct  1 18:34:41 2002
@@ -451,6 +451,9 @@
 EXPORT_SYMBOL(interruptible_sleep_on_timeout);
 EXPORT_SYMBOL(schedule);
 EXPORT_SYMBOL(schedule_timeout);
+#if CONFIG_SMP
+EXPORT_SYMBOL(set_cpus_allowed);
+#endif
 EXPORT_SYMBOL(yield);
 EXPORT_SYMBOL(__cond_resched);
 EXPORT_SYMBOL(jiffies);
--- linux/kernel/sched.c~	Mon Sep 30 17:41:22 2002
+++ linux/kernel/sched.c	Tue Oct  1 18:54:49 2002
@@ -850,6 +850,45 @@
 
 void scheduling_functions_end_here(void) { }
 
+#if CONFIG_SMP
+/**
+ * set_cpus_allowed() - change a given task's processor affinity
+ * @p: task to bind
+ * @new_mask: bitmask of allowed processors
+ *
+ * Upon return, the task is running on a legal processor.  Note the caller
+ * must have a valid reference to the task: it must not exit() prematurely.
+ * This call can sleep; do not hold locks on call.
+ */
+void set_cpus_allowed(struct task_struct *p, unsigned long new_mask)
+{
+	new_mask &= cpu_online_map;
+	BUG_ON(!new_mask);
+
+	p->cpus_allowed = new_mask;
+
+	/*
+	 * If the task is on a no-longer-allowed processor, we need to move
+	 * it.  If the task is not current, then set need_resched and send
+	 * its processor an IPI to reschedule.
+	 */
+	if (!(p->cpus_runnable & p->cpus_allowed)) {
+		if (p != current) {
+			p->need_resched = 1;
+			smp_send_reschedule(p->processor);
+		}
+
+		/*
+		 * Wait until we are on a legal processor.  If the task is
+		 * current, then we should be on a legal processor the next
+		 * time we reschedule.  Otherwise, we need to wait for the IPI.
+		 */
+		while (!(p->cpus_runnable & p->cpus_allowed))
+			schedule();
+	}
+}
+#endif /* CONFIG_SMP */
+
 #ifndef __alpha__
 
 /*

^ permalink raw reply	[flat|nested] 45+ messages in thread

end of thread, other threads:[~2002-12-13 21:34 UTC | newest]

Thread overview: 45+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-01 23:03 [PATCH] set_cpus_allowed() for 2.4 Robert Love
2002-10-02 13:01 ` Christoph Hellwig
2002-10-02 15:00   ` Robert Love
2002-11-05  3:37 ` Christoph Hellwig
2002-11-06 15:32   ` Adrian Bunk
2002-11-07 21:42     ` Christoph Hellwig
2002-12-02 17:12   ` Mikael Pettersson
2002-12-03  0:51     ` Christoph Hellwig
2002-12-02 17:47       ` Mikael Pettersson
2002-12-02 19:10         ` Robert Love
2002-12-03  0:26 Christoph Hellwig
2002-12-02 17:24 ` Jeff Garzik
2002-12-02 18:57   ` Robert Love
2002-12-03  0:51   ` Christoph Hellwig
2002-12-02 17:50 ` Martin J. Bligh
2002-12-02 18:50   ` Adrian Bunk
2002-12-02 19:12     ` Robert Love
2002-12-02 19:30   ` Andrew Morton
2002-12-02 19:50     ` Andrea Arcangeli
2002-12-03 20:49       ` Andrew Morton
2002-12-03 21:09         ` Martin J. Bligh
2002-12-04  0:09           ` Andrea Arcangeli
2002-12-04  0:06         ` Andrea Arcangeli
2002-12-04  0:30           ` Andrew Morton
2002-12-04  0:42             ` Andrea Arcangeli
2002-12-04  1:03               ` William Lee Irwin III
2002-12-04  9:25                 ` William Lee Irwin III
2002-12-04  1:14               ` Andrew Morton
2002-12-04  1:21                 ` Andrea Arcangeli
2002-12-04  2:14                   ` Andrew Morton
2002-12-06 18:11                 ` William Lee Irwin III
2002-12-08 13:23     ` Ingo Molnar
2002-12-08 19:56       ` Andrew Morton
2002-12-09 20:13         ` Ingo Molnar
2002-12-03  1:11   ` Christoph Hellwig
2002-12-02 18:59     ` Robert Love
2002-12-02 22:47     ` Alan Cox
2002-12-02 22:38       ` Christoph Hellwig
2002-12-02 22:41         ` Robert Love
2002-12-07 16:55           ` bill davidsen
2002-12-09  3:02 Jim Houston
2002-12-09 20:19 kernel
2002-12-13 23:08 Christoph Hellwig
2002-12-13 21:34 ` Adrian Bunk
2002-12-14  4:55   ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).