Skip to contentSkip to conversation

bre^H^H^H^H reworking self deletion, take 3.

PATCH In review Gilles Chanteperdrix 2 participants 5 messages Jan 31, 2008 – Feb 1, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bre^H^H^H^H reworking self deletion, take 3.

Hi Philippe,

here comes a new patch on the theme "reworking self deletion". This time, execution of nkpod delete hooks is made before the context switch whereas finalizing the thread takes place after the context switch. Special care has been taken to call xnfreesync before we run the hooks, in order to avoid freeing the thread control block before the finalization, and xnheap_t::idleq was made a per-cpu thing for the same purpose.

I made a simple watchdog test with all debugs enabled, and xnshadow_unmap did not complain.

If you are OK with this patch, I will rebase the unlocked context switch patch on it.

Signature · 1 line
					    Gilles Chanteperdrix.
asm-ia64/bits/pod.h +0 -6
Index: include/asm-ia64/bits/pod.h
===================================================================
@@ -100,12 +100,6 @@ static inline void xnarch_switch_to(xnar
 	}
 }
 
-static inline void xnarch_finalize_and_switch(xnarchtcb_t * dead_tcb,
-					      xnarchtcb_t * next_tcb)
-{
-	xnarch_switch_to(dead_tcb, next_tcb);
-}
-
 static inline void xnarch_finalize_no_switch(xnarchtcb_t * dead_tcb)
 {
 	/* Empty */
asm-blackfin/bits/pod.h +0 -6
Index: include/asm-blackfin/bits/pod.h
===================================================================
@@ -67,12 +67,6 @@ static inline void xnarch_switch_to(xnar
 	rthal_thread_switch(out_tcb->tsp, in_tcb->tsp);
 }
 
-static inline void xnarch_finalize_and_switch(xnarchtcb_t * dead_tcb,
-					      xnarchtcb_t * next_tcb)
-{
-	xnarch_switch_to(dead_tcb, next_tcb);
-}
-
 static inline void xnarch_finalize_no_switch(xnarchtcb_t * dead_tcb)
 {
 	/* Empty */
asm-powerpc/bits/pod.h +0 -6
Index: include/asm-powerpc/bits/pod.h
===================================================================
@@ -106,12 +106,6 @@ static inline void xnarch_switch_to(xnar
 	barrier();
 }
 
-static inline void xnarch_finalize_and_switch(xnarchtcb_t * dead_tcb,
-					      xnarchtcb_t * next_tcb)
-{
-	xnarch_switch_to(dead_tcb, next_tcb);
-}
-
 static inline void xnarch_finalize_no_switch(xnarchtcb_t * dead_tcb)
 {
 	/* Empty */
asm-arm/bits/pod.h +0 -6
Index: include/asm-arm/bits/pod.h
===================================================================
@@ -96,12 +96,6 @@ static inline void xnarch_switch_to(xnar
 	rthal_thread_switch(prev, out_tcb->tip, in_tcb->tip);
 }
 
-static inline void xnarch_finalize_and_switch(xnarchtcb_t * dead_tcb,
-					      xnarchtcb_t * next_tcb)
-{
-	xnarch_switch_to(dead_tcb, next_tcb);
-}
-
 static inline void xnarch_finalize_no_switch(xnarchtcb_t * dead_tcb)
 {
 	/* Empty */
asm-x86/bits/pod_64.h +0 -6
Index: include/asm-x86/bits/pod_64.h
===================================================================
@@ -96,12 +96,6 @@ static inline void xnarch_switch_to(xnar
 	stts();
 }
 
-static inline void xnarch_finalize_and_switch(xnarchtcb_t * dead_tcb,
-					      xnarchtcb_t * next_tcb)
-{
-	xnarch_switch_to(dead_tcb, next_tcb);
-}
-
 static inline void xnarch_finalize_no_switch(xnarchtcb_t * dead_tcb)
 {
 	/* Empty */
asm-x86/bits/pod_32.h +0 -6
Index: include/asm-x86/bits/pod_32.h
===================================================================
@@ -123,12 +123,6 @@ static inline void xnarch_switch_to(xnar
 	stts();
 }
 
-static inline void xnarch_finalize_and_switch(xnarchtcb_t * dead_tcb,
-					      xnarchtcb_t * next_tcb)
-{
-	xnarch_switch_to(dead_tcb, next_tcb);
-}
-
 static inline void xnarch_finalize_no_switch(xnarchtcb_t * dead_tcb)
 {
 	/* Empty */
asm-sim/bits/pod.h +0 -6
Index: include/asm-sim/bits/pod.h
===================================================================
@@ -38,12 +38,6 @@ static inline void xnarch_switch_to (xna
     __mvm_breakable(mvm_switch_threads)(out_tcb->vmthread,in_tcb->vmthread);
 }
 
-static inline void xnarch_finalize_and_switch (xnarchtcb_t *dead_tcb,
-					       xnarchtcb_t *next_tcb)
-{
-    mvm_finalize_switch_threads(dead_tcb->vmthread,next_tcb->vmthread);
-}
-
 static inline void xnarch_finalize_no_switch (xnarchtcb_t *dead_tcb)
 {
     if (dead_tcb->vmthread)	/* Might be unstarted. */
nucleus/heap.h +1 -1
Index: include/nucleus/heap.h
===================================================================
@@ -97,7 +97,7 @@ typedef struct xnheap {
 		int fcount;
 	} buckets[XNHEAP_NBUCKETS];
 
-	xnholder_t *idleq;
+	xnholder_t *idleq[XNARCH_NR_CPUS];
 
 	xnarch_heapcb_t archdep;
 
nucleus/pod.h +9 -0
Index: include/nucleus/pod.h
===================================================================
@@ -139,6 +139,7 @@ typedef struct xnsched {
 
 	xntimer_t htimer;	/*!< Host timer. */
 
+	xnqueue_t zombies;
 } xnsched_t;
 
 #define nkpod (&nkpod_struct)
@@ -238,6 +239,14 @@ static inline void xnpod_reset_watchdog(
 }
 #endif /* CONFIG_XENO_OPT_WATCHDOG */
 
+void __xnpod_finalize_zombies(xnsched_t *sched);
+
+static inline void xnpod_finalize_zombies(xnsched_t *sched)
+{
+	if (!emptyq_p(&sched->zombies))
+		__xnpod_finalize_zombies(sched);
+}
+
 	/* -- Beginning of the exported interface */
 
 #define xnpod_sched_slot(cpu) \
nucleus/heap.c +11 -5
Index: ksrc/nucleus/heap.c
===================================================================
@@ -163,6 +163,7 @@ static void init_extent(xnheap_t *heap, 
 int xnheap_init(xnheap_t *heap,
 		void *heapaddr, u_long heapsize, u_long pagesize)
 {
+	unsigned cpu, nr_cpus = xnarch_num_online_cpus();
 	u_long hdrsize, shiftsize, pageshift;
 	xnextent_t *extent;
 
@@ -218,7 +219,8 @@ int xnheap_init(xnheap_t *heap,
 
 	heap->ubytes = 0;
 	heap->maxcont = heap->npages * pagesize;
-	heap->idleq = NULL;
+	for (cpu = 0; cpu < nr_cpus; cpu++)
+		heap->idleq[cpu] = NULL;
 	inith(&heap->link);
 	initq(&heap->extents);
 	xnlock_init(&heap->lock);
@@ -866,6 +868,7 @@ int xnheap_extend(xnheap_t *heap, void *
 
 void xnheap_schedule_free(xnheap_t *heap, void *block, xnholder_t *link)
 {
+	unsigned cpu;
 	spl_t s;
 
 	xnlock_get_irqsave(&heap->lock, s);
@@ -873,21 +876,24 @@ void xnheap_schedule_free(xnheap_t *heap
 	   idle objects through the 'next' field, so the 'last' field of
 	   the link is used to point at the beginning of the freed
 	   memory. */
+	cpu = xnarch_current_cpu();
 	link->last = (xnholder_t *)block;
-	link->next = heap->idleq;
-	heap->idleq = link;
+	link->next = heap->idleq[cpu];
+	heap->idleq[cpu] = link;
 	xnlock_put_irqrestore(&heap->lock, s);
 }
 
 void xnheap_finalize_free_inner(xnheap_t *heap)
 {
 	xnholder_t *holder;
+	unsigned cpu;
 	spl_t s;
 
 	xnlock_get_irqsave(&heap->lock, s);
+	cpu = xnarch_current_cpu();
 
-	while ((holder = heap->idleq) != NULL) {
-		heap->idleq = holder->next;
+	while ((holder = heap->idleq[cpu]) != NULL) {
+		heap->idleq[cpu] = holder->next;
 		xnheap_free(heap, holder->last);
 	}
 
nucleus/pod.c +63 -77

241 lines not on this page · raw message

nucleus/shadow.c +5 -1
Index: ksrc/nucleus/shadow.c
===================================================================
@@ -1059,6 +1059,7 @@ int xnshadow_harden(void)
 	struct task_struct *this_task = current;
 	struct __gatekeeper *gk;
 	xnthread_t *thread;
+	xnsched_t *sched;
 	int gk_cpu;
 
 redo:
@@ -1124,9 +1125,12 @@ redo:
 	}
 
 	/* "current" is now running into the Xenomai domain. */
+	sched = xnpod_current_sched();
+
+	xnpod_finalize_zombies(sched);
 
 #ifdef CONFIG_XENO_HW_FPU
-	xnpod_switch_fpu(xnpod_current_sched());
+	xnpod_switch_fpu(sched);
 #endif /* CONFIG_XENO_HW_FPU */
 
 	xnarch_schedule_tail(this_task);
#1 lore raw
Quoted 11 lines · Gilles Chanteperdrix

Hi Philippe,

here comes a new patch on the theme "reworking self deletion". This time, execution of nkpod delete hooks is made before the context switch whereas finalizing the thread takes place after the context switch. Special care has been taken to call xnfreesync before we run the hooks, in order to avoid freeing the thread control block before the finalization, and xnheap_t::idleq was made a per-cpu thing for the same purpose.

Quoted 5 lines

I made a simple watchdog test with all debugs enabled, and xnshadow_unmap did not complain.

If you are OK with this patch, I will rebase the unlocked context switch patch on it.

Thinking a bit more about the unlocked context switch case: do we tolerate that an ISR may delete a thread that is not current ? Because if an ISR deleted a non current thread, we would not run the hooks over the deleted thread context, so we would be again in the case where xnshadow_unmap is not run by current. Besides, at first sight, it seems to simplify greatly the case of the unlocked context switch.

Signature · 1 line
					    Gilles Chanteperdrix.
#2 lore raw
Quoted 18 lines · Gilles Chanteperdrix
Quoted 11 lines · Gilles Chanteperdrix

Hi Philippe,

here comes a new patch on the theme "reworking self deletion". This time, execution of nkpod delete hooks is made before the context switch whereas finalizing the thread takes place after the context switch. Special care has been taken to call xnfreesync before we run the hooks, in order to avoid freeing the thread control block before the finalization, and xnheap_t::idleq was made a per-cpu thing for the same purpose.

Quoted 5 lines

I made a simple watchdog test with all debugs enabled, and xnshadow_unmap did not complain.

If you are OK with this patch, I will rebase the unlocked context switch patch on it.

Quoted 7 lines

Thinking a bit more about the unlocked context switch case: do we tolerate that an ISR may delete a thread that is not current ? Because if an ISR deleted a non current thread, we would not run the hooks over the deleted thread context, so we would be again in the case where xnshadow_unmap is not run by current. Besides, at first sight, it seems to simplify greatly the case of the unlocked context switch.

No we don't; thread deletion is normally a service callable from thread context only. We allow the watchdog code to call into xnpod_delete_thread() as an exception, because we have no other mean to fix the runaway situation properly, and we know that this will work precisely because the deleted thread is current.

Any other situation would lockup, because the termination signal would never make it to the runaway thread since Linux is starved from CPU at that point, and we can't even relax the thread either. So, yes, we may safely assume that thread deletion is either called from a thread, or called on behalf of an ISR for the preempted thread only (and solely for internal code and well-know situations).

Signature · 1 line
Philippe.
#3 lore raw
Quoted 34 lines · Philippe Gerum
Quoted 18 lines · Gilles Chanteperdrix
Quoted 11 lines · Gilles Chanteperdrix

Hi Philippe,

here comes a new patch on the theme "reworking self deletion". This time, execution of nkpod delete hooks is made before the context switch whereas finalizing the thread takes place after the context switch. Special care has been taken to call xnfreesync before we run the hooks, in order to avoid freeing the thread control block before the finalization, and xnheap_t::idleq was made a per-cpu thing for the same purpose.

Quoted 5 lines

I made a simple watchdog test with all debugs enabled, and xnshadow_unmap did not complain.

If you are OK with this patch, I will rebase the unlocked context switch patch on it.

Quoted 7 lines

Thinking a bit more about the unlocked context switch case: do we tolerate that an ISR may delete a thread that is not current ? Because if an ISR deleted a non current thread, we would not run the hooks over the deleted thread context, so we would be again in the case where xnshadow_unmap is not run by current. Besides, at first sight, it seems to simplify greatly the case of the unlocked context switch.

No we don't; thread deletion is normally a service callable from thread context only. We allow the watchdog code to call into xnpod_delete_thread() as an exception, because we have no other mean to fix the runaway situation properly, and we know that this will work precisely because the deleted thread is current.

Quoted 6 lines

Any other situation would lockup, because the termination signal would never make it to the runaway thread since Linux is starved from CPU at that point, and we can't even relax the thread either. So, yes, we may safely assume that thread deletion is either called from a thread, or called on behalf of an ISR for the preempted thread only (and solely for internal code and well-know situations).

Unfortunately, my reasoning was UP only, in SMP, it may happen that a distant CPU deletes from a valid context the thread being currently switched out with nklock unlocked on current CPU. So, we have to take care about this case anyway.

Signature · 1 line
                                               Gilles Chanteperdrix
#4 lore raw
Quoted 41 lines · Gilles Chanteperdrix
Quoted 33 lines · Philippe Gerum
Quoted 18 lines · Gilles Chanteperdrix
Quoted 11 lines · Gilles Chanteperdrix

Hi Philippe,

here comes a new patch on the theme "reworking self deletion". This time, execution of nkpod delete hooks is made before the context switch whereas finalizing the thread takes place after the context switch. Special care has been taken to call xnfreesync before we run the hooks, in order to avoid freeing the thread control block before the finalization, and xnheap_t::idleq was made a per-cpu thing for the same purpose.

Quoted 5 lines

I made a simple watchdog test with all debugs enabled, and xnshadow_unmap did not complain.

If you are OK with this patch, I will rebase the unlocked context switch patch on it.

Quoted 7 lines

Thinking a bit more about the unlocked context switch case: do we tolerate that an ISR may delete a thread that is not current ? Because if an ISR deleted a non current thread, we would not run the hooks over the deleted thread context, so we would be again in the case where xnshadow_unmap is not run by current. Besides, at first sight, it seems to simplify greatly the case of the unlocked context switch.

No we don't; thread deletion is normally a service callable from thread context only. We allow the watchdog code to call into xnpod_delete_thread() as an exception, because we have no other mean to fix the runaway situation properly, and we know that this will work precisely because the deleted thread is current.

Quoted 6 lines

Any other situation would lockup, because the termination signal would never make it to the runaway thread since Linux is starved from CPU at that point, and we can't even relax the thread either. So, yes, we may safely assume that thread deletion is either called from a thread, or called on behalf of an ISR for the preempted thread only (and solely for internal code and well-know situations).

Quoted 5 lines

Unfortunately, my reasoning was UP only, in SMP, it may happen that a distant CPU deletes from a valid context the thread being currently switched out with nklock unlocked on current CPU. So, we have to take care about this case anyway.

IIUC, this should only be a problem with kernel threads, others would end up self-deleting on behalf of a termination signal from the remote CPU.

Signature · 1 line
Philippe.