mirror of
https://github.com/xen-project/xen.git
synced 2026-10-28 02:01:24 +01:00
xen: move per-cpu area management into common code
Centralize per-cpu area management to reduce code duplication and enhance maintainability across architectures. The per-cpu area management code, which is largely common among architectures, is moved to a shared implementation in xen/common/percpu.c. This change includes: * Remove percpu.c from the X86 and Arm architectures. * For x86, define INVALID_PERCPU_AREAS and PARK_OFFLINE_CPUS_VAR. * Drop the declaration of __per_cpu_offset[] from stubs.c in PPC and RISC-V to facilitate the build of the common per-cpu code. No functional changes for x86. For Arm add support of CPU_RESUME_FAILED, CPU_REMOVE and freeing of percpu in the case when system_state != SYS_STATE_suspend, however, there is no change in behavior for Arm at this time. Move the asm-generic/percpu.h definitions to xen/percpu.h, except for __per_cpu_start[] and __per_cpu_data_end[], which are moved to common/percpu.c as they are only used in common/percpu.c. Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com> Reviewed-by: Jan Beulich <jbeulich@suse.com> Reviewed-by: Michal Orzel <michal.orzel@amd.com>
This commit is contained in:
1 parent
eb21ce14d7
commit
f75780d26b
14 files changed
+62
-140
No files matched your search
@@ -39,7 +39,6 @@ obj-$(CONFIG_MEM_ACCESS) += mem_access.o
|
||||
obj-y += mm.o
|
||||
obj-y += monitor.o
|
||||
obj-y += p2m.o
|
||||
obj-y += percpu.o
|
||||
obj-y += platform.o
|
||||
obj-y += platform_hypercall.o
|
||||
obj-y += physdev.o
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define __ASM_SMP_H
|
||||
|
||||
#ifndef __ASSEMBLY__
|
||||
#include <xen/percpu.h>
|
||||
#include <xen/cpumask.h>
|
||||
#include <asm/current.h>
|
||||
#endif
|
||||
@@ -12,12 +13,6 @@ extern unsigned long smp_up_cpu;
|
||||
DECLARE_PER_CPU(cpumask_var_t, cpu_sibling_mask);
|
||||
DECLARE_PER_CPU(cpumask_var_t, cpu_core_mask);
|
||||
|
||||
/*
|
||||
* Do we, for platform reasons, need to actually keep CPUs online when we
|
||||
* would otherwise prefer them to be off?
|
||||
*/
|
||||
#define park_offline_cpus false
|
||||
|
||||
extern void noreturn stop_cpu(void);
|
||||
|
||||
extern int arch_smp_init(void);
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
#include <xen/percpu.h>
|
||||
#include <xen/cpu.h>
|
||||
#include <xen/init.h>
|
||||
#include <xen/mm.h>
|
||||
#include <xen/rcupdate.h>
|
||||
|
||||
unsigned long __per_cpu_offset[NR_CPUS];
|
||||
#define INVALID_PERCPU_AREA (-(long)__per_cpu_start)
|
||||
#define PERCPU_ORDER (get_order_from_bytes(__per_cpu_data_end-__per_cpu_start))
|
||||
|
||||
void __init percpu_init_areas(void)
|
||||
{
|
||||
unsigned int cpu;
|
||||
for ( cpu = 1; cpu < NR_CPUS; cpu++ )
|
||||
__per_cpu_offset[cpu] = INVALID_PERCPU_AREA;
|
||||
}
|
||||
|
||||
static int init_percpu_area(unsigned int cpu)
|
||||
{
|
||||
char *p;
|
||||
if ( __per_cpu_offset[cpu] != INVALID_PERCPU_AREA )
|
||||
return -EBUSY;
|
||||
if ( (p = alloc_xenheap_pages(PERCPU_ORDER, 0)) == NULL )
|
||||
return -ENOMEM;
|
||||
memset(p, 0, __per_cpu_data_end - __per_cpu_start);
|
||||
__per_cpu_offset[cpu] = p - __per_cpu_start;
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct free_info {
|
||||
unsigned int cpu;
|
||||
struct rcu_head rcu;
|
||||
};
|
||||
static DEFINE_PER_CPU(struct free_info, free_info);
|
||||
|
||||
static void _free_percpu_area(struct rcu_head *head)
|
||||
{
|
||||
struct free_info *info = container_of(head, struct free_info, rcu);
|
||||
unsigned int cpu = info->cpu;
|
||||
char *p = __per_cpu_start + __per_cpu_offset[cpu];
|
||||
free_xenheap_pages(p, PERCPU_ORDER);
|
||||
__per_cpu_offset[cpu] = INVALID_PERCPU_AREA;
|
||||
}
|
||||
|
||||
static void free_percpu_area(unsigned int cpu)
|
||||
{
|
||||
struct free_info *info = &per_cpu(free_info, cpu);
|
||||
info->cpu = cpu;
|
||||
call_rcu(&info->rcu, _free_percpu_area);
|
||||
}
|
||||
|
||||
static int cpu_percpu_callback(
|
||||
struct notifier_block *nfb, unsigned long action, void *hcpu)
|
||||
{
|
||||
unsigned int cpu = (unsigned long)hcpu;
|
||||
int rc = 0;
|
||||
|
||||
switch ( action )
|
||||
{
|
||||
case CPU_UP_PREPARE:
|
||||
rc = init_percpu_area(cpu);
|
||||
break;
|
||||
case CPU_UP_CANCELED:
|
||||
case CPU_DEAD:
|
||||
free_percpu_area(cpu);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return notifier_from_errno(rc);
|
||||
}
|
||||
|
||||
static struct notifier_block cpu_percpu_nfb = {
|
||||
.notifier_call = cpu_percpu_callback,
|
||||
.priority = 100 /* highest priority */
|
||||
};
|
||||
|
||||
static int __init percpu_presmp_init(void)
|
||||
{
|
||||
register_cpu_notifier(&cpu_percpu_nfb);
|
||||
return 0;
|
||||
}
|
||||
presmp_initcall(percpu_presmp_init);
|
||||
@@ -7,10 +7,4 @@
|
||||
DECLARE_PER_CPU(cpumask_var_t, cpu_sibling_mask);
|
||||
DECLARE_PER_CPU(cpumask_var_t, cpu_core_mask);
|
||||
|
||||
/*
|
||||
* Do we, for platform reasons, need to actually keep CPUs online when we
|
||||
* would otherwise prefer them to be off?
|
||||
*/
|
||||
#define park_offline_cpus false
|
||||
|
||||
#endif
|
||||
@@ -141,7 +141,6 @@ void smp_send_state_dump(unsigned int cpu)
|
||||
/* domain.c */
|
||||
|
||||
DEFINE_PER_CPU(struct vcpu *, curr_vcpu);
|
||||
unsigned long __per_cpu_offset[NR_CPUS];
|
||||
|
||||
void context_switch(struct vcpu *prev, struct vcpu *next)
|
||||
{
|
||||
|
||||
@@ -10,12 +10,6 @@
|
||||
DECLARE_PER_CPU(cpumask_var_t, cpu_sibling_mask);
|
||||
DECLARE_PER_CPU(cpumask_var_t, cpu_core_mask);
|
||||
|
||||
/*
|
||||
* Do we, for platform reasons, need to actually keep CPUs online when we
|
||||
* would otherwise prefer them to be off?
|
||||
*/
|
||||
#define park_offline_cpus false
|
||||
|
||||
/*
|
||||
* Mapping between Xen logical cpu index and hartid.
|
||||
*/
|
||||
|
||||
@@ -133,7 +133,6 @@ void smp_send_state_dump(unsigned int cpu)
|
||||
/* domain.c */
|
||||
|
||||
DEFINE_PER_CPU(struct vcpu *, curr_vcpu);
|
||||
unsigned long __per_cpu_offset[NR_CPUS];
|
||||
|
||||
void context_switch(struct vcpu *prev, struct vcpu *next)
|
||||
{
|
||||
|
||||
@@ -54,7 +54,6 @@ obj-y += mpparse.o
|
||||
obj-y += nmi.o
|
||||
obj-y += numa.o
|
||||
obj-y += pci.o
|
||||
obj-y += percpu.o
|
||||
obj-y += physdev.o
|
||||
obj-$(CONFIG_COMPAT) += x86_64/physdev.o
|
||||
obj-$(CONFIG_X86_PSR) += psr.o
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
generic-y += div64.h
|
||||
generic-y += percpu.h
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef __X86_PERCPU_H__
|
||||
#define __X86_PERCPU_H__
|
||||
|
||||
#define PARK_OFFLINE_CPUS_VAR
|
||||
|
||||
/*
|
||||
* Force uses of per_cpu() with an invalid area to attempt to access the
|
||||
* middle of the non-canonical address space resulting in a #GP, rather than a
|
||||
* possible #PF at (NULL + a little) which has security implications in the
|
||||
* context of PV guests.
|
||||
*/
|
||||
#define INVALID_PERCPU_AREA (0x8000000000000000UL - (unsigned long)__per_cpu_start)
|
||||
|
||||
#endif /* __X86_PERCPU_H__ */
|
||||
@@ -31,6 +31,7 @@ obj-y += notifier.o
|
||||
obj-$(CONFIG_NUMA) += numa.o
|
||||
obj-y += page_alloc.o
|
||||
obj-y += pdx.o
|
||||
obj-y += percpu.o
|
||||
obj-$(CONFIG_PERF_COUNTERS) += perfc.o
|
||||
obj-bin-$(CONFIG_HAS_PMAP) += pmap.init.o
|
||||
obj-y += preempt.o
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <xen/percpu.h>
|
||||
#include <xen/cpu.h>
|
||||
#include <xen/init.h>
|
||||
#include <xen/mm.h>
|
||||
#include <xen/rcupdate.h>
|
||||
#include <xen/sections.h>
|
||||
|
||||
unsigned long __per_cpu_offset[NR_CPUS];
|
||||
#ifndef INVALID_PERCPU_AREA
|
||||
#define INVALID_PERCPU_AREA (-(long)__per_cpu_start)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Force uses of per_cpu() with an invalid area to attempt to access the
|
||||
* middle of the non-canonical address space resulting in a #GP, rather than a
|
||||
* possible #PF at (NULL + a little) which has security implications in the
|
||||
* context of PV guests.
|
||||
*/
|
||||
#define INVALID_PERCPU_AREA (0x8000000000000000UL - (unsigned long)__per_cpu_start)
|
||||
#define PERCPU_ORDER get_order_from_bytes(__per_cpu_data_end - __per_cpu_start)
|
||||
|
||||
extern char __per_cpu_start[];
|
||||
extern const char __per_cpu_data_end[];
|
||||
|
||||
unsigned long __read_mostly __per_cpu_offset[NR_CPUS];
|
||||
|
||||
void __init percpu_init_areas(void)
|
||||
{
|
||||
unsigned int cpu;
|
||||
@@ -28,7 +30,7 @@ static int init_percpu_area(unsigned int cpu)
|
||||
char *p;
|
||||
|
||||
if ( __per_cpu_offset[cpu] != INVALID_PERCPU_AREA )
|
||||
return 0;
|
||||
return park_offline_cpus ? 0 : -EBUSY;
|
||||
|
||||
if ( (p = alloc_xenheap_pages(PERCPU_ORDER, 0)) == NULL )
|
||||
return -ENOMEM;
|
||||
@@ -74,16 +76,21 @@ static int cf_check cpu_percpu_callback(
|
||||
case CPU_UP_PREPARE:
|
||||
rc = init_percpu_area(cpu);
|
||||
break;
|
||||
|
||||
case CPU_UP_CANCELED:
|
||||
case CPU_DEAD:
|
||||
case CPU_RESUME_FAILED:
|
||||
if ( !park_offline_cpus && system_state != SYS_STATE_suspend )
|
||||
free_percpu_area(cpu);
|
||||
break;
|
||||
|
||||
case CPU_REMOVE:
|
||||
if ( park_offline_cpus )
|
||||
free_percpu_area(cpu);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return notifier_from_errno(rc);
|
||||
@@ -2,29 +2,6 @@
|
||||
#ifndef __ASM_GENERIC_PERCPU_H__
|
||||
#define __ASM_GENERIC_PERCPU_H__
|
||||
|
||||
#ifndef __ASSEMBLY__
|
||||
|
||||
#include <xen/types.h>
|
||||
#include <asm/current.h>
|
||||
|
||||
extern char __per_cpu_start[];
|
||||
extern const char __per_cpu_data_end[];
|
||||
extern unsigned long __per_cpu_offset[NR_CPUS];
|
||||
void percpu_init_areas(void);
|
||||
|
||||
#define per_cpu(var, cpu) \
|
||||
(*RELOC_HIDE(&per_cpu__##var, __per_cpu_offset[cpu]))
|
||||
|
||||
#define this_cpu(var) \
|
||||
(*RELOC_HIDE(&per_cpu__##var, get_per_cpu_offset()))
|
||||
|
||||
#define per_cpu_ptr(var, cpu) \
|
||||
(*RELOC_HIDE(var, __per_cpu_offset[cpu]))
|
||||
#define this_cpu_ptr(var) \
|
||||
(*RELOC_HIDE(var, get_per_cpu_offset()))
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* __ASM_GENERIC_PERCPU_H__ */
|
||||
|
||||
/*
|
||||
|
||||
@@ -29,6 +29,36 @@
|
||||
|
||||
#include <asm/percpu.h>
|
||||
|
||||
#ifndef __ASSEMBLY__
|
||||
|
||||
#include <xen/types.h>
|
||||
#include <asm/current.h>
|
||||
|
||||
#ifndef PARK_OFFLINE_CPUS_VAR
|
||||
/*
|
||||
* Do we, for platform reasons, need to actually keep CPUs online when we
|
||||
* would otherwise prefer them to be off?
|
||||
*/
|
||||
#define park_offline_cpus false
|
||||
#endif
|
||||
|
||||
extern unsigned long __per_cpu_offset[];
|
||||
|
||||
#define per_cpu(var, cpu) \
|
||||
(*RELOC_HIDE(&per_cpu__##var, __per_cpu_offset[cpu]))
|
||||
|
||||
#define this_cpu(var) \
|
||||
(*RELOC_HIDE(&per_cpu__##var, get_per_cpu_offset()))
|
||||
|
||||
#define per_cpu_ptr(var, cpu) \
|
||||
(*RELOC_HIDE(var, __per_cpu_offset[cpu]))
|
||||
#define this_cpu_ptr(var) \
|
||||
(*RELOC_HIDE(var, get_per_cpu_offset()))
|
||||
|
||||
void percpu_init_areas(void);
|
||||
|
||||
#endif /* __ASSEMBLY__ */
|
||||
|
||||
/* Linux compatibility. */
|
||||
#define get_cpu_var(var) this_cpu(var)
|
||||
#define put_cpu_var(var)
|
||||
|
||||