Skip to contentSkip to conversation

2.5: don't miss a preemption oppurtunity

PATCH New Robert Love 1 participant 1 message Apr 6, 2002

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 2.5: don't miss a preemption oppurtunity

Linus,

The new kernel preemption code in 2.5 is great. The only missing bit is a check for need to reschedule at the end of the preemption handling code in preempt_schedule and entry.S. Anytime preemption is disabled, there is a possibility kernel code could set need_resched and try to preempt. We need to check need_resched as soon as we reenable preemption (preempt_count=0) in these code paths.

Right now this window exists anytime after resume_kernel sets preempt_count to 0 and after return from schedule when preempt_count is also set to 0. Note they can last as long as the next interrupt - or longer if a lock was grabbed in the interim. This is a decade in real-time land.

This patch, against 2.4.8-pre2, adds a simple check for need_resched to the end of these codepaths and jumps back into them if needed. Please apply.

Robert Love

kernel/sched.c +5 -0
diff -urN linux-2.5.8-pre2/kernel/sched.c linux/kernel/sched.c
@@ -851,10 +851,15 @@
 	if (unlikely(ti->preempt_count))
 		return;
 
+need_resched:
 	ti->preempt_count = PREEMPT_ACTIVE;
 	schedule();
 	ti->preempt_count = 0;
 	barrier();
+
+	/* we could miss a preemption between schedule() and now */
+	if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
+		goto need_resched;
 }
 #endif /* CONFIG_PREEMPT */
 
arch/i386/kernel/entry.S +4 -2
diff -urN linux-2.5.8-pre2/arch/i386/kernel/entry.S linux/arch/i386/kernel/entry.S
@@ -232,6 +232,7 @@
 ENTRY(resume_kernel)
 	cmpl $0,TI_PRE_COUNT(%ebx)
 	jnz restore_all
+need_resched:
 	movl TI_FLAGS(%ebx), %ecx
 	testb $_TIF_NEED_RESCHED, %cl
 	jz restore_all
@@ -241,8 +242,9 @@
 	movl $PREEMPT_ACTIVE,TI_PRE_COUNT(%ebx)
 	sti
 	call SYMBOL_NAME(schedule)
-	movl $0,TI_PRE_COUNT(%ebx) 
-	jmp restore_all
+	movl $0,TI_PRE_COUNT(%ebx)
+	cli
+	jmp need_resched
 #endif
 
 	# system call handler stub