From 0b4be2db392682d7558e98046fc3e92b1027e15a Mon Sep 17 00:00:00 2001 From: Santhosh Charles Date: Wed, 18 Nov 2026 17:28:30 +0000 Subject: [PATCH 1/4] sched: check the mask of nxsched_get_affinity() Return -EINVAL for a NULL mask or a wrong size instead of writing through the pointer when assertions are off. Signed-off-by: Santhosh Charles --- sched/sched/sched_getaffinity.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sched/sched/sched_getaffinity.c b/sched/sched/sched_getaffinity.c index 7b973183baa81..6518acfb9afb1 100644 --- a/sched/sched/sched_getaffinity.c +++ b/sched/sched/sched_getaffinity.c @@ -76,6 +76,11 @@ int nxsched_get_affinity(pid_t pid, size_t cpusetsize, FAR cpu_set_t *mask) DEBUGASSERT(cpusetsize == sizeof(cpu_set_t) && mask != NULL); + if (mask == NULL || cpusetsize != sizeof(cpu_set_t)) + { + return -EINVAL; + } + /* Verify that the PID corresponds to a real task */ flags = enter_critical_section(); From ccd7eede5ea79ba6472bfffcb5442fee05ecefb7 Mon Sep 17 00:00:00 2001 From: Santhosh Charles Date: Wed, 18 Nov 2026 17:28:30 +0000 Subject: [PATCH 2/4] sched: roundrobin: use one SMP call per CPU All CPUs shared one smp_call_data_s. When the time slices of tasks on two CPUs ended in the same tick, the second init cleared the data while it was still queued for the first CPU, and the pid of the first call was lost. Keep one call per target CPU and do not queue it again until that CPU has handled it. Signed-off-by: Santhosh Charles --- sched/sched/sched_roundrobin.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/sched/sched/sched_roundrobin.c b/sched/sched/sched_roundrobin.c index 8400b40055772..1906ef3c1f15e 100644 --- a/sched/sched/sched_roundrobin.c +++ b/sched/sched/sched_roundrobin.c @@ -46,7 +46,8 @@ ****************************************************************************/ #ifdef CONFIG_SMP -static struct smp_call_data_s g_call_data; +static struct smp_call_data_s g_call_data[CONFIG_SMP_NCPUS]; +static volatile bool g_call_pending[CONFIG_SMP_NCPUS]; #endif /**************************************************************************** @@ -69,6 +70,7 @@ static int nxsched_roundrobin_handler(FAR void *cookie) irqstate_t flags; flags = enter_critical_section(); + g_call_pending[this_cpu()] = false; tcb = nxsched_get_tcb(pid); if (!tcb || tcb->task_state == TSTATE_TASK_INVALID || @@ -195,10 +197,15 @@ clock_t nxsched_process_roundrobin(FAR struct tcb_s *tcb, clock_t ticks, DEBUGASSERT(tcb->task_state == TSTATE_TASK_RUNNING); if (tcb->cpu != this_cpu()) { - nxsched_smp_call_init(&g_call_data, - nxsched_roundrobin_handler, - (FAR void *)(uintptr_t)tcb->pid); - nxsched_smp_call_single_async(tcb->cpu, &g_call_data); + if (!g_call_pending[tcb->cpu]) + { + g_call_pending[tcb->cpu] = true; + nxsched_smp_call_init(&g_call_data[tcb->cpu], + nxsched_roundrobin_handler, + (FAR void *)(uintptr_t)tcb->pid); + nxsched_smp_call_single_async(tcb->cpu, + &g_call_data[tcb->cpu]); + } } else if (nxsched_switch_running(tcb->cpu, true)) { From 86e85826cb173725d8793ebb223580ca982e5c30 Mon Sep 17 00:00:00 2001 From: Santhosh Charles Date: Wed, 18 Nov 2026 17:28:30 +0000 Subject: [PATCH 3/4] mm: heap: free the delay lists of all CPUs on SMP A thread that exits frees its own stack while it still runs on it, so the free goes to the delay list of its CPU. Only a malloc on that same CPU ever freed that list, so a CPU that never calls malloc kept the memory forever and the heap ran out (ENOMEM in ostest on a 64 KiB heap). When malloc fails, take the lists of every CPU and free them before giving up. A spinlock now guards the lists, since any CPU may empty them. Signed-off-by: Santhosh Charles --- mm/mm_heap/mm.h | 7 +++++ mm/mm_heap/mm_lock.c | 8 ++++++ mm/mm_heap/mm_malloc.c | 64 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/mm/mm_heap/mm.h b/mm/mm_heap/mm.h index 1753da304b463..f8d9daf9b6428 100644 --- a/mm/mm_heap/mm.h +++ b/mm/mm_heap/mm.h @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -258,6 +259,12 @@ struct mm_heap_s size_t mm_delaycount[CONFIG_SMP_NCPUS]; #endif +#ifdef CONFIG_SMP + /* Protects the delay lists, which any CPU may drain */ + + spinlock_t mm_delaylock; +#endif + /* The is a multiple mempool of the heap */ #ifdef CONFIG_MM_HEAP_MEMPOOL diff --git a/mm/mm_heap/mm_lock.c b/mm/mm_heap/mm_lock.c index 431b68ba92af9..56b55fbe52522 100644 --- a/mm/mm_heap/mm_lock.c +++ b/mm/mm_heap/mm_lock.c @@ -138,8 +138,12 @@ void mm_unlock(FAR struct mm_heap_s *heap) irqstate_t mm_lock_irq(FAR struct mm_heap_s *heap) { +#ifdef CONFIG_SMP + return spin_lock_irqsave(&heap->mm_delaylock); +#else UNUSED(heap); return up_irq_save(); +#endif } /**************************************************************************** @@ -152,6 +156,10 @@ irqstate_t mm_lock_irq(FAR struct mm_heap_s *heap) void mm_unlock_irq(FAR struct mm_heap_s *heap, irqstate_t state) { +#ifdef CONFIG_SMP + spin_unlock_irqrestore(&heap->mm_delaylock, state); +#else UNUSED(heap); up_irq_restore(state); +#endif } diff --git a/mm/mm_heap/mm_malloc.c b/mm/mm_heap/mm_malloc.c index a91b4e6bd3cbf..31407f5ac25da 100644 --- a/mm/mm_heap/mm_malloc.c +++ b/mm/mm_heap/mm_malloc.c @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -111,6 +112,62 @@ static bool free_delaylist(FAR struct mm_heap_s *heap, bool force) return ret; } +/**************************************************************************** + * Name: free_delaylist_all + * + * Description: + * Free the delay lists of all CPUs. A CPU frees the stack of an exiting + * thread while it still runs on it and holds the critical section until + * it has switched away, so the lists are taken inside the critical + * section. Returns true if there is memory freed. + * + ****************************************************************************/ + +#if defined(CONFIG_SMP) && (defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)) +static bool free_delaylist_all(FAR struct mm_heap_s *heap) +{ + FAR struct mm_delaynode_s *list[CONFIG_SMP_NCPUS]; + FAR struct mm_delaynode_s *tmp; + irqstate_t cflags; + irqstate_t flags; + bool ret = false; + int cpu; + + if (heap == NULL || up_interrupt_context()) + { + return false; + } + + cflags = enter_critical_section(); + flags = mm_lock_irq(heap); + + for (cpu = 0; cpu < CONFIG_SMP_NCPUS; cpu++) + { + list[cpu] = heap->mm_delaylist[cpu]; + heap->mm_delaylist[cpu] = NULL; +#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0 + heap->mm_delaycount[cpu] = 0; +#endif + } + + mm_unlock_irq(heap, flags); + leave_critical_section(cflags); + + for (cpu = 0; cpu < CONFIG_SMP_NCPUS; cpu++) + { + while (list[cpu] != NULL) + { + tmp = list[cpu]; + list[cpu] = tmp->flink; + mm_delayfree(heap, tmp, false); + ret = true; + } + } + + return ret; +} +#endif + #if CONFIG_MM_BACKTRACE >= 0 void mm_dump_handler(FAR struct tcb_s *tcb, FAR void *arg) { @@ -355,6 +412,13 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size) } #endif +#if defined(CONFIG_SMP) && (defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)) + else if (free_delaylist_all(heap)) + { + return mm_malloc(heap, size); + } +#endif + #ifdef CONFIG_DEBUG_MM else if (MM_INTERNAL_HEAP(heap)) { From 9f894f3b074307a04c26b5cceeab162fa33126a3 Mon Sep 17 00:00:00 2001 From: Santhosh Charles Date: Wed, 18 Nov 2026 17:28:30 +0000 Subject: [PATCH 4/4] sched: add BMP, bound multi processing on SMP With BMP one kernel runs on all CPUs, but every task stays on one CPU. A new task or thread runs on the CPU of its creator, or on the single CPU named in its attributes. An affinity must name exactly one CPU that exists, and a task that has started cannot move to another CPU. The scheduler never moves a task. nxsched_set_affinity() now also returns -EINVAL for a NULL mask, a wrong size or a mask without any existing CPU. SCHED_ALL_CPUS moves to sched.h so these checks can use it. Signed-off-by: Santhosh Charles --- sched/Kconfig | 9 +++++++++ sched/init/nx_start.c | 4 ---- sched/pthread/pthread_create.c | 8 ++++++++ sched/sched/sched.h | 4 ++++ sched/sched/sched_setaffinity.c | 17 ++++++++++++++++- sched/task/task_setup.c | 4 ++++ 6 files changed, 41 insertions(+), 5 deletions(-) diff --git a/sched/Kconfig b/sched/Kconfig index b2177ba02a662..cf9b95e76feac 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -454,6 +454,15 @@ config SMP_DEFAULT_CPUSET Set the Default CPU bits. The way to use the unset CPU is to call the sched_setaffinity function to bind a task to the CPU. bit0 means CPU0. +config BMP + bool "Bound multi-processing (BMP)" + default n + ---help--- + Bound multi-processing on top of SMP: a new task or thread runs on + the CPU of its creator, or on the single CPU named in its creation + attributes. An affinity mask must name exactly one CPU, and once + a task has started its CPU can no longer be changed. + endif # SMP choice diff --git a/sched/init/nx_start.c b/sched/init/nx_start.c index 3abae8bf71157..416cea35bc41e 100644 --- a/sched/init/nx_start.c +++ b/sched/init/nx_start.c @@ -68,10 +68,6 @@ * Pre-processor Definitions ****************************************************************************/ -/* This set of all CPUs */ - -#define SCHED_ALL_CPUS ((1 << CONFIG_SMP_NCPUS) - 1) - /**************************************************************************** * Public Data ****************************************************************************/ diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c index c06d3c104a23b..f0fe07a1a6453 100644 --- a/sched/pthread/pthread_create.c +++ b/sched/pthread/pthread_create.c @@ -226,6 +226,14 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread, return EINVAL; } +#ifdef CONFIG_BMP + if ((attr->affinity & ~SCHED_ALL_CPUS) != 0 || + (attr->affinity & (attr->affinity - 1)) != 0) + { + return EINVAL; + } +#endif + #ifdef CONFIG_TLS_ALIGNED /* The TLS alignment limits the size of the stack */ diff --git a/sched/sched/sched.h b/sched/sched/sched.h index 8a2e38ca9cb58..abd160a66391a 100644 --- a/sched/sched/sched.h +++ b/sched/sched/sched.h @@ -78,6 +78,10 @@ # define current_task(cpu) ((FAR struct tcb_s *)list_readytorun()->head) #endif +/* This set of all CPUs */ + +#define SCHED_ALL_CPUS ((cpu_set_t)((1 << CONFIG_SMP_NCPUS) - 1)) + /* This macro returns the running task which may different from this_task() * during interrupt level context switches. */ diff --git a/sched/sched/sched_setaffinity.c b/sched/sched/sched_setaffinity.c index a488914524733..908734be253ac 100644 --- a/sched/sched/sched_setaffinity.c +++ b/sched/sched/sched_setaffinity.c @@ -82,7 +82,13 @@ int nxsched_set_affinity(pid_t pid, size_t cpusetsize, /* Make sure that affinity mask is valid */ - if (*mask == 0) +#ifdef CONFIG_BMP + if (mask == NULL || cpusetsize != sizeof(cpu_set_t) || *mask == 0 || + (*mask & ~SCHED_ALL_CPUS) != 0 || (*mask & (*mask - 1)) != 0) +#else + if (mask == NULL || cpusetsize != sizeof(cpu_set_t) || *mask == 0 || + (*mask & SCHED_ALL_CPUS) == 0) +#endif { return -EINVAL; } @@ -115,6 +121,15 @@ int nxsched_set_affinity(pid_t pid, size_t cpusetsize, goto errout_with_csection; } +#ifdef CONFIG_BMP + if (tcb->task_state != TSTATE_TASK_INACTIVE && + *mask != ((cpu_set_t)1 << tcb->cpu)) + { + ret = -EINVAL; + goto errout_with_csection; + } +#endif + /* Set the new affinity mask. */ tcb->affinity = *mask; diff --git a/sched/task/task_setup.c b/sched/task/task_setup.c index 0203ad2ad4c79..c08a6684c037a 100644 --- a/sched/task/task_setup.c +++ b/sched/task/task_setup.c @@ -216,9 +216,13 @@ static int nxtask_assign_pid(FAR struct tcb_s *tcb) #ifdef CONFIG_SMP static inline void nxtask_inherit_affinity(FAR struct tcb_s *tcb) { +#ifdef CONFIG_BMP + tcb->affinity = (cpu_set_t)1 << this_cpu(); +#else FAR struct tcb_s *rtcb = this_task(); tcb->affinity = rtcb->affinity; +#endif } #else # define nxtask_inherit_affinity(tcb)