Commit Graph

2240 Commits

Author SHA1 Message Date
Gregory Nutt
717bb04cb7 Increase the number of real time signals. Two is not enough.
Refer to issue #8867 for details and rational.

Convert sigset_t to an array type so that more than 32 signals can be supported.

Why not use a uin64_t?
- Using a uin32_t is more flexible if we decide to increase the number of signals beyound 64.
- 64-bit accesses are not atomic, at least not on 32-bit ARMv7-M and similar
- Keeping the base type as uint32_t does not introduce additional overhead due to padding to achieve 64-bit alignment of uin64_t
- Some architectures still supported by NuttX do not support uin64_t
  types,

Increased the number of signals to 64. This matches Linux. This will support all xsignals defined by Linux and also 32 real time signals (also like Linux).

This is is a work in progress; a draft PR that you are encouraged to comment on.
2023-03-27 16:59:04 +03:00
Petro Karashchenko
c7326717ca sched/paging: add FAR in paging worker
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-03-26 09:23:50 -06:00
Xiang Xiao
beb38917d1 signal: Replace NuttX special SIGWORK with SIGPAGING(SIGRTMIN)
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-03-26 08:31:36 -06:00
Xiang Xiao
5ce5d01d75 signal: Remove configurable assignment of signal numbers
please reference the issue here for more information:
https://github.com/apache/nuttx/issues/8898

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-03-26 08:31:36 -06:00
chenrun1
d60dee4a65 sched/clock_gettime:Remove output log
Calling syslog to print logs in clock_gettime will cause the system to have recursive output, i.e., clock_gettime->sinfo->syslog->clock_gettime, with the consequences of stack overflow or non-stop log output.
2023-03-25 01:43:01 +02:00
Petro Karashchenko
5651715486 signal: remove unused SIGCONDTIMEDOUT
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-03-23 17:17:25 -06:00
Zhe Weng
c9a38f42f7 sched/wqueue: Do as much work as possible in work_thread
Decouple the semcount and the work queue length.

Previous Problem:

If a work is queued and cancelled in high priority threads (or queued
by timer and cancelled by another high priority thread) before
work_thread runs, the queue operation will mark work_thread as ready to
run, but the cancel operation minus the semcount back to -1 and makes
wqueue->q empty. Then the work_thread still runs, found empty queue,
and wait sem again, then semcount becomes -2 (being minused by 1)

This can be done multiple times, then semcount can become very small
value. Test case to produce incorrect semcount:

high_priority_task()
{
  for (int i = 0; i < 10000; i++)
    {
      work_queue(LPWORK, &work, worker, NULL, 0);
      work_cancel(LPWORK, &work);
      usleep(1);
    }

  /* Now the g_lpwork.sem.semcount is a value near -10000 */
}

With incorrect semcount, any queue operation when the work_thread is
busy, will only increase semcount and push work into queue, but cannot
trigger work_thread (semcount is negative but work_thread is not
waiting), then there will be more and more works left in queue while
the work_thread is waiting sem and cannot call them.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2023-03-21 17:50:40 +02:00
Ville Juven
776e2d774f signal/sig_dispatch: Add signal action, if task is in system call
The _unmasked_ signal action was never added if the task is in system call
and waiting for (a different) signal.

This fixes deliver especially for default signal actions / unmaskable
signals, like SIGTERM.
2023-03-14 11:16:52 +09:00
Petro Karashchenko
4581a84064 nuttx: fix indentation issue
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-03-09 16:27:07 -03:00
Ville Juven
47ce89f757 Revert "signal/sig_dispatch: Signal action was not performed if TCB_FLAG_SYSCALL is set"
This reverts commit 0e44666828.
2023-03-08 10:13:26 +01:00
Ville Juven
3f529b4882 Revert "signal/sig_dispatch: Fix case where signal action is sent twice"
This reverts commit be0cb4cbe3.
2023-03-08 10:13:26 +01:00
wangbowen6
14ba7a7dd1 sched: build error fix in task_exithook
CC:  task/task_recover.c task/task_exithook.c: In function 'nxtask_exithook':
task/task_exithook.c:464:11: error: 'struct tcb_s' has no member named 'cmn'
  464 |   if ((tcb->cmn.flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_KERNEL)
      |           ^~
CC:  opus/silk/shell_coder.c make[1]: *** [Makefile:60: task_exithook.o] Error 1

Signed-off-by: wangbowen6 <wangbowen6@xiaomi.com>
2023-03-03 20:10:43 +08:00
Xiang Xiao
66c5f86018 sched: Disable stdio api by default when DEFAULT_SMALL equals y
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-03-03 00:18:03 +02:00
wangbowen6
51c30289ae sched: add critical section in nxsched_get_stateinfo
Signed-off-by: wangbowen6 <wangbowen6@xiaomi.com>
2023-03-02 01:12:53 +08:00
wangbowen6
d77b53957c sched/env: add tg_envc in task_group_s to avoid some loops in code
this commit optimize the env api speed

Signed-off-by: wangbowen6 <wangbowen6@xiaomi.com>
2023-02-25 23:10:37 +02:00
Junbo Zheng
468a0f0991 sched: fix kconfig warning
warning: SIG_ABRT (defined at sched/Kconfig:1481) has 'help' but empty help text

Reference: https://dsa.cs.tsinghua.edu.cn/oj/static/unix_signal.html

Signed-off-by: Junbo Zheng <zhengjunbo1@xiaomi.com>
2023-02-24 15:49:33 +08:00
lilei19
38f64f559d change strcpy to strlcpy
Signed-off-by: lilei19 <lilei19@xiaomi.com>
2023-02-24 12:15:40 +08:00
chenrun1
e6227e1943 include/signal.h:Expanding SIGNAL to be consistent with Linux 2023-02-24 04:07:10 +08:00
yinshengkai
29a7d267fb note: correct systime with perf count
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2023-02-23 22:30:07 +08:00
yinshengkai
55ce5d0511 note: remove SCHED_INSTRUMENTATION_HIRES
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2023-02-23 22:30:07 +08:00
Ville Juven
be0cb4cbe3 signal/sig_dispatch: Fix case where signal action is sent twice
As far as I can interpret how signal delivery should work when the signal
is blocked, it should still be sent to the pending queue even if the signal
is masked. When the sigmask changes it will be delivered.

The original implementation did not add the pending signal action, if
stcb->task_state == TSTATE_WAIT_SIG is true.

An attempt to patch this was made in #8563 but it is insufficient as it
creates an issue when the task is not waiting for a signal, but is in
syscall, in this case the signal is incorrectly queued twice.
2023-02-22 01:29:06 +08:00
Xiang Xiao
6b14ad9337 driver/note: Restore SCHED_INSTRUMENTATION config to sched/Konfig
since the chip/board vendor could disable dirvers/note and
provide the implementation of sched_note_xxx by self

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-02-20 14:30:59 +08:00
lilei19
77f6319268 sync ps/assert output 2023-02-17 23:35:44 +08:00
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