zephyr/kernel
Andy Ross 15c400774e kernel: Rework SMP irq_lock() compatibility layer
This was wrong in two ways, one subtle and one awful.

The subtle problem was that the IRQ lock isn't actually globally
recursive, it gets reset when you context switch (i.e. a _Swap()
implicitly releases and reacquires it).  So the recursive count I was
keeping needs to be per-thread or else we risk deadlock any time we
swap away from a thread holding the lock.

And because part of my brain apparently knew this, there was an
"optimization" in the code that tested the current count vs. zero
outside the lock, on the argument that if it was non-zero we must
already hold the lock.  Which would be true of a per-thread counter,
but NOT a global one: the other CPU may be holding that lock, and this
test will tell you *you* do.  The upshot is that a recursive
irq_lock() would almost always SUCCEED INCORRECTLY when there was lock
contention.  That this didn't break more things is amazing to me.

The rework is actually simpler than the original, thankfully.  Though
there are some further subtleties:

* The lock state implied by irq_lock() allows the lock to be
  implicitly released on context switch (i.e. you can _Swap() with the
  lock held at a recursion level higher than 1, which needs to allow
  other processes to run).  So return paths into threads from _Swap()
  and interrupt/exception exit need to check and restore the global
  lock state, spinning as needed.

* The idle loop design specifies a k_cpu_idle() function that is on
  common architectures expected to enable interrupts (for obvious
  reasons), but there is no place to put non-arch code to wire it into
  the global lock accounting.  So on SMP, even CPU0 needs to use the
  "dumb" spinning idle loop.

Finally this patch contains a simple bugfix too, found by inspection:
the interrupt return code used when CONFIG_SWITCH is enabled wasn't
correctly setting the active flag on the threads, opening up the
potential for a race that might result in a thread being scheduled on
two CPUs simultaneously.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2018-05-02 10:00:17 -07:00
..
include kernel: Rework SMP irq_lock() compatibility layer 2018-05-02 10:00:17 -07:00
CMakeLists.txt lib: posix: Move posix layer from 'kernel' to 'lib' 2018-04-05 16:43:05 -04:00
Kconfig arch: x86: Use retpolines in core assembly routines 2018-04-24 04:00:01 +05:30
Kconfig.event_logger kconfig: fix more help spacing issues 2018-02-15 23:20:55 -05:00
Kconfig.power_mgmt kconfig: fix more help spacing issues 2018-02-15 23:20:55 -05:00
alert.c
atomic_c.c
compiler_stack_protect.c kernel: add CODE_UNREACHABLE in _StackCheckHandler 2018-04-17 10:50:12 -07:00
device.c kernel: device: Only compare strings if pointer comparison fails 2018-02-15 17:31:59 -08:00
errno.c
idle.c kernel: Rework SMP irq_lock() compatibility layer 2018-05-02 10:00:17 -07:00
init.c kernel: Rework SMP irq_lock() compatibility layer 2018-05-02 10:00:17 -07:00
int_latency_bench.c
mailbox.c kernel: Clean up _unpend_thread() API 2018-04-24 03:57:20 +05:30
mem_domain.c kernel: mem_domain: Fix compile issues 2018-03-05 10:47:00 -06:00
mem_slab.c kernel: Clean up _unpend_thread() API 2018-04-24 03:57:20 +05:30
mempool.c kernel: Clean up _unpend_thread() API 2018-04-24 03:57:20 +05:30
msg_q.c kernel: Clean up _unpend_thread() API 2018-04-24 03:57:20 +05:30
mutex.c kernel: Clean up _unpend_thread() API 2018-04-24 03:57:20 +05:30
pipes.c kernel: Clean up _unpend_thread() API 2018-04-24 03:57:20 +05:30
poll.c kernel: Clean up _unpend_thread() API 2018-04-24 03:57:20 +05:30
queue.c kernel: Clean up _unpend_thread() API 2018-04-24 03:57:20 +05:30
sched.c kernel: Rework SMP irq_lock() compatibility layer 2018-05-02 10:00:17 -07:00
sem.c kernel: sem: Ensure that initial count is lesser or equal than limit 2018-04-24 04:04:36 +05:30
smp.c kernel: Rework SMP irq_lock() compatibility layer 2018-05-02 10:00:17 -07:00
stack.c kernel: Clean up _unpend_thread() API 2018-04-24 03:57:20 +05:30
sys_clock.c kernel: SMP timer integration 2018-02-16 10:44:29 -05:00
system_work_q.c
thread.c kernel: Clean up _unpend_thread() API 2018-04-24 03:57:20 +05:30
thread_abort.c kernel: Further unify _reschedule APIs 2018-04-24 03:57:20 +05:30
timer.c kernel: Clean up _unpend_thread() API 2018-04-24 03:57:20 +05:30
userspace.c scripts: gen_kobject_list: Generate enums and case statements 2018-04-26 02:57:12 +05:30
userspace_handler.c userspace: assign thread IDs at build time 2017-11-03 11:29:23 -07:00
version.c
work_q.c work_q: Correctly clear pending flag in delayed work queue, update docs 2018-02-13 18:08:57 -05:00