Skip to content
Open
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
7 changes: 7 additions & 0 deletions mm/mm_heap/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <nuttx/mutex.h>
#include <nuttx/sched.h>
#include <nuttx/spinlock.h>
#include <nuttx/fs/procfs.h>
#include <nuttx/lib/math32.h>
#include <nuttx/mm/mempool.h>
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions mm/mm_heap/mm_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/****************************************************************************
Expand All @@ -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
}
64 changes: 64 additions & 0 deletions mm/mm_heap/mm_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <string.h>

#include <nuttx/arch.h>
#include <nuttx/irq.h>
#include <nuttx/mm/mm.h>
#include <nuttx/mm/kasan.h>
#include <nuttx/sched.h>
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it's dangerous to free the memory blindly

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the better approach is export delay free api and call it in idle loop. please wait @huojianchaoyy upstream the patch which make delay free publicly, so you can fix this problem easier.

{
return mm_malloc(heap, size);
}
#endif

#ifdef CONFIG_DEBUG_MM
else if (MM_INTERNAL_HEAP(heap))
{
Expand Down
9 changes: 9 additions & 0 deletions sched/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions sched/init/nx_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@
* Pre-processor Definitions
****************************************************************************/

/* This set of all CPUs */

#define SCHED_ALL_CPUS ((1 << CONFIG_SMP_NCPUS) - 1)

/****************************************************************************
* Public Data
****************************************************************************/
Expand Down
8 changes: 8 additions & 0 deletions sched/pthread/pthread_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
4 changes: 4 additions & 0 deletions sched/sched/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
5 changes: 5 additions & 0 deletions sched/sched/sched_getaffinity.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
17 changes: 12 additions & 5 deletions sched/sched/sched_roundrobin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's define by DEFINE_PER_CPU_BSS_SMP https://github.com/apache/nuttx/pull/22261/changes#diff-c52fe2ff2c90e3d863728334401a22b53dc58457840d6242af67c5a62d381cbdR104, so the framework will layout all percpu in one continue block on each cpu for better cache utilization.

#endif

/****************************************************************************
Expand All @@ -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 ||
Expand Down Expand Up @@ -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))
{
Expand Down
17 changes: 16 additions & 1 deletion sched/sched/sched_setaffinity.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't real BMP, why not set affinity before create thread/task? which don't require to modify sched code.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sched change makes the binding enforced instead of a convention, the kernel refuses multi-CPU masks and moving a running task, so a missed affinity attribute cannot break it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you reuse SMP_DEFAULT_CPUSET==0 to trigger this special mode instead reusing BMP? BMP is better to reserve for the real bound multiple process.

@santhosh-c-c santhosh-c-c Nov 20, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BMP name follows QNX, which uses it for a single kernel instance with tasks bound by affinity [1]. One instance per core, as in #12020/#12032, is AMP in the same QNX terms, so I would prefer to keep BMP for this one.

[1] https://www.qnx.com/developers/docs/8.0/com.qnx.doc.neutrino.prog/topic/multicore.html

@xiaoxiang781216 xiaoxiang781216 Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you study #12020 or some AutoSAR implementation? the approach in this PR isn't the real BMP mode, so please don't reuse this name which just make confusion, and incorporate your special action into SMP_DEFAULT_CPUSET==0 is a good choice.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, let's clarify the term:
SMP: one code and data manage multiple core
AMP: different code and data manage single core
BMP: one code and different data manage single core

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying. Is there a reference for these definitions?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here: https://www.ghs.com/products/safety_critical/integrity_178_multicore.html
BTW, as far as I know, that tricore doesn't support the hardware cache coherence and then SMP can't work reliable on it. So, I’m curious how this PR manages to work reliably.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that page says the same: BMP is an enhanced and restricted form of SMP that statically binds an application's tasks to specific cores, with one kernel running across all cores (SMP). That is what this PR does.

On cache coherence: right, the TriCore data cache is not coherent, so with SMP the data cache is bypassed on all CPUs (DCON0.DCBYP, set in the boot code of every CPU). Kernel data is in the DLMU (uncached through the bypass) and stacks/heap in the DSPR (never cached), only the program cache stays on, which is safe for read-only flash code.

@xiaoxiang781216 xiaoxiang781216 Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that page says the same: BMP is an enhanced and restricted form of SMP that statically binds an application's tasks to specific cores, with one kernel running across all cores (SMP). That is what this PR does.

but it's still SMP with minor affinity change:

  1. No isolation between the cpu
  2. crash on one cpu impact other cpu
  3. the performance is poor than UP and BMP

On cache coherence: right, the TriCore data cache is not coherent, so with SMP the data cache is bypassed on all CPUs (DCON0.DCBYP, set in the boot code of every CPU). Kernel data is in the DLMU (uncached through the bypass) and stacks/heap in the DSPR (never cached), only the program cache stays on, which is safe for read-only flash code.

So, the performance isn't good, I would suggest you select other name for this feature and reserve BMP to the real implementation which:

  1. data is isolated between cpu
  2. one cpu crash doesn't impact other cpu
  3. data cache can be enabled without hardware cache coherence
  4. performance is same as UP and much better than SMP

and it's required for automative application.

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;
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions sched/task/task_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down