nuttx/sched/pthread
Ville Juven 09e7987121 sched/addrenv: Fix system crash when process group has been deleted
There is currently a big problem in the address environment handling which
is that the address environment is released too soon when the process is
exiting. The current MMU mappings will always be the exiting process's, which means
the system needs them AT LEAST until the next context switch happens. If
the next thread is a kernel thread, the address environment is needed for
longer.

Kernel threads "lend" the address environment of the previous user process.
This is beneficial in two ways:
- The kernel processes do not need an allocated address environment
- When a context switch happens from user -> kernel or kernel -> kernel,
  the TLB does not need to be flushed. This must be done only when
  changing to a different user address environment.

Another issue is when a new process is created; the address environment
of the new process must be temporarily instantiated by up_addrenv_select().
However, the system scheduler does not know that the process has a different
address environment to its own and when / if a context restore happens, the
wrong MMU page directory is restored and the process will either crash or
do something horribly wrong.

The following changes are needed to fix the issues:
- Add mm_curr which is the current address environment of the process
- Add a reference counter to safeguard the address environment
- Whenever an address environment is mapped to MMU, its reference counter
  is incremented
- Whenever and address environment is unmapped from MMU, its reference
  counter is decremented, and tested. If no more references -> drop the
  address environment and release the memory as well
- To limit the context switch delay, the address environment is freed in
  a separate low priority clean-up thread (LPWORK)
- When a process temporarily instantiates another process's address
  environment, the scheduler will now know of this and will restore the
  correct mappings to MMU

Why is this not causing more noticeable issues ? The problem only happens
under the aforementioned special conditions, and if a context switch or
IRQ occurs during this time.
2023-02-08 02:51:23 +08:00
..
Make.defs sched: Implement tkill/tgkill 2023-01-26 08:11:56 +02:00
pthread_cancel.c libc/stdio: Flush streams in userspace when process exits 2022-12-22 20:16:11 +08:00
pthread_completejoin.c pthread: fix pthread exit error when set DETACHED 2023-01-17 11:09:36 +08:00
pthread_condbroadcast.c sched: Remove the unnecessary (FAR sem_t *) cast 2022-10-29 21:12:40 +02:00
pthread_condclockwait.c sched/getpid: replace syscall getpid/tid/ppid() to kernel version 2023-02-02 10:33:01 +08:00
pthread_condsignal.c sched: Remove the unnecessary (FAR sem_t *) cast 2022-10-29 21:12:40 +02:00
pthread_condwait.c sched/getpid: replace syscall getpid/tid/ppid() to kernel version 2023-02-02 10:33:01 +08:00
pthread_create.c sched/addrenv: Fix system crash when process group has been deleted 2023-02-08 02:51:23 +08:00
pthread_detach.c Replace nxsem API when used as a lock with nxmutex API 2022-10-17 15:59:46 +09:00
pthread_exit.c sched/getpid: replace syscall getpid/tid/ppid() to kernel version 2023-02-02 10:33:01 +08:00
pthread_findjoininfo.c sched/pthread: check pthread group and flags before join 2023-01-18 11:57:40 +08:00
pthread_getaffinity.c
pthread_getschedparam.c sched: Remove "0x" prefix preceding "%p" specifier on format string 2022-06-30 22:08:58 +03:00
pthread_initialize.c Remove intr param from pthread_sem_take 2022-12-08 03:13:44 +08:00
pthread_join.c sched/pthread: check pthread group and flags before join 2023-01-18 11:57:40 +08:00
pthread_mutex.c Remove intr param from pthread_mutex_take 2022-12-08 03:13:44 +08:00
pthread_mutexconsistent.c sched: Remove the unnecessary (FAR sem_t *) cast 2022-10-29 21:12:40 +02:00
pthread_mutexdestroy.c sched: Remove the unnecessary (FAR sem_t *) cast 2022-10-29 21:12:40 +02:00
pthread_mutexinconsistent.c
pthread_mutexinit.c sched: Remove the unnecessary (FAR sem_t *) cast 2022-10-29 21:12:40 +02:00
pthread_mutextimedlock.c sched/getpid: replace syscall getpid/tid/ppid() to kernel version 2023-02-02 10:33:01 +08:00
pthread_mutextrylock.c sched/getpid: replace syscall getpid/tid/ppid() to kernel version 2023-02-02 10:33:01 +08:00
pthread_mutexunlock.c sched/getpid: replace syscall getpid/tid/ppid() to kernel version 2023-02-02 10:33:01 +08:00
pthread_release.c Replace nxsem API when used as a lock with nxmutex API 2022-10-17 15:59:46 +09:00
pthread_setaffinity.c
pthread_setschedparam.c sched: Remove "0x" prefix preceding "%p" specifier on format string 2022-06-30 22:08:58 +03:00
pthread_setschedprio.c
pthread_sigmask.c
pthread.h Remove intr param from pthread_mutex_take 2022-12-08 03:13:44 +08:00