Commit Graph

277 Commits

Author SHA1 Message Date
zhangyuan21
ff6ba02378 sched/pthread: return ESRCH when thread not found at pthread_detach
https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_detach.html

If an implementation detects that the value specified by the thread argument
to pthread_detach() does not refer to a joinable thread, it is recommended
that the function should fail and report an [EINVAL] error.

If an implementation detects use of a thread ID after the end of its lifetime,
it is recommended that the function should fail and report an [ESRCH] error.

Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2023-05-12 01:06:23 +08:00
Xiang Xiao
325f395300 Replace all strncpy with strlcpy
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-05-08 09:57:01 +02:00
hujun5
9cd8ff5a0b pthread: sched_lock should replace with enter_critical_secion
sched_lock cannot lock threads from other CPUs, use enter_critical_section

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2023-05-06 23:12:20 +08:00
hujun5
d189a86a35 system: pthread_barrierwait should be moved to kernel space
The current implementation requires the use of enter_critical_section, so the source code needs to be moved to kernel space

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2023-04-25 15:34:40 +08:00
hujun5
0477fb116a pthread: pthread_cond_wait dead lock
pthread_cond_wait is preempted after releasing the lock, sched_lock cannot lock threads from other CPUs, use enter_critical_section

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2023-04-24 12:53:56 +03:00
yinshengkai
a40a802f9b sched/pthread: repalce sched_lock to enter_critical_section
After RR is enabled, an interrupt occurs during this period and the task cannot be switched

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2023-04-24 01:54:44 +08:00
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
5651715486 signal: remove unused SIGCONDTIMEDOUT
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-03-23 17:17:25 -06: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
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
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
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
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
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
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
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
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
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
Ville Juven
3be81e649f libc/stdio: Flush streams in userspace when process exits
This fixes a long standing issue, where the kernel does flushing of file
streams, although it should not handle such streams at all.
2022-12-22 20:16:11 +08:00
zhangyuan21
0113865bb0 sched/pthread: Delay pjoininfo allocation time
Create pjoininfo until pthread_exit/pthread_canel/pthread_join get called.

Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2022-12-20 10:26:02 +08:00
zhangyuan21
3d2ffed8ae Revert "pthread: Change the default name from <pthread> to <0xyyyyyyyy>"
This reverts commit 092d23b25d.

Signed-off-by: zhangyuan21 <zhangyuan21@xiaomi.com>
2022-12-17 14:53:59 +08:00
Marco Casaroli
7ee8d38fda Remove intr param from pthread_mutex_take 2022-12-08 03:13:44 +08:00
Marco Casaroli
4cc7f3747c Remove intr param from pthread_sem_take 2022-12-08 03:13:44 +08:00
Marco Casaroli
11cc232de5 pthread_mutex_timedlock never returns EINTR
According to posix spec, this function should never return `EINTR`.

This fixes the call to `pthread_mutex_take` so it keeps retrying the
lock and doesn't return `EINTR`
2022-12-07 21:08:23 +08:00
qinwei1
8021dfece6 sched/task/task_getpid: getpid should return process id not thread id
Summary:
   implement the right semantics:
1. getpid should return the main thread id
2. gettid should return the current thread id

Refer to:
 https://github.com/apache/incubator-nuttx/issues/2499
 https://github.com/apache/incubator-nuttx/pull/2518

Signed-off-by: qinwei1 <qinwei1@xiaomi.com>
2022-11-17 17:58:08 +08:00
Xiang Xiao
3f12b4f1e3 sched: Remove the unnecessary (FAR sem_t *) cast
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-10-29 21:12:40 +02:00
anjiahao
5724c6b2e4 sem:remove sem default protocl
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2022-10-22 14:50:48 +08:00
anjiahao
d1d46335df Replace nxsem API when used as a lock with nxmutex API
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-10-17 15:59:46 +09:00
Xiang Xiao
40ef5bc6db libc: Move queue.h from include to include/nuttx
to avoid the conflict with libuv's queue.h

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-09-26 08:04:58 +02:00
Juha Niskanen
fb852440af pthread: fix typo with CONFIG_PTHREAD_MUTEX_DEFAULT_PRIO_INHERIT
Signed-off-by: Juha Niskanen <juha.niskanen@haltian.com>
2022-09-05 15:36:23 +02:00
Xiang Xiao
2a981cec95 sched: Remove the unnecessary "FAR dq_queue_t *" cast
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-08-31 08:50:57 +09:00
chao.an
6e5908cc92 sched: fix visual studio Compiler Warning (level 4) C4189
D:\code\incubator-nuttx\sched\pthread\pthread_create.c(154,22):
  warning C4189: “pjoin”: local variable is initialized but not referenced
  [D:\code\incubator-nuttx\vs20221\sched\sched.vcxproj]

D:\code\incubator-nuttx\sched\group\group_setupidlefiles.c(61,28):
  warning C4189: “group”: local variable is initialized but not referenced
  [D:\code\incubator-nuttx\vs20221\sched\sched.vcxproj]

Reference:
https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4189?view=msvc-170

Signed-off-by: chao.an <anchao@xiaomi.com>
2022-08-17 10:00:20 +03:00
Gustavo Henrique Nihei
1416afc6b6 sched: Remove "0x" prefix preceding "%p" specifier on format string
The "p" format specifier already prepends the pointer address with
"0x" when printing.

Signed-off-by: Gustavo Henrique Nihei <gustavo.nihei@espressif.com>
2022-06-30 22:08:58 +03:00
Xiang Xiao
11c8f3125d sched/tls: Wrap up_info_size into tls_info_size
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-06-08 19:07:33 +09:00
Xiang Xiao
47b707bbf8 sched/tls: Add tls_init_info and tls_dup_info
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-06-03 23:56:50 +03:00
Xiang Xiao
3e32b605fe libc/tls: Make tls_get_info as the pulibc function instead up_tls_info
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-06-03 23:56:50 +03:00
Xiang Xiao
33a6aa2f48 sched/tls: Don't compensate the tls size to the stack size
it isn't good to change the stack size passed by caller

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-06-03 23:56:50 +03:00
Xiang Xiao
3b136771ff sched: Move the call of pthread_mutex_inconsistent to nxtask_recover
to avoid it spread around all possible exit path

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-05-27 09:39:09 +03:00
Xiang Xiao
feb54fc272 sched/pthread: Should call up_exit in pthread_exit
since _exit may kill all sibling thread when
HAVE_GROUP_MEMBERS equal true. Regressed by:
commit 622677d4a1
Author: Ville Juven <ville.juven@unikie.com>
Date:   Mon May 2 15:15:06 2022 +0300

    libc: Implement exit, atexit, on_exit and cxa_exit on the user side

    For CONFIG_BUILD_KERNEL using the sched/task/task_exithook implementation
    will just not work. It calls user code with kernel privileges which is
    a bit of a security issue.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-05-26 08:16:46 +03:00
jihandong
89338a6914 sched/pthread/pthread_condclockwait.c: follow POSIX.
pthread_condclockwait() can not distinguish between interrupt and timeout,
which cause these API not follow POSIX:
	pthread_rwlock_timedrdlock()
	pthread_rwlock_timedwrlock()
	pthread_condtimedwait()
POSIX:
	Upon return from the signal handler the thread resumes waiting for
	the condition variable as if it wasnot interrupted
	These functions shall not return an error code of [EINTR].

Replacing nxsem_wait() with nxsem_clockwait_uninterruptible() can solve it.

Signed-off-by: jihandong <jihandong@xiaomi.com>
2022-05-19 14:57:33 +08:00
zhuyanlin
1c977e97d2 pthread_mutexinit: fix deadcode in pthread_mutexinit
Signed-off-by: zhuyanlin <zhuyanlin1@xiaomi.com>
2022-05-16 10:20:52 +03:00
Xiang Xiao
08002a0a38 sched: Replace pthread_sem_take with nxsem_wait_uninterruptible
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-05-14 20:47:34 +03:00
anjiahao
a191d9bc3c sched/pthread_create:fix bug,delete data_sem
if a pthread set attr is detach,and when call pthread_create,
new thread exit quikly,new thread's tcb be free,then pthread_create
use new thread's tcb will crash.
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2022-04-14 01:42:16 +08:00
anjiahao
47304e7254 sched/pthread:need check pthread is DETACHED
pthread_join need check thread is DETACHED,
Whether to wait according to the result.And,
if a thread is DETACHED,it will not set a new
attr.
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2022-04-08 01:41:06 +08:00
Petro Karashchenko
68902d8732 pid_t: unify usage of special task IDs
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2022-03-22 21:22:32 +08:00
Huang Qi
f452dd7ab4 sched: Mark pthread as non-cancelable to avoid additional calls to pthread_exit()
Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
2022-02-20 15:36:07 +01:00
Oki Minabe
682984e9ba add the kernel stack allocation in pthread create. 2022-02-19 19:32:21 +08:00
Huang Qi
8bf7f94d39 pthread: Call cleanup callback while asyncrhonous cancel
Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
2022-02-09 21:21:43 +08:00