Move percpu code to common
PATCH 2/2 xen: move per-cpu area management into common code
In reply to Oleksii KurochkoCentralize 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:
* Add arch_percpu_area_init_status() and arch_cpu_percpu_callback() functions to address architecture-specific variations.
* Remove percpu.c from the Arm architecture.
* For x86, percpu.c now only defines arch_percpu_area_init_status() and arch_cpu_percpu_callback(), and INVALID_PERCPU_AREA.
* 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.
--- xen/arch/arm/Makefile | 1 - xen/arch/arm/percpu.c | 85 -------------------- xen/arch/ppc/stubs.c | 1 - xen/arch/riscv/stubs.c | 1 - xen/arch/x86/include/asm/Makefile | 1 - xen/arch/x86/include/asm/percpu.h | 18 +++++ xen/arch/x86/percpu.c | 92 ++-------------------- xen/common/Makefile | 1 + xen/common/percpu.c | 127 ++++++++++++++++++++++++++++++ xen/include/asm-generic/percpu.h | 8 ++ 10 files changed, 159 insertions(+), 176 deletions(-) delete mode 100644 xen/arch/arm/percpu.c create mode 100644 xen/arch/x86/include/asm/percpu.h create mode 100644 xen/common/percpu.c
xen/arch/arm/Makefile +0 -1
@@ -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
xen/arch/arm/percpu.c deleted +0 -85
85 lines not on this page · raw message
xen/arch/ppc/stubs.c +0 -1
@@ -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) {
xen/arch/riscv/stubs.c +0 -1
@@ -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) {
xen/arch/x86/include/asm/Makefile +0 -1
@@ -1,3 +1,2 @@ # SPDX-License-Identifier: GPL-2.0-only generic-y += div64.h -generic-y += percpu.h
xen/arch/x86/include/asm/percpu.h new file +18 -0
@@ -0,0 +1,18 @@ +#ifndef __X86_PERCPU_H__ +#define __X86_PERCPU_H__ + +#include <asm-generic/percpu.h> + +/* +* 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 ARCH_PERCPU_AREA_CHECK + +#define ARCH_CPU_PERCPU_CALLBACK + +#endif /* __X86_PERCPU_H__ */
xen/arch/x86/percpu.c +5 -87
111 lines not on this page · raw message
xen/common/Makefile +1 -0
@@ -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
xen/common/percpu.c new file +127 -0
127 lines not on this page · raw message
xen/include/asm-generic/percpu.h +8 -0
@@ -12,6 +12,14 @@ extern const char __per_cpu_data_end[];extern unsigned long __per_cpu_offset[NR_CPUS ];void percpu_init_areas (void ); +void free_percpu_area (unsigned int cpu); + +int arch_percpu_area_init_status (void ); + +struct notifier_block ; +int arch_cpu_percpu_callback (struct notifier_block *nfb, +unsigned long action,void *hcpu); +#define per_cpu (var, cpu) \ (*RELOC_HIDE(&per_cpu__##var, __per_cpu_offset[cpu]))
Signature · 1 line
2.46.0
cover Move percpu code to common
The per-CPU area management code, which is largely common across architectures, is a strong candidate for consolidation into common code.
Currently, there are three architectural-specific aspects that are not yet unified ( if they can be unified at all ):
1. INVALID_PERCPU_AREA:
ARM:
#define INVALID_PERCPU_AREA (-(long)__per_cpu_start)
x86:
#define INVALID_PERCPU_AREA (0x8000000000000000UL - (unsigned long)__per_cpu_start)
2. Return value when __per_cpu_offset Is Already Initialized:
Arm:
if (__per_cpu_offset[cpu] != INVALID_PERCPU_AREA)
return -EBUSY;
x86:
if (__per_cpu_offset[cpu] != INVALID_PERCPU_AREA)
return 0;
3. Handling of CPU_RESUME_FAILED and CPU_REMOVE in cpu_percpu_callback:
Arm:
switch (action)
{
case CPU_UP_CANCELED:
case CPU_DEAD:
free_percpu_area(cpu);
break;
}
x86:
switch (action)
{
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;
}These changes do not affect the functional behavior.
Oleksii Kurochko (2):
xen: introduce common macros for per-CPU sections defintion
xen: move per-cpu area management into common code
xen/arch/arm/Makefile | 1 - xen/arch/arm/percpu.c | 85 -------------------- xen/arch/arm/xen.lds.S | 9 +-- xen/arch/ppc/stubs.c | 1 - xen/arch/ppc/xen.lds.S | 9 +-- xen/arch/riscv/stubs.c | 1 - xen/arch/x86/include/asm/Makefile | 1 - xen/arch/x86/include/asm/percpu.h | 18 +++++ xen/arch/x86/percpu.c | 92 ++-------------------- xen/arch/x86/xen.lds.S | 9 +-- xen/common/Makefile | 1 + xen/common/percpu.c | 127 ++++++++++++++++++++++++++++++ xen/include/asm-generic/percpu.h | 8 ++ xen/include/xen/xen.lds.h | 10 +++ 14 files changed, 172 insertions(+), 200 deletions(-) delete mode 100644 xen/arch/arm/percpu.c create mode 100644 xen/arch/x86/include/asm/percpu.h create mode 100644 xen/common/percpu.c
Signature · 1 line
2.46.0
PATCH 1/2 xen: introduce common macros for per-CPU sections defintion
Introduce PERCPU_SECTION macro which manages:
* Alignment of the section start
* Insertion of per-CPU data sections
* Alignment and start/end markers for per-CPU data
This change simplifies the linker script maintenance and ensures a unified approach for per-CPU sections across different architectures.
Refactor the linker scripts for Arm, PPC, and x86 architectures by using the common macro PERCPU_SECTION defined in xen/xen.lds.h to handle per-CPU data sections.
No functional changes.
--- xen/arch/arm/xen.lds.S | 9 +-------- xen/arch/ppc/xen.lds.S | 9 +-------- xen/arch/x86/xen.lds.S | 9 +-------- xen/include/xen/xen.lds.h | 10 ++++++++++ 4 files changed, 13 insertions(+), 24 deletions(-)
xen/arch/arm/xen.lds.S +1 -8
@@ -198,14 +198,7 @@ SECTIONS __bss_start = .; *(.bss.stack_aligned) *(.bss.page_aligned) - . = ALIGN(PAGE_SIZE); - __per_cpu_start = .; - *(.bss.percpu.page_aligned) - *(.bss.percpu) - . = ALIGN(SMP_CACHE_BYTES); - *(.bss.percpu.read_mostly) - . = ALIGN(SMP_CACHE_BYTES); - __per_cpu_data_end = .; + PERCPU_SECTION *(.bss .bss.*) . = ALIGN(POINTER_ALIGN); __bss_end = .;
xen/arch/ppc/xen.lds.S +1 -8
@@ -148,14 +148,7 @@ SECTIONS __bss_start = .; *(.bss.stack_aligned) *(.bss.page_aligned) - . = ALIGN(PAGE_SIZE); - __per_cpu_start = .; - *(.bss.percpu.page_aligned) - *(.bss.percpu) - . = ALIGN(SMP_CACHE_BYTES); - *(.bss.percpu.read_mostly) - . = ALIGN(SMP_CACHE_BYTES); - __per_cpu_data_end = .; + PERCPU_SECTION *(.bss .bss.*) . = ALIGN(POINTER_ALIGN); __bss_end = .;
xen/arch/x86/xen.lds.S +1 -8
@@ -321,14 +321,7 @@ SECTIONS DECL_SECTION(.bss) { __bss_start = .; *(.bss.page_aligned*) - . = ALIGN(PAGE_SIZE); - __per_cpu_start = .; - *(.bss.percpu.page_aligned) - *(.bss.percpu) - . = ALIGN(SMP_CACHE_BYTES); - *(.bss.percpu.read_mostly) - . = ALIGN(SMP_CACHE_BYTES); - __per_cpu_data_end = .; + PERCPU_SECTION *(.bss .bss.*) . = ALIGN(POINTER_ALIGN); __bss_end = .;
xen/include/xen/xen.lds.h +10 -0
@@ -151,6 +151,16 @@#define LOCK_PROFILE_DATA #endif +#define PERCPU_SECTION \ + . = ALIGN(PAGE_SIZE); \ + __per_cpu_start = .; \ + *(.bss.percpu.page_aligned) \ + *(.bss.percpu) \ + . = ALIGN(SMP_CACHE_BYTES); \ + *(.bss.percpu.read_mostly) \ + . = ALIGN(SMP_CACHE_BYTES); \ + __per_cpu_data_end = .; \ +#ifdef CONFIG_HAS_VPCI #define VPCI_ARRAY \ . = ALIGN(POINTER_ALIGN); \
Signature · 1 line
2.46.0
Re: [PATCH v1 1/2] xen: introduce common macros for per-CPU sections defintion
Hi,
Quoted 7 lines · Oleksii Kurochko
Introduce
PERCPU_SECTIONmacro which manages:
* Alignment of the section start
* Insertion of per-CPU data sections
* Alignment and start/end markers for per-CPU data
This change simplifies the linker script maintenance and ensures a unified approach for per-CPU sections across different architectures.
Quoted 7 lines
Refactor the linker scripts for Arm, PPC, and x86 architectures by using the common macro
PERCPU_SECTIONdefined inxen/xen.lds.hto handle per-CPU data sections.No functional changes.
Signed-off-by Oleksii Kurochko <[email protected]>
Cheers,
Quoted 88 lines
--- xen/arch/arm/xen.lds.S | 9 +-------- xen/arch/ppc/xen.lds.S | 9 +-------- xen/arch/x86/xen.lds.S | 9 +-------- xen/include/xen/xen.lds.h | 10 ++++++++++ 4 files changed, 13 insertions(+), 24 deletions(-)xen/arch/arm/xen.lds.S +1
-8@@ -198,14 +198,7 @@ SECTIONS __bss_start = .; *(.bss.stack_aligned) *(.bss.page_aligned) - . = ALIGN(PAGE_SIZE); - __per_cpu_start = .; - *(.bss.percpu.page_aligned) - *(.bss.percpu) - . = ALIGN(SMP_CACHE_BYTES); - *(.bss.percpu.read_mostly) - . = ALIGN(SMP_CACHE_BYTES); - __per_cpu_data_end = .; + PERCPU_SECTION *(.bss .bss.*) . = ALIGN(POINTER_ALIGN); __bss_end = .;xen/arch/ppc/xen.lds.S +1
-8@@ -148,14 +148,7 @@ SECTIONS __bss_start = .; *(.bss.stack_aligned) *(.bss.page_aligned) - . = ALIGN(PAGE_SIZE); - __per_cpu_start = .; - *(.bss.percpu.page_aligned) - *(.bss.percpu) - . = ALIGN(SMP_CACHE_BYTES); - *(.bss.percpu.read_mostly) - . = ALIGN(SMP_CACHE_BYTES); - __per_cpu_data_end = .; + PERCPU_SECTION *(.bss .bss.*) . = ALIGN(POINTER_ALIGN); __bss_end = .;xen/arch/x86/xen.lds.S +1
-8@@ -321,14 +321,7 @@ SECTIONS DECL_SECTION(.bss) { __bss_start = .; *(.bss.page_aligned*) - . = ALIGN(PAGE_SIZE); - __per_cpu_start = .; - *(.bss.percpu.page_aligned) - *(.bss.percpu) - . = ALIGN(SMP_CACHE_BYTES); - *(.bss.percpu.read_mostly) - . = ALIGN(SMP_CACHE_BYTES); - __per_cpu_data_end = .; + PERCPU_SECTION *(.bss .bss.*) . = ALIGN(POINTER_ALIGN); __bss_end = .;xen/include/xen/xen.lds.h +10
-0@@ -151,6 +151,16 @@#define LOCK_PROFILE_DATA #endif +#define PERCPU_SECTION \ + . = ALIGN(PAGE_SIZE); \ + __per_cpu_start = .; \ + *(.bss.percpu.page_aligned) \ + *(.bss.percpu) \ + . = ALIGN(SMP_CACHE_BYTES); \ + *(.bss.percpu.read_mostly) \ + . = ALIGN(SMP_CACHE_BYTES); \ + __per_cpu_data_end = .; \ +#ifdef CONFIG_HAS_VPCI #define VPCI_ARRAY \ . = ALIGN(POINTER_ALIGN); \
Signature · 1 line
Julien Grall
Re: [PATCH v1 2/2] xen: move per-cpu area management into common code
In reply to Oleksii KurochkoHi,
Quoted 216 lines · Oleksii Kurochko
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:
* Addarch_percpu_area_init_status()andarch_cpu_percpu_callback()functions to address architecture-specific variations.
* Removepercpu.cfrom the Arm architecture.
* For x86,percpu.cnow only definesarch_percpu_area_init_status()andarch_cpu_percpu_callback(), andINVALID_PERCPU_AREA.
* Drop the declaration of __per_cpu_offset[] fromstubs.cin PPC and RISC-V to facilitate the build of the common per-cpu code.No functional changes.
Signed-off-by Oleksii Kurochko <[email protected]>--- xen/arch/arm/Makefile | 1 - xen/arch/arm/percpu.c | 85 -------------------- xen/arch/ppc/stubs.c | 1 - xen/arch/riscv/stubs.c | 1 - xen/arch/x86/include/asm/Makefile | 1 - xen/arch/x86/include/asm/percpu.h | 18 +++++ xen/arch/x86/percpu.c | 92 ++-------------------- xen/common/Makefile | 1 + xen/common/percpu.c | 127 ++++++++++++++++++++++++++++++ xen/include/asm-generic/percpu.h | 8 ++ 10 files changed, 159 insertions(+), 176 deletions(-)delete mode 100644
xen/arch/arm/percpu.ccreate mode 100644xen/arch/x86/include/asm/percpu.hcreate mode 100644xen/common/percpu.cxen/arch/arm/Makefile +0
-1@@ -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.oxen/arch/arm/percpu.c deleted +0
-85@@ -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);xen/arch/ppc/stubs.c +0
-1@@ -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) {xen/arch/riscv/stubs.c +0
-1@@ -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) {xen/arch/x86/include/asm/Makefile +0
-1@@ -1,3 +1,2 @@ # SPDX-License-Identifier: GPL-2.0-only generic-y += div64.h -generic-y += percpu.hxen/arch/x86/include/asm/percpu.h new file +18
-0@@ -0,0 +1,18 @@ +#ifndef __X86_PERCPU_H__ +#define __X86_PERCPU_H__ + +#include <asm-generic/percpu.h> + +/* +* 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 ARCH_PERCPU_AREA_CHECK + +#define ARCH_CPU_PERCPU_CALLBACK + +#endif /* __X86_PERCPU_H__ */ xen/arch/x86/percpu.c +0
-18@@ -1,79 +1,19 @@ -#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 ]; - -/* -* 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) - -void __init percpu_init_areas (void ) -{
xen/arch/x86/percpu.c line 5
Quoted 10 lines
xen/arch/x86/percpu.c +3
-6-unsigned int cpu; - -for ( cpu =1 ; cpu <NR_CPUS ; cpu++ ) - __per_cpu_offset[cpu] =INVALID_PERCPU_AREA ; -} +#include <xen/percpu.h> +#include <xen/smp.h> -static int init_percpu_area (unsigned int cpu) +int arch_percpu_area_init_status (void )
I understand that Arm and x86 are returning a different value today. But now that we are provising a common implementation, I think we need to explain the difference. This is probably a question for the x86 folks.
Quoted 73 lines
xen/arch/x86/percpu.c +2
-53{ -char *p; - -if ( __per_cpu_offset[cpu] !=INVALID_PERCPU_AREA ) -return 0 ; - -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 cf_check _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 cf_check cpu_percpu_callback( -struct notifier_block *nfb,unsigned long action,void *hcpu) +int arch_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 :case CPU_RESUME_FAILED :@@ -86,27 +26,5 @@ static int cf_check cpu_percpu_callback(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 cf_check percpu_presmp_init(void) -{ -register_cpu_notifier (&cpu_percpu_nfb); -return 0 ; }
xen/arch/x86/percpu.c static int cf_check cpu_percpu_callback( · line 112
Quoted 10 lines
xen/arch/x86/percpu.c +0
-10-presmp_initcall (percpu_presmp_init); - -/* -* Local variables: -* mode: C -* c-file-style: "BSD" -* c-basic-offset: 4 -* indent-tabs-mode: nil -* End: -*/
This needs to stay.
Quoted 23 lines
xen/common/Makefile +1
-0@@ -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.oxen/common/percpu.c new file +5
-0@@ -0,0 +1,127 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#include <xen/percpu.h> +#include <xen/cpu.h> +#include <xen/init.h> +#include <xen/mm.h>
xen/common/percpu.c line 15
Quoted 10 lines
xen/common/percpu.c new file +10
-0+#include <xen/rcupdate.h> + +unsigned long __per_cpu_offset[NR_CPUS ]; + +#ifndef INVALID_PERCPU_AREA +#define INVALID_PERCPU_AREA (-(long)__per_cpu_start) +#endif + +#ifndef ARCH_PERCPU_AREA_CHECK +inline int arch_percpu_area_init_status (void )
This wants to be static. No need for inline as the compiler is free to ignore it...
xen/common/percpu.c line 17
Quoted 2 lines
xen/common/percpu.c new file +2
-0+{ +return -EBUSY ;
See my question above about the difference between x86 and the rest.
xen/common/percpu.c line 23
Quoted 5 lines
xen/common/percpu.c new file +5
-0+} +#endif > + +#ifndef ARCH_CPU_PERCPU_CALLBACK +inline int arch_cpu_percpu_callback (struct notifier_block *nfb, +unsigned long action,void *hcpu)
I am not entirely sure we should introduce arch_cpu_percpu_callback. It seems there are some redundant work. Looking at the x86 implementation the differences are:
* The additional checks
* Extra actions (e.g CPU_RESUME_FAILED/CPU_REMOVE).
I think the extra actions also make sense for other architectures. For the additional checks, the parking feature is implemented in common/*.
So is there any way we could avoid introduce arch_cpu_percpu_callback()?
Quoted 110 lines
xen/common/percpu.c new file +104
-0+{ +unsigned int cpu = (unsigned long )hcpu; + +switch ( action ) + { +case CPU_UP_CANCELED : +case CPU_DEAD : +free_percpu_area (cpu); +break ; + } + +return 0 ; +} +#endif + +#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 arch_percpu_area_init_status (); + +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 cf_check _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 ; +} + +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 cf_check 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 ; +default : +arch_cpu_percpu_callback (nfb, action, hcpu); + } + +return notifier_from_errno (rc); +} + +static struct notifier_block cpu_percpu_nfb = { + .notifier_call = cpu_percpu_callback, + .priority =100 /* highest priority */ +}; + +static int __init cf_check percpu_presmp_init(void) +{ +register_cpu_notifier (&cpu_percpu_nfb); + +return 0 ; +} +presmp_initcall (percpu_presmp_init); + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* indent-tabs-mode: nil +* End: +*/ xen/include/asm-generic/percpu.h +0
-0@@ -12,6 +12,14 @@ extern const char __per_cpu_data_end[];extern unsigned long __per_cpu_offset[NR_CPUS ];
xen/include/asm-generic/percpu.h extern const char __per_cpu_data_end[]; · line 22
Quoted 10 lines
xen/include/asm-generic/percpu.h +8
-0void percpu_init_areas (void ); +void free_percpu_area (unsigned int cpu); + +int arch_percpu_area_init_status (void ); + +struct notifier_block ; +int arch_cpu_percpu_callback (struct notifier_block *nfb, +unsigned long action,void *hcpu); +
I believe this belong to include/xen/percpu.h. asm-generic is more for common definition that can be overriden by arch specific header.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v1 2/2] xen: move per-cpu area management into common code
Quoted 35 lines · Julien Grall
Quoted 34 lines · Oleksii Kurochko
xen/arch/x86/percpu.c +0
-9@@ -1,79 +1,19 @@ -#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 ]; - -/* -* 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.
- */
-#defineINVALID_PERCPU_AREA(0x8000000000000000UL- (unsigned long)__per_cpu_start)
-#definePERCPU_ORDERget_order_from_bytes(__per_cpu_data_end - __per_cpu_start)xen/arch/x86/percpu.c +0
-8- -void __init percpu_init_areas (void ) -{ - unsigned int cpu; - - for ( cpu = 1; cpu < NR_CPUS; cpu++ ) - __per_cpu_offset[cpu] = INVALID_PERCPU_AREA; -}
Quoted 8 lines
Quoted 4 lines
xen/arch/x86/percpu.c +2
-0+#include <xen/percpu.h> +#include <xen/smp.h> -static int
init_percpu_area(unsigned int cpu)
+intarch_percpu_area_init_status(void)I understand that Arm and x86 are returning a different value today. But now that we are provising a common implementation, I think we need to explain the difference. This is probably a question for the x86 folks.
The declarations for CPU Parking (variable, or compile time false) wants to be in the new common header, at which point arch_percpu_area_init_status() doesn't need to exist.
That also makes it very clear that there's a difference in return value based on CPU Parking (and the comment beside the variable explains this is about not quite offlining CPUs), which is far clearer than some arch function.
Quoted 6 lines
Quoted 6 lines
xen/common/percpu.c new file +0
-0@@ -0,0 +1,127 @@
Quoted 8 lines
xen/common/percpu.c line 8
Quoted 8 lines
xen/common/percpu.c new file +8
-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 ];
unsigned long __per_cpu_offset[NR_CPUS] = {
[0 ... NR_CPUS - 1] = INVALID_PERCPU_AREA,
};
should work, removing the need for percpu_init_areas() and avoids a window during boot where all CPUs "share" a percpu area.
~Andrew
Re: [PATCH v1 2/2] xen: move per-cpu area management into common code
Quoted 55 lines · Andrew Cooper
Quoted 40 lines · Julien Grall
Quoted 39 lines · Oleksii Kurochko
xen/arch/x86/percpu.c +0
-9@@ -1,79 +1,19 @@ -#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 ]; - -/* -* 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.
- */
-#defineINVALID_PERCPU_AREA(0x8000000000000000UL- (unsigned long)__per_cpu_start)
-#definePERCPU_ORDERget_order_from_bytes(__per_cpu_data_end - __per_cpu_start)xen/arch/x86/percpu.c +2
-8- -void __init percpu_init_areas (void ) -{ - unsigned int cpu; - - for ( cpu = 1; cpu < NR_CPUS; cpu++ ) - __per_cpu_offset[cpu] = INVALID_PERCPU_AREA; -} +#include <xen/percpu.h> +#include <xen/smp.h> -static int
init_percpu_area(unsigned int cpu)Quoted 8 lines
Quoted 1 line
+int
arch_percpu_area_init_status(void)I understand that Arm and x86 are returning a different value today.
But now that we are provising a common implementation, I think we need
to explain the difference. This is probably a question for the x86 folks.The declarations for CPU Parking (variable, or compile time false) wants
to be in the new common header, at which pointarch_percpu_area_init_status()doesn't need to exist.
Quoted 7 lines
That also makes it very clear that there's a difference in return value
based on CPU Parking (and the comment beside the variable explains this
is about not quite offlining CPUs), which is far clearer than some arch
function.
Thanks for suggestion. It would be better, I had also concerns about arch_percpu_area_init_status but couldn't come up with better thing.
Just to make sure I understand correctly—are you saying that I can use park_offline_cpus instead of arch_percpu_area_init_status()?
diff --git a/xen/common/percpu.c b/xen/common/percpu.c index 3837ef5714..f997418586 100644
--- a/xen/common/percpu.c
+++ b/xen/common/percpu.c
@@ -51,7 +51,7 @@ static int init_percpu_area(unsigned int cpu)
char *p;
if ( __per_cpu_offset[cpu] != INVALID_PERCPU_AREA )
- return arch_percpu_area_init_status();
+ return park_offline_cpus ? 0 : -EBUSY;
if ( (p = alloc_xenheap_pages(PERCPU_ORDER, 0)) == NULL )
return -ENOMEM;~ Oleksii
Re: [PATCH v1 2/2] xen: move per-cpu area management into common code
In reply to Julien GrallHi Julien,
Quoted 15 lines · Julien Grall
Quoted 1 line
+}
> +#endif> +
Quoted 4 lines
xen/common/percpu.c new file +3
-0+#ifndef ARCH_CPU_PERCPU_CALLBACK +inline int arch_cpu_percpu_callback (struct notifier_block *nfb, + unsigned long action, void*hcpu)
I am not entirely sure we should introduce
arch_cpu_percpu_callback. It
seems there are some redundant work. Looking at the x86 implementation
the differences are:
* The additional checks
* Extra actions (e.g CPU_RESUME_FAILED/CPU_REMOVE).
Quoted 7 lines
I think the extra actions also make sense for other architectures. For
the additional checks, the parking feature is implemented in common/*.So is there any way we could avoid introduce
arch_cpu_percpu_callback()?
Initially, I did not want to introduce arch_cpu_percpu_callback(), and if it were only the park_offline_cpus check, it would be safe enough since other architectures would continue to function as before ( and CPU parking is a common feature so it is to be expected to work on different architectures ), with park_offline_cpus = false for them.
I then decided to investigate why the check system_state != SYS_STATE_suspend is necessary, and according to the commit message:
```
xen: don't free percpu areas during suspend
Instead of freeing percpu areas during suspend and allocating them
again when resuming keep them. Only free an area in case a cpu
didn't
come up again when resuming.
It should be noted that there is a potential change in behaviour as
the percpu areas are no longer zeroed out during suspend/resume.
While
I have checked the called cpu notifier hooks to cope with that
there
might be some well hidden dependency on the previous behaviour.
OTOH
a component not registering itself for cpu down/up and expecting to
see a zeroed percpu variable after suspend/resume is kind of broken
already. And the opposite case, where a component is not registered
to be called for cpu down/up and is not expecting a percpu variable
suddenly to be zero due to suspend/resume is much more probable,
especially as the suspend/resume functionality seems not to be
tested
that often.
```It is mentioned that "there is a potential change in behavior as the percpu areas are no longer zeroed out during suspend/resume." I was unsure if this change might break something for Arm.
I can attempt to move everything to the common percpu.c and at least verify that there are no issues in CI. If that suffices to confirm that everything is okay, I can create a new patch series version.
Does this make sense?
~ Oleksii
Re: [PATCH v1 2/2] xen: move per-cpu area management into common code
In reply to [email protected]Quoted 71 lines · On 23/09/2024 10:26 am, [email protected] wrote:
Quoted 53 lines · Andrew Cooper
Quoted 39 lines · Julien Grall
Quoted 38 lines · Oleksii Kurochko
xen/arch/x86/percpu.c +0
-9@@ -1,79 +1,19 @@ -#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 ]; - -/* -* 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.
- */
-#defineINVALID_PERCPU_AREA(0x8000000000000000UL- (unsigned long)__per_cpu_start)
-#definePERCPU_ORDERget_order_from_bytes(__per_cpu_data_end - __per_cpu_start)xen/arch/x86/percpu.c +2
-8- -void __init percpu_init_areas (void ) -{ - unsigned int cpu; - - for ( cpu = 1; cpu < NR_CPUS; cpu++ ) - __per_cpu_offset[cpu] = INVALID_PERCPU_AREA; -} +#include <xen/percpu.h> +#include <xen/smp.h> Quoted 8 lines
Quoted 2 lines
-static int
init_percpu_area(unsigned int cpu)
+intarch_percpu_area_init_status(void)I understand that Arm and x86 are returning a different value today.
But now that we are provising a common implementation, I think we need
to explain the difference. This is probably a question for the x86 folks.The declarations for CPU Parking (variable, or compile time false) wants
to be in the new common header, at which pointarch_percpu_area_init_status()doesn't need to exist.Quoted 7 lines
That also makes it very clear that there's a difference in return value
based on CPU Parking (and the comment beside the variable explains this
is about not quite offlining CPUs), which is far clearer than some arch
function.Thanks for suggestion. It would be better, I had also concerns about
arch_percpu_area_init_statusbut couldn't come up with better thing.Just to make sure I understand correctly—are you saying that I can use
park_offline_cpusinstead ofarch_percpu_area_init_status()?
diff --gita/xen/common/percpu.cb/xen/common/percpu.cindex3837ef5714..f997418586100644
---a/xen/common/percpu.c
+++b/xen/common/percpu.c
@@ -51,7 +51,7 @@ static intinit_percpu_area(unsigned int cpu)
Quoted 8 lines
char *p;
if ( __per_cpu_offset[cpu] != INVALID_PERCPU_AREA )
- return arch_percpu_area_init_status();
+ return park_offline_cpus ? 0 : -EBUSY;
if ( (p = alloc_xenheap_pages(PERCPU_ORDER, 0)) == NULL )
return -ENOMEM;Yes, that's exactly what I had in mind.
~Andrew
Re: [PATCH v1 1/2] xen: introduce common macros for per-CPU sections defintion
In reply to Oleksii KurochkoQuoted 8 lines · Oleksii Kurochko
xen/arch/x86/xen.lds.S +0
-2@@ -321,14 +321,7 @@ SECTIONS DECL_SECTION(.bss) { __bss_start = .; *(.bss.page_aligned*) - . = ALIGN(PAGE_SIZE); - __per_cpu_start = .;
xen/arch/x86/xen.lds.S SECTIONS · line 327
Quoted 10 lines
xen/arch/x86/xen.lds.S +1
-6- *(.bss.percpu.page_aligned) - *(.bss.percpu) - . = ALIGN(SMP_CACHE_BYTES); - *(.bss.percpu.read_mostly) - . = ALIGN(SMP_CACHE_BYTES); - __per_cpu_data_end = .; + PERCPU_SECTION *(.bss .bss.*) . = ALIGN(POINTER_ALIGN); __bss_end = .;
Like the _SEC in the other patch I question _SECTION here, albeit for a different
reason: This is no separate output section, and it's more than one kind of input ones. Perhaps PERCPU_DATA? With that
Jan
Re: [PATCH v1 1/2] xen: introduce common macros for per-CPU sections defintion
Quoted 25 lines · Jan Beulich
Quoted 8 lines · Oleksii Kurochko
xen/arch/x86/xen.lds.S +0
-2@@ -321,14 +321,7 @@ SECTIONS DECL_SECTION(.bss) { __bss_start = .; *(.bss.page_aligned*) - . = ALIGN(PAGE_SIZE); - __per_cpu_start = .;xen/arch/x86/xen.lds.S SECTIONS · line 327
Quoted 10 lines
xen/arch/x86/xen.lds.S +1
-6- *(.bss.percpu.page_aligned) - *(.bss.percpu) - . = ALIGN(SMP_CACHE_BYTES); - *(.bss.percpu.read_mostly) - . = ALIGN(SMP_CACHE_BYTES); - __per_cpu_data_end = .; + PERCPU_SECTION *(.bss .bss.*) . = ALIGN(POINTER_ALIGN); __bss_end = .;Like the
_SECin the other patch I question_SECTIONhere, albeit for a different
reason: This is no separate output section, and it's more than one kind of input
ones. PerhapsPERCPU_DATA? With that
Quoted 1 line
Acked-by Jan Beulich <[email protected]>
Sure, I will drop _SECTION here too. Thanks.
~ Oleksii
Re: [PATCH v1 1/2] xen: introduce common macros for per-CPU sections defintion
Quoted 26 lines · On 23/09/2024 12:48 pm, [email protected] wrote:
Quoted 24 lines · Jan Beulich
Quoted 8 lines · Oleksii Kurochko
xen/arch/x86/xen.lds.S +0
-2@@ -321,14 +321,7 @@ SECTIONS DECL_SECTION(.bss) { __bss_start = .; *(.bss.page_aligned*) - . = ALIGN(PAGE_SIZE); - __per_cpu_start = .;xen/arch/x86/xen.lds.S SECTIONS · line 327
Quoted 10 lines
xen/arch/x86/xen.lds.S +1
-6- *(.bss.percpu.page_aligned) - *(.bss.percpu) - . = ALIGN(SMP_CACHE_BYTES); - *(.bss.percpu.read_mostly) - . = ALIGN(SMP_CACHE_BYTES); - __per_cpu_data_end = .; + PERCPU_SECTION *(.bss .bss.*) . = ALIGN(POINTER_ALIGN); __bss_end = .;Like the
_SECin the other patch I question_SECTIONhere, albeit for a different
reason: This is no separate output section, and it's more than one kind of input
ones. PerhapsPERCPU_DATA? With thatQuoted 1 line
Acked-by Jan Beulich <[email protected]>
Quoted 1 line
Sure, I will drop
_SECTIONhere too. Thanks.
Can we call it PERCPU_BSS? Just to highlight the fact that it really is BSS, and not initialised DATA.
~Andrew
Re: [PATCH v1 1/2] xen: introduce common macros for per-CPU sections defintion
Quoted 27 lines · Andrew Cooper
Quoted 26 lines · On 23/09/2024 12:48 pm, [email protected] wrote:
Quoted 25 lines · Jan Beulich
Quoted 8 lines · Oleksii Kurochko
xen/arch/x86/xen.lds.S +0
-2@@ -321,14 +321,7 @@ SECTIONS DECL_SECTION(.bss) { __bss_start = .; *(.bss.page_aligned*) - . = ALIGN(PAGE_SIZE); - __per_cpu_start = .;xen/arch/x86/xen.lds.S SECTIONS · line 327
Quoted 10 lines
xen/arch/x86/xen.lds.S +1
-6- *(.bss.percpu.page_aligned) - *(.bss.percpu) - . = ALIGN(SMP_CACHE_BYTES); - *(.bss.percpu.read_mostly) - . = ALIGN(SMP_CACHE_BYTES); - __per_cpu_data_end = .; + PERCPU_SECTION *(.bss .bss.*) . = ALIGN(POINTER_ALIGN); __bss_end = .;Like the
_SECin the other patch I question_SECTIONhere, albeit for a different
reason: This is no separate output section, and it's more than one kind of input
ones. PerhapsPERCPU_DATA? With thatAcked-by Jan Beulich <[email protected]>
Quoted 4 lines
Quoted 1 line
Sure, I will drop
_SECTIONhere too. Thanks.Can we call it PERCPU_BSS? Just to highlight the fact that it really is BSS, and not initialised DATA.
Fine with me.
Jan
Re: [PATCH v1 2/2] xen: move per-cpu area management into common code
In reply to Andrew CooperQuoted 19 lines · Andrew Cooper
Quoted 6 lines
Quoted 6 lines
xen/common/percpu.c new file +0
-0@@ -0,0 +1,127 @@Quoted 8 lines
xen/common/percpu.c line 8
Quoted 8 lines
xen/common/percpu.c new file +8
-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 ];unsigned long __per_cpu_offset[NR_CPUS] = {
[0 ...NR_CPUS- 1] =INVALID_PERCPU_AREA,
};
Quoted 2 lines
should work, removing the need for
percpu_init_areas()and avoids a window during boot where all CPUs "share" a percpu area.
If to define in this way, it will compilation error:
./arch/x86/include/asm/percpu.h:14:29: error: initializer element is not constant
14 | #define INVALID_PERCPU_AREA (0x8000000000000000UL -
(unsigned long)__per_cpu_start)
I think it is the reason why separate function was introduced.
~ Oleksii