Priority Inheritance enhancements
Subsystems (6)
cover Priority Inheritance enhancements
** RFC for PREEMPT_RT branch, 26-rt1 **
Hi All,
The following series applies to 26-rt1 as a request-for-comment on a new approach to priority-inheritance (PI), as well as some performance enhancements to take advantage of those new approaches. This yields at least a 10-15% improvement for diskio on my 4-way x86_64 system. An 8-way system saw as much as 700% improvement during early testing, but I have not recently reconfirmed this number.
Motivation for series:
I have several ideas on things we can do to enhance and improve kernel performance with respect to PREEMPT_RT
1) For instance, it would be nice to support priority queuing and (at least positional) inheritance in the wait-queue infrastructure.
2) Reducing overhead in the real-time locks (sleepable replacements for spinlock_t in PREEMPT_RT) to try to approach the minimal overhead if their non-rt equivalent. We have determined via instrumentation that one area of major overhead is the pi-boost logic.
However, today the PI code is entwined in the rtmutex infrastructure, yet we require more flexibility if we want to address (1) and (2) above. Therefore the first step is to separate the PI code away from rtmutex into its own library (libpi). This is covered in patches 1-6.
(I realize patch #6 is a little hard to review since I removed and added a lot of code that the unified diff is all mashing together...I will try to find a way to make this more readable).
Patch 7 is the first real consumer of the libpi logic to try to enhance performance. It accomplishes this by deferring pi-boosting a lock owner unless it is absolutely necessary. Since instrumentation shows that the majority of locks are acquired either via the fast-path, or via the adaptive-spin path, we can eliminate most of the pi-overhead with this technique. This yields a measurable performance gain (at least 10% for workloads with heavy lock contention was observed in our lab).
For your convenience, you can also find these patches in a git tree here:
git://git.kernel.org/pub/scm/linux/kernel/git/ghaskins/linux-2.6-hacks.git pi-rework
We have not yet completed the work on the pi-waitqueues or any of the other related pi enhancements. Those will be coming in a follow-on announcement.
Feedback/comments welcome!
Regards,
-Greg
RFC PATCH 1/7 add generalized priority-inheritance interface
The kernel currently addresses priority-inversion through priority- inheritence. However, all of the priority-inheritence logic is integrated into the Real-Time Mutex infrastructure. This causes a few problems:
1) This tightly coupled relationship makes it difficult to extend to other areas of the kernel (for instance, pi-aware wait-queues may be desirable).
2) Enhancing the rtmutex infrastructure becomes challenging because there is no seperation between the locking code, and the pi-code.
This patch aims to rectify these shortcomings by designing a stand-alone pi framework which can then be used to replace the rtmutex-specific version. The goal of this framework is to provide similar functionality to the existing subsystem, but with sole focus on PI and the relationships between objects that can boost priority, and the objects that get boosted.
We introduce the concept of a "pi_source" and a "pi_sink", where, as the name suggests provides the basic relationship of a priority source, and its boosted target. A pi_source acts as a reference to some arbitrary source of priority, and a pi_sink can be boosted (or deboosted) by a pi_source. For more details, please read the library documentation.
There are currently no users of this inteface.
---
Documentation/libpi.txt | 59 +++++++ include/linux/pi.h | 226 +++++++++++++++++++++++++++ lib/Makefile | 3 lib/pi.c | 398 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 685 insertions(+), 1 deletions(-)
Documentation/libpi.txt new file +59 -0
@@ -0,0 +1,59 @@ +lib/pi.c - Priority Inheritance library + +Sources and sinks: +------------ + +This library introduces the basic concept of a "pi_source" and a "pi_sink", where, as the name suggests provides the basic relationship of a priority source, and its boosted target. + +A pi_source is simply a reference to some arbitrary priority value that may range from 0 (highest prio), to MAX_PRIO (currently 140, lowest prio). A pi_source calls pi_sink.boost() whenever it wishes to boost the sink to (at least minimally) the priority value that the source represents. It uses pi_sink.boost() for both the initial boosting, or for any subsequent refreshes to the value (even if the value is decreasing in logical priority). The policy of the sink will dictate what happens as a result of that boost. Likewise, a pi_source calls pi_sink.deboost() to stop contributing to the sink's minimum priority. + +It is important to note that a source is a reference to a priority value, not a value itself. This is one of the concepts that allows the interface to be idempotent, which is important for properly updating a chain of sources and sinks in the proper order. If we passed the priority on the stack, the order in which the system executes could allow the actual value that is set to race. + +Nodes: + +A pi_node is a convenience object which is simultaneously a source and a sink. As its name suggests, it would typically be deployed as a node in a pi-chain. Other pi_sources can boost a node via its pi_sink.boost() interface. Likewise, a node can boost a fixed number of sinks via the node.add_sink() interface. + +Generally speaking, a node takes care of many common operations associated with being a “link in the chain”, such as: + + 1) determining the current priority of the node based on the (logically) highest priority source that is boosting the node. + 2) boosting/deboosting upstream sinks whenever the node locally changes priority. + 3) taking care to avoid deadlock during a chain update. + +Design details: + +Destruction: + +The pi-library objects are designed to be implicitly-destructable (meaning they do not require an explicit “free()” operation when they are not used anymore). This is important considering their intended use (spinlock_t's which are also implicitly-destructable). As such, any allocations needed for operation must come from internal structure storage as there will be no opportunity to free it later. + +Multiple sinks per Node: + +We allow multiple sinks to be associated with a node. This is a slight departure from the previous implementation which had the notion of only a single sink (i.e. “task->pi_blocked_on”). The reason why we added the ability to add more than one sink was not to change the default chaining model (I.e. multiple boost targets), but rather to add a flexible notification mechanism that is peripheral to the chain, which are informally called “leaf sinks”. + +Leaf-sinks are boostable objects that do not perpetuate a chain per se. Rather, they act as endpoints to a priority boosting. Ultimately, every chain ends with a leaf-sink, which presumably will act on the new priority information. However, there may be any number of leaf-sinks along a chain as well. Each one will act on its localized priority in its own implementation specific way. For instance, a task_struct pi-leaf may change the priority of the task and reschedule it if necessary. Whereas an rwlock leaf-sink may boost a list of reader-owners. + +The following diagram depicts an example relationship (warning: cheesy ascii art) + + --------- --------- + | leaf | | leaf | + --------- --------- + / / + --------- / ---------- / --------- --------- + ->-| node |->---| node |-->---| node |->---| leaf | + --------- ---------- --------- --------- + +The reason why this was done was to unify the notion of a “sink” to a single interface, rather than having something like task->pi_blocks_on and a separate callback for the leaf action. Instead, any downstream object can be represented by a sink, and the implementation details are hidden (e.g. im a task, im a lock, im a node, im a work-item, im a wait-queue, etc). + +Sinkrefs: + +Each pi_sink.boost() operation is represented by a unique pi_source to properly facilitate a one node to many source relationship. Therefore, if a pi_node is to act as aggregator to multiple sinks, it implicitly must have one internal pi_source object for every sink that is added (via node.add_sink(). This pi_source object has to be internally managed for the lifetime of the sink reference. + +Recall that due to the implicit-destruction requirement above, and the fact that we will typically be executing in a preempt-disabled region, we have to be very careful about how we allocate references to those sinks. More on that next. But long story short we limit the number of sinks to MAX_PI_DEPENDENDICES (currently 5). + +Locking: + +(work in progress....) + + + + +
include/linux/pi.h new file +226 -0
226 lines not on this page · raw message
lib/Makefile +2 -1
@@ -23,7 +23,8 @@ lib-$(CONFIG_SMP) += cpumask.o lib-y += kobject.o kref.o klist.o obj-y += div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ - bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o + bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ + pi.o ifeq ($(CONFIG_DEBUG_KOBJECT),y) CFLAGS_kobject.o += -DDEBUG
lib/pi.c new file +398 -0
398 lines not on this page · raw message
--
To unsubscribe from this list: send the line "unsubscribe linux-rt-users" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
RFC PATCH 2/7 sched: add the basic PI infrastructure to the task_struct
In reply to Gregory HaskinsThis is a first pass at converting the system to use the new PI library. We dont go for a wholesale replacement quite yet so that we can focus on getting the basic plumbing in place. Later in the series we will begin replacing some of the proprietary logic with the generic framework.
---
include/linux/sched.h | 37 +++++++-- include/linux/workqueue.h | 2 kernel/fork.c | 1 kernel/rcupreempt-boost.c | 18 +--- kernel/rtmutex.c | 6 + kernel/sched.c | 188 ++++++++++++++++++++++++++++++++------------- kernel/workqueue.c | 39 ++++++++- 7 files changed, 207 insertions(+), 84 deletions(-)
include/linux/sched.h +31 -6
@@ -87,6 +87,7 @@ struct sched_param {#include <linux/task_io_accounting.h> #include <linux/kobject.h> #include <linux/latencytop.h> +#include <linux/pi.h> #include <asm/processor.h>
@@ -1125,6 +1126,7 @@ struct task_struct {int prio, static_prio, normal_prio;#ifdef CONFIG_PREEMPT_RCU_BOOST int rcu_prio; +struct pi_source rcu_prio_src;#endif const struct sched_class *sched_class;struct sched_entity se;
@@ -1298,11 +1300,20 @@ struct task_struct {/* Protection of the PI data structures: */ raw_spinlock_t pi_lock; +struct { +struct pi_source src;/* represents normal_prio to 'this' */ +struct pi_node node; +struct pi_sink snk;/* registered to 'this' to get updates */ +int prio; + } pi; +#ifdef CONFIG_RT_MUTEXES /* PI waiters blocked on a rt_mutex held by this task */ struct plist_head pi_waiters;/* Deadlock detection and priority inheritance handling */ struct rt_mutex_waiter *pi_blocked_on; +int rtmutex_prio; +struct pi_source rtmutex_prio_src;#endif #ifdef CONFIG_DEBUG_MUTEXES
@@ -1440,6 +1451,26 @@ struct task_struct {#endif }; +static inline int +task_pi_boost (struct task_struct *p,struct pi_source *src, +unsigned int flags) +{ +return pi_boost (&p->pi.node, src, flags); +} + +static inline int +task_pi_deboost (struct task_struct *p,struct pi_source *src, +unsigned int flags) +{ +return pi_deboost (&p->pi.node, src, flags); +} + +static inline int +task_pi_update (struct task_struct *p,unsigned int flags) +{ +return pi_update (&p->pi.node, flags); +} +#ifdef CONFIG_PREEMPT_RT # define set_printk_might_sleep (x) do { current->in_printk = x; } while(0)#else
@@ -1774,14 +1805,8 @@ int sched_rt_handler(struct ctl_table *table, int write,extern unsigned int sysctl_sched_compat_yield; -extern void task_setprio (struct task_struct *p,int prio); -#ifdef CONFIG_RT_MUTEXES extern int rt_mutex_getprio (struct task_struct *p); -static inline void rt_mutex_setprio (struct task_struct *p,int prio) -{ -task_setprio (p, prio); -}extern void rt_mutex_adjust_pi (struct task_struct *p);#else static inline int rt_mutex_getprio (struct task_struct *p)
include/linux/workqueue.h +2 -0
@@ -11,6 +11,7 @@#include <linux/lockdep.h> #include <linux/plist.h> #include <linux/sched_prio.h> +#include <linux/pi.h> #include <asm/atomic.h> struct workqueue_struct ;
@@ -31,6 +32,7 @@ struct work_struct {#define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK)struct plist_node entry;work_func_t func; +struct pi_source pi_src;#ifdef CONFIG_LOCKDEP struct lockdep_map lockdep_map;#endif
kernel/fork.c +1 -0
@@ -990,6 +990,7 @@ static struct task_struct *copy_process(unsigned long clone_flags, p->rcu_flipctr_idx =0 ;#ifdef CONFIG_PREEMPT_RCU_BOOST p->rcu_prio =MAX_PRIO ; +pi_source_init (&p->rcu_prio_src, &p->rcu_prio); p->rcub_rbdp =NULL ; p->rcub_state =RCU_BOOST_IDLE ;INIT_LIST_HEAD (&p->rcub_entry);
kernel/rcupreempt-boost.c +4 -14
@@ -236,10 +236,8 @@ static void rcu_boost_task(struct task_struct *task)rcu_trace_boost_task_boost_called (RCU_BOOST_ME ); -if (task->rcu_prio < task->prio) { +if (task_pi_boost (task, &task->rcu_prio_src,0 ))rcu_trace_boost_task_boosted (RCU_BOOST_ME ); -task_setprio (task, task->rcu_prio); - } }/**
@@ -281,14 +279,8 @@ void __rcu_preempt_boost(void)rcu_trace_boost_try_boost (rbd); - prio =rt_mutex_getprio (curr); -if (list_empty (&curr->rcub_entry))list_add_tail (&curr->rcub_entry, &rbd->rbs_toboost); -if (prio <= rbd->rbs_prio) - goto out; - -rcu_trace_boost_boosted (curr->rcub_rbdp);set_rcu_prio (curr, rbd->rbs_prio);rcu_boost_task (curr);
@@ -353,11 +345,11 @@ void __rcu_preempt_unboost(void)rcu_trace_boost_unboosted (rbd); +task_pi_deboost (curr, &curr->rcu_prio_src,0 ); +set_rcu_prio (curr,MAX_PRIO );spin_lock (&curr->pi_lock); - prio =rt_mutex_getprio (curr); -task_setprio (curr, prio); curr->rcub_rbdp =NULL ;
@@ -393,9 +385,7 @@ static int __rcu_boost_readers(struct rcu_boost_dat *rbd, int prio, unsigned lonlist_move_tail (&p->rcub_entry, &rbd->rbs_boosted);set_rcu_prio (p, prio); -spin_lock (&p->pi_lock); -rcu_boost_task (p); -spin_unlock (&p->pi_lock); +task_pi_boost (p, &p->rcu_prio_src,0 );/* * Now we release the lock to allow for a higher
kernel/rtmutex.c +4 -2
@@ -178,8 +178,10 @@ static void __rt_mutex_adjust_prio(struct task_struct *task) {int prio =rt_mutex_getprio (task); -if (task->prio != prio) -rt_mutex_setprio (task, prio); +if (task->rtmutex_prio != prio) { + task->rtmutex_prio = prio; +task_pi_boost (task, &task->rtmutex_prio_src,0 ); + } }/*
kernel/sched.c +133 -55
305 lines not on this page · raw message
kernel/workqueue.c +32 -7
@@ -145,8 +145,13 @@ static void insert_work(struct cpu_workqueue_struct *cwq,plist_node_init (&work->entry, prio);plist_add (&work->entry, &cwq->worklist); -if (boost_prio < cwq->thread->prio) -task_setprio (cwq->thread, boost_prio); +/* +* FIXME: We want to boost to boost_prio, but we dont record that +* value in the work_struct for later deboosting +*/ +pi_source_init (&work->pi_src, &work->entry.prio); +task_pi_boost (cwq->thread, &work->pi_src,0 ); +wake_up (&cwq->more_work); }
@@ -280,6 +285,10 @@ struct wq_barrier {static void run_workqueue (struct cpu_workqueue_struct *cwq) {struct plist_head *worklist = &cwq->worklist; +struct pi_source pi_src; +int prio; + +pi_source_init (&pi_src, &prio);spin_lock_irq (&cwq->lock); cwq->run_depth++;
@@ -292,10 +301,10 @@ static void run_workqueue(struct cpu_workqueue_struct *cwq) again:while (!plist_head_empty (worklist)) { -int prio;struct work_struct *work =plist_first_entry (worklist, struct work_struct, entry);work_func_t f = work->func; +#ifdef CONFIG_LOCKDEP /* * It is permissible to free the struct work_struct
@@ -316,14 +325,28 @@ again: } prio =max (prio,0 ); -if (likely (cwq->thread->prio != prio)) -task_setprio (cwq->thread, prio); - cwq->current_work = work;plist_del (&work->entry, worklist);plist_node_init (&work->entry,MAX_PRIO );spin_unlock_irq (&cwq->lock); +/* +* The owner is free to reuse the work object once we execute +* the work->func() below. Therefore we cannot leave the +* work->pi_src boosting our thread or it may get stomped +* on when the work item is requeued. +* +* So what we do is we boost ourselves with an on-the +* stack copy of the priority of the work item, and then +* deboost the workitem. Once the work is complete, we +* can then simply deboost the stack version. +* +* Note that this will not typically cause a pi-chain +* update since we are boosting the node laterally +*/ +task_pi_boost (current, &pi_src,PI_FLAG_DEFER_UPDATE ); +task_pi_deboost (current, &work->pi_src,PI_FLAG_DEFER_UPDATE ); +BUG_ON (get_wq_data (work) != cwq);work_clear_pending (work);leak_check (NULL );
@@ -334,6 +357,9 @@ again:lock_release (&cwq->wq->lockdep_map,1 , _THIS_IP_);leak_check (f); +/* Deboost the stack copy of the work->prio (see above) */ +task_pi_deboost (current, &pi_src,0 ); +spin_lock_irq (&cwq->lock); cwq->current_work =NULL ;wake_up_all (&cwq->work_done);
@@ -357,7 +383,6 @@ again: goto again; } -task_setprio (cwq->thread, current->normal_prio); cwq->run_depth--;spin_unlock_irq (&cwq->lock); }
RFC PATCH 3/7 rtmutex: formally initialize the rt_mutex_waiters
In reply to Gregory HaskinsWe will be adding more logic to rt_mutex_waiters and therefore lets centralize the initialization to make this easier going forward.
---
kernel/rtmutex.c | 26 ++++++++++++++------------ 1 files changed, 14 insertions(+), 12 deletions(-)
kernel/rtmutex.c +14 -12
@@ -805,6 +805,15 @@ static int adaptive_wait(struct rt_mutex_waiter *waiter, }#endif +static void init_waiter (struct rt_mutex_waiter *waiter) +{ +memset (waiter,0 ,sizeof (*waiter)); + +debug_rt_mutex_init_waiter (waiter); + waiter->task =NULL ; + waiter->write_lock =0 ; +} +/* * Slow path lock function spin_lock style: this variant is very * careful not to miss any non-lock wakeups.
@@ -823,9 +832,7 @@ rt_spin_lock_slowlock(struct rt_mutex *lock)struct task_struct *orig_owner;int missed =0 ; -debug_rt_mutex_init_waiter (&waiter); - waiter.task =NULL ; - waiter.write_lock =0 ; +init_waiter (&waiter);spin_lock_irqsave (&lock->wait_lock, flags);init_lists (lock);
@@ -1324,6 +1331,8 @@ rt_read_slowlock(struct rw_mutex *rwm, int mtx)int saved_lock_depth =-1 ;unsigned long saved_state =-1 , state, flags; +init_waiter (&waiter); +spin_lock_irqsave (&mutex->wait_lock, flags);init_rw_lists (rwm);
@@ -1335,10 +1344,6 @@ rt_read_slowlock(struct rw_mutex *rwm, int mtx)/* Owner is a writer (or a blocked writer). Block on the lock */ -debug_rt_mutex_init_waiter (&waiter); - waiter.task =NULL ; - waiter.write_lock =0 ; -if (mtx) {/* * We drop the BKL here before we go into the wait loop to avoid a
@@ -1538,8 +1543,7 @@ rt_write_slowlock(struct rw_mutex *rwm, int mtx)int saved_lock_depth =-1 ;unsigned long flags, saved_state =-1 , state; -debug_rt_mutex_init_waiter (&waiter); - waiter.task =NULL ; +init_waiter (&waiter);/* we do PI different for writers that are blocked */ waiter.write_lock =1 ;
@@ -2270,9 +2274,7 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,struct rt_mutex_waiter waiter;unsigned long flags; -debug_rt_mutex_init_waiter (&waiter); - waiter.task =NULL ; - waiter.write_lock =0 ; +init_waiter (&waiter);spin_lock_irqsave (&lock->wait_lock, flags);init_lists (lock);
RFC PATCH 4/7 RT: wrap the rt_rwlock "add reader" logic
In reply to Gregory HaskinsWe will use this later in the series to add PI functions on "add".
---
kernel/rtmutex.c | 16 +++++++++++----- 1 files changed, 11 insertions(+), 5 deletions(-)
kernel/rtmutex.c +11 -5
@@ -1122,6 +1122,12 @@ static void rw_check_held(struct rw_mutex *rwm)# define rw_check_held (rwm) do { } while (0)#endif +static inline void +rt_rwlock_add_reader (struct reader_lock_struct *rls,struct rw_mutex *rwm) +{ +list_add (&rls->list, &rwm->readers); +} +/* * The fast path does not add itself to the reader list to keep * from needing to grab the spinlock. We need to add the owner
@@ -1163,7 +1169,7 @@ rt_rwlock_update_owner(struct rw_mutex *rwm, struct task_struct *own)if (rls->list.prev && !list_empty (&rls->list))return ; -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rls, rwm);/* change to reader, so no one else updates too */ rt_rwlock_set_owner (rwm,RT_RW_READER ,RT_RWLOCK_CHECK );
@@ -1197,7 +1203,7 @@ static int try_to_take_rw_read(struct rw_mutex *rwm, int mtx)* it hasn't been added to the link list yet. */ if (!rls->list.prev ||list_empty (&rls->list)) -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rls, rwm);rt_rwlock_set_owner (rwm,RT_RW_READER ,0 ); rls->count++; incr =0 ;
@@ -1276,7 +1282,7 @@ static int try_to_take_rw_read(struct rw_mutex *rwm, int mtx) rls->lock = rwm; rls->count =1 ;WARN_ON (rls->list.prev && !list_empty (&rls->list)); -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rls, rwm); }else WARN_ON_ONCE (1 );spin_unlock (¤t->pi_lock);
@@ -1473,7 +1479,7 @@ __rt_read_fasttrylock(struct rw_mutex *rwm)spin_lock (&mutex->wait_lock); rls = ¤t->owned_read_locks[reader_count];if (!rls->list.prev ||list_empty (&rls->list)) -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rlw, rwm);spin_unlock (&mutex->wait_lock); }else spin_unlock (¤t->pi_lock);
@@ -2083,7 +2089,7 @@ rt_mutex_downgrade_write(struct rw_mutex *rwm)/* Set us up for multiple readers or conflicts */ -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rls, rwm); rwm->owner =RT_RW_READER ;/*
RFC PATCH 5/7 rtmutex: use runtime init for rtmutexes
In reply to Gregory HaskinsThe system already has facilities to perform late/run-time init for rtmutexes. We want to add more advanced initialization later in the series so we force all rtmutexes through the init path in preparation for the later patches.
---
include/linux/rtmutex.h | 2 -- 1 files changed, 0 insertions(+), 2 deletions(-)
include/linux/rtmutex.h +0 -2
@@ -64,8 +64,6 @@ struct hrtimer_sleeper;#define __RT_MUTEX_INITIALIZER (mutexname) \ { .wait_lock = RAW_SPIN_LOCK_UNLOCKED(mutexname) \ - , .wait_list = PLIST_HEAD_INIT(mutexname.wait_list, &mutexname.wait_lock) \ - , .owner = NULL \ __DEBUG_RT_MUTEX_INITIALIZER(mutexname)}#define DEFINE_RT_MUTEX (mutexname) \
RFC PATCH 6/7 rtmutex: convert rtmutexes to fully use the PI library
In reply to Gregory HaskinsWe have previously only laid some of the groundwork to use the PI library, but left the existing infrastructure in place in the rtmutex code. This patch converts the rtmutex PI code to officially use the PI library.
---
include/linux/rt_lock.h | 2 include/linux/rtmutex.h | 15 - include/linux/sched.h | 21 - kernel/fork.c | 2 kernel/rcupreempt-boost.c | 2 kernel/rtmutex-debug.c | 4 kernel/rtmutex.c | 944 ++++++++++++++------------------------------- kernel/rtmutex_common.h | 18 - kernel/rwlock_torture.c | 32 -- kernel/sched.c | 12 - 10 files changed, 319 insertions(+), 733 deletions(-)
include/linux/rt_lock.h +2 -0
@@ -14,6 +14,7 @@#include <asm/atomic.h> #include <linux/spinlock_types.h> #include <linux/sched_prio.h> +#include <linux/pi.h> #ifdef CONFIG_PREEMPT_RT /*
@@ -67,6 +68,7 @@ struct rw_mutex {atomic_t count;/* number of times held for read */ atomic_t owners;/* number of owners as readers */ struct list_head readers; +struct pi_sink pi_snk;int prio; };
include/linux/rtmutex.h +7 -8
@@ -15,6 +15,7 @@#include <linux/linkage.h> #include <linux/plist.h> #include <linux/spinlock_types.h> +#include <linux/pi.h> /** * The rt_mutex structure
@@ -27,6 +28,12 @@ struct rt_mutex {raw_spinlock_t wait_lock;struct plist_head wait_list;struct task_struct *owner; +struct { +struct pi_source src; +struct pi_node node; +struct pi_sink snk; +int prio; + } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES int save_state;const char *name, *file;
@@ -96,12 +103,4 @@ extern int rt_mutex_trylock(struct rt_mutex *lock);extern void rt_mutex_unlock (struct rt_mutex *lock); -#ifdef CONFIG_RT_MUTEXES -# define INIT_RT_MUTEXES (tsk) \ - .pi_waiters = PLIST_HEAD_INIT(tsk.pi_waiters, &tsk.pi_lock), \ - INIT_RT_MUTEX_DEBUG(tsk) -#else -# define INIT_RT_MUTEXES (tsk) -#endif -#endif
include/linux/sched.h +1 -20
@@ -1106,6 +1106,7 @@ struct reader_lock_struct {struct rw_mutex *lock;struct list_head list;struct task_struct *task; +struct pi_source pi_src;int count; };
@@ -1307,15 +1308,6 @@ struct task_struct {int prio; } pi; -#ifdef CONFIG_RT_MUTEXES -/* PI waiters blocked on a rt_mutex held by this task */ -struct plist_head pi_waiters; -/* Deadlock detection and priority inheritance handling */ -struct rt_mutex_waiter *pi_blocked_on; -int rtmutex_prio; -struct pi_source rtmutex_prio_src; -#endif -#ifdef CONFIG_DEBUG_MUTEXES /* mutex deadlock detection */ struct mutex_waiter *blocked_on;
@@ -1805,17 +1797,6 @@ int sched_rt_handler(struct ctl_table *table, int write,extern unsigned int sysctl_sched_compat_yield; -#ifdef CONFIG_RT_MUTEXES -extern int rt_mutex_getprio (struct task_struct *p); -extern void rt_mutex_adjust_pi (struct task_struct *p); -#else -static inline int rt_mutex_getprio (struct task_struct *p) -{ -return p->normal_prio; -} -# define rt_mutex_adjust_pi (p) do { } while (0) -#endif -extern void set_user_nice (struct task_struct *p,long nice);extern int task_prio (const struct task_struct *p);extern int task_nice (const struct task_struct *p);
kernel/fork.c +0 -2
@@ -887,8 +887,6 @@ static void rt_mutex_init_task(struct task_struct *p) {spin_lock_init (&p->pi_lock);#ifdef CONFIG_RT_MUTEXES -plist_head_init (&p->pi_waiters, &p->pi_lock); - p->pi_blocked_on =NULL ;# ifdef CONFIG_DEBUG_RT_MUTEXES p->last_kernel_lock =NULL ;# endif
kernel/rcupreempt-boost.c +1 -1
@@ -431,7 +431,7 @@ void rcu_boost_readers(void)spin_lock_irqsave (&rcu_boost_wake_lock, flags); - prio =rt_mutex_getprio (curr); + prio =get_rcu_prio (curr);rcu_trace_boost_try_boost_readers (RCU_BOOST_ME );
kernel/rtmutex-debug.c +0 -4
@@ -57,8 +57,6 @@ static void printk_lock(struct rt_mutex *lock, int print_owner)void rt_mutex_debug_task_free (struct task_struct *task) { -DEBUG_LOCKS_WARN_ON (!plist_head_empty (&task->pi_waiters)); -DEBUG_LOCKS_WARN_ON (task->pi_blocked_on);#ifdef CONFIG_PREEMPT_RT WARN_ON (task->reader_lock_count);#endif
@@ -156,7 +154,6 @@ void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter) {memset (waiter,0x11 ,sizeof (*waiter));plist_node_init (&waiter->list_entry,MAX_PRIO ); -plist_node_init (&waiter->pi_list_entry,MAX_PRIO ); waiter->deadlock_task_pid =NULL ; }
@@ -164,7 +161,6 @@ void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter) {put_pid (waiter->deadlock_task_pid);DEBUG_LOCKS_WARN_ON (!plist_node_empty (&waiter->list_entry)); -DEBUG_LOCKS_WARN_ON (!plist_node_empty (&waiter->pi_list_entry));DEBUG_LOCKS_WARN_ON (waiter->task);memset (waiter,0x22 ,sizeof (*waiter)); }
kernel/rtmutex.c +302 -642
1,280 lines not on this page · raw message
kernel/rtmutex_common.h +5 -13
@@ -14,6 +14,7 @@#include <linux/rtmutex.h> #include <linux/rt_lock.h> +#include <linux/pi.h> /* * The rtmutex in kernel tester is independent of rtmutex debugging. We
@@ -48,10 +49,13 @@ extern void schedule_rt_mutex_test(struct rt_mutex *lock);*/ struct rt_mutex_waiter {struct plist_node list_entry; -struct plist_node pi_list_entry;struct task_struct *task;struct rt_mutex *lock;int write_lock; +struct { +struct pi_sink snk; +int prio; + } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES unsigned long ip;struct pid *deadlock_task_pid;
@@ -79,18 +83,6 @@ rt_mutex_top_waiter(struct rt_mutex *lock)return w; } -static inline int task_has_pi_waiters (struct task_struct *p) -{ -return !plist_head_empty (&p->pi_waiters); -} - -static inline struct rt_mutex_waiter * -task_top_pi_waiter (struct task_struct *p) -{ - return plist_first_entry(&p->pi_waiters, struct rt_mutex_waiter, - pi_list_entry); -} -/* * lock->owner state tracking: */
kernel/rwlock_torture.c +1 -31
@@ -682,37 +682,7 @@ static int __init mutex_stress_init(void)print_owned_read_locks (tsks[i]); -if (tsks[i]->pi_blocked_on) { - w = (void *)tsks[i]->pi_blocked_on; - mtx = w->lock; -spin_unlock_irq (&tsks[i]->pi_lock); -spin_lock_irq (&mtx->wait_lock); -spin_lock (&tsks[i]->pi_lock); - own = (unsigned long )mtx->owner & ~3UL ; - oops_in_progress++; -printk ("%s:%d is blocked on " , - tsks[i]->comm, tsks[i]->pid); -__print_symbol ("%s" , (unsigned long )mtx); -if (own ==0x100 ) -printk (" owner is READER\n" ); -else if (!(own & ~300 )) -printk (" owner is ILLEGAL!!\n" ); -else if (!own) -printk (" has no owner!\n" ); -else { -struct task_struct *owner = (void *)own; - -printk (" owner is %s:%d\n" , - owner->comm, owner->pid); - } - oops_in_progress--; - -spin_unlock (&tsks[i]->pi_lock); -spin_unlock_irq (&mtx->wait_lock); - }else { -print_owned_read_locks (tsks[i]); -spin_unlock_irq (&tsks[i]->pi_lock); - } + spin_unlock_irq(&tsks[i]->pi_lock); } }#endif
kernel/sched.c +0 -11
@@ -2390,12 +2390,6 @@ task_pi_init(struct task_struct *p)pi_source_init (&p->pi.src, &p->normal_prio);task_pi_boost (p, &p->pi.src,PI_FLAG_DEFER_UPDATE ); -#ifdef CONFIG_RT_MUTEXES - p->rtmutex_prio =MAX_PRIO ; -pi_source_init (&p->rtmutex_prio_src, &p->rtmutex_prio); -task_pi_boost (p, &p->rtmutex_prio_src,PI_FLAG_DEFER_UPDATE ); -#endif -/* * We add our own task as a dependency of ourselves so that * we get boost-notifications (via task_pi_boost_cb) whenever
@@ -5006,7 +5000,6 @@ task_pi_update_cb(struct pi_sink *snk, unsigned int flags)*/ if (unlikely (p == rq->idle)) {WARN_ON (p != rq->curr); -WARN_ON (p->pi_blocked_on); goto out_unlock; }
@@ -5337,7 +5330,6 @@ recheck:spin_unlock_irqrestore (&p->pi_lock, flags);task_pi_update (p,0 ); -rt_mutex_adjust_pi (p);return 0 ; }
@@ -8471,10 +8463,6 @@ void __init sched_init(void)task_pi_init (&init_task); -#ifdef CONFIG_RT_MUTEXES -plist_head_init (&init_task.pi_waiters, &init_task.pi_lock); -#endif
RFC PATCH 7/7 rtmutex: pi-boost locks as late as possible
In reply to Gregory HaskinsAdaptive-locking technology often times acquires the lock by spinning on a running-owner instead of sleeping. It is unecessary to go through pi-boosting if the owner is of equal or (logically) lower priority. Therefore, we can save some significant overhead by deferring the boost until absolutely necessary. This has shown to improve overall performance in PREEMPT_RT
Special thanks to Peter Morreale for suggesting the optimization to only consider skipping the boost if the owner is >= to current
---
include/linux/rtmutex.h | 1 kernel/rtmutex.c | 195 ++++++++++++++++++++++++++++++++++++----------- kernel/rtmutex_common.h | 1 3 files changed, 153 insertions(+), 44 deletions(-)
include/linux/rtmutex.h +1 -0
@@ -33,6 +33,7 @@ struct rt_mutex {struct pi_node node;struct pi_sink snk;int prio; +int boosters; } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES int save_state;
kernel/rtmutex.c +151 -44
325 lines not on this page · raw message
kernel/rtmutex_common.h +1 -0
@@ -55,6 +55,7 @@ struct rt_mutex_waiter {struct {struct pi_sink snk;int prio; +int boosted; } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES unsigned long ip;
Re: [PATCH RT RFC 7/7] rtmutex: pi-boost locks as late as possible
Quoted 7 lines · Gregory Haskins
Adaptive-locking technology often times acquires the lock by spinning on a running-owner instead of sleeping. It is unecessary to go through pi-boosting if the owner is of equal or (logically) lower priority. Therefore, we can save some significant overhead by deferring the boost until absolutely necessary. This has shown to improve overall performance in
PREEMPT_RT
Quoted 6 lines
Special thanks to Peter Morreale for suggesting the optimization to only consider skipping the boost if the owner is >= to current
Signed-off-by Gregory Haskins <[email protected]>CC Peter Morreale <[email protected]>
I received feedback that this prologue was too vague to accurately describe what this patch does and why it is not broken to use this optimization. Therefore, here is the new prologue:
-----------------------
From: Gregory Haskins <[email protected]>
rtmutex: pi-boost locks as late as possible
PREEMPT_RT replaces most spinlock_t instances with a preemptible real-time lock that supports priority inheritance. An uncontended (fastpath) acquisition of this lock has no more overhead than its non-rt spinlock_t counterpart. However, the contended case has considerably more overhead so that the lock can maintain proper priority queue order and support pi-boosting of the lock owner, yet remaining fully preemptible.
Instrumentation shows that the majority of acquisitions under most workloads falls either into the fastpath category, or the adaptive spin category within the slowpath. The necessity to pi-boost a lock-owner should be sufficiently rare, yet the slow-path path blindly incurs this overhead in 100% of contentions.
Therefore, this patch intends to capitalize on this observation in order to reduce overhead and improve acquisition throughput. It is important to note that real-time latency is still treated as a higher order constraint than throughput, so the full pi-protocol is observed using new carefully constructed rules around the old concepts.
1) We check the priority of the owner relative to the waiter on each spin of the lock (if we are not boosted already). If the owner's effective priority is logically less than the waiters priority, we must boost them.
2) We check the priority of ourselves against our current queue position on the waiters-list (if we are not boosted already). If our priority was changed, we need to re-queue ourselves to update our position.
3) We break out of the adaptive-spin if either of the above conditions (1), (2) change so that we can re-evaluate the lock conditions.
4) We must enter pi-boost mode if, at any time, we decide to voluntarily preempt since we are losing our ability to dynamically process the conditions above.
Note: We still fully support priority inheritance with this protocol, even if we defer the low-level calls to adjust priority. The difference is really in terms of being a pro-active protocol (boost on entry), verses a reactive protocol (boost when necessary). The upside to the latter is that we don't take a penalty for pi when it is not necessary. The downside is that we technically leave the owner exposed to getting preempted, even if our waiter is the highest priority task in the system. When this happens, the owner would be immediately boosted (because we would hit the "oncpu" condition, and subsequently follow the voluntary preempt path which boosts the owner). Therefore, inversion is fully prevented, but we have the extra latency of the preempt/boost/wakeup that could have been avoided in the proactive model.
However, the design of the algorithm described above constrains the probability of this phenomenon occurring to setscheduler() operations. Since rt-locks do not support being interrupted by signals or timeouts, waiters only depart via the acquisition path. And while acquisitions do deboost the owner, the owner also changes simultaneously, rending the deboost moot relative to the other waiters.
What this all means is that the downside to this implementation is that a high-priority waiter *may* see an extra latency (equivalent to roughly two wake-ups) if the owner has its priority reduced via setscheduler() while it holds the lock. The penalty is deterministic, arguably small enough, and sufficiently rare that I do not believe it should be an issue.
Note: If the concept of other exit paths are ever introduced in the future, simply adapting the condition to look at owner->normal_prio instead of owner->prio should once again constrain the limitation to setscheduler().
Special thanks to Peter Morreale for suggesting the optimization to only consider skipping the boost if the owner is >= to current.
Quoted 382 lines
---
include/linux/rtmutex.h | 1 kernel/rtmutex.c | 195 ++++++++++++++++++++++++++++++++++++----------- kernel/rtmutex_common.h | 1 3 files changed, 153 insertions(+), 44 deletions(-)include/linux/rtmutex.h +1
-0@@ -33,6 +33,7 @@ struct rt_mutex {struct pi_node node;struct pi_sink snk;int prio; +int boosters; } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES int save_state;kernel/rtmutex.c +151
-44@@ -76,14 +76,15 @@ rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner, {unsigned long val = (unsigned long )owner | mask; -if (rt_mutex_has_waiters (lock)) { +if (lock->pi.boosters) {struct task_struct *prev_owner =rt_mutex_owner (lock);rtmutex_pi_owner (lock, prev_owner,0 );rtmutex_pi_owner (lock, owner,1 ); + } +if (rt_mutex_has_waiters (lock)) val |=RT_MUTEX_HAS_WAITERS ; - } lock->owner = (struct task_struct *)val; }@@ -177,7 +178,7 @@ static inline int rtmutex_pi_update(struct pi_sink *snk,spin_lock_irqsave (&lock->wait_lock, iflags); -if (rt_mutex_has_waiters (lock)) { +if (lock->pi.boosters) { owner =rt_mutex_owner (lock);if (owner && owner !=RT_RW_READER ) {@@ -206,6 +207,7 @@ static void init_pi(struct rt_mutex *lock)pi_node_init (&lock->pi.node); lock->pi.prio =MAX_PRIO ; + lock->pi.boosters =0 ;pi_source_init (&lock->pi.src, &lock->pi.prio); lock->pi.snk = rtmutex_pi_snk;@@ -303,6 +305,16 @@ static inline int try_to_take_rt_mutex(struct rt_mutex *lock)return do_try_to_take_rt_mutex (lock,STEAL_NORMAL ); } +static inline void requeue_waiter (struct rt_mutex *lock, +struct rt_mutex_waiter *waiter) +{ +BUG_ON (!waiter->task); + +plist_del (&waiter->list_entry, &lock->wait_list); +plist_node_init (&waiter->list_entry, waiter->pi.prio); +plist_add (&waiter->list_entry, &lock->wait_list); +} +/* * These callbacks are invoked whenever a waiter has changed priority. * So we should requeue it within the lock->wait_list @@ -343,11 +355,8 @@ static inline int rtmutex_waiter_pi_update(struct pi_sink *snk,* pi list. Therefore, if waiter->pi.prio has changed since we * queued ourselves, requeue it. */ -if (waiter->task && waiter->list_entry.prio != waiter->pi.prio) { -plist_del (&waiter->list_entry, &lock->wait_list); -plist_node_init (&waiter->list_entry, waiter->pi.prio); -plist_add (&waiter->list_entry, &lock->wait_list); - } +if (waiter->task && waiter->list_entry.prio != waiter->pi.prio) +requeue_waiter (lock, waiter);spin_unlock_irqrestore (&lock->wait_lock, iflags);@@ -359,20 +368,9 @@ static struct pi_sink rtmutex_waiter_pi_snk = { .update = rtmutex_waiter_pi_update, }; -/* -* This must be called with lock->wait_lock held. -*/ -static int add_waiter (struct rt_mutex *lock, -struct rt_mutex_waiter *waiter, -unsigned long *flags) +static void boost_lock (struct rt_mutex *lock, +struct rt_mutex_waiter *waiter) { -int has_waiters =rt_mutex_has_waiters (lock); - - waiter->task = current; - waiter->lock = lock; - waiter->pi.prio = current->prio; -plist_node_init (&waiter->list_entry, waiter->pi.prio); -plist_add (&waiter->list_entry, &lock->wait_list); waiter->pi.snk = rtmutex_waiter_pi_snk;/* @@ -397,35 +395,28 @@ static int add_waiter(struct rt_mutex *lock,* If we previously had no waiters, we are transitioning to * a mode where we need to boost the owner */ -if (!has_waiters) { +if (!lock->pi.boosters) {struct task_struct *owner =rt_mutex_owner (lock);rtmutex_pi_owner (lock, owner,1 ); } -spin_unlock_irqrestore (&lock->wait_lock, *flags); -task_pi_update (current,0 ); -spin_lock_irqsave (&lock->wait_lock, *flags); - -return 0 ; + lock->pi.boosters++; + waiter->pi.boosted =1 ; } -/* -* Remove a waiter from a lock -* -* Must be called with lock->wait_lock held -*/ -static void remove_waiter (struct rt_mutex *lock, -struct rt_mutex_waiter *waiter) +static void deboost_lock (struct rt_mutex *lock, +struct rt_mutex_waiter *waiter, +struct task_struct *p) { -struct task_struct *p = waiter->task; +BUG_ON (!waiter->pi.boosted); -plist_del (&waiter->list_entry, &lock->wait_list); - waiter->task =NULL ; + waiter->pi.boosted =0 ; + lock->pi.boosters--;/* * We can stop boosting the owner if there are no more waiters */ -if (!rt_mutex_has_waiters (lock)) { +if (!lock->pi.boosters) {struct task_struct *owner =rt_mutex_owner (lock);rtmutex_pi_owner (lock, owner,0 ); }@@ -446,6 +437,51 @@ static void remove_waiter(struct rt_mutex *lock, }/* +* This must be called with lock->wait_lock held. +*/ +static void _add_waiter (struct rt_mutex *lock, +struct rt_mutex_waiter *waiter) +{ + waiter->task = current; + waiter->lock = lock; + waiter->pi.prio = current->prio; +plist_node_init (&waiter->list_entry, waiter->pi.prio); +plist_add (&waiter->list_entry, &lock->wait_list); +} + +static int add_waiter (struct rt_mutex *lock, +struct rt_mutex_waiter *waiter, +unsigned long *flags) +{ +_add_waiter (lock, waiter); + +boost_lock (lock, waiter); + +spin_unlock_irqrestore (&lock->wait_lock, *flags); +task_pi_update (current,0 ); +spin_lock_irqsave (&lock->wait_lock, *flags); + +return 0 ; +} + +/* +* Remove a waiter from a lock +* +* Must be called with lock->wait_lock held +*/ +static void remove_waiter (struct rt_mutex *lock, +struct rt_mutex_waiter *waiter) +{ +struct task_struct *p = waiter->task; + +plist_del (&waiter->list_entry, &lock->wait_list); + waiter->task =NULL ; + +if (waiter->pi.boosted) +deboost_lock (lock, waiter, p); +} + +/* * Wake up the next waiter on the lock. * * Remove the top waiter from the current tasks waiter list and from @@ -558,6 +594,24 @@ static int adaptive_wait(struct rt_mutex_waiter *waiter,if (orig_owner !=rt_mutex_owner (waiter->lock))return 0 ; +/* Special handling for when we are not in pi-boost mode */ +if (!waiter->pi.boosted) { +/* +* Are we higher priority than the owner? If so +* we should bail out immediately so that we can +* pi boost them. +*/ +if (current->prio < orig_owner->prio) +return 0 ; + +/* +* Did our priority change? If so, we need to +* requeue our position in the list +*/ +if (waiter->pi.prio != current->prio) +return 0 ; + } +/* Owner went to bed, so should we */ if (!task_is_current (orig_owner))return 1 ;@@ -599,6 +653,7 @@ rt_spin_lock_slowlock(struct rt_mutex *lock)unsigned long saved_state, state, flags;struct task_struct *orig_owner;int missed =0 ; +int boosted =0 ;init_waiter (&waiter);@@ -631,26 +686,54 @@ rt_spin_lock_slowlock(struct rt_mutex *lock) } missed =1 ; + orig_owner =rt_mutex_owner (lock); +/* * waiter.task is NULL the first time we come here and * when we have been woken up by the previous owner * but the lock got stolen by an higher prio task. */ -if (!waiter.task) { -add_waiter (lock, &waiter, &flags); +if (!waiter.task) +_add_waiter (lock, &waiter); + +/* +* We only need to pi-boost the owner if they are lower +* priority than us. We dont care if this is racy +* against priority changes as we will break out of +* the adaptive spin anytime any priority changes occur +* without boosting enabled. +*/ +if (!waiter.pi.boosted && current->prio < orig_owner->prio) { +boost_lock (lock, &waiter); + boosted =1 ; + +spin_unlock_irqrestore (&lock->wait_lock, flags); +task_pi_update (current,0 ); +spin_lock_irqsave (&lock->wait_lock, flags); +/* Wakeup during boost ? */ if (unlikely (!waiter.task))continue ; }/* +* If we are not currently pi-boosting the lock, we have to +* monitor whether our priority changed since the last +* time it was recorded and requeue ourselves if it moves. +*/ +if (!waiter.pi.boosted && waiter.pi.prio != current->prio) { + waiter.pi.prio = current->prio; + +requeue_waiter (lock, &waiter); + } + +/* * Prevent schedule() to drop BKL, while waiting for * the lock ! We restore lock_depth when we come back. */ saved_flags = current->flags &PF_NOSCHED ; current->lock_depth =-1 ; current->flags &= ~PF_NOSCHED ; - orig_owner =rt_mutex_owner (lock);get_task_struct (orig_owner);spin_unlock_irqrestore (&lock->wait_lock, flags);@@ -664,6 +747,24 @@ rt_spin_lock_slowlock(struct rt_mutex *lock)* barrier which we rely upon to ensure current->state * is visible before we test waiter.task. */ +if (waiter.task && !waiter.pi.boosted) { +spin_lock_irqsave (&lock->wait_lock, flags); + +/* +* We get here if we have not yet boosted +* the lock, yet we are going to sleep. If +* we are still pending (waiter.task != 0), +* then go ahead and boost them now +*/ +if (waiter.task) { +boost_lock (lock, &waiter); + boosted =1 ; + } + +spin_unlock_irqrestore (&lock->wait_lock, flags); +task_pi_update (current,0 ); + } +if (waiter.task)schedule_rt_mutex (lock); }else @@ -696,7 +797,8 @@ rt_spin_lock_slowlock(struct rt_mutex *lock)spin_unlock_irqrestore (&lock->wait_lock, flags);/* Undo any pi boosting, if necessary */ -task_pi_update (current,0 ); +if (boosted) +task_pi_update (current,0 );debug_rt_mutex_free_waiter (&waiter); }@@ -708,6 +810,7 @@ static void noinline __sched rt_spin_lock_slowunlock(struct rt_mutex *lock) {unsigned long flags; +int deboost =0 ;spin_lock_irqsave (&lock->wait_lock, flags);@@ -721,12 +824,16 @@ rt_spin_lock_slowunlock(struct rt_mutex *lock)return ; } +if (lock->pi.boosters) + deboost =1 ; +wakeup_next_waiter (lock,1 );spin_unlock_irqrestore (&lock->wait_lock, flags); -/* Undo pi boosting when necessary */ -task_pi_update (current,0 ); +if (deboost) +/* Undo pi boosting when necessary */ +task_pi_update (current,0 ); } void __lockfunc rt_spin_lock(spinlock_t *lock)kernel/rtmutex_common.h +1
-1@@ -55,6 +55,7 @@ struct rt_mutex_waiter {struct {struct pi_sink snk;int prio; +int boosted; } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES unsigned long ip; --To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH RT RFC 7/7] rtmutex: pi-boost locks as late as possible
Quoted 61 lines · Gregory Haskins
Quoted 7 lines · Gregory Haskins
Adaptive-locking technology often times acquires the lock by spinning on a running-owner instead of sleeping. It is unecessary to go through pi-boosting if the owner is of equal or (logically) lower priority. Therefore, we can save some significant overhead by deferring the boost until absolutely necessary. This has shown to improve overall performance in
PREEMPT_RTQuoted 6 lines
Special thanks to Peter Morreale for suggesting the optimization to only consider skipping the boost if the owner is >= to current
Signed-off-by Gregory Haskins <[email protected]>CC Peter Morreale <[email protected]>I received feedback that this prologue was too vague to accurately describe what this patch does and why it is not broken to use this optimization. Therefore, here is the new prologue:
-----------------------
From: Gregory Haskins <[email protected]>rtmutex: pi-boost locks as late as possible
PREEMPT_RTreplaces mostspinlock_tinstances with a preemptible real-time lock that supports priority inheritance. An uncontended (fastpath) acquisition of this lock has no more overhead than its non-rtspinlock_tcounterpart. However, the contended case has considerably more overhead so that the lock can maintain proper priority queue order and support pi-boosting of the lock owner, yet remaining fully preemptible.Instrumentation shows that the majority of acquisitions under most workloads falls either into the fastpath category, or the adaptive spin category within the slowpath. The necessity to pi-boost a lock-owner should be sufficiently rare, yet the slow-path path blindly incurs this overhead in 100% of contentions.
Therefore, this patch intends to capitalize on this observation in order to reduce overhead and improve acquisition throughput. It is important to note that real-time latency is still treated as a higher order constraint than throughput, so the full pi-protocol is observed using new carefully constructed rules around the old concepts.
1) We check the priority of the owner relative to the waiter on each spin of the lock (if we are not boosted already). If the owner's effective priority is logically less than the waiters priority, we must boost them.
2) We check the priority of ourselves against our current queue position on the waiters-list (if we are not boosted already). If our priority was changed, we need to re-queue ourselves to update our position.
3) We break out of the adaptive-spin if either of the above conditions (1), (2) change so that we can re-evaluate the lock conditions.
4) We must enter pi-boost mode if, at any time, we decide to voluntarily preempt since we are losing our ability to dynamically process the conditions above.
Quoted 8 lines
Note: We still fully support priority inheritance with this protocol, even if we defer the low-level calls to adjust priority. The difference is really in terms of being a pro-active protocol (boost on entry), verses a reactive protocol (boost when necessary). The upside to the latter is that we don't take a penalty for pi when it is not necessary. The downside is that we technically leave the owner exposed to getting preempted, even if our waiter is the highest priority task in the system.
David Holmes (CC'd) pointed out that this statement is a little vague and confusing as well. The question is: how could the owner be exposed to preemption since it would presumably be running at or above the waiters priority or we would have boosted it already? The answer is that this is in reference to the fact that the owner may have its priority lowered after we have already made the decision to defer boosting.
Therefore, my updated statement should read:
"The downside is that we technically leave the owner exposed to getting preempted *should it get asynchronously deprioritized*, even if ...." As I go on to explain below, this deboosting could only happen as the result of a setscheduler() call, which I assert should not be cause for concern. However, I wanted to highlight this phenomenon in the interest of full disclosure since it is technically a difference in behavior from the original algorithm. I will update the header with this edit for clarity.
Thanks for the review, David!
-Greg
Quoted 424 lines
When this
happens, the owner would be immediately boosted (because we would hit the "oncpu" condition, and subsequently follow the voluntary preempt path which boosts the owner). Therefore, inversion is fully prevented, but we have the extra latency of the preempt/boost/wakeup that could have been avoided in the proactive model.However, the design of the algorithm described above constrains the probability of this phenomenon occurring to
setscheduler()operations. Since rt-locks do not support being interrupted by signals or timeouts, waiters only depart via the acquisition path. And while acquisitions do deboost the owner, the owner also changes simultaneously, rending the deboost moot relative to the other waiters.What this all means is that the downside to this implementation is that a high-priority waiter *may* see an extra latency (equivalent to roughly two wake-ups) if the owner has its priority reduced via
setscheduler()while it holds the lock. The penalty is deterministic, arguably small enough, and sufficiently rare that I do not believe it should be an issue.Note: If the concept of other exit paths are ever introduced in the future, simply adapting the condition to look at
owner->normal_prioinstead ofowner->prioshould once again constrain the limitation tosetscheduler().Special thanks to Peter Morreale for suggesting the optimization to only consider skipping the boost if the owner is >= to current.
Signed-off-by Gregory Haskins <[email protected]>CC Peter Morreale <[email protected]>Quoted 387 lines
---
include/linux/rtmutex.h | 1 kernel/rtmutex.c | 195++++++++++++++++++++++++++++++++++++-----------
kernel/rtmutex_common.h| 1 3 files changed, 153 insertions(+),
44 deletions(-)include/linux/rtmutex.h +1
-0@@ -33,6 +33,7 @@ struct rt_mutex {struct pi_node node;struct pi_sink snk;int prio; +int boosters; } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES int save_state;kernel/rtmutex.c +3
-2@@ -76,14 +76,15 @@ rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner, {unsigned long val = (unsigned long )owner | mask; -if (rt_mutex_has_waiters (lock)) { +if (lock->pi.boosters) {struct task_struct *prev_owner =rt_mutex_owner (lock);rtmutex_pi_owner (lock, prev_owner,0 );rtmutex_pi_owner (lock, owner,1 ); + } +if (rt_mutex_has_waiters (lock)) val |=RT_MUTEX_HAS_WAITERS ; - } lock->owner = (struct task_struct *)val;}
kernel/rtmutex.c +1
-1@@ -177,7 +178,7 @@ static inline int rtmutex_pi_update(struct pi_sink *snk,spin_lock_irqsave (&lock->wait_lock, iflags); -if (rt_mutex_has_waiters (lock)) { +if (lock->pi.boosters) { owner =rt_mutex_owner (lock);if (owner && owner != RT_RW_READER) {kernel/rtmutex.c +11
-0@@ -206,6 +207,7 @@ static void init_pi(struct rt_mutex *lock)pi_node_init (&lock->pi.node); lock->pi.prio =MAX_PRIO ; + lock->pi.boosters =0 ;pi_source_init (&lock->pi.src, &lock->pi.prio); lock->pi.snk = rtmutex_pi_snk;@@ -303,6 +305,16 @@ static inline int try_to_take_rt_mutex(struct rt_mutex *lock)return do_try_to_take_rt_mutex (lock,STEAL_NORMAL ); } +static inline void requeue_waiter (struct rt_mutex *lock, +struct rt_mutex_waiter *waiter) +{ +BUG_ON (!waiter->task); + +plist_del (&waiter->list_entry, &lock->wait_list); +plist_node_init (&waiter->list_entry, waiter->pi.prio); +plist_add (&waiter->list_entry, &lock->wait_list); +} +/* * These callbacks are invoked whenever a waiter has changed priority. * So we should requeue it within the
lock->wait_listkernel/rtmutex.c +77
-35@@ -343,11 +355,8 @@ static inline intrtmutex_waiter_pi_update(struct pi_sink *snk, * pi list. Therefore, if waiter->pi.prio has changed since we * queued ourselves, requeue it. */ -if (waiter->task && waiter->list_entry.prio != waiter->pi.prio) { -plist_del (&waiter->list_entry, &lock->wait_list); -plist_node_init (&waiter->list_entry, waiter->pi.prio); -plist_add (&waiter->list_entry, &lock->wait_list); - } +if (waiter->task && waiter->list_entry.prio != waiter->pi.prio) +requeue_waiter (lock, waiter);spin_unlock_irqrestore (&lock->wait_lock, iflags);@@ -359,20 +368,9 @@ static struct pi_sink rtmutex_waiter_pi_snk = { .update = rtmutex_waiter_pi_update, }; -/* -* This must be called with lock->wait_lock held. -*/ -static int add_waiter (struct rt_mutex *lock, -struct rt_mutex_waiter *waiter, -unsigned long *flags) +static void boost_lock (struct rt_mutex *lock, +struct rt_mutex_waiter *waiter) { -int has_waiters =rt_mutex_has_waiters (lock); - - waiter->task = current; - waiter->lock = lock; - waiter->pi.prio = current->prio; -plist_node_init (&waiter->list_entry, waiter->pi.prio); -plist_add (&waiter->list_entry, &lock->wait_list); waiter->pi.snk = rtmutex_waiter_pi_snk;/* @@ -397,35 +395,28 @@ static int add_waiter(struct rt_mutex *lock,* If we previously had no waiters, we are transitioning to * a mode where we need to boost the owner */ -if (!has_waiters) { +if (!lock->pi.boosters) {struct task_struct *owner =rt_mutex_owner (lock);rtmutex_pi_owner (lock, owner,1 ); } -spin_unlock_irqrestore (&lock->wait_lock, *flags); -task_pi_update (current,0 ); -spin_lock_irqsave (&lock->wait_lock, *flags); - -return 0 ; + lock->pi.boosters++; + waiter->pi.boosted =1 ; } -/* -* Remove a waiter from a lock -* -* Must be called with lock->wait_lock held -*/ -static void remove_waiter (struct rt_mutex *lock, -struct rt_mutex_waiter *waiter) +static void deboost_lock (struct rt_mutex *lock, +struct rt_mutex_waiter *waiter, +struct task_struct *p) { -struct task_struct *p = waiter->task; +BUG_ON (!waiter->pi.boosted); -plist_del (&waiter->list_entry, &lock->wait_list); - waiter->task =NULL ; + waiter->pi.boosted =0 ; + lock->pi.boosters--;/* * We can stop boosting the owner if there are no more waiters */ -if (!rt_mutex_has_waiters (lock)) { +if (!lock->pi.boosters) {struct task_struct *owner =rt_mutex_owner (lock);rtmutex_pi_owner (lock, owner,0 ); }@@ -446,6 +437,51 @@ static void remove_waiter(struct rt_mutex *lock, }/* +* This must be called with lock->wait_lock held. +*/ +static void _add_waiter (struct rt_mutex *lock, +struct rt_mutex_waiter *waiter) +{ + waiter->task = current; + waiter->lock = lock; + waiter->pi.prio = current->prio; +plist_node_init (&waiter->list_entry, waiter->pi.prio); +plist_add (&waiter->list_entry, &lock->wait_list); +} + +static int add_waiter (struct rt_mutex *lock, +struct rt_mutex_waiter *waiter, +unsigned long *flags) +{ +_add_waiter (lock, waiter); + +boost_lock (lock, waiter); + +spin_unlock_irqrestore (&lock->wait_lock, *flags); +task_pi_update (current,0 ); +spin_lock_irqsave (&lock->wait_lock, *flags); + +return 0 ; +} + +/* +* Remove a waiter from a lock +* +* Must be called with lock->wait_lock held +*/ +static void remove_waiter (struct rt_mutex *lock, +struct rt_mutex_waiter *waiter) +{ +struct task_struct *p = waiter->task; + +plist_del (&waiter->list_entry, &lock->wait_list); + waiter->task =NULL ; + +if (waiter->pi.boosted) +deboost_lock (lock, waiter, p); +} + +/* * Wake up the next waiter on the lock. * * Remove the top waiter from the current tasks waiter list and from @@ -558,6 +594,24 @@ static int adaptive_wait(struct rt_mutex_waiter *waiter, if (orig_owner != rt_mutex_owner(waiter->lock))return 0 ; +/* Special handling for when we are not in pi-boost mode */ +if (!waiter->pi.boosted) { +/* +* Are we higher priority than the owner? If so +* we should bail out immediately so that we can +* pi boost them. +*/ +if (current->prio < orig_owner->prio) +return 0 ; + +/* +* Did our priority change? If so, we need to +* requeue our position in the list +*/ +if (waiter->pi.prio != current->prio) +return 0 ; + } +/* Owner went to bed, so should we */ if (!task_is_current (orig_owner))return 1;kernel/rtmutex.c +59
-6@@ -599,6 +653,7 @@ rt_spin_lock_slowlock(struct rt_mutex *lock)unsigned long saved_state, state, flags;struct task_struct *orig_owner;int missed =0 ; +int boosted =0 ;init_waiter (&waiter);@@ -631,26 +686,54 @@ rt_spin_lock_slowlock(struct rt_mutex *lock) } missed =1 ; + orig_owner =rt_mutex_owner (lock); +/* * waiter.task is NULL the first time we come here and * when we have been woken up by the previous owner * but the lock got stolen by an higher prio task. */ -if (!waiter.task) { -add_waiter (lock, &waiter, &flags); +if (!waiter.task) +_add_waiter (lock, &waiter); + +/* +* We only need to pi-boost the owner if they are lower +* priority than us. We dont care if this is racy +* against priority changes as we will break out of +* the adaptive spin anytime any priority changes occur +* without boosting enabled. +*/ +if (!waiter.pi.boosted && current->prio < orig_owner->prio) { +boost_lock (lock, &waiter); + boosted =1 ; + +spin_unlock_irqrestore (&lock->wait_lock, flags); +task_pi_update (current,0 ); +spin_lock_irqsave (&lock->wait_lock, flags); +/* Wakeup during boost ? */ if (unlikely (!waiter.task))continue ; }/* +* If we are not currently pi-boosting the lock, we have to +* monitor whether our priority changed since the last +* time it was recorded and requeue ourselves if it moves. +*/ +if (!waiter.pi.boosted && waiter.pi.prio != current->prio) { + waiter.pi.prio = current->prio; + +requeue_waiter (lock, &waiter); + } + +/* * Prevent schedule() to drop BKL, while waiting for * the lock ! We restore lock_depth when we come back. */ saved_flags = current->flags &PF_NOSCHED ; current->lock_depth =-1 ; current->flags &= ~PF_NOSCHED ; - orig_owner =rt_mutex_owner (lock);get_task_struct (orig_owner);spin_unlock_irqrestore (&lock->wait_lock, flags);@@ -664,6 +747,24 @@ rt_spin_lock_slowlock(struct rt_mutex *lock)* barrier which we rely upon to ensure current->state * is visible before we test waiter.task. */ +if (waiter.task && !waiter.pi.boosted) { +spin_lock_irqsave (&lock->wait_lock, flags); + +/* +* We get here if we have not yet boosted +* the lock, yet we are going to sleep. If +* we are still pending (waiter.task != 0), +* then go ahead and boost them now +*/ +if (waiter.task) { +boost_lock (lock, &waiter); + boosted =1 ; + } + +spin_unlock_irqrestore (&lock->wait_lock, flags); +task_pi_update (current,0 ); + } +if (waiter.task)schedule_rt_mutex (lock); }else @@ -696,7 +797,8 @@ rt_spin_lock_slowlock(struct rt_mutex *lock)spin_unlock_irqrestore (&lock->wait_lock, flags);/* Undo any pi boosting, if necessary */ -task_pi_update (current,0 ); +if (boosted) +task_pi_update (current,0 );debug_rt_mutex_free_waiter (&waiter); }@@ -708,6 +810,7 @@ static void noinline __sched rt_spin_lock_slowunlock(struct rt_mutex *lock) {unsigned long flags; +int deboost =0 ;spin_lock_irqsave (&lock->wait_lock, flags);@@ -721,12 +824,16 @@ rt_spin_lock_slowunlock(struct rt_mutex *lock)return ; } +if (lock->pi.boosters) + deboost =1 ; +wakeup_next_waiter (lock,1 );spin_unlock_irqrestore (&lock->wait_lock, flags); -/* Undo pi boosting when necessary */ -task_pi_update (current,0 ); +if (deboost) +/* Undo pi boosting when necessary */ +task_pi_update (current,0 ); } void __lockfunc rt_spin_lock(spinlock_t *lock)kernel/rtmutex_common.h +1
-0@@ -55,6 +55,7 @@ struct rt_mutex_waiter {struct {struct pi_sink snk;int prio; +int boosted; } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES unsigned long ip;Signature · 5 lines
-- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
v2 cover Priority Inheritance enhancements
In reply to Gregory Haskins** RFC for PREEMPT_RT branch, 26-rt1 **
Synopsis: We gain a 13%+ IO improvement in the PREEMPT_RT kernel by re-working some of the PI logic.
[
pi-enhancements v2
Changes since v1:
*) Added proper reference counting to prevent tasks from
deleting while a node->update() is still in flight
*) unified the RCU boost path
]
[
fyi -> you can find this series at the following URLs in addition to this thread:
http://git.kernel.org/?p=linux/kernel/git/ghaskins/linux-2.6-hacks.git;a=shortlog;h=pi-rework
ftp://ftp.novell.com/dev/ghaskins/pi-rework-v2.tar.bz2
]
Hi All,
The following series applies to 26-rt1 as a request-for-comment on a new approach to priority-inheritance (PI), as well as some performance enhancements to take advantage of those new approaches. This yields at least a 13-15% improvement for diskio on my 4-way x86_64 system. An 8-way system saw as much as 700% improvement during early testing, but I have not recently reconfirmed this number.
Motivation for series:
I have several ideas on things we can do to enhance and improve kernel performance with respect to PREEMPT_RT
1) For instance, it would be nice to support priority queuing and (at least positional) inheritance in the wait-queue infrastructure.
2) Reducing overhead in the real-time locks (sleepable replacements for spinlock_t in PREEMPT_RT) to try to approach the minimal overhead if their non-rt equivalent. We have determined via instrumentation that one area of major overhead is the pi-boost logic.
However, today the PI code is entwined in the rtmutex infrastructure, yet we require more flexibility if we want to address (1) and (2) above. Therefore the first step is to separate the PI code away from rtmutex into its own library (libpi). This is covered in patches 1-7.
(I realize patch #7 is a little hard to review since I removed and added a lot of code that the unified diff is all mashing together...I will try to find a way to make this more readable).
Patch 8 is the first real consumer of the libpi logic to try to enhance performance. It accomplishes this by deferring pi-boosting a lock owner unless it is absolutely necessary. Since instrumentation shows that the majority of locks are acquired either via the fast-path, or via the adaptive-spin path, we can eliminate most of the pi-overhead with this technique. This yields a measurable performance gain (at least 13% for workloads with heavy lock contention was observed in our lab).
We have not yet completed the work on the pi-waitqueues or any of the other related pi enhancements. Those will be coming in a follow-on announcement.
Feedback/comments welcome!
Regards,
-Greg
---
Gregory Haskins (8):
rtmutex: pi-boost locks as late as possible
rtmutex: convert rtmutexes to fully use the PI library
rtmutex: use runtime init for rtmutexes
RT: wrap the rt_rwlock "add reader" logic
rtmutex: formally initialize the rt_mutex_waiters
sched: rework task reference counting to work with the pi infrastructure
sched: add the basic PI infrastructure to the task_struct add generalized priority-inheritance interface
Documentation/libpi.txt | 59 ++ include/linux/pi.h | 278 +++++++++++ include/linux/rt_lock.h | 2 include/linux/rtmutex.h | 18 - include/linux/sched.h | 57 +- include/linux/workqueue.h | 2 kernel/fork.c | 35 + kernel/rcupreempt-boost.c | 25 - kernel/rtmutex-debug.c | 4 kernel/rtmutex-tester.c | 4 kernel/rtmutex.c | 1091 ++++++++++++++++++--------------------------- kernel/rtmutex_common.h | 19 - kernel/rwlock_torture.c | 32 - kernel/sched.c | 209 ++++++--- kernel/workqueue.c | 39 +- lib/Makefile | 3 lib/pi.c | 516 +++++++++++++++++++++ 17 files changed, 1543 insertions(+), 850 deletions(-) create mode 100644 Documentation/libpi.txt create mode 100644 include/linux/pi.h create mode 100644 lib/pi.c
RFC PATCH v2 1/8 add generalized priority-inheritance interface
The kernel currently addresses priority-inversion through priority- inheritence. However, all of the priority-inheritence logic is integrated into the Real-Time Mutex infrastructure. This causes a few problems:
1) This tightly coupled relationship makes it difficult to extend to other areas of the kernel (for instance, pi-aware wait-queues may be desirable).
2) Enhancing the rtmutex infrastructure becomes challenging because there is no seperation between the locking code, and the pi-code.
This patch aims to rectify these shortcomings by designing a stand-alone pi framework which can then be used to replace the rtmutex-specific version. The goal of this framework is to provide similar functionality to the existing subsystem, but with sole focus on PI and the relationships between objects that can boost priority, and the objects that get boosted.
We introduce the concept of a "pi_source" and a "pi_sink", where, as the name suggests provides the basic relationship of a priority source, and its boosted target. A pi_source acts as a reference to some arbitrary source of priority, and a pi_sink can be boosted (or deboosted) by a pi_source. For more details, please read the library documentation.
There are currently no users of this inteface.
---
Documentation/libpi.txt | 59 +++++ include/linux/pi.h | 278 +++++++++++++++++++++++++ lib/Makefile | 3 lib/pi.c | 516 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 855 insertions(+), 1 deletions(-) create mode 100644 Documentation/libpi.txt create mode 100644 include/linux/pi.h create mode 100644 lib/pi.c
Documentation/libpi.txt new file +59 -0
@@ -0,0 +1,59 @@ +lib/pi.c - Priority Inheritance library + +Sources and sinks: +------------ + +This library introduces the basic concept of a "pi_source" and a "pi_sink", where, as the name suggests provides the basic relationship of a priority source, and its boosted target. + +A pi_source is simply a reference to some arbitrary priority value that may range from 0 (highest prio), to MAX_PRIO (currently 140, lowest prio). A pi_source calls pi_sink.boost() whenever it wishes to boost the sink to (at least minimally) the priority value that the source represents. It uses pi_sink.boost() for both the initial boosting, or for any subsequent refreshes to the value (even if the value is decreasing in logical priority). The policy of the sink will dictate what happens as a result of that boost. Likewise, a pi_source calls pi_sink.deboost() to stop contributing to the sink's minimum priority. + +It is important to note that a source is a reference to a priority value, not a value itself. This is one of the concepts that allows the interface to be idempotent, which is important for properly updating a chain of sources and sinks in the proper order. If we passed the priority on the stack, the order in which the system executes could allow the actual value that is set to race. + +Nodes: + +A pi_node is a convenience object which is simultaneously a source and a sink. As its name suggests, it would typically be deployed as a node in a pi-chain. Other pi_sources can boost a node via its pi_sink.boost() interface. Likewise, a node can boost a fixed number of sinks via the node.add_sink() interface. + +Generally speaking, a node takes care of many common operations associated with being a “link in the chain”, such as: + + 1) determining the current priority of the node based on the (logically) highest priority source that is boosting the node. + 2) boosting/deboosting upstream sinks whenever the node locally changes priority. + 3) taking care to avoid deadlock during a chain update. + +Design details: + +Destruction: + +The pi-library objects are designed to be implicitly-destructable (meaning they do not require an explicit “free()” operation when they are not used anymore). This is important considering their intended use (spinlock_t's which are also implicitly-destructable). As such, any allocations needed for operation must come from internal structure storage as there will be no opportunity to free it later. + +Multiple sinks per Node: + +We allow multiple sinks to be associated with a node. This is a slight departure from the previous implementation which had the notion of only a single sink (i.e. “task->pi_blocked_on”). The reason why we added the ability to add more than one sink was not to change the default chaining model (I.e. multiple boost targets), but rather to add a flexible notification mechanism that is peripheral to the chain, which are informally called “leaf sinks”. + +Leaf-sinks are boostable objects that do not perpetuate a chain per se. Rather, they act as endpoints to a priority boosting. Ultimately, every chain ends with a leaf-sink, which presumably will act on the new priority information. However, there may be any number of leaf-sinks along a chain as well. Each one will act on its localized priority in its own implementation specific way. For instance, a task_struct pi-leaf may change the priority of the task and reschedule it if necessary. Whereas an rwlock leaf-sink may boost a list of reader-owners. + +The following diagram depicts an example relationship (warning: cheesy ascii art) + + --------- --------- + | leaf | | leaf | + --------- --------- + / / + --------- / ---------- / --------- --------- + ->-| node |->---| node |-->---| node |->---| leaf | + --------- ---------- --------- --------- + +The reason why this was done was to unify the notion of a “sink” to a single interface, rather than having something like task->pi_blocks_on and a separate callback for the leaf action. Instead, any downstream object can be represented by a sink, and the implementation details are hidden (e.g. im a task, im a lock, im a node, im a work-item, im a wait-queue, etc). + +Sinkrefs: + +Each pi_sink.boost() operation is represented by a unique pi_source to properly facilitate a one node to many source relationship. Therefore, if a pi_node is to act as aggregator to multiple sinks, it implicitly must have one internal pi_source object for every sink that is added (via node.add_sink(). This pi_source object has to be internally managed for the lifetime of the sink reference. + +Recall that due to the implicit-destruction requirement above, and the fact that we will typically be executing in a preempt-disabled region, we have to be very careful about how we allocate references to those sinks. More on that next. But long story short we limit the number of sinks to MAX_PI_DEPENDENDICES (currently 5). + +Locking: + +(work in progress....) + + + + +
include/linux/pi.h new file +278 -0
278 lines not on this page · raw message
lib/Makefile +2 -1
@@ -23,7 +23,8 @@ lib-$(CONFIG_SMP) += cpumask.o lib-y += kobject.o kref.o klist.o obj-y += div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ - bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o + bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ + pi.o ifeq ($(CONFIG_DEBUG_KOBJECT),y) CFLAGS_kobject.o += -DDEBUG
lib/pi.c new file +516 -0
516 lines not on this page · raw message
--
To unsubscribe from this list: send the line "unsubscribe linux-rt-users" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
RFC PATCH v3 1/1 add generalized priority-inheritance interface
[
of course, 2 seconds after I hit "send" on v2 I realized there was a race condition in libpi w.r.t. the sinkref->prio. Rather than spam you guys with a full "v3" refresh of the series, here is a fixed version of patch 1/8 which constitutes "v3" when used with patches 2-8 from v2.
]
The kernel currently addresses priority-inversion through priority- inheritence. However, all of the priority-inheritence logic is integrated into the Real-Time Mutex infrastructure. This causes a few problems:
1) This tightly coupled relationship makes it difficult to extend to other areas of the kernel (for instance, pi-aware wait-queues may be desirable).
2) Enhancing the rtmutex infrastructure becomes challenging because there is no seperation between the locking code, and the pi-code.
This patch aims to rectify these shortcomings by designing a stand-alone pi framework which can then be used to replace the rtmutex-specific version. The goal of this framework is to provide similar functionality to the existing subsystem, but with sole focus on PI and the relationships between objects that can boost priority, and the objects that get boosted.
We introduce the concept of a "pi_source" and a "pi_sink", where, as the name suggests provides the basic relationship of a priority source, and its boosted target. A pi_source acts as a reference to some arbitrary source of priority, and a pi_sink can be boosted (or deboosted) by a pi_source. For more details, please read the library documentation.
There are currently no users of this inteface.
---
Documentation/libpi.txt | 59 +++++ include/linux/pi.h | 277 ++++++++++++++++++++++++++ lib/Makefile | 3 lib/pi.c | 509 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 847 insertions(+), 1 deletions(-) create mode 100644 Documentation/libpi.txt create mode 100644 include/linux/pi.h create mode 100644 lib/pi.c
Documentation/libpi.txt new file +59 -0
@@ -0,0 +1,59 @@ +lib/pi.c - Priority Inheritance library + +Sources and sinks: +------------ + +This library introduces the basic concept of a "pi_source" and a "pi_sink", where, as the name suggests provides the basic relationship of a priority source, and its boosted target. + +A pi_source is simply a reference to some arbitrary priority value that may range from 0 (highest prio), to MAX_PRIO (currently 140, lowest prio). A pi_source calls pi_sink.boost() whenever it wishes to boost the sink to (at least minimally) the priority value that the source represents. It uses pi_sink.boost() for both the initial boosting, or for any subsequent refreshes to the value (even if the value is decreasing in logical priority). The policy of the sink will dictate what happens as a result of that boost. Likewise, a pi_source calls pi_sink.deboost() to stop contributing to the sink's minimum priority. + +It is important to note that a source is a reference to a priority value, not a value itself. This is one of the concepts that allows the interface to be idempotent, which is important for properly updating a chain of sources and sinks in the proper order. If we passed the priority on the stack, the order in which the system executes could allow the actual value that is set to race. + +Nodes: + +A pi_node is a convenience object which is simultaneously a source and a sink. As its name suggests, it would typically be deployed as a node in a pi-chain. Other pi_sources can boost a node via its pi_sink.boost() interface. Likewise, a node can boost a fixed number of sinks via the node.add_sink() interface. + +Generally speaking, a node takes care of many common operations associated with being a “link in the chain”, such as: + + 1) determining the current priority of the node based on the (logically) highest priority source that is boosting the node. + 2) boosting/deboosting upstream sinks whenever the node locally changes priority. + 3) taking care to avoid deadlock during a chain update. + +Design details: + +Destruction: + +The pi-library objects are designed to be implicitly-destructable (meaning they do not require an explicit “free()” operation when they are not used anymore). This is important considering their intended use (spinlock_t's which are also implicitly-destructable). As such, any allocations needed for operation must come from internal structure storage as there will be no opportunity to free it later. + +Multiple sinks per Node: + +We allow multiple sinks to be associated with a node. This is a slight departure from the previous implementation which had the notion of only a single sink (i.e. “task->pi_blocked_on”). The reason why we added the ability to add more than one sink was not to change the default chaining model (I.e. multiple boost targets), but rather to add a flexible notification mechanism that is peripheral to the chain, which are informally called “leaf sinks”. + +Leaf-sinks are boostable objects that do not perpetuate a chain per se. Rather, they act as endpoints to a priority boosting. Ultimately, every chain ends with a leaf-sink, which presumably will act on the new priority information. However, there may be any number of leaf-sinks along a chain as well. Each one will act on its localized priority in its own implementation specific way. For instance, a task_struct pi-leaf may change the priority of the task and reschedule it if necessary. Whereas an rwlock leaf-sink may boost a list of reader-owners. + +The following diagram depicts an example relationship (warning: cheesy ascii art) + + --------- --------- + | leaf | | leaf | + --------- --------- + / / + --------- / ---------- / --------- --------- + ->-| node |->---| node |-->---| node |->---| leaf | + --------- ---------- --------- --------- + +The reason why this was done was to unify the notion of a “sink” to a single interface, rather than having something like task->pi_blocks_on and a separate callback for the leaf action. Instead, any downstream object can be represented by a sink, and the implementation details are hidden (e.g. im a task, im a lock, im a node, im a work-item, im a wait-queue, etc). + +Sinkrefs: + +Each pi_sink.boost() operation is represented by a unique pi_source to properly facilitate a one node to many source relationship. Therefore, if a pi_node is to act as aggregator to multiple sinks, it implicitly must have one internal pi_source object for every sink that is added (via node.add_sink(). This pi_source object has to be internally managed for the lifetime of the sink reference. + +Recall that due to the implicit-destruction requirement above, and the fact that we will typically be executing in a preempt-disabled region, we have to be very careful about how we allocate references to those sinks. More on that next. But long story short we limit the number of sinks to MAX_PI_DEPENDENDICES (currently 5). + +Locking: + +(work in progress....) + + + + +
include/linux/pi.h new file +277 -0
277 lines not on this page · raw message
lib/Makefile +2 -1
@@ -23,7 +23,8 @@ lib-$(CONFIG_SMP) += cpumask.o lib-y += kobject.o kref.o klist.o obj-y += div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ - bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o + bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ + pi.o ifeq ($(CONFIG_DEBUG_KOBJECT),y) CFLAGS_kobject.o += -DDEBUG
lib/pi.c new file +509 -0
509 lines not on this page · raw message
--
To unsubscribe from this list: send the line "unsubscribe linux-rt-users" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
RFC PATCH v2 2/8 sched: add the basic PI infrastructure to the task_struct
In reply to Gregory HaskinsThis is a first pass at converting the system to use the new PI library. We dont go for a wholesale replacement quite yet so that we can focus on getting the basic plumbing in place. Later in the series we will begin replacing some of the proprietary logic with the generic framework.
---
include/linux/sched.h | 37 +++++++-- include/linux/workqueue.h | 2 kernel/fork.c | 1 kernel/rcupreempt-boost.c | 23 +----- kernel/rtmutex.c | 6 + kernel/sched.c | 188 ++++++++++++++++++++++++++++++++------------- kernel/workqueue.c | 39 ++++++++- 7 files changed, 206 insertions(+), 90 deletions(-)
include/linux/sched.h +31 -6
@@ -87,6 +87,7 @@ struct sched_param {#include <linux/task_io_accounting.h> #include <linux/kobject.h> #include <linux/latencytop.h> +#include <linux/pi.h> #include <asm/processor.h>
@@ -1125,6 +1126,7 @@ struct task_struct {int prio, static_prio, normal_prio;#ifdef CONFIG_PREEMPT_RCU_BOOST int rcu_prio; +struct pi_source rcu_prio_src;#endif const struct sched_class *sched_class;struct sched_entity se;
@@ -1298,11 +1300,20 @@ struct task_struct {/* Protection of the PI data structures: */ raw_spinlock_t pi_lock; +struct { +struct pi_source src;/* represents normal_prio to 'this' */ +struct pi_node node; +struct pi_sink snk;/* registered to 'this' to get updates */ +int prio; + } pi; +#ifdef CONFIG_RT_MUTEXES /* PI waiters blocked on a rt_mutex held by this task */ struct plist_head pi_waiters;/* Deadlock detection and priority inheritance handling */ struct rt_mutex_waiter *pi_blocked_on; +int rtmutex_prio; +struct pi_source rtmutex_prio_src;#endif #ifdef CONFIG_DEBUG_MUTEXES
@@ -1440,6 +1451,26 @@ struct task_struct {#endif }; +static inline int +task_pi_boost (struct task_struct *p,struct pi_source *src, +unsigned int flags) +{ +return pi_boost (&p->pi.node, src, flags); +} + +static inline int +task_pi_deboost (struct task_struct *p,struct pi_source *src, +unsigned int flags) +{ +return pi_deboost (&p->pi.node, src, flags); +} + +static inline int +task_pi_update (struct task_struct *p,unsigned int flags) +{ +return pi_update (&p->pi.node, flags); +} +#ifdef CONFIG_PREEMPT_RT # define set_printk_might_sleep (x) do { current->in_printk = x; } while(0)#else
@@ -1774,14 +1805,8 @@ int sched_rt_handler(struct ctl_table *table, int write,extern unsigned int sysctl_sched_compat_yield; -extern void task_setprio (struct task_struct *p,int prio); -#ifdef CONFIG_RT_MUTEXES extern int rt_mutex_getprio (struct task_struct *p); -static inline void rt_mutex_setprio (struct task_struct *p,int prio) -{ -task_setprio (p, prio); -}extern void rt_mutex_adjust_pi (struct task_struct *p);#else static inline int rt_mutex_getprio (struct task_struct *p)
include/linux/workqueue.h +2 -0
@@ -11,6 +11,7 @@#include <linux/lockdep.h> #include <linux/plist.h> #include <linux/sched_prio.h> +#include <linux/pi.h> #include <asm/atomic.h> struct workqueue_struct ;
@@ -31,6 +32,7 @@ struct work_struct {#define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK)struct plist_node entry;work_func_t func; +struct pi_source pi_src;#ifdef CONFIG_LOCKDEP struct lockdep_map lockdep_map;#endif
kernel/fork.c +1 -0
@@ -990,6 +990,7 @@ static struct task_struct *copy_process(unsigned long clone_flags, p->rcu_flipctr_idx =0 ;#ifdef CONFIG_PREEMPT_RCU_BOOST p->rcu_prio =MAX_PRIO ; +pi_source_init (&p->rcu_prio_src, &p->rcu_prio); p->rcub_rbdp =NULL ; p->rcub_state =RCU_BOOST_IDLE ;INIT_LIST_HEAD (&p->rcub_entry);
kernel/rcupreempt-boost.c +3 -20
@@ -232,14 +232,11 @@ static inline int rcu_is_boosted(struct task_struct *task)static void rcu_boost_task (struct task_struct *task) {WARN_ON (!irqs_disabled ()); -WARN_ON_SMP (!spin_is_locked (&task->pi_lock));rcu_trace_boost_task_boost_called (RCU_BOOST_ME ); -if (task->rcu_prio < task->prio) { +if (task_pi_boost (task, &task->rcu_prio_src,0 ))rcu_trace_boost_task_boosted (RCU_BOOST_ME ); -task_setprio (task, task->rcu_prio); - } }/**
@@ -275,26 +272,17 @@ void __rcu_preempt_boost(void) rbd = &__get_cpu_var (rcu_boost_data);spin_lock (&rbd->rbs_lock); -spin_lock (&curr->pi_lock); - curr->rcub_rbdp = rbd;rcu_trace_boost_try_boost (rbd); - prio =rt_mutex_getprio (curr); -if (list_empty (&curr->rcub_entry))list_add_tail (&curr->rcub_entry, &rbd->rbs_toboost); -if (prio <= rbd->rbs_prio) - goto out; - -rcu_trace_boost_boosted (curr->rcub_rbdp);set_rcu_prio (curr, rbd->rbs_prio);rcu_boost_task (curr); out: -spin_unlock (&curr->pi_lock);spin_unlock_irqrestore (&rbd->rbs_lock, flags); }
@@ -353,15 +341,12 @@ void __rcu_preempt_unboost(void)rcu_trace_boost_unboosted (rbd); -set_rcu_prio (curr,MAX_PRIO ); +task_pi_deboost (curr, &curr->rcu_prio_src,0 ); -spin_lock (&curr->pi_lock); - prio =rt_mutex_getprio (curr); -task_setprio (curr, prio); +set_rcu_prio (curr,MAX_PRIO ); curr->rcub_rbdp =NULL ; -spin_unlock (&curr->pi_lock); out:spin_unlock_irqrestore (&rbd->rbs_lock, flags); }
@@ -393,9 +378,7 @@ static int __rcu_boost_readers(struct rcu_boost_dat *rbd, int prio, unsigned lonlist_move_tail (&p->rcub_entry, &rbd->rbs_boosted);set_rcu_prio (p, prio); -spin_lock (&p->pi_lock);rcu_boost_task (p); -spin_unlock (&p->pi_lock);/* * Now we release the lock to allow for a higher
kernel/rtmutex.c +4 -2
@@ -178,8 +178,10 @@ static void __rt_mutex_adjust_prio(struct task_struct *task) {int prio =rt_mutex_getprio (task); -if (task->prio != prio) -rt_mutex_setprio (task, prio); +if (task->rtmutex_prio != prio) { + task->rtmutex_prio = prio; +task_pi_boost (task, &task->rtmutex_prio_src,0 ); + } }/*
kernel/sched.c +133 -55
305 lines not on this page · raw message
kernel/workqueue.c +32 -7
@@ -145,8 +145,13 @@ static void insert_work(struct cpu_workqueue_struct *cwq,plist_node_init (&work->entry, prio);plist_add (&work->entry, &cwq->worklist); -if (boost_prio < cwq->thread->prio) -task_setprio (cwq->thread, boost_prio); +/* +* FIXME: We want to boost to boost_prio, but we dont record that +* value in the work_struct for later deboosting +*/ +pi_source_init (&work->pi_src, &work->entry.prio); +task_pi_boost (cwq->thread, &work->pi_src,0 ); +wake_up (&cwq->more_work); }
@@ -280,6 +285,10 @@ struct wq_barrier {static void run_workqueue (struct cpu_workqueue_struct *cwq) {struct plist_head *worklist = &cwq->worklist; +struct pi_source pi_src; +int prio; + +pi_source_init (&pi_src, &prio);spin_lock_irq (&cwq->lock); cwq->run_depth++;
@@ -292,10 +301,10 @@ static void run_workqueue(struct cpu_workqueue_struct *cwq) again:while (!plist_head_empty (worklist)) { -int prio;struct work_struct *work =plist_first_entry (worklist, struct work_struct, entry);work_func_t f = work->func; +#ifdef CONFIG_LOCKDEP /* * It is permissible to free the struct work_struct
@@ -316,14 +325,28 @@ again: } prio =max (prio,0 ); -if (likely (cwq->thread->prio != prio)) -task_setprio (cwq->thread, prio); - cwq->current_work = work;plist_del (&work->entry, worklist);plist_node_init (&work->entry,MAX_PRIO );spin_unlock_irq (&cwq->lock); +/* +* The owner is free to reuse the work object once we execute +* the work->func() below. Therefore we cannot leave the +* work->pi_src boosting our thread or it may get stomped +* on when the work item is requeued. +* +* So what we do is we boost ourselves with an on-the +* stack copy of the priority of the work item, and then +* deboost the workitem. Once the work is complete, we +* can then simply deboost the stack version. +* +* Note that this will not typically cause a pi-chain +* update since we are boosting the node laterally +*/ +task_pi_boost (current, &pi_src,PI_FLAG_DEFER_UPDATE ); +task_pi_deboost (current, &work->pi_src,PI_FLAG_DEFER_UPDATE ); +BUG_ON (get_wq_data (work) != cwq);work_clear_pending (work);leak_check (NULL );
@@ -334,6 +357,9 @@ again:lock_release (&cwq->wq->lockdep_map,1 , _THIS_IP_);leak_check (f); +/* Deboost the stack copy of the work->prio (see above) */ +task_pi_deboost (current, &pi_src,0 ); +spin_lock_irq (&cwq->lock); cwq->current_work =NULL ;wake_up_all (&cwq->work_done);
@@ -357,7 +383,6 @@ again: goto again; } -task_setprio (cwq->thread, current->normal_prio); cwq->run_depth--;spin_unlock_irq (&cwq->lock); }
RFC PATCH v2 3/8 sched: rework task reference counting to work with the pi infrastructure
In reply to Gregory Haskins---
include/linux/sched.h | 5 +++-- kernel/fork.c | 32 +++++++++++++++----------------- kernel/sched.c | 23 +++++++++++++++++++++++ 3 files changed, 41 insertions(+), 19 deletions(-)
include/linux/sched.h +3 -2
@@ -1305,6 +1305,8 @@ struct task_struct {struct pi_node node;struct pi_sink snk;/* registered to 'this' to get updates */ int prio; +struct rcu_head rcu;/* for destruction cleanup */ + } pi;#ifdef CONFIG_RT_MUTEXES
@@ -1633,12 +1635,11 @@ static inline void put_task_struct(struct task_struct *t) call_rcu(&t->rcu, __put_task_struct_cb); }#else -extern void __put_task_struct (struct task_struct *t);static inline void put_task_struct (struct task_struct *t) {if (atomic_dec_and_test (&t->usage)) -__put_task_struct (t); +pi_dropref (&t->pi.node,0 ); }#endif
kernel/fork.c +15 -17
@@ -130,39 +130,37 @@ void free_task(struct task_struct *tsk) }EXPORT_SYMBOL (free_task); -#ifdef CONFIG_PREEMPT_RT -void __put_task_struct_cb (struct rcu_head *rhp) +void prepare_free_task (struct task_struct *tsk) { - struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); -BUG_ON (atomic_read (&tsk->usage)); -WARN_ON (!tsk->exit_state);WARN_ON (tsk == current); +#ifdef CONFIG_PREEMPT_RT +WARN_ON (!tsk->exit_state); +#else +WARN_ON (!(tsk->exit_state & (EXIT_DEAD |EXIT_ZOMBIE ))); +#endif +security_task_free (tsk);free_uid (tsk->user);put_group_info (tsk->group_info); + +#ifdef CONFIG_PREEMPT_RT delayacct_tsk_free (tsk); +#endif if (!profile_handoff_task (tsk))free_task (tsk); } -#else - -void __put_task_struct (struct task_struct *tsk) +#ifdef CONFIG_PREEMPT_RT +void __put_task_struct_cb (struct rcu_head *rhp) { -WARN_ON (!(tsk->exit_state & (EXIT_DEAD |EXIT_ZOMBIE ))); -BUG_ON (atomic_read (&tsk->usage)); -WARN_ON (tsk == current); - -security_task_free (tsk); -free_uid (tsk->user); -put_group_info (tsk->group_info); + struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); -if (!profile_handoff_task (tsk)) -free_task (tsk); +pi_dropref (&tsk->pi.node,0 ); } +#endif /*
kernel/sched.c +23 -0
@@ -2370,11 +2370,34 @@ task_pi_boost_cb(struct pi_sink *snk, struct pi_source *src,return 0 ; } +extern void prepare_free_task (struct task_struct *tsk); + +static void task_pi_free_rcu (struct rcu_head *rhp) +{ + struct task_struct *tsk = container_of(rhp, struct task_struct, pi.rcu); + +prepare_free_task (tsk); +} + +/* +* This function is invoked whenever the last references to a task have +* been dropped, and we should free the memory on the next rcu grace period +*/ +static int task_pi_free_cb (struct pi_sink *snk,unsigned int flags) +{ + struct task_struct *p = container_of(snk, struct task_struct, pi.snk); + +call_rcu (&p->pi.rcu, task_pi_free_rcu); + +return 0 ; +} +static int task_pi_update_cb (struct pi_sink *snk,unsigned int flags);static struct pi_sink task_pi_sink = { .boost = task_pi_boost_cb, .update = task_pi_update_cb, + .free = task_pi_free_cb, }; static inline void
RFC PATCH v2 4/8 rtmutex: formally initialize the rt_mutex_waiters
In reply to Gregory HaskinsWe will be adding more logic to rt_mutex_waiters and therefore lets centralize the initialization to make this easier going forward.
---
kernel/rtmutex.c | 26 ++++++++++++++------------ 1 files changed, 14 insertions(+), 12 deletions(-)
kernel/rtmutex.c +14 -12
@@ -805,6 +805,15 @@ static int adaptive_wait(struct rt_mutex_waiter *waiter, }#endif +static void init_waiter (struct rt_mutex_waiter *waiter) +{ +memset (waiter,0 ,sizeof (*waiter)); + +debug_rt_mutex_init_waiter (waiter); + waiter->task =NULL ; + waiter->write_lock =0 ; +} +/* * Slow path lock function spin_lock style: this variant is very * careful not to miss any non-lock wakeups.
@@ -823,9 +832,7 @@ rt_spin_lock_slowlock(struct rt_mutex *lock)struct task_struct *orig_owner;int missed =0 ; -debug_rt_mutex_init_waiter (&waiter); - waiter.task =NULL ; - waiter.write_lock =0 ; +init_waiter (&waiter);spin_lock_irqsave (&lock->wait_lock, flags);init_lists (lock);
@@ -1324,6 +1331,8 @@ rt_read_slowlock(struct rw_mutex *rwm, int mtx)int saved_lock_depth =-1 ;unsigned long saved_state =-1 , state, flags; +init_waiter (&waiter); +spin_lock_irqsave (&mutex->wait_lock, flags);init_rw_lists (rwm);
@@ -1335,10 +1344,6 @@ rt_read_slowlock(struct rw_mutex *rwm, int mtx)/* Owner is a writer (or a blocked writer). Block on the lock */ -debug_rt_mutex_init_waiter (&waiter); - waiter.task =NULL ; - waiter.write_lock =0 ; -if (mtx) {/* * We drop the BKL here before we go into the wait loop to avoid a
@@ -1538,8 +1543,7 @@ rt_write_slowlock(struct rw_mutex *rwm, int mtx)int saved_lock_depth =-1 ;unsigned long flags, saved_state =-1 , state; -debug_rt_mutex_init_waiter (&waiter); - waiter.task =NULL ; +init_waiter (&waiter);/* we do PI different for writers that are blocked */ waiter.write_lock =1 ;
@@ -2270,9 +2274,7 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,struct rt_mutex_waiter waiter;unsigned long flags; -debug_rt_mutex_init_waiter (&waiter); - waiter.task =NULL ; - waiter.write_lock =0 ; +init_waiter (&waiter);spin_lock_irqsave (&lock->wait_lock, flags);init_lists (lock);
RFC PATCH v2 5/8 RT: wrap the rt_rwlock "add reader" logic
In reply to Gregory HaskinsWe will use this later in the series to add PI functions on "add".
---
kernel/rtmutex.c | 16 +++++++++++----- 1 files changed, 11 insertions(+), 5 deletions(-)
kernel/rtmutex.c +11 -5
@@ -1122,6 +1122,12 @@ static void rw_check_held(struct rw_mutex *rwm)# define rw_check_held (rwm) do { } while (0)#endif +static inline void +rt_rwlock_add_reader (struct reader_lock_struct *rls,struct rw_mutex *rwm) +{ +list_add (&rls->list, &rwm->readers); +} +/* * The fast path does not add itself to the reader list to keep * from needing to grab the spinlock. We need to add the owner
@@ -1163,7 +1169,7 @@ rt_rwlock_update_owner(struct rw_mutex *rwm, struct task_struct *own)if (rls->list.prev && !list_empty (&rls->list))return ; -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rls, rwm);/* change to reader, so no one else updates too */ rt_rwlock_set_owner (rwm,RT_RW_READER ,RT_RWLOCK_CHECK );
@@ -1197,7 +1203,7 @@ static int try_to_take_rw_read(struct rw_mutex *rwm, int mtx)* it hasn't been added to the link list yet. */ if (!rls->list.prev ||list_empty (&rls->list)) -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rls, rwm);rt_rwlock_set_owner (rwm,RT_RW_READER ,0 ); rls->count++; incr =0 ;
@@ -1276,7 +1282,7 @@ static int try_to_take_rw_read(struct rw_mutex *rwm, int mtx) rls->lock = rwm; rls->count =1 ;WARN_ON (rls->list.prev && !list_empty (&rls->list)); -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rls, rwm); }else WARN_ON_ONCE (1 );spin_unlock (¤t->pi_lock);
@@ -1473,7 +1479,7 @@ __rt_read_fasttrylock(struct rw_mutex *rwm)spin_lock (&mutex->wait_lock); rls = ¤t->owned_read_locks[reader_count];if (!rls->list.prev ||list_empty (&rls->list)) -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rlw, rwm);spin_unlock (&mutex->wait_lock); }else spin_unlock (¤t->pi_lock);
@@ -2083,7 +2089,7 @@ rt_mutex_downgrade_write(struct rw_mutex *rwm)/* Set us up for multiple readers or conflicts */ -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rls, rwm); rwm->owner =RT_RW_READER ;/*
RFC PATCH v2 6/8 rtmutex: use runtime init for rtmutexes
In reply to Gregory HaskinsThe system already has facilities to perform late/run-time init for rtmutexes. We want to add more advanced initialization later in the series so we force all rtmutexes through the init path in preparation for the later patches.
---
include/linux/rtmutex.h | 2 -- 1 files changed, 0 insertions(+), 2 deletions(-)
include/linux/rtmutex.h +0 -2
@@ -64,8 +64,6 @@ struct hrtimer_sleeper;#define __RT_MUTEX_INITIALIZER (mutexname) \ { .wait_lock = RAW_SPIN_LOCK_UNLOCKED(mutexname) \ - , .wait_list = PLIST_HEAD_INIT(mutexname.wait_list, &mutexname.wait_lock) \ - , .owner = NULL \ __DEBUG_RT_MUTEX_INITIALIZER(mutexname)}#define DEFINE_RT_MUTEX (mutexname) \
RFC PATCH v2 7/8 rtmutex: convert rtmutexes to fully use the PI library
In reply to Gregory HaskinsWe have previously only laid some of the groundwork to use the PI library, but left the existing infrastructure in place in the rtmutex code. This patch converts the rtmutex PI code to officially use the PI library.
---
include/linux/rt_lock.h | 2 include/linux/rtmutex.h | 15 - include/linux/sched.h | 21 - kernel/fork.c | 2 kernel/rcupreempt-boost.c | 2 kernel/rtmutex-debug.c | 4 kernel/rtmutex-tester.c | 4 kernel/rtmutex.c | 944 ++++++++++++++------------------------------- kernel/rtmutex_common.h | 18 - kernel/rwlock_torture.c | 32 -- kernel/sched.c | 12 - 11 files changed, 321 insertions(+), 735 deletions(-)
include/linux/rt_lock.h +2 -0
@@ -14,6 +14,7 @@#include <asm/atomic.h> #include <linux/spinlock_types.h> #include <linux/sched_prio.h> +#include <linux/pi.h> #ifdef CONFIG_PREEMPT_RT /*
@@ -67,6 +68,7 @@ struct rw_mutex {atomic_t count;/* number of times held for read */ atomic_t owners;/* number of owners as readers */ struct list_head readers; +struct pi_sink pi_snk;int prio; };
include/linux/rtmutex.h +7 -8
@@ -15,6 +15,7 @@#include <linux/linkage.h> #include <linux/plist.h> #include <linux/spinlock_types.h> +#include <linux/pi.h> /** * The rt_mutex structure
@@ -27,6 +28,12 @@ struct rt_mutex {raw_spinlock_t wait_lock;struct plist_head wait_list;struct task_struct *owner; +struct { +struct pi_source src; +struct pi_node node; +struct pi_sink snk; +int prio; + } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES int save_state;const char *name, *file;
@@ -96,12 +103,4 @@ extern int rt_mutex_trylock(struct rt_mutex *lock);extern void rt_mutex_unlock (struct rt_mutex *lock); -#ifdef CONFIG_RT_MUTEXES -# define INIT_RT_MUTEXES (tsk) \ - .pi_waiters = PLIST_HEAD_INIT(tsk.pi_waiters, &tsk.pi_lock), \ - INIT_RT_MUTEX_DEBUG(tsk) -#else -# define INIT_RT_MUTEXES (tsk) -#endif -#endif
include/linux/sched.h +1 -20
@@ -1106,6 +1106,7 @@ struct reader_lock_struct {struct rw_mutex *lock;struct list_head list;struct task_struct *task; +struct pi_source pi_src;int count; };
@@ -1309,15 +1310,6 @@ struct task_struct { } pi; -#ifdef CONFIG_RT_MUTEXES -/* PI waiters blocked on a rt_mutex held by this task */ -struct plist_head pi_waiters; -/* Deadlock detection and priority inheritance handling */ -struct rt_mutex_waiter *pi_blocked_on; -int rtmutex_prio; -struct pi_source rtmutex_prio_src; -#endif -#ifdef CONFIG_DEBUG_MUTEXES /* mutex deadlock detection */ struct mutex_waiter *blocked_on;
@@ -1806,17 +1798,6 @@ int sched_rt_handler(struct ctl_table *table, int write,extern unsigned int sysctl_sched_compat_yield; -#ifdef CONFIG_RT_MUTEXES -extern int rt_mutex_getprio (struct task_struct *p); -extern void rt_mutex_adjust_pi (struct task_struct *p); -#else -static inline int rt_mutex_getprio (struct task_struct *p) -{ -return p->normal_prio; -} -# define rt_mutex_adjust_pi (p) do { } while (0) -#endif -extern void set_user_nice (struct task_struct *p,long nice);extern int task_prio (const struct task_struct *p);extern int task_nice (const struct task_struct *p);
kernel/fork.c +0 -2
@@ -885,8 +885,6 @@ static void rt_mutex_init_task(struct task_struct *p) {spin_lock_init (&p->pi_lock);#ifdef CONFIG_RT_MUTEXES -plist_head_init (&p->pi_waiters, &p->pi_lock); - p->pi_blocked_on =NULL ;# ifdef CONFIG_DEBUG_RT_MUTEXES p->last_kernel_lock =NULL ;# endif
kernel/rcupreempt-boost.c +1 -1
@@ -424,7 +424,7 @@ void rcu_boost_readers(void)spin_lock_irqsave (&rcu_boost_wake_lock, flags); - prio =rt_mutex_getprio (curr); + prio =get_rcu_prio (curr);rcu_trace_boost_try_boost_readers (RCU_BOOST_ME );
kernel/rtmutex-debug.c +0 -4
@@ -57,8 +57,6 @@ static void printk_lock(struct rt_mutex *lock, int print_owner)void rt_mutex_debug_task_free (struct task_struct *task) { -DEBUG_LOCKS_WARN_ON (!plist_head_empty (&task->pi_waiters)); -DEBUG_LOCKS_WARN_ON (task->pi_blocked_on);#ifdef CONFIG_PREEMPT_RT WARN_ON (task->reader_lock_count);#endif
@@ -156,7 +154,6 @@ void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter) {memset (waiter,0x11 ,sizeof (*waiter));plist_node_init (&waiter->list_entry,MAX_PRIO ); -plist_node_init (&waiter->pi_list_entry,MAX_PRIO ); waiter->deadlock_task_pid =NULL ; }
@@ -164,7 +161,6 @@ void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter) {put_pid (waiter->deadlock_task_pid);DEBUG_LOCKS_WARN_ON (!plist_node_empty (&waiter->list_entry)); -DEBUG_LOCKS_WARN_ON (!plist_node_empty (&waiter->pi_list_entry));DEBUG_LOCKS_WARN_ON (waiter->task);memset (waiter,0x22 ,sizeof (*waiter)); }
kernel/rtmutex-tester.c +2 -2
@@ -373,11 +373,11 @@ static ssize_t sysfs_test_status(struct sys_device *dev, char *buf)spin_lock (&rttest_lock); curr +=sprintf (curr, -"O: %4d, E:%8d, S: 0x%08lx, P: %4d, N: %4d, B: %p, K: %d, M:" , +"O: %4d, E:%8d, S: 0x%08lx, P: %4d, N: %4d, K: %d M:" , td->opcode, td->event, tsk->state, (MAX_RT_PRIO -1 ) - tsk->prio, (MAX_RT_PRIO -1 ) - tsk->normal_prio, - tsk->pi_blocked_on, td->bkl); + td->bkl);for (i =MAX_RT_TEST_MUTEXES -1 ; i >=0 ; i--) curr +=sprintf (curr,"%d" , td->mutexes[i]);
kernel/rtmutex.c +302 -642
1,280 lines not on this page · raw message
kernel/rtmutex_common.h +5 -13
@@ -14,6 +14,7 @@#include <linux/rtmutex.h> #include <linux/rt_lock.h> +#include <linux/pi.h> /* * The rtmutex in kernel tester is independent of rtmutex debugging. We
@@ -48,10 +49,13 @@ extern void schedule_rt_mutex_test(struct rt_mutex *lock);*/ struct rt_mutex_waiter {struct plist_node list_entry; -struct plist_node pi_list_entry;struct task_struct *task;struct rt_mutex *lock;int write_lock; +struct { +struct pi_sink snk; +int prio; + } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES unsigned long ip;struct pid *deadlock_task_pid;
@@ -79,18 +83,6 @@ rt_mutex_top_waiter(struct rt_mutex *lock)return w; } -static inline int task_has_pi_waiters (struct task_struct *p) -{ -return !plist_head_empty (&p->pi_waiters); -} - -static inline struct rt_mutex_waiter * -task_top_pi_waiter (struct task_struct *p) -{ - return plist_first_entry(&p->pi_waiters, struct rt_mutex_waiter, - pi_list_entry); -} -/* * lock->owner state tracking: */
kernel/rwlock_torture.c +1 -31
@@ -682,37 +682,7 @@ static int __init mutex_stress_init(void)print_owned_read_locks (tsks[i]); -if (tsks[i]->pi_blocked_on) { - w = (void *)tsks[i]->pi_blocked_on; - mtx = w->lock; -spin_unlock_irq (&tsks[i]->pi_lock); -spin_lock_irq (&mtx->wait_lock); -spin_lock (&tsks[i]->pi_lock); - own = (unsigned long )mtx->owner & ~3UL ; - oops_in_progress++; -printk ("%s:%d is blocked on " , - tsks[i]->comm, tsks[i]->pid); -__print_symbol ("%s" , (unsigned long )mtx); -if (own ==0x100 ) -printk (" owner is READER\n" ); -else if (!(own & ~300 )) -printk (" owner is ILLEGAL!!\n" ); -else if (!own) -printk (" has no owner!\n" ); -else { -struct task_struct *owner = (void *)own; - -printk (" owner is %s:%d\n" , - owner->comm, owner->pid); - } - oops_in_progress--; - -spin_unlock (&tsks[i]->pi_lock); -spin_unlock_irq (&mtx->wait_lock); - }else { -print_owned_read_locks (tsks[i]); -spin_unlock_irq (&tsks[i]->pi_lock); - } + spin_unlock_irq(&tsks[i]->pi_lock); } }#endif
kernel/sched.c +0 -11
@@ -2413,12 +2413,6 @@ task_pi_init(struct task_struct *p)pi_source_init (&p->pi.src, &p->normal_prio);task_pi_boost (p, &p->pi.src,PI_FLAG_DEFER_UPDATE ); -#ifdef CONFIG_RT_MUTEXES - p->rtmutex_prio =MAX_PRIO ; -pi_source_init (&p->rtmutex_prio_src, &p->rtmutex_prio); -task_pi_boost (p, &p->rtmutex_prio_src,PI_FLAG_DEFER_UPDATE ); -#endif -/* * We add our own task as a dependency of ourselves so that * we get boost-notifications (via task_pi_boost_cb) whenever
@@ -5029,7 +5023,6 @@ task_pi_update_cb(struct pi_sink *snk, unsigned int flags)*/ if (unlikely (p == rq->idle)) {WARN_ON (p != rq->curr); -WARN_ON (p->pi_blocked_on); goto out_unlock; }
@@ -5360,7 +5353,6 @@ recheck:spin_unlock_irqrestore (&p->pi_lock, flags);task_pi_update (p,0 ); -rt_mutex_adjust_pi (p);return 0 ; }
@@ -8494,10 +8486,6 @@ void __init sched_init(void)task_pi_init (&init_task); -#ifdef CONFIG_RT_MUTEXES -plist_head_init (&init_task.pi_waiters, &init_task.pi_lock); -#endif
RFC PATCH v2 8/8 rtmutex: pi-boost locks as late as possible
In reply to Gregory HaskinsPREEMPT_RT replaces most spinlock_t instances with a preemptible real-time lock that supports priority inheritance. An uncontended (fastpath) acquisition of this lock has no more overhead than its non-rt spinlock_t counterpart. However, the contended case has considerably more overhead so that the lock can maintain proper priority queue order and support pi-boosting of the lock owner, yet remaining fully preemptible.
Instrumentation shows that the majority of acquisitions under most workloads falls either into the fastpath category, or the adaptive spin category within the slowpath. The necessity to pi-boost a lock-owner should be sufficiently rare, yet the slow-path path blindly incurs this overhead in 100% of contentions.
Therefore, this patch intends to capitalize on this observation in order to reduce overhead and improve acquisition throughput. It is important to note that real-time latency is still treated as a higher order constraint than throughput, so the full pi-protocol is observed using new carefully constructed rules around the old concepts.
1) We check the priority of the owner relative to the waiter on each spin of the lock (if we are not boosted already). If the owner's effective priority is logically less than the waiters priority, we must boost them.
2) We check the priority of ourselves against our current queue position on the waiters-list (if we are not boosted already). If our priority was changed, we need to re-queue ourselves to update our position.
3) We break out of the adaptive-spin if either of the above conditions (1), (2) change so that we can re-evaluate the lock conditions.
4) We must enter pi-boost mode if, at any time, we decide to voluntarily preempt since we are losing our ability to dynamically process the conditions above.
Note: We still fully support priority inheritance with this protocol, even if we defer the low-level calls to adjust priority. The difference is really in terms of being a pro-active protocol (boost on entry), verses a reactive protocol (boost when necessary). The upside to the latter is that we don't take a penalty for pi when it is not necessary (which is most of the time) The downside is that we technically leave the owner exposed to getting preempted (should it get asynchronously deprioritized), even if our waiter is the highest priority task in the system. When this happens, the owner would be immediately boosted (because we would hit the "oncpu" condition, and subsequently follow the voluntary preempt path which boosts the owner). Therefore, inversion is correctly prevented, but we have the extra latency of the preempt/boost/wakeup that could have been avoided in the proactive model.
However, the design of the algorithm described above constrains the probability of this phenomenon occurring to setscheduler() operations. Since rt-locks do not support being interrupted by signals or timeouts, waiters only depart via the acquisition path. And while acquisitions do deboost the owner, the owner also changes simultaneously, rending the deboost moot relative to the other waiters.
What this all means is that the downside to this implementation is that a high-priority waiter *may* see an extra latency (equivalent to roughly two wake-ups) if the owner has its priority reduced via setscheduler() while it holds the lock. The penalty is deterministic, arguably small enough, and sufficiently rare that I do not believe it should be an issue.
Note: If the concept of other exit paths are ever introduced in the future, simply adapting the condition to look at owner->normal_prio instead of owner->prio should once again constrain the limitation to setscheduler().
Special thanks to Peter Morreale for suggesting the optimization to only consider skipping the boost if the owner is >= to current.
---
include/linux/rtmutex.h | 1 kernel/rtmutex.c | 195 ++++++++++++++++++++++++++++++++++++----------- kernel/rtmutex_common.h | 1 3 files changed, 153 insertions(+), 44 deletions(-)
include/linux/rtmutex.h +1 -0
@@ -33,6 +33,7 @@ struct rt_mutex {struct pi_node node;struct pi_sink snk;int prio; +int boosters; } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES int save_state;
kernel/rtmutex.c +151 -44
325 lines not on this page · raw message
kernel/rtmutex_common.h +1 -0
@@ -55,6 +55,7 @@ struct rt_mutex_waiter {struct {struct pi_sink snk;int prio; +int boosted; } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES unsigned long ip;
v4 cover Priority Inheritance enhancements
In reply to Gregory Haskins** RFC for PREEMPT_RT branch, 26-rt1 **
Synopsis: We gain a 13%+ IO improvement in the PREEMPT_RT kernel by re-working some of the PI logic.
[
Changelog:
v4:
1) Incorporated review comments
*) Fixed checkpatch warning about extern in .c *) Renamed s/snk/sink
*) Renamed s/addref/get
*) Renamed s/dropref/put
*) Made pi_sink use static *ops
2) Fixed a bug w.r.t. enabling interrupts too early
v3:
*) fixed a race with sinkref->prio
v2:
*) Added proper reference counting to prevent tasks from
deleting while a node->update() is still in flight
*) unified the RCU boost path
v1:
*) initial release
]
[
fyi -> you can find this series at the following URLs in addition to this thread:
http://git.kernel.org/?p=linux/kernel/git/ghaskins/linux-2.6-hacks.git;a=shortlog;h=pi-rework
ftp://ftp.novell.com/dev/ghaskins/pi-rework.tar.bz2
]
Hi All,
The following series applies to 26-rt1 as a request-for-comment on a new approach to priority-inheritance (PI), as well as some performance enhancements to take advantage of those new approaches. This yields at least a 13-15% improvement for diskio on my 4-way x86_64 system. An 8-way system saw as much as 700% improvement during early testing, but I have not recently reconfirmed this number.
Motivation for series:
I have several ideas on things we can do to enhance and improve kernel performance with respect to PREEMPT_RT
1) For instance, it would be nice to support priority queuing and (at least positional) inheritance in the wait-queue infrastructure.
2) Reducing overhead in the real-time locks (sleepable replacements for spinlock_t in PREEMPT_RT) to try to approach the minimal overhead if their non-rt equivalent. We have determined via instrumentation that one area of major overhead is the pi-boost logic.
However, today the PI code is entwined in the rtmutex infrastructure, yet we require more flexibility if we want to address (1) and (2) above. Therefore the first step is to separate the PI code away from rtmutex into its own library (libpi). This is covered in patches 1-7.
(I realize patch #7 is a little hard to review since I removed and added a lot of code that the unified diff is all mashing together...I will try to find a way to make this more readable).
Patch 8 is the first real consumer of the libpi logic to try to enhance performance. It accomplishes this by deferring pi-boosting a lock owner unless it is absolutely necessary. Since instrumentation shows that the majority of locks are acquired either via the fast-path, or via the adaptive-spin path, we can eliminate most of the pi-overhead with this technique. This yields a measurable performance gain (at least 13% for workloads with heavy lock contention was observed in our lab).
We have not yet completed the work on the pi-waitqueues or any of the other related pi enhancements. Those will be coming in a follow-on announcement.
Feedback/comments welcome!
Regards,
-Greg
---
Gregory Haskins (8):
rtmutex: pi-boost locks as late as possible
rtmutex: convert rtmutexes to fully use the PI library
rtmutex: use runtime init for rtmutexes
RT: wrap the rt_rwlock "add reader" logic
rtmutex: formally initialize the rt_mutex_waiters
sched: rework task reference counting to work with the pi infrastructure
sched: add the basic PI infrastructure to the task_struct add generalized priority-inheritance interface
Documentation/libpi.txt | 59 ++ include/linux/pi.h | 278 +++++++++++ include/linux/rt_lock.h | 2 include/linux/rtmutex.h | 18 - include/linux/sched.h | 57 +- include/linux/workqueue.h | 2 kernel/fork.c | 35 + kernel/rcupreempt-boost.c | 25 - kernel/rtmutex-debug.c | 4 kernel/rtmutex-tester.c | 4 kernel/rtmutex.c | 1091 ++++++++++++++++++--------------------------- kernel/rtmutex_common.h | 19 - kernel/rwlock_torture.c | 32 - kernel/sched.c | 209 ++++++--- kernel/workqueue.c | 39 +- lib/Makefile | 3 lib/pi.c | 516 +++++++++++++++++++++ 17 files changed, 1543 insertions(+), 850 deletions(-) create mode 100644 Documentation/libpi.txt create mode 100644 include/linux/pi.h create mode 100644 lib/pi.c
---
Gregory Haskins (8):
rtmutex: pi-boost locks as late as possible
rtmutex: convert rtmutexes to fully use the PI library
rtmutex: use runtime init for rtmutexes
RT: wrap the rt_rwlock "add reader" logic
rtmutex: formally initialize the rt_mutex_waiters
sched: rework task reference counting to work with the pi infrastructure
sched: add the basic PI infrastructure to the task_struct add generalized priority-inheritance interface
Documentation/libpi.txt | 59 ++ include/linux/pi.h | 293 ++++++++++++ include/linux/rt_lock.h | 2 include/linux/rtmutex.h | 18 - include/linux/sched.h | 59 +- include/linux/workqueue.h | 2 kernel/fork.c | 35 + kernel/rcupreempt-boost.c | 25 - kernel/rtmutex-debug.c | 4 kernel/rtmutex-tester.c | 4 kernel/rtmutex.c | 1091 ++++++++++++++++++--------------------------- kernel/rtmutex_common.h | 19 - kernel/rwlock_torture.c | 32 - kernel/sched.c | 207 ++++++--- kernel/workqueue.c | 39 +- lib/Makefile | 3 lib/pi.c | 489 ++++++++++++++++++++ 17 files changed, 1531 insertions(+), 850 deletions(-) create mode 100644 Documentation/libpi.txt create mode 100644 include/linux/pi.h create mode 100644 lib/pi.c
Signature · 1 line
Signature
RFC PATCH v4 1/8 add generalized priority-inheritance interface
The kernel currently addresses priority-inversion through priority- inheritence. However, all of the priority-inheritence logic is integrated into the Real-Time Mutex infrastructure. This causes a few problems:
1) This tightly coupled relationship makes it difficult to extend to other areas of the kernel (for instance, pi-aware wait-queues may be desirable).
2) Enhancing the rtmutex infrastructure becomes challenging because there is no seperation between the locking code, and the pi-code.
This patch aims to rectify these shortcomings by designing a stand-alone pi framework which can then be used to replace the rtmutex-specific version. The goal of this framework is to provide similar functionality to the existing subsystem, but with sole focus on PI and the relationships between objects that can boost priority, and the objects that get boosted.
We introduce the concept of a "pi_source" and a "pi_sink", where, as the name suggests provides the basic relationship of a priority source, and its boosted target. A pi_source acts as a reference to some arbitrary source of priority, and a pi_sink can be boosted (or deboosted) by a pi_source. For more details, please read the library documentation.
There are currently no users of this inteface.
---
Documentation/libpi.txt | 59 ++++++ include/linux/pi.h | 293 ++++++++++++++++++++++++++++ lib/Makefile | 3 lib/pi.c | 489 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 843 insertions(+), 1 deletions(-) create mode 100644 Documentation/libpi.txt create mode 100644 include/linux/pi.h create mode 100644 lib/pi.c
Documentation/libpi.txt new file +59 -0
@@ -0,0 +1,59 @@ +lib/pi.c - Priority Inheritance library + +Sources and sinks: +------------ + +This library introduces the basic concept of a "pi_source" and a "pi_sink", where, as the name suggests provides the basic relationship of a priority source, and its boosted target. + +A pi_source is simply a reference to some arbitrary priority value that may range from 0 (highest prio), to MAX_PRIO (currently 140, lowest prio). A pi_source calls pi_sink.boost() whenever it wishes to boost the sink to (at least minimally) the priority value that the source represents. It uses pi_sink.boost() for both the initial boosting, or for any subsequent refreshes to the value (even if the value is decreasing in logical priority). The policy of the sink will dictate what happens as a result of that boost. Likewise, a pi_source calls pi_sink.deboost() to stop contributing to the sink's minimum priority. + +It is important to note that a source is a reference to a priority value, not a value itself. This is one of the concepts that allows the interface to be idempotent, which is important for properly updating a chain of sources and sinks in the proper order. If we passed the priority on the stack, the order in which the system executes could allow the actual value that is set to race. + +Nodes: + +A pi_node is a convenience object which is simultaneously a source and a sink. As its name suggests, it would typically be deployed as a node in a pi-chain. Other pi_sources can boost a node via its pi_sink.boost() interface. Likewise, a node can boost a fixed number of sinks via the node.add_sink() interface. + +Generally speaking, a node takes care of many common operations associated with being a “link in the chain”, such as: + + 1) determining the current priority of the node based on the (logically) highest priority source that is boosting the node. + 2) boosting/deboosting upstream sinks whenever the node locally changes priority. + 3) taking care to avoid deadlock during a chain update. + +Design details: + +Destruction: + +The pi-library objects are designed to be implicitly-destructable (meaning they do not require an explicit “free()” operation when they are not used anymore). This is important considering their intended use (spinlock_t's which are also implicitly-destructable). As such, any allocations needed for operation must come from internal structure storage as there will be no opportunity to free it later. + +Multiple sinks per Node: + +We allow multiple sinks to be associated with a node. This is a slight departure from the previous implementation which had the notion of only a single sink (i.e. “task->pi_blocked_on”). The reason why we added the ability to add more than one sink was not to change the default chaining model (I.e. multiple boost targets), but rather to add a flexible notification mechanism that is peripheral to the chain, which are informally called “leaf sinks”. + +Leaf-sinks are boostable objects that do not perpetuate a chain per se. Rather, they act as endpoints to a priority boosting. Ultimately, every chain ends with a leaf-sink, which presumably will act on the new priority information. However, there may be any number of leaf-sinks along a chain as well. Each one will act on its localized priority in its own implementation specific way. For instance, a task_struct pi-leaf may change the priority of the task and reschedule it if necessary. Whereas an rwlock leaf-sink may boost a list of reader-owners. + +The following diagram depicts an example relationship (warning: cheesy ascii art) + + --------- --------- + | leaf | | leaf | + --------- --------- + / / + --------- / ---------- / --------- --------- + ->-| node |->---| node |-->---| node |->---| leaf | + --------- ---------- --------- --------- + +The reason why this was done was to unify the notion of a “sink” to a single interface, rather than having something like task->pi_blocks_on and a separate callback for the leaf action. Instead, any downstream object can be represented by a sink, and the implementation details are hidden (e.g. im a task, im a lock, im a node, im a work-item, im a wait-queue, etc). + +Sinkrefs: + +Each pi_sink.boost() operation is represented by a unique pi_source to properly facilitate a one node to many source relationship. Therefore, if a pi_node is to act as aggregator to multiple sinks, it implicitly must have one internal pi_source object for every sink that is added (via node.add_sink(). This pi_source object has to be internally managed for the lifetime of the sink reference. + +Recall that due to the implicit-destruction requirement above, and the fact that we will typically be executing in a preempt-disabled region, we have to be very careful about how we allocate references to those sinks. More on that next. But long story short we limit the number of sinks to MAX_PI_DEPENDENDICES (currently 5). + +Locking: + +(work in progress....) + + + + +
include/linux/pi.h new file +293 -0
293 lines not on this page · raw message
lib/Makefile +2 -1
@@ -23,7 +23,8 @@ lib-$(CONFIG_SMP) += cpumask.o lib-y += kobject.o kref.o klist.o obj-y += div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ - bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o + bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ + pi.o ifeq ($(CONFIG_DEBUG_KOBJECT),y) CFLAGS_kobject.o += -DDEBUG
lib/pi.c new file +489 -0
489 lines not on this page · raw message
--
To unsubscribe from this list: send the line "unsubscribe linux-rt-users" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH RT RFC v4 1/8] add generalized priority-inheritance interface
Quoted 830 lines · Gregory Haskins
The kernel currently addresses priority-inversion through priority- inheritence. However, all of the priority-inheritence logic is integrated into the Real-Time Mutex infrastructure. This causes a few problems:
1) This tightly coupled relationship makes it difficult to extend to other areas of the kernel (for instance, pi-aware wait-queues may be desirable).
2) Enhancing the rtmutex infrastructure becomes challenging because there is no seperation between the locking code, and the pi-code.This patch aims to rectify these shortcomings by designing a stand-alone pi framework which can then be used to replace the rtmutex-specific version. The goal of this framework is to provide similar functionality to the existing subsystem, but with sole focus on PI and the relationships between objects that can boost priority, and the objects that get boosted.
We introduce the concept of a "
pi_source" and a "pi_sink", where, as the name suggests provides the basic relationship of a priority source, and its boosted target. Api_sourceacts as a reference to some arbitrary source of priority, and api_sinkcan be boosted (or deboosted) by api_source. For more details, please read the library documentation.There are currently no users of this inteface.
Signed-off-by Gregory Haskins <[email protected]>---
Documentation/libpi.txt | 59 ++++++ include/linux/pi.h | 293 ++++++++++++++++++++++++++++ lib/Makefile | 3 lib/pi.c | 489 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 843 insertions(+), 1 deletions(-) create mode 100644 Documentation/libpi.txt create mode 100644 include/linux/pi.h create mode 100644 lib/pi.cDocumentation/libpi.txt new file +59
-0@@ -0,0 +1,59 @@ +lib/pi.c - Priority Inheritance library + +Sources and sinks: +------------ + +This library introduces the basic concept of a "pi_source" and a "pi_sink", where, as the name suggests provides the basic relationship of a priority source, and its boosted target. + +A pi_source is simply a reference to some arbitrary priority value that may range from 0 (highest prio), to MAX_PRIO (currently 140, lowest prio). A pi_source calls pi_sink.boost() whenever it wishes to boost the sink to (at least minimally) the priority value that the source represents. It uses pi_sink.boost() for both the initial boosting, or for any subsequent refreshes to the value (even if the value is decreasing in logical priority). The policy of the sink will dictate what happens as a result of that boost. Likewise, a pi_source calls pi_sink.deboost() to stop contributing to the sink's minimum priority. + +It is important to note that a source is a reference to a priority value, not a value itself. This is one of the concepts that allows the interface to be idempotent, which is important for properly updating a chain of sources and sinks in the proper order. If we passed the priority on the stack, the order in which the system executes could allow the actual value that is set to race. + +Nodes: + +A pi_node is a convenience object which is simultaneously a source and a sink. As its name suggests, it would typically be deployed as a node in a pi-chain. Other pi_sources can boost a node via its pi_sink.boost() interface. Likewise, a node can boost a fixed number of sinks via the node.add_sink() interface. + +Generally speaking, a node takes care of many common operations associated with being a “link in the chain”, such as: + + 1) determining the current priority of the node based on the (logically) highest priority source that is boosting the node. + 2) boosting/deboosting upstream sinks whenever the node locally changes priority. + 3) taking care to avoid deadlock during a chain update. + +Design details: + +Destruction: + +The pi-library objects are designed to be implicitly-destructable (meaning they do not require an explicit “free()” operation when they are not used anymore). This is important considering their intended use (spinlock_t's which are also implicitly-destructable). As such, any allocations needed for operation must come from internal structure storage as there will be no opportunity to free it later. + +Multiple sinks per Node: + +We allow multiple sinks to be associated with a node. This is a slight departure from the previous implementation which had the notion of only a single sink (i.e. “task->pi_blocked_on”). The reason why we added the ability to add more than one sink was not to change the default chaining model (I.e. multiple boost targets), but rather to add a flexible notification mechanism that is peripheral to the chain, which are informally called “leaf sinks”. + +Leaf-sinks are boostable objects that do not perpetuate a chain per se. Rather, they act as endpoints to a priority boosting. Ultimately, every chain ends with a leaf-sink, which presumably will act on the new priority information. However, there may be any number of leaf-sinks along a chain as well. Each one will act on its localized priority in its own implementation specific way. For instance, a task_struct pi-leaf may change the priority of the task and reschedule it if necessary. Whereas an rwlock leaf-sink may boost a list of reader-owners. + +The following diagram depicts an example relationship (warning: cheesy ascii art) + + --------- --------- + | leaf | | leaf | + --------- --------- + / / + --------- / ---------- / --------- --------- + ->-| node |->---| node |-->---| node |->---| leaf | + --------- ---------- --------- --------- + +The reason why this was done was to unify the notion of a “sink” to a single interface, rather than having something like task->pi_blocks_on and a separate callback for the leaf action. Instead, any downstream object can be represented by a sink, and the implementation details are hidden (e.g. im a task, im a lock, im a node, im a work-item, im a wait-queue, etc). + +Sinkrefs: + +Each pi_sink.boost() operation is represented by a unique pi_source to properly facilitate a one node to many source relationship. Therefore, if a pi_node is to act as aggregator to multiple sinks, it implicitly must have one internal pi_source object for every sink that is added (via node.add_sink(). This pi_source object has to be internally managed for the lifetime of the sink reference. + +Recall that due to the implicit-destruction requirement above, and the fact that we will typically be executing in a preempt-disabled region, we have to be very careful about how we allocate references to those sinks. More on that next. But long story short we limit the number of sinks to MAX_PI_DEPENDENDICES (currently 5). + +Locking: + +(work in progress....) + + + + +include/linux/pi.h new file +293
-0@@ -0,0 +1,293 @@ +/* +* see Documentation/libpi.txt for details +*/ + +#ifndef _LINUX_PI_H +#define _LINUX_PI_H + +#include <linux/list.h> +#include <linux/plist.h> +#include <asm/atomic.h> + +#define MAX_PI_DEPENDENCIES 5 + +struct pi_source { +struct plist_node list; +int *prio; +int boosted; +}; + + +#define PI_FLAG_DEFER_UPDATE (1 << 0) +#define PI_FLAG_ALREADY_BOOSTED (1 << 1) + +struct pi_sink ; + +struct pi_sink_ops { +int (*boost)(struct pi_sink *sink,struct pi_source *src, +unsigned int flags); +int (*deboost)(struct pi_sink *sink,struct pi_source *src, +unsigned int flags); +int (*update)(struct pi_sink *sink, +unsigned int flags); +int (*free)(struct pi_sink *sink, +unsigned int flags); +}; + +struct pi_sink { +atomic_t refs; +struct pi_sink_ops *ops; +}; + +enum pi_state { + pi_state_boost, + pi_state_boosted, + pi_state_deboost, + pi_state_free, +}; + +/* +* NOTE: PI must always use a true (e.g. raw) spinlock, since it is used by +* rtmutex infrastructure. +*/ + +struct pi_sinkref { +raw_spinlock_t lock; +struct list_head list; +enum pi_state state; +struct pi_sink *sink; +struct pi_source src; +atomic_t refs; +}; + +struct pi_sinkref_pool { +struct list_head free; +struct pi_sinkref data[MAX_PI_DEPENDENCIES ]; +}; + +struct pi_node { +raw_spinlock_t lock; +int prio; +struct pi_sink sink; +struct pi_sinkref_pool sinkref_pool; +struct list_head sinks; +struct plist_head srcs; +}; + +/** +* pi_node_init - initialize a pi_node before use +* @node: a node context +*/ +extern void pi_node_init (struct pi_node *node); + +/** +* pi_add_sink - add a sink as an downstream object +* @node: the node context +* @sink: the sink context to add to the node +* @flags: optional flags to modify behavior +* PI_FLAG_DEFER_UPDATE - Do not perform sync update +* PI_FLAG_ALREADY_BOOSTED - Do not perform initial boosting +* +* This function registers a sink to get notified whenever the +* node changes priority. +* +* Note: By default, this function will schedule the newly added sink +* to get an inital boost notification on the next update (even +* without the presence of a priority transition). However, if the +* ALREADY_BOOSTED flag is specified, the sink is initially marked as +* BOOSTED and will only get notified if the node changes priority +* in the future. +* +* Note: By default, this function will synchronously update the +* chain unless the DEFER_UPDATE flag is specified. +* +* Returns: (int) +* 0 = success +* any other value = failure +*/ +extern int pi_add_sink (struct pi_node *node,struct pi_sink *sink, +unsigned int flags); + +/** +* pi_del_sink - del a sink from the current downstream objects +* @node: the node context +* @sink: the sink context to delete from the node +* @flags: optional flags to modify behavior +* PI_FLAG_DEFER_UPDATE - Do not perform sync update +* +* This function unregisters a sink from the node. +* +* Note: The sink will not actually become fully deboosted until +* a call to node.update() successfully returns. +* +* Note: By default, this function will synchronously update the +* chain unless the DEFER_UPDATE flag is specified. +* +* Returns: (int) +* 0 = success +* any other value = failure +*/ +extern int pi_del_sink (struct pi_node *node,struct pi_sink *sink, +unsigned int flags); + +/** +* pi_sink_init - initialize a pi_sink before use +* @sink: a sink context +* @ops: pointer to an pi_sink_ops structure +*/ +static inline void +pi_sink_init (struct pi_sink *sink,struct pi_sink_ops *ops) +{ +atomic_set (&sink->refs,0 ); + sink->ops = ops; +} + +/** +* pi_source_init - initialize a pi_source before use +* @src: a src context +* @prio: pointer to a priority value +* +* A pointer to a priority value is used so that boost and update +* are fully idempotent. +*/ +static inline void +pi_source_init (struct pi_source *src,int *prio) +{ +plist_node_init (&src->list, *prio); + src->prio = prio; + src->boosted =0 ; +} + +/** +* pi_boost - boost a node with a pi_source +* @node: the node context +* @src: the src context to boost the node with +* @flags: optional flags to modify behavior +* PI_FLAG_DEFER_UPDATE - Do not perform sync update +* +* This function registers a priority source with the node, possibly +* boosting its value if the new source is the highest registered source. +* +* This function is used to both initially register a source, as well as +* to notify the node if the value changes in the future (even if the +* priority is decreasing). +* +* Note: By default, this function will synchronously update the +* chain unless the DEFER_UPDATE flag is specified. +* +* Returns: (int) +* 0 = success +* any other value = failure +*/ +static inline int +pi_boost (struct pi_node *node,struct pi_source *src,unsigned int flags) +{ +struct pi_sink *sink = &node->sink; + +if (sink->ops->boost) +return sink->ops->boost (sink, src, flags); + +return 0 ; +} + +/** +* pi_deboost - deboost a pi_source from a node +* @node: the node context +* @src: the src context to boost the node with +* @flags: optional flags to modify behavior +* PI_FLAG_DEFER_UPDATE - Do not perform sync update +* +* This function unregisters a priority source from the node, possibly +* deboosting its value if the departing source was the highest +* registered source. +* +* Note: By default, this function will synchronously update the +* chain unless the DEFER_UPDATE flag is specified. +* +* Returns: (int) +* 0 = success +* any other value = failure +*/ +static inline int +pi_deboost (struct pi_node *node,struct pi_source *src,unsigned int flags) +{ +struct pi_sink *sink = &node->sink; + +if (sink->ops->deboost) +return sink->ops->deboost (sink, src, flags); + +return 0 ; +} + +/** +* pi_update - force a manual chain update +* @node: the node context +* @flags: optional flags to modify behavior. Reserved, must be 0. +* +* This function will push any priority changes (as a result of +* boost/deboost or add_sink/del_sink) down through the chain. +* If no changes are necessary, this function is a no-op. +* +* Returns: (int) +* 0 = success +* any other value = failure +*/ +static inline int +pi_update (struct pi_node *node,unsigned int flags) +{ +struct pi_sink *sink = &node->sink; + +if (sink->ops->update) +return sink->ops->update (sink, flags); + +return 0 ; +} + +/** +* pi_sink_put - down the reference count, freeing the sink if 0 +* @node: the node context +* @flags: optional flags to modify behavior. Reserved, must be 0. +* +* Returns: none +*/ +static inline void +pi_sink_put (struct pi_sink *sink,unsigned int flags) +{ +if (atomic_dec_and_test (&sink->refs)) { +if (sink->ops->free) + sink->ops->free (sink, flags); + } +} + + +/** +* pi_get - up the reference count +* @node: the node context +* @flags: optional flags to modify behavior. Reserved, must be 0. +* +* Returns: none +*/ +static inline void +pi_get (struct pi_node *node,unsigned int flags) +{ +struct pi_sink *sink = &node->sink; + +atomic_inc (&sink->refs); +} + +/** +* pi_put - down the reference count, freeing the node if 0 +* @node: the node context +* @flags: optional flags to modify behavior. Reserved, must be 0. +* +* Returns: none +*/ +static inline void +pi_put (struct pi_node *node,unsigned int flags) +{ +struct pi_sink *sink = &node->sink; + +pi_sink_put (sink, flags); +} + +#endif /* _LINUX_PI_H */ lib/Makefile +2
-1@@ -23,7 +23,8 @@ lib-$(CONFIG_SMP) += cpumask.o lib-y += kobject.o kref.o klist.o obj-y += div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ - bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o + bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ + pi.o ifeq ($(CONFIG_DEBUG_KOBJECT),y) CFLAGS_kobject.o += -DDEBUGlib/pi.c new file +407
-0@@ -0,0 +1,489 @@ +/* +* lib/pi.c +* +* Priority-Inheritance library +* +* Copyright (C) 2008 Novell +* +* Author: Gregory Haskins <[email protected]> +* +* This code provides a generic framework for preventing priority +* inversion by means of priority-inheritance. (see Documentation/libpi.txt +* for details) +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; version 2 +* of the License. +*/ + +#include <linux/sched.h> +#include <linux/module.h> +#include <linux/pi.h> + + +struct updater { +int update; +struct pi_sinkref *sinkref; +struct pi_sink *sink; +}; + +/* +*----------------------------------------------------------- +* pi_sinkref_pool +*----------------------------------------------------------- +*/ + +static void +pi_sinkref_pool_init (struct pi_sinkref_pool *pool) +{ +int i; + +INIT_LIST_HEAD (&pool->free); + +for (i =0 ; i <MAX_PI_DEPENDENCIES ; ++i) { +struct pi_sinkref *sinkref = &pool->data[i]; + +memset (sinkref,0 ,sizeof (*sinkref)); +INIT_LIST_HEAD (&sinkref->list); +list_add_tail (&sinkref->list, &pool->free); + } +} + +static struct pi_sinkref * +pi_sinkref_alloc (struct pi_sinkref_pool *pool) +{ +struct pi_sinkref *sinkref; + +if (list_empty (&pool->free)) +return NULL ; + + sinkref = list_first_entry(&pool->free, struct pi_sinkref, list); +list_del (&sinkref->list); +memset (sinkref,0 ,sizeof (*sinkref)); + +return sinkref; +} + +static void +pi_sinkref_free (struct pi_sinkref_pool *pool, +struct pi_sinkref *sinkref) +{ +list_add_tail (&sinkref->list, &pool->free); +} + +/* +*----------------------------------------------------------- +* pi_sinkref +*----------------------------------------------------------- +*/ + +static inline void +_pi_sink_get (struct pi_sinkref *sinkref) +{ +atomic_inc (&sinkref->sink->refs); +atomic_inc (&sinkref->refs); +} + +static inline void +_pi_sink_put_local (struct pi_node *node,struct pi_sinkref *sinkref) +{ +if (atomic_dec_and_lock (&sinkref->refs, &node->lock)) { +list_del (&sinkref->list); +pi_sinkref_free (&node->sinkref_pool, sinkref); +spin_unlock (&node->lock); + } +} + +static inline void +_pi_sink_put_all (struct pi_node *node,struct pi_sinkref *sinkref) +{ +struct pi_sink *sink = sinkref->sink; + +_pi_sink_put_local (node, sinkref); +pi_sink_put (sink,0 ); +} + +/* +*----------------------------------------------------------- +* pi_node +*----------------------------------------------------------- +*/ + +static struct pi_node *node_of (struct pi_sink *sink) +{ + return container_of(sink, struct pi_node, sink); +} + +static inline void +__pi_boost (struct pi_node *node,struct pi_source *src) +{ +BUG_ON (src->boosted); + +plist_node_init (&src->list, *src->prio); +plist_add (&src->list, &node->srcs); + src->boosted =1 ; +} + +static inline void +__pi_deboost (struct pi_node *node,struct pi_source *src) +{ +BUG_ON (!src->boosted); + +plist_del (&src->list, &node->srcs); + src->boosted =0 ; +} + +/* +* _pi_node_update - update the chain +* +* We loop through up to MAX_PI_DEPENDENCIES times looking for stale entries +* that need to propagate up the chain. This is a step-wise process where we +* have to be careful about locking and preemption. By trying MAX_PI_DEPs +* times, we guarantee that this update routine is an effective barrier... +* all modifications made prior to the call to this barrier will have completed. +* +* Deadlock avoidance: This node may participate in a chain of nodes which +* form a graph of arbitrary structure. While the graph should technically +* never close on itself barring any bugs, we still want to protect against +* a theoretical ABBA deadlock (if for nothing else, to prevent lockdep +* from detecting this potential). To do this, we employ a dual-locking +* scheme where we can carefully control the order. That is: node->lock +* protects most of the node's internal state, but it will never be held +* across a chain update. sinkref->lock, on the other hand, can be held +* across a boost/deboost, and also guarantees proper execution order. Also +* note that no locks are held across an sink->update. +*/ +static int +_pi_node_update (struct pi_sink *sink,unsigned int flags) +{ +struct pi_node *node =node_of (sink); +struct pi_sinkref *sinkref; +unsigned long iflags; +int count =0 ; +int i; +int pprio; +struct updater updaters[MAX_PI_DEPENDENCIES ]; + +spin_lock_irqsave (&node->lock, iflags); + + pprio = node->prio; + +if (!plist_head_empty (&node->srcs)) + node->prio =plist_first (&node->srcs)->prio; +else + node->prio =MAX_PRIO ; + +list_for_each_entry (sinkref, &node->sinks, list) { +/* +* If the priority is changing, or if this is a +* BOOST/DEBOOST, we consider this sink "stale" +*/ +if (pprio != node->prio + || sinkref->state != pi_state_boosted) { +struct updater *iter = &updaters[count++]; + +BUG_ON (!atomic_read (&sinkref->sink->refs)); +_pi_sink_get (sinkref); + + iter->update =1 ; + iter->sinkref = sinkref; + iter->sink = sinkref->sink; + } + } + +spin_unlock (&node->lock); + +for (i =0 ; i < count; ++i) { +struct updater *iter = &updaters[i]; +unsigned int lflags =PI_FLAG_DEFER_UPDATE ; +struct pi_sink *sink; + + sinkref = iter->sinkref; + sink = iter->sink; + +spin_lock (&sinkref->lock); + +switch (sinkref->state) { +case pi_state_boost: + sinkref->state = pi_state_boosted; +/* Fall through */ +case pi_state_boosted: + sink->ops->boost (sink, &sinkref->src, lflags); +break ; +case pi_state_deboost: + sink->ops->deboost (sink, &sinkref->src, lflags); + sinkref->state = pi_state_free; + +/* +* drop the ref that we took when the sinkref +* was allocated. We still hold a ref from +* above. +*/ +_pi_sink_put_all (node, sinkref); +break ; +case pi_state_free: + iter->update =0 ; +break ; +default : +panic ("illegal sinkref type: %d" , sinkref->state); + } + +spin_unlock (&sinkref->lock); + +/* +* We will drop the sinkref reference while still holding the +* preempt/irqs off so that the memory is returned synchronously +* to the system. +*/ +_pi_sink_put_local (node, sinkref); + } + +local_irq_restore (iflags); + +/* +* Note: At this point, sinkref is invalid since we put'd +* it above, but sink is valid since we still hold the remote +* reference. This is key to the design because it allows us +* to synchronously free the sinkref object, yet maintain a +* reference to the sink across the update +*/ +for (i =0 ; i < count; ++i) { +struct updater *iter = &updaters[i]; + +if (iter->update) + iter->sink->ops->update (iter->sink,0 ); + } + +/* +* We perform all the free opertations together at the end, using +* only automatic/stack variables since any one of these operations +* could result in our node object being deallocated +*/ +for (i =0 ; i < count; ++i) { +struct updater *iter = &updaters[i]; + +pi_sink_put (iter->sink,0 ); + } + +return 0 ; +} + +static int +_pi_del_sink (struct pi_node *node,struct pi_sink *sink,unsigned int flags) +{ +struct pi_sinkref *sinkref; +struct updater updaters[MAX_PI_DEPENDENCIES ]; +unsigned long iflags; +int count =0 ; +int i; + +local_irq_save (iflags); +spin_lock (&node->lock); + +list_for_each_entry (sinkref, &node->sinks, list) { +if (!sink || sink == sinkref->sink) { +struct updater *iter = &updaters[count++]; + +_pi_sink_get (sinkref); + iter->sinkref = sinkref; + iter->sink = sinkref->sink; + } + } + +spin_unlock (&node->lock); + +for (i =0 ; i < count; ++i) { +struct updater *iter = &updaters[i]; +int remove =0 ; + + sinkref = iter->sinkref; + +spin_lock (&sinkref->lock); + +switch (sinkref->state) { +case pi_state_boost: +/* +* This state indicates the sink was never formally +* boosted so we can just delete it immediately +*/ + remove =1 ; +break ; +case pi_state_boosted: +if (sinkref->sink->ops->deboost) +/* +* If the sink supports deboost notification, +* schedule it for deboost at the next update +*/ + sinkref->state = pi_state_deboost; +else +/* +* ..otherwise schedule it for immediate +* removal +*/ + remove =1 ; +break ; +default : +break ; + } + +if (remove) { +/* +* drop the ref that we took when the sinkref +* was allocated. We still hold a ref from +* above +*/ +_pi_sink_put_all (node, sinkref); + sinkref->state = pi_state_free; + } + +spin_unlock (&sinkref->lock); + +_pi_sink_put_local (node, sinkref); + } + +local_irq_restore (iflags); + +for (i =0 ; i < count; ++i) +pi_sink_put (updaters[i].sink,0 ); + +if (!(flags &PI_FLAG_DEFER_UPDATE )) +_pi_node_update (&node->sink,0 ); + +return 0 ; +} + +static int +_pi_node_boost (struct pi_sink *sink,struct pi_source *src, +unsigned int flags) +{ +struct pi_node *node =node_of (sink); +unsigned long iflags; + +spin_lock_irqsave (&node->lock, iflags); +if (src->boosted) +__pi_deboost (node, src); +__pi_boost (node, src); +spin_unlock_irqrestore (&node->lock, iflags); + +if (!(flags &PI_FLAG_DEFER_UPDATE )) +_pi_node_update (sink,0 ); + +return 0 ; +} + +static int +_pi_node_deboost (struct pi_sink *sink,struct pi_source *src, +unsigned int flags) +{ +struct pi_node *node =node_of (sink); +unsigned long iflags; + +spin_lock_irqsave (&node->lock, iflags); +__pi_deboost (node, src); +spin_unlock_irqrestore (&node->lock, iflags); + +if (!(flags &PI_FLAG_DEFER_UPDATE )) +_pi_node_update (sink,0 ); + +return 0 ; +} + +static int +_pi_node_free (struct pi_sink *sink,unsigned int flags) +{ +struct pi_node *node =node_of (sink); + +/* +* When the node is freed, we should perform an implicit +* del_sink on any remaining sinks we may have. +*/ +return _pi_del_sink (node,NULL , flags); +} + +static struct pi_sink_ops pi_node_sink = { + .boost = _pi_node_boost, + .deboost = _pi_node_deboost, + .update = _pi_node_update,
lib/pi.c line 417
Quoted 10 lines
lib/pi.c new file +10
-0+ .free = _pi_node_free, +}; + +void +pi_node_init (struct pi_node *node) +{ +spin_lock_init (&node->lock); + node->prio =MAX_PRIO ; +atomic_set (&node->sink.refs,1 ); + node->sink.ops = &pi_node_sink;
^^^^^^
Note to self: this should use pi_sink_init()
Quoted 64 lines
lib/pi.c new file +62
-0+pi_sinkref_pool_init (&node->sinkref_pool); +INIT_LIST_HEAD (&node->sinks); +plist_head_init (&node->srcs, &node->lock); +} + +int +pi_add_sink (struct pi_node *node,struct pi_sink *sink,unsigned int flags) +{ +struct pi_sinkref *sinkref; +int ret =0 ; +unsigned long iflags; + +spin_lock_irqsave (&node->lock, iflags); + +if (!atomic_read (&node->sink.refs)) { + ret = -EINVAL ; + goto out; + } + + sinkref =pi_sinkref_alloc (&node->sinkref_pool); +if (!sinkref) { + ret = -ENOMEM ; + goto out; + } + +spin_lock_init (&sinkref->lock); +INIT_LIST_HEAD (&sinkref->list); + +if (flags &PI_FLAG_ALREADY_BOOSTED ) + sinkref->state = pi_state_boosted; +else +/* +* Schedule it for addition at the next update +*/ + sinkref->state = pi_state_boost; + +pi_source_init (&sinkref->src, &node->prio); + sinkref->sink = sink; + +/* set one ref from ourselves. It will be dropped on del_sink */ +atomic_inc (&sinkref->sink->refs); +atomic_set (&sinkref->refs,1 ); + +list_add_tail (&sinkref->list, &node->sinks); + +spin_unlock_irqrestore (&node->lock, iflags); + +if (!(flags &PI_FLAG_DEFER_UPDATE )) +_pi_node_update (&node->sink,0 ); + +return 0 ; + + out: +spin_unlock_irqrestore (&node->lock, iflags); + +return ret; +} + +int +pi_del_sink (struct pi_node *node,struct pi_sink *sink,unsigned int flags) +{ +/*
lib/pi.c line 489
Quoted 10 lines
lib/pi.c new file +10
-0+* There may be multiple matches to sink because sometimes a +* deboost/free may still be pending an update when the same +* node has been added. So we want to process any and all +* instances that match our target +*/ +return _pi_del_sink (node, sink, flags); +} + + +
--
To unsubscribe from this list: send the line "unsubscribe linux-rt-users" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
AW: [PATCH RT RFC v4 1/8] add generalized priority-inheritance interface
In reply to Gregory HaskinsHi Greg,
I got a few review comments/questions. Pls see below.
Best Regards,
Matthias
P.S. I'm a kernel newbie so don't hesitate to tell me if I'm wrong ;-)
include/linux/pi.h line 143
Quoted 11 lines
include/linux/pi.h new file +11
-0+/** +* pi_sink_init - initialize a pi_sink before use +* @sink: a sink context +* @ops: pointer to an pi_sink_ops structure +*/ +static inline void +pi_sink_init (struct pi_sink *sink,struct pi_sink_ops *ops) +{ +atomic_set (&sink->refs,0 ); + sink->ops = ops; +}
Shouldn't ops be tested for 0 here? (ASSERT/BUG_ON/...) (get's dereferenced later quite often in the form "if (sink->ops->...)".
Quoted 5 lines
include/linux/pi.h new file +5
-0+/** +* pi_sink_put - down the reference count, freeing the sink if 0 +* @node: the node context +* @flags: optional flags to modify behavior. Reserved, must be 0. +*
include/linux/pi.h line 260
Quoted 10 lines
include/linux/pi.h new file +10
-0+* Returns: none +*/ +static inline void +pi_sink_put (struct pi_sink *sink,unsigned int flags) +{ +if (atomic_dec_and_test (&sink->refs)) { +if (sink->ops->free) + sink->ops->free (sink, flags); + } +}
Shouldn't the atomic/locked part cover the ...->free(...) as well? A pi_get right after the atomic_dec_and_test but before the free() could lead to a free() with refs>0?
Re: AW: [PATCH RT RFC v4 1/8] add generalized priority-inheritance interface
Hi Matthias,
Quoted 10 lines · Matthias Behr
Hi Greg,
I got a few review comments/questions. Pls see below.
Best Regards,
Matthias
P.S. I'm a kernel newbie so don't hesitate to tell me if I'm wrong ;-)
Quoted 15 lines
include/linux/pi.h line 143
Quoted 12 lines
include/linux/pi.h new file +11
-0+/** +* pi_sink_init - initialize a pi_sink before use +* @sink: a sink context +* @ops: pointer to an pi_sink_ops structure +*/ +static inline void +pi_sink_init (struct pi_sink *sink,struct pi_sink_ops *ops) +{ +atomic_set (&sink->refs,0 ); + sink->ops = ops; +}Shouldn't ops be tested for 0 here? (ASSERT/BUG_ON/...) (get's dereferenced later quite often in the form "if (sink->ops->...)".
This is a good idea. I will add this.
Quoted 11 lines
Quoted 11 lines
include/linux/pi.h new file +10
-0+/** +* pi_sink_put - down the reference count, freeing the sink if 0 +* @node: the node context +* @flags: optional flags to modify behavior. Reserved, must be 0. +* +* Returns: none +*/ +static inline void +pi_sink_put (struct pi_sink *sink,unsigned int flags) +{
Quoted 8 lines
include/linux/pi.h line 260
Quoted 6 lines
include/linux/pi.h new file +5
-0+if (atomic_dec_and_test (&sink->refs)) { +if (sink->ops->free) + sink->ops->free (sink, flags); + } +}Shouldn't the atomic/locked part cover the ...->free(...) as well?
Actually, it already does. The free can only be called by the last reference dropping the ref-count.
Quoted 2 lines
A
pi_getright after theatomic_dec_and_testbut before thefree()could lead to afree()with refs>0?
A pi_get() after the ref could have already dropped to zero is broken at a higher layer. E.g. the caller of pi_get() has to ensure that there are no races against the reference dropping to begin with. This is the same as any reference-counted object (for instance, see get_task_struct()).
Thanks for the review, Matthias!
Regards,
-Greg
Peter Zijlstra
maintainerRe: [PATCH RT RFC v4 1/8] add generalized priority-inheritance interface
In reply to Gregory HaskinsQuoted 18 lines · Gregory Haskins
The kernel currently addresses priority-inversion through priority- inheritence. However, all of the priority-inheritence logic is integrated into the Real-Time Mutex infrastructure. This causes a few problems:
1) This tightly coupled relationship makes it difficult to extend to other areas of the kernel (for instance, pi-aware wait-queues may be desirable).
2) Enhancing the rtmutex infrastructure becomes challenging because there is no seperation between the locking code, and the pi-code.This patch aims to rectify these shortcomings by designing a stand-alone pi framework which can then be used to replace the rtmutex-specific version. The goal of this framework is to provide similar functionality to the existing subsystem, but with sole focus on PI and the relationships between objects that can boost priority, and the objects that get boosted.
Quoted 7 lines
We introduce the concept of a "
pi_source" and a "pi_sink", where, as the name suggests provides the basic relationship of a priority source, and its boosted target. Api_sourceacts as a reference to some arbitrary source of priority, and api_sinkcan be boosted (or deboosted) by api_source. For more details, please read the library documentation.There are currently no users of this inteface.
You should have started out by discussing your design - the document just rambles a bit about some implementation details - it doesn't talk about how it maps to the PI problem space.
Anyway - from what I can make of the code, you managed to convert the pi graph walking code that used to be in rt_mutex_adjust_prio_chain() and was iterative, into a recursive function call.
Not something you should do lightly..
Re: [PATCH RT RFC v4 1/8] add generalized priority-inheritance interface
Hi Peter,
Quoted 26 lines · Peter Zijlstra
Quoted 24 lines · Gregory Haskins
The kernel currently addresses priority-inversion through priority- inheritence. However, all of the priority-inheritence logic is integrated into the Real-Time Mutex infrastructure. This causes a few problems:
1) This tightly coupled relationship makes it difficult to extend to other areas of the kernel (for instance, pi-aware wait-queues may be desirable).
2) Enhancing the rtmutex infrastructure becomes challenging because there is no seperation between the locking code, and the pi-code.This patch aims to rectify these shortcomings by designing a stand-alone pi framework which can then be used to replace the rtmutex-specific version. The goal of this framework is to provide similar functionality to the existing subsystem, but with sole focus on PI and the relationships between objects that can boost priority, and the objects that get boosted.
We introduce the concept of a "
pi_source" and a "pi_sink", where, as the name suggests provides the basic relationship of a priority source, and its boosted target. Api_sourceacts as a reference to some arbitrary source of priority, and api_sinkcan be boosted (or deboosted) by api_source. For more details, please read the library documentation.
Quoted 7 lines
Quoted 2 lines
There are currently no users of this inteface.
You should have started out by discussing your design - the document just rambles a bit about some implementation details - it doesn't talk about how it maps to the PI problem space.
The doc is still a work-in-progress, but point taken ;) I will address this shortly.
Quoted 6 lines
Anyway - from what I can make of the code, you managed to convert the pi graph walking code that used to be in
rt_mutex_adjust_prio_chain()and was iterative, into a recursive function call.Not something you should do lightly..
As we discussed on IRC yesterday, you are correct here. I was thinking that the graph couldn't get deeper than a few dozen entries, but I forgot about userspace futex access. But, this is precisely what the "release early" policy is designed to catch ;)
I think I can make a slight adjustment to the model to return it to an iterative design. I will address this in v5.
Thanks for the review, Peter!
Regards,
-Greg
Re: [PATCH RT RFC v4 1/8] add generalized priority-inheritance interface
In reply to Gregory HaskinsDisclaimer: I am no longer actively involved and I must admit I might have lost out on much of
what have been going on since I contributed to the PI system 2 years ago. But I allow myself to comment
anyway.
Quoted 11 lines · Gregory Haskins
The kernel currently addresses priority-inversion through priority- inheritence. However, all of the priority-inheritence logic is integrated into the Real-Time Mutex infrastructure. This causes a few problems:
1) This tightly coupled relationship makes it difficult to extend to other areas of the kernel (for instance, pi-aware wait-queues may be desirable).
2) Enhancing the rtmutex infrastructure becomes challenging because there is no seperation between the locking code, and the pi-code.
Quoted 6 lines
This patch aims to rectify these shortcomings by designing a stand-alone pi framework which can then be used to replace the rtmutex-specific version. The goal of this framework is to provide similar functionality to the existing subsystem, but with sole focus on PI and the relationships between objects that can boost priority, and the objects that get boosted.
This is really a good idea. When I had time (2 years ago) to actively work on these problem
I also came to the conclusion that PI should be more general than just the rtmutex. Preemptive RCU
was the example which drove it.
But I do disagree that general objects should get boosted: The end targets are always tasks. The objects might
be boosted as intermediate steps, but priority end the only applies to tasks.
I also have a few comments to the actual design:
Documentation/libpi.txt line 32
Quoted 7 lines
....
Documentation/libpi.txt new file +6
-0+ +Multiple sinks per Node: + +We allow multiple sinks to be associated with a node. This is a slight departure from the previous implementation which had the notion of only a single sink (i.e. "task->pi_blocked_on"). The reason why we added the ability to add more than one sink was not to change the default chaining model (I.e. multiple boost targets), but rather to add a flexible notification mechanism that is peripheral to the chain, which are informally called "leaf sinks". + +Leaf-sinks are boostable objects that do not perpetuate a chain per se. Rather, they act as endpoints to a priority boosting. Ultimately, every chain ends with a leaf-sink, which presumably will act on the new priority information. However, there may be any number of leaf-sinks along a chain as well. Each one will act on its localized priority in its own implementation specific way. For instance, a task_struct pi-leaf may change the priority of the task and reschedule it if necessary. Whereas an rwlock leaf-sink may boost a list of reader-owners.
This is bad from a RT point of view: You have a hard time determininig the number of sinks per node. An rw-lock could have an arbitrary number of readers (is supposed to really). Therefore
you have no chance of knowing how long the boost/deboost operation will take. And you also know for how long the boosted tasks stay boosted. If there can be an arbitrary number of
such tasks you can no longer be deterministic.
Review
Quoted 3 lines
...
(quoted diff) +2
-0+ +#define MAX_PI_DEPENDENCIES 5
WHAT??? There is a finite lock depth defined. I know we did that originally but it wasn't hardcoded (as far as I remember) and it was certainly not as low as 5.
Remember: PI is used by the user space futeces as well!
Quoted 39 lines
....
lib/pi.c new file +38
-0+/* +* _pi_node_update - update the chain +* +* We loop through up to MAX_PI_DEPENDENCIES times looking for stale entries +* that need to propagate up the chain. This is a step-wise process where we +* have to be careful about locking and preemption. By trying MAX_PI_DEPs +* times, we guarantee that this update routine is an effective barrier... +* all modifications made prior to the call to this barrier will have completed. +* +* Deadlock avoidance: This node may participate in a chain of nodes which +* form a graph of arbitrary structure. While the graph should technically +* never close on itself barring any bugs, we still want to protect against +* a theoretical ABBA deadlock (if for nothing else, to prevent lockdep +* from detecting this potential). To do this, we employ a dual-locking +* scheme where we can carefully control the order. That is: node->lock +* protects most of the node's internal state, but it will never be held +* across a chain update. sinkref->lock, on the other hand, can be held +* across a boost/deboost, and also guarantees proper execution order. Also +* note that no locks are held across an sink->update. +*/ +static int +_pi_node_update (struct pi_sink *sink,unsigned int flags) +{ +struct pi_node *node =node_of (sink); +struct pi_sinkref *sinkref; +unsigned long iflags; +int count =0 ; +int i; +int pprio; +struct updater updaters[MAX_PI_DEPENDENCIES ]; + +spin_lock_irqsave (&node->lock, iflags); + + pprio = node->prio; + +if (!plist_head_empty (&node->srcs)) + node->prio =plist_first (&node->srcs)->prio; +else
lib/pi.c line 184
Quoted 10 lines
lib/pi.c new file +10
-0+ node->prio =MAX_PRIO ; + +list_for_each_entry (sinkref, &node->sinks, list) { +/* +* If the priority is changing, or if this is a +* BOOST/DEBOOST, we consider this sink "stale" +*/ +if (pprio != node->prio + || sinkref->state != pi_state_boosted) { +struct updater *iter = &updaters[count++];
What prevents count from overrun?
Quoted 48 lines
lib/pi.c new file +48
-0+ +BUG_ON (!atomic_read (&sinkref->sink->refs)); +_pi_sink_get (sinkref); + + iter->update =1 ; + iter->sinkref = sinkref; + iter->sink = sinkref->sink; + } + } + +spin_unlock (&node->lock); + +for (i =0 ; i < count; ++i) { +struct updater *iter = &updaters[i]; +unsigned int lflags =PI_FLAG_DEFER_UPDATE ; +struct pi_sink *sink; + + sinkref = iter->sinkref; + sink = iter->sink; + +spin_lock (&sinkref->lock); + +switch (sinkref->state) { +case pi_state_boost: + sinkref->state = pi_state_boosted; +/* Fall through */ +case pi_state_boosted: + sink->ops->boost (sink, &sinkref->src, lflags); +break ; +case pi_state_deboost: + sink->ops->deboost (sink, &sinkref->src, lflags); + sinkref->state = pi_state_free; + +/* +* drop the ref that we took when the sinkref +* was allocated. We still hold a ref from +* above. +*/ +_pi_sink_put_all (node, sinkref); +break ; +case pi_state_free: + iter->update =0 ; +break ; +default : +panic ("illegal sinkref type: %d" , sinkref->state); + } + +spin_unlock (&sinkref->lock);
lib/pi.c line 242
Quoted 10 lines
lib/pi.c new file +10
-0+ +/* +* We will drop the sinkref reference while still holding the +* preempt/irqs off so that the memory is returned synchronously +* to the system. +*/ +_pi_sink_put_local (node, sinkref); + } + +local_irq_restore (iflags);
Yack! You keep interrupts off while doing the chain. I think my main contribution to the PI system 2 years ago was to do this preemptively. I.e. there was points in the loop where interrupts and preemption where turned on.
Remember: It goes into user space again. An evil user could craft an application with a very long lock depth and keep higher priority real time tasks from running for an arbitrary long time (if
no limit on the lock depth is set, which is bad because it will be too low in some cases.)
But as I said I have had no time to watch what has actually been going on in the kernel for the last 2 years roughly. The said defects might have creeped in by other contributers already :-(
Esben
Re: [PATCH RT RFC v4 1/8] add generalized priority-inheritance interface
Hi Esben,
Thank you for the review. Comments inline.
Quoted 33 lines · Esben Nielsen
Disclaimer: I am no longer actively involved and I must admit I might have lost out on much of
what have been going on since I contributed to the PI system 2 years ago. But I allow myself to comment
anyway.Quoted 11 lines · Gregory Haskins
The kernel currently addresses priority-inversion through priority- inheritence. However, all of the priority-inheritence logic is integrated into the Real-Time Mutex infrastructure. This causes a few problems:
1) This tightly coupled relationship makes it difficult to extend to other areas of the kernel (for instance, pi-aware wait-queues may be desirable).
2) Enhancing the rtmutex infrastructure becomes challenging because there is no seperation between the locking code, and the pi-code.Quoted 7 lines
This patch aims to rectify these shortcomings by designing a stand-alone pi framework which can then be used to replace the rtmutex-specific version. The goal of this framework is to provide similar functionality to the existing subsystem, but with sole focus on PI and the relationships between objects that can boost priority, and the objects that get boosted.
This is really a good idea. When I had time (2 years ago) to actively work on these problem
I also came to the conclusion that PI should be more general than just the rtmutex. Preemptive RCU
was the example which drove it.
Quoted 4 lines
But I do disagree that general objects should get boosted: The end targets are always tasks. The objects might
be boosted as intermediate steps, but priority end the only applies to tasks.
Actually I fully agree with you here. Its probably just poor wording on my part, but this is exactly what happens. We may "boost" arbitrary objects on the way to boosting a task...but the intermediate objects are just there to help find our way to the proper tasks. Ultimately everything ends up at the scheduler eventually ;)
Quoted 20 lines
I also have a few comments to the actual design:
Documentation/libpi.txt line 32
Quoted 8 lines
....
Documentation/libpi.txt new file +6
-0+ +Multiple sinks per Node: + +We allow multiple sinks to be associated with a node. This is a slight departure from the previous implementation which had the notion of only a single sink (i.e. "task->pi_blocked_on"). The reason why we added the ability to add more than one sink was not to change the default chaining model (I.e. multiple boost targets), but rather to add a flexible notification mechanism that is peripheral to the chain, which are informally called "leaf sinks". + +Leaf-sinks are boostable objects that do not perpetuate a chain per se. Rather, they act as endpoints to a priority boosting. Ultimately, every chain ends with a leaf-sink, which presumably will act on the new priority information. However, there may be any number of leaf-sinks along a chain as well. Each one will act on its localized priority in its own implementation specific way. For instance, a task_struct pi-leaf may change the priority of the task and reschedule it if necessary. Whereas an rwlock leaf-sink may boost a list of reader-owners.This is bad from a RT point of view: You have a hard time determininig the number of sinks per node. An rw-lock could have an arbitrary number of readers (is supposed to really). Therefore
you have no chance of knowing how long the boost/deboost operation will take. And you also know for how long the boosted tasks stay boosted. If there can be an arbitrary number of
such tasks you can no longer be deterministic.
While you may have a valid concern about what rwlocks can do to determinism, not that we already have PI enabled rwlocks before my patch, so I am not increasing nor decreasing determinism in this regard. That being said, Steven Rostedt (author of the pi-rwlocks, CC'd) has facilities to manage this (such as limiting the number of readers to num_online_cpus) which this design would retain. Long story short, I do not believe I have made anything worse here, so this is a different discussion if you are still concerned.
Quoted 11 lines
Review
Quoted 5 lines
...
(quoted diff) +2
-0+ +#define MAX_PI_DEPENDENCIES 5WHAT??? There is a finite lock depth defined. I know we did that originally but it wasn't hardcoded (as far as I remember) and it was certainly not as low as 5.
Note that this is simply in reference to how many direct sinks you can link to a node, not how long the resulting chain can grow. The chain depth is actually completely unconstrained by the design. I chose "5" here because typically we need 1 sink for the next link in the chain, and 1 sink for local notifications. The other 3 are there for head-room (we often hit 3-4 as we transition between nodes (add one node -> delete another, etc).
You are not the first to comment about this, however, so it makes me realize it is not very clear ;) I will comment the code better.
Quoted 2 lines
Remember: PI is used by the user space futeces as well!
Yes, and on a slight tangent from your point, this incidentally is actually a problem in the design such that I need to respin at least a v5. My current design uses recursion against the sink->update() methods, which Peter Zijlstra pointed out would blow up with large userpspace chains. My next version will forgo the recursion in favor of an iterative method more reminiscent of the original design.
Quoted 45 lines
Quoted 45 lines
....
lib/pi.c new file +43
-0+/* +* _pi_node_update - update the chain +* +* We loop through up to MAX_PI_DEPENDENCIES times looking for stale entries +* that need to propagate up the chain. This is a step-wise process where we +* have to be careful about locking and preemption. By trying MAX_PI_DEPs +* times, we guarantee that this update routine is an effective barrier... +* all modifications made prior to the call to this barrier will have completed. +* +* Deadlock avoidance: This node may participate in a chain of nodes which +* form a graph of arbitrary structure. While the graph should technically +* never close on itself barring any bugs, we still want to protect against +* a theoretical ABBA deadlock (if for nothing else, to prevent lockdep +* from detecting this potential). To do this, we employ a dual-locking +* scheme where we can carefully control the order. That is: node->lock +* protects most of the node's internal state, but it will never be held +* across a chain update. sinkref->lock, on the other hand, can be held +* across a boost/deboost, and also guarantees proper execution order. Also +* note that no locks are held across an sink->update. +*/ +static int +_pi_node_update (struct pi_sink *sink,unsigned int flags) +{ +struct pi_node *node =node_of (sink); +struct pi_sinkref *sinkref; +unsigned long iflags; +int count =0 ; +int i; +int pprio; +struct updater updaters[MAX_PI_DEPENDENCIES ]; + +spin_lock_irqsave (&node->lock, iflags); + + pprio = node->prio; + +if (!plist_head_empty (&node->srcs)) + node->prio =plist_first (&node->srcs)->prio; +else + node->prio =MAX_PRIO ; + +list_for_each_entry (sinkref, &node->sinks, list) { +/* +* If the priority is changing, or if this is a
Quoted 9 lines
lib/pi.c line 184
Quoted 6 lines
lib/pi.c new file +5
-0+* BOOST/DEBOOST, we consider this sink "stale" +*/ +if (pprio != node->prio + || sinkref->state != pi_state_boosted) { +struct updater *iter = &updaters[count++];What prevents count from overrun?
The node->sinks list will never have more than MAX_PI_DEPs in it, by design.
Quoted 54 lines
Quoted 54 lines
lib/pi.c new file +53
-0+ +BUG_ON (!atomic_read (&sinkref->sink->refs)); +_pi_sink_get (sinkref); + + iter->update =1 ; + iter->sinkref = sinkref; + iter->sink = sinkref->sink; + } + } + +spin_unlock (&node->lock); + +for (i =0 ; i < count; ++i) { +struct updater *iter = &updaters[i]; +unsigned int lflags =PI_FLAG_DEFER_UPDATE ; +struct pi_sink *sink; + + sinkref = iter->sinkref; + sink = iter->sink; + +spin_lock (&sinkref->lock); + +switch (sinkref->state) { +case pi_state_boost: + sinkref->state = pi_state_boosted; +/* Fall through */ +case pi_state_boosted: + sink->ops->boost (sink, &sinkref->src, lflags); +break ; +case pi_state_deboost: + sink->ops->deboost (sink, &sinkref->src, lflags); + sinkref->state = pi_state_free; + +/* +* drop the ref that we took when the sinkref +* was allocated. We still hold a ref from +* above. +*/ +_pi_sink_put_all (node, sinkref); +break ; +case pi_state_free: + iter->update =0 ; +break ; +default : +panic ("illegal sinkref type: %d" , sinkref->state); + } + +spin_unlock (&sinkref->lock); + +/* +* We will drop the sinkref reference while still holding the +* preempt/irqs off so that the memory is returned synchronously +* to the system.
Quoted 8 lines
lib/pi.c line 242
Quoted 6 lines
lib/pi.c new file +5
-0+*/ +_pi_sink_put_local (node, sinkref); + } + +local_irq_restore (iflags);Yack! You keep interrupts off while doing the chain.
Actually, not quite. The first pass (with interrupts off) simply sets the new priority value at each local element (limited to 5, typically 1-2). Short and sweet. Its the "update" that happens next (with interrupts/preemption enabled) that updates the chain.
Quoted 5 lines
I think my main
contribution to the PI system 2 years ago was to do this preemptively. I.e. there was points in the loop where interrupts and preemption where turned on.
I agree this is important, but I think you will see with further review that this is in fact what I do too.
Quoted 6 lines
Remember: It goes into user space again. An evil user could craft an application with a very long lock depth and keep higher priority real time tasks from running for an arbitrary long time (if
no limit on the lock depth is set, which is bad because it will be too low in some cases.)
Quoted 6 lines
But as I said I have had no time to watch what has actually been going on in the kernel for the last 2 years roughly. The said defects might have creeped in by other contributers already :-(
Esben
Esben,
Your review and insight are very much appreciated. I will be sure to
address the concerns mentioned above and CC you on the next release.
Thanks again,
-Greg
Re: [PATCH RT RFC v4 1/8] add generalized priority-inheritance interface
Quoted 98 lines · Gregory Haskins
Hi Esben,
Thank you for the review. Comments inline.Quoted 34 lines · Esben Nielsen
Disclaimer: I am no longer actively involved and I must admit I might have lost out on much of
what have been going on since I contributed to the PI system 2 years ago. But I allow myself to comment
anyway.Quoted 11 lines · Gregory Haskins
The kernel currently addresses priority-inversion through priority- inheritence. However, all of the priority-inheritence logic is integrated into the Real-Time Mutex infrastructure. This causes a few problems:
1) This tightly coupled relationship makes it difficult to extend to other areas of the kernel (for instance, pi-aware wait-queues may be desirable).
2) Enhancing the rtmutex infrastructure becomes challenging because there is no seperation between the locking code, and the pi-code.Quoted 8 lines
This patch aims to rectify these shortcomings by designing a stand-alone pi framework which can then be used to replace the rtmutex-specific version. The goal of this framework is to provide similar functionality to the existing subsystem, but with sole focus on PI and the relationships between objects that can boost priority, and the objects that get boosted.
This is really a good idea. When I had time (2 years ago) to actively work on these problem
I also came to the conclusion that PI should be more general than just the rtmutex. Preemptive RCU
was the example which drove it.Quoted 5 lines
But I do disagree that general objects should get boosted: The end targets are always tasks. The objects might
be boosted as intermediate steps, but priority end the only applies to tasks.Actually I fully agree with you here. Its probably just poor wording on my part, but this is exactly what happens. We may "boost" arbitrary objects on the way to boosting a task...but the intermediate objects are just there to help find our way to the proper tasks. Ultimately everything ends up at the scheduler eventually ;)
Quoted 4 lines
I also have a few comments to the actual design:
Quoted 18 lines
Documentation/libpi.txt line 32
Quoted 9 lines
....
Documentation/libpi.txt new file +6
-0+ +Multiple sinks per Node: + +We allow multiple sinks to be associated with a node. This is a slight departure from the previous implementation which had the notion of only a single sink (i.e. "task->pi_blocked_on"). The reason why we added the ability to add more than one sink was not to change the default chaining model (I.e. multiple boost targets), but rather to add a flexible notification mechanism that is peripheral to the chain, which are informally called "leaf sinks". + +Leaf-sinks are boostable objects that do not perpetuate a chain per se. Rather, they act as endpoints to a priority boosting. Ultimately, every chain ends with a leaf-sink, which presumably will act on the new priority information. However, there may be any number of leaf-sinks along a chain as well. Each one will act on its localized priority in its own implementation specific way. For instance, a task_struct pi-leaf may change the priority of the task and reschedule it if necessary. Whereas an rwlock leaf-sink may boost a list of reader-owners.This is bad from a RT point of view: You have a hard time determininig the number of sinks per node. An rw-lock could have an arbitrary number of readers (is supposed to really). Therefore
you have no chance of knowing how long the boost/deboost operation will take. And you also know for how long the boosted tasks stay boosted. If there can be an arbitrary number of
such tasks you can no longer be deterministic.While you may have a valid concern about what rwlocks can do to determinism, not that we already have PI enabled rwlocks before my patch, so I am not increasing nor decreasing determinism in this regard. That being said, Steven Rostedt (author of the pi-rwlocks, CC'd) has facilities to manage this (such as limiting the number of readers to
num_online_cpus) which this design would retain. Long story short, I do not believe I have made anything worse here, so this is a different discussion if you are still concerned.Quoted 12 lines
Review
Quoted 7 lines
...
(quoted diff) +2
-0+ +#define MAX_PI_DEPENDENCIES 5WHAT??? There is a finite lock depth defined. I know we did that originally but it wasn't hardcoded (as far as I remember) and it was certainly not as low as 5.
Quoted 8 lines
Note that this is simply in reference to how many direct sinks you can link to a node, not how long the resulting chain can grow. The chain depth is actually completely unconstrained by the design. I chose "5" here because typically we need 1 sink for the next link in the chain, and 1 sink for local notifications. The other 3 are there for head-room (we often hit 3-4 as we transition between nodes (add one node -> delete another, etc).
To clarify what I meant here: If you think of a normal linked-list node having a single "next" pointer, this implementation is like each node
having up to 5 "next" pointers. However typically only 1-2 are used,
and all but one will usually point to a "leaf" node, meaning it does not form a chain but terminates processing locally. Typically there will be only one link to something that forms a chain with other nodes. I did this because I realized the pattern (boost/deboost/update) was similar whether the node was a leaf or a chain-link, so I unified both behind the single pi_sink interface.
That being understood, note that as with any linked-list, the nodes can still have an arbitrary chaining depth (and I will fix this to be iterative instead of recursive, as previously mentioned).
Quoted 183 lines
You are not the first to comment about this, however, so it makes me realize it is not very clear ;) I will comment the code better.
Quoted 3 lines
Remember: PI is used by the user space futeces as well!
Yes, and on a slight tangent from your point, this incidentally is actually a problem in the design such that I need to respin at least a v5. My current design uses recursion against the
sink->update()methods, which Peter Zijlstra pointed out would blow up with large userpspace chains. My next version will forgo the recursion in favor of an iterative method more reminiscent of the original design.Quoted 46 lines
Quoted 46 lines
....
lib/pi.c new file +43
-0+/* +* _pi_node_update - update the chain +* +* We loop through up to MAX_PI_DEPENDENCIES times looking for stale entries +* that need to propagate up the chain. This is a step-wise process where we +* have to be careful about locking and preemption. By trying MAX_PI_DEPs +* times, we guarantee that this update routine is an effective barrier... +* all modifications made prior to the call to this barrier will have completed. +* +* Deadlock avoidance: This node may participate in a chain of nodes which +* form a graph of arbitrary structure. While the graph should technically +* never close on itself barring any bugs, we still want to protect against +* a theoretical ABBA deadlock (if for nothing else, to prevent lockdep +* from detecting this potential). To do this, we employ a dual-locking +* scheme where we can carefully control the order. That is: node->lock +* protects most of the node's internal state, but it will never be held +* across a chain update. sinkref->lock, on the other hand, can be held +* across a boost/deboost, and also guarantees proper execution order. Also +* note that no locks are held across an sink->update. +*/ +static int +_pi_node_update (struct pi_sink *sink,unsigned int flags) +{ +struct pi_node *node =node_of (sink); +struct pi_sinkref *sinkref; +unsigned long iflags; +int count =0 ; +int i; +int pprio; +struct updater updaters[MAX_PI_DEPENDENCIES ]; + +spin_lock_irqsave (&node->lock, iflags); + + pprio = node->prio; + +if (!plist_head_empty (&node->srcs)) + node->prio =plist_first (&node->srcs)->prio; +else + node->prio =MAX_PRIO ; + +list_for_each_entry (sinkref, &node->sinks, list) { +/* +* If the priority is changing, or if this is a Quoted 10 lines
lib/pi.c line 184
Quoted 7 lines
lib/pi.c new file +5
-0+* BOOST/DEBOOST, we consider this sink "stale" +*/ +if (pprio != node->prio + || sinkref->state != pi_state_boosted) { +struct updater *iter = &updaters[count++];What prevents count from overrun?
The
node->sinkslist will never have more thanMAX_PI_DEPsin it, by design.Quoted 55 lines
Quoted 55 lines
lib/pi.c new file +53
-0+ +BUG_ON (!atomic_read (&sinkref->sink->refs)); +_pi_sink_get (sinkref); + + iter->update =1 ; + iter->sinkref = sinkref; + iter->sink = sinkref->sink; + } + } + +spin_unlock (&node->lock); + +for (i =0 ; i < count; ++i) { +struct updater *iter = &updaters[i]; +unsigned int lflags =PI_FLAG_DEFER_UPDATE ; +struct pi_sink *sink; + + sinkref = iter->sinkref; + sink = iter->sink; + +spin_lock (&sinkref->lock); + +switch (sinkref->state) { +case pi_state_boost: + sinkref->state = pi_state_boosted; +/* Fall through */ +case pi_state_boosted: + sink->ops->boost (sink, &sinkref->src, lflags); +break ; +case pi_state_deboost: + sink->ops->deboost (sink, &sinkref->src, lflags); + sinkref->state = pi_state_free; + +/* +* drop the ref that we took when the sinkref +* was allocated. We still hold a ref from +* above. +*/ +_pi_sink_put_all (node, sinkref); +break ; +case pi_state_free: + iter->update =0 ; +break ; +default : +panic ("illegal sinkref type: %d" , sinkref->state); + } + +spin_unlock (&sinkref->lock); + +/* +* We will drop the sinkref reference while still holding the +* preempt/irqs off so that the memory is returned synchronously +* to the system. Quoted 9 lines
lib/pi.c line 242
Quoted 7 lines
lib/pi.c new file +5
-0+*/ +_pi_sink_put_local (node, sinkref); + } + +local_irq_restore (iflags);Yack! You keep interrupts off while doing the chain.
Actually, not quite. The first pass (with interrupts off) simply sets the new priority value at each local element (limited to 5, typically 1-2). Short and sweet. Its the "update" that happens next (with interrupts/preemption enabled) that updates the chain.
Quoted 6 lines
I think my main
contribution to the PI system 2 years ago was to do this preemptively. I.e. there was points in the loop where interrupts and preemption where turned on.I agree this is important, but I think you will see with further review that this is in fact what I do too.
Quoted 6 lines
Remember: It goes into user space again. An evil user could craft an application with a very long lock depth and keep higher priority real time tasks from running for an arbitrary long time (if
no limit on the lock depth is set, which is bad because it will be too low in some cases.)Quoted 7 lines
But as I said I have had no time to watch what has actually been going on in the kernel for the last 2 years roughly. The said defects might have creeped in by other contributers already :-(
Esben
Esben,
Your review and insight are very much appreciated. I will be sure to
address the concerns mentioned above and CC you on the next release.Thanks again,
-Greg
Steven Rostedt
reviewerRe: [PATCH RT RFC v4 1/8] add generalized priority-inheritance interface
In reply to Esben NielsenQuoted 5 lines · Esben Nielsen
Disclaimer: I am no longer actively involved and I must admit I might have lost out on much of
what have been going on since I contributed to the PI system 2 years ago. But I allow myself to comment
anyway.
Esben, you are always welcomed. You are one of the copyright owners of rtmutex.c ;-)
Quoted 48 lines · Gregory Haskins
Quoted 11 lines
The kernel currently addresses priority-inversion through priority- inheritence. However, all of the priority-inheritence logic is integrated into the Real-Time Mutex infrastructure. This causes a few problems:
1) This tightly coupled relationship makes it difficult to extend to other areas of the kernel (for instance, pi-aware wait-queues may be desirable).
2) Enhancing the rtmutex infrastructure becomes challenging because there is no seperation between the locking code, and the pi-code.Quoted 6 lines
This patch aims to rectify these shortcomings by designing a stand-alone pi framework which can then be used to replace the rtmutex-specific version. The goal of this framework is to provide similar functionality to the existing subsystem, but with sole focus on PI and the relationships between objects that can boost priority, and the objects that get boosted.
This is really a good idea. When I had time (2 years ago) to actively work on these problem
I also came to the conclusion that PI should be more general than just the rtmutex. Preemptive RCU
was the example which drove it.But I do disagree that general objects should get boosted: The end targets are always tasks. The objects might
be boosted as intermediate steps, but priority end the only applies to tasks.I also have a few comments to the actual design:
Documentation/libpi.txt line 32
Quoted 7 lines
....
Documentation/libpi.txt new file +6
-0+ +Multiple sinks per Node: + +We allow multiple sinks to be associated with a node. This is a slight departure from the previous implementation which had the notion of only a single sink (i.e. "task->pi_blocked_on"). The reason why we added the ability to add more than one sink was not to change the default chaining model (I.e. multiple boost targets), but rather to add a flexible notification mechanism that is peripheral to the chain, which are informally called "leaf sinks". + +Leaf-sinks are boostable objects that do not perpetuate a chain per se. Rather, they act as endpoints to a priority boosting. Ultimately, every chain ends with a leaf-sink, which presumably will act on the new priority information. However, there may be any number of leaf-sinks along a chain as well. Each one will act on its localized priority in its own implementation specific way. For instance, a task_struct pi-leaf may change the priority of the task and reschedule it if necessary. Whereas an rwlock leaf-sink may boost a list of reader-owners.This is bad from a RT point of view: You have a hard time determininig the number of sinks per node. An rw-lock could have an arbitrary number of readers (is supposed to really). Therefore
you have no chance of knowing how long the boost/deboost operation will take. And you also know for how long the boosted tasks stay boosted. If there can be an arbitrary number of
such tasks you can no longer be deterministic.
Quoted 8 lines
Review
Quoted 3 lines
...
(quoted diff) +2
-0+ +#define MAX_PI_DEPENDENCIES 5WHAT??? There is a finite lock depth defined. I know we did that originally but it wasn't hardcoded (as far as I remember) and it was certainly not as low as 5.
Yeah, I believe our number is 1024, and is not hardcoded, but is there to detect recursive locks.
Quoted 2 lines
Remember: PI is used by the user space futeces as well!
I haven't looked to hard at this code yet, but this may only be kernel related for multiple owners (see my explanaiton below).
Quoted 123 lines
Quoted 40 lines
....
lib/pi.c new file +38
-0+/* +* _pi_node_update - update the chain +* +* We loop through up to MAX_PI_DEPENDENCIES times looking for stale entries +* that need to propagate up the chain. This is a step-wise process where we +* have to be careful about locking and preemption. By trying MAX_PI_DEPs +* times, we guarantee that this update routine is an effective barrier... +* all modifications made prior to the call to this barrier will have completed. +* +* Deadlock avoidance: This node may participate in a chain of nodes which +* form a graph of arbitrary structure. While the graph should technically +* never close on itself barring any bugs, we still want to protect against +* a theoretical ABBA deadlock (if for nothing else, to prevent lockdep +* from detecting this potential). To do this, we employ a dual-locking +* scheme where we can carefully control the order. That is: node->lock +* protects most of the node's internal state, but it will never be held +* across a chain update. sinkref->lock, on the other hand, can be held +* across a boost/deboost, and also guarantees proper execution order. Also +* note that no locks are held across an sink->update. +*/ +static int +_pi_node_update (struct pi_sink *sink,unsigned int flags) +{ +struct pi_node *node =node_of (sink); +struct pi_sinkref *sinkref; +unsigned long iflags; +int count =0 ; +int i; +int pprio; +struct updater updaters[MAX_PI_DEPENDENCIES ]; + +spin_lock_irqsave (&node->lock, iflags); + + pprio = node->prio; + +if (!plist_head_empty (&node->srcs)) + node->prio =plist_first (&node->srcs)->prio; +else lib/pi.c line 184
Quoted 10 lines
lib/pi.c new file +10
-0+ node->prio =MAX_PRIO ; + +list_for_each_entry (sinkref, &node->sinks, list) { +/* +* If the priority is changing, or if this is a +* BOOST/DEBOOST, we consider this sink "stale" +*/ +if (pprio != node->prio + || sinkref->state != pi_state_boosted) { +struct updater *iter = &updaters[count++];What prevents count from overrun?
Quoted 48 lines
lib/pi.c new file +48
-0+ +BUG_ON (!atomic_read (&sinkref->sink->refs)); +_pi_sink_get (sinkref); + + iter->update =1 ; + iter->sinkref = sinkref; + iter->sink = sinkref->sink; + } + } + +spin_unlock (&node->lock); + +for (i =0 ; i < count; ++i) { +struct updater *iter = &updaters[i]; +unsigned int lflags =PI_FLAG_DEFER_UPDATE ; +struct pi_sink *sink; + + sinkref = iter->sinkref; + sink = iter->sink; + +spin_lock (&sinkref->lock); + +switch (sinkref->state) { +case pi_state_boost: + sinkref->state = pi_state_boosted; +/* Fall through */ +case pi_state_boosted: + sink->ops->boost (sink, &sinkref->src, lflags); +break ; +case pi_state_deboost: + sink->ops->deboost (sink, &sinkref->src, lflags); + sinkref->state = pi_state_free; + +/* +* drop the ref that we took when the sinkref +* was allocated. We still hold a ref from +* above. +*/ +_pi_sink_put_all (node, sinkref); +break ; +case pi_state_free: + iter->update =0 ; +break ; +default : +panic ("illegal sinkref type: %d" , sinkref->state); + } + +spin_unlock (&sinkref->lock);lib/pi.c line 242
Quoted 10 lines
lib/pi.c new file +10
-0+ +/* +* We will drop the sinkref reference while still holding the +* preempt/irqs off so that the memory is returned synchronously +* to the system. +*/ +_pi_sink_put_local (node, sinkref); + } + +local_irq_restore (iflags);Yack! You keep interrupts off while doing the chain. I think my main contribution to the PI system 2 years ago was to do this preemptively. I.e. there was points in the loop where interrupts and preemption where turned on.
Remember: It goes into user space again. An evil user could craft an application with a very long lock depth and keep higher priority real time tasks from running for an arbitrary long time (if
no limit on the lock depth is set, which is bad because it will be too low in some cases.)
Quoted 3 lines
But as I said I have had no time to watch what has actually been going on in the kernel for the last 2 years roughly. The said defects might have creeped in by other contributers already :-(
The rtmutex.c has hardly changed since you last left it. The two big additions, were adaptive locks, which hardly touched the pi chain, and my rwlocks allowing multiple readers. It added a hook to allow going into the pi chain for all readers while holding a spinlock and yes irqs off. The difference is that it is a bug to hold an rwlock (internal kernel lock only) and take a futex. Thus, this rwlock code did have a recursive depth of 5. Perhaps that's what the PI depth is from above?
I still haven't had the time to analyze Gregory's code, so those points that you made, may be only related to kernel activities (like the new rwlock code). But by generalizing it, it decouples the PI from the locking, which in general is a good thing, but for the multiple reader locks, it is dangerous to decouple it, since there are a lot of assumptions between the multiple PI owner code and rwlocks. In a general approach, the assumptions will be harder to see.
-- Steve
RFC PATCH v4 2/8 sched: add the basic PI infrastructure to the task_struct
In reply to Gregory HaskinsThis is a first pass at converting the system to use the new PI library. We dont go for a wholesale replacement quite yet so that we can focus on getting the basic plumbing in place. Later in the series we will begin replacing some of the proprietary logic with the generic framework.
---
include/linux/sched.h | 37 +++++++-- include/linux/workqueue.h | 2 kernel/fork.c | 1 kernel/rcupreempt-boost.c | 23 +----- kernel/rtmutex.c | 6 + kernel/sched.c | 188 ++++++++++++++++++++++++++++++++------------- kernel/workqueue.c | 39 ++++++++- 7 files changed, 206 insertions(+), 90 deletions(-)
include/linux/sched.h +31 -6
@@ -87,6 +87,7 @@ struct sched_param {#include <linux/task_io_accounting.h> #include <linux/kobject.h> #include <linux/latencytop.h> +#include <linux/pi.h> #include <asm/processor.h>
@@ -1125,6 +1126,7 @@ struct task_struct {int prio, static_prio, normal_prio;#ifdef CONFIG_PREEMPT_RCU_BOOST int rcu_prio; +struct pi_source rcu_prio_src;#endif const struct sched_class *sched_class;struct sched_entity se;
@@ -1298,11 +1300,20 @@ struct task_struct {/* Protection of the PI data structures: */ raw_spinlock_t pi_lock; +struct { +struct pi_source src;/* represents normal_prio to 'this' */ +struct pi_node node; +struct pi_sink sink;/* registered to 'this' to get updates */ +int prio; + } pi; +#ifdef CONFIG_RT_MUTEXES /* PI waiters blocked on a rt_mutex held by this task */ struct plist_head pi_waiters;/* Deadlock detection and priority inheritance handling */ struct rt_mutex_waiter *pi_blocked_on; +int rtmutex_prio; +struct pi_source rtmutex_prio_src;#endif #ifdef CONFIG_DEBUG_MUTEXES
@@ -1440,6 +1451,26 @@ struct task_struct {#endif }; +static inline int +task_pi_boost (struct task_struct *p,struct pi_source *src, +unsigned int flags) +{ +return pi_boost (&p->pi.node, src, flags); +} + +static inline int +task_pi_deboost (struct task_struct *p,struct pi_source *src, +unsigned int flags) +{ +return pi_deboost (&p->pi.node, src, flags); +} + +static inline int +task_pi_update (struct task_struct *p,unsigned int flags) +{ +return pi_update (&p->pi.node, flags); +} +#ifdef CONFIG_PREEMPT_RT # define set_printk_might_sleep (x) do { current->in_printk = x; } while(0)#else
@@ -1774,14 +1805,8 @@ int sched_rt_handler(struct ctl_table *table, int write,extern unsigned int sysctl_sched_compat_yield; -extern void task_setprio (struct task_struct *p,int prio); -#ifdef CONFIG_RT_MUTEXES extern int rt_mutex_getprio (struct task_struct *p); -static inline void rt_mutex_setprio (struct task_struct *p,int prio) -{ -task_setprio (p, prio); -}extern void rt_mutex_adjust_pi (struct task_struct *p);#else static inline int rt_mutex_getprio (struct task_struct *p)
include/linux/workqueue.h +2 -0
@@ -11,6 +11,7 @@#include <linux/lockdep.h> #include <linux/plist.h> #include <linux/sched_prio.h> +#include <linux/pi.h> #include <asm/atomic.h> struct workqueue_struct ;
@@ -31,6 +32,7 @@ struct work_struct {#define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK)struct plist_node entry;work_func_t func; +struct pi_source pi_src;#ifdef CONFIG_LOCKDEP struct lockdep_map lockdep_map;#endif
kernel/fork.c +1 -0
@@ -990,6 +990,7 @@ static struct task_struct *copy_process(unsigned long clone_flags, p->rcu_flipctr_idx =0 ;#ifdef CONFIG_PREEMPT_RCU_BOOST p->rcu_prio =MAX_PRIO ; +pi_source_init (&p->rcu_prio_src, &p->rcu_prio); p->rcub_rbdp =NULL ; p->rcub_state =RCU_BOOST_IDLE ;INIT_LIST_HEAD (&p->rcub_entry);
kernel/rcupreempt-boost.c +3 -20
@@ -232,14 +232,11 @@ static inline int rcu_is_boosted(struct task_struct *task)static void rcu_boost_task (struct task_struct *task) {WARN_ON (!irqs_disabled ()); -WARN_ON_SMP (!spin_is_locked (&task->pi_lock));rcu_trace_boost_task_boost_called (RCU_BOOST_ME ); -if (task->rcu_prio < task->prio) { +if (task_pi_boost (task, &task->rcu_prio_src,0 ))rcu_trace_boost_task_boosted (RCU_BOOST_ME ); -task_setprio (task, task->rcu_prio); - } }/**
@@ -275,26 +272,17 @@ void __rcu_preempt_boost(void) rbd = &__get_cpu_var (rcu_boost_data);spin_lock (&rbd->rbs_lock); -spin_lock (&curr->pi_lock); - curr->rcub_rbdp = rbd;rcu_trace_boost_try_boost (rbd); - prio =rt_mutex_getprio (curr); -if (list_empty (&curr->rcub_entry))list_add_tail (&curr->rcub_entry, &rbd->rbs_toboost); -if (prio <= rbd->rbs_prio) - goto out; - -rcu_trace_boost_boosted (curr->rcub_rbdp);set_rcu_prio (curr, rbd->rbs_prio);rcu_boost_task (curr); out: -spin_unlock (&curr->pi_lock);spin_unlock_irqrestore (&rbd->rbs_lock, flags); }
@@ -353,15 +341,12 @@ void __rcu_preempt_unboost(void)rcu_trace_boost_unboosted (rbd); -set_rcu_prio (curr,MAX_PRIO ); +task_pi_deboost (curr, &curr->rcu_prio_src,0 ); -spin_lock (&curr->pi_lock); - prio =rt_mutex_getprio (curr); -task_setprio (curr, prio); +set_rcu_prio (curr,MAX_PRIO ); curr->rcub_rbdp =NULL ; -spin_unlock (&curr->pi_lock); out:spin_unlock_irqrestore (&rbd->rbs_lock, flags); }
@@ -393,9 +378,7 @@ static int __rcu_boost_readers(struct rcu_boost_dat *rbd, int prio, unsigned lonlist_move_tail (&p->rcub_entry, &rbd->rbs_boosted);set_rcu_prio (p, prio); -spin_lock (&p->pi_lock);rcu_boost_task (p); -spin_unlock (&p->pi_lock);/* * Now we release the lock to allow for a higher
kernel/rtmutex.c +4 -2
@@ -178,8 +178,10 @@ static void __rt_mutex_adjust_prio(struct task_struct *task) {int prio =rt_mutex_getprio (task); -if (task->prio != prio) -rt_mutex_setprio (task, prio); +if (task->rtmutex_prio != prio) { + task->rtmutex_prio = prio; +task_pi_boost (task, &task->rtmutex_prio_src,0 ); + } }/*
kernel/sched.c +133 -55
305 lines not on this page · raw message
kernel/workqueue.c +32 -7
@@ -145,8 +145,13 @@ static void insert_work(struct cpu_workqueue_struct *cwq,plist_node_init (&work->entry, prio);plist_add (&work->entry, &cwq->worklist); -if (boost_prio < cwq->thread->prio) -task_setprio (cwq->thread, boost_prio); +/* +* FIXME: We want to boost to boost_prio, but we dont record that +* value in the work_struct for later deboosting +*/ +pi_source_init (&work->pi_src, &work->entry.prio); +task_pi_boost (cwq->thread, &work->pi_src,0 ); +wake_up (&cwq->more_work); }
@@ -280,6 +285,10 @@ struct wq_barrier {static void run_workqueue (struct cpu_workqueue_struct *cwq) {struct plist_head *worklist = &cwq->worklist; +struct pi_source pi_src; +int prio; + +pi_source_init (&pi_src, &prio);spin_lock_irq (&cwq->lock); cwq->run_depth++;
@@ -292,10 +301,10 @@ static void run_workqueue(struct cpu_workqueue_struct *cwq) again:while (!plist_head_empty (worklist)) { -int prio;struct work_struct *work =plist_first_entry (worklist, struct work_struct, entry);work_func_t f = work->func; +#ifdef CONFIG_LOCKDEP /* * It is permissible to free the struct work_struct
@@ -316,14 +325,28 @@ again: } prio =max (prio,0 ); -if (likely (cwq->thread->prio != prio)) -task_setprio (cwq->thread, prio); - cwq->current_work = work;plist_del (&work->entry, worklist);plist_node_init (&work->entry,MAX_PRIO );spin_unlock_irq (&cwq->lock); +/* +* The owner is free to reuse the work object once we execute +* the work->func() below. Therefore we cannot leave the +* work->pi_src boosting our thread or it may get stomped +* on when the work item is requeued. +* +* So what we do is we boost ourselves with an on-the +* stack copy of the priority of the work item, and then +* deboost the workitem. Once the work is complete, we +* can then simply deboost the stack version. +* +* Note that this will not typically cause a pi-chain +* update since we are boosting the node laterally +*/ +task_pi_boost (current, &pi_src,PI_FLAG_DEFER_UPDATE ); +task_pi_deboost (current, &work->pi_src,PI_FLAG_DEFER_UPDATE ); +BUG_ON (get_wq_data (work) != cwq);work_clear_pending (work);leak_check (NULL );
@@ -334,6 +357,9 @@ again:lock_release (&cwq->wq->lockdep_map,1 , _THIS_IP_);leak_check (f); +/* Deboost the stack copy of the work->prio (see above) */ +task_pi_deboost (current, &pi_src,0 ); +spin_lock_irq (&cwq->lock); cwq->current_work =NULL ;wake_up_all (&cwq->work_done);
@@ -357,7 +383,6 @@ again: goto again; } -task_setprio (cwq->thread, current->normal_prio); cwq->run_depth--;spin_unlock_irq (&cwq->lock); }
RFC PATCH v4 3/8 sched: rework task reference counting to work with the pi infrastructure
In reply to Gregory Haskins---
include/linux/sched.h | 7 +++++-- kernel/fork.c | 32 +++++++++++++++----------------- kernel/sched.c | 21 +++++++++++++++++++++ 3 files changed, 41 insertions(+), 19 deletions(-)
include/linux/sched.h +5 -2
@@ -1305,6 +1305,8 @@ struct task_struct {struct pi_node node;struct pi_sink sink;/* registered to 'this' to get updates */ int prio; +struct rcu_head rcu;/* for destruction cleanup */ + } pi;#ifdef CONFIG_RT_MUTEXES
@@ -1633,12 +1635,11 @@ static inline void put_task_struct(struct task_struct *t) call_rcu(&t->rcu, __put_task_struct_cb); }#else -extern void __put_task_struct (struct task_struct *t);static inline void put_task_struct (struct task_struct *t) {if (atomic_dec_and_test (&t->usage)) -__put_task_struct (t); +pi_put (&t->pi.node,0 ); }#endif
@@ -2469,6 +2470,8 @@ static inline int task_is_current(struct task_struct *task) }#endif +extern void prepare_free_task (struct task_struct *tsk); +#define TASK_STATE_TO_CHAR_STR "RMSDTtZX"#endif /* __KERNEL__ */
kernel/fork.c +15 -17
@@ -130,39 +130,37 @@ void free_task(struct task_struct *tsk) }EXPORT_SYMBOL (free_task); -#ifdef CONFIG_PREEMPT_RT -void __put_task_struct_cb (struct rcu_head *rhp) +void prepare_free_task (struct task_struct *tsk) { - struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); -BUG_ON (atomic_read (&tsk->usage)); -WARN_ON (!tsk->exit_state);WARN_ON (tsk == current); +#ifdef CONFIG_PREEMPT_RT +WARN_ON (!tsk->exit_state); +#else +WARN_ON (!(tsk->exit_state & (EXIT_DEAD |EXIT_ZOMBIE ))); +#endif +security_task_free (tsk);free_uid (tsk->user);put_group_info (tsk->group_info); + +#ifdef CONFIG_PREEMPT_RT delayacct_tsk_free (tsk); +#endif if (!profile_handoff_task (tsk))free_task (tsk); } -#else - -void __put_task_struct (struct task_struct *tsk) +#ifdef CONFIG_PREEMPT_RT +void __put_task_struct_cb (struct rcu_head *rhp) { -WARN_ON (!(tsk->exit_state & (EXIT_DEAD |EXIT_ZOMBIE ))); -BUG_ON (atomic_read (&tsk->usage)); -WARN_ON (tsk == current); - -security_task_free (tsk); -free_uid (tsk->user); -put_group_info (tsk->group_info); + struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); -if (!profile_handoff_task (tsk)) -free_task (tsk); +pi_put (&tsk->pi.node,0 ); } +#endif /*
kernel/sched.c +21 -0
@@ -2370,11 +2370,32 @@ task_pi_boost_cb(struct pi_sink *sink, struct pi_source *src,return 0 ; } +static void task_pi_free_rcu (struct rcu_head *rhp) +{ + struct task_struct *tsk = container_of(rhp, struct task_struct, pi.rcu); + +prepare_free_task (tsk); +} + +/* +* This function is invoked whenever the last references to a task have +* been dropped, and we should free the memory on the next rcu grace period +*/ +static int task_pi_free_cb (struct pi_sink *sink,unsigned int flags) +{ + struct task_struct *p = container_of(sink, struct task_struct, pi.sink); + +call_rcu (&p->pi.rcu, task_pi_free_rcu); + +return 0 ; +} +static int task_pi_update_cb (struct pi_sink *sink,unsigned int flags);static struct pi_sink_ops task_pi_sink = { .boost = task_pi_boost_cb, .update = task_pi_update_cb, + .free = task_pi_free_cb, }; static inline void
RFC PATCH v4 4/8 rtmutex: formally initialize the rt_mutex_waiters
In reply to Gregory HaskinsWe will be adding more logic to rt_mutex_waiters and therefore lets centralize the initialization to make this easier going forward.
---
kernel/rtmutex.c | 26 ++++++++++++++------------ 1 files changed, 14 insertions(+), 12 deletions(-)
kernel/rtmutex.c +14 -12
@@ -805,6 +805,15 @@ static int adaptive_wait(struct rt_mutex_waiter *waiter, }#endif +static void init_waiter (struct rt_mutex_waiter *waiter) +{ +memset (waiter,0 ,sizeof (*waiter)); + +debug_rt_mutex_init_waiter (waiter); + waiter->task =NULL ; + waiter->write_lock =0 ; +} +/* * Slow path lock function spin_lock style: this variant is very * careful not to miss any non-lock wakeups.
@@ -823,9 +832,7 @@ rt_spin_lock_slowlock(struct rt_mutex *lock)struct task_struct *orig_owner;int missed =0 ; -debug_rt_mutex_init_waiter (&waiter); - waiter.task =NULL ; - waiter.write_lock =0 ; +init_waiter (&waiter);spin_lock_irqsave (&lock->wait_lock, flags);init_lists (lock);
@@ -1324,6 +1331,8 @@ rt_read_slowlock(struct rw_mutex *rwm, int mtx)int saved_lock_depth =-1 ;unsigned long saved_state =-1 , state, flags; +init_waiter (&waiter); +spin_lock_irqsave (&mutex->wait_lock, flags);init_rw_lists (rwm);
@@ -1335,10 +1344,6 @@ rt_read_slowlock(struct rw_mutex *rwm, int mtx)/* Owner is a writer (or a blocked writer). Block on the lock */ -debug_rt_mutex_init_waiter (&waiter); - waiter.task =NULL ; - waiter.write_lock =0 ; -if (mtx) {/* * We drop the BKL here before we go into the wait loop to avoid a
@@ -1538,8 +1543,7 @@ rt_write_slowlock(struct rw_mutex *rwm, int mtx)int saved_lock_depth =-1 ;unsigned long flags, saved_state =-1 , state; -debug_rt_mutex_init_waiter (&waiter); - waiter.task =NULL ; +init_waiter (&waiter);/* we do PI different for writers that are blocked */ waiter.write_lock =1 ;
@@ -2270,9 +2274,7 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,struct rt_mutex_waiter waiter;unsigned long flags; -debug_rt_mutex_init_waiter (&waiter); - waiter.task =NULL ; - waiter.write_lock =0 ; +init_waiter (&waiter);spin_lock_irqsave (&lock->wait_lock, flags);init_lists (lock);
RFC PATCH v4 5/8 RT: wrap the rt_rwlock "add reader" logic
In reply to Gregory HaskinsWe will use this later in the series to add PI functions on "add".
---
kernel/rtmutex.c | 16 +++++++++++----- 1 files changed, 11 insertions(+), 5 deletions(-)
kernel/rtmutex.c +11 -5
@@ -1122,6 +1122,12 @@ static void rw_check_held(struct rw_mutex *rwm)# define rw_check_held (rwm) do { } while (0)#endif +static inline void +rt_rwlock_add_reader (struct reader_lock_struct *rls,struct rw_mutex *rwm) +{ +list_add (&rls->list, &rwm->readers); +} +/* * The fast path does not add itself to the reader list to keep * from needing to grab the spinlock. We need to add the owner
@@ -1163,7 +1169,7 @@ rt_rwlock_update_owner(struct rw_mutex *rwm, struct task_struct *own)if (rls->list.prev && !list_empty (&rls->list))return ; -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rls, rwm);/* change to reader, so no one else updates too */ rt_rwlock_set_owner (rwm,RT_RW_READER ,RT_RWLOCK_CHECK );
@@ -1197,7 +1203,7 @@ static int try_to_take_rw_read(struct rw_mutex *rwm, int mtx)* it hasn't been added to the link list yet. */ if (!rls->list.prev ||list_empty (&rls->list)) -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rls, rwm);rt_rwlock_set_owner (rwm,RT_RW_READER ,0 ); rls->count++; incr =0 ;
@@ -1276,7 +1282,7 @@ static int try_to_take_rw_read(struct rw_mutex *rwm, int mtx) rls->lock = rwm; rls->count =1 ;WARN_ON (rls->list.prev && !list_empty (&rls->list)); -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rls, rwm); }else WARN_ON_ONCE (1 );spin_unlock (¤t->pi_lock);
@@ -1473,7 +1479,7 @@ __rt_read_fasttrylock(struct rw_mutex *rwm)spin_lock (&mutex->wait_lock); rls = ¤t->owned_read_locks[reader_count];if (!rls->list.prev ||list_empty (&rls->list)) -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rlw, rwm);spin_unlock (&mutex->wait_lock); }else spin_unlock (¤t->pi_lock);
@@ -2083,7 +2089,7 @@ rt_mutex_downgrade_write(struct rw_mutex *rwm)/* Set us up for multiple readers or conflicts */ -list_add (&rls->list, &rwm->readers); +rt_rwlock_add_reader (rls, rwm); rwm->owner =RT_RW_READER ;/*
RFC PATCH v4 6/8 rtmutex: use runtime init for rtmutexes
In reply to Gregory HaskinsThe system already has facilities to perform late/run-time init for rtmutexes. We want to add more advanced initialization later in the series so we force all rtmutexes through the init path in preparation for the later patches.
---
include/linux/rtmutex.h | 2 -- 1 files changed, 0 insertions(+), 2 deletions(-)
include/linux/rtmutex.h +0 -2
@@ -64,8 +64,6 @@ struct hrtimer_sleeper;#define __RT_MUTEX_INITIALIZER (mutexname) \ { .wait_lock = RAW_SPIN_LOCK_UNLOCKED(mutexname) \ - , .wait_list = PLIST_HEAD_INIT(mutexname.wait_list, &mutexname.wait_lock) \ - , .owner = NULL \ __DEBUG_RT_MUTEX_INITIALIZER(mutexname)}#define DEFINE_RT_MUTEX (mutexname) \
RFC PATCH v4 7/8 rtmutex: convert rtmutexes to fully use the PI library
In reply to Gregory HaskinsWe have previously only laid some of the groundwork to use the PI library, but left the existing infrastructure in place in the rtmutex code. This patch converts the rtmutex PI code to officially use the PI library.
---
include/linux/rt_lock.h | 2 include/linux/rtmutex.h | 15 - include/linux/sched.h | 21 - kernel/fork.c | 2 kernel/rcupreempt-boost.c | 2 kernel/rtmutex-debug.c | 4 kernel/rtmutex-tester.c | 4 kernel/rtmutex.c | 944 ++++++++++++++------------------------------- kernel/rtmutex_common.h | 18 - kernel/rwlock_torture.c | 32 -- kernel/sched.c | 12 - 11 files changed, 321 insertions(+), 735 deletions(-)
include/linux/rt_lock.h +2 -0
@@ -14,6 +14,7 @@#include <asm/atomic.h> #include <linux/spinlock_types.h> #include <linux/sched_prio.h> +#include <linux/pi.h> #ifdef CONFIG_PREEMPT_RT /*
@@ -67,6 +68,7 @@ struct rw_mutex {atomic_t count;/* number of times held for read */ atomic_t owners;/* number of owners as readers */ struct list_head readers; +struct pi_sink pi_sink;int prio; };
include/linux/rtmutex.h +7 -8
@@ -15,6 +15,7 @@#include <linux/linkage.h> #include <linux/plist.h> #include <linux/spinlock_types.h> +#include <linux/pi.h> /** * The rt_mutex structure
@@ -27,6 +28,12 @@ struct rt_mutex {raw_spinlock_t wait_lock;struct plist_head wait_list;struct task_struct *owner; +struct { +struct pi_source src; +struct pi_node node; +struct pi_sink sink; +int prio; + } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES int save_state;const char *name, *file;
@@ -96,12 +103,4 @@ extern int rt_mutex_trylock(struct rt_mutex *lock);extern void rt_mutex_unlock (struct rt_mutex *lock); -#ifdef CONFIG_RT_MUTEXES -# define INIT_RT_MUTEXES (tsk) \ - .pi_waiters = PLIST_HEAD_INIT(tsk.pi_waiters, &tsk.pi_lock), \ - INIT_RT_MUTEX_DEBUG(tsk) -#else -# define INIT_RT_MUTEXES (tsk) -#endif -#endif
include/linux/sched.h +1 -20
@@ -1106,6 +1106,7 @@ struct reader_lock_struct {struct rw_mutex *lock;struct list_head list;struct task_struct *task; +struct pi_source pi_src;int count; };
@@ -1309,15 +1310,6 @@ struct task_struct { } pi; -#ifdef CONFIG_RT_MUTEXES -/* PI waiters blocked on a rt_mutex held by this task */ -struct plist_head pi_waiters; -/* Deadlock detection and priority inheritance handling */ -struct rt_mutex_waiter *pi_blocked_on; -int rtmutex_prio; -struct pi_source rtmutex_prio_src; -#endif -#ifdef CONFIG_DEBUG_MUTEXES /* mutex deadlock detection */ struct mutex_waiter *blocked_on;
@@ -1806,17 +1798,6 @@ int sched_rt_handler(struct ctl_table *table, int write,extern unsigned int sysctl_sched_compat_yield; -#ifdef CONFIG_RT_MUTEXES -extern int rt_mutex_getprio (struct task_struct *p); -extern void rt_mutex_adjust_pi (struct task_struct *p); -#else -static inline int rt_mutex_getprio (struct task_struct *p) -{ -return p->normal_prio; -} -# define rt_mutex_adjust_pi (p) do { } while (0) -#endif -extern void set_user_nice (struct task_struct *p,long nice);extern int task_prio (const struct task_struct *p);extern int task_nice (const struct task_struct *p);
kernel/fork.c +0 -2
@@ -885,8 +885,6 @@ static void rt_mutex_init_task(struct task_struct *p) {spin_lock_init (&p->pi_lock);#ifdef CONFIG_RT_MUTEXES -plist_head_init (&p->pi_waiters, &p->pi_lock); - p->pi_blocked_on =NULL ;# ifdef CONFIG_DEBUG_RT_MUTEXES p->last_kernel_lock =NULL ;# endif
kernel/rcupreempt-boost.c +1 -1
@@ -424,7 +424,7 @@ void rcu_boost_readers(void)spin_lock_irqsave (&rcu_boost_wake_lock, flags); - prio =rt_mutex_getprio (curr); + prio =get_rcu_prio (curr);rcu_trace_boost_try_boost_readers (RCU_BOOST_ME );
kernel/rtmutex-debug.c +0 -4
@@ -57,8 +57,6 @@ static void printk_lock(struct rt_mutex *lock, int print_owner)void rt_mutex_debug_task_free (struct task_struct *task) { -DEBUG_LOCKS_WARN_ON (!plist_head_empty (&task->pi_waiters)); -DEBUG_LOCKS_WARN_ON (task->pi_blocked_on);#ifdef CONFIG_PREEMPT_RT WARN_ON (task->reader_lock_count);#endif
@@ -156,7 +154,6 @@ void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter) {memset (waiter,0x11 ,sizeof (*waiter));plist_node_init (&waiter->list_entry,MAX_PRIO ); -plist_node_init (&waiter->pi_list_entry,MAX_PRIO ); waiter->deadlock_task_pid =NULL ; }
@@ -164,7 +161,6 @@ void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter) {put_pid (waiter->deadlock_task_pid);DEBUG_LOCKS_WARN_ON (!plist_node_empty (&waiter->list_entry)); -DEBUG_LOCKS_WARN_ON (!plist_node_empty (&waiter->pi_list_entry));DEBUG_LOCKS_WARN_ON (waiter->task);memset (waiter,0x22 ,sizeof (*waiter)); }
kernel/rtmutex-tester.c +2 -2
@@ -373,11 +373,11 @@ static ssize_t sysfs_test_status(struct sys_device *dev, char *buf)spin_lock (&rttest_lock); curr +=sprintf (curr, -"O: %4d, E:%8d, S: 0x%08lx, P: %4d, N: %4d, B: %p, K: %d, M:" , +"O: %4d, E:%8d, S: 0x%08lx, P: %4d, N: %4d, K: %d M:" , td->opcode, td->event, tsk->state, (MAX_RT_PRIO -1 ) - tsk->prio, (MAX_RT_PRIO -1 ) - tsk->normal_prio, - tsk->pi_blocked_on, td->bkl); + td->bkl);for (i =MAX_RT_TEST_MUTEXES -1 ; i >=0 ; i--) curr +=sprintf (curr,"%d" , td->mutexes[i]);
kernel/rtmutex.c +302 -642
1,280 lines not on this page · raw message
kernel/rtmutex_common.h +5 -13
@@ -14,6 +14,7 @@#include <linux/rtmutex.h> #include <linux/rt_lock.h> +#include <linux/pi.h> /* * The rtmutex in kernel tester is independent of rtmutex debugging. We
@@ -48,10 +49,13 @@ extern void schedule_rt_mutex_test(struct rt_mutex *lock);*/ struct rt_mutex_waiter {struct plist_node list_entry; -struct plist_node pi_list_entry;struct task_struct *task;struct rt_mutex *lock;int write_lock; +struct { +struct pi_sink sink; +int prio; + } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES unsigned long ip;struct pid *deadlock_task_pid;
@@ -79,18 +83,6 @@ rt_mutex_top_waiter(struct rt_mutex *lock)return w; } -static inline int task_has_pi_waiters (struct task_struct *p) -{ -return !plist_head_empty (&p->pi_waiters); -} - -static inline struct rt_mutex_waiter * -task_top_pi_waiter (struct task_struct *p) -{ - return plist_first_entry(&p->pi_waiters, struct rt_mutex_waiter, - pi_list_entry); -} -/* * lock->owner state tracking: */
kernel/rwlock_torture.c +1 -31
@@ -682,37 +682,7 @@ static int __init mutex_stress_init(void)print_owned_read_locks (tsks[i]); -if (tsks[i]->pi_blocked_on) { - w = (void *)tsks[i]->pi_blocked_on; - mtx = w->lock; -spin_unlock_irq (&tsks[i]->pi_lock); -spin_lock_irq (&mtx->wait_lock); -spin_lock (&tsks[i]->pi_lock); - own = (unsigned long )mtx->owner & ~3UL ; - oops_in_progress++; -printk ("%s:%d is blocked on " , - tsks[i]->comm, tsks[i]->pid); -__print_symbol ("%s" , (unsigned long )mtx); -if (own ==0x100 ) -printk (" owner is READER\n" ); -else if (!(own & ~300 )) -printk (" owner is ILLEGAL!!\n" ); -else if (!own) -printk (" has no owner!\n" ); -else { -struct task_struct *owner = (void *)own; - -printk (" owner is %s:%d\n" , - owner->comm, owner->pid); - } - oops_in_progress--; - -spin_unlock (&tsks[i]->pi_lock); -spin_unlock_irq (&mtx->wait_lock); - }else { -print_owned_read_locks (tsks[i]); -spin_unlock_irq (&tsks[i]->pi_lock); - } + spin_unlock_irq(&tsks[i]->pi_lock); } }#endif
kernel/sched.c +0 -11
@@ -2411,12 +2411,6 @@ task_pi_init(struct task_struct *p)pi_source_init (&p->pi.src, &p->normal_prio);task_pi_boost (p, &p->pi.src,PI_FLAG_DEFER_UPDATE ); -#ifdef CONFIG_RT_MUTEXES - p->rtmutex_prio =MAX_PRIO ; -pi_source_init (&p->rtmutex_prio_src, &p->rtmutex_prio); -task_pi_boost (p, &p->rtmutex_prio_src,PI_FLAG_DEFER_UPDATE ); -#endif -/* * We add our own task as a dependency of ourselves so that * we get boost-notifications (via task_pi_boost_cb) whenever
@@ -5027,7 +5021,6 @@ task_pi_update_cb(struct pi_sink *sink, unsigned int flags)*/ if (unlikely (p == rq->idle)) {WARN_ON (p != rq->curr); -WARN_ON (p->pi_blocked_on); goto out_unlock; }
@@ -5358,7 +5351,6 @@ recheck:spin_unlock_irqrestore (&p->pi_lock, flags);task_pi_update (p,0 ); -rt_mutex_adjust_pi (p);return 0 ; }
@@ -8492,10 +8484,6 @@ void __init sched_init(void)task_pi_init (&init_task); -#ifdef CONFIG_RT_MUTEXES -plist_head_init (&init_task.pi_waiters, &init_task.pi_lock); -#endif
RFC PATCH v4 8/8 rtmutex: pi-boost locks as late as possible
In reply to Gregory HaskinsPREEMPT_RT replaces most spinlock_t instances with a preemptible real-time lock that supports priority inheritance. An uncontended (fastpath) acquisition of this lock has no more overhead than its non-rt spinlock_t counterpart. However, the contended case has considerably more overhead so that the lock can maintain proper priority queue order and support pi-boosting of the lock owner, yet remaining fully preemptible.
Instrumentation shows that the majority of acquisitions under most workloads falls either into the fastpath category, or the adaptive spin category within the slowpath. The necessity to pi-boost a lock-owner should be sufficiently rare, yet the slow-path path blindly incurs this overhead in 100% of contentions.
Therefore, this patch intends to capitalize on this observation in order to reduce overhead and improve acquisition throughput. It is important to note that real-time latency is still treated as a higher order constraint than throughput, so the full pi-protocol is observed using new carefully constructed rules around the old concepts.
1) We check the priority of the owner relative to the waiter on each spin of the lock (if we are not boosted already). If the owner's effective priority is logically less than the waiters priority, we must boost them.
2) We check the priority of ourselves against our current queue position on the waiters-list (if we are not boosted already). If our priority was changed, we need to re-queue ourselves to update our position.
3) We break out of the adaptive-spin if either of the above conditions (1), (2) change so that we can re-evaluate the lock conditions.
4) We must enter pi-boost mode if, at any time, we decide to voluntarily preempt since we are losing our ability to dynamically process the conditions above.
Note: We still fully support priority inheritance with this protocol, even if we defer the low-level calls to adjust priority. The difference is really in terms of being a pro-active protocol (boost on entry), verses a reactive protocol (boost when necessary). The upside to the latter is that we don't take a penalty for pi when it is not necessary (which is most of the time) The downside is that we technically leave the owner exposed to getting preempted (should it get asynchronously deprioritized), even if our waiter is the highest priority task in the system. When this happens, the owner would be immediately boosted (because we would hit the "oncpu" condition, and subsequently follow the voluntary preempt path which boosts the owner). Therefore, inversion is correctly prevented, but we have the extra latency of the preempt/boost/wakeup that could have been avoided in the proactive model.
However, the design of the algorithm described above constrains the probability of this phenomenon occurring to setscheduler() operations. Since rt-locks do not support being interrupted by signals or timeouts, waiters only depart via the acquisition path. And while acquisitions do deboost the owner, the owner also changes simultaneously, rending the deboost moot relative to the other waiters.
What this all means is that the downside to this implementation is that a high-priority waiter *may* see an extra latency (equivalent to roughly two wake-ups) if the owner has its priority reduced via setscheduler() while it holds the lock. The penalty is deterministic, arguably small enough, and sufficiently rare that I do not believe it should be an issue.
Note: If the concept of other exit paths are ever introduced in the future, simply adapting the condition to look at owner->normal_prio instead of owner->prio should once again constrain the limitation to setscheduler().
Special thanks to Peter Morreale for suggesting the optimization to only consider skipping the boost if the owner is >= to current.
---
include/linux/rtmutex.h | 1 kernel/rtmutex.c | 195 ++++++++++++++++++++++++++++++++++++----------- kernel/rtmutex_common.h | 1 3 files changed, 153 insertions(+), 44 deletions(-)
include/linux/rtmutex.h +1 -0
@@ -33,6 +33,7 @@ struct rt_mutex {struct pi_node node;struct pi_sink sink;int prio; +int boosters; } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES int save_state;
kernel/rtmutex.c +151 -44
325 lines not on this page · raw message
kernel/rtmutex_common.h +1 -0
@@ -55,6 +55,7 @@ struct rt_mutex_waiter {struct {struct pi_sink sink;int prio; +int boosted; } pi;#ifdef CONFIG_DEBUG_RT_MUTEXES unsigned long ip;