[RFC Patch 5/7] x86: Introduce arch_cpu_physical_id() to obtain physical CPU ID

Cong Wang
2025-09-18T22:26:30Z

From: Cong Wang <cwang@multikernel.io>

The tranditional smp_processor_id() is a software-defined CPU ID which
is only unique within the same kernel. With Multikernel architecture, we
run multiple Linux kernels on different CPU's, hence the host kernel
needs a globally unique CPU ID to manage the CPU's. The physical CPU ID
is perfect for this case.

This API will be used to globally distinguish CPU's among different
multikernels.

Signed-off-by: Cong Wang <cwang@multikernel.io>
---
arch/x86/include/asm/smp.h | 6 ++++++
arch/x86/kernel/smp.c | 6 ++++++
kernel/multikernel.c | 9 +++++----
3 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/arch/x86/include/asm/smp.h b/arch/x86/include/asm/smp.h
index 1a59fd0de759..378be65ceafa 100644
--- a/arch/x86/include/asm/smp.h
+++ b/arch/x86/include/asm/smp.h
@@ -40,6 +40,7 @@ struct smp_ops {

void (*send_call_func_ipi)(const struct cpumask *mask);
void (*send_call_func_single_ipi)(int cpu);
+ int (*cpu_physical_id)(int cpu);
};

/* Globals due to paravirt */
@@ -100,6 +101,11 @@ static inline void arch_send_call_function_ipi_mask(const struct cpumask *mask)
smp_ops.send_call_func_ipi(mask);
}

+static inline int arch_cpu_physical_id(int cpu)
+{
+ return smp_ops.cpu_physical_id(cpu);
+}
+
void cpu_disable_common(void);
void native_smp_prepare_boot_cpu(void);
void smp_prepare_cpus_common(void);
diff --git a/arch/x86/kernel/smp.c b/arch/x86/kernel/smp.c
index 3ee515e32383..face9f80e05c 100644
--- a/arch/x86/kernel/smp.c
+++ b/arch/x86/kernel/smp.c
@@ -289,6 +289,11 @@ static int __init nonmi_ipi_setup(char *str)

__setup("nonmi_ipi", nonmi_ipi_setup);

+static int native_cpu_physical_id(int cpu)
+{
+ return cpu_physical_id(cpu);
+}
+
struct smp_ops smp_ops = {
.smp_prepare_boot_cpu = native_smp_prepare_boot_cpu,
.smp_prepare_cpus = native_smp_prepare_cpus,
@@ -306,6 +311,7 @@ struct smp_ops smp_ops = {

.send_call_func_ipi = native_send_call_func_ipi,
.send_call_func_single_ipi = native_send_call_func_single_ipi,
+ .cpu_physical_id = native_cpu_physical_id,
};
EXPORT_SYMBOL_GPL(smp_ops);

diff --git a/kernel/multikernel.c b/kernel/multikernel.c
index 74e2f84b7914..7f6f90485876 100644
--- a/kernel/multikernel.c
+++ b/kernel/multikernel.c
@@ -150,7 +150,7 @@ int multikernel_send_ipi_data(int cpu, void *data, size_t data_size, unsigned lo

/* Set header information */
target->data_size = data_size;
- target->sender_cpu = smp_processor_id();
+ target->sender_cpu = arch_cpu_physical_id(smp_processor_id());
target->type = type;

/* Copy the actual data into the buffer */
@@ -175,6 +175,7 @@ static void multikernel_interrupt_handler(void)
struct mk_ipi_data *data;
struct mk_ipi_handler *handler;
int current_cpu = smp_processor_id();
+ int current_physical_id = arch_cpu_physical_id(current_cpu);

/* Ensure shared memory is initialized */
if (!mk_shared_mem) {
@@ -183,10 +184,10 @@ static void multikernel_interrupt_handler(void)
}

/* Get this CPU's data area from shared memory */
- data = &mk_shared_mem->cpu_data[current_cpu];
+ data = &mk_shared_mem->cpu_data[current_physical_id];

- pr_debug("Multikernel IPI received on CPU %d from CPU %d, type=%u\n",
- current_cpu, data->sender_cpu, data->type);
+ pr_info("Multikernel IPI received on CPU %d (physical id %d) from CPU %d type=%u\n",
+ current_cpu, current_physical_id, data->sender_cpu, data->type);

raw_spin_lock(&mk_handlers_lock);
for (handler = mk_handlers; handler; handler = handler->next) {
--
2.34.1