Skip to contentSkip to conversation

Heads up: some race condition fixes for Xenomai 3

Xenomai Jan Kiszka 4 participants 11 messages Jun 26, 2015 – Mar 9, 2017

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

Hi,

just pushed 3 patches to git.xenomai.org/xenomai-jki.git for-forge that are supposed to fix race conditions while manipulating xnthread::state and info (both need to be nklock-protected). Please review if finding and fixes make sense.

cobalt/kernel: Fix locking for xnthread info manipulations cobalt/kernel: Fix locking for setting XNFPU cobalt/kernel: Rework thread debugging helpers

Maybe some of the issues also exist in Xenomai 2, didn't check yet.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE
Corporate Competence Center Embedded Linux
#1 lore raw
Quoted 7 lines · Jan Kiszka

Hi,

just pushed 3 patches to git.xenomai.org/xenomai-jki.git for-forge that are supposed to fix race conditions while manipulating xnthread::state and info (both need to be nklock-protected). Please review if finding and fixes make sense.

Quoted 5 lines

cobalt/kernel: Fix locking for xnthread info manipulations cobalt/kernel: Fix locking for setting XNFPU cobalt/kernel: Rework thread debugging helpers

Maybe some of the issues also exist in Xenomai 2, didn't check yet.

At some point, one of info and state was supposed to be only modified by the current thread, but I am not sure this still holds.

Anyway, instead of adding explicit nklock sections around xnthread_clear_info/xnthread_set_info, would not it be better to have xnthread_clear_info/xnthread_set_info implicitly take the lock, and add __xnthread_clear_info/__xnthread_set_info for the sections where the caller already holds the nklock?

Signature · 2 lines
					    Gilles.
https://click-hack.org
author
#2 lore raw
Quoted 8 lines · Gilles Chanteperdrix
Quoted 7 lines · Jan Kiszka

Hi,

just pushed 3 patches to git.xenomai.org/xenomai-jki.git for-forge that are supposed to fix race conditions while manipulating xnthread::state and info (both need to be nklock-protected). Please review if finding and fixes make sense.

Quoted 8 lines
Quoted 5 lines

cobalt/kernel: Fix locking for xnthread info manipulations cobalt/kernel: Fix locking for setting XNFPU cobalt/kernel: Rework thread debugging helpers

Maybe some of the issues also exist in Xenomai 2, didn't check yet.

At some point, one of info and state was supposed to be only modified by the current thread, but I am not sure this still holds.

I vaguely remember something like this as well. From my analysis of today, this is no longer the case.

Quoted 6 lines

Anyway, instead of adding explicit nklock sections around xnthread_clear_info/xnthread_set_info, would not it be better to have xnthread_clear_info/xnthread_set_info implicitly take the lock, and add __xnthread_clear_info/__xnthread_set_info for the sections where the caller already holds the nklock?

That is an option, though we likely only have very few users of self-locking variants.

Or we make those fields atomics, but that would affect all users.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE
Corporate Competence Center Embedded Linux
#3 lore raw
In reply to Jan Kiszka

Am Fri, 26 Jun 2015 16:20:29 +0200
schrieb Jan Kiszka <[email protected]>:

Quoted 7 lines

Hi,

just pushed 3 patches to git.xenomai.org/xenomai-jki.git for-forge that are supposed to fix race conditions while manipulating xnthread::state and info (both need to be nklock-protected). Please review if finding and fixes make sense.

Quoted 5 lines

cobalt/kernel: Fix locking for xnthread info manipulations cobalt/kernel: Fix locking for setting XNFPU cobalt/kernel: Rework thread debugging helpers

Maybe some of the issues also exist in Xenomai 2, didn't check yet.

After looking deeper into the the mysterious -EINTR i asked about a few days ago we now got a trace that suggests something is going wrong. Jan remembered the race in thread flag manipulation he found in Xeno3.

I did not do a thorough code analysis yet but instead just put two asserts into xnthread_set_info and xnthread_clear_info.
1. !xnlock_is_owner(&nklock)
2. xnpod_current_thread() != thread_to_update

Both cases do happen. The flags are manipulated without holding the lock and the flags are manipulated from another context. I guess that suggests that the race found in xenomai3 is also in xenomai2.

Henning

#4 lore raw
Quoted 20 lines · Henning Schild

Am Fri, 26 Jun 2015 16:20:29 +0200
schrieb Jan Kiszka <[email protected]>:

Quoted 7 lines

Hi,

just pushed 3 patches to git.xenomai.org/xenomai-jki.git for-forge that are supposed to fix race conditions while manipulating xnthread::state and info (both need to be nklock-protected). Please review if finding and fixes make sense.

Quoted 5 lines

cobalt/kernel: Fix locking for xnthread info manipulations cobalt/kernel: Fix locking for setting XNFPU cobalt/kernel: Rework thread debugging helpers

Maybe some of the issues also exist in Xenomai 2, didn't check yet.

After looking deeper into the the mysterious -EINTR i asked about a few days ago we now got a trace that suggests something is going wrong. Jan remembered the race in thread flag manipulation he found in Xeno3.

Quoted 9 lines

I did not do a thorough code analysis yet but instead just put two asserts into xnthread_set_info and xnthread_clear_info.
1. !xnlock_is_owner(&nklock)
2. xnpod_current_thread() != thread_to_update

Both cases do happen. The flags are manipulated without holding the lock and the flags are manipulated from another context. I guess that suggests that the race found in xenomai3 is also in xenomai2.

I would not compare both code bases. Much rewrite took place from the legacy nucleus to the cobalt core.

I have reviewed every single statement involving set/clear info bits in 3.x and I can't seem to find any unlocked access for those. Any specifics about the exact locations where your debug statements trigger?

Signature · 1 line
Philippe.
author
#5 lore raw
Quoted 31 lines · Philippe Gerum
Quoted 20 lines · Henning Schild

Am Fri, 26 Jun 2015 16:20:29 +0200
schrieb Jan Kiszka <[email protected]>:

Quoted 7 lines

Hi,

just pushed 3 patches to git.xenomai.org/xenomai-jki.git for-forge that are supposed to fix race conditions while manipulating xnthread::state and info (both need to be nklock-protected). Please review if finding and fixes make sense.

Quoted 5 lines

cobalt/kernel: Fix locking for xnthread info manipulations cobalt/kernel: Fix locking for setting XNFPU cobalt/kernel: Rework thread debugging helpers

Maybe some of the issues also exist in Xenomai 2, didn't check yet.

After looking deeper into the the mysterious -EINTR i asked about a few days ago we now got a trace that suggests something is going wrong. Jan remembered the race in thread flag manipulation he found in Xeno3.

Quoted 9 lines

I did not do a thorough code analysis yet but instead just put two asserts into xnthread_set_info and xnthread_clear_info.
1. !xnlock_is_owner(&nklock)
2. xnpod_current_thread() != thread_to_update

Both cases do happen. The flags are manipulated without holding the lock and the flags are manipulated from another context. I guess that suggests that the race found in xenomai3 is also in xenomai2.

Quoted 7 lines

I would not compare both code bases. Much rewrite took place from the legacy nucleus to the cobalt core.

I have reviewed every single statement involving set/clear info bits in 3.x and I can't seem to find any unlocked access for those. Any specifics about the exact locations where your debug statements trigger?

One quickly discoverable example is in xnshadow_harden (xnthread_set/clear_info(curr, XNATOMIC) without nklock protection). And Henning also confirmed that the info field is not used only thread-locally, though I don't have his finding in mind. I could imagine, though, that do_sigwake_event would make a good one.

So I'm pretty sure we have the same issue in Xenomai 2 as in 3. Too bad I didn't follow up on the backport topic back then. Do you see any reasons why that could be complicated?

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA ITP SES-DE
Corporate Competence Center Embedded Linux
#6 lore raw
Quoted 35 lines · Jan Kiszka
Quoted 34 lines · Philippe Gerum
Quoted 20 lines · Henning Schild

Am Fri, 26 Jun 2015 16:20:29 +0200
schrieb Jan Kiszka <[email protected]>:

Quoted 7 lines

Hi,

just pushed 3 patches to git.xenomai.org/xenomai-jki.git for-forge that are supposed to fix race conditions while manipulating xnthread::state and info (both need to be nklock-protected). Please review if finding and fixes make sense.

Quoted 5 lines

cobalt/kernel: Fix locking for xnthread info manipulations cobalt/kernel: Fix locking for setting XNFPU cobalt/kernel: Rework thread debugging helpers

Maybe some of the issues also exist in Xenomai 2, didn't check yet.

After looking deeper into the the mysterious -EINTR i asked about a few days ago we now got a trace that suggests something is going wrong. Jan remembered the race in thread flag manipulation he found in Xeno3.

Quoted 9 lines

I did not do a thorough code analysis yet but instead just put two asserts into xnthread_set_info and xnthread_clear_info.
1. !xnlock_is_owner(&nklock)
2. xnpod_current_thread() != thread_to_update

Both cases do happen. The flags are manipulated without holding the lock and the flags are manipulated from another context. I guess that suggests that the race found in xenomai3 is also in xenomai2.

I would not compare both code bases. Much rewrite took place from the legacy nucleus to the cobalt core.

Quoted 7 lines
Quoted 4 lines

I have reviewed every single statement involving set/clear info bits in 3.x and I can't seem to find any unlocked access for those. Any specifics about the exact locations where your debug statements trigger?

One quickly discoverable example is in xnshadow_harden (xnthread_set/clear_info(curr, XNATOMIC) without nklock protection). And

This is v2. I'm referring to v3.

Signature · 1 line
Philippe.
author
#7 lore raw
Quoted 41 lines · Philippe Gerum
Quoted 40 lines · Jan Kiszka
Quoted 38 lines · Philippe Gerum
Quoted 20 lines · Henning Schild

Am Fri, 26 Jun 2015 16:20:29 +0200
schrieb Jan Kiszka <[email protected]>:

Quoted 7 lines

Hi,

just pushed 3 patches to git.xenomai.org/xenomai-jki.git for-forge that are supposed to fix race conditions while manipulating xnthread::state and info (both need to be nklock-protected). Please review if finding and fixes make sense.

Quoted 5 lines

cobalt/kernel: Fix locking for xnthread info manipulations cobalt/kernel: Fix locking for setting XNFPU cobalt/kernel: Rework thread debugging helpers

Maybe some of the issues also exist in Xenomai 2, didn't check yet.

After looking deeper into the the mysterious -EINTR i asked about a few days ago we now got a trace that suggests something is going wrong. Jan remembered the race in thread flag manipulation he found in Xeno3.

Quoted 9 lines

I did not do a thorough code analysis yet but instead just put two asserts into xnthread_set_info and xnthread_clear_info.
1. !xnlock_is_owner(&nklock)
2. xnpod_current_thread() != thread_to_update

Both cases do happen. The flags are manipulated without holding the lock and the flags are manipulated from another context. I guess that suggests that the race found in xenomai3 is also in xenomai2.

I would not compare both code bases. Much rewrite took place from the legacy nucleus to the cobalt core.

I have reviewed every single statement involving set/clear info bits in 3.x and I can't seem to find any unlocked access for those. Any specifics about the exact locations where your debug statements trigger?

Quoted 6 lines
Quoted 2 lines

One quickly discoverable example is in xnshadow_harden (xnthread_set/clear_info(curr, XNATOMIC) without nklock protection). And

This is v2. I'm referring to v3.

I think we are talking past each out: v3 is fixed by c35b5bbfabef, this request is about a potential backport to v2.6, and Henning's test was run on 2.6.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA ITP SES-DE
Corporate Competence Center Embedded Linux
#8 lore raw
Quoted 45 lines · Jan Kiszka
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
Quoted 31 lines · Philippe Gerum
Quoted 20 lines · Henning Schild

Am Fri, 26 Jun 2015 16:20:29 +0200
schrieb Jan Kiszka <[email protected]>:

Quoted 7 lines

Hi,

just pushed 3 patches to git.xenomai.org/xenomai-jki.git for-forge that are supposed to fix race conditions while manipulating xnthread::state and info (both need to be nklock-protected). Please review if finding and fixes make sense.

Quoted 5 lines

cobalt/kernel: Fix locking for xnthread info manipulations cobalt/kernel: Fix locking for setting XNFPU cobalt/kernel: Rework thread debugging helpers

Maybe some of the issues also exist in Xenomai 2, didn't check yet.

After looking deeper into the the mysterious -EINTR i asked about a few days ago we now got a trace that suggests something is going wrong. Jan remembered the race in thread flag manipulation he found in Xeno3.

Quoted 9 lines

I did not do a thorough code analysis yet but instead just put two asserts into xnthread_set_info and xnthread_clear_info.
1. !xnlock_is_owner(&nklock)
2. xnpod_current_thread() != thread_to_update

Both cases do happen. The flags are manipulated without holding the lock and the flags are manipulated from another context. I guess that suggests that the race found in xenomai3 is also in xenomai2.

Quoted 7 lines

I would not compare both code bases. Much rewrite took place from the legacy nucleus to the cobalt core.

I have reviewed every single statement involving set/clear info bits in 3.x and I can't seem to find any unlocked access for those. Any specifics about the exact locations where your debug statements trigger?

One quickly discoverable example is in xnshadow_harden (xnthread_set/clear_info(curr, XNATOMIC) without nklock protection). And

Quoted 8 lines
Quoted 3 lines

This is v2. I'm referring to v3.

I think we are talking past each out: v3 is fixed by c35b5bbfabef, this request is about a potential backport to v2.6, and Henning's test was run on 2.6.

Ok, I was mislead by the subject line. So to answer your last question, I don't think we could bluntly backport the v3 fixes due to significant differences between v2 and v3 there (e.g. XNATOMIC does not exist in v3 anymore), however a patient review of all statements manipulating the state and info bits in v2 like you did for v3 should be a reasonable effort.

Signature · 1 line
Philippe.
#9 lore raw
Quoted 55 lines · Philippe Gerum
Quoted 45 lines · Jan Kiszka
Quoted 44 lines · Philippe Gerum
Quoted 42 lines · Jan Kiszka
Quoted 31 lines · Philippe Gerum
Quoted 20 lines · Henning Schild

Am Fri, 26 Jun 2015 16:20:29 +0200
schrieb Jan Kiszka <[email protected]>:

Quoted 7 lines

Hi,

just pushed 3 patches to git.xenomai.org/xenomai-jki.git for-forge that are supposed to fix race conditions while manipulating xnthread::state and info (both need to be nklock-protected). Please review if finding and fixes make sense.

Quoted 5 lines

cobalt/kernel: Fix locking for xnthread info manipulations cobalt/kernel: Fix locking for setting XNFPU cobalt/kernel: Rework thread debugging helpers

Maybe some of the issues also exist in Xenomai 2, didn't check yet.

After looking deeper into the the mysterious -EINTR i asked about a few days ago we now got a trace that suggests something is going wrong. Jan remembered the race in thread flag manipulation he found in Xeno3.

Quoted 9 lines

I did not do a thorough code analysis yet but instead just put two asserts into xnthread_set_info and xnthread_clear_info.
1. !xnlock_is_owner(&nklock)
2. xnpod_current_thread() != thread_to_update

Both cases do happen. The flags are manipulated without holding the lock and the flags are manipulated from another context. I guess that suggests that the race found in xenomai3 is also in xenomai2.

Quoted 7 lines

I would not compare both code bases. Much rewrite took place from the legacy nucleus to the cobalt core.

I have reviewed every single statement involving set/clear info bits in 3.x and I can't seem to find any unlocked access for those. Any specifics about the exact locations where your debug statements trigger?

One quickly discoverable example is in xnshadow_harden (xnthread_set/clear_info(curr, XNATOMIC) without nklock protection). And

Quoted 8 lines
Quoted 3 lines

This is v2. I'm referring to v3.

I think we are talking past each out: v3 is fixed by c35b5bbfabef, this request is about a potential backport to v2.6, and Henning's test was run on 2.6.

Quoted 6 lines

Ok, I was mislead by the subject line. So to answer your last question, I don't think we could bluntly backport the v3 fixes due to significant differences between v2 and v3 there (e.g. XNATOMIC does not exist in v3 anymore), however a patient review of all statements manipulating the state and info bits in v2 like you did for v3 should be a reasonable effort.

Introducing local flags like I did with c35b5bbfabef is probably a matter of taking the base logic of v3 regarding this, extending the scope to the extra v2 bits that disappeared during the transition to v3. Looks doable without too much fuss (famous last words).

Signature · 1 line
Philippe.
#10 lore raw
In reply to Jan Kiszka

Am Wed, 8 Mar 2017 12:25:26 +0100
schrieb Jan Kiszka <[email protected]>:

Quoted 42 lines · Philippe Gerum
Quoted 33 lines · Henning Schild
Quoted 22 lines

Am Fri, 26 Jun 2015 16:20:29 +0200
schrieb Jan Kiszka <[email protected]>:

Quoted 7 lines

Hi,

just pushed 3 patches to git.xenomai.org/xenomai-jki.git for-forge that are supposed to fix race conditions while manipulating xnthread::state and info (both need to be nklock-protected). Please review if finding and fixes make sense.

Quoted 6 lines

cobalt/kernel: Fix locking for xnthread info manipulations cobalt/kernel: Fix locking for setting XNFPU cobalt/kernel: Rework thread debugging helpers

Maybe some of the issues also exist in Xenomai 2, didn't check yet.

After looking deeper into the the mysterious -EINTR i asked about a few days ago we now got a trace that suggests something is going wrong. Jan remembered the race in thread flag manipulation he found in Xeno3.

Quoted 9 lines

I did not do a thorough code analysis yet but instead just put two asserts into xnthread_set_info and xnthread_clear_info.
1. !xnlock_is_owner(&nklock)
2. xnpod_current_thread() != thread_to_update

Both cases do happen. The flags are manipulated without holding the lock and the flags are manipulated from another context. I guess that suggests that the race found in xenomai3 is also in xenomai2.

Quoted 7 lines

I would not compare both code bases. Much rewrite took place from the legacy nucleus to the cobalt core.

I have reviewed every single statement involving set/clear info bits in 3.x and I can't seem to find any unlocked access for those. Any specifics about the exact locations where your debug statements trigger?

Quoted 5 lines

One quickly discoverable example is in xnshadow_harden (xnthread_set/clear_info(curr, XNATOMIC) without nklock protection). And Henning also confirmed that the info field is not used only thread-locally, though I don't have his finding in mind. I could imagine, though, that do_sigwake_event would make a good one.

Here some information to what i found with the two assertions:

Xenomai: nklock not held while calling xnthread_set_info info 0 40 xnshadow_harden

Xenomai: nklock not held while calling xnthread_set_info info 0 100 xnshadow_map

Xenomai: current thread ffffffff81bb7600 != thread ffffc900001eb440 when calling xnthread_set_info info 0 40
xnshadow_harden

Xenomai: current thread ffffffff81bb7600 != thread ffffc900001eb440 when calling xnthread_set_info info 0 100
xnshadow_map

So as Jan said, XNATOMIC (0x40) in _harden and XNPRIOSET (0x100) in _map. Both are used without locking and to manipulate remote threads. But those are just samples from a few runs, there may be more cases.

Henning

Quoted 7 lines

So I'm pretty sure we have the same issue in Xenomai 2 as in 3. Too bad I didn't follow up on the backport topic back then. Do you see any reasons why that could be complicated?

Jan