Skip to contentSkip to conversation

SysRq-n changes RT tasks to normal

PATCH In review Måns Rullgård 2 participants 4 messages Oct 29, 2004

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

PATCH 1/1 SysRq-n changes RT tasks to normal

This makes SysRq-n change all RT tasks into normal tasks. Useful for regaining control if an RT task is behaving badly.

# This is a BitKeeper generated diff -Nru style patch. #
# ChangeSet

#   2004/10/09 12:44:26+02:00 mru@ford.(none) 
#   sysrq-n changes all RT tasks into normally scheduled tasks
#   this is useful in case an RT task is misbehaving
# 
# kernel/sched.c
#   2004/10/09 12:44:13+02:00 mru@ford.(none) +32 -0
#   normalize_rt_tasks() converts all RT tasks into normal tasks
# 
# include/linux/sched.h
#   2004/10/09 12:44:13+02:00 mru@ford.(none) +6 -0
#   prototype for normalize_rt_tasks()
# 
# drivers/char/sysrq.c
#   2004/10/09 12:44:13+02:00 mru@ford.(none) +11 -1
#   sysrq-n calls normalize_rt_tasks()
# 
diff -Nru a/drivers/char/sysrq.c b/drivers/char/sysrq.c
--- a/drivers/char/sysrq.c	2004-10-29 13:19:05 +02:00
+++ b/drivers/char/sysrq.c	2004-10-29 13:19:05 +02:00
@@ -216,6 +216,16 @@

/* END SIGNAL SYSRQ HANDLERS BLOCK */

+static void sysrq_handle_unrt(int key, struct pt_regs *pt_regs,
+ struct tty_struct *tty)
+{
+ normalize_rt_tasks();
+}
+static struct sysrq_key_op sysrq_unrt_op = {
+ .handler = sysrq_handle_unrt,
+ .help_msg = "Nice",
+ .action_msg = "Nice All RT Tasks"
+};

/* Key Operations table and lock */
static spinlock_t sysrq_key_table_lock = SPIN_LOCK_UNLOCKED;
@@ -250,7 +260,7 @@
#endif
/* l */ NULL,
/* m */ &sysrq_showmem_op,
-/* n */ NULL,
+/* n */ &sysrq_unrt_op,
/* o */ NULL, /* This will often be registered
as 'Off' at init time */
/* p */ &sysrq_showregs_op,

include/linux/sched.h +6 -0
diff -Nru a/include/linux/sched.h b/include/linux/sched.h
@@ -1027,6 +1027,12 @@
 extern long sched_setaffinity(pid_t pid, cpumask_t new_mask);
 extern long sched_getaffinity(pid_t pid, cpumask_t *mask);
 
+#ifdef CONFIG_MAGIC_SYSRQ
+
+extern void normalize_rt_tasks(void);
+
+#endif
+
 #endif /* __KERNEL__ */
 
 #endif
kernel/sched.c +32 -0
diff -Nru a/kernel/sched.c b/kernel/sched.c
@@ -4767,3 +4767,35 @@
 }
 EXPORT_SYMBOL(__might_sleep);
 #endif
+
+#ifdef CONFIG_MAGIC_SYSRQ
+void normalize_rt_tasks(void)
+{
+	struct task_struct *p;
+	prio_array_t *array;
+	unsigned long flags;
+	runqueue_t *rq;
+
+	for_each_process (p) {
+		if (!rt_task(p))
+			continue;
+
+		read_lock_irq(&tasklist_lock);
+		rq = task_rq_lock(p, &flags);
+
+		array = p->array;
+		if (array)
+			deactivate_task(p, task_rq(p));
+		__setscheduler(p, SCHED_NORMAL, 0);
+		if (array) {
+			__activate_task(p, task_rq(p));
+			resched_task(rq->curr);
+		}
+
+		task_rq_unlock(rq, &flags);
+		read_unlock_irq(&tasklist_lock);
+	}
+}
+
+EXPORT_SYMBOL(normalize_rt_tasks);
+#endif /* CONFIG_MAGIC_SYSRQ */
Signature · 2 lines
Måns Rullgård
[email protected]
#1 lore raw

kernel/sched.c line 4784

Quoted 6 lines · Måns Rullgård
kernel/sched.c +6 -0
+	for_each_process (p) {
+		if (!rt_task(p))
+			continue;
+
+		read_lock_irq(&tasklist_lock);
+		rq = task_rq_lock(p, &flags);

you must take the tasklist_lock outside the for_each_process().

otherwise, looks good.

Ingo

#2 lore raw
Quoted 10 lines · Ingo Molnar

Review

Quoted 6 lines · Måns Rullgård
(quoted diff) +6 -0
+	for_each_process (p) {
+		if (!rt_task(p))
+			continue;
+
+		read_lock_irq(&tasklist_lock);
+		rq = task_rq_lock(p, &flags);

you must take the tasklist_lock outside the for_each_process().

The danger of cut and paste programming. New diff below.

drivers/char/sysrq.c +11 -1
diff -Nru a/drivers/char/sysrq.c b/drivers/char/sysrq.c
@@ -216,6 +216,16 @@
 
 /* END SIGNAL SYSRQ HANDLERS BLOCK */
 
+static void sysrq_handle_unrt(int key, struct pt_regs *pt_regs,
+			      struct tty_struct *tty)
+{
+	normalize_rt_tasks();
+}
+static struct sysrq_key_op sysrq_unrt_op = {
+	.handler	= sysrq_handle_unrt,
+	.help_msg	= "Nice",
+	.action_msg	= "Nice All RT Tasks"
+};
 
 /* Key Operations table and lock */
 static spinlock_t sysrq_key_table_lock = SPIN_LOCK_UNLOCKED;
@@ -250,7 +260,7 @@
 #endif
 /* l */	NULL,
 /* m */	&sysrq_showmem_op,
-/* n */	NULL,
+/* n */	&sysrq_unrt_op,
 /* o */	NULL, /* This will often be registered
 		 as 'Off' at init time */
 /* p */	&sysrq_showregs_op,
include/linux/sched.h +6 -0
diff -Nru a/include/linux/sched.h b/include/linux/sched.h
@@ -1027,6 +1027,12 @@
 extern long sched_setaffinity(pid_t pid, cpumask_t new_mask);
 extern long sched_getaffinity(pid_t pid, cpumask_t *mask);
 
+#ifdef CONFIG_MAGIC_SYSRQ
+
+extern void normalize_rt_tasks(void);
+
+#endif
+
 #endif /* __KERNEL__ */
 
 #endif
kernel/sched.c +32 -0
diff -Nru a/kernel/sched.c b/kernel/sched.c
@@ -4590,3 +4590,35 @@
 }
 EXPORT_SYMBOL(__might_sleep);
 #endif
+
+#ifdef CONFIG_MAGIC_SYSRQ
+void normalize_rt_tasks(void)
+{
+       struct task_struct *p;
+       prio_array_t *array;
+       unsigned long flags;
+       runqueue_t *rq;
+
+       read_lock_irq(&tasklist_lock);
+       for_each_process (p) {
+               if (!rt_task(p))
+                       continue;
+
+               rq = task_rq_lock(p, &flags);
+
+               array = p->array;
+               if (array)
+                       deactivate_task(p, task_rq(p));
+               __setscheduler(p, SCHED_NORMAL, 0);
+               if (array) {
+                       __activate_task(p, task_rq(p));
+                       resched_task(rq->curr);
+               }
+
+               task_rq_unlock(rq, &flags);
+       }
+       read_unlock_irq(&tasklist_lock);
+}
+
+EXPORT_SYMBOL(normalize_rt_tasks);
+#endif /* CONFIG_MAGIC_SYSRQ */
Signature · 2 lines
Måns Rullgård
[email protected]
#3 lore raw
Quoted 3 lines · Måns Rullgård
Quoted 1 line

you must take the tasklist_lock outside the for_each_process().

The danger of cut and paste programming. New diff below.

it's looking good now.

Ingo