Realtime thread priorities

John Baldwin jhb at freebsd.org
Tue Dec 14 12:57:16 UTC 2010


On Monday, December 13, 2010 7:37:49 pm Sergey Babkin wrote:
> John Baldwin wrote:
> > 
> > On Sunday, December 12, 2010 3:06:20 pm Sergey Babkin wrote:
> > > John Baldwin wrote:
> > > >
> > > > The current layout breaks up the global thread priority space (0 - 255)
> > into a
> > > > couple of bands:
> > > >
> > > >   0 -  63 : interrupt threads
> > > >  64 - 127 : kernel sleep priorities (PSOCK, etc.)
> > > > 128 - 159 : real-time user threads (rtprio)
> > > > 160 - 223 : time-sharing user threads
> > > > 224 - 255 : idle threads (idprio and kernel idle procs)
> > > >
> > > > If we decide to change the behavior I see two possible fixes:
> > > >
> > > > 1) (easy) just move the real-time priority range above the kernel sleep
> > > > priority range
> > >
> > > Would not this cause a priority inversion when an RT process
> > > enters the kernel mode?
> > 
> > How so?  Note that timesharing threads are not "bumped" to a kernel sleep
> > priority when they enter the kernel either.  The kernel sleep priorities are
> > purely a way for certain sleep channels to cause a thread to be treated as
> > interactive and give it a priority boost to favor interactive threads.
> > Threads in the kernel do not automatically have higher priority than threads
> > not in the kernel.  Keep in mind that all stopped threads (threads not
> > executing) are always in the kernel when they stop.
> 
> I may be a bit behind the times here. But historically the "default"
> process priority means the priority when the process was pre-empted.
> If it did a system call, the priority on wake up would be as
> specified in the sleep() kernel function (or its more modern
> analog, like a sleeplock or condition variable). This would 
> let the kernel code react quickly, and then on return from 
> the syscall revert to the original priority, and possibly 
> get pre-empted by another process at that time.

Except we don't do an explicit check in userret() to see if we should preempt
when we drop the priority.  We effectively let the process/thread run at the
higher "sleep" priority until either 1) it's quantum expires, or 2) an
interrupt causes a preemption due to some other higher priority thread being
scheduled.  However, if a higher priority thread is already on the run queue
when we return to userland, it will not be preempted to.  That is what the 2)
suggestion in the original e-mail was about.

> If the user-mode priority is higher than the kernel-mode priority,
> this would mean that once a high priority process does a system
> call (say for example, poll()), it would experience a priority
> inversion and sleep with a lower priority than specified.

That's what this part of the patch for 1) is about:

Index: kern/kern_synch.c
===================================================================
--- kern/kern_synch.c   (revision 215592)
+++ kern/kern_synch.c   (working copy)
@@ -214,7 +214,8 @@
         * Adjust this thread's priority, if necessary.
         */
        pri = priority & PRIMASK;
-       if (pri != 0 && pri != td->td_priority) {
+       if (pri != 0 && pri != td->td_priority &&
+           td->td_pri_class == PRI_TIMESHARE) {
                thread_lock(td);
                sched_prio(td, pri);
                thread_unlock(td);

This avoids the priority inversion.  It also avoids giving a bump to an
'idprio' thread.  Note that if any thread holds a mutex or rwlock that a
higher priority thread needs, we lend the priority to the lock holder while
the mutex is held and we will preempt to the higher priority thread when the
mutex is released.

-- 
John Baldwin


More information about the freebsd-arch mailing list