Skip to contentSkip to conversation

Abstracted Priority Inheritance for RT

PATCH In review Daniel Walker 4 participants 13 messages Jun 1, 2005 – Jun 3, 2005 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Abstracted Priority Inheritance for RT

I've abstracted priority inheritance from the RT patch. I created pi.h and pi.c that can be used by other structures that need priority inheritance. I also added a config option CONFIG_PRIORITY_INHERITANCE so that PI can be turned off . This could also be made completely separate from the RT patch.

I'm not sure if this is a draw back or not. It uses a callback structure to notify when a task changes priority , and to obtain an owner task_struct. While this works I'm not sure that it is the best method, it seems somewhat slow. At the same time it may make it more flexible for other structures to use.

While doing this I noticed that deadlock detect could be modified in the same way. So that it would work for anything using the blocked_on field of the task_struct. We could also have abstracted deadlock detect.

Daniel

include/linux/pi.h +126 -0
Index: linux-2.6.11/include/linux/pi.h
===================================================================
@@ -0,0 +1,126 @@
+/*
+ * Real-Time Preemption Support
+ *
+ * started by Ingo Molnar:
+ *
+ *  Copyright (C) 2004, 2005 Red Hat, Inc., Ingo Molnar <[email protected]>
+ *
+ *  Copyright (C) 2001-2005 MontaVista Software, Inc.
+ *  Daniel Walker <[email protected]>
+ */
+
+#ifndef _LINUX_PI_H
+#define _LINUX_PI_H
+
+#include <linux/plist.h>
+
+struct rt_mutex_waiter;
+struct task_struct;
+
+/* 
+ * This is the PI call back structure. All APIs using Priority Inheritance
+ * must implement the functions in this callback structure. They must also 
+ * use the rt_mutex_waiter structure.
+ *
+ */
+struct rt_mutex_waiter_ops {
+
+	/* 
+	 * This function is used to get a struct task_struct for
+	 * the lock owner. 
+	 * @lock: The lock in question.
+	 */
+	struct task_struct * (*lock_owner)(void* lock);
+
+	/*
+	 * This function signals that the mutex waiter has changed
+	 * priorities.
+	 * @lock: The lock in question
+	 * @waiter: The waiter on the lock above
+	 */
+	void (*waiter_changed_prio) (void* lock, struct rt_mutex_waiter* waiter);
+
+};
+
+/*
+ * This is the control structure for tasks blocked on an
+ * mutex:
+ */
+struct rt_mutex_waiter {
+	void *lock;
+	struct plist     list;
+	struct plist     pi_list;
+	struct task_struct *task;
+
+	struct rt_mutex_waiter_ops * mutex_ops;
+
+	unsigned long eip;      // for debugging
+};
+
+#ifdef CONFIG_PRIORITY_INHERITANCE
+/*
+ * Unlock these on crash:
+ */
+extern void zap_pi_lock(void);
+
+/*
+ * Very expensive way to find out if a pi_waiter exists from a lock
+ */
+extern int pi_is_lock_waiter(void *lock, struct task_struct *owner);
+
+/*
+ * Very expensive way to find out if a waiter is a pi_waiter
+ */
+extern int pi_is_waiter(struct rt_mutex_waiter *waiter, struct task_struct *owner);
+
+
+/*
+ * Remove a waiter from the PI list, of old_owner
+ */
+extern void pi_remove_waiter(struct rt_mutex_waiter *waiter, struct task_struct *old_owner);
+
+/*
+ * Move PI waiters of this lock to the new owner:
+ */
+extern void
+pi_change_owner(void *lock, struct task_struct *old_owner,
+                   struct task_struct *new_owner);
+
+/*
+ * This functions should be called when a task no longer holds a
+ * lock. It will restore the task to it original priority, or
+ * to the next highest priority PI waiter.
+ *
+ * @lock: the lock that this task just finished with
+ * @old_owner: the task_struct for the old owner
+ *
+ */
+extern void pi_restore_task(void *lock, struct task_struct *old_owner);
+
+/*
+ * This function performs all nessesary PI when a new tasks blocks on a
+ * specific lock
+ *
+ * @waiter : The rt_mutex_waiter structure for the blocking task
+ * @task : The task structure for the blocking task
+ * @lock: The lock that the task is blocking on
+ * @eip : The instruction pointer of where the task blocked
+ * @all_tasks_pi: boolean value dispense with PI on non-RT tasks
+ *
+ */
+extern void
+pi_task_blocks_on_lock(struct rt_mutex_waiter *waiter, struct task_struct *task,
+                   void *lock, unsigned long eip, int all_tasks_pi);
+
+#else
+
+#define zap_pi_lock()	{ /* */ }
+#define pi_is_lock_waiter(lock, owner)	(0)
+#define pi_is_waiter(waiter, owner)	(0)
+#define pi_remove_waiter(waiter,old_owner)	{ /* */ }
+#define pi_change_owner(lock, old_owner, new_owner)	{ new_owner->blocked_on = NULL; }
+#define pi_restore_task(lock, old_owner)	{ /* */ }
+#define pi_task_blocks_on_lock(waiter, task, lock, eip, all_tasks_pi)	{ (task)->blocked_on = waiter; }
+
+#endif /* CONFIG_PRIORITY_INHERITANCE */ 
+#endif /* _LINUX_PI_H */
include/linux/rt_lock.h +1 -13
Index: linux-2.6.11/include/linux/rt_lock.h
===================================================================
@@ -13,6 +13,7 @@
 #include <linux/config.h>
 #include <linux/list.h>
 #include <linux/plist.h>
+#include <linux/pi.h>
 
 /*
  * These are the basic SMP spinlocks, allowing only a single CPU anywhere.
@@ -79,19 +80,6 @@ struct rt_mutex {
 # endif
 };
 
-/*
- * This is the control structure for tasks blocked on an
- * RT mutex:
- */
-struct rt_mutex_waiter {
-	struct rt_mutex *lock;
-	struct plist	 list;
-	struct plist	 pi_list;
-	struct task_struct *task;
-
-	unsigned long eip;	// for debugging
-};
-
 #ifdef CONFIG_RT_DEADLOCK_DETECT
 # define __RT_MUTEX_INITIALIZER(lockname) \
 	{ .wait_lock = RAW_SPIN_LOCK_UNLOCKED, \
kernel/rt.c +43 -243

447 lines not on this page · raw message

lib/Kconfig.RT +12 -0
Index: linux-2.6.11/lib/Kconfig.RT
===================================================================
@@ -86,6 +86,18 @@ config PREEMPT
 	default y
 	depends on PREEMPT_DESKTOP || PREEMPT_RT
 
+config PRIORITY_INHERITANCE
+	bool "Priority Inheritance"
+	default n if !PREEMPT_RT 
+	help
+	  This option enables system wide priority inheritance. It will
+	  enable a high priority waiting task to transfer it's priority
+	  down to the task which is being waited on. This option will 
+	  mostly effect the operation of mutexes and semaphore , but
+	  could also effect other blocking objects in the kernel. The
+	  end result should be faster sleep times for high priority tasks,
+	  but will also increase locking overhead.
+
 config PREEMPT_SOFTIRQS
 	bool "Thread Softirqs"
 	default n
lib/Makefile +1 -2
Index: linux-2.6.11/lib/Makefile
===================================================================
@@ -14,8 +14,7 @@ ifeq ($(CONFIG_DEBUG_KOBJECT),y)
 CFLAGS_kobject.o += -DDEBUG
 CFLAGS_kobject_uevent.o += -DDEBUG
 endif
-
-obj-$(CONFIG_PREEMPT_RT) += plist.o
+obj-$(CONFIG_PRIORITY_INHERITANCE) += pi.o
 obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
 lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
 lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o
lib/pi.c +261 -0

261 lines not on this page · raw message

#1 lore raw

Daniel,

I'll start taking a closer look at it tomorrow, but on first working with it, you might want to change the following config on PRIORITY_INHERITANCE. At least for me on a "make oldconfig" the default is usually "No" so I made the following change. It may even be wise to just force it. I haven't taking a look yet, but would PREEMPT_RT work without the inheritance?

-- Steve

Kconfig.RT renamed from Kconfig.RT.orig +1 -1
@@ -88,7 +88,7 @@
 
 config PRIORITY_INHERITANCE
 	bool "Priority Inheritance"
-	default n if !PREEMPT_RT 
+	default y if PREEMPT_RT 
 	help
 	  This option enables system wide priority inheritance. It will
 	  enable a high priority waiting task to transfer it's priority
#2 lore raw
In reply to Daniel Walker
Quoted 5 lines · Daniel Walker

I've abstracted priority inheritance from the RT patch. I created pi.h and pi.c that can be used by other structures that need priority inheritance. I also added a config option CONFIG_PRIORITY_INHERITANCE so that PI can be turned off . This could also be made completely separate from the RT patch.

i'd rather not slow things down by callbacks and other abstraction before seeing how things want to integrate in fact. Do we really need the callbacks?

Ingo

#3 lore raw
Quoted 3 lines · Ingo Molnar

i'd rather not slow things down by callbacks and other abstraction before seeing how things want to integrate in fact. Do we really need the callbacks?

I think it would be hard to do without a way to signal when a waiter changes priorties. Since other structures could handle it differently.

Another problem is that there needs to be a clear way to know which structure owns the rt_mutex_waiter . Something in there needs to be unique. It can't be assumed anymore that everything is an rt_mutex.

The lock owner could be put into the rt_mutex_waiter structure. Which would make the structure bigger, but it's usually stack space. This would also create some duplicate data since every waiter would need to hold the owners task_struct pointer.

Daniel

#4 lore raw
Quoted 10 lines · Daniel Walker
Quoted 3 lines · Ingo Molnar

i'd rather not slow things down by callbacks and other abstraction before seeing how things want to integrate in fact. Do we really need the callbacks?

I think it would be hard to do without a way to signal when a waiter changes priorties.

Do you plan to use that callback for priority inheritance? If so: It would lead to an recursive algorithm. That is not very nice in the kernel with a limited call-stack. It is not so much a problem if the mechanism is used in the kernel only, but if it is used for user-space locking, which can have unlimited neesting, it is potential problem.

Esben

Quoted 4 lines

[...]

Daniel

#5 lore raw
Quoted 6 lines · Esben Nielsen

Do you plan to use that callback for priority inheritance? If so: It would lead to an recursive algorithm. That is not very nice in the kernel with a limited call-stack. It is not so much a problem if the mechanism is used in the kernel only, but if it is used for user-space locking, which can have unlimited neesting, it is potential problem.

There is an API for for priority inheritance, the call by is strictly for the PI mechanism to signal when it changes a waiters priority , as the result of PI.

It's somewhat explained in linux/pi.h . Currently the rt_mutex uses this callback to move the waiter depending on it's new priority.

I'm not sure I see how this could become recursive, could you explain more?

Daniel

#6 lore raw
Quoted 13 lines · Daniel Walker
Quoted 6 lines · Esben Nielsen

Do you plan to use that callback for priority inheritance? If so: It would lead to an recursive algorithm. That is not very nice in the kernel with a limited call-stack. It is not so much a problem if the mechanism is used in the kernel only, but if it is used for user-space locking, which can have unlimited neesting, it is potential problem.

There is an API for for priority inheritance, the call by is strictly for the PI mechanism to signal when it changes a waiters priority , as the result of PI.

Quoted 5 lines

It's somewhat explained in linux/pi.h . Currently the rt_mutex uses this callback to move the waiter depending on it's new priority.

I'm not sure I see how this could become recursive, could you explain more?

Let us say you have task1 waiting on task2 waiting on task3 waiting on task4 etc. When you try to boost the prorities you will set the priority of each task using the hook, right? In the hook you will set the priority of the next task using the hook, right? ....

Esben

Quoted 2 lines

Daniel

#7 lore raw
Quoted 5 lines · Esben Nielsen

Let us say you have task1 waiting on task2 waiting on task3 waiting on task4 etc. When you try to boost the prorities you will set the priority of each task using the hook, right? In the hook you will set the priority of the next task using the hook, right? ....

No .. The callback doesn't change priorities, it just signals that a priority has changed. The priority changing is iterative, and there is a function added to sched.c (not in my patch, but in the RT patch) to actualy change the priorities. My patch only adds a new structure to the prioritiy inheritance that was already in the RT patch, I'm not remaking the actualy PI code.

Daniel

#8 lore raw
Quoted 9 lines · Daniel Walker
Quoted 5 lines · Esben Nielsen

Let us say you have task1 waiting on task2 waiting on task3 waiting on task4 etc. When you try to boost the prorities you will set the priority of each task using the hook, right? In the hook you will set the priority of the next task using the hook, right? ....

Quoted 7 lines

No .. The callback doesn't change priorities, it just signals that a priority has changed. The priority changing is iterative, and there is a function added to sched.c (not in my patch, but in the RT patch) to actualy change the priorities. My patch only adds a new structure to the prioritiy inheritance that was already in the RT patch, I'm not remaking the actualy PI code.

Good :-)
I asked the question because I considered (and started but didn't have time) doing what you have done. I wanted to generalise the rt_mutex to have real rw_lock as well - which was dropped due to the non-deterministc behavioir even with PI. To do that I needed to have the recursion and the callback..

Esben

Quoted 2 lines

Daniel

#9 lore raw
Quoted 6 lines · Esben Nielsen

Good :-)
I asked the question because I considered (and started but didn't have time) doing what you have done. I wanted to generalise the rt_mutex to have real rw_lock as well - which was dropped due to the non-deterministc behavioir even with PI. To do that I needed to have the recursion and the callback..

I'm not planning to do a real rw-lock, but I hope this generic PI will help with that. I'm still not completely satisfied with this callback structure , but I don't see a better way to do it. Do you have an suggestions for replacing it?

Daniel

#10 lore raw
Quoted 11 lines · Daniel Walker
Quoted 6 lines · Esben Nielsen

Good :-)
I asked the question because I considered (and started but didn't have time) doing what you have done. I wanted to generalise the rt_mutex to have real rw_lock as well - which was dropped due to the non-deterministc behavioir even with PI. To do that I needed to have the recursion and the callback..

I'm not planning to do a real rw-lock, but I hope this generic PI will help with that.

I came to the conclusion that an rw-lock would make all writers non-deterministic. I had a small discussion on this list with Ingo about it. He had also come to the same conclusion.

Quoted 4 lines

I'm still not completely satisfied with this callback structure , but I don't see a better way to do it. Do you have an suggestions for replacing it?

No, I got stock there too.

But right now the following ideas spring to my mind:
If it is to solve the problem of having a callback wrap every use in macroes and use the TYPE_EQUAL() to expclicit call the right function. Only if the explicit type is unknown in the macro use the callback. That should optimize stuff a little bit.. Just a wild idea.

If it is explicitly for PI you can do a thing like
waiter->get_next_waiter();
to resolve the chain of waiters. Then you can have the PI algotithm work iteratively without knowing the explicit kind of lock involved.

Quoted 2 lines

Daniel

Esben

#11 lore raw
Quoted 5 lines · Esben Nielsen

But right now the following ideas spring to my mind:
If it is to solve the problem of having a callback wrap every use in macroes and use the TYPE_EQUAL() to expclicit call the right function. Only if the explicit type is unknown in the macro use the callback. That should optimize stuff a little bit.. Just a wild idea.

It's a little hard to do that. It's basically the situation you have below, there is no way to know what "waiter" is at compile time, so you can't really do the TYPE_EQUAL() trick on "get_next_waiter" .

I have "waiter->waiter_changed_prio()" which results in the same problem. There is no way to know what "type" waiter is at compile time ..

Quoted 4 lines

If it is explicitly for PI you can do a thing like
waiter->get_next_waiter();
to resolve the chain of waiters. Then you can have the PI algotithm work iteratively without knowing the explicit kind of lock involved.

This is essentially what I have now, but it's also what I'm unhappy with. The only reason that I don't like this method is that it's a little slow .. I don't mind keeping it as long as no better way presents itself.

Daniel

#12 lore raw
Quoted 22 lines · Daniel Walker
Quoted 5 lines · Esben Nielsen

But right now the following ideas spring to my mind:
If it is to solve the problem of having a callback wrap every use in macroes and use the TYPE_EQUAL() to expclicit call the right function. Only if the explicit type is unknown in the macro use the callback. That should optimize stuff a little bit.. Just a wild idea.

It's a little hard to do that. It's basically the situation you have below, there is no way to know what "waiter" is at compile time, so you can't really do the TYPE_EQUAL() trick on "get_next_waiter" .

I have "waiter->waiter_changed_prio()" which results in the same problem. There is no way to know what "type" waiter is at compile time ..

Quoted 4 lines

If it is explicitly for PI you can do a thing like
waiter->get_next_waiter();
to resolve the chain of waiters. Then you can have the PI algotithm work iteratively without knowing the explicit kind of lock involved.

Quoted 7 lines

This is essentially what I have now, but it's also what I'm unhappy with. The only reason that I don't like this method is that it's a little slow .. I don't mind keeping it as long as no better way presents itself.

Daniel

C++ templates would have helped a lot... But we only have the low tech version: macroes.

Esben