Skip to contentSkip to conversation

set_cpus_allowed() for 2.4

PATCH Christoph Hellwig 2 participants 3 messages Dec 13, 2002 Scheduler

lore
Share & tools
lore
#0 lore raw

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.

include/linux/sched.h renamed from include/linux/sched.h~ +6 -0
@@ -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().
kernel/ksyms.c renamed from kernel/ksyms.c~ +3 -0
@@ -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);
kernel/sched.c renamed from kernel/sched.c~ +39 -0
@@ -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__
 
 /*
#1 lore raw
Quoted 3 lines · Christoph Hellwig

...
now that all vendors ship a backport of Ingo's O(1) scheduler, external
...

#include <all-vendors-except-Debian> ;-)

cu
Adrian

Signature · 4 lines
       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed
#2 lore raw
Quoted 7 lines · Adrian Bunk
Quoted 3 lines · Christoph Hellwig

...
now that all vendors ship a backport of Ingo's O(1) scheduler, external
...

#include <all-vendors-except-Debian> ;-)

OOPS, I reused the first template again. I think I owe you a beer :)