From b3039b981ad29600306ab214b37b5a221df7ec3e Mon Sep 17 00:00:00 2001 From: Yanfeng Liu Date: Wed, 5 Jun 2024 08:34:42 +0800 Subject: [PATCH] sched/tls: drop ta_argv and g_idleargv - replaces `ta_argv` usage with `stackargs` - drops `ta_argv` from `task_info_s` - drops `g_idleargv` and checks idle in `group_argvstr` Signed-off-by: Yanfeng Liu --- include/nuttx/tls.h | 2 -- libs/libc/stdlib/lib_getprogname.c | 14 ++++++++++---- sched/group/group_argvstr.c | 9 ++++----- sched/init/nx_start.c | 25 +------------------------ sched/task/task_fork.c | 5 +++-- sched/task/task_setup.c | 7 ------- 6 files changed, 18 insertions(+), 44 deletions(-) diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h index 0b49476965eea..5529b09d84b4a 100644 --- a/include/nuttx/tls.h +++ b/include/nuttx/tls.h @@ -122,8 +122,6 @@ struct pthread_atfork_s struct task_info_s { mutex_t ta_lock; - int ta_argc; /* Number of arguments */ - FAR char **ta_argv; /* Name+start-up parameters */ #if CONFIG_TLS_TASK_NELEM > 0 uintptr_t ta_telem[CONFIG_TLS_TASK_NELEM]; /* Task local storage elements */ #endif diff --git a/libs/libc/stdlib/lib_getprogname.c b/libs/libc/stdlib/lib_getprogname.c index 6d1f541659ad4..eb244e2a24209 100644 --- a/libs/libc/stdlib/lib_getprogname.c +++ b/libs/libc/stdlib/lib_getprogname.c @@ -25,7 +25,7 @@ #include #include -#include +#include /**************************************************************************** * Public Functions @@ -33,12 +33,18 @@ /**************************************************************************** * Name: getprogname + * + * Note that previous impl returns address in the stack of main thread when + * used from a pthread, that is dangerous as main thread may end earlier. ****************************************************************************/ FAR const char *getprogname(void) { - FAR struct task_info_s *info; + struct stackinfo_s si; + FAR struct tls_info_s *ti = tls_get_info(); + uintptr_t ret = ti ? (uintptr_t)ti + ti->tl_size : 0; + int rc = nxsched_get_stackinfo(0, &si); - info = task_get_info(); - return info->ta_argv[0]; + return (rc >= 0 && ret && ((uintptr_t)si.stack_base_ptr) > ret) ? + ((FAR char **)ret)[0] : NULL; } diff --git a/sched/group/group_argvstr.c b/sched/group/group_argvstr.c index dbf483401f12b..ab40dc84bbeb8 100644 --- a/sched/group/group_argvstr.c +++ b/sched/group/group_argvstr.c @@ -63,12 +63,11 @@ size_t group_argvstr(FAR struct tcb_s *tcb, FAR char *args, size_t size) FAR struct addrenv_s *oldenv; #endif - /* Perform sanity checks */ + /* Sanity checks and idle tasks */ - if (!tcb || !tcb->group || !tcb->group->tg_info) + if (!tcb || !tcb->group || !tcb->group->tg_info || size < 1 || + is_idle_task(tcb)) { - /* Something is very wrong -> get out */ - *args = '\0'; return 0; } @@ -90,7 +89,7 @@ size_t group_argvstr(FAR struct tcb_s *tcb, FAR char *args, size_t size) else #endif { - FAR char **argv = tcb->group->tg_info->ta_argv + 1; + FAR char **argv = nxsched_get_stackargs(tcb) + 1; while (*argv != NULL && n < size) { diff --git a/sched/init/nx_start.c b/sched/init/nx_start.c index a38125229841c..8e6ae007b7424 100644 --- a/sched/init/nx_start.c +++ b/sched/init/nx_start.c @@ -209,21 +209,10 @@ static struct tcb_s g_idletcb[CONFIG_SMP_NCPUS]; /* This is the name of the idle task */ -#if CONFIG_TASK_NAME_SIZE <= 0 || !defined(CONFIG_SMP) -# ifdef CONFIG_SMP -static const char g_idlename[] = "CPU_Idle"; -# else +#if CONFIG_TASK_NAME_SIZE > 0 && !defined(CONFIG_SMP) static const char g_idlename[] = "Idle_Task"; -# endif #endif -/* This is IDLE threads argument list. NOTE: Normally the argument - * list is created on the stack prior to starting the task. We have to - * do things little differently here for the IDLE tasks. - */ - -static FAR char *g_idleargv[CONFIG_SMP_NCPUS][2]; - /**************************************************************************** * Private Functions ****************************************************************************/ @@ -419,17 +408,6 @@ static void idle_task_initialize(void) strlcpy(tcb->name, g_idlename, CONFIG_TASK_NAME_SIZE); # endif - /* Configure the task name in the argument list. The IDLE task does - * not really have an argument list, but this name is still useful - * for things like the NSH PS command. - * - * In the kernel mode build, the arguments are saved on the task's - * stack and there is no support that yet. - */ - - g_idleargv[i][0] = tcb->name; -#else - g_idleargv[i][0] = (FAR char *)g_idlename; #endif /* CONFIG_TASK_NAME_SIZE */ /* Then add the idle task's TCB to the head of the current ready to @@ -476,7 +454,6 @@ static void idle_group_initialize(void) DEBUGVERIFY( group_initialize((FAR struct task_tcb_s *)tcb, tcb->flags)); - tcb->group->tg_info->ta_argv = &g_idleargv[i][0]; /* Initialize the task join */ diff --git a/sched/task/task_fork.c b/sched/task/task_fork.c index 66854106c157c..51b61f93fb971 100644 --- a/sched/task/task_fork.c +++ b/sched/task/task_fork.c @@ -96,6 +96,7 @@ FAR struct task_tcb_s *nxtask_setup_fork(start_t retaddr) FAR struct tcb_s *ptcb = this_task(); FAR struct tcb_s *parent; FAR struct task_tcb_s *child; + FAR char **argv; size_t stack_size; uint8_t ttype; int priority; @@ -211,8 +212,8 @@ FAR struct task_tcb_s *nxtask_setup_fork(start_t retaddr) /* Setup to pass parameters to the new task */ - ret = nxtask_setup_arguments(child, parent->group->tg_info->ta_argv[0], - &parent->group->tg_info->ta_argv[1]); + argv = nxsched_get_stackargs(parent); + ret = nxtask_setup_arguments(child, argv[0], &argv[1]); if (ret < OK) { goto errout_with_tcb; diff --git a/sched/task/task_setup.c b/sched/task/task_setup.c index 5255f216c9fb8..f2723e1af340a 100644 --- a/sched/task/task_setup.c +++ b/sched/task/task_setup.c @@ -528,7 +528,6 @@ static int nxtask_setup_stackargs(FAR struct task_tcb_s *tcb, FAR const char *name, FAR char * const argv[]) { - const uint8_t ttype = tcb->cmn.flags & TCB_FLAG_TTYPE_MASK; FAR char **stackargv; FAR char *str; size_t strtablen; @@ -632,12 +631,6 @@ static int nxtask_setup_stackargs(FAR struct task_tcb_s *tcb, stackargv[argc + 1] = NULL; - if (ttype != TCB_FLAG_TTYPE_KERNEL) - { - tcb->cmn.group->tg_info->ta_argc = argc; - tcb->cmn.group->tg_info->ta_argv = stackargv; - } - return OK; }