Commit Graph

2217 Commits

Author SHA1 Message Date
Ville Juven df1d7dd480 libc/exit: Purge calls to userspace API exit() from kernel
Remove calls to the userspace API exit() from the kernel. The problem
with doing such calls is that the exit functions are called with kernel
mode privileges which is a big security no-no.
2023-02-17 23:07:17 +08:00
Ville Juven 2cdba4a0a2 task/task_cancelpt: Kill the child if it is not in a cancel point
Do not allow a deferred cancellation if the group is exiting, it is too
dangerous to allow the threads to execute any user space code after the
exit has started.

If the cancelled thread is not inside a cancellation point, just kill it
immediately via asynchronous cancellation. This will create far less
problems than allowing it to continue running user code.
2023-02-17 22:57:36 +08:00
Ville Juven 0e44666828 signal/sig_dispatch: Signal action was not performed if TCB_FLAG_SYSCALL is set
For some reason the signal action was never performed if the receiveing
task was within a system call, the pending queue inser was simply missing.

This fixes the issue.
2023-02-17 22:54:55 +08:00
Ville Juven 905cba3ee3 group/tg_info/argv: Make utility function to read argv as string
This creates a generic and safe way to read a process argument vector
as string from any context.
2023-02-17 01:27:16 +08:00
Ville Juven 40800823c6 sched/nxtask_sigchild: Set exit code when CONFIG_SCHED_CHILD_STATUS=y
This is a follow-up to https://github.com/apache/nuttx/pull/8486
2023-02-14 10:27:46 +09:00
zhangyuan21 a20bc77010 sched/pthread: change the wrong type cast
Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2023-02-13 23:17:29 +08:00
zhangyuan21 01741a0b65 sched/semaphore: increase sem count when holder task exit
Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2023-02-12 16:49:45 +08:00
zhangyuan21 9017f70561 sched/pthread: check pthread group after find joininfo
Need check tcb or group info after find joininfo,
otherwise the wrong value will be returned.

Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2023-02-12 16:47:28 +08:00
Ville Juven 9638187192 sched/nxtask_sigchild: Set process exit code to group exit code
There is an issue where the wrong process exit code is given to the parent
when a process exits. This happens when the process has pthreads running
user code i.e. not within a cancel point / system call.

Why does this happen ?

When exit() is called, the following steps are done:
- group_kill_children(), which tells the children to die via pthread_cancel()

Then, one of two things can happen:
1. if the child is in a cancel point, it gets scheduled to allow it to leave
   the cancel point and gets destroyed immediately
2. if the child is not in a cancel point, a "cancel pending" flag is set and
   the child will die when the next cancel point is encountered

So what is the problem here?

The last thread alive dispatches SIGCHLD to the parent, which carries the
process's exit code. The group head has the only meaningful exit code and
this is what should be passed. However, in the second case, the group head
exits before the child, taking the process exit code to its grave. The child
that was alive will exit next and will pass its "status" to the parent process,
but this status is not the correct value to pass.

This commit fixes the issue by passing the group head's exit code ALWAYS to
the parent process.
2023-02-10 00:36:30 +08:00
Ville Juven f4b82b6405 sched/addrenv: Remove up_addrenv_restore
The function is not relevant any longer, remove it. Also remove
save_addrenv_t, the parameter taken by up_addrenv_restore.

Implement addrenv_select() / addrenv_restore() to handle the temporary
instantiation of address environments, e.g. when a process is being
created.
2023-02-08 02:51:23 +08:00
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
Ville Juven 5713d85df0 group/group_addrenv: Move address environment from group -> tcb
Detach the address environment handling from the group structure to the
tcb. This is preparation to fix rare cases where the system (MMU) is left
without a valid page directory, e.g. when a process exits.
2023-02-08 02:51:23 +08:00
chao an dff2192c75 misc/assert: remove const string from assert expression
Signed-off-by: chao an <anchao@xiaomi.com>
2023-02-02 15:08:47 +08:00
chao an 4c8d244fae sched/getpid: replace syscall getpid/tid/ppid() to kernel version
NuttX kernel should not use the syscall functions, especially after
enabling CONFIG_SCHED_INSTRUMENTATION_SYSCALL, all system functions
will be traced to backend, which will impact system performance.

Signed-off-by: chao an <anchao@xiaomi.com>
2023-02-02 10:33:01 +08:00
chao an c24dc389e4 sched/task: add kernel interface nxsched_* gettid/getpid/getppid
add new interface to implement getpid()/gettid()/getppid() for kernel version.

Signed-off-by: chao an <anchao@xiaomi.com>
2023-02-02 10:33:01 +08:00
Gustavo Henrique Nihei e6b204f438 nuttx: Use MIN/MAX definitions from "sys/param.h"
Signed-off-by: Gustavo Henrique Nihei <gustavo.nihei@espressif.com>
2023-02-01 23:47:44 +08:00
Ville Juven 8306e0d175 sched/group: Implement group_drop()
Implement a function for dropping references to the group structure and
finally freeing the allocated memory, if the group has been marked for
destruction
2023-02-01 09:49:09 -03:00
chao an cdfca12ab6 sched/waitpid: rename nx_waitpid() to nxsched_waitpid()
Signed-off-by: chao an <anchao@xiaomi.com>
2023-02-01 20:40:41 +08:00
Xiang Xiao 631a8da1e2 sched: Map SCHED_OTHER to SCHED_FIFO or SCHED_RR
this behaviour is explicitly specified here:
https://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_08.html
and map SCHED_NORMAL to SCHED_OTHER like Linux:
https://man7.org/linux/man-pages/man7/sched.7.html

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-01-30 03:01:03 +02:00
chao an 2ad0a02182 sched/pthread: set default pthread name to parent's name
Align ps behavior with linux:

linux:

  $ ps -e -T | grep hello
  3758130 3758130 pts/10   00:00:00 hello
  3758130 3758131 pts/10   00:00:00 hello

nuttx:

  nsh> ps
    PID GROUP PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK   STACK COMMAND
  ...
      4     4 100 FIFO     Task    --- Waiting  Signal    00000000 067504 hello
      5     4 100 FIFO     pthread --- Waiting  Signal    00000000 067536 hello 0x561cef0dfc53 0x1

Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-30 02:59:17 +02:00
chao an 45e4bc5f33 sched/semaphore: correct the return value of sem_post()
sem_post() should return EOVERFLOW if maximum allowable value for
a semaphore would be exceeded.

Reference:
https://man7.org/linux/man-pages/man3/sem_post.3.html

Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-28 21:45:10 +09:00
chao an b9d70365a1 sched/wqueue: semaphore count should be consistent with the number of work entries.
The number of work entries will be inconsistent with semaphore count
if the work is canceled, in extreme case, semaphore count will overflow
and fallback to 0 the workqueue will stop scheduling the enqueue work.

Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-28 21:45:10 +09:00
yintao beec3e4d80 fix run ltp_interfaces_sched_setscheduler_17_1 fail
The policy has changed when sched_setscheduler called invalid args.

Signed-off-by: yintao <yintao@xiaomi.com>
2023-01-28 09:10:23 +02:00
Xiang Xiao 69da13c86d sched/signal: Fix typo error in Kconfig(SIG_SEGA->SIG_SEGV)
Made by https://github.com/apache/nuttx/pull/8286

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-01-28 09:07:23 +02:00
yintao 739be67744 sched: Add SIGFPE, SIGILL and SIGSEGV definition
Signed-off-by: yintao <yintao@xiaomi.com>
2023-01-27 13:21:49 -03:00
Ville Juven 1c84fc9cd9 Revert "sched/release_tcb: Do not release stack for user processes here"
This reverts commit 1a67dc7c68.
2023-01-26 21:15:25 +08:00
Ville Juven 29eaf95ced Revert "sched/sched_releasetcb: Fix code style issue from prior patch"
This reverts commit 920956b20f.
2023-01-26 21:15:25 +08:00
Xiang Xiao 819fbd22cc sched: Implement tkill/tgkill
https://linux.die.net/man/2/tgkill

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-01-26 08:11:56 +02:00
Petro Karashchenko e5dbc3328c sched/misc/assert: remove extra indentations
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-01-26 10:26:09 +08:00
Xiang Xiao 1280a2a8f9 Launch the initial task through task_spawn instead of nxtask_create
to share the code with the code path of posix_spawn

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-01-25 23:34:52 +02:00
Xiang Xiao 99e89809d7 board: Pass the assertion expression to board_crashdump too
continue the follow work:
commit 43e7b13697
Author: Xiang Xiao <xiaoxiang@xiaomi.com>
Date:   Sun Jan 22 19:31:32 2023 +0800

    assert: Log the assertion expression in case of fail

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-01-25 16:00:48 -03:00
Petro Karashchenko 5f55290e41 sched/pthread: restore C89 compliance in pthread_create
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-01-26 01:26:11 +08:00
Xiang Xiao 43e7b13697 assert: Log the assertion expression in case of fail
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-01-24 15:00:19 -03:00
ligd 0a88799bab sched: fix task_delete crash in SMP case
A testcase as following:

child_task()
{
  sleep(3);
}

main_task()
{
  while (1)
    {
      ret = task_create("child_task", child_task, );
      sleep(1);

      task_delete(ret);
    }
}

Root casuse:
task_delete hasn's cover the condition that the deleted-task
is justing running on the other CPU.

Fix:
Let the nxsched_remove_readytorun() do the real work

Signed-off-by: ligd <liguiding1@xiaomi.com>
2023-01-18 14:11:18 +09:00
zhangyuan21 acd108a5ed pthread: fixed pthread_cancel and pthread_exit crash in SMP mode
1. When pthread exit, set the default cancellability state to NONCANCELABLE state.
2. Make sure modify tcb->flags is atomic operations.

Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2023-01-18 11:57:40 +08:00
zhangyuan21 9fd30c7f63 sched/pthread: check pthread group and flags before join
Prevent users from using the wrong pid and causing system crash.

Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2023-01-18 11:57:40 +08:00
Ville Juven 6a026382f0 group_leave: Don't instantiate address environment prior to destroying it
This is just unnecessary, a process cannot be destroyed by another
process in any case, every time this is executed the active address
environment is the process getting destroyed.

Even in the hypothetical case this was possible, the system would
crash at once if a context switch happens between "select()" and
"restore()", which is possible as the granule allocator is protected by
a semaphore (which is a synchronization point).
2023-01-18 11:02:19 +08:00
ligd c0aaaa94e1 assert: used %p to logout the pointer
Signed-off-by: ligd <liguiding1@xiaomi.com>
2023-01-17 09:46:40 -03:00
ligd 19f87e88b9 assert: fix build break.
misc/assert.c: In function 'show_tasks':
Error: misc/assert.c:411:10: error: format '%p' expects argument of type 'void *', but argument 3 has type 'uintptr_t' {aka 'long unsigned int'} [-Werror=format=]
  411 |   _alert("  ----   ---"
      |          ^~~~~~~~~~~~~~
......
  424 |          , up_get_intstackbase()
      |            ~~~~~~~~~~~~~~~~~~~~~
      |            |
      |            uintptr_t {aka long unsigned int}

Signed-off-by: ligd <liguiding1@xiaomi.com>
2023-01-17 09:46:40 -03:00
ligd c821088734 pthread: fix pthread exit error when set DETACHED
Signed-off-by: ligd <liguiding1@xiaomi.com>
2023-01-17 11:09:36 +08:00
ligd 940de60892 assert: add stackbase dump
Signed-off-by: ligd <liguiding1@xiaomi.com>
2023-01-17 11:04:12 +08:00
ligd 4de9317373 assert: add uname info to assert message
Signed-off-by: ligd <liguiding1@xiaomi.com>
2023-01-17 11:04:12 +08:00
Gustavo Henrique Nihei a3e253b4a3 mm: Enable a dedicated kernel heap on BUILD_FLAT via MM_KERNEL_HEAP
Signed-off-by: Gustavo Henrique Nihei <gustavo.nihei@espressif.com>
2023-01-17 10:30:00 +08:00
zhangyuan21 63039b80e1 sched/wqueue: do work_cancel when worker is not null
Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2023-01-16 13:37:00 +08:00
Jukka Laitinen a2a10c87e3 mm/shm: Switch to use process' common virtual memory region allocator
- Also remove the nuttx private shm.h file nuttx/mm/shm.h, which became redundant
- Also remove the gran allocator initialization/release in binfmt since common
  vpage allocator is initialized in group_create/group_leave

Signed-off-by: Jukka Laitinen <jukkax@ssrc.tii.ae>
2023-01-13 02:20:13 +08:00
Jukka Laitinen 2236facca9 mm/map: Add a common virtual memory region allocator
Signed-off-by: Jukka Laitinen <jukkax@ssrc.tii.ae>
2023-01-13 02:20:13 +08:00
Xiang Xiao ef65d443ad sem: Remove PRIOINHERIT_FLAGS_ENABLE and use SEM_PRIO_INHERIT instead
and refine the code to prepare the support of new flags

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-01-11 17:35:28 +02:00
chao an fdc3c44cc4 sched/group: fix task info heap-use-after-free
tg_info is still in use after task_uninit_info(), unifies
lib_stream_* with life cycle of task info to avoid this issue.

| ==1940861==ERROR: AddressSanitizer: heap-use-after-free on address 0xf47032e0 at pc 0x5676dc4f bp 0xf2f38c68 sp 0xf2f38c58
|
|#10 0xf7abec89 in __asan::__asan_report_load2 (addr=4100993760) at ../../../../src/libsanitizer/asan/asan_rtl.cpp:119
|#11 0x5677356a in nxsem_destroy (sem=0xf47032e0) at semaphore/sem_destroy.c:73
|#12 0x56773695 in sem_destroy (sem=0xf47032e0) at semaphore/sem_destroy.c:120
|#13 0x5676faa2 in nxmutex_destroy (mutex=0xf47032e0) at include/nuttx/mutex.h:126
|#14 0x567a3430 in lib_stream_release (group=0xf4901ba0) at stdio/lib_libstream.c:98
|#15 0x5676da75 in group_release (group=0xf4901ba0) at group/group_leave.c:162
|#16 0x5676e51c in group_leave (tcb=0xf5377740) at group/group_leave.c:360
|#17 0x569fe79b in nxtask_exithook (tcb=0xf5377740, status=0) at task/task_exithook.c:455
|#18 0x569f90b9 in _exit (status=0) at task/exit.c:82
|#19 0x56742680 in exit (status=0) at stdlib/lib_exit.c:61
|#20 0x56a69c78 in iperf_showusage (progname=0xf2f28838 "iperf", exitcode=0) at iperf_main.c:91
|#21 0x56a6a6ec in iperf_main (argc=1, argv=0xf2f28830) at iperf_main.c:140
|#22 0x5679c148 in nxtask_startup (entrypt=0x56a69c78 <iperf_main>, argc=1, argv=0xf2f28830) at sched/task_startup.c:70
|#23 0x56767f58 in nxtask_start () at task/task_start.c:134

Signed-off-by: chao an <anchao@xiaomi.com>
2023-01-11 01:53:59 +08:00
Jukka Laitinen 7f8bec7070 Add mm/mm_map virtual memory mapping list
The task_group specific list can be used to store information about
mmappings.

For a driver or filesystem performing mmap can also enable munmap by
adding an item to this list using mm_map_add(). The item is then
returned in the corresponding munmap call.

Signed-off-by: Jukka Laitinen <jukkax@ssrc.tii.ae>
2023-01-10 18:34:25 +08:00
anjiahao 9a32cf7a1a assert:add a last type to call notifier
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2023-01-05 22:58:00 +08:00