TDF_NEEDRESCHED (was: Realtime thread priorities)

John Baldwin jhb at
Wed Dec 29 14:04:44 UTC 2010

On Wednesday, December 29, 2010 3:22:24 am David Xu wrote:
> Hi all,
> I think flag TDF_NEEDRESCHED should not be cleared by sched_switch() in
> ULE or 4BSD, instead it should only be cleared by ast() in subr_trap.c.
> The reason is that the flag indicates thread should reset its priority
> and switch context at user boundary because its user mode priority is
> lowered or there is higher priority thread wants to run.
> Kernel needs to use this flag to reset its priority to td_user_pri
> before a thread returns to user mode, in current code, if an interrupt
> thread preempts a user thread, sched_switch() clears the flag for
> preempted thread and then switches to preempting thread, this causes
> preempted thread to forget resetting its current priority to td_user_pri
> this becauses assemble language code doreti() can not find the flag,
> and ast() is not called, the thread ends up running user mode code
> at very high level priority. Fix me, if I am wrong.

Hmm, I think you are correct in which case I broke this many years ago. :(
I think it might have worked originally because TDF_NEEDRESCHED triggered the 
preemption itself in ast().  The bug was probably introduced when I moved 
preemption into setrunqueue() itself (now sched_add() / critical_exit()).
I think originally we also set TDF_NEEDRESCHED more often than we needed to, 
but now we probably don't (though we should check that it is now only set when 
we need to run that particular bit of code in ast() and nowhere else).

John Baldwin

More information about the freebsd-arch mailing list