Improvements with noreturn
v5 cover Improvements with noreturn
Make better use of noreturn. It allows optimising compilers to produce more efficient code.
Each patch is compile tested on each architecture, and the result is functionally tested on x86 and compile tested on GCC 4.1.1 to verify that older compilers are happy.
Changes in v5:
* Reorder patches 1 and 2 and tweak so they compile in isolation.
PATCH v5 1/5 x86/crash: Fix up declaration of do_nmi_crash()
... so it can correctly be annotated as noreturn. Move the declaration of nmi_crash() to be effectively private in crash.c
---
Changes in v5:
* Move to start of series to fix bisection issue.
--- xen/arch/x86/crash.c | 3 ++- xen/include/asm-x86/processor.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-)
xen/arch/x86/crash.c +2 -1
@@ -36,7 +36,7 @@ static unsigned int crashing_cpu; static DEFINE_PER_CPU_READ_MOSTLY(bool_t, crash_save_done);/* This becomes the NMI handler for non-crashing CPUs, when Xen is crashing. */ -void __attribute__((noreturn))do_nmi_crash (struct cpu_user_regs *regs) +void do_nmi_crash (struct cpu_user_regs *regs) {int cpu =smp_processor_id ();
@@ -113,6 +113,7 @@ void __attribute__((noreturn)) do_nmi_crash(struct cpu_user_regs *regs)halt (); } +void nmi_crash (void );static void nmi_shootdown_cpus (void ) {unsigned long msecs;
xen/include/asm-x86/processor.h +1 -1
@@ -530,7 +530,6 @@ void do_ ## _name(struct cpu_user_regs *regs)DECLARE_TRAP_HANDLER (divide_error);DECLARE_TRAP_HANDLER (debug);DECLARE_TRAP_HANDLER (nmi); -DECLARE_TRAP_HANDLER (nmi_crash);DECLARE_TRAP_HANDLER (int3);DECLARE_TRAP_HANDLER (overflow);DECLARE_TRAP_HANDLER (bounds);
@@ -551,6 +550,7 @@ DECLARE_TRAP_HANDLER(spurious_interrupt_bug);void trap_nop (void );void enable_nmis (void ); +void __attribute__((noreturn))do_nmi_crash (struct cpu_user_regs *regs);void syscall_enter (void );void sysenter_entry (void );
Signature · 1 line
1.7.10.4
PATCH v5 2/5 xen/compiler: Replace opencoded __attribute__((noreturn))
In reply to Andrew CooperMake a formal define for noreturn in compiler.h, and fix up opencoded uses of __attribute__((noreturn)). This includes removing redundant uses with function definitions which have a public declaration.
--- xen/arch/arm/early_printk.c | 2 +- xen/arch/x86/efi/boot.c | 4 ++-- xen/arch/x86/shutdown.c | 2 +- xen/include/asm-arm/early_printk.h | 4 ++-- xen/include/asm-x86/processor.h | 2 +- xen/include/xen/compiler.h | 2 ++ xen/include/xen/lib.h | 2 +- xen/include/xen/sched.h | 4 ++-- 8 files changed, 12 insertions(+), 10 deletions(-)
xen/arch/arm/early_printk.c +1 -1
@@ -52,7 +52,7 @@ void __init early_printk(const char *fmt, ...)va_end (args); } -void __attribute__((noreturn))__init +void __init early_panic (const char *fmt, ...) {va_list args;
xen/arch/x86/efi/boot.c +2 -2
@@ -183,7 +183,7 @@ static bool_t __init match_guid(const EFI_GUID *guid1, const EFI_GUID *guid2) !memcmp (guid1->Data4, guid2->Data4,sizeof (guid1->Data4)); } -static void __init __attribute__((__noreturn__))blexit (const CHAR16 *str) +static void __init noreturnblexit (const CHAR16 *str) {if ( str )PrintStr ((CHAR16 *)str);
@@ -762,7 +762,7 @@ static void __init relocate_trampoline(unsigned long phys) *(u16 *)(*trampoline_ptr + (long )trampoline_ptr) = phys >>4 ; } -void EFIAPI __init __attribute__((__noreturn__)) +void EFIAPI __init noreturnefi_start (EFI_HANDLE ImageHandle,EFI_SYSTEM_TABLE *SystemTable) {static EFI_GUID __initdata loaded_image_guid =LOADED_IMAGE_PROTOCOL ;
xen/arch/x86/shutdown.c +1 -1
@@ -85,7 +85,7 @@ static inline void kb_wait(void)break ; } -static void __attribute__((noreturn))__machine_halt (void *unused) +static void noreturn__machine_halt (void *unused) {local_irq_disable ();for ( ; ; )
xen/include/asm-arm/early_printk.h +2 -2
@@ -26,7 +26,7 @@void early_printk (const char *fmt, ...) __attribute__((format (printf,1 ,2 ))); -void early_panic (const char *fmt, ...) __attribute__((noreturn)) +void noreturnearly_panic (const char *fmt, ...) __attribute__((format (printf,1 ,2 )));#else
@@ -35,7 +35,7 @@ static inline __attribute__((format (printf, 1, 2))) void early_printk(const char *fmt, ...) {} -static inline void __attribute__((noreturn)) +static inline void noreturn __attribute__((format (printf,1 ,2 )))early_panic (const char *fmt, ...) {while (1 );}
xen/include/asm-x86/processor.h +1 -1
@@ -550,7 +550,7 @@ DECLARE_TRAP_HANDLER(spurious_interrupt_bug);void trap_nop (void );void enable_nmis (void ); -void __attribute__((noreturn))do_nmi_crash (struct cpu_user_regs *regs); +void noreturndo_nmi_crash (struct cpu_user_regs *regs);void syscall_enter (void );void sysenter_entry (void );
xen/include/xen/compiler.h +2 -0
@@ -14,6 +14,8 @@#define always_inline __inline__ __attribute__ ((always_inline))#define noinline __attribute__((noinline)) +#define noreturn __attribute__((noreturn)) +#if (!defined(__clang__) && (__GNUC__ ==4 ) && (__GNUC_MINOR__ <5 ))#define unreachable () do {} while (1)#else
xen/include/xen/lib.h +1 -1
@@ -8,7 +8,7 @@#include <xen/string.h> #include <asm/bug.h> -void __bug (char *file,int line) __attribute__((noreturn)); +void noreturn__bug (char *file,int line);void __warn (char *file,int line);#define BUG_ON (p) do { if (unlikely(p)) BUG(); } while (0)
xen/include/xen/sched.h +2 -2
@@ -580,7 +580,7 @@ void __domain_crash(struct domain *d);* Mark current domain as crashed and synchronously deschedule from the local * processor. This function never returns. */ -void __domain_crash_synchronous (void ) __attribute__((noreturn)); +void noreturn__domain_crash_synchronous (void );#define domain_crash_synchronous () do { \ printk("domain_crash_sync called from %s:%d\n", __FILE__, __LINE__); \ __domain_crash_synchronous(); \
@@ -591,7 +591,7 @@ void __domain_crash_synchronous(void) __attribute__((noreturn));* the crash occured. If addr is 0, look up address from last extable * redirection. */ -void asm_domain_crash_synchronous (unsigned long addr) __attribute__((noreturn)); +void noreturnasm_domain_crash_synchronous (unsigned long addr);#define set_current_state (_s) do { current->state = (_s); } while (0)void scheduler_init (void );
Signature · 1 line
1.7.10.4
PATCH v5 3/5 xen: Identify panic and reboot/halt functions as noreturn
In reply to Andrew CooperOn an x86 build (GCC Debian 4.7.2-5), this substantially reduces the size of .text and .init.text sections.
Experimentally, even in a non-debug build, GCC uses call rather than jmp so there should be no impact on any stack trace generation.
--- xen/arch/arm/shutdown.c | 2 +- xen/arch/x86/cpu/mcheck/mce.h | 2 +- xen/arch/x86/shutdown.c | 2 +- xen/common/shutdown.c | 2 +- xen/include/asm-arm/smp.h | 2 +- xen/include/asm-x86/processor.h | 2 +- xen/include/xen/lib.h | 2 +- xen/include/xen/shutdown.h | 8 +++++--- 8 files changed, 12 insertions(+), 10 deletions(-)
xen/arch/arm/shutdown.c +1 -1
@@ -11,7 +11,7 @@ static void raw_machine_reset(void)platform_reset (); } -static void halt_this_cpu (void *arg) +static void noreturnhalt_this_cpu (void *arg) {stop_cpu (); }
xen/arch/x86/cpu/mcheck/mce.h +1 -1
@@ -57,7 +57,7 @@ int mce_available(struct cpuinfo_x86 *c);unsigned int mce_firstbank (struct cpuinfo_x86 *c);/* Helper functions used for collecting error telemetry */ struct mc_info *x86_mcinfo_getptr (void ); -void mc_panic (char *s); +void noreturnmc_panic (char *s);void x86_mc_get_cpu_info (unsigned ,uint32_t *,uint16_t *,uint16_t *,uint32_t *,uint32_t *,uint32_t *,uint32_t *);
xen/arch/x86/shutdown.c +1 -1
@@ -452,7 +452,7 @@ static int __init reboot_init(void) }__initcall (reboot_init); -static void __machine_restart (void *pdelay) +static void noreturn__machine_restart (void *pdelay) {machine_restart (*(unsigned int *)pdelay); }
xen/common/shutdown.c +1 -1
@@ -17,7 +17,7 @@bool_t __read_mostly opt_noreboot;boolean_param ("noreboot" , opt_noreboot); -static void maybe_reboot (void ) +static void noreturnmaybe_reboot (void ) {if ( opt_noreboot ) {
xen/include/asm-arm/smp.h +1 -1
@@ -15,7 +15,7 @@ DECLARE_PER_CPU(cpumask_var_t, cpu_core_mask);#define raw_smp_processor_id () (get_processor_id()) -extern void stop_cpu (void ); +extern void noreturnstop_cpu (void );extern int arch_smp_init (void );extern int arch_cpu_init (int cpu,struct dt_device_node *dn);
xen/include/asm-x86/processor.h +1 -1
@@ -515,7 +515,7 @@ void show_registers(struct cpu_user_regs *regs);void show_execution_state (struct cpu_user_regs *regs);#define dump_execution_state () run_in_exception_handler(show_execution_state)void show_page_walk (unsigned long addr); -void fatal_trap (int trapnr,struct cpu_user_regs *regs); +void noreturnfatal_trap (int trapnr,struct cpu_user_regs *regs);void compat_show_guest_stack (struct vcpu *,struct cpu_user_regs *,int lines);
xen/include/xen/lib.h +1 -1
@@ -87,7 +87,7 @@ extern void printk(const char *format, ...) __attribute__ ((format (printf, 1, 2)));extern void guest_printk (const struct domain *d,const char *format, ...) __attribute__ ((format (printf,2 ,3 ))); -extern void panic (const char *format, ...) +extern void noreturnpanic (const char *format, ...) __attribute__ ((format (printf,1 ,2 )));extern long vm_assist (struct domain *,unsigned int ,unsigned int );extern int __printk_ratelimit (int ratelimit_ms,int ratelimit_burst);
xen/include/xen/shutdown.h +5 -3
@@ -1,13 +1,15 @@#ifndef __XEN_SHUTDOWN_H__#define __XEN_SHUTDOWN_H__ +#include <xen/compiler.h> +/* opt_noreboot: If true, machine will need manual reset on error. */ extern bool_t opt_noreboot; -void dom0_shutdown (u8 reason); +void noreturndom0_shutdown (u8 reason); -void machine_restart (unsigned int delay_millisecs); -void machine_halt (void ); +void noreturnmachine_restart (unsigned int delay_millisecs); +void noreturnmachine_halt (void );void machine_power_off (void );#endif /* __XEN_SHUTDOWN_H__ */
Signature · 1 line
1.7.10.4
Re: [PATCH v5 3/5] xen: Identify panic and reboot/halt functions as noreturn
Quoted 9 lines · Andrew Cooper
On an x86 build (GCC Debian 4.7.2-5), this substantially reduces the size of .text and .init.text sections.
Experimentally, even in a non-debug build, GCC uses
callrather thanjmpso there should be no impact on any stack trace generation.Signed-off-by Andrew Cooper <[email protected]>Acked-by Keir Fraser <[email protected]>Reviewed-by Jan Beulich <[email protected]>
Quoted 127 lines
CC Stefano Stabellini <[email protected]>Acked-by Tim Deegan <[email protected]>--- xen/arch/arm/shutdown.c | 2 +- xen/arch/x86/cpu/mcheck/mce.h | 2 +- xen/arch/x86/shutdown.c | 2 +- xen/common/shutdown.c | 2 +- xen/include/asm-arm/smp.h | 2 +- xen/include/asm-x86/processor.h | 2 +- xen/include/xen/lib.h | 2 +- xen/include/xen/shutdown.h | 8 +++++--- 8 files changed, 12 insertions(+), 10 deletions(-)xen/arch/arm/shutdown.c +1
-1@@ -11,7 +11,7 @@ static void raw_machine_reset(void)platform_reset (); } -static void halt_this_cpu (void *arg) +static void noreturnhalt_this_cpu (void *arg) {stop_cpu (); }xen/arch/x86/cpu/mcheck/mce.h +1
-1@@ -57,7 +57,7 @@ int mce_available(struct cpuinfo_x86 *c);unsigned int mce_firstbank (struct cpuinfo_x86 *c);/* Helper functions used for collecting error telemetry */ struct mc_info *x86_mcinfo_getptr (void ); -void mc_panic (char *s); +void noreturnmc_panic (char *s);void x86_mc_get_cpu_info (unsigned ,uint32_t *,uint16_t *,uint16_t *,uint32_t *,uint32_t *,uint32_t *,uint32_t *);xen/arch/x86/shutdown.c +1
-1@@ -452,7 +452,7 @@ static int __init reboot_init(void) }__initcall (reboot_init); -static void __machine_restart (void *pdelay) +static void noreturn__machine_restart (void *pdelay) {machine_restart (*(unsigned int *)pdelay); }xen/common/shutdown.c +1
-1@@ -17,7 +17,7 @@bool_t __read_mostly opt_noreboot;boolean_param ("noreboot" , opt_noreboot); -static void maybe_reboot (void ) +static void noreturnmaybe_reboot (void ) {if ( opt_noreboot ) {xen/include/asm-arm/smp.h +1
-1@@ -15,7 +15,7 @@ DECLARE_PER_CPU(cpumask_var_t, cpu_core_mask);#define raw_smp_processor_id () (get_processor_id()) -extern void stop_cpu (void ); +extern void noreturnstop_cpu (void );extern int arch_smp_init (void );extern int arch_cpu_init (int cpu,struct dt_device_node *dn);xen/include/asm-x86/processor.h +1
-1@@ -515,7 +515,7 @@ void show_registers(struct cpu_user_regs *regs);void show_execution_state (struct cpu_user_regs *regs);#define dump_execution_state () run_in_exception_handler(show_execution_state)void show_page_walk (unsigned long addr); -void fatal_trap (int trapnr,struct cpu_user_regs *regs); +void noreturnfatal_trap (int trapnr,struct cpu_user_regs *regs);void compat_show_guest_stack (struct vcpu *,struct cpu_user_regs *,int lines);xen/include/xen/lib.h +1
-1@@ -87,7 +87,7 @@ extern void printk(const char *format, ...) __attribute__ ((format (printf, 1, 2)));extern void guest_printk (const struct domain *d,const char *format, ...) __attribute__ ((format (printf,2 ,3 ))); -extern void panic (const char *format, ...) +extern void noreturnpanic (const char *format, ...) __attribute__ ((format (printf,1 ,2 )));extern long vm_assist (struct domain *,unsigned int ,unsigned int );extern int __printk_ratelimit (int ratelimit_ms,int ratelimit_burst);xen/include/xen/shutdown.h +5
-3@@ -1,13 +1,15 @@#ifndef __XEN_SHUTDOWN_H__#define __XEN_SHUTDOWN_H__ +#include <xen/compiler.h> +/* opt_noreboot: If true, machine will need manual reset on error. */ extern bool_t opt_noreboot; -void dom0_shutdown (u8 reason); +void noreturndom0_shutdown (u8 reason); -void machine_restart (unsigned int delay_millisecs); -void machine_halt (void ); +void noreturnmachine_restart (unsigned int delay_millisecs); +void noreturnmachine_halt (void );void machine_power_off (void );#endif /* __XEN_SHUTDOWN_H__ */
PATCH v5 4/5 xen: Misc cleanup as a result of the previous patches
In reply to Andrew CooperThis includes:
* A stale comment in sh_skip_sync()
* A dead for ever loop in __bug()
* A prototype for machine_power_off() which unimplemented in any architecture
* Replacing a for(;;); loop with unreachable()
--- xen/arch/x86/efi/boot.c | 2 +- xen/arch/x86/mm/shadow/common.c | 1 - xen/drivers/char/console.c | 1 - xen/include/xen/shutdown.h | 1 - 4 files changed, 1 insertion(+), 4 deletions(-)
xen/arch/x86/efi/boot.c +1 -1
@@ -201,7 +201,7 @@ static void __init noreturn blexit(const CHAR16 *str) efi_bs->FreePages (xsm.addr,PFN_UP (xsm.size)); efi_bs->Exit (efi_ih,EFI_SUCCESS ,0 ,NULL ); -for ( ; ; );/* not reached */ +unreachable ();/* not reached */ }/* generic routine for printing error messages */
xen/arch/x86/mm/shadow/common.c +0 -1
@@ -875,7 +875,6 @@ static int sh_skip_sync(struct vcpu *v, mfn_t gl1mfn)SHADOW_ERROR ("gmfn %#lx was OOS but not shadowed as an l1.\n" ,mfn_x (gl1mfn));BUG (); -return 0 ;/* BUG() is no longer __attribute__((noreturn)). */ }
xen/drivers/char/console.c +0 -1
@@ -1089,7 +1089,6 @@ void __bug(char *file, int line)printk ("Xen BUG at %s:%d\n" , file, line);dump_execution_state ();panic ("Xen BUG at %s:%d" , file, line); -for ( ; ; ) ; }void __warn (char *file,int line)
xen/include/xen/shutdown.h +0 -1
@@ -10,6 +10,5 @@ void noreturn dom0_shutdown(u8 reason);void noreturnmachine_restart (unsigned int delay_millisecs);void noreturnmachine_halt (void ); -void machine_power_off (void );#endif /* __XEN_SHUTDOWN_H__ */
Signature · 1 line
1.7.10.4
PATCH v5 5/5 xen/x86: Identify reset_stack_and_jump() as noreturn
In reply to Andrew Cooperreset_stack_and_jump() is actually a macro, but can effectively become noreturn by giving it an unreachable() declaration.
Propagate the 'noreturn-ness' up through the direct and indirect callers.
--- xen/arch/x86/domain.c | 6 ++---- xen/arch/x86/hvm/svm/svm.c | 2 +- xen/arch/x86/setup.c | 2 +- xen/include/asm-x86/current.h | 11 +++++++---- xen/include/asm-x86/domain.h | 2 +- xen/include/asm-x86/hvm/vmx/vmx.h | 2 +- 6 files changed, 13 insertions(+), 12 deletions(-)
xen/arch/x86/domain.c +2 -4
@@ -133,12 +133,12 @@ void startup_cpu_idle_loop(void)reset_stack_and_jump (idle_loop); } -static void continue_idle_domain (struct vcpu *v) +static void noreturncontinue_idle_domain (struct vcpu *v) {reset_stack_and_jump (idle_loop); } -static void continue_nonidle_domain (struct vcpu *v) +static void noreturncontinue_nonidle_domain (struct vcpu *v) {check_wakeup_from_wait ();mark_regs_dirty (guest_cpu_user_regs ());
@@ -1521,13 +1521,11 @@ void context_switch(struct vcpu *prev, struct vcpu *next)update_vcpu_system_time (next);schedule_tail (next); -BUG (); }void continue_running (struct vcpu *same) {schedule_tail (same); -BUG (); }int __sync_local_execstate (void )
xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -911,7 +911,7 @@ static void svm_ctxt_switch_to(struct vcpu *v)wrmsrl (MSR_TSC_AUX ,hvm_msr_tsc_aux (v)); } -static void svm_do_resume (struct vcpu *v) +static void noreturnsvm_do_resume (struct vcpu *v) {struct vmcb_struct *vmcb = v->arch.hvm_svm.vmcb;bool_t debug_state = v->domain->debugger_attached;
xen/arch/x86/setup.c +1 -1
@@ -541,7 +541,7 @@ static char * __init cmdline_cook(char *p, char *loader_name)return p; } -void __init __start_xen (unsigned long mbi_p) +void __init noreturn__start_xen (unsigned long mbi_p) {char *memmap_type =NULL ;char *cmdline, *kextra, *loader;
xen/include/asm-x86/current.h +7 -4
@@ -59,10 +59,13 @@ static inline struct cpu_info *get_cpu_info(void) ((sp & (~(STACK_SIZE -1 ))) + \ (STACK_SIZE - sizeof(struct cpu_info) - sizeof(unsigned long))) -#define reset_stack_and_jump (__fn) \ - __asm__ __volatile__ ( \ - "mov %0,%%"__OP"sp; jmp %c1" \ - : : "r" (guest_cpu_user_regs()), "i" (__fn) : "memory" ) +#define reset_stack_and_jump (__fn) \ + ({ \ + __asm__ __volatile__ ( \ + "mov %0,%%"__OP"sp; jmp %c1" \ + : : "r" (guest_cpu_user_regs()), "i" (__fn) : "memory" ); \ + unreachable(); \ + })#define schedule_tail (vcpu) (((vcpu)->arch.schedule_tail)(vcpu))
xen/include/asm-x86/domain.h +1 -1
@@ -395,7 +395,7 @@ struct arch_vcpuunsigned long flags;/* TF_ */ -void (*schedule_tail) (struct vcpu *); +void noreturn (*schedule_tail) (struct vcpu *);void (*ctxt_switch_from) (struct vcpu *);void (*ctxt_switch_to) (struct vcpu *);
xen/include/asm-x86/hvm/vmx/vmx.h +1 -1
@@ -91,7 +91,7 @@ typedef enum {void vmx_asm_vmexit_handler (struct cpu_user_regs );void vmx_asm_do_vmentry (void );void vmx_intr_assist (void ); -void vmx_do_resume (struct vcpu *); +void noreturnvmx_do_resume (struct vcpu *);void vmx_vlapic_msr_changed (struct vcpu *v);void vmx_realmode (struct cpu_user_regs *regs);void vmx_update_debug_state (struct vcpu *v);
Signature · 1 line
1.7.10.4