Skip to contentSkip to conversation

Spinlock question.

Xenomai-core Gilles Chanteperdrix 4 participants 5 messages Mar 26, 2007

lore
Share & tools
lore
#0 lore raw

Hi,

I am porting a network driver from Linux to RTnet, and I have a sudden doubt about what I am doing: is it safe to call a function like wake_up_interruptible, from a non real-time context, with CONFIG_PREEMPT enabled, while holding an rthal spinlock ? Do not I risk a rescheduling with the spinlock locked ?

Signature · 1 line
                                                 Gilles Chanteperdrix
#1 lore raw
Quoted 7 lines · Gilles Chanteperdrix

Hi,

I am porting a network driver from Linux to RTnet, and I have a sudden doubt about what I am doing: is it safe to call a function like wake_up_interruptible, from a non real-time context, with CONFIG_PREEMPT enabled, while holding an rthal spinlock ? Do not I risk a rescheduling with the spinlock locked ?

Mmh, doesn't stalling the primary domain (what rthal_spin_lock_irqsave surely does) also make the root domain think it is running in interrupt context, thus will prevent any Linux reschedule?

However, there is one big fat issue with this approach:
wake_up_interruptible takes some Linux spin lock. So you must never call it from primary context on UP to avoid corruption. And an SMP, you create a nice source for priority inversion: CPU2 holds wait_queue lock, gets preempted by Xenomai and runs some longer low-prio RT job. Meanwhile CPU1 tries to acquire wait_queue as well with IRQs disabled, blocking any RT task on that CPU.

Better defer the Linux wake-up via a rtdm_nrtsig. Alternatively, make the Linux waiter also a Xenomai task and use a rtdm_event e.g. (mmh, maybe we should discuss the auto-shadow-any-Linux-task-on-demand feature for Xenomai once again...).

Jan

#2 lore raw
Quoted 10 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix

Hi,

I am porting a network driver from Linux to RTnet, and I have a sudden doubt about what I am doing: is it safe to call a function like wake_up_interruptible, from a non real-time context, with CONFIG_PREEMPT enabled, while holding an rthal spinlock ? Do not I risk a rescheduling with the spinlock locked ?

Quoted 3 lines

Mmh, doesn't stalling the primary domain (what rthal_spin_lock_irqsave surely does) also make the root domain think it is running in interrupt context, thus will prevent any Linux reschedule?

I don't know the PREEMPT_RT code. Although, I do remember I asked Philippe once and the subject was some code in nucleus/pipe.c

Just read the comments on PREEMPT_RT in xnpipe_wakeup_proc()... Philippe may comment more on that.

However, there is one big fat issue with this approach:

Quoted 6 lines

wake_up_interruptible takes some Linux spin lock. So you must never call it from primary context on UP to avoid corruption. And an SMP, you create a nice source for priority inversion: CPU2 holds wait_queue lock, gets preempted by Xenomai and runs some longer low-prio RT job. Meanwhile CPU1 tries to acquire wait_queue as well with IRQs disabled, blocking any RT task on that CPU.

Jan, Gilles is talking about calling this function from non-rt context :o)

"...is it safe to call a function like wake_up_interruptible, from a *non real-time* context, with CONFIG_PREEMPT"

And for calling it from the real-time context, It's not only about a spinlock but a list (of pending tasks) as well and, in general, any data this routine may access and which can be /inconsistent/ at the moment. So it's a no go.

Better defer the Linux wake-up via a rtdm_nrtsig. Alternatively, make

Quoted 14 lines

the Linux waiter also a Xenomai task and use a rtdm_event e.g. (mmh, maybe we should discuss the auto-shadow-any-Linux-task-on-demand feature for Xenomai once again...).

Jan

_______________________________________________
Xenomai-core mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-core

Signature · 2 lines
Best regards,
Dmitry Adamushko
#3 lore raw
In reply to Jan Kiszka
Quoted 11 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix

Hi,

I am porting a network driver from Linux to RTnet, and I have a sudden doubt about what I am doing: is it safe to call a function like wake_up_interruptible, from a non real-time context, with CONFIG_PREEMPT enabled, while holding an rthal spinlock ? Do not I risk a rescheduling with the spinlock locked ?

Quoted 3 lines

Mmh, doesn't stalling the primary domain (what rthal_spin_lock_irqsave surely does) also make the root domain think it is running in interrupt context, thus will prevent any Linux reschedule?

Is not it sufficient to add a call to preempt_disable/preempt_enable around the spinlock functions ?

Quoted 8 lines

However, there is one big fat issue with this approach:
wake_up_interruptible takes some Linux spin lock. So you must never call it from primary context on UP to avoid corruption. And an SMP, you create a nice source for priority inversion: CPU2 holds wait_queue lock, gets preempted by Xenomai and runs some longer low-prio RT job. Meanwhile CPU1 tries to acquire wait_queue as well with IRQs disabled, blocking any RT task on that CPU.

Quoted 4 lines

Better defer the Linux wake-up via a rtdm_nrtsig. Alternatively, make the Linux waiter also a Xenomai task and use a rtdm_event e.g. (mmh, maybe we should discuss the auto-shadow-any-Linux-task-on-demand feature for Xenomai once again...).

I have other cases where the function that does the wakeup do it from real-time context, so I will probably have to use the nrtsig approach anyway, thanks.

Signature · 1 line
                                                 Gilles Chanteperdrix
#4 lore raw
Quoted 12 lines · Gilles Chanteperdrix
Quoted 11 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix

Hi,

I am porting a network driver from Linux to RTnet, and I have a sudden doubt about what I am doing: is it safe to call a function like wake_up_interruptible, from a non real-time context, with CONFIG_PREEMPT enabled, while holding an rthal spinlock ? Do not I risk a rescheduling with the spinlock locked ?

Quoted 7 lines
Quoted 3 lines

Mmh, doesn't stalling the primary domain (what rthal_spin_lock_irqsave surely does) also make the root domain think it is running in interrupt context, thus will prevent any Linux reschedule?

Is not it sufficient to add a call to preempt_disable/preempt_enable around the spinlock functions ?

Yes, you could do that, and actually, you should do that, since rthal_* macros manipulate raw spinlocks, not preempt-aware ones. This said, as Jan pointed out, it would be pretty dangerous to hold such spinlock right before going to sleep Linux-wise, due to priority inversion issues.

The bottom-line is to never ever hold the nklock and anything that could stall the high pipeline stage before being switched out by the Linux scheduler.

Quoted 18 lines
Quoted 9 lines

However, there is one big fat issue with this approach:
wake_up_interruptible takes some Linux spin lock. So you must never call it from primary context on UP to avoid corruption. And an SMP, you create a nice source for priority inversion: CPU2 holds wait_queue lock, gets preempted by Xenomai and runs some longer low-prio RT job. Meanwhile CPU1 tries to acquire wait_queue as well with IRQs disabled, blocking any RT task on that CPU.

Quoted 4 lines

Better defer the Linux wake-up via a rtdm_nrtsig. Alternatively, make the Linux waiter also a Xenomai task and use a rtdm_event e.g. (mmh, maybe we should discuss the auto-shadow-any-Linux-task-on-demand feature for Xenomai once again...).

I have other cases where the function that does the wakeup do it from real-time context, so I will probably have to use the nrtsig approach anyway, thanks.

Signature · 1 line
Philippe.