Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions arch/arm/src/arm_a_r/arm_userfault.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ bool arm_fault_kill(uint32_t *regs)
ptcb != NULL ? get_task_name(ptcb) : "<gone>",
tcb->pid, get_task_name(tcb));

nxsched_put_tcb(ptcb);

atomic_or(&tcb->shared_flags, TCB_FLAG_FORCED_CANCEL);

regs[REG_PC] = (uint32_t)_exit;
Expand Down
2 changes: 2 additions & 0 deletions arch/arm64/src/common/arm64_fatal.c
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,8 @@ uint64_t *arm64_fatal_handler(uint64_t *regs)
tcb->pid, get_task_name(tcb));
_alert("Reason: %s - %s\n", reason, desc);

nxsched_put_tcb(ptcb);

atomic_or(&tcb->shared_flags, TCB_FLAG_FORCED_CANCEL);

regs[REG_ELR] = (uint64_t)_exit;
Expand Down
2 changes: 2 additions & 0 deletions arch/risc-v/src/common/riscv_exception.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ static void riscv_fault_handler(uintreg_t cause, void *regs)
_alert("Segmentation fault in %s (PID %d: %s)\n", get_task_name(ptcb),
tcb->pid, get_task_name(tcb));

nxsched_put_tcb(ptcb);

#ifdef CONFIG_SCHED_BACKTRACE
sched_dumpstack(tcb->pid);
#endif
Expand Down
4 changes: 3 additions & 1 deletion arch/risc-v/src/gd32vw55x/gdwifi/wrapper_nuttx.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,10 @@ char *sys_task_name_get(void *task)
#if CONFIG_TASK_NAME_SIZE > 0
struct tcb_s *tcb = nxsched_get_tcb(task == NULL ? nxsched_gettid() :
(pid_t)(uintptr_t)task - 1);
char *name = tcb ? tcb->name : NULL;

return tcb ? tcb->name : NULL;
nxsched_put_tcb(tcb);
return name;
#else
return NULL;
#endif
Expand Down
2 changes: 2 additions & 0 deletions arch/x86_64/src/intel64/intel64_irq.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ static int x86_64_fault_user_isr(int irq, void *c, void *arg)
ptcb != NULL ? get_task_name(ptcb) : "<gone>",
tcb->pid, get_task_name(tcb));

nxsched_put_tcb(ptcb);

/* Only a page fault sets CR2; for the others it is stale */

if (irq == ISR14)
Expand Down
2 changes: 1 addition & 1 deletion binfmt/binfmt_execmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ int exec_module(FAR struct binary_s *binp,

if (attr)
{
ret = spawn_execattrs(pid, attr);
ret = spawn_execattrs(tcb, attr);
if (ret < 0)
{
goto errout_with_tcbinit;
Expand Down
16 changes: 2 additions & 14 deletions binfmt/binfmt_loadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,15 @@

static int load_default_priority(FAR struct binary_s *bin)
{
struct sched_param param;
int ret;

/* Get the priority of this thread */

ret = nxsched_get_param(0, &param);
if (ret < 0)
{
berr("ERROR: nxsched_get_param failed: %d\n", ret);
return ret;
}

/* Save that as the priority of child thread */

bin->priority = param.sched_priority;
bin->priority = nxsched_self()->sched_priority;
if (bin->priority <= 0)
{
bin->priority = SCHED_PRIORITY_DEFAULT;
}

return ret;
return OK;
}

/****************************************************************************
Expand Down
3 changes: 3 additions & 0 deletions boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
# modifications.
#
# CONFIG_EXAMPLES_ELF_FSMOUNT is not set
# CONFIG_LIBC_INLINE_QUEUE is not set
# CONFIG_MM_INLINE_ADDFREECHUNK is not set
# CONFIG_NSH_DISABLE_SHUTDOWN is not set
# CONFIG_SCHED_INLINE_ADDPRIORITIZED is not set
# CONFIG_SPI_EXCHANGE is not set
CONFIG_ALLOW_BSD_COMPONENTS=y
CONFIG_ARCH="arm"
Expand Down
3 changes: 3 additions & 0 deletions boards/arm/stm32g4/b-g431b-esc1/configs/cansock/defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
# CONFIG_LIBC_INLINE_QUEUE is not set
# CONFIG_MM_INLINE_ADDFREECHUNK is not set
# CONFIG_NET_ETHERNET is not set
# CONFIG_NET_IPv4 is not set
# CONFIG_SCHED_INLINE_ADDPRIORITIZED is not set
# CONFIG_SYSTEM_DD is not set
# CONFIG_TLS_EMUTLS is not set
CONFIG_ALLOW_BSD_COMPONENTS=y
Expand Down
3 changes: 3 additions & 0 deletions boards/arm/stm32g4/nucleo-g431rb/configs/cansock/defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
# CONFIG_LIBC_INLINE_QUEUE is not set
# CONFIG_MM_INLINE_ADDFREECHUNK is not set
# CONFIG_NET_ETHERNET is not set
# CONFIG_NET_IPv4 is not set
# CONFIG_SCHED_INLINE_ADDPRIORITIZED is not set
# CONFIG_SYSTEM_DD is not set
# CONFIG_TLS_EMUTLS is not set
CONFIG_ALLOW_BSD_COMPONENTS=y
Expand Down
1 change: 1 addition & 0 deletions drivers/note/note_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 +2206,7 @@ FAR char *note_get_taskname(pid_t pid, FAR char *buf, size_t len)
if (tcb != NULL)
{
strlcpy(buf, tcb->name, len);
nxsched_put_tcb(tcb);
return buf;
}

Expand Down
2 changes: 1 addition & 1 deletion fs/inode/inode.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
# define FS_ADD_BACKTRACE(fd) \
do \
{ \
FAR struct tcb_s *tcb = nxsched_get_tcb(nxsched_gettid()); \
FAR struct tcb_s *tcb = this_task(); \
if (tcb != NULL && tcb->group != NULL && \
(tcb->group->tg_flags & GROUP_FLAG_FD_BACKTRACE) != 0) \
{ \
Expand Down
8 changes: 7 additions & 1 deletion fs/procfs/fs_procfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ static int procfs_readdir(FAR struct inode *mountpt,
*/

while (priv->index < priv->nentries &&
(nxsched_get_tcb(level0->pid[priv->index]) == NULL ||
(!nxsched_verify_pid(level0->pid[priv->index]) ||
nxsig_kill_target(level0->pid[priv->index]) < 0))
{
priv->index++;
Expand Down Expand Up @@ -1050,6 +1050,12 @@ static int procfs_readdir(FAR struct inode *mountpt,

pid_t pid = level0->pid[index];

if (!nxsched_verify_pid(pid))
{
ferr("ERROR: PID %d is no longer valid\n", pid);
return -ENOENT;
}

/* Save the filename=pid and file type=directory */

entry->d_type = DTYPE_DIRECTORY;
Expand Down
38 changes: 22 additions & 16 deletions fs/procfs/fs_procfsproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#include <nuttx/lib/lib.h>

#include "fs_heap.h"
#include "sched/sched.h"

#if !defined(CONFIG_SCHED_CPULOAD_NONE) || defined(CONFIG_SCHED_CRITMONITOR)
# include <nuttx/clock.h>
Expand Down Expand Up @@ -1421,6 +1422,7 @@ static ssize_t proc_groupfd(FAR struct proc_file_s *procfile, int count,
if (tcb == NULL || tcb->group == NULL)
{
leave_critical_section(flags);
nxsched_put_tcb(tcb);
break;
}

Expand All @@ -1433,6 +1435,7 @@ static ssize_t proc_groupfd(FAR struct proc_file_s *procfile, int count,
#endif

leave_critical_section(flags);
nxsched_put_tcb(tcb);
if (ret < 0)
{
continue;
Expand Down Expand Up @@ -1645,7 +1648,6 @@ static int proc_open(FAR struct file *filep, FAR const char *relpath,
{
FAR struct proc_file_s *procfile;
FAR const struct proc_node_s *node;
FAR struct tcb_s *tcb;
FAR char *ptr;
unsigned long tmp;
pid_t pid;
Expand Down Expand Up @@ -1692,8 +1694,7 @@ static int proc_open(FAR struct file *filep, FAR const char *relpath,

pid = (pid_t)tmp;

tcb = nxsched_get_tcb(pid);
if (tcb == NULL || nxsig_kill_target(pid) < 0)
if (nxsig_kill_target(pid) < 0)
{
ferr("ERROR: PID %d is no longer valid\n", pid);
return -ENOENT;
Expand Down Expand Up @@ -1863,6 +1864,8 @@ static ssize_t proc_read(FAR struct file *filep, FAR char *buffer,
ret = proc_groupfd(procfile, count, buffer, buflen, filep->f_pos);
}

nxsched_put_tcb(tcb);

/* Update the file offset */

if (ret > 0)
Expand Down Expand Up @@ -1914,6 +1917,7 @@ static ssize_t proc_write(FAR struct file *filep, FAR const char *buffer,
break;
}

nxsched_put_tcb(tcb);
return ret;
}

Expand Down Expand Up @@ -1969,7 +1973,6 @@ static int proc_opendir(FAR const char *relpath,
{
FAR struct proc_dir_s *procdir;
FAR const struct proc_node_s *node;
FAR struct tcb_s *tcb;
unsigned long tmp;
FAR char *ptr;
pid_t pid;
Expand Down Expand Up @@ -2020,8 +2023,7 @@ static int proc_opendir(FAR const char *relpath,

pid = (pid_t)tmp;

tcb = nxsched_get_tcb(pid);
if (tcb == NULL || nxsig_kill_target(pid) < 0)
if (nxsig_kill_target(pid) < 0)
{
ferr("ERROR: PID %d is not valid\n", pid);
return -ENOENT;
Expand Down Expand Up @@ -2111,7 +2113,6 @@ static int proc_readdir(FAR struct fs_dirent_s *dir,
{
FAR struct proc_dir_s *procdir;
FAR const struct proc_node_s *node = NULL;
FAR struct tcb_s *tcb;
unsigned int index;
pid_t pid;
int ret;
Expand Down Expand Up @@ -2139,9 +2140,7 @@ static int proc_readdir(FAR struct fs_dirent_s *dir,
/* Verify that the pid still refers to an active task/thread */

pid = procdir->pid;

tcb = nxsched_get_tcb(pid);
if (tcb == NULL)
if (!nxsched_verify_pid(pid))
{
ferr("ERROR: PID %d is no longer valid\n", pid);
return -ENOENT;
Expand Down Expand Up @@ -2213,10 +2212,12 @@ static int proc_rewinddir(struct fs_dirent_s *dir)
static int proc_stat(const char *relpath, struct stat *buf)
{
FAR const struct proc_node_s *node;
FAR struct tcb_s *tcb;
unsigned long tmp;
FAR char *ptr;
pid_t pid;
#ifdef CONFIG_SCHED_USER_IDENTITY
FAR struct tcb_s *tcb;
#endif

/* Two path forms are accepted:
*
Expand Down Expand Up @@ -2258,8 +2259,7 @@ static int proc_stat(const char *relpath, struct stat *buf)

pid = (pid_t)tmp;

tcb = nxsched_get_tcb(pid);
if (tcb == NULL || nxsig_kill_target(pid) < 0)
if (nxsig_kill_target(pid) < 0)
{
ferr("ERROR: PID %d is no longer valid\n", pid);
return -ENOENT;
Expand All @@ -2274,10 +2274,16 @@ static int proc_stat(const char *relpath, struct stat *buf)
* the task group.
*/

if (tcb->group != NULL)
tcb = nxsched_get_tcb(pid);
if (tcb != NULL)
{
buf->st_uid = tcb->group->tg_euid;
buf->st_gid = tcb->group->tg_egid;
if (tcb->group != NULL)
{
buf->st_uid = tcb->group->tg_euid;
buf->st_gid = tcb->group->tg_egid;
}

nxsched_put_tcb(tcb);
}
#endif

Expand Down
3 changes: 2 additions & 1 deletion fs/vfs/fs_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,14 @@ static void task_fssync(pid_t pid)
if (tcb == NULL || tcb->group == NULL ||
fd >= fdlist_count(&tcb->group->tg_fdlist))
{
nxsched_put_tcb(tcb);
leave_critical_section(flags);
break;
}

ret = fdlist_get(&tcb->group->tg_fdlist, fd, &filep);
nxsched_put_tcb(tcb);
leave_critical_section(flags);

if (ret >= 0)
{
file_fsync(filep);
Expand Down
35 changes: 35 additions & 0 deletions include/nuttx/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
#define TCB_FLAG_FORCED_CANCEL (1 << 6) /* Bit 6: Pthread cancel is forced */
#define TCB_FLAG_JOIN_COMPLETED (1 << 7) /* Bit 7: Pthread join completed */
#define TCB_FLAG_REAPED (1 << 8) /* Bit 8: Exit waited for by parent */
#define TCB_FLAG_KILL_PROCESSING (1 << 9) /* Bit 9: tcb is killed */

/* Values for struct task_group tg_flags */

Expand Down Expand Up @@ -695,6 +696,18 @@ struct tcb_s
cpu_set_t affinity; /* Bit set of permitted CPUs */
#endif
atomic_t shared_flags; /* Flags written by other tasks */

/* The total number that we are referenced by other tasks */

atomic_t refs;

/* The total number of references that we hold on other tasks */

atomic_t holds;

/* When we exit, we need post this sem. */

sem_t exit_sem;
int16_t lockcount; /* 0=preemptible (not-locked) */
#ifdef CONFIG_IRQCOUNT
int16_t irqcount; /* 0=Not in critical section */
Expand Down Expand Up @@ -1036,6 +1049,28 @@ void nxsched_foreach(nxsched_foreach_t handler, FAR void *arg);

FAR struct tcb_s *nxsched_get_tcb(pid_t pid);

/****************************************************************************
* Name: nxsched_verify_pid
*
* Description:
* Given a task ID, this function will check whether the TCB
* corresponding to the PID exists.
*
****************************************************************************/

bool nxsched_verify_pid(pid_t pid);

/****************************************************************************
* Name: nxsched_put_tcb
*
* Description:
* Drop a reference taken with nxsched_get_tcb(); each successful
* get must be paired with a put.
*
****************************************************************************/

void nxsched_put_tcb(FAR struct tcb_s *tcb);

/****************************************************************************
* Name: nxsched_releasepid
*
Expand Down
4 changes: 3 additions & 1 deletion include/spawn.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
#define POSIX_SPAWN_RESETIDS (1u << 0) /* 1: Reset effective user ID */
#define POSIX_SPAWN_SETPGROUP (1u << 1) /* 1: Set process group */
#define POSIX_SPAWN_SETSCHEDPARAM (1u << 2) /* 1: Set task's priority */
#define POSIX_SPAWN_SETSCHEDULER (1u << 3) /* 1: Set task's scheduler policy */
#if CONFIG_RR_INTERVAL > 0 || defined(CONFIG_SCHED_SPORADIC)
# define POSIX_SPAWN_SETSCHEDULER (1u << 3) /* 1: Set task's scheduler policy */
#endif
#define POSIX_SPAWN_SETSIGDEF (1u << 4) /* 1: Set default signal actions */
#define POSIX_SPAWN_SETSIGMASK (1u << 5) /* 1: Set sigmask */
#define POSIX_SPAWN_SETSID (1u << 7) /* 1: Create the new session(glibc specific) */
Expand Down
Loading