Skip to contentSkip to conversation

x86: Endless minor faults

Xenomai-core Jan Kiszka 3 participants 45 messages Jun 29, 2009 – Jul 7, 2009

Subsystems (6)
x86 VFS MM core Scheduler +2 more subsystems
lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

:|   +func                -653    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
:|   +func                -653    0.096  ipipe_check_context+0xd (__ipipe_handle_exception+0x71)
:|   #end     0x80000000  -653    0.069  do_page_fault+0x33 (__ipipe_handle_exception+0x1ff)
:    #func                -653    0.078  __ipipe_unstall_root+0x9 (do_page_fault+0x3cb)
:|   #begin   0x80000000  -653    0.068  __ipipe_unstall_root+0x34 (do_page_fault+0x3cb)
:|   +end     0x80000000  -653    0.069  __ipipe_unstall_root+0x59 (do_page_fault+0x3cb)
:    +func                -653    0.060  down_read_trylock+0x4 (do_page_fault+0x424)
:    +func                -653    0.068  _spin_lock_irqsave+0x9 (__down_read_trylock+0x16)
:    +func                -653    0.108  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -652    0.066  _spin_unlock_irqrestore+0x4 (__down_read_trylock+0x3f)
:    #func                -652    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -652    0.074  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -652    0.066  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -652    0.069  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:    +func                -652    0.096  find_vma+0x4 (do_page_fault+0x465)
:    +func                -652    0.150  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -652    0.098  handle_mm_fault+0x11 (do_page_fault+0x537)
:    +func                -652    0.090  _spin_lock+0x4 (handle_mm_fault+0x680)
:    +func                -652    0.063  ptep_set_access_flags+0x9 (handle_mm_fault+0x6d1)
:    +func                -652    0.282  flush_tlb_page+0xd (handle_mm_fault+0x6e7)
:    +func                -651    0.162  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -651    0.062  up_read+0x4 (do_page_fault+0x5a9)
:    +func                -651    0.072  _spin_lock_irqsave+0x9 (__up_read+0x1c)
:    +func                -651    0.117  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -651    0.074  _spin_unlock_irqrestore+0x4 (__up_read+0x92)
:    #func                -651    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -651    0.060  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -651    0.056  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -651    0.420  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:|   +func                -650    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)

and again and again...

We are looping over a minor fault here (according to /proc/PID/stat), the context is a Xenomai task in secondary mode. As the task no longer processes signals in this state, the whole system is more or less broken. Tomorrow I will try to find out the faulting address with an instrumented kernel, but maybe you already have some ideas.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#1 lore raw
Quoted 5 lines · Jan Kiszka

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

Do you mean 2.6.29.5/2.4-03?

Signature · 1 line
Philippe.
author
#2 lore raw
Quoted 8 lines · Philippe Gerum
Quoted 5 lines · Jan Kiszka

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

Do you mean 2.6.29.5/2.4-03?

No, in fact I meant 2.4-01 (the one of rc2).

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
author
#3 lore raw
In reply to Jan Kiszka
Quoted 37 lines · Jan Kiszka

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

:|   +func                -653    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
:|   +func                -653    0.096  ipipe_check_context+0xd (__ipipe_handle_exception+0x71)
:|   #end     0x80000000  -653    0.069  do_page_fault+0x33 (__ipipe_handle_exception+0x1ff)
:    #func                -653    0.078  __ipipe_unstall_root+0x9 (do_page_fault+0x3cb)
:|   #begin   0x80000000  -653    0.068  __ipipe_unstall_root+0x34 (do_page_fault+0x3cb)
:|   +end     0x80000000  -653    0.069  __ipipe_unstall_root+0x59 (do_page_fault+0x3cb)
:    +func                -653    0.060  down_read_trylock+0x4 (do_page_fault+0x424)
:    +func                -653    0.068  _spin_lock_irqsave+0x9 (__down_read_trylock+0x16)
:    +func                -653    0.108  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -652    0.066  _spin_unlock_irqrestore+0x4 (__down_read_trylock+0x3f)
:    #func                -652    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -652    0.074  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -652    0.066  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -652    0.069  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:    +func                -652    0.096  find_vma+0x4 (do_page_fault+0x465)
:    +func                -652    0.150  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -652    0.098  handle_mm_fault+0x11 (do_page_fault+0x537)
:    +func                -652    0.090  _spin_lock+0x4 (handle_mm_fault+0x680)
:    +func                -652    0.063  ptep_set_access_flags+0x9 (handle_mm_fault+0x6d1)
:    +func                -652    0.282  flush_tlb_page+0xd (handle_mm_fault+0x6e7)
:    +func                -651    0.162  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -651    0.062  up_read+0x4 (do_page_fault+0x5a9)
:    +func                -651    0.072  _spin_lock_irqsave+0x9 (__up_read+0x1c)
:    +func                -651    0.117  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -651    0.074  _spin_unlock_irqrestore+0x4 (__up_read+0x92)
:    #func                -651    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -651    0.060  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -651    0.056  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -651    0.420  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:|   +func                -650    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
Quoted 7 lines

and again and again...

We are looping over a minor fault here (according to /proc/PID/stat), the context is a Xenomai task in secondary mode. As the task no longer processes signals in this state, the whole system is more or less broken. Tomorrow I will try to find out the faulting address with an instrumented kernel, but maybe you already have some ideas.

The fault is apparently triggered by __xn_put_user(XNRELAX, thread->u_mode) in xnshadow_relax. thread->u_mode is pointing to an invalid region ATM. The questions are now: Who corrupted this, user space on init (not that likely) or kernel space later on (unpleasant thought)? Moreover: Why can't we recover from a fault on u_mode?

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#4 lore raw
Quoted 46 lines · Jan Kiszka
Quoted 37 lines · Jan Kiszka

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

:|   +func                -653    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
:|   +func                -653    0.096  ipipe_check_context+0xd (__ipipe_handle_exception+0x71)
:|   #end     0x80000000  -653    0.069  do_page_fault+0x33 (__ipipe_handle_exception+0x1ff)
:    #func                -653    0.078  __ipipe_unstall_root+0x9 (do_page_fault+0x3cb)
:|   #begin   0x80000000  -653    0.068  __ipipe_unstall_root+0x34 (do_page_fault+0x3cb)
:|   +end     0x80000000  -653    0.069  __ipipe_unstall_root+0x59 (do_page_fault+0x3cb)
:    +func                -653    0.060  down_read_trylock+0x4 (do_page_fault+0x424)
:    +func                -653    0.068  _spin_lock_irqsave+0x9 (__down_read_trylock+0x16)
:    +func                -653    0.108  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -652    0.066  _spin_unlock_irqrestore+0x4 (__down_read_trylock+0x3f)
:    #func                -652    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -652    0.074  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -652    0.066  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -652    0.069  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:    +func                -652    0.096  find_vma+0x4 (do_page_fault+0x465)
:    +func                -652    0.150  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -652    0.098  handle_mm_fault+0x11 (do_page_fault+0x537)
:    +func                -652    0.090  _spin_lock+0x4 (handle_mm_fault+0x680)
:    +func                -652    0.063  ptep_set_access_flags+0x9 (handle_mm_fault+0x6d1)
:    +func                -652    0.282  flush_tlb_page+0xd (handle_mm_fault+0x6e7)
:    +func                -651    0.162  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -651    0.062  up_read+0x4 (do_page_fault+0x5a9)
:    +func                -651    0.072  _spin_lock_irqsave+0x9 (__up_read+0x1c)
:    +func                -651    0.117  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -651    0.074  _spin_unlock_irqrestore+0x4 (__up_read+0x92)
:    #func                -651    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -651    0.060  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -651    0.056  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -651    0.420  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:|   +func                -650    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
Quoted 7 lines

and again and again...

We are looping over a minor fault here (according to /proc/PID/stat), the context is a Xenomai task in secondary mode. As the task no longer processes signals in this state, the whole system is more or less broken. Tomorrow I will try to find out the faulting address with an instrumented kernel, but maybe you already have some ideas.

Quoted 5 lines

The fault is apparently triggered by __xn_put_user(XNRELAX, thread->u_mode) in xnshadow_relax. thread->u_mode is pointing to an invalid region ATM. The questions are now: Who corrupted this, user space on init (not that likely) or kernel space later on (unpleasant thought)? Moreover: Why can't we recover from a fault on u_mode?

Can not this be a cleanup issue? The dying thread would have freed its u_mode before the final relax which really kills it.

Signature · 1 line
                                          Gilles
#6 lore raw
In reply to Jan Kiszka
Quoted 46 lines · Jan Kiszka
Quoted 37 lines · Jan Kiszka

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

:|   +func                -653    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
:|   +func                -653    0.096  ipipe_check_context+0xd (__ipipe_handle_exception+0x71)
:|   #end     0x80000000  -653    0.069  do_page_fault+0x33 (__ipipe_handle_exception+0x1ff)
:    #func                -653    0.078  __ipipe_unstall_root+0x9 (do_page_fault+0x3cb)
:|   #begin   0x80000000  -653    0.068  __ipipe_unstall_root+0x34 (do_page_fault+0x3cb)
:|   +end     0x80000000  -653    0.069  __ipipe_unstall_root+0x59 (do_page_fault+0x3cb)
:    +func                -653    0.060  down_read_trylock+0x4 (do_page_fault+0x424)
:    +func                -653    0.068  _spin_lock_irqsave+0x9 (__down_read_trylock+0x16)
:    +func                -653    0.108  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -652    0.066  _spin_unlock_irqrestore+0x4 (__down_read_trylock+0x3f)
:    #func                -652    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -652    0.074  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -652    0.066  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -652    0.069  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:    +func                -652    0.096  find_vma+0x4 (do_page_fault+0x465)
:    +func                -652    0.150  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -652    0.098  handle_mm_fault+0x11 (do_page_fault+0x537)
:    +func                -652    0.090  _spin_lock+0x4 (handle_mm_fault+0x680)
:    +func                -652    0.063  ptep_set_access_flags+0x9 (handle_mm_fault+0x6d1)
:    +func                -652    0.282  flush_tlb_page+0xd (handle_mm_fault+0x6e7)
:    +func                -651    0.162  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -651    0.062  up_read+0x4 (do_page_fault+0x5a9)
:    +func                -651    0.072  _spin_lock_irqsave+0x9 (__up_read+0x1c)
:    +func                -651    0.117  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -651    0.074  _spin_unlock_irqrestore+0x4 (__up_read+0x92)
:    #func                -651    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -651    0.060  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -651    0.056  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -651    0.420  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:|   +func                -650    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
Quoted 7 lines

and again and again...

We are looping over a minor fault here (according to /proc/PID/stat), the context is a Xenomai task in secondary mode. As the task no longer processes signals in this state, the whole system is more or less broken. Tomorrow I will try to find out the faulting address with an instrumented kernel, but maybe you already have some ideas.

Quoted 5 lines

The fault is apparently triggered by __xn_put_user(XNRELAX, thread->u_mode) in xnshadow_relax. thread->u_mode is pointing to an invalid region ATM. The questions are now: Who corrupted this, user space on init (not that likely) or kernel space later on (unpleasant thought)? Moreover: Why can't we recover from a fault on u_mode?

I already investigated such an issue, and my conclusion was that there are some places in the code where we can not cope with a fault. xnshadow_relax being such a place, because, if relax faults, then what will the fault handler do? Call relax again. Fortunately, mlockall and the nocow stuff fixes this.

Another way to implement the u_mode thing would be to use the shared heaps we use for fast mutexes.

Signature · 1 line
                                          Gilles
author
#5 lore raw
In reply to Gilles Chanteperdrix
Quoted 46 lines · Gilles Chanteperdrix
Quoted 45 lines · Jan Kiszka
Quoted 44 lines · Jan Kiszka

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

:|   +func                -653    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
:|   +func                -653    0.096  ipipe_check_context+0xd (__ipipe_handle_exception+0x71)
:|   #end     0x80000000  -653    0.069  do_page_fault+0x33 (__ipipe_handle_exception+0x1ff)
:    #func                -653    0.078  __ipipe_unstall_root+0x9 (do_page_fault+0x3cb)
:|   #begin   0x80000000  -653    0.068  __ipipe_unstall_root+0x34 (do_page_fault+0x3cb)
:|   +end     0x80000000  -653    0.069  __ipipe_unstall_root+0x59 (do_page_fault+0x3cb)
:    +func                -653    0.060  down_read_trylock+0x4 (do_page_fault+0x424)
:    +func                -653    0.068  _spin_lock_irqsave+0x9 (__down_read_trylock+0x16)
:    +func                -653    0.108  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -652    0.066  _spin_unlock_irqrestore+0x4 (__down_read_trylock+0x3f)
:    #func                -652    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -652    0.074  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -652    0.066  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -652    0.069  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:    +func                -652    0.096  find_vma+0x4 (do_page_fault+0x465)
:    +func                -652    0.150  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -652    0.098  handle_mm_fault+0x11 (do_page_fault+0x537)
:    +func                -652    0.090  _spin_lock+0x4 (handle_mm_fault+0x680)
:    +func                -652    0.063  ptep_set_access_flags+0x9 (handle_mm_fault+0x6d1)
:    +func                -652    0.282  flush_tlb_page+0xd (handle_mm_fault+0x6e7)
:    +func                -651    0.162  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -651    0.062  up_read+0x4 (do_page_fault+0x5a9)
:    +func                -651    0.072  _spin_lock_irqsave+0x9 (__up_read+0x1c)
:    +func                -651    0.117  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -651    0.074  _spin_unlock_irqrestore+0x4 (__up_read+0x92)
:    #func                -651    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -651    0.060  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -651    0.056  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -651    0.420  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:|   +func                -650    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)

and again and again...

We are looping over a minor fault here (according to /proc/PID/stat), the context is a Xenomai task in secondary mode. As the task no longer processes signals in this state, the whole system is more or less broken. Tomorrow I will try to find out the faulting address with an instrumented kernel, but maybe you already have some ideas.

Quoted 9 lines
Quoted 5 lines

The fault is apparently triggered by __xn_put_user(XNRELAX, thread->u_mode) in xnshadow_relax. thread->u_mode is pointing to an invalid region ATM. The questions are now: Who corrupted this, user space on init (not that likely) or kernel space later on (unpleasant thought)? Moreover: Why can't we recover from a fault on u_mode?

Can not this be a cleanup issue? The dying thread would have freed its u_mode before the final relax which really kills it.

u_mode is 0x018fbf60, and that's typically an unused range for any process on x86_64. It is definitely unused for the process in question according to /proc/PID/maps.

That said, you scenario may be problematic too, but I don't think we see it here.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
author
#7 lore raw
In reply to Gilles Chanteperdrix
Quoted 52 lines · Gilles Chanteperdrix
Quoted 45 lines · Jan Kiszka
Quoted 37 lines · Jan Kiszka

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

:|   +func                -653    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
:|   +func                -653    0.096  ipipe_check_context+0xd (__ipipe_handle_exception+0x71)
:|   #end     0x80000000  -653    0.069  do_page_fault+0x33 (__ipipe_handle_exception+0x1ff)
:    #func                -653    0.078  __ipipe_unstall_root+0x9 (do_page_fault+0x3cb)
:|   #begin   0x80000000  -653    0.068  __ipipe_unstall_root+0x34 (do_page_fault+0x3cb)
:|   +end     0x80000000  -653    0.069  __ipipe_unstall_root+0x59 (do_page_fault+0x3cb)
:    +func                -653    0.060  down_read_trylock+0x4 (do_page_fault+0x424)
:    +func                -653    0.068  _spin_lock_irqsave+0x9 (__down_read_trylock+0x16)
:    +func                -653    0.108  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -652    0.066  _spin_unlock_irqrestore+0x4 (__down_read_trylock+0x3f)
:    #func                -652    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -652    0.074  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -652    0.066  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -652    0.069  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:    +func                -652    0.096  find_vma+0x4 (do_page_fault+0x465)
:    +func                -652    0.150  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -652    0.098  handle_mm_fault+0x11 (do_page_fault+0x537)
:    +func                -652    0.090  _spin_lock+0x4 (handle_mm_fault+0x680)
:    +func                -652    0.063  ptep_set_access_flags+0x9 (handle_mm_fault+0x6d1)
:    +func                -652    0.282  flush_tlb_page+0xd (handle_mm_fault+0x6e7)
:    +func                -651    0.162  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -651    0.062  up_read+0x4 (do_page_fault+0x5a9)
:    +func                -651    0.072  _spin_lock_irqsave+0x9 (__up_read+0x1c)
:    +func                -651    0.117  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -651    0.074  _spin_unlock_irqrestore+0x4 (__up_read+0x92)
:    #func                -651    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -651    0.060  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -651    0.056  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -651    0.420  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:|   +func                -650    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
Quoted 7 lines

and again and again...

We are looping over a minor fault here (according to /proc/PID/stat), the context is a Xenomai task in secondary mode. As the task no longer processes signals in this state, the whole system is more or less broken. Tomorrow I will try to find out the faulting address with an instrumented kernel, but maybe you already have some ideas.

Quoted 5 lines

The fault is apparently triggered by __xn_put_user(XNRELAX, thread->u_mode) in xnshadow_relax. thread->u_mode is pointing to an invalid region ATM. The questions are now: Who corrupted this, user space on init (not that likely) or kernel space later on (unpleasant thought)? Moreover: Why can't we recover from a fault on u_mode?

Quoted 5 lines

I already investigated such an issue, and my conclusion was that there are some places in the code where we can not cope with a fault. xnshadow_relax being such a place, because, if relax faults, then what will the fault handler do? Call relax again. Fortunately, mlockall and the nocow stuff fixes this.

That was the assumption I built u_mode setting on. Granted, I neglected the case where we fail due to corrupt addresses.

Quoted 3 lines

Another way to implement the u_mode thing would be to use the shared heaps we use for fast mutexes.

This is probably the way to go for a robust u_mode interface. A must-fix for 2.5-final. Nevertheless, I will do another run to find out where this address comes from. As often, we may see a chain of bugs...

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#8 lore raw
In reply to Gilles Chanteperdrix
Quoted 53 lines · Gilles Chanteperdrix
Quoted 46 lines · Jan Kiszka
Quoted 37 lines · Jan Kiszka

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

:|   +func                -653    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
:|   +func                -653    0.096  ipipe_check_context+0xd (__ipipe_handle_exception+0x71)
:|   #end     0x80000000  -653    0.069  do_page_fault+0x33 (__ipipe_handle_exception+0x1ff)
:    #func                -653    0.078  __ipipe_unstall_root+0x9 (do_page_fault+0x3cb)
:|   #begin   0x80000000  -653    0.068  __ipipe_unstall_root+0x34 (do_page_fault+0x3cb)
:|   +end     0x80000000  -653    0.069  __ipipe_unstall_root+0x59 (do_page_fault+0x3cb)
:    +func                -653    0.060  down_read_trylock+0x4 (do_page_fault+0x424)
:    +func                -653    0.068  _spin_lock_irqsave+0x9 (__down_read_trylock+0x16)
:    +func                -653    0.108  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -652    0.066  _spin_unlock_irqrestore+0x4 (__down_read_trylock+0x3f)
:    #func                -652    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -652    0.074  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -652    0.066  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -652    0.069  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:    +func                -652    0.096  find_vma+0x4 (do_page_fault+0x465)
:    +func                -652    0.150  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -652    0.098  handle_mm_fault+0x11 (do_page_fault+0x537)
:    +func                -652    0.090  _spin_lock+0x4 (handle_mm_fault+0x680)
:    +func                -652    0.063  ptep_set_access_flags+0x9 (handle_mm_fault+0x6d1)
:    +func                -652    0.282  flush_tlb_page+0xd (handle_mm_fault+0x6e7)
:    +func                -651    0.162  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -651    0.062  up_read+0x4 (do_page_fault+0x5a9)
:    +func                -651    0.072  _spin_lock_irqsave+0x9 (__up_read+0x1c)
:    +func                -651    0.117  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -651    0.074  _spin_unlock_irqrestore+0x4 (__up_read+0x92)
:    #func                -651    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -651    0.060  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -651    0.056  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -651    0.420  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:|   +func                -650    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
Quoted 7 lines

and again and again...

We are looping over a minor fault here (according to /proc/PID/stat), the context is a Xenomai task in secondary mode. As the task no longer processes signals in this state, the whole system is more or less broken. Tomorrow I will try to find out the faulting address with an instrumented kernel, but maybe you already have some ideas.

Quoted 5 lines

The fault is apparently triggered by __xn_put_user(XNRELAX, thread->u_mode) in xnshadow_relax. thread->u_mode is pointing to an invalid region ATM. The questions are now: Who corrupted this, user space on init (not that likely) or kernel space later on (unpleasant thought)? Moreover: Why can't we recover from a fault on u_mode?

Quoted 5 lines

I already investigated such an issue, and my conclusion was that there are some places in the code where we can not cope with a fault. xnshadow_relax being such a place, because, if relax faults, then what will the fault handler do? Call relax again. Fortunately, mlockall and the nocow stuff fixes this.

xnshadow_relax() faulting before the current thread bears the XNRELAX bit would mean that a creepy issue involving ondemand PTEs in _kernel_ space must have caused this. Having the init_mm mappings known from all processes seems more relevant to this issue than anything nocow and/or mlockall could ever do to fix it.

Quoted 4 lines

Another way to implement the u_mode thing would be to use the shared heaps we use for fast mutexes.

Signature · 1 line
Philippe.
#9 lore raw
Quoted 59 lines · Philippe Gerum
Quoted 51 lines · Gilles Chanteperdrix
Quoted 45 lines · Jan Kiszka
Quoted 37 lines · Jan Kiszka

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

:|   +func                -653    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
:|   +func                -653    0.096  ipipe_check_context+0xd (__ipipe_handle_exception+0x71)
:|   #end     0x80000000  -653    0.069  do_page_fault+0x33 (__ipipe_handle_exception+0x1ff)
:    #func                -653    0.078  __ipipe_unstall_root+0x9 (do_page_fault+0x3cb)
:|   #begin   0x80000000  -653    0.068  __ipipe_unstall_root+0x34 (do_page_fault+0x3cb)
:|   +end     0x80000000  -653    0.069  __ipipe_unstall_root+0x59 (do_page_fault+0x3cb)
:    +func                -653    0.060  down_read_trylock+0x4 (do_page_fault+0x424)
:    +func                -653    0.068  _spin_lock_irqsave+0x9 (__down_read_trylock+0x16)
:    +func                -653    0.108  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -652    0.066  _spin_unlock_irqrestore+0x4 (__down_read_trylock+0x3f)
:    #func                -652    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -652    0.074  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -652    0.066  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -652    0.069  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:    +func                -652    0.096  find_vma+0x4 (do_page_fault+0x465)
:    +func                -652    0.150  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -652    0.098  handle_mm_fault+0x11 (do_page_fault+0x537)
:    +func                -652    0.090  _spin_lock+0x4 (handle_mm_fault+0x680)
:    +func                -652    0.063  ptep_set_access_flags+0x9 (handle_mm_fault+0x6d1)
:    +func                -652    0.282  flush_tlb_page+0xd (handle_mm_fault+0x6e7)
:    +func                -651    0.162  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -651    0.062  up_read+0x4 (do_page_fault+0x5a9)
:    +func                -651    0.072  _spin_lock_irqsave+0x9 (__up_read+0x1c)
:    +func                -651    0.117  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -651    0.074  _spin_unlock_irqrestore+0x4 (__up_read+0x92)
:    #func                -651    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -651    0.060  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -651    0.056  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -651    0.420  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:|   +func                -650    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
Quoted 7 lines

and again and again...

We are looping over a minor fault here (according to /proc/PID/stat), the context is a Xenomai task in secondary mode. As the task no longer processes signals in this state, the whole system is more or less broken. Tomorrow I will try to find out the faulting address with an instrumented kernel, but maybe you already have some ideas.

Quoted 5 lines

The fault is apparently triggered by __xn_put_user(XNRELAX, thread->u_mode) in xnshadow_relax. thread->u_mode is pointing to an invalid region ATM. The questions are now: Who corrupted this, user space on init (not that likely) or kernel space later on (unpleasant thought)? Moreover: Why can't we recover from a fault on u_mode?

Quoted 5 lines

I already investigated such an issue, and my conclusion was that there are some places in the code where we can not cope with a fault. xnshadow_relax being such a place, because, if relax faults, then what will the fault handler do? Call relax again. Fortunately, mlockall and the nocow stuff fixes this.

Quoted 5 lines

xnshadow_relax() faulting before the current thread bears the XNRELAX bit would mean that a creepy issue involving ondemand PTEs in _kernel_ space must have caused this. Having the init_mm mappings known from all processes seems more relevant to this issue than anything nocow and/or mlockall could ever do to fix it.

u_mode is a user-space address.

Signature · 1 line
                                          Gilles
#10 lore raw
Quoted 60 lines · Gilles Chanteperdrix
Quoted 59 lines · Philippe Gerum
Quoted 56 lines · Gilles Chanteperdrix
Quoted 45 lines · Jan Kiszka
Quoted 37 lines · Jan Kiszka

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

:|   +func                -653    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
:|   +func                -653    0.096  ipipe_check_context+0xd (__ipipe_handle_exception+0x71)
:|   #end     0x80000000  -653    0.069  do_page_fault+0x33 (__ipipe_handle_exception+0x1ff)
:    #func                -653    0.078  __ipipe_unstall_root+0x9 (do_page_fault+0x3cb)
:|   #begin   0x80000000  -653    0.068  __ipipe_unstall_root+0x34 (do_page_fault+0x3cb)
:|   +end     0x80000000  -653    0.069  __ipipe_unstall_root+0x59 (do_page_fault+0x3cb)
:    +func                -653    0.060  down_read_trylock+0x4 (do_page_fault+0x424)
:    +func                -653    0.068  _spin_lock_irqsave+0x9 (__down_read_trylock+0x16)
:    +func                -653    0.108  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -652    0.066  _spin_unlock_irqrestore+0x4 (__down_read_trylock+0x3f)
:    #func                -652    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -652    0.074  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -652    0.066  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -652    0.069  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:    +func                -652    0.096  find_vma+0x4 (do_page_fault+0x465)
:    +func                -652    0.150  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -652    0.098  handle_mm_fault+0x11 (do_page_fault+0x537)
:    +func                -652    0.090  _spin_lock+0x4 (handle_mm_fault+0x680)
:    +func                -652    0.063  ptep_set_access_flags+0x9 (handle_mm_fault+0x6d1)
:    +func                -652    0.282  flush_tlb_page+0xd (handle_mm_fault+0x6e7)
:    +func                -651    0.162  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -651    0.062  up_read+0x4 (do_page_fault+0x5a9)
:    +func                -651    0.072  _spin_lock_irqsave+0x9 (__up_read+0x1c)
:    +func                -651    0.117  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -651    0.074  _spin_unlock_irqrestore+0x4 (__up_read+0x92)
:    #func                -651    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -651    0.060  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -651    0.056  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -651    0.420  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:|   +func                -650    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
Quoted 7 lines

and again and again...

We are looping over a minor fault here (according to /proc/PID/stat), the context is a Xenomai task in secondary mode. As the task no longer processes signals in this state, the whole system is more or less broken. Tomorrow I will try to find out the faulting address with an instrumented kernel, but maybe you already have some ideas.

Quoted 5 lines

The fault is apparently triggered by __xn_put_user(XNRELAX, thread->u_mode) in xnshadow_relax. thread->u_mode is pointing to an invalid region ATM. The questions are now: Who corrupted this, user space on init (not that likely) or kernel space later on (unpleasant thought)? Moreover: Why can't we recover from a fault on u_mode?

I already investigated such an issue, and my conclusion was that there are some places in the code where we can not cope with a fault. xnshadow_relax being such a place, because, if relax faults, then what will the fault handler do? Call relax again. Fortunately, mlockall and the nocow stuff fixes this.

Quoted 8 lines
Quoted 5 lines

xnshadow_relax() faulting before the current thread bears the XNRELAX bit would mean that a creepy issue involving ondemand PTEs in _kernel_ space must have caused this. Having the init_mm mappings known from all processes seems more relevant to this issue than anything nocow and/or mlockall could ever do to fix it.

u_mode is a user-space address.

Why do you think xnshadow_relax() would be called for an already relaxed thread?

Signature · 1 line
Philippe.
#11 lore raw
Quoted 65 lines · Philippe Gerum
Quoted 64 lines · Gilles Chanteperdrix
Quoted 63 lines · Philippe Gerum
Quoted 51 lines · Gilles Chanteperdrix
Quoted 45 lines · Jan Kiszka
Quoted 37 lines · Jan Kiszka

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

:|   +func                -653    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
:|   +func                -653    0.096  ipipe_check_context+0xd (__ipipe_handle_exception+0x71)
:|   #end     0x80000000  -653    0.069  do_page_fault+0x33 (__ipipe_handle_exception+0x1ff)
:    #func                -653    0.078  __ipipe_unstall_root+0x9 (do_page_fault+0x3cb)
:|   #begin   0x80000000  -653    0.068  __ipipe_unstall_root+0x34 (do_page_fault+0x3cb)
:|   +end     0x80000000  -653    0.069  __ipipe_unstall_root+0x59 (do_page_fault+0x3cb)
:    +func                -653    0.060  down_read_trylock+0x4 (do_page_fault+0x424)
:    +func                -653    0.068  _spin_lock_irqsave+0x9 (__down_read_trylock+0x16)
:    +func                -653    0.108  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -652    0.066  _spin_unlock_irqrestore+0x4 (__down_read_trylock+0x3f)
:    #func                -652    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -652    0.074  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -652    0.066  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -652    0.069  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:    +func                -652    0.096  find_vma+0x4 (do_page_fault+0x465)
:    +func                -652    0.150  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -652    0.098  handle_mm_fault+0x11 (do_page_fault+0x537)
:    +func                -652    0.090  _spin_lock+0x4 (handle_mm_fault+0x680)
:    +func                -652    0.063  ptep_set_access_flags+0x9 (handle_mm_fault+0x6d1)
:    +func                -652    0.282  flush_tlb_page+0xd (handle_mm_fault+0x6e7)
:    +func                -651    0.162  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -651    0.062  up_read+0x4 (do_page_fault+0x5a9)
:    +func                -651    0.072  _spin_lock_irqsave+0x9 (__up_read+0x1c)
:    +func                -651    0.117  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -651    0.074  _spin_unlock_irqrestore+0x4 (__up_read+0x92)
:    #func                -651    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -651    0.060  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -651    0.056  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -651    0.420  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:|   +func                -650    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
Quoted 7 lines

and again and again...

We are looping over a minor fault here (according to /proc/PID/stat), the context is a Xenomai task in secondary mode. As the task no longer processes signals in this state, the whole system is more or less broken. Tomorrow I will try to find out the faulting address with an instrumented kernel, but maybe you already have some ideas.

Quoted 5 lines

The fault is apparently triggered by __xn_put_user(XNRELAX, thread->u_mode) in xnshadow_relax. thread->u_mode is pointing to an invalid region ATM. The questions are now: Who corrupted this, user space on init (not that likely) or kernel space later on (unpleasant thought)? Moreover: Why can't we recover from a fault on u_mode?

Quoted 5 lines

I already investigated such an issue, and my conclusion was that there are some places in the code where we can not cope with a fault. xnshadow_relax being such a place, because, if relax faults, then what will the fault handler do? Call relax again. Fortunately, mlockall and the nocow stuff fixes this.

xnshadow_relax() faulting before the current thread bears the XNRELAX bit would mean that a creepy issue involving ondemand PTEs in _kernel_ space must have caused this. Having the init_mm mappings known from all processes seems more relevant to this issue than anything nocow and/or mlockall could ever do to fix it.

Quoted 5 lines
Quoted 2 lines

u_mode is a user-space address.

Why do you think xnshadow_relax() would be called for an already relaxed thread?

Because the fault happens before it has finished relaxing ?

Signature · 1 line
                                          Gilles
#12 lore raw
Quoted 66 lines · Gilles Chanteperdrix
Quoted 65 lines · Philippe Gerum
Quoted 64 lines · Gilles Chanteperdrix
Quoted 63 lines · Philippe Gerum
Quoted 51 lines · Gilles Chanteperdrix
Quoted 45 lines · Jan Kiszka
Quoted 37 lines · Jan Kiszka

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

:|   +func                -653    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
:|   +func                -653    0.096  ipipe_check_context+0xd (__ipipe_handle_exception+0x71)
:|   #end     0x80000000  -653    0.069  do_page_fault+0x33 (__ipipe_handle_exception+0x1ff)
:    #func                -653    0.078  __ipipe_unstall_root+0x9 (do_page_fault+0x3cb)
:|   #begin   0x80000000  -653    0.068  __ipipe_unstall_root+0x34 (do_page_fault+0x3cb)
:|   +end     0x80000000  -653    0.069  __ipipe_unstall_root+0x59 (do_page_fault+0x3cb)
:    +func                -653    0.060  down_read_trylock+0x4 (do_page_fault+0x424)
:    +func                -653    0.068  _spin_lock_irqsave+0x9 (__down_read_trylock+0x16)
:    +func                -653    0.108  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -652    0.066  _spin_unlock_irqrestore+0x4 (__down_read_trylock+0x3f)
:    #func                -652    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -652    0.074  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -652    0.066  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -652    0.069  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:    +func                -652    0.096  find_vma+0x4 (do_page_fault+0x465)
:    +func                -652    0.150  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -652    0.098  handle_mm_fault+0x11 (do_page_fault+0x537)
:    +func                -652    0.090  _spin_lock+0x4 (handle_mm_fault+0x680)
:    +func                -652    0.063  ptep_set_access_flags+0x9 (handle_mm_fault+0x6d1)
:    +func                -652    0.282  flush_tlb_page+0xd (handle_mm_fault+0x6e7)
:    +func                -651    0.162  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -651    0.062  up_read+0x4 (do_page_fault+0x5a9)
:    +func                -651    0.072  _spin_lock_irqsave+0x9 (__up_read+0x1c)
:    +func                -651    0.117  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -651    0.074  _spin_unlock_irqrestore+0x4 (__up_read+0x92)
:    #func                -651    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -651    0.060  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -651    0.056  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -651    0.420  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:|   +func                -650    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
Quoted 7 lines

and again and again...

We are looping over a minor fault here (according to /proc/PID/stat), the context is a Xenomai task in secondary mode. As the task no longer processes signals in this state, the whole system is more or less broken. Tomorrow I will try to find out the faulting address with an instrumented kernel, but maybe you already have some ideas.

Quoted 5 lines

The fault is apparently triggered by __xn_put_user(XNRELAX, thread->u_mode) in xnshadow_relax. thread->u_mode is pointing to an invalid region ATM. The questions are now: Who corrupted this, user space on init (not that likely) or kernel space later on (unpleasant thought)? Moreover: Why can't we recover from a fault on u_mode?

Quoted 5 lines

I already investigated such an issue, and my conclusion was that there are some places in the code where we can not cope with a fault. xnshadow_relax being such a place, because, if relax faults, then what will the fault handler do? Call relax again. Fortunately, mlockall and the nocow stuff fixes this.

xnshadow_relax() faulting before the current thread bears the XNRELAX bit would mean that a creepy issue involving ondemand PTEs in _kernel_ space must have caused this. Having the init_mm mappings known from all processes seems more relevant to this issue than anything nocow and/or mlockall could ever do to fix it.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

u_mode is a user-space address.

Why do you think xnshadow_relax() would be called for an already relaxed thread?

Because the fault happens before it has finished relaxing ?

Well, no. Have a second look at the code.

Signature · 1 line
Philippe.
#13 lore raw
Quoted 66 lines · Philippe Gerum
Quoted 65 lines · Gilles Chanteperdrix
Quoted 64 lines · Philippe Gerum
Quoted 63 lines · Gilles Chanteperdrix
Quoted 62 lines · Philippe Gerum
Quoted 51 lines · Gilles Chanteperdrix
Quoted 45 lines · Jan Kiszka
Quoted 37 lines · Jan Kiszka

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

:|   +func                -653    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
:|   +func                -653    0.096  ipipe_check_context+0xd (__ipipe_handle_exception+0x71)
:|   #end     0x80000000  -653    0.069  do_page_fault+0x33 (__ipipe_handle_exception+0x1ff)
:    #func                -653    0.078  __ipipe_unstall_root+0x9 (do_page_fault+0x3cb)
:|   #begin   0x80000000  -653    0.068  __ipipe_unstall_root+0x34 (do_page_fault+0x3cb)
:|   +end     0x80000000  -653    0.069  __ipipe_unstall_root+0x59 (do_page_fault+0x3cb)
:    +func                -653    0.060  down_read_trylock+0x4 (do_page_fault+0x424)
:    +func                -653    0.068  _spin_lock_irqsave+0x9 (__down_read_trylock+0x16)
:    +func                -653    0.108  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -652    0.066  _spin_unlock_irqrestore+0x4 (__down_read_trylock+0x3f)
:    #func                -652    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -652    0.074  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -652    0.066  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -652    0.069  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:    +func                -652    0.096  find_vma+0x4 (do_page_fault+0x465)
:    +func                -652    0.150  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -652    0.098  handle_mm_fault+0x11 (do_page_fault+0x537)
:    +func                -652    0.090  _spin_lock+0x4 (handle_mm_fault+0x680)
:    +func                -652    0.063  ptep_set_access_flags+0x9 (handle_mm_fault+0x6d1)
:    +func                -652    0.282  flush_tlb_page+0xd (handle_mm_fault+0x6e7)
:    +func                -651    0.162  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -651    0.062  up_read+0x4 (do_page_fault+0x5a9)
:    +func                -651    0.072  _spin_lock_irqsave+0x9 (__up_read+0x1c)
:    +func                -651    0.117  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -651    0.074  _spin_unlock_irqrestore+0x4 (__up_read+0x92)
:    #func                -651    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -651    0.060  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -651    0.056  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -651    0.420  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:|   +func                -650    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
Quoted 7 lines

and again and again...

We are looping over a minor fault here (according to /proc/PID/stat), the context is a Xenomai task in secondary mode. As the task no longer processes signals in this state, the whole system is more or less broken. Tomorrow I will try to find out the faulting address with an instrumented kernel, but maybe you already have some ideas.

Quoted 5 lines

The fault is apparently triggered by __xn_put_user(XNRELAX, thread->u_mode) in xnshadow_relax. thread->u_mode is pointing to an invalid region ATM. The questions are now: Who corrupted this, user space on init (not that likely) or kernel space later on (unpleasant thought)? Moreover: Why can't we recover from a fault on u_mode?

Quoted 5 lines

I already investigated such an issue, and my conclusion was that there are some places in the code where we can not cope with a fault. xnshadow_relax being such a place, because, if relax faults, then what will the fault handler do? Call relax again. Fortunately, mlockall and the nocow stuff fixes this.

xnshadow_relax() faulting before the current thread bears the XNRELAX bit would mean that a creepy issue involving ondemand PTEs in _kernel_ space must have caused this. Having the init_mm mappings known from all processes seems more relevant to this issue than anything nocow and/or mlockall could ever do to fix it.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

u_mode is a user-space address.

Why do you think xnshadow_relax() would be called for an already relaxed thread?

Because the fault happens before it has finished relaxing ?

Well, no. Have a second look at the code.

Ok, you are right then, in my case the faults were probably due to vmalloc.

Signature · 1 line
                                          Gilles
author
#14 lore raw
Quoted 69 lines · Gilles Chanteperdrix
Quoted 68 lines · Philippe Gerum
Quoted 67 lines · Gilles Chanteperdrix
Quoted 66 lines · Philippe Gerum
Quoted 65 lines · Gilles Chanteperdrix
Quoted 57 lines · Philippe Gerum
Quoted 51 lines · Gilles Chanteperdrix
Quoted 45 lines · Jan Kiszka
Quoted 37 lines · Jan Kiszka

Hi all,

seen such loops before? This particular trace is from a 2.6.29.3 kernel with ipipe-2.3-01 (SMP/PREEMPT_VOLUNTARY), but the same happens with 2.6.29.5/2.3-03:

:|   +func                -653    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
:|   +func                -653    0.096  ipipe_check_context+0xd (__ipipe_handle_exception+0x71)
:|   #end     0x80000000  -653    0.069  do_page_fault+0x33 (__ipipe_handle_exception+0x1ff)
:    #func                -653    0.078  __ipipe_unstall_root+0x9 (do_page_fault+0x3cb)
:|   #begin   0x80000000  -653    0.068  __ipipe_unstall_root+0x34 (do_page_fault+0x3cb)
:|   +end     0x80000000  -653    0.069  __ipipe_unstall_root+0x59 (do_page_fault+0x3cb)
:    +func                -653    0.060  down_read_trylock+0x4 (do_page_fault+0x424)
:    +func                -653    0.068  _spin_lock_irqsave+0x9 (__down_read_trylock+0x16)
:    +func                -653    0.108  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -652    0.066  _spin_unlock_irqrestore+0x4 (__down_read_trylock+0x3f)
:    #func                -652    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -652    0.074  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -652    0.066  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -652    0.069  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:    +func                -652    0.096  find_vma+0x4 (do_page_fault+0x465)
:    +func                -652    0.150  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -652    0.098  handle_mm_fault+0x11 (do_page_fault+0x537)
:    +func                -652    0.090  _spin_lock+0x4 (handle_mm_fault+0x680)
:    +func                -652    0.063  ptep_set_access_flags+0x9 (handle_mm_fault+0x6d1)
:    +func                -652    0.282  flush_tlb_page+0xd (handle_mm_fault+0x6e7)
:    +func                -651    0.162  ltt_run_filter_default+0x4 (_ltt_specialized_trace+0xc1)
:    +func                -651    0.062  up_read+0x4 (do_page_fault+0x5a9)
:    +func                -651    0.072  _spin_lock_irqsave+0x9 (__up_read+0x1c)
:    +func                -651    0.117  ipipe_check_context+0xd (_spin_lock_irqsave+0x1d)
:    #func                -651    0.074  _spin_unlock_irqrestore+0x4 (__up_read+0x92)
:    #func                -651    0.069  __ipipe_restore_root+0x4 (_spin_unlock_irqrestore+0x21)
:    #func                -651    0.060  __ipipe_unstall_root+0x9 (__ipipe_restore_root+0x2c)
:|   #begin   0x80000000  -651    0.056  __ipipe_unstall_root+0x34 (__ipipe_restore_root+0x2c)
:|   +end     0x80000000  -651    0.420  __ipipe_unstall_root+0x59 (__ipipe_restore_root+0x2c)
:|   +func                -650    0.084  __ipipe_handle_exception+0x11 (page_fault+0x26)
Quoted 7 lines

and again and again...

We are looping over a minor fault here (according to /proc/PID/stat), the context is a Xenomai task in secondary mode. As the task no longer processes signals in this state, the whole system is more or less broken. Tomorrow I will try to find out the faulting address with an instrumented kernel, but maybe you already have some ideas.

Quoted 5 lines

The fault is apparently triggered by __xn_put_user(XNRELAX, thread->u_mode) in xnshadow_relax. thread->u_mode is pointing to an invalid region ATM. The questions are now: Who corrupted this, user space on init (not that likely) or kernel space later on (unpleasant thought)? Moreover: Why can't we recover from a fault on u_mode?

Quoted 5 lines

I already investigated such an issue, and my conclusion was that there are some places in the code where we can not cope with a fault. xnshadow_relax being such a place, because, if relax faults, then what will the fault handler do? Call relax again. Fortunately, mlockall and the nocow stuff fixes this.

Quoted 5 lines

xnshadow_relax() faulting before the current thread bears the XNRELAX bit would mean that a creepy issue involving ondemand PTEs in _kernel_ space must have caused this. Having the init_mm mappings known from all processes seems more relevant to this issue than anything nocow and/or mlockall could ever do to fix it.

u_mode is a user-space address.

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

Why do you think xnshadow_relax() would be called for an already relaxed thread?

Because the fault happens before it has finished relaxing ?

Well, no. Have a second look at the code.

Ok, you are right then, in my case the faults were probably due to vmalloc.

In our case, it must be some special fault path, too. I tried injecting a non-NULL but invalid address intentionally. But the __xn_put_user just swallowed it without complaints.

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
author
#15 lore raw
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
author
#16 lore raw
Quoted 10 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

CONFIG_IPIPE_DELAYED_ATOMICSW is nonsense. And we don't do switch_mm without irq protection on x86, at least not from the nucleus, right? Maybe a race due some Linux user running it unprotectedly?

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#17 lore raw
In reply to Jan Kiszka
Quoted 9 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 4 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Signature · 1 line
                                          Gilles
author
#18 lore raw
Quoted 10 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Quoted 9 lines

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#19 lore raw
Quoted 19 lines · Jan Kiszka
Quoted 9 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Quoted 8 lines

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Quoted 5 lines

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

prepare_arch_switch and task_hijacked

Signature · 1 line
                                          Gilles
author
#20 lore raw
In reply to Jan Kiszka
Quoted 19 lines · Jan Kiszka
Quoted 9 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Quoted 8 lines

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Quoted 5 lines

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Ha, that's the point: only activate_mm is protected, but we have more spots in 2.6.29 and maybe other kernels, too!

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#21 lore raw
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Quoted 8 lines
Quoted 5 lines

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Ha, that's the point: only activate_mm is protected, but we have more spots in 2.6.29 and maybe other kernels, too!

Ok, I do not see where switch_mm is called with IRQs off. What I found, however, is that leave_mm sets the cr3 and just clears active_mm->cpu_vm_mask. So, at this point, we have a discrepancy between cr3 and active_mm. I do not know what could happen if Xenomai could interrupt leave_mm between the cpu_clear and the write_cr3. From what I understand, switch_mm called by Xenomai upon return to root would re-set the bit, and re-set cr3, which would be set to the kernel cr3 right after that, but this would result in the active_mm.cpu_vm_mask bit being set instead of cleared as expected. So, maybe an irqs off section is missing in leave_mm.

Signature · 1 line
                                          Gilles
author
#22 lore raw
Quoted 22 lines · Gilles Chanteperdrix
Quoted 21 lines · Jan Kiszka
Quoted 20 lines · Jan Kiszka
Quoted 9 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Quoted 8 lines

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Yes.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Ha, that's the point: only activate_mm is protected, but we have more spots in 2.6.29 and maybe other kernels, too!

Ok, I do not see where switch_mm is called with IRQs off. What I found,

We have two direct callers of switch_mm in sched.c and one in fs/aio.c. Both need protection (I pushed IRQ disabling into switch_mm), but that is not enough according to current tests. It seems to reduce to probability of corruption, though.

Quoted 9 lines

however, is that leave_mm sets the cr3 and just clears active_mm->cpu_vm_mask. So, at this point, we have a discrepancy between cr3 and active_mm. I do not know what could happen if Xenomai could interrupt leave_mm between the cpu_clear and the write_cr3. From what I understand, switch_mm called by Xenomai upon return to root would re-set the bit, and re-set cr3, which would be set to the kernel cr3 right after that, but this would result in the active_mm.cpu_vm_mask bit being set instead of cleared as expected. So, maybe an irqs off section is missing in leave_mm.

leave_mm is already protected by its caller smp_invalidate_interrupt - but now I'm parsing context_switch /wrt to lazy tlb.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
author
#23 lore raw
Quoted 45 lines · Jan Kiszka
Quoted 20 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Ha, that's the point: only activate_mm is protected, but we have more spots in 2.6.29 and maybe other kernels, too!

Ok, I do not see where switch_mm is called with IRQs off. What I found,

We have two direct callers of switch_mm in sched.c and one in fs/aio.c. Both need protection (I pushed IRQ disabling into switch_mm), but that is not enough according to current tests. It seems to reduce to probability of corruption, though.

Quoted 9 lines

however, is that leave_mm sets the cr3 and just clears active_mm->cpu_vm_mask. So, at this point, we have a discrepancy between cr3 and active_mm. I do not know what could happen if Xenomai could interrupt leave_mm between the cpu_clear and the write_cr3. From what I understand, switch_mm called by Xenomai upon return to root would re-set the bit, and re-set cr3, which would be set to the kernel cr3 right after that, but this would result in the active_mm.cpu_vm_mask bit being set instead of cleared as expected. So, maybe an irqs off section is missing in leave_mm.

Quoted 3 lines

leave_mm is already protected by its caller smp_invalidate_interrupt - but now I'm parsing context_switch /wrt to lazy tlb.

Hmm... lazy tlb: This means a new task is switched in and has active_mm != mm. But do_page_fault reads task->mm... Just thoughts, no clear picture yet.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
author
#24 lore raw
Quoted 44 lines · Jan Kiszka
Quoted 43 lines · Jan Kiszka
Quoted 20 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Ha, that's the point: only activate_mm is protected, but we have more spots in 2.6.29 and maybe other kernels, too!

Ok, I do not see where switch_mm is called with IRQs off. What I found,

We have two direct callers of switch_mm in sched.c and one in fs/aio.c. Both need protection (I pushed IRQ disabling into switch_mm), but that is not enough according to current tests. It seems to reduce to probability of corruption, though.

Quoted 9 lines

however, is that leave_mm sets the cr3 and just clears active_mm->cpu_vm_mask. So, at this point, we have a discrepancy between cr3 and active_mm. I do not know what could happen if Xenomai could interrupt leave_mm between the cpu_clear and the write_cr3. From what I understand, switch_mm called by Xenomai upon return to root would re-set the bit, and re-set cr3, which would be set to the kernel cr3 right after that, but this would result in the active_mm.cpu_vm_mask bit being set instead of cleared as expected. So, maybe an irqs off section is missing in leave_mm.

Quoted 8 lines
Quoted 3 lines

leave_mm is already protected by its caller smp_invalidate_interrupt - but now I'm parsing context_switch /wrt to lazy tlb.

Hmm... lazy tlb: This means a new task is switched in and has active_mm != mm. But do_page_fault reads task->mm... Just thoughts, no clear picture yet.

Looking closer at the call sites of switch_mm, I think our the problem is mostly related to use_mm from fs/aio.c (customer is using aio heavily). But other callers need protection, too. We are going to test this patch tomorrow:

fs/aio.c +3 -0
index 76da125..d90fca3 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
+	local_irq_save_hw_cond(flags);
 	tsk->mm = mm;
 	tsk->active_mm = mm;
 	switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
kernel/sched.c +10 -2
index aa8f86c..8c545a4 100644
@@ -2668,8 +2668,12 @@ context_switch(struct rq *rq, struct task_struct *prev,
 		next->active_mm = oldmm;
 		atomic_inc(&oldmm->mm_count);
 		enter_lazy_tlb(oldmm, next);
-	} else
+	} else {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(oldmm, mm, next);
+		local_irq_restore_hw_cond(flags);
+	}
 
 	if (unlikely(!prev->mm)) {
 		prev->active_mm = NULL;
@@ -6406,8 +6410,12 @@ void idle_task_exit(void)
 
 	BUG_ON(cpu_online(smp_processor_id()));
 
-	if (mm != &init_mm)
+	if (mm != &init_mm) {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(mm, &init_mm, current);
+		local_irq_restore_hw_cond(flags);
+	}
 	mmdrop(mm);
 }
 

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#25 lore raw
Quoted 101 lines · Jan Kiszka
Quoted 44 lines · Jan Kiszka
Quoted 43 lines · Jan Kiszka
Quoted 20 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Ha, that's the point: only activate_mm is protected, but we have more spots in 2.6.29 and maybe other kernels, too!

Ok, I do not see where switch_mm is called with IRQs off. What I found,

We have two direct callers of switch_mm in sched.c and one in fs/aio.c. Both need protection (I pushed IRQ disabling into switch_mm), but that is not enough according to current tests. It seems to reduce to probability of corruption, though.

Quoted 9 lines

however, is that leave_mm sets the cr3 and just clears active_mm->cpu_vm_mask. So, at this point, we have a discrepancy between cr3 and active_mm. I do not know what could happen if Xenomai could interrupt leave_mm between the cpu_clear and the write_cr3. From what I understand, switch_mm called by Xenomai upon return to root would re-set the bit, and re-set cr3, which would be set to the kernel cr3 right after that, but this would result in the active_mm.cpu_vm_mask bit being set instead of cleared as expected. So, maybe an irqs off section is missing in leave_mm.

Quoted 8 lines
Quoted 3 lines

leave_mm is already protected by its caller smp_invalidate_interrupt - but now I'm parsing context_switch /wrt to lazy tlb.

Hmm... lazy tlb: This means a new task is switched in and has active_mm != mm. But do_page_fault reads task->mm... Just thoughts, no clear picture yet.

Looking closer at the call sites of switch_mm, I think our the problem is mostly related to use_mm from fs/aio.c (customer is using aio heavily). But other callers need protection, too. We are going to test this patch tomorrow:

fs/aio.c +3 -0
index 76da125..d90fca3 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
+	local_irq_save_hw_cond(flags);
 	tsk->mm = mm;
 	tsk->active_mm = mm;
 	switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
kernel/sched.c +5 -1
index aa8f86c..8c545a4 100644
@@ -2668,8 +2668,12 @@ context_switch(struct rq *rq, struct task_struct *prev,
 		next->active_mm = oldmm;
 		atomic_inc(&oldmm->mm_count);
 		enter_lazy_tlb(oldmm, next);
-	} else
+	} else {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(oldmm, mm, next);
+		local_irq_restore_hw_cond(flags);
+	}
 
 	if (unlikely(!prev->mm)) {
 		prev->active_mm = NULL;
@@ -6406,8 +6410,12 @@ void idle_task_exit(void)
 
 	BUG_ON(cpu_online(smp_processor_id()));

kernel/sched.c void idle_task_exit(void) · line 6420

Quoted 10 lines
kernel/sched.c +5 -1
 
-	if (mm != &init_mm)
+	if (mm != &init_mm) {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(mm, &init_mm, current);
+		local_irq_restore_hw_cond(flags);
+	}
 	mmdrop(mm);
 }

Please fix the callee instead of ironing the call sites. This would avoid further issues as upstream emits additional switch_mm calls over time, and make ironing activate_mm useless.

Quoted 4 lines

Jan

Signature · 1 line
Philippe.
#26 lore raw
Quoted 103 lines · Philippe Gerum
Quoted 102 lines · Jan Kiszka
Quoted 44 lines · Jan Kiszka
Quoted 43 lines · Jan Kiszka
Quoted 20 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Ha, that's the point: only activate_mm is protected, but we have more spots in 2.6.29 and maybe other kernels, too!

Ok, I do not see where switch_mm is called with IRQs off. What I found,

We have two direct callers of switch_mm in sched.c and one in fs/aio.c. Both need protection (I pushed IRQ disabling into switch_mm), but that is not enough according to current tests. It seems to reduce to probability of corruption, though.

Quoted 9 lines

however, is that leave_mm sets the cr3 and just clears active_mm->cpu_vm_mask. So, at this point, we have a discrepancy between cr3 and active_mm. I do not know what could happen if Xenomai could interrupt leave_mm between the cpu_clear and the write_cr3. From what I understand, switch_mm called by Xenomai upon return to root would re-set the bit, and re-set cr3, which would be set to the kernel cr3 right after that, but this would result in the active_mm.cpu_vm_mask bit being set instead of cleared as expected. So, maybe an irqs off section is missing in leave_mm.

Quoted 8 lines
Quoted 3 lines

leave_mm is already protected by its caller smp_invalidate_interrupt - but now I'm parsing context_switch /wrt to lazy tlb.

Hmm... lazy tlb: This means a new task is switched in and has active_mm != mm. But do_page_fault reads task->mm... Just thoughts, no clear picture yet.

Looking closer at the call sites of switch_mm, I think our the problem is mostly related to use_mm from fs/aio.c (customer is using aio heavily). But other callers need protection, too. We are going to test this patch tomorrow:

fs/aio.c +3 -0
index 76da125..d90fca3 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
+	local_irq_save_hw_cond(flags);
 	tsk->mm = mm;
 	tsk->active_mm = mm;
 	switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
kernel/sched.c +5 -1
index aa8f86c..8c545a4 100644
@@ -2668,8 +2668,12 @@ context_switch(struct rq *rq, struct task_struct *prev,
 		next->active_mm = oldmm;
 		atomic_inc(&oldmm->mm_count);
 		enter_lazy_tlb(oldmm, next);
-	} else
+	} else {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(oldmm, mm, next);
+		local_irq_restore_hw_cond(flags);
+	}
 
 	if (unlikely(!prev->mm)) {
 		prev->active_mm = NULL;
@@ -6406,8 +6410,12 @@ void idle_task_exit(void)
 
 	BUG_ON(cpu_online(smp_processor_id()));
 
Quoted 14 lines

kernel/sched.c void idle_task_exit(void) · line 6420

Quoted 9 lines
kernel/sched.c +5 -1
-	if (mm != &init_mm)
+	if (mm != &init_mm) {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(mm, &init_mm, current);
+		local_irq_restore_hw_cond(flags);
+	}
 	mmdrop(mm);
 }

Please fix the callee instead of ironing the call sites. This would avoid further issues as upstream emits additional switch_mm calls over time, and make ironing activate_mm useless.

Btw, this how the powerpc port works in locked switch mode, and this particular bug does not apply in unlocked switch mode anyway; this is why we don't have that problem on this arch. The ARM port always works in unlocked switch mode IIRC for latency reasons, so this should be a non-issue here as well. Gilles, would you confirm this?

Quoted 4 lines
Quoted 4 lines

Jan

Signature · 1 line
Philippe.
author
#34 lore raw
In reply to Philippe Gerum
Quoted 101 lines · Philippe Gerum
Quoted 100 lines · Jan Kiszka
Quoted 44 lines · Jan Kiszka
Quoted 43 lines · Jan Kiszka
Quoted 20 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Ha, that's the point: only activate_mm is protected, but we have more spots in 2.6.29 and maybe other kernels, too!

Ok, I do not see where switch_mm is called with IRQs off. What I found,

We have two direct callers of switch_mm in sched.c and one in fs/aio.c. Both need protection (I pushed IRQ disabling into switch_mm), but that is not enough according to current tests. It seems to reduce to probability of corruption, though.

Quoted 9 lines

however, is that leave_mm sets the cr3 and just clears active_mm->cpu_vm_mask. So, at this point, we have a discrepancy between cr3 and active_mm. I do not know what could happen if Xenomai could interrupt leave_mm between the cpu_clear and the write_cr3. From what I understand, switch_mm called by Xenomai upon return to root would re-set the bit, and re-set cr3, which would be set to the kernel cr3 right after that, but this would result in the active_mm.cpu_vm_mask bit being set instead of cleared as expected. So, maybe an irqs off section is missing in leave_mm.

Quoted 7 lines
Quoted 3 lines

leave_mm is already protected by its caller smp_invalidate_interrupt - but now I'm parsing context_switch /wrt to lazy tlb.

Hmm... lazy tlb: This means a new task is switched in and has active_mm != mm. But do_page_fault reads task->mm... Just thoughts, no clear picture yet.

Looking closer at the call sites of switch_mm, I think our the problem is mostly related to use_mm from fs/aio.c (customer is using aio heavily). But other callers need protection, too. We are going to test this patch tomorrow:

fs/aio.c +3 -0
index 76da125..d90fca3 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
+	local_irq_save_hw_cond(flags);
 	tsk->mm = mm;
 	tsk->active_mm = mm;
 	switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
kernel/sched.c +5 -1
index aa8f86c..8c545a4 100644
@@ -2668,8 +2668,12 @@ context_switch(struct rq *rq, struct task_struct *prev,
 		next->active_mm = oldmm;
 		atomic_inc(&oldmm->mm_count);
 		enter_lazy_tlb(oldmm, next);
-	} else
+	} else {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(oldmm, mm, next);
+		local_irq_restore_hw_cond(flags);
+	}
 
 	if (unlikely(!prev->mm)) {
 		prev->active_mm = NULL;
@@ -6406,8 +6410,12 @@ void idle_task_exit(void)
 
 	BUG_ON(cpu_online(smp_processor_id()));
 
Quoted 13 lines

kernel/sched.c void idle_task_exit(void) · line 6420

Quoted 9 lines
kernel/sched.c +5 -1
-	if (mm != &init_mm)
+	if (mm != &init_mm) {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(mm, &init_mm, current);
+		local_irq_restore_hw_cond(flags);
+	}
 	mmdrop(mm);
 }

Please fix the callee instead of ironing the call sites. This would avoid further issues as upstream emits additional switch_mm calls over time, and make ironing activate_mm useless.

This was my first idea, too, but it didn't work, see use_mm(). I don't think we will get around reviewing switch_mm users on kernel updates.

Jan

author
#27 lore raw
In reply to Philippe Gerum
Quoted 116 lines · Philippe Gerum
Quoted 101 lines · Philippe Gerum
Quoted 100 lines · Jan Kiszka
Quoted 44 lines · Jan Kiszka
Quoted 43 lines · Jan Kiszka
Quoted 20 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Ha, that's the point: only activate_mm is protected, but we have more spots in 2.6.29 and maybe other kernels, too!

Ok, I do not see where switch_mm is called with IRQs off. What I found,

We have two direct callers of switch_mm in sched.c and one in fs/aio.c. Both need protection (I pushed IRQ disabling into switch_mm), but that is not enough according to current tests. It seems to reduce to probability of corruption, though.

Quoted 9 lines

however, is that leave_mm sets the cr3 and just clears active_mm->cpu_vm_mask. So, at this point, we have a discrepancy between cr3 and active_mm. I do not know what could happen if Xenomai could interrupt leave_mm between the cpu_clear and the write_cr3. From what I understand, switch_mm called by Xenomai upon return to root would re-set the bit, and re-set cr3, which would be set to the kernel cr3 right after that, but this would result in the active_mm.cpu_vm_mask bit being set instead of cleared as expected. So, maybe an irqs off section is missing in leave_mm.

Quoted 7 lines
Quoted 3 lines

leave_mm is already protected by its caller smp_invalidate_interrupt - but now I'm parsing context_switch /wrt to lazy tlb.

Hmm... lazy tlb: This means a new task is switched in and has active_mm != mm. But do_page_fault reads task->mm... Just thoughts, no clear picture yet.

Looking closer at the call sites of switch_mm, I think our the problem is mostly related to use_mm from fs/aio.c (customer is using aio heavily). But other callers need protection, too. We are going to test this patch tomorrow:

fs/aio.c +3 -0
index 76da125..d90fca3 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
+	local_irq_save_hw_cond(flags);
 	tsk->mm = mm;
 	tsk->active_mm = mm;
 	switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
kernel/sched.c +5 -1
index aa8f86c..8c545a4 100644
@@ -2668,8 +2668,12 @@ context_switch(struct rq *rq, struct task_struct *prev,
 		next->active_mm = oldmm;
 		atomic_inc(&oldmm->mm_count);
 		enter_lazy_tlb(oldmm, next);
-	} else
+	} else {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(oldmm, mm, next);
+		local_irq_restore_hw_cond(flags);
+	}
 
 	if (unlikely(!prev->mm)) {
 		prev->active_mm = NULL;
@@ -6406,8 +6410,12 @@ void idle_task_exit(void)
 
 	BUG_ON(cpu_online(smp_processor_id()));
 
Quoted 13 lines

kernel/sched.c void idle_task_exit(void) · line 6420

Quoted 9 lines
kernel/sched.c +5 -1
-	if (mm != &init_mm)
+	if (mm != &init_mm) {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(mm, &init_mm, current);
+		local_irq_restore_hw_cond(flags);
+	}
 	mmdrop(mm);
 }

Please fix the callee instead of ironing the call sites. This would avoid further issues as upstream emits additional switch_mm calls over time, and make ironing activate_mm useless.

Quoted 5 lines

Btw, this how the powerpc port works in locked switch mode, and this particular bug does not apply in unlocked switch mode anyway; this is why we don't have that problem on this arch. The ARM port always works in unlocked switch mode IIRC for latency reasons, so this should be a non-issue here as well. Gilles, would you confirm this?

The aio issue (if it turns out to be one, /me still needs to understand the concrete patch) should bite any arch, but we may solve it differently where switch_mm is actually reentrant.

Jan

author
#28 lore raw
Quoted 122 lines · Jan Kiszka
Quoted 115 lines · Philippe Gerum
Quoted 101 lines · Philippe Gerum
Quoted 100 lines · Jan Kiszka
Quoted 44 lines · Jan Kiszka
Quoted 43 lines · Jan Kiszka
Quoted 20 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Ha, that's the point: only activate_mm is protected, but we have more spots in 2.6.29 and maybe other kernels, too!

Ok, I do not see where switch_mm is called with IRQs off. What I found,

We have two direct callers of switch_mm in sched.c and one in fs/aio.c. Both need protection (I pushed IRQ disabling into switch_mm), but that is not enough according to current tests. It seems to reduce to probability of corruption, though.

Quoted 9 lines

however, is that leave_mm sets the cr3 and just clears active_mm->cpu_vm_mask. So, at this point, we have a discrepancy between cr3 and active_mm. I do not know what could happen if Xenomai could interrupt leave_mm between the cpu_clear and the write_cr3. From what I understand, switch_mm called by Xenomai upon return to root would re-set the bit, and re-set cr3, which would be set to the kernel cr3 right after that, but this would result in the active_mm.cpu_vm_mask bit being set instead of cleared as expected. So, maybe an irqs off section is missing in leave_mm.

Quoted 7 lines
Quoted 3 lines

leave_mm is already protected by its caller smp_invalidate_interrupt - but now I'm parsing context_switch /wrt to lazy tlb.

Hmm... lazy tlb: This means a new task is switched in and has active_mm != mm. But do_page_fault reads task->mm... Just thoughts, no clear picture yet.

Looking closer at the call sites of switch_mm, I think our the problem is mostly related to use_mm from fs/aio.c (customer is using aio heavily). But other callers need protection, too. We are going to test this patch tomorrow:

fs/aio.c +3 -0
index 76da125..d90fca3 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
+	local_irq_save_hw_cond(flags);
 	tsk->mm = mm;
 	tsk->active_mm = mm;
 	switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
kernel/sched.c +5 -1
index aa8f86c..8c545a4 100644
@@ -2668,8 +2668,12 @@ context_switch(struct rq *rq, struct task_struct *prev,
 		next->active_mm = oldmm;
 		atomic_inc(&oldmm->mm_count);
 		enter_lazy_tlb(oldmm, next);
-	} else
+	} else {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(oldmm, mm, next);
+		local_irq_restore_hw_cond(flags);
+	}
 
 	if (unlikely(!prev->mm)) {
 		prev->active_mm = NULL;
@@ -6406,8 +6410,12 @@ void idle_task_exit(void)
 
 	BUG_ON(cpu_online(smp_processor_id()));
 
Quoted 13 lines

kernel/sched.c void idle_task_exit(void) · line 6420

Quoted 9 lines
kernel/sched.c +5 -1
-	if (mm != &init_mm)
+	if (mm != &init_mm) {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(mm, &init_mm, current);
+		local_irq_restore_hw_cond(flags);
+	}
 	mmdrop(mm);
 }

Please fix the callee instead of ironing the call sites. This would avoid further issues as upstream emits additional switch_mm calls over time, and make ironing activate_mm useless.

Quoted 5 lines

Btw, this how the powerpc port works in locked switch mode, and this particular bug does not apply in unlocked switch mode anyway; this is why we don't have that problem on this arch. The ARM port always works in unlocked switch mode IIRC for latency reasons, so this should be a non-issue here as well. Gilles, would you confirm this?

Quoted 3 lines

The aio issue (if it turns out to be one, /me still needs to understand the concrete patch) should bite any arch, but we may solve it differently where switch_mm is actually reentrant.

Took a beer to get my mind free, here is the potential race:

-> aio thread runs with mm == init_mm
-> aio thread calls into use_mm
-> preemption by Xenomai right after tsk->active_mm = mm
-> Xenomai switches from the aio thread to an RT thread with identical
mm (definitely possible in our scenario)
-> switch_mm will not update cr3 as prev == next
-> next fault of the RT thread (e.g. in __xn_put_user...) will attempt
to fix the right mm, but the CPU will continue to fail over init_mm

Jan

#29 lore raw
In reply to Philippe Gerum
Quoted 116 lines · Philippe Gerum
Quoted 101 lines · Philippe Gerum
Quoted 100 lines · Jan Kiszka
Quoted 44 lines · Jan Kiszka
Quoted 43 lines · Jan Kiszka
Quoted 20 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Ha, that's the point: only activate_mm is protected, but we have more spots in 2.6.29 and maybe other kernels, too!

Ok, I do not see where switch_mm is called with IRQs off. What I found,

We have two direct callers of switch_mm in sched.c and one in fs/aio.c. Both need protection (I pushed IRQ disabling into switch_mm), but that is not enough according to current tests. It seems to reduce to probability of corruption, though.

Quoted 9 lines

however, is that leave_mm sets the cr3 and just clears active_mm->cpu_vm_mask. So, at this point, we have a discrepancy between cr3 and active_mm. I do not know what could happen if Xenomai could interrupt leave_mm between the cpu_clear and the write_cr3. From what I understand, switch_mm called by Xenomai upon return to root would re-set the bit, and re-set cr3, which would be set to the kernel cr3 right after that, but this would result in the active_mm.cpu_vm_mask bit being set instead of cleared as expected. So, maybe an irqs off section is missing in leave_mm.

Quoted 7 lines
Quoted 3 lines

leave_mm is already protected by its caller smp_invalidate_interrupt - but now I'm parsing context_switch /wrt to lazy tlb.

Hmm... lazy tlb: This means a new task is switched in and has active_mm != mm. But do_page_fault reads task->mm... Just thoughts, no clear picture yet.

Looking closer at the call sites of switch_mm, I think our the problem is mostly related to use_mm from fs/aio.c (customer is using aio heavily). But other callers need protection, too. We are going to test this patch tomorrow:

fs/aio.c +3 -0
index 76da125..d90fca3 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
+	local_irq_save_hw_cond(flags);
 	tsk->mm = mm;
 	tsk->active_mm = mm;
 	switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
kernel/sched.c +5 -1
index aa8f86c..8c545a4 100644
@@ -2668,8 +2668,12 @@ context_switch(struct rq *rq, struct task_struct *prev,
 		next->active_mm = oldmm;
 		atomic_inc(&oldmm->mm_count);
 		enter_lazy_tlb(oldmm, next);
-	} else
+	} else {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(oldmm, mm, next);
+		local_irq_restore_hw_cond(flags);
+	}
 
 	if (unlikely(!prev->mm)) {
 		prev->active_mm = NULL;
@@ -6406,8 +6410,12 @@ void idle_task_exit(void)
 
 	BUG_ON(cpu_online(smp_processor_id()));
 
Quoted 13 lines

kernel/sched.c void idle_task_exit(void) · line 6420

Quoted 9 lines
kernel/sched.c +5 -1
-	if (mm != &init_mm)
+	if (mm != &init_mm) {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(mm, &init_mm, current);
+		local_irq_restore_hw_cond(flags);
+	}
 	mmdrop(mm);
 }

Please fix the callee instead of ironing the call sites. This would avoid further issues as upstream emits additional switch_mm calls over time, and make ironing activate_mm useless.

Quoted 5 lines

Btw, this how the powerpc port works in locked switch mode, and this particular bug does not apply in unlocked switch mode anyway; this is why we don't have that problem on this arch. The ARM port always works in unlocked switch mode IIRC for latency reasons, so this should be a non-issue here as well. Gilles, would you confirm this?

Yes, on arm switch_mm always has irqs on. We handle the case where switch_mm was interrupted in the middle of its operations by restarting the mm switch after xenomai switches back to Linux.

Signature · 1 line
					    Gilles.
author
#30 lore raw
Quoted 122 lines · Gilles Chanteperdrix
Quoted 115 lines · Philippe Gerum
Quoted 101 lines · Philippe Gerum
Quoted 100 lines · Jan Kiszka
Quoted 44 lines · Jan Kiszka
Quoted 43 lines · Jan Kiszka
Quoted 20 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Ha, that's the point: only activate_mm is protected, but we have more spots in 2.6.29 and maybe other kernels, too!

Ok, I do not see where switch_mm is called with IRQs off. What I found,

We have two direct callers of switch_mm in sched.c and one in fs/aio.c. Both need protection (I pushed IRQ disabling into switch_mm), but that is not enough according to current tests. It seems to reduce to probability of corruption, though.

Quoted 9 lines

however, is that leave_mm sets the cr3 and just clears active_mm->cpu_vm_mask. So, at this point, we have a discrepancy between cr3 and active_mm. I do not know what could happen if Xenomai could interrupt leave_mm between the cpu_clear and the write_cr3. From what I understand, switch_mm called by Xenomai upon return to root would re-set the bit, and re-set cr3, which would be set to the kernel cr3 right after that, but this would result in the active_mm.cpu_vm_mask bit being set instead of cleared as expected. So, maybe an irqs off section is missing in leave_mm.

Quoted 7 lines
Quoted 3 lines

leave_mm is already protected by its caller smp_invalidate_interrupt - but now I'm parsing context_switch /wrt to lazy tlb.

Hmm... lazy tlb: This means a new task is switched in and has active_mm != mm. But do_page_fault reads task->mm... Just thoughts, no clear picture yet.

Looking closer at the call sites of switch_mm, I think our the problem is mostly related to use_mm from fs/aio.c (customer is using aio heavily). But other callers need protection, too. We are going to test this patch tomorrow:

fs/aio.c +3 -0
index 76da125..d90fca3 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
+	local_irq_save_hw_cond(flags);
 	tsk->mm = mm;
 	tsk->active_mm = mm;
 	switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
kernel/sched.c +5 -1
index aa8f86c..8c545a4 100644
@@ -2668,8 +2668,12 @@ context_switch(struct rq *rq, struct task_struct *prev,
 		next->active_mm = oldmm;
 		atomic_inc(&oldmm->mm_count);
 		enter_lazy_tlb(oldmm, next);
-	} else
+	} else {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(oldmm, mm, next);
+		local_irq_restore_hw_cond(flags);
+	}
 
 	if (unlikely(!prev->mm)) {
 		prev->active_mm = NULL;
@@ -6406,8 +6410,12 @@ void idle_task_exit(void)
 
 	BUG_ON(cpu_online(smp_processor_id()));
 
Quoted 13 lines

kernel/sched.c void idle_task_exit(void) · line 6420

Quoted 9 lines
kernel/sched.c +5 -1
-	if (mm != &init_mm)
+	if (mm != &init_mm) {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(mm, &init_mm, current);
+		local_irq_restore_hw_cond(flags);
+	}
 	mmdrop(mm);
 }

Please fix the callee instead of ironing the call sites. This would avoid further issues as upstream emits additional switch_mm calls over time, and make ironing activate_mm useless.

Quoted 5 lines

Btw, this how the powerpc port works in locked switch mode, and this particular bug does not apply in unlocked switch mode anyway; this is why we don't have that problem on this arch. The ARM port always works in unlocked switch mode IIRC for latency reasons, so this should be a non-issue here as well. Gilles, would you confirm this?

Quoted 4 lines

Yes, on arm switch_mm always has irqs on. We handle the case where switch_mm was interrupted in the middle of its operations by restarting the mm switch after xenomai switches back to Linux.

On archs with non-atomic switch_mm(), use_mm() will require a different strategy. I'm thinking about something like

use_mm():

	set_some_flag();
	barrier();
	current->mm = new_mm;
	current->active_mm = new_mm;
	switch_mm(old_active_mm, new_mm, current);
	clear_some_flag();

and switch_mm():

	...
	if (likely(prev != next) || some_flag_set()) {
		clear_some_flag();
		...

ie. enforce mm switch if we may have interrupted use_mm at an unpleasant time. I just don't know yet where to attach that some_flag to. Should be the current task, but can we always access it from switch_mm?

Jan

#31 lore raw
Quoted 139 lines · Jan Kiszka
Quoted 116 lines · Gilles Chanteperdrix
Quoted 115 lines · Philippe Gerum
Quoted 114 lines · Philippe Gerum
Quoted 99 lines · Jan Kiszka
Quoted 44 lines · Jan Kiszka
Quoted 43 lines · Jan Kiszka
Quoted 20 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the
test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Ha, that's the point: only activate_mm is protected, but we have more spots in 2.6.29 and maybe other kernels, too!

Ok, I do not see where switch_mm is called with IRQs off. What I found,

We have two direct callers of switch_mm in sched.c and one in fs/aio.c. Both need protection (I pushed IRQ disabling into switch_mm), but that is not enough according to current tests. It seems to reduce to probability of corruption, though.

Quoted 9 lines

however, is that leave_mm sets the cr3 and just clears active_mm->cpu_vm_mask. So, at this point, we have a discrepancy between cr3 and active_mm. I do not know what could happen if Xenomai could interrupt leave_mm between the cpu_clear and the write_cr3. From what I understand, switch_mm called by Xenomai upon return to root would re-set the bit, and re-set cr3, which would be set to the kernel cr3 right after that, but this would result in the active_mm.cpu_vm_mask bit being set instead of cleared as expected. So, maybe an irqs off section is missing in leave_mm.

Quoted 7 lines
Quoted 3 lines

leave_mm is already protected by its caller smp_invalidate_interrupt - but now I'm parsing context_switch /wrt to lazy tlb.

Hmm... lazy tlb: This means a new task is switched in and has active_mm != mm. But do_page_fault reads task->mm... Just thoughts, no clear picture yet.

Looking closer at the call sites of switch_mm, I think our the problem is mostly related to use_mm from fs/aio.c (customer is using aio heavily). But other callers need protection, too. We are going to test this patch tomorrow:

fs/aio.c +3 -0
index 76da125..d90fca3 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
+	local_irq_save_hw_cond(flags);
 	tsk->mm = mm;
 	tsk->active_mm = mm;
 	switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
kernel/sched.c +5 -1
index aa8f86c..8c545a4 100644
@@ -2668,8 +2668,12 @@ context_switch(struct rq *rq, struct task_struct *prev,
 		next->active_mm = oldmm;
 		atomic_inc(&oldmm->mm_count);
 		enter_lazy_tlb(oldmm, next);
-	} else
+	} else {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(oldmm, mm, next);
+		local_irq_restore_hw_cond(flags);
+	}
 
 	if (unlikely(!prev->mm)) {
 		prev->active_mm = NULL;
@@ -6406,8 +6410,12 @@ void idle_task_exit(void)
 
 	BUG_ON(cpu_online(smp_processor_id()));

kernel/sched.c void idle_task_exit(void) · line 6420

Quoted 10 lines
kernel/sched.c +5 -1
 
-	if (mm != &init_mm)
+	if (mm != &init_mm) {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(mm, &init_mm, current);
+		local_irq_restore_hw_cond(flags);
+	}
 	mmdrop(mm);
 }

Please fix the callee instead of ironing the call sites. This would avoid further issues as upstream emits additional switch_mm calls over time, and make ironing activate_mm useless.

Quoted 9 lines
Quoted 5 lines

Btw, this how the powerpc port works in locked switch mode, and this particular bug does not apply in unlocked switch mode anyway; this is why we don't have that problem on this arch. The ARM port always works in unlocked switch mode IIRC for latency reasons, so this should be a non-issue here as well. Gilles, would you confirm this?

Yes, on arm switch_mm always has irqs on. We handle the case where switch_mm was interrupted in the middle of its operations by restarting the mm switch after xenomai switches back to Linux.

On archs with non-atomic switch_mm(), use_mm() will require a different strategy. I'm thinking about something like

use_mm():

	set_some_flag();
	barrier();
	current->mm = new_mm;
	current->active_mm = new_mm;
	switch_mm(old_active_mm, new_mm, current);
	clear_some_flag();

and switch_mm():

Quoted 8 lines
	...
	if (likely(prev != next) || some_flag_set()) {
		clear_some_flag();
		...

ie. enforce mm switch if we may have interrupted use_mm at an unpleasant time. I just don't know yet where to attach that some_flag to. Should be the current task, but can we always access it from switch_mm?

These mechanisms are already in place. All you have to do is:

use_mm()

	ipipe_active_mm = NULL;
	barrier();
	current->mm = new_mm;
	current->active_mm = new_mm;
	switch_mm(old_active_mm, new_mm, current);
Signature · 1 line
					    Gilles.
#35 lore raw
In reply to Jan Kiszka
Quoted 116 lines · Jan Kiszka
Quoted 101 lines · Philippe Gerum
Quoted 100 lines · Jan Kiszka
Quoted 44 lines · Jan Kiszka
Quoted 43 lines · Jan Kiszka
Quoted 20 lines · Gilles Chanteperdrix
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 2 lines · Jan Kiszka

It's still unclear what goes on precisely, we are still digging, but the test system that can produce this is highly contended.

Short update: Further instrumentation revealed that cr3 differs from active_mm->pgd while we are looping over that fault, ie. the kernel tries to fixup the wrong mm. And that means we have some open race window between updating cr3 and active_mm somewhere (isn't switch_mm run in a preemptible manner now?).

Maybe the rsp is wrong and leads you to the wrong active_mm ?

Quoted 3 lines

As a first shot I disabled CONFIG_IPIPE_DELAYED_ATOMICSW, and we are now checking if it makes a difference. Digging deeper into the code in the meanwhile...

As you have found out in the mean time, we do not use unlocked context switches on x86.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

Yes.

The last question I asked myself (but couldn't answer yet due to other activity) was: Where are the local_irq_disable/enable_hw around switch_mm for its Linux callers?

Ha, that's the point: only activate_mm is protected, but we have more spots in 2.6.29 and maybe other kernels, too!

Ok, I do not see where switch_mm is called with IRQs off. What I found,

We have two direct callers of switch_mm in sched.c and one in fs/aio.c. Both need protection (I pushed IRQ disabling into switch_mm), but that is not enough according to current tests. It seems to reduce to probability of corruption, though.

Quoted 9 lines

however, is that leave_mm sets the cr3 and just clears active_mm->cpu_vm_mask. So, at this point, we have a discrepancy between cr3 and active_mm. I do not know what could happen if Xenomai could interrupt leave_mm between the cpu_clear and the write_cr3. From what I understand, switch_mm called by Xenomai upon return to root would re-set the bit, and re-set cr3, which would be set to the kernel cr3 right after that, but this would result in the active_mm.cpu_vm_mask bit being set instead of cleared as expected. So, maybe an irqs off section is missing in leave_mm.

Quoted 7 lines
Quoted 3 lines

leave_mm is already protected by its caller smp_invalidate_interrupt - but now I'm parsing context_switch /wrt to lazy tlb.

Hmm... lazy tlb: This means a new task is switched in and has active_mm != mm. But do_page_fault reads task->mm... Just thoughts, no clear picture yet.

Looking closer at the call sites of switch_mm, I think our the problem is mostly related to use_mm from fs/aio.c (customer is using aio heavily). But other callers need protection, too. We are going to test this patch tomorrow:

fs/aio.c +3 -0
index 76da125..d90fca3 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
+	local_irq_save_hw_cond(flags);
 	tsk->mm = mm;
 	tsk->active_mm = mm;
 	switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
kernel/sched.c +5 -1
index aa8f86c..8c545a4 100644
@@ -2668,8 +2668,12 @@ context_switch(struct rq *rq, struct task_struct *prev,
 		next->active_mm = oldmm;
 		atomic_inc(&oldmm->mm_count);
 		enter_lazy_tlb(oldmm, next);
-	} else
+	} else {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(oldmm, mm, next);
+		local_irq_restore_hw_cond(flags);
+	}
 
 	if (unlikely(!prev->mm)) {
 		prev->active_mm = NULL;
@@ -6406,8 +6410,12 @@ void idle_task_exit(void)
 
 	BUG_ON(cpu_online(smp_processor_id()));
 
Quoted 13 lines

kernel/sched.c void idle_task_exit(void) · line 6420

Quoted 9 lines
kernel/sched.c +5 -1
-	if (mm != &init_mm)
+	if (mm != &init_mm) {
+		unsigned long flags;
+		local_irq_save_hw_cond(flags);
 		switch_mm(mm, &init_mm, current);
+		local_irq_restore_hw_cond(flags);
+	}
 	mmdrop(mm);
 }

Please fix the callee instead of ironing the call sites. This would avoid further issues as upstream emits additional switch_mm calls over time, and make ironing activate_mm useless.

Quoted 3 lines

This was my first idea, too, but it didn't work, see use_mm(). I don't think we will get around reviewing switch_mm users on kernel updates.

At the very least, you won't have to patch separately the spots that may work this way. Avoiding uselessly invasive changes is not a substitute for proper reviewing in any case.

Quoted 2 lines

Jan

Signature · 1 line
Philippe.
author
#32 lore raw
In reply to Gilles Chanteperdrix
Quoted 22 lines · Gilles Chanteperdrix
Quoted 12 lines · Jan Kiszka

On archs with non-atomic switch_mm(), use_mm() will require a different strategy. I'm thinking about something like

use_mm():

	set_some_flag();
	barrier();
	current->mm = new_mm;
	current->active_mm = new_mm;
	switch_mm(old_active_mm, new_mm, current);
	clear_some_flag();

and switch_mm():

Quoted 8 lines
	...
	if (likely(prev != next) || some_flag_set()) {
		clear_some_flag();
		...

ie. enforce mm switch if we may have interrupted use_mm at an unpleasant time. I just don't know yet where to attach that some_flag to. Should be the current task, but can we always access it from switch_mm?

Quoted 9 lines

These mechanisms are already in place. All you have to do is:

use_mm()

	ipipe_active_mm = NULL;
	barrier();
	current->mm = new_mm;
	current->active_mm = new_mm;
	switch_mm(old_active_mm, new_mm, current);

As far as I understand, ipipe_active_mm has different semantics on ARM. Specifically, it has no relation to the initial "next != prev" check. The test we need is likely orthogonal to this.

Jan

#33 lore raw
Quoted 35 lines · Jan Kiszka
Quoted 24 lines · Gilles Chanteperdrix
Quoted 17 lines · Jan Kiszka

On archs with non-atomic switch_mm(), use_mm() will require a different strategy. I'm thinking about something like

use_mm():

	set_some_flag();
	barrier();
	current->mm = new_mm;
	current->active_mm = new_mm;
	switch_mm(old_active_mm, new_mm, current);
	clear_some_flag();

and switch_mm():

	...
	if (likely(prev != next) || some_flag_set()) {
		clear_some_flag();
		...
Quoted 5 lines

ie. enforce mm switch if we may have interrupted use_mm at an unpleasant
time. I just don't know yet where to attach that some_flag to. Should be
the current task, but can we always access it from switch_mm?

Quoted 9 lines

These mechanisms are already in place. All you have to do is:

use_mm()

	ipipe_active_mm = NULL;
	barrier();
	current->mm = new_mm;
	current->active_mm = new_mm;
	switch_mm(old_active_mm, new_mm, current);
Quoted 3 lines

As far as I understand, ipipe_active_mm has different semantics on ARM. Specifically, it has no relation to the initial "next != prev" check. The test we need is likely orthogonal to this.

Well except that prev is ipipe_active_mm, so if NULL next != prev is always true. ipipe_active_mm set to NULL means that the mm state is currently unknown, what it triggers is that if Xenomai switches at this moment, it will forcibly reload the mm state, and when switching back to Linux it will not touch the mm state, but mark the MMSWITCHINT flag in the current thread state, switch_mm will detect the flag and restart the mm switch. This mechanism is well tested and we would have had problems before if it was not working (the mm switch on ARM does a full cache flush which stands for about 100us, so the chances to be preempted at this moment are in fact pretty high).

So, I still think that setting ipipe_active_mm to NULL at the beginning of use_mm is all what is needed, else I do not understand what you are talking about.

Signature · 1 line
                    Gilles.
author
#36 lore raw
In reply to Jan Kiszka

Hi again,

this is now basically the patch which seems to stabilized x86 /wrt mmu switches again:

There were 3 race windows between setting active_mm of the current task and actually switching it (that's a noarch issue), there were several calls into switch_mm without proper hard interrupt protection (on archs that have no preemptible switch_mm, like x86) and there was a race in x86's leave_mm (as Gilles already remarked earlier in this thread - while I was looking at an old tree where smp_invalidate_interrupt took care of this).

The patch is thought as a basis for further discussions about

o how to solve all the issues for all archs technically (ideally without the need to patch noarch parts in an arch-specific way...)

o if anyone thinks there could be more spots like these (I've checked the code only for x86 so far)

Jan

-------->

arch/x86/include/asm/mmu_context.h +9 -4
index dddfb84..d261b77 100644
@@ -29,12 +29,17 @@ void destroy_context(struct mm_struct *mm);
 
 #define activate_mm(prev, next)			\
 do {						\
- 	unsigned long flags;			\
 	paravirt_activate_mm((prev), (next));	\
- 	local_irq_save_hw_cond(flags);		\
-	switch_mm((prev), (next), NULL);	\
- 	local_irq_restore_hw_cond(flags);	\
+	__switch_mm((prev), (next), NULL);	\
 } while (0);
 
+static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
+			     struct task_struct *tsk)
+{
+	unsigned long flags;
+	local_irq_save_hw_cond(flags);
+	__switch_mm(prev, next, tsk);
+	local_irq_restore_hw_cond(flags);
+}
 
 #endif /* _ASM_X86_MMU_CONTEXT_H */
arch/x86/include/asm/mmu_context_32.h +4 -3
index 7e98ce1..e51cd09 100644
@@ -9,12 +9,13 @@ static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
 #endif
 }
 
-static inline void switch_mm(struct mm_struct *prev,
-			     struct mm_struct *next,
-			     struct task_struct *tsk)
+static inline void __switch_mm(struct mm_struct *prev,
+			       struct mm_struct *next,
+			       struct task_struct *tsk)
 {
 	int cpu = smp_processor_id();
 
+	WARN_ON_ONCE(!irqs_disabled_hw());
 	if (likely(prev != next)) {
 		/* stop flush ipis for the previous mm */
 		cpu_clear(cpu, prev->cpu_vm_mask);
arch/x86/include/asm/mmu_context_64.h +4 -2
index 677d36e..0118200 100644
@@ -11,10 +11,12 @@ static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
 #endif
 }
 
-static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
-			     struct task_struct *tsk)
+static inline void __switch_mm(struct mm_struct *prev, struct mm_struct *next,
+			       struct task_struct *tsk)
 {
 	unsigned cpu = smp_processor_id();
+
+	WARN_ON_ONCE(!irqs_disabled_hw());
 	if (likely(prev != next)) {
 		/* stop flush ipis for the previous mm */
 		cpu_clear(cpu, prev->cpu_vm_mask);
arch/x86/kernel/tlb_32.c +4 -0
index 08028a7..f68f2a6 100644
@@ -34,9 +34,13 @@ static DEFINE_SPINLOCK(tlbstate_lock);
  */
 void leave_mm(int cpu)
 {
+	unsigned long flags;
+
 	BUG_ON(x86_read_percpu(cpu_tlbstate.state) == TLBSTATE_OK);
+	local_irq_save_hw_cond(flags);
 	cpu_clear(cpu, x86_read_percpu(cpu_tlbstate.active_mm)->cpu_vm_mask);
 	load_cr3(swapper_pg_dir);
+	local_irq_restore_hw_cond(flags);
 }
 EXPORT_SYMBOL_GPL(leave_mm);
 
arch/x86/kernel/tlb_64.c +4 -0
index ce54e12..9829990 100644
@@ -62,10 +62,14 @@ static DEFINE_PER_CPU(union smp_flush_state, flush_state);
  */
 void leave_mm(int cpu)
 {
+	unsigned long flags;
+
 	if (read_pda(mmu_state) == TLBSTATE_OK)
 		BUG();
+	local_irq_save_hw_cond(flags);
 	cpu_clear(cpu, read_pda(active_mm)->cpu_vm_mask);
 	load_cr3(swapper_pg_dir);
+	local_irq_restore_hw_cond(flags);
 }
 EXPORT_SYMBOL_GPL(leave_mm);
 
fs/aio.c +4 -1
index 76da125..0286f0f 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
-	switch_mm(active_mm, mm, tsk);
+	__switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
fs/exec.c +3 -0
index 3b36c69..06591ac 100644
@@ -718,6 +718,7 @@ static int exec_mmap(struct mm_struct *mm)
 {
 	struct task_struct *tsk;
 	struct mm_struct * old_mm, *active_mm;
+	unsigned long flags;
 
 	/* Notify parent that we're no longer interested in the old VM */
 	tsk = current;
@@ -740,8 +741,10 @@ static int exec_mmap(struct mm_struct *mm)
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
 	activate_mm(active_mm, mm);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 	arch_pick_mmap_layout(mm);
 	if (old_mm) {
kernel/fork.c +3 -0
index 01a836b..cf3b68a 100644
@@ -1665,11 +1665,14 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
 		}
 
 		if (new_mm) {
+			unsigned long flags;
 			mm = current->mm;
 			active_mm = current->active_mm;
 			current->mm = new_mm;
+			local_irq_save_hw_cond(flags);
 			current->active_mm = new_mm;
 			activate_mm(active_mm, new_mm);
+			local_irq_restore_hw_cond(flags);
 			new_mm = mm;
 		}
  
Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#37 lore raw
Quoted 73 lines · Jan Kiszka

Hi again,

this is now basically the patch which seems to stabilized x86 /wrt mmu switches again:

There were 3 race windows between setting active_mm of the current task and actually switching it (that's a noarch issue), there were several calls into switch_mm without proper hard interrupt protection (on archs that have no preemptible switch_mm, like x86) and there was a race in x86's leave_mm (as Gilles already remarked earlier in this thread - while I was looking at an old tree where smp_invalidate_interrupt took care of this).

The patch is thought as a basis for further discussions about

o how to solve all the issues for all archs technically (ideally without the need to patch noarch parts in an arch-specific way...)

o if anyone thinks there could be more spots like these (I've checked the code only for x86 so far)

fs/aio.c +4 -1
index 76da125..0286f0f 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
-	switch_mm(active_mm, mm, tsk);
+	__switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
fs/exec.c +3 -0
index 3b36c69..06591ac 100644
@@ -718,6 +718,7 @@ static int exec_mmap(struct mm_struct *mm)
 {
 	struct task_struct *tsk;
 	struct mm_struct * old_mm, *active_mm;
+	unsigned long flags;
 
 	/* Notify parent that we're no longer interested in the old VM */
 	tsk = current;
@@ -740,8 +741,10 @@ static int exec_mmap(struct mm_struct *mm)
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
 	activate_mm(active_mm, mm);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 	arch_pick_mmap_layout(mm);
 	if (old_mm) {
kernel/fork.c +0 -0
index 01a836b..cf3b68a 100644
@@ -1665,11 +1665,14 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
 		}
 
 		if (new_mm) {

kernel/fork.c SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) · line 1677

Quoted 10 lines
kernel/fork.c +3 -0
+			unsigned long flags;
 			mm = current->mm;
 			active_mm = current->active_mm;
 			current->mm = new_mm;
+			local_irq_save_hw_cond(flags);
 			current->active_mm = new_mm;
 			activate_mm(active_mm, new_mm);
+			local_irq_restore_hw_cond(flags);
 			new_mm = mm;
 		}

Ok. These are patches of the noarch code which are dependent on x86 implementation.

I think we need something like

mm_change_enter(flags);
mm_change_leave(flags);

which would resolve to ipipe_active_mm = NULL on architectures with unlocked context switches, and to local_irq_save_hw/local_irq_restore on x86?

Quoted 2 lines
Signature · 1 line
					    Gilles.
author
#38 lore raw
Quoted 88 lines · Gilles Chanteperdrix
Quoted 73 lines · Jan Kiszka

Hi again,

this is now basically the patch which seems to stabilized x86 /wrt mmu switches again:

There were 3 race windows between setting active_mm of the current task and actually switching it (that's a noarch issue), there were several calls into switch_mm without proper hard interrupt protection (on archs that have no preemptible switch_mm, like x86) and there was a race in x86's leave_mm (as Gilles already remarked earlier in this thread - while I was looking at an old tree where smp_invalidate_interrupt took care of this).

The patch is thought as a basis for further discussions about

o how to solve all the issues for all archs technically (ideally without the need to patch noarch parts in an arch-specific way...)

o if anyone thinks there could be more spots like these (I've checked the code only for x86 so far)

fs/aio.c +4 -1
index 76da125..0286f0f 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
-	switch_mm(active_mm, mm, tsk);
+	__switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
fs/exec.c +3 -0
index 3b36c69..06591ac 100644
@@ -718,6 +718,7 @@ static int exec_mmap(struct mm_struct *mm)
 {
 	struct task_struct *tsk;
 	struct mm_struct * old_mm, *active_mm;
+	unsigned long flags;
 
 	/* Notify parent that we're no longer interested in the old VM */
 	tsk = current;
@@ -740,8 +741,10 @@ static int exec_mmap(struct mm_struct *mm)
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
 	activate_mm(active_mm, mm);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 	arch_pick_mmap_layout(mm);
 	if (old_mm) {
kernel/fork.c +0 -0
index 01a836b..cf3b68a 100644
@@ -1665,11 +1665,14 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
 		}
 
 		if (new_mm) {

kernel/fork.c SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) · line 1677

Quoted 10 lines
kernel/fork.c +3 -0
+			unsigned long flags;
 			mm = current->mm;
 			active_mm = current->active_mm;
 			current->mm = new_mm;
+			local_irq_save_hw_cond(flags);
 			current->active_mm = new_mm;
 			activate_mm(active_mm, new_mm);
+			local_irq_restore_hw_cond(flags);
 			new_mm = mm;
 		}

Ok. These are patches of the noarch code which are dependent on x86 implementation.

Quoted 8 lines

I think we need something like

mm_change_enter(flags);
mm_change_leave(flags);

which would resolve to ipipe_active_mm = NULL on architectures with unlocked context switches, and to local_irq_save_hw/local_irq_restore on x86?

+ we have to change switch_mm in the lockless case, and maybe also its caller (if I look at the arm code), to use ipipe_active_mm in order to decide if to switch or not - see my explanation of the possible race in aio.c.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#39 lore raw
Quoted 93 lines · Jan Kiszka
Quoted 92 lines · Gilles Chanteperdrix
Quoted 73 lines · Jan Kiszka

Hi again,

this is now basically the patch which seems to stabilized x86 /wrt mmu switches again:

There were 3 race windows between setting active_mm of the current task and actually switching it (that's a noarch issue), there were several calls into switch_mm without proper hard interrupt protection (on archs that have no preemptible switch_mm, like x86) and there was a race in x86's leave_mm (as Gilles already remarked earlier in this thread - while I was looking at an old tree where smp_invalidate_interrupt took care of this).

The patch is thought as a basis for further discussions about

o how to solve all the issues for all archs technically (ideally without the need to patch noarch parts in an arch-specific way...)

o if anyone thinks there could be more spots like these (I've checked the code only for x86 so far)

fs/aio.c +4 -1
index 76da125..0286f0f 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
-	switch_mm(active_mm, mm, tsk);
+	__switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
fs/exec.c +3 -0
index 3b36c69..06591ac 100644
@@ -718,6 +718,7 @@ static int exec_mmap(struct mm_struct *mm)
 {
 	struct task_struct *tsk;
 	struct mm_struct * old_mm, *active_mm;
+	unsigned long flags;
 
 	/* Notify parent that we're no longer interested in the old VM */
 	tsk = current;
@@ -740,8 +741,10 @@ static int exec_mmap(struct mm_struct *mm)
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
 	activate_mm(active_mm, mm);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 	arch_pick_mmap_layout(mm);
 	if (old_mm) {
kernel/fork.c +0 -0
index 01a836b..cf3b68a 100644
@@ -1665,11 +1665,14 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
 		}
 
 		if (new_mm) {

kernel/fork.c SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) · line 1677

Quoted 10 lines
kernel/fork.c +3 -0
+			unsigned long flags;
 			mm = current->mm;
 			active_mm = current->active_mm;
 			current->mm = new_mm;
+			local_irq_save_hw_cond(flags);
 			current->active_mm = new_mm;
 			activate_mm(active_mm, new_mm);
+			local_irq_restore_hw_cond(flags);
 			new_mm = mm;
 		}

Ok. These are patches of the noarch code which are dependent on x86 implementation.

I think we need something like

mm_change_enter(flags);
mm_change_leave(flags);

Quoted 8 lines
Quoted 3 lines

which would resolve to ipipe_active_mm = NULL on architectures with unlocked context switches, and to local_irq_save_hw/local_irq_restore on x86?

+ we have to change switch_mm in the lockless case, and maybe also its caller (if I look at the arm code), to use ipipe_active_mm in order to decide if to switch or not - see my explanation of the possible race in aio.c.

This is already the way it is currently working. the "prev" passed to switch_mm is always ipipe_active_mm.

Signature · 1 line
					    Gilles.
author
#40 lore raw
Quoted 97 lines · Gilles Chanteperdrix
Quoted 96 lines · Jan Kiszka
Quoted 95 lines · Gilles Chanteperdrix
Quoted 73 lines · Jan Kiszka

Hi again,

this is now basically the patch which seems to stabilized x86 /wrt mmu switches again:

There were 3 race windows between setting active_mm of the current task and actually switching it (that's a noarch issue), there were several calls into switch_mm without proper hard interrupt protection (on archs that have no preemptible switch_mm, like x86) and there was a race in x86's leave_mm (as Gilles already remarked earlier in this thread - while I was looking at an old tree where smp_invalidate_interrupt took care of this).

The patch is thought as a basis for further discussions about

o how to solve all the issues for all archs technically (ideally without the need to patch noarch parts in an arch-specific way...)

o if anyone thinks there could be more spots like these (I've checked the code only for x86 so far)

fs/aio.c +4 -1
index 76da125..0286f0f 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
-	switch_mm(active_mm, mm, tsk);
+	__switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
fs/exec.c +3 -0
index 3b36c69..06591ac 100644
@@ -718,6 +718,7 @@ static int exec_mmap(struct mm_struct *mm)
 {
 	struct task_struct *tsk;
 	struct mm_struct * old_mm, *active_mm;
+	unsigned long flags;
 
 	/* Notify parent that we're no longer interested in the old VM */
 	tsk = current;
@@ -740,8 +741,10 @@ static int exec_mmap(struct mm_struct *mm)
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
 	activate_mm(active_mm, mm);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 	arch_pick_mmap_layout(mm);
 	if (old_mm) {
kernel/fork.c +0 -0
index 01a836b..cf3b68a 100644
@@ -1665,11 +1665,14 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
 		}
 
 		if (new_mm) {

kernel/fork.c SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) · line 1677

Quoted 10 lines
kernel/fork.c +3 -0
+			unsigned long flags;
 			mm = current->mm;
 			active_mm = current->active_mm;
 			current->mm = new_mm;
+			local_irq_save_hw_cond(flags);
 			current->active_mm = new_mm;
 			activate_mm(active_mm, new_mm);
+			local_irq_restore_hw_cond(flags);
 			new_mm = mm;
 		}

Ok. These are patches of the noarch code which are dependent on x86 implementation.

I think we need something like

mm_change_enter(flags);
mm_change_leave(flags);

which would resolve to ipipe_active_mm = NULL on architectures with unlocked context switches, and to local_irq_save_hw/local_irq_restore on x86?

Quoted 8 lines
Quoted 4 lines

+ we have to change switch_mm in the lockless case, and maybe also its caller (if I look at the arm code), to use ipipe_active_mm in order to decide if to switch or not - see my explanation of the possible race in aio.c.

This is already the way it is currently working. the "prev" passed to switch_mm is always ipipe_active_mm.

Ah, I missed the difference in arm's xnarch_leave_root. Now I got it.

OK, but this needs to be thought through in details again, specifically also the enter-root scenarios. I have no safe feeling about it yet, partly because switch_mm fiddles with ipipe_active_mm as well, but more generally because we used to have such critical races in this area for a "fairly" long time.

Jan

#41 lore raw
Quoted 106 lines · Jan Kiszka
Quoted 97 lines · Gilles Chanteperdrix
Quoted 96 lines · Jan Kiszka
Quoted 95 lines · Gilles Chanteperdrix
Quoted 73 lines · Jan Kiszka

Hi again,

this is now basically the patch which seems to stabilized x86 /wrt mmu switches again:

There were 3 race windows between setting active_mm of the current task and actually switching it (that's a noarch issue), there were several calls into switch_mm without proper hard interrupt protection (on archs that have no preemptible switch_mm, like x86) and there was a race in x86's leave_mm (as Gilles already remarked earlier in this thread - while I was looking at an old tree where smp_invalidate_interrupt took care of this).

The patch is thought as a basis for further discussions about

o how to solve all the issues for all archs technically (ideally without the need to patch noarch parts in an arch-specific way...)

o if anyone thinks there could be more spots like these (I've checked the code only for x86 so far)

fs/aio.c +4 -1
index 76da125..0286f0f 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
-	switch_mm(active_mm, mm, tsk);
+	__switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
fs/exec.c +3 -0
index 3b36c69..06591ac 100644
@@ -718,6 +718,7 @@ static int exec_mmap(struct mm_struct *mm)
 {
 	struct task_struct *tsk;
 	struct mm_struct * old_mm, *active_mm;
+	unsigned long flags;
 
 	/* Notify parent that we're no longer interested in the old VM */
 	tsk = current;
@@ -740,8 +741,10 @@ static int exec_mmap(struct mm_struct *mm)
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
 	activate_mm(active_mm, mm);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 	arch_pick_mmap_layout(mm);
 	if (old_mm) {
kernel/fork.c +0 -0
index 01a836b..cf3b68a 100644
@@ -1665,11 +1665,14 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
 		}
 
 		if (new_mm) {

kernel/fork.c SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) · line 1677

Quoted 10 lines
kernel/fork.c +3 -0
+			unsigned long flags;
 			mm = current->mm;
 			active_mm = current->active_mm;
 			current->mm = new_mm;
+			local_irq_save_hw_cond(flags);
 			current->active_mm = new_mm;
 			activate_mm(active_mm, new_mm);
+			local_irq_restore_hw_cond(flags);
 			new_mm = mm;
 		}

Ok. These are patches of the noarch code which are dependent on x86 implementation.

I think we need something like

mm_change_enter(flags);
mm_change_leave(flags);

which would resolve to ipipe_active_mm = NULL on architectures with unlocked context switches, and to local_irq_save_hw/local_irq_restore on x86?

Quoted 7 lines
Quoted 4 lines

+ we have to change switch_mm in the lockless case, and maybe also its caller (if I look at the arm code), to use ipipe_active_mm in order to decide if to switch or not - see my explanation of the possible race in aio.c.

This is already the way it is currently working. the "prev" passed to switch_mm is always ipipe_active_mm.

Quoted 7 lines

Ah, I missed the difference in arm's xnarch_leave_root. Now I got it.

OK, but this needs to be thought through in details again, specifically also the enter-root scenarios. I have no safe feeling about it yet, partly because switch_mm fiddles with ipipe_active_mm as well, but more generally because we used to have such critical races in this area for a "fairly" long time.

When reentering root with ipipe_active_mm NULL, the TIF_MMSWTICH_INT bit is set, in which case the switch_mm preempted by Xenomai is restarted.

Signature · 1 line
					    Gilles.
author
#42 lore raw
Quoted 108 lines · Gilles Chanteperdrix
Quoted 107 lines · Jan Kiszka
Quoted 97 lines · Gilles Chanteperdrix
Quoted 96 lines · Jan Kiszka
Quoted 95 lines · Gilles Chanteperdrix
Quoted 73 lines · Jan Kiszka

Hi again,

this is now basically the patch which seems to stabilized x86 /wrt mmu switches again:

There were 3 race windows between setting active_mm of the current task and actually switching it (that's a noarch issue), there were several calls into switch_mm without proper hard interrupt protection (on archs that have no preemptible switch_mm, like x86) and there was a race in x86's leave_mm (as Gilles already remarked earlier in this thread - while I was looking at an old tree where smp_invalidate_interrupt took care of this).

The patch is thought as a basis for further discussions about

o how to solve all the issues for all archs technically (ideally without the need to patch noarch parts in an arch-specific way...)

o if anyone thinks there could be more spots like these (I've checked the code only for x86 so far)

fs/aio.c +4 -1
index 76da125..0286f0f 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
-	switch_mm(active_mm, mm, tsk);
+	__switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
fs/exec.c +3 -0
index 3b36c69..06591ac 100644
@@ -718,6 +718,7 @@ static int exec_mmap(struct mm_struct *mm)
 {
 	struct task_struct *tsk;
 	struct mm_struct * old_mm, *active_mm;
+	unsigned long flags;
 
 	/* Notify parent that we're no longer interested in the old VM */
 	tsk = current;
@@ -740,8 +741,10 @@ static int exec_mmap(struct mm_struct *mm)
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
 	activate_mm(active_mm, mm);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 	arch_pick_mmap_layout(mm);
 	if (old_mm) {
kernel/fork.c +0 -0
index 01a836b..cf3b68a 100644
@@ -1665,11 +1665,14 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
 		}
 
 		if (new_mm) {

kernel/fork.c SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) · line 1677

Quoted 10 lines
kernel/fork.c +3 -0
+			unsigned long flags;
 			mm = current->mm;
 			active_mm = current->active_mm;
 			current->mm = new_mm;
+			local_irq_save_hw_cond(flags);
 			current->active_mm = new_mm;
 			activate_mm(active_mm, new_mm);
+			local_irq_restore_hw_cond(flags);
 			new_mm = mm;
 		}

Ok. These are patches of the noarch code which are dependent on x86 implementation.

I think we need something like

mm_change_enter(flags);
mm_change_leave(flags);

which would resolve to ipipe_active_mm = NULL on architectures with unlocked context switches, and to local_irq_save_hw/local_irq_restore on x86?

Quoted 7 lines
Quoted 4 lines

+ we have to change switch_mm in the lockless case, and maybe also its caller (if I look at the arm code), to use ipipe_active_mm in order to decide if to switch or not - see my explanation of the possible race in aio.c.

This is already the way it is currently working. the "prev" passed to switch_mm is always ipipe_active_mm.

Ah, I missed the difference in arm's xnarch_leave_root. Now I got it.

Quoted 9 lines
Quoted 5 lines

OK, but this needs to be thought through in details again, specifically also the enter-root scenarios. I have no safe feeling about it yet, partly because switch_mm fiddles with ipipe_active_mm as well, but more generally because we used to have such critical races in this area for a "fairly" long time.

When reentering root with ipipe_active_mm NULL, the TIF_MMSWTICH_INT bit is set, in which case the switch_mm preempted by Xenomai is restarted.

Right. But now we are also dealing with preemptions outside that loop. And we are dealing with archs that have atomic switch_mm, thus no such loops.

The goal should be to find a solution against the races outside switch_mm that is applicable to all archs. But the question is still unanswered to me if we can simply reuse existing ipipe_active_mm without disturbing its current use in non-atomic switch_mm.

Moreover, part of the problems on x86 was the non-atomic update of cpu_vm_mask vs. actually switching the hardware. How do you deal with cpu_vm_mask on ARM? Why can you call switch_mm without IRQ protection? My impression is that only the (costly) cpu_switch_mm should be allowed to be preemptible to have a safe state around it. I'm also asking as we would have to address this on x86 & co. once switch_mm could be called with undefined prev_mm.

Jan

#43 lore raw
Quoted 127 lines · Jan Kiszka
Quoted 108 lines · Gilles Chanteperdrix
Quoted 107 lines · Jan Kiszka
Quoted 97 lines · Gilles Chanteperdrix
Quoted 96 lines · Jan Kiszka
Quoted 95 lines · Gilles Chanteperdrix
Quoted 73 lines · Jan Kiszka

Hi again,

this is now basically the patch which seems to stabilized x86 /wrt mmu switches again:

There were 3 race windows between setting active_mm of the current task and actually switching it (that's a noarch issue), there were several calls into switch_mm without proper hard interrupt protection (on archs that have no preemptible switch_mm, like x86) and there was a race in x86's leave_mm (as Gilles already remarked earlier in this thread - while I was looking at an old tree where smp_invalidate_interrupt took care of this).

The patch is thought as a basis for further discussions about

o how to solve all the issues for all archs technically (ideally without the need to patch noarch parts in an arch-specific way...)

o if anyone thinks there could be more spots like these (I've checked the code only for x86 so far)

fs/aio.c +4 -1
index 76da125..0286f0f 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
-	switch_mm(active_mm, mm, tsk);
+	__switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
fs/exec.c +3 -0
index 3b36c69..06591ac 100644
@@ -718,6 +718,7 @@ static int exec_mmap(struct mm_struct *mm)
 {
 	struct task_struct *tsk;
 	struct mm_struct * old_mm, *active_mm;
+	unsigned long flags;
 
 	/* Notify parent that we're no longer interested in the old VM */
 	tsk = current;
@@ -740,8 +741,10 @@ static int exec_mmap(struct mm_struct *mm)
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
 	activate_mm(active_mm, mm);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 	arch_pick_mmap_layout(mm);
 	if (old_mm) {
kernel/fork.c +0 -0
index 01a836b..cf3b68a 100644
@@ -1665,11 +1665,14 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
 		}
 
 		if (new_mm) {

kernel/fork.c SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) · line 1677

Quoted 10 lines
kernel/fork.c +3 -0
+			unsigned long flags;
 			mm = current->mm;
 			active_mm = current->active_mm;
 			current->mm = new_mm;
+			local_irq_save_hw_cond(flags);
 			current->active_mm = new_mm;
 			activate_mm(active_mm, new_mm);
+			local_irq_restore_hw_cond(flags);
 			new_mm = mm;
 		}

Ok. These are patches of the noarch code which are dependent on x86 implementation.

I think we need something like

mm_change_enter(flags);
mm_change_leave(flags);

which would resolve to ipipe_active_mm = NULL on architectures with unlocked context switches, and to local_irq_save_hw/local_irq_restore on x86?

Quoted 7 lines
Quoted 4 lines

+ we have to change switch_mm in the lockless case, and maybe also its caller (if I look at the arm code), to use ipipe_active_mm in order to decide if to switch or not - see my explanation of the possible race in aio.c.

This is already the way it is currently working. the "prev" passed to switch_mm is always ipipe_active_mm.

Ah, I missed the difference in arm's xnarch_leave_root. Now I got it.

Quoted 8 lines
Quoted 5 lines

OK, but this needs to be thought through in details again, specifically also the enter-root scenarios. I have no safe feeling about it yet, partly because switch_mm fiddles with ipipe_active_mm as well, but more generally because we used to have such critical races in this area for a "fairly" long time.

When reentering root with ipipe_active_mm NULL, the TIF_MMSWTICH_INT bit is set, in which case the switch_mm preempted by Xenomai is restarted.

Right. But now we are also dealing with preemptions outside that loop. And we are dealing with archs that have atomic switch_mm, thus no such loops.

The goal should be to find a solution against the races outside switch_mm that is applicable to all archs. But the question is still unanswered to me if we can simply reuse existing ipipe_active_mm without disturbing its current use in non-atomic switch_mm.

Quoted 7 lines

Moreover, part of the problems on x86 was the non-atomic update of cpu_vm_mask vs. actually switching the hardware. How do you deal with cpu_vm_mask on ARM? Why can you call switch_mm without IRQ protection? My impression is that only the (costly) cpu_switch_mm should be allowed to be preemptible to have a safe state around it. I'm also asking as we would have to address this on x86 & co. once switch_mm could be called with undefined prev_mm.

when ipipe_active_mm is set to NULL, switch_mm does not touch the current mm cpu_vm_mask, since it does not know what the current mm is. However, we have a problem when switching back to root: we do not clear the previous mm cpu_vm_mask since we do not call switch_mm in xnarch_switch_to, we should fix this. So, I think we can extend the zone where ipipe_active_mm is set to NULL, to protect this area from the effects of preemption by Xenomai.

So, IMO, what we need is something like ipipe_mm_switch_protect which sets ipipe_active_mm to NULL on ARM, and results in local_irq_save_hw on x86.

Quoted 3 lines

Jan

Signature · 1 line
                                          Gilles
author
#44 lore raw
Quoted 143 lines · Gilles Chanteperdrix
Quoted 126 lines · Jan Kiszka
Quoted 108 lines · Gilles Chanteperdrix
Quoted 107 lines · Jan Kiszka
Quoted 97 lines · Gilles Chanteperdrix
Quoted 96 lines · Jan Kiszka
Quoted 95 lines · Gilles Chanteperdrix
Quoted 73 lines · Jan Kiszka

Hi again,

this is now basically the patch which seems to stabilized x86 /wrt mmu switches again:

There were 3 race windows between setting active_mm of the current task and actually switching it (that's a noarch issue), there were several calls into switch_mm without proper hard interrupt protection (on archs that have no preemptible switch_mm, like x86) and there was a race in x86's leave_mm (as Gilles already remarked earlier in this thread - while I was looking at an old tree where smp_invalidate_interrupt took care of this).

The patch is thought as a basis for further discussions about

o how to solve all the issues for all archs technically (ideally without the need to patch noarch parts in an arch-specific way...)

o if anyone thinks there could be more spots like these (I've checked the code only for x86 so far)

fs/aio.c +4 -1
index 76da125..0286f0f 100644
@@ -618,13 +618,16 @@ static void use_mm(struct mm_struct *mm)
 {
 	struct mm_struct *active_mm;
 	struct task_struct *tsk = current;
+	unsigned long flags;
 
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	atomic_inc(&mm->mm_count);
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
-	switch_mm(active_mm, mm, tsk);
+	__switch_mm(active_mm, mm, tsk);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 
 	mmdrop(active_mm);
fs/exec.c +3 -0
index 3b36c69..06591ac 100644
@@ -718,6 +718,7 @@ static int exec_mmap(struct mm_struct *mm)
 {
 	struct task_struct *tsk;
 	struct mm_struct * old_mm, *active_mm;
+	unsigned long flags;
 
 	/* Notify parent that we're no longer interested in the old VM */
 	tsk = current;
@@ -740,8 +741,10 @@ static int exec_mmap(struct mm_struct *mm)
 	task_lock(tsk);
 	active_mm = tsk->active_mm;
 	tsk->mm = mm;
+	local_irq_save_hw_cond(flags);
 	tsk->active_mm = mm;
 	activate_mm(active_mm, mm);
+	local_irq_restore_hw_cond(flags);
 	task_unlock(tsk);
 	arch_pick_mmap_layout(mm);
 	if (old_mm) {
kernel/fork.c +0 -0
index 01a836b..cf3b68a 100644
@@ -1665,11 +1665,14 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
 		}
 
 		if (new_mm) {

kernel/fork.c SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) · line 1677

Quoted 10 lines
kernel/fork.c +3 -0
+			unsigned long flags;
 			mm = current->mm;
 			active_mm = current->active_mm;
 			current->mm = new_mm;
+			local_irq_save_hw_cond(flags);
 			current->active_mm = new_mm;
 			activate_mm(active_mm, new_mm);
+			local_irq_restore_hw_cond(flags);
 			new_mm = mm;
 		}

Ok. These are patches of the noarch code which are dependent on x86 implementation.

I think we need something like

mm_change_enter(flags);
mm_change_leave(flags);

which would resolve to ipipe_active_mm = NULL on architectures with unlocked context switches, and to local_irq_save_hw/local_irq_restore on x86?

Quoted 7 lines
Quoted 4 lines

+ we have to change switch_mm in the lockless case, and maybe also its caller (if I look at the arm code), to use ipipe_active_mm in order to decide if to switch or not - see my explanation of the possible race in aio.c.

This is already the way it is currently working. the "prev" passed to switch_mm is always ipipe_active_mm.

Ah, I missed the difference in arm's xnarch_leave_root. Now I got it.

Quoted 8 lines
Quoted 5 lines

OK, but this needs to be thought through in details again, specifically also the enter-root scenarios. I have no safe feeling about it yet, partly because switch_mm fiddles with ipipe_active_mm as well, but more generally because we used to have such critical races in this area for a "fairly" long time.

When reentering root with ipipe_active_mm NULL, the TIF_MMSWTICH_INT bit is set, in which case the switch_mm preempted by Xenomai is restarted.

Right. But now we are also dealing with preemptions outside that loop. And we are dealing with archs that have atomic switch_mm, thus no such loops.

The goal should be to find a solution against the races outside switch_mm that is applicable to all archs. But the question is still unanswered to me if we can simply reuse existing ipipe_active_mm without disturbing its current use in non-atomic switch_mm.

Quoted 7 lines

Moreover, part of the problems on x86 was the non-atomic update of cpu_vm_mask vs. actually switching the hardware. How do you deal with cpu_vm_mask on ARM? Why can you call switch_mm without IRQ protection? My impression is that only the (costly) cpu_switch_mm should be allowed to be preemptible to have a safe state around it. I'm also asking as we would have to address this on x86 & co. once switch_mm could be called with undefined prev_mm.

when ipipe_active_mm is set to NULL, switch_mm does not touch the current mm cpu_vm_mask, since it does not know what the current mm is. However, we have a problem when switching back to root: we do not clear the previous mm cpu_vm_mask since we do not call switch_mm in xnarch_switch_to, we should fix this. So, I think we can extend the zone where ipipe_active_mm is set to NULL, to protect this area from the effects of preemption by Xenomai.

Quoted 3 lines

So, IMO, what we need is something like ipipe_mm_switch_protect which sets ipipe_active_mm to NULL on ARM, and results in local_irq_save_hw on x86.

That sounds like a good step forward. I will split up my patch into a non-arch part that instruments the code and an x86 part that installs the required protection for that arch.

Jan