Add support for NMI source reporting
Subsystems (12)
cover Add support for NMI source reporting
Hi Thomas and all,
Non-Maskable Interrupts (NMIs) are routed to the local Advanced Programmable Interrupt Controller (APIC) using vector #2. Before the advent of the Flexible Return and Event Delivery (FRED)[1], the vector information set by the NMI initiator was disregarded or lost within the hardware, compelling system software to poll every registered NMI handler to pinpoint the source of the NMI[2]. This approach led to several issues:
1. Inefficiency due to the CPU's time spent polling all handlers.
2. Increased latency from the additional time taken to poll all handlers.
3. The occurrence of unnecessary NMIs if they are triggered shortly
after being processed by a different source.
To tackle these challenges, Intel introduced NMI source reporting as a part of the FRED specification (detailed in Chapter 9). This CPU feature ensures that while all NMI sources are still aggregated into NMI vector (#2) for delivery, the source of the NMI is now conveyed through FRED event data (a 16-bit bitmap on the stack). This allows for the selective dispatch of the NMI source handler based on the bitmap, eliminating the need to invoke all NMI source handlers indiscriminately.
In line with the hardware architecture, various interrupt sources can generate NMIs by encoding an NMI delivery mode. However, this patchset activates only the local NMI sources that are currently utilized by the Linux kernel, which includes:
1. Performance monitoring.
2. Inter-Processor Interrupts (IPIs) for functions like CPU backtrace,
machine check, Kernel GNU Debugger (KGDB), reboot, panic stop, and self-test.
Other NMI sources will continue to be handled as previously when the NMI source is not utilized or remains unidentified.
[1] https://www.intel.com/content/www/us/en/content-details/779982/flexible-return-and-event-delivery-fred-specification.html
[2] https://lore.kernel.org/lkml/171011362209.2468526.15187874627966416701.tglx@xen13/
Thanks,
Jacob
Jacob Pan (6):
x86/irq: Add enumeration of NMI source reporting CPU feature x86/irq: Extend NMI handler registration interface to include source x86/irq: Factor out common NMI handling code
x86/irq: Process nmi sources in NMI handler
perf/x86: Enable NMI source reporting for perfmon x86/irq: Enable NMI source on IPIs delivered as NMI
arch/x86/Kconfig | 9 +++ arch/x86/events/amd/ibs.c | 2 +- arch/x86/events/core.c | 11 ++- arch/x86/events/intel/core.c | 6 +- arch/x86/include/asm/apic.h | 1 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 ++- arch/x86/include/asm/irq_vectors.h | 31 ++++++++ arch/x86/include/asm/nmi.h | 4 +- arch/x86/kernel/apic/hw_nmi.c | 5 +- arch/x86/kernel/apic/ipi.c | 4 +- arch/x86/kernel/apic/local.h | 18 +++-- arch/x86/kernel/cpu/cpuid-deps.c | 1 + arch/x86/kernel/cpu/mce/inject.c | 4 +- arch/x86/kernel/cpu/mshyperv.c | 2 +- arch/x86/kernel/kgdb.c | 6 +- arch/x86/kernel/nmi.c | 92 ++++++++++++++++++++---- arch/x86/kernel/nmi_selftest.c | 7 +- arch/x86/kernel/reboot.c | 4 +- arch/x86/kernel/smp.c | 4 +- arch/x86/kernel/traps.c | 4 +- arch/x86/platform/uv/uv_nmi.c | 4 +- drivers/acpi/apei/ghes.c | 2 +- drivers/char/ipmi/ipmi_watchdog.c | 2 +- drivers/edac/igen6_edac.c | 2 +- drivers/watchdog/hpwdt.c | 6 +- 26 files changed, 187 insertions(+), 53 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/6 x86/irq: Add enumeration of NMI source reporting CPU feature
The lack of a mechanism to pinpoint the origins of Non-Maskable Interrupts (NMIs) necessitates that the NMI vector 2 handler consults each NMI source handler individually. This approach leads to inefficiencies, delays, and the occurrence of unnecessary NMIs, thereby also constraining the potential applications of NMIs.
A new CPU feature, known as NMI source reporting, has been introduced as part of the Flexible Return and Event Delivery (FRED) spec. This feature enables the NMI vector 2 handler to directly obtain information about the NMI source from the FRED event data.
The functionality of NMI source reporting is tied to the FRED. Although it is enumerated by a unique CPUID feature bit, it cannot be turned off independently once FRED is activated.
--- arch/x86/Kconfig | 9 +++++++++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +++++++- arch/x86/kernel/cpu/cpuid-deps.c | 1 + arch/x86/kernel/traps.c | 4 +++- 5 files changed, 21 insertions(+), 2 deletions(-)
arch/x86/Kconfig +9 -0
@@ -511,12 +511,21 @@ config X86_CPU_RESCTRL config X86_FRED bool "Flexible Return and Event Delivery" depends on X86_64 + select X86_NMI_SOURCE help When enabled, try to use Flexible Return and Event Delivery instead of the legacy SYSCALL/SYSENTER/IDT architecture for ring transitions and exception/interrupt handling if the system supports it. +config X86_NMI_SOURCE + def_bool n + help + Once enabled, information on NMI originator/source can be provided + via FRED event data. This makes NMI processing more efficient in that + NMI handler does not need to check for every possible source at + runtime when NMI is delivered. + config X86_BIGSMP bool "Support for big SMP systems with more than 8 CPUs" depends on SMP && X86_32
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -327,6 +327,7 @@#define X86_FEATURE_FRED (12*32+17)/* Flexible Return and Event Delivery */ #define X86_FEATURE_LKGS (12*32+18)/* "" Load "kernel" (userspace) GS */ #define X86_FEATURE_WRMSRNS (12*32+19)/* "" Non-serializing WRMSR */ +#define X86_FEATURE_NMI_SOURCE (12*32+20)/* NMI source reporting */ #define X86_FEATURE_AMX_FP16 (12*32+21)/* "" AMX fp16 Support */ #define X86_FEATURE_AVX_IFMA (12*32+23)/* "" Support for VPMADD52[H,L]UQ */ #define X86_FEATURE_LAM (12*32+26)/* Linear Address Masking */
arch/x86/include/asm/disabled-features.h +7 -1
@@ -123,6 +123,12 @@# define DISABLE_FRED (1 << (X86_FEATURE_FRED & 31))#endif +#ifdef CONFIG_X86_NMI_SOURCE +# define DISABLE_NMI_SOURCE 0 +#else +# define DISABLE_NMI_SOURCE (1 << (X86_FEATURE_NMI_SOURCE & 31)) +#endif +#ifdef CONFIG_KVM_AMD_SEV #define DISABLE_SEV_SNP 0#else
@@ -145,7 +151,7 @@#define DISABLED_MASK10 0#define DISABLED_MASK11 (DISABLE_RETPOLINE|DISABLE_RETHUNK|DISABLE_UNRET| \ DISABLE_CALL_DEPTH_TRACKING|DISABLE_USER_SHSTK) -#define DISABLED_MASK12 (DISABLE_FRED|DISABLE_LAM) +#define DISABLED_MASK12 (DISABLE_FRED|DISABLE_LAM|DISABLE_NMI_SOURCE)#define DISABLED_MASK13 0#define DISABLED_MASK14 0#define DISABLED_MASK15 0
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
@@ -84,6 +84,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_SHSTK, X86_FEATURE_XSAVES }, { X86_FEATURE_FRED, X86_FEATURE_LKGS }, { X86_FEATURE_FRED, X86_FEATURE_WRMSRNS }, + { X86_FEATURE_FRED, X86_FEATURE_NMI_SOURCE}, {} };
arch/x86/kernel/traps.c +3 -1
@@ -1427,8 +1427,10 @@ early_param("fred", fred_setup);void __init trap_init (void ) { -if (cpu_feature_enabled (X86_FEATURE_FRED ) && !enable_fred) +if (cpu_feature_enabled (X86_FEATURE_FRED ) && !enable_fred) {setup_clear_cpu_cap (X86_FEATURE_FRED ); +setup_clear_cpu_cap (X86_FEATURE_NMI_SOURCE ); + }/* Init cpu_entry_area before IST entries are set up */ setup_cpu_entry_areas ();
Signature · 1 line
2.25.1
PATCH 2/6 x86/irq: Extend NMI handler registration interface to include source
In reply to Jacob PanAdd a source vector argument to register_nmi_handler() such that designated NMI originators can leverage NMI source reporting feature. For those who do not use NMI source reporting, 0 (unknown) is used as the source vector. NMI source vectors (up to 16) are pre-defined.
--- arch/x86/events/amd/ibs.c | 2 +- arch/x86/events/core.c | 3 ++- arch/x86/include/asm/irq_vectors.h | 21 +++++++++++++++++++++ arch/x86/include/asm/nmi.h | 4 +++- arch/x86/kernel/apic/hw_nmi.c | 2 +- arch/x86/kernel/cpu/mce/inject.c | 2 +- arch/x86/kernel/cpu/mshyperv.c | 2 +- arch/x86/kernel/kgdb.c | 4 ++-- arch/x86/kernel/nmi.c | 16 ++++++++++++++++ arch/x86/kernel/nmi_selftest.c | 5 +++-- arch/x86/kernel/reboot.c | 2 +- arch/x86/kernel/smp.c | 2 +- arch/x86/platform/uv/uv_nmi.c | 4 ++-- drivers/acpi/apei/ghes.c | 2 +- drivers/char/ipmi/ipmi_watchdog.c | 2 +- drivers/edac/igen6_edac.c | 2 +- drivers/watchdog/hpwdt.c | 6 +++--- 17 files changed, 61 insertions(+), 20 deletions(-)
arch/x86/events/amd/ibs.c +1 -1
@@ -1246,7 +1246,7 @@ static __init int perf_event_ibs_init(void)if (ret) goto err_op; - ret =register_nmi_handler (NMI_LOCAL , perf_ibs_nmi_handler,0 ,"perf_ibs" ); + ret =register_nmi_handler (NMI_LOCAL , perf_ibs_nmi_handler,0 ,"perf_ibs" ,0 );if (ret) goto err_nmi;
arch/x86/events/core.c +2 -1
@@ -2100,7 +2100,8 @@ static int __init init_hw_perf_events(void) x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) -1 ;perf_events_lapic_init (); -register_nmi_handler (NMI_LOCAL , perf_event_nmi_handler,0 ,"PMI" ); + +register_nmi_handler (NMI_LOCAL , perf_event_nmi_handler,0 ,"PMI" ,NMI_SOURCE_VEC_PMI ); unconstrained = (struct event_constraint ) __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
arch/x86/include/asm/irq_vectors.h +21 -0
@@ -105,6 +105,27 @@#define NR_VECTORS 256 +/* +* The NMI senders specify the NMI source vector as an 8bit integer in their +* vector field with NMI delivery mode. A local APIC receiving an NMI will +* set the corresponding bit in a 16bit bitmask, which is accumulated until +* the NMI is delivered. +* When a sender didn't specify an NMI source vector the source vector will +* be 0, which will result in bit 0 of the bitmask being set. For out of +* bounds vectors >= 16 bit 0 will also be set. +* When bit 0 is set, system software must invoke all registered NMI handlers +* as if NMI source feature is not enabled. +*/ +#define NMI_SOURCE_VEC_UNKNOWN 0 +#define NMI_SOURCE_VEC_PMI 1/* PerfMon counters */ +#define NMI_SOURCE_VEC_IPI_BT 2/* CPU backtrace */ +#define NMI_SOURCE_VEC_IPI_MCE 3/* MCE injection */ +#define NMI_SOURCE_VEC_IPI_KGDB 4 +#define NMI_SOURCE_VEC_IPI_REBOOT 5/* Crash reboot */ +#define NMI_SOURCE_VEC_IPI_SMP_STOP 6/* Panic stop CPU */ +#define NMI_SOURCE_VEC_IPI_TEST 7/* For remote and local IPIs*/ +#define NR_NMI_SOURCE_VECTORS 8 +#ifdef CONFIG_X86_LOCAL_APIC #define FIRST_SYSTEM_VECTOR POSTED_MSI_NOTIFICATION_VECTOR#else
arch/x86/include/asm/nmi.h +3 -1
@@ -39,15 +39,17 @@ struct nmiaction {u64 max_duration;unsigned long flags;const char *name; +unsigned int source_vec; }; -#define register_nmi_handler(t, fn, fg, n, init...) \ +#define register_nmi_handler(t, fn, fg, n, src, init...) \ ({ \ static struct nmiaction init fn##_na = { \ .list = LIST_HEAD_INIT(fn##_na.list), \ .handler = (fn), \ .name = (n), \ .flags = (fg), \ + .source_vec = (src), \ }; \ __register_nmi_handler((t), &fn##_na); \ })
arch/x86/kernel/apic/hw_nmi.c +1 -1
@@ -54,7 +54,7 @@ NOKPROBE_SYMBOL(nmi_cpu_backtrace_handler);static int __init register_nmi_cpu_backtrace_handler (void ) {register_nmi_handler (NMI_LOCAL , nmi_cpu_backtrace_handler, -0 ,"arch_bt" ); +0 ,"arch_bt" ,NMI_SOURCE_VEC_IPI_BT );return 0 ; }early_initcall (register_nmi_cpu_backtrace_handler);
arch/x86/kernel/cpu/mce/inject.c +1 -1
@@ -769,7 +769,7 @@ static int __init inject_init(void)debugfs_init (); -register_nmi_handler (NMI_LOCAL , mce_raise_notify,0 ,"mce_notify" ); +register_nmi_handler (NMI_LOCAL , mce_raise_notify,0 ,"mce_notify" ,NMI_SOURCE_VEC_IPI_MCE );mce_register_injector_chain (&inject_nb);setup_inj_struct (&i_mce);
arch/x86/kernel/cpu/mshyperv.c +1 -1
@@ -486,7 +486,7 @@ static void __init ms_hyperv_init_platform(void) }register_nmi_handler (NMI_UNKNOWN , hv_nmi_unknown,NMI_FLAG_FIRST , -"hv_nmi_unknown" ); +"hv_nmi_unknown" ,0 );#endif #ifdef CONFIG_X86_IO_APIC
arch/x86/kernel/kgdb.c +2 -2
@@ -603,12 +603,12 @@ int kgdb_arch_init(void) goto out; retval =register_nmi_handler (NMI_LOCAL , kgdb_nmi_handler, -0 ,"kgdb" ); +0 ,"kgdb" ,NMI_SOURCE_VEC_IPI_KGDB );if (retval) goto out1; retval =register_nmi_handler (NMI_UNKNOWN , kgdb_nmi_handler, -0 ,"kgdb" ); +0 ,"kgdb" ,0 );if (retval) goto out2;
arch/x86/kernel/nmi.c +16 -0
@@ -86,6 +86,12 @@ static DEFINE_PER_CPU(struct nmi_stats, nmi_stats);static int ignore_nmis__read_mostly ; +/* +* Contains all actions registered by originators with source vector, +* excluding UNKNOWN NMI source vector 0. +*/ +static struct nmiaction *nmiaction_src_table[NR_NMI_SOURCE_VECTORS -1 ]; +int unknown_nmi_panic;/* * Prevent NMI reason port (0x61) being accessed simultaneously, can
@@ -163,6 +169,12 @@ static int nmi_handle(unsigned int type, struct pt_regs *regs) }NOKPROBE_SYMBOL (nmi_handle); +static inline bool use_nmi_source (unsigned int type,struct nmiaction *a) +{ +return (cpu_feature_enabled (X86_FEATURE_NMI_SOURCE ) && + type ==NMI_LOCAL && a->source_vec); +} +int __register_nmi_handler (unsigned int type,struct nmiaction *action) {struct nmi_desc *desc =nmi_to_desc (type);
@@ -173,6 +185,8 @@ int __register_nmi_handler(unsigned int type, struct nmiaction *action)raw_spin_lock_irqsave (&desc->lock, flags); +if (use_nmi_source (type, action)) +rcu_assign_pointer (nmiaction_src_table[action->source_vec], action);/* * Indicate if there are multiple registrations on the * internal NMI handler call chains (SERR and IO_CHECK).
@@ -210,6 +224,8 @@ void unregister_nmi_handler(unsigned int type, const char *name)if (!strcmp (n->name, name)) {WARN (in_nmi (),"Trying to free NMI (%s) from NMI context!\n" , n->name); +if (use_nmi_source (type, n)) +rcu_assign_pointer (nmiaction_src_table[n->source_vec],NULL );list_del_rcu (&n->list); found = n;break ;
arch/x86/kernel/nmi_selftest.c +3 -2
@@ -44,7 +44,7 @@ static void __init init_nmi_testsuite(void) {/* trap all the unknown NMIs we may generate */ register_nmi_handler (NMI_UNKNOWN , nmi_unk_cb,0 ,"nmi_selftest_unk" , -__initdata ); + 0, __initdata); }static void __init cleanup_nmi_testsuite (void )
@@ -67,7 +67,8 @@ static void __init test_nmi_ipi(struct cpumask *mask)unsigned long timeout;if (register_nmi_handler (NMI_LOCAL , test_nmi_ipi_callback, - NMI_FLAG_FIRST, "nmi_selftest", __initdata)) { + NMI_FLAG_FIRST, "nmi_selftest", NMI_SOURCE_VEC_IPI_TEST, +__initdata )) { nmi_fail =FAILURE ;return ; }
arch/x86/kernel/reboot.c +1 -1
@@ -910,7 +910,7 @@ void nmi_shootdown_cpus(nmi_shootdown_cb callback)atomic_set (&waiting_for_crash_ipi,num_online_cpus () -1 );/* Would it be better to replace the trap vector here? */ if (register_nmi_handler (NMI_LOCAL , crash_nmi_callback, -NMI_FLAG_FIRST ,"crash" )) +NMI_FLAG_FIRST ,"crash" ,NMI_SOURCE_VEC_IPI_REBOOT ))return ;/* Return what? */ /* * Ensure the new callback function is set before sending
arch/x86/kernel/smp.c +1 -1
@@ -143,7 +143,7 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_reboot)static int register_stop_handler (void ) {return register_nmi_handler (NMI_LOCAL , smp_stop_nmi_callback, -NMI_FLAG_FIRST ,"smp_stop" ); +NMI_FLAG_FIRST ,"smp_stop" ,NMI_SOURCE_VEC_IPI_SMP_STOP ); }static void native_stop_other_cpus (int wait)
arch/x86/platform/uv/uv_nmi.c +2 -2
@@ -1029,10 +1029,10 @@ static int uv_handle_nmi_ping(unsigned int reason, struct pt_regs *regs)static void uv_register_nmi_notifier (void ) { -if (register_nmi_handler (NMI_UNKNOWN , uv_handle_nmi,0 ,"uv" )) +if (register_nmi_handler (NMI_UNKNOWN , uv_handle_nmi,0 ,"uv" ,0 ))pr_warn ("UV: NMI handler failed to register\n" ); -if (register_nmi_handler (NMI_LOCAL , uv_handle_nmi_ping,0 ,"uvping" )) +if (register_nmi_handler (NMI_LOCAL , uv_handle_nmi_ping,0 ,"uvping" ,0 ))pr_warn ("UV: PING NMI handler failed to register\n" ); }
drivers/acpi/apei/ghes.c +1 -1
@@ -1318,7 +1318,7 @@ static void ghes_nmi_add(struct ghes *ghes) {mutex_lock (&ghes_list_mutex);if (list_empty (&ghes_nmi)) -register_nmi_handler (NMI_LOCAL , ghes_notify_nmi,0 ,"ghes" ); +register_nmi_handler (NMI_LOCAL , ghes_notify_nmi,0 ,"ghes" ,0 );list_add_rcu (&ghes->list, &ghes_nmi);mutex_unlock (&ghes_list_mutex); }
drivers/char/ipmi/ipmi_watchdog.c +1 -1
@@ -1272,7 +1272,7 @@ static void check_parms(void) }if (do_nmi && !nmi_handler_registered) { rv =register_nmi_handler (NMI_UNKNOWN , ipmi_nmi,0 , -"ipmi" ); +"ipmi" ,0 );if (rv) {pr_warn ("Can't register nmi handler\n" );return ;
drivers/edac/igen6_edac.c +1 -1
@@ -1321,7 +1321,7 @@ static int register_err_handler(void) } rc =register_nmi_handler (NMI_SERR , ecclog_nmi_handler, -0 ,IGEN6_NMI_NAME ); +0 ,IGEN6_NMI_NAME ,0 );if (rc) {igen6_printk (KERN_ERR ,"Failed to register NMI handler\n" );return rc;
drivers/watchdog/hpwdt.c +3 -3
@@ -242,13 +242,13 @@ static int hpwdt_init_nmi_decoding(struct pci_dev *dev)/* * Only one function can register for NMI_UNKNOWN */ - retval =register_nmi_handler (NMI_UNKNOWN , hpwdt_pretimeout,0 ,"hpwdt" ); + retval =register_nmi_handler (NMI_UNKNOWN , hpwdt_pretimeout,0 ,"hpwdt" ,0 );if (retval) goto error; - retval =register_nmi_handler (NMI_SERR , hpwdt_pretimeout,0 ,"hpwdt" ); + retval =register_nmi_handler (NMI_SERR , hpwdt_pretimeout,0 ,"hpwdt" ,0 );if (retval) goto error1; - retval =register_nmi_handler (NMI_IO_CHECK , hpwdt_pretimeout,0 ,"hpwdt" ); + retval =register_nmi_handler (NMI_IO_CHECK , hpwdt_pretimeout,0 ,"hpwdt" ,0 );if (retval) goto error2;
Signature · 1 line
2.25.1
PATCH 3/6 x86/irq: Factor out common NMI handling code
In reply to Jacob PanIn preparation for handling NMIs with explicit source reporting, factor out common code for reuse.
--- arch/x86/kernel/nmi.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-)
arch/x86/kernel/nmi.c +16 -12
@@ -135,6 +135,20 @@ static void nmi_check_duration(struct nmiaction *action, u64 duration) action->handler, duration, decimal_msecs); } +static inline int do_handle_nmi (struct nmiaction *a,struct pt_regs *regs,unsigned int type) +{ +int thishandled; +u64 delta; + + delta =sched_clock (); + thishandled = a->handler (type, regs); + delta =sched_clock () - delta; +trace_nmi_handler (a->handler, (int )delta, thishandled); +nmi_check_duration (a, delta); + +return thishandled; +} +static int nmi_handle (unsigned int type,struct pt_regs *regs) {struct nmi_desc *desc =nmi_to_desc (type);
@@ -149,18 +163,8 @@ static int nmi_handle(unsigned int type, struct pt_regs *regs)* can be latched at any given time. Walk the whole list * to handle those situations. */ -list_for_each_entry_rcu (a, &desc->head, list) { -int thishandled; -u64 delta; - - delta =sched_clock (); - thishandled = a->handler (type, regs); - handled += thishandled; - delta =sched_clock () - delta; -trace_nmi_handler (a->handler, (int )delta, thishandled); - -nmi_check_duration (a, delta); - } +list_for_each_entry_rcu (a, &desc->head, list) + handled +=do_handle_nmi (a, regs, type);rcu_read_unlock ();
Signature · 1 line
2.25.1
PATCH 4/6 x86/irq: Process nmi sources in NMI handler
In reply to Jacob PanWith NMI source reporting enabled, NMI handler can prioritize the handling of sources reported explicitly. If the source is unknown, then resume the existing processing flow. i.e. invoke all NMI handlers.
--- arch/x86/kernel/nmi.c | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)
arch/x86/kernel/nmi.c +48 -0
@@ -149,12 +149,60 @@ static inline int do_handle_nmi(struct nmiaction *a, struct pt_regs *regs, unsigreturn thishandled; } +static inline int nmi_handle_src (unsigned int type,struct pt_regs *regs) +{ +unsigned long source_bitmask; +struct nmiaction *a; +int handled =0 ; +int vec =1 ; + +if (!cpu_feature_enabled (X86_FEATURE_NMI_SOURCE ) || type !=NMI_LOCAL ) +return 0 ; + + source_bitmask =fred_event_data (regs); +if (!source_bitmask) { +pr_warn_ratelimited ("NMI received without source information!\n" ); +return 0 ; + } + +/* +* Per NMI source specification, there is no guarantee that a valid +* NMI vector is always delivered, even when the source specified +* one. It is software's responsibility to check all available NMI +* sources when bit 0 is set in the NMI source bitmap. i.e. we have +* to call every handler as if we have no NMI source. +* On the other hand, if we do get non-zero vectors, we know exactly +* what the sources are. So we only call the handlers with the bit set. +*/ +if (source_bitmask &BIT (NMI_SOURCE_VEC_UNKNOWN )) { +pr_warn_ratelimited ("NMI received with unknown source\n" ); +return 0 ; + } + +rcu_read_lock (); +/* Bit 0 is for unknown NMI sources, skip it. */ +for_each_set_bit_from (vec, &source_bitmask,NR_NMI_SOURCE_VECTORS ) { + a =rcu_dereference (nmiaction_src_table[vec]); +if (!a) { +pr_warn_ratelimited ("NMI received %d no handler" , vec); +continue ; + } + handled +=do_handle_nmi (a, regs, type); + } +rcu_read_unlock (); +return handled; +} +static int nmi_handle (unsigned int type,struct pt_regs *regs) {struct nmi_desc *desc =nmi_to_desc (type);struct nmiaction *a;int handled=0 ; + handled =nmi_handle_src (type, regs); +if (handled) +return handled; +rcu_read_lock ();/*
Signature · 1 line
2.25.1
PATCH 6/6 x86/irq: Enable NMI source on IPIs delivered as NMI
In reply to Jacob PanProgram designated NMI source vectors for all NMI delivered IPIs such that their handlers can be selectively invoked.
--- arch/x86/include/asm/irq_vectors.h | 10 ++++++++++ arch/x86/kernel/apic/hw_nmi.c | 3 ++- arch/x86/kernel/apic/ipi.c | 4 ++-- arch/x86/kernel/apic/local.h | 18 ++++++++++++------ arch/x86/kernel/cpu/mce/inject.c | 2 +- arch/x86/kernel/kgdb.c | 2 +- arch/x86/kernel/nmi_selftest.c | 2 +- arch/x86/kernel/reboot.c | 2 +- arch/x86/kernel/smp.c | 2 +- 9 files changed, 31 insertions(+), 14 deletions(-)
arch/x86/include/asm/irq_vectors.h +10 -0
@@ -126,6 +126,16 @@#define NMI_SOURCE_VEC_IPI_TEST 7/* For remote and local IPIs*/ #define NR_NMI_SOURCE_VECTORS 8 +/* +* When programming the local APIC, IDT NMI vector and NMI source vector +* are encoded in a single 32 bit variable. The top 16 bits contain +* the NMI source vector and the bottom 16 bits contain NMI_VECTOR (2) +* The top 16 bits are always zero when NMI source feature is not enabled +* or the caller does not use NMI source. +*/ +#define NMI_VECTOR_WITH_SOURCE (src) (NMI_VECTOR | (src << 16)) +#define NMI_SOURCE_VEC_MASK GENMASK(15, 0) +#ifdef CONFIG_X86_LOCAL_APIC #define FIRST_SYSTEM_VECTOR POSTED_MSI_NOTIFICATION_VECTOR#else
arch/x86/kernel/apic/hw_nmi.c +2 -1
@@ -20,6 +20,7 @@#include <linux/nmi.h> #include <linux/init.h> #include <linux/delay.h> +#include <asm/irq_vectors.h> #include "local.h"
@@ -33,7 +34,7 @@ u64 hw_nmi_get_sample_period(int watchdog_thresh)#ifdef arch_trigger_cpumask_backtracestatic void nmi_raise_cpu_backtrace (cpumask_t *mask) { -__apic_send_IPI_mask (mask,NMI_VECTOR ); +__apic_send_IPI_mask (mask,NMI_VECTOR_WITH_SOURCE (NMI_SOURCE_VEC_IPI_BT )); }void arch_trigger_cpumask_backtrace (const cpumask_t *mask,int exclude_cpu)
arch/x86/kernel/apic/ipi.c +2 -2
@@ -157,7 +157,7 @@ static void __default_send_IPI_shortcut(unsigned int shortcut, int vector)* issues where otherwise the system hangs when the panic CPU tries * to stop the others before launching the kdump kernel. */ -if (unlikely (vector ==NMI_VECTOR )) +if (unlikely (is_nmi_vector (vector)))apic_mem_wait_icr_idle_timeout ();else apic_mem_wait_icr_idle ();
@@ -174,7 +174,7 @@ void __default_send_IPI_dest_field(unsigned int dest_mask, int vector, unsigned int dest_mode) {/* See comment in __default_send_IPI_shortcut() */ -if (unlikely (vector ==NMI_VECTOR )) +if (unlikely (is_nmi_vector (vector)))apic_mem_wait_icr_idle_timeout ();else apic_mem_wait_icr_idle ();
arch/x86/kernel/apic/local.h +12 -6
@@ -12,6 +12,7 @@#include <asm/irq_vectors.h> #include <asm/apic.h> +#include <asm/nmi.h> /* X2APIC */ void __x2apic_send_IPI_dest (unsigned int apicid,int vector,unsigned int dest);
@@ -26,19 +27,24 @@ extern u32 x2apic_max_apicid;DECLARE_STATIC_KEY_FALSE (apic_use_ipi_shorthand); +static inline bool is_nmi_vector (int vector) +{ +return (vector &NMI_SOURCE_VEC_MASK ) ==NMI_VECTOR ; +} +static inline unsigned int __prepare_ICR (unsigned int shortcut,int vector,unsigned int dest) {unsigned int icr = shortcut | dest; -switch (vector) { -default : - icr |=APIC_DM_FIXED | vector; -break ; -case NMI_VECTOR : +if (is_nmi_vector (vector)) { icr |=APIC_DM_NMI ; -break ; +if (cpu_feature_enabled (X86_FEATURE_NMI_SOURCE )) + icr |= vector >>16 ; + }else { + icr |=APIC_DM_FIXED | vector; } +return icr; }
arch/x86/kernel/cpu/mce/inject.c +1 -1
@@ -270,7 +270,7 @@ static void __maybe_unused raise_mce(struct mce *m) mce_irq_ipi, NULL, 0);preempt_enable (); }else if (m->inject_flags &MCJ_NMI_BROADCAST ) -__apic_send_IPI_mask (mce_inject_cpumask,NMI_VECTOR ); +__apic_send_IPI_mask (mce_inject_cpumask,NMI_VECTOR_WITH_SOURCE (NMI_SOURCE_VEC_IPI_MCE )); } start = jiffies;while (!cpumask_empty (mce_inject_cpumask)) {
arch/x86/kernel/kgdb.c +1 -1
@@ -416,7 +416,7 @@ static void kgdb_disable_hw_debug(struct pt_regs *regs)*/ void kgdb_roundup_cpus (void ) { -apic_send_IPI_allbutself (NMI_VECTOR ); +apic_send_IPI_allbutself (NMI_VECTOR_WITH_SOURCE (NMI_SOURCE_VEC_IPI_KGDB )); }#endif
arch/x86/kernel/nmi_selftest.c +1 -1
@@ -76,7 +76,7 @@ static void __init test_nmi_ipi(struct cpumask *mask)/* sync above data before sending NMI */ wmb (); -__apic_send_IPI_mask (mask,NMI_VECTOR ); +__apic_send_IPI_mask (mask,NMI_VECTOR_WITH_SOURCE (NMI_SOURCE_VEC_IPI_TEST ));/* Don't wait longer than a second */ timeout =USEC_PER_SEC ;
arch/x86/kernel/reboot.c +1 -1
@@ -918,7 +918,7 @@ void nmi_shootdown_cpus(nmi_shootdown_cb callback)*/ wmb (); -apic_send_IPI_allbutself (NMI_VECTOR ); +apic_send_IPI_allbutself (NMI_VECTOR_WITH_SOURCE (NMI_SOURCE_VEC_IPI_REBOOT ));/* Kick CPUs looping in NMI context. */ WRITE_ONCE (crash_ipi_issued,1 );
arch/x86/kernel/smp.c +1 -1
@@ -217,7 +217,7 @@ static void native_stop_other_cpus(int wait)pr_emerg ("Shutting down cpus with NMI\n" );for_each_cpu (cpu, &cpus_stop_mask) -__apic_send_IPI (cpu,NMI_VECTOR ); +__apic_send_IPI (cpu,NMI_VECTOR_WITH_SOURCE (NMI_SOURCE_VEC_IPI_SMP_STOP )); }/* * Don't wait longer than 10 ms if the caller didn't
Signature · 1 line
2.25.1
PATCH 5/6 perf/x86: Enable NMI source reporting for perfmon
In reply to Jacob PanProgram the designated NMI source vector into the performance monitoring interrupt (PMI) of the local vector table. PMI handler will be directly invoked when its NMI is generated. This avoids the latency of calling all NMI handlers blindly.
--- arch/x86/events/core.c | 8 ++++++-- arch/x86/events/intel/core.c | 6 +++--- arch/x86/include/asm/apic.h | 1 + 3 files changed, 10 insertions(+), 5 deletions(-)
arch/x86/events/core.c +6 -2
@@ -46,6 +46,7 @@struct x86_pmu x86_pmu__read_mostly ;static struct pmu pmu; +u32 apic_perfmon_ctr =APIC_DM_NMI ; DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { .enabled =1 ,
@@ -1680,7 +1681,7 @@ int x86_pmu_handle_irq(struct pt_regs *regs)* This generic handler doesn't seem to have any issues where the * unmasking occurs so it was left at the top. */ -apic_write (APIC_LVTPC ,APIC_DM_NMI ); +apic_write (APIC_LVTPC , apic_perfmon_ctr);for (idx =0 ; idx < x86_pmu.num_counters; idx++) {if (!test_bit (idx, cpuc->active_mask))
@@ -1723,7 +1724,10 @@ void perf_events_lapic_init(void)/* * Always use NMI for PMU */ -apic_write (APIC_LVTPC ,APIC_DM_NMI ); +if (cpu_feature_enabled (X86_FEATURE_NMI_SOURCE )) + apic_perfmon_ctr |=NMI_SOURCE_VEC_PMI ; + +apic_write (APIC_LVTPC , apic_perfmon_ctr); } static int
arch/x86/events/intel/core.c +3 -3
@@ -3093,7 +3093,7 @@ static int intel_pmu_handle_irq(struct pt_regs *regs)* NMI handler. */ if (!late_ack && !mid_ack) -apic_write (APIC_LVTPC ,APIC_DM_NMI ); +apic_write (APIC_LVTPC , apic_perfmon_ctr);intel_bts_disable_local (); cpuc->enabled =0 ;__intel_pmu_disable_all (true);
@@ -3130,7 +3130,7 @@ static int intel_pmu_handle_irq(struct pt_regs *regs) done:if (mid_ack) -apic_write (APIC_LVTPC ,APIC_DM_NMI ); +apic_write (APIC_LVTPC , apic_perfmon_ctr);/* Only restore PMU state when it's active. See x86_pmu_disable(). */ cpuc->enabled = pmu_enabled;if (pmu_enabled)
@@ -3143,7 +3143,7 @@ static int intel_pmu_handle_irq(struct pt_regs *regs)* Haswell CPUs. */ if (late_ack) -apic_write (APIC_LVTPC ,APIC_DM_NMI ); +apic_write (APIC_LVTPC , apic_perfmon_ctr);return handled; }
arch/x86/include/asm/apic.h +1 -0
@@ -59,6 +59,7 @@ extern int local_apic_timer_c2_ok;extern bool apic_is_disabled;extern unsigned int lapic_timer_period; +extern u32 apic_perfmon_ctr;extern enum apic_intr_mode_id apic_intr_mode;enum apic_intr_mode_id {
Signature · 1 line
2.25.1
H. Peter Anvin
reviewerRe: [PATCH 1/6] x86/irq: Add enumeration of NMI source reporting CPU feature
In reply to Jacob Panarch/x86/kernel/cpu/cpuid-deps.c static const struct cpuid_dep cpuid_deps[] = { · line 90
Quoted 12 lines · Jacob Pan
arch/x86/kernel/cpu/cpuid-deps.c +1
-0@@ -84,6 +84,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_SHSTK, X86_FEATURE_XSAVES }, { X86_FEATURE_FRED, X86_FEATURE_LKGS }, { X86_FEATURE_FRED, X86_FEATURE_WRMSRNS }, + { X86_FEATURE_FRED, X86_FEATURE_NMI_SOURCE}, {} };
This is incorrect. FRED does *not* inherently depend on NMI_SOURCE; the dependency is the reverse, but since it *also* depends on FRED being dynamically enabled, there is no need to add it to the static table; the dynamic test:
Quoted 6 lines
arch/x86/kernel/traps.c +0
-0@@ -1427,8 +1427,10 @@ early_param("fred", fred_setup);
arch/x86/kernel/traps.c early_param("fred", fred_setup); · line 1436
Quoted 10 lines
arch/x86/kernel/traps.c +3
-1void __init trap_init (void ) { -if (cpu_feature_enabled (X86_FEATURE_FRED ) && !enable_fred) +if (cpu_feature_enabled (X86_FEATURE_FRED ) && !enable_fred) {setup_clear_cpu_cap (X86_FEATURE_FRED ); +setup_clear_cpu_cap (X86_FEATURE_NMI_SOURCE ); + }/* Init cpu_entry_area before IST entries are set up */ setup_cpu_entry_areas ();
... suffices just fine on its own.
-hpa
H. Peter Anvin
reviewerRe: [PATCH 2/6] x86/irq: Extend NMI handler registration interface to include source
In reply to Jacob PanQuoted 19 lines · Jacob Pan
arch/x86/include/asm/irq_vectors.h +11
-0@@ -105,6 +105,27 @@#define NR_VECTORS 256 +/* +* The NMI senders specify the NMI source vector as an 8bit integer in their +* vector field with NMI delivery mode. A local APIC receiving an NMI will +* set the corresponding bit in a 16bit bitmask, which is accumulated until +* the NMI is delivered. +* When a sender didn't specify an NMI source vector the source vector will +* be 0, which will result in bit 0 of the bitmask being set. For out of +* bounds vectors >= 16 bit 0 will also be set. +* When bit 0 is set, system software must invoke all registered NMI handlers +* as if NMI source feature is not enabled. +*/
arch/x86/include/asm/irq_vectors.h line 128
Quoted 10 lines
arch/x86/include/asm/irq_vectors.h +10
-0+#define NMI_SOURCE_VEC_UNKNOWN 0 +#define NMI_SOURCE_VEC_PMI 1/* PerfMon counters */ +#define NMI_SOURCE_VEC_IPI_BT 2/* CPU backtrace */ +#define NMI_SOURCE_VEC_IPI_MCE 3/* MCE injection */ +#define NMI_SOURCE_VEC_IPI_KGDB 4 +#define NMI_SOURCE_VEC_IPI_REBOOT 5/* Crash reboot */ +#define NMI_SOURCE_VEC_IPI_SMP_STOP 6/* Panic stop CPU */ +#define NMI_SOURCE_VEC_IPI_TEST 7/* For remote and local IPIs*/ +#define NR_NMI_SOURCE_VECTORS 8 +
I would avoid using vector 2; it is at least remotely possible that some third-party chipset sends NMI messages with a hardcoded vector of 2. As long as you don't actively need that slot, it is better to avoid it.
Even better is to set the LINT1 (= external NMI) vector to 2 and treat bit 2 as "other"; use vector 2 for anything that you don't have an explicit vector for. You can treat a received bit 2 the same as bit 0, except that you can explicitly trust that any event assigned an explicit vector number is *NOT* triggered and so do not need to be polled.
I would also recommend sorting these in order of decreasing importance (other than 0 and 2). Although the current intent is there to be 16 vectors indefinitely, defensive design would be to consider that number to be potentially variable (up to 64, obviously). Since any out-of-range vector will set bit 0, the code will still Just Work[TM].
-hpa
H. Peter Anvin
reviewerRe: [PATCH 4/6] x86/irq: Process nmi sources in NMI handler
In reply to Jacob Panarch/x86/kernel/nmi.c static inline int do_handle_nmi(struct nmiaction *a, struct pt_regs *regs, unsig · line 189
Quoted 9 lines · Jacob Pan
arch/x86/kernel/nmi.c +9
-0+ +rcu_read_lock (); +/* Bit 0 is for unknown NMI sources, skip it. */ +for_each_set_bit_from (vec, &source_bitmask,NR_NMI_SOURCE_VECTORS ) { + a =rcu_dereference (nmiaction_src_table[vec]); +if (!a) { +pr_warn_ratelimited ("NMI received %d no handler" , vec); +continue ; + }
In this case, you should assume some chipset hardware or VMM is giving you garbage in the event bitmask, and treat it as if bit 0 were set.
-hpa
H. Peter Anvin
reviewerRe: [PATCH 4/6] x86/irq: Process nmi sources in NMI handler
In reply to Jacob PanQuoted 5 lines · Jacob Pan
arch/x86/kernel/nmi.c +5
-0+ +/* +* Per NMI source specification, there is no guarantee that a valid +* NMI vector is always delivered, even when the source specified +* one. It is software's responsibility to check all available NMI
arch/x86/kernel/nmi.c static inline int do_handle_nmi(struct nmiaction *a, struct pt_regs *regs, unsig · line 181
Quoted 10 lines
arch/x86/kernel/nmi.c +10
-0+* sources when bit 0 is set in the NMI source bitmap. i.e. we have +* to call every handler as if we have no NMI source. +* On the other hand, if we do get non-zero vectors, we know exactly +* what the sources are. So we only call the handlers with the bit set. +*/ +if (source_bitmask &BIT (NMI_SOURCE_VEC_UNKNOWN )) { +pr_warn_ratelimited ("NMI received with unknown source\n" ); +return 0 ; + } +
Note: if bit 0 is set, you can process any other bits first (on the general assumption that if you bother with NMI source then those events are performance sensitive), and you could even exclude them from the poll. This is an optimization, and what you have here is correct from a functional point of view.
arch/x86/kernel/nmi.c static inline int do_handle_nmi(struct nmiaction *a, struct pt_regs *regs, unsig · line 166
Quoted 5 lines
arch/x86/kernel/nmi.c +5
-0+ source_bitmask =fred_event_data (regs); +if (!source_bitmask) { +pr_warn_ratelimited ("NMI received without source information!\n" ); +return 0 ; + }
If the event data word is 0, it probably should be treated as a *permanent* failure, as it is a Should Not Happen[TM] situation, and means there is an implementation (or, perhaps more likely, virtualization!) bug, and as such it may not be safe to trust the NMI source information in the future.
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (!cpu_feature_enabled(X86_FEATURE_NMI_SOURCE) || type != NMI_LOCAL) + return 0;
I'm not sure I understand why you are requiring type to be NMI_LOCAL here?
-hpa
kernel test robot
build botRe: [PATCH 5/6] perf/x86: Enable NMI source reporting for perfmon
In reply to Jacob Pankernel test robot noticed the following build errors:
Hi Jacob,
kernel test robot noticed the following build errors:
[auto build test ERROR on perf-tools-next/perf-tools-next]
[also build test ERROR on perf-tools/perf-tools linus/master v6.10-rc1 next-20240529] [cannot apply to tip/perf/core tip/x86/core acme/perf/core]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Jacob-Pan/x86-irq-Add-enumeration-of-NMI-source-reporting-CPU-feature/20240530-043112 base: https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git perf-tools-next patch link: https://lore.kernel.org/r/20240529203325.3039243-6-jacob.jun.pan%40linux.intel.com patch subject: [PATCH 5/6] perf/x86: Enable NMI source reporting for perfmon config: i386-randconfig-012-20240530 (https://download.01.org/0day-ci/archive/20240530/[email protected]/config) compiler: gcc-13 (Ubuntu 13.2.0-4ubuntu3) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240530/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
arch/x86/events/intel/core.c: In function 'intel_pmu_handle_irq':
>> arch/x86/events/intel/core.c:3096:40: error: 'apic_perfmon_ctr' undeclared (first use in this function)
3096 | apic_write(APIC_LVTPC, apic_perfmon_ctr);
| ^~~~~~~~~~~~~~~~ arch/x86/events/intel/core.c:3096:40: note: each undeclared identifier is reported only once for each function it appears in
vim +/apic_perfmon_ctr +3096 arch/x86/events/intel/core.c
3067
3068 /*
3069 * This handler is triggered by the local APIC, so the APIC IRQ handling
3070 * rules apply:
3071 */
3072 static int intel_pmu_handle_irq(struct pt_regs *regs)
3073 {
3074 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
3075 bool late_ack = hybrid_bit(cpuc->pmu, late_ack);
3076 bool mid_ack = hybrid_bit(cpuc->pmu, mid_ack);
3077 int loops;
3078 u64 status;
3079 int handled;
3080 int pmu_enabled;
3081
3082 /*
3083 * Save the PMU state.
3084 * It needs to be restored when leaving the handler.
3085 */
3086 pmu_enabled = cpuc->enabled;
3087 /*
3088 * In general, the early ACK is only applied for old platforms.
3089 * For the big core starts from Haswell, the late ACK should be
3090 * applied.
3091 * For the small core after Tremont, we have to do the ACK right
3092 * before re-enabling counters, which is in the middle of the
3093 * NMI handler.
3094 */
3095 if (!late_ack && !mid_ack)
> 3096 apic_write(APIC_LVTPC, apic_perfmon_ctr);
3097 intel_bts_disable_local();
3098 cpuc->enabled = 0;
3099 __intel_pmu_disable_all(true);
3100 handled = intel_pmu_drain_bts_buffer();
3101 handled += intel_bts_interrupt();
3102 status = intel_pmu_get_status();
3103 if (!status)
3104 goto done;
3105
3106 loops = 0;
3107 again:
3108 intel_pmu_lbr_read();
3109 intel_pmu_ack_status(status);
3110 if (++loops > 100) {
3111 static bool warned;
3112
3113 if (!warned) {
3114 WARN(1, "perfevents: irq loop stuck!\n");
3115 perf_event_print_debug();
3116 warned = true;
3117 }
3118 intel_pmu_reset();
3119 goto done;
3120 }
3121
3122 handled += handle_pmi_common(regs, status);
3123
3124 /*
3125 * Repeat if there is more work to be done:
3126 */
3127 status = intel_pmu_get_status();
3128 if (status)
3129 goto again;
3130
3131 done:
3132 if (mid_ack)
3133 apic_write(APIC_LVTPC, apic_perfmon_ctr);
3134 /* Only restore PMU state when it's active. See x86_pmu_disable(). */
3135 cpuc->enabled = pmu_enabled;
3136 if (pmu_enabled)
3137 __intel_pmu_enable_all(0, true);
3138 intel_bts_enable_local();
3139
3140 /*
3141 * Only unmask the NMI after the overflow counters
3142 * have been reset. This avoids spurious NMIs on
3143 * Haswell CPUs.
3144 */
3145 if (late_ack)
3146 apic_write(APIC_LVTPC, apic_perfmon_ctr);
3147 return handled;
3148 }
3149
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH 2/6] x86/irq: Extend NMI handler registration interface to include source
In reply to Jacob Pankernel test robot noticed the following build warnings:
Hi Jacob,
kernel test robot noticed the following build warnings:
[auto build test WARNING on perf-tools-next/perf-tools-next]
[also build test WARNING on perf-tools/perf-tools linus/master v6.10-rc1 next-20240529] [cannot apply to tip/perf/core tip/x86/core acme/perf/core]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Jacob-Pan/x86-irq-Add-enumeration-of-NMI-source-reporting-CPU-feature/20240530-043112 base: https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git perf-tools-next patch link: https://lore.kernel.org/r/20240529203325.3039243-3-jacob.jun.pan%40linux.intel.com patch subject: [PATCH 2/6] x86/irq: Extend NMI handler registration interface to include source config: x86_64-randconfig-122-20240530 (https://download.01.org/0day-ci/archive/20240530/[email protected]/config) compiler: gcc-13 (Ubuntu 13.2.0-4ubuntu3) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240530/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
sparse warnings: (new ones prefixed by >>)
>> arch/x86/kernel/nmi.c:189:17: sparse: sparse: incompatible types in comparison expression (different address spaces):
arch/x86/kernel/nmi.c:189:17: sparse: struct nmiaction [noderef] __rcu *
arch/x86/kernel/nmi.c:189:17: sparse: struct nmiaction *
arch/x86/kernel/nmi.c:228:33: sparse: sparse: incompatible types in comparison expression (different address spaces):
arch/x86/kernel/nmi.c:228:33: sparse: struct nmiaction [noderef] __rcu *
arch/x86/kernel/nmi.c:228:33: sparse: struct nmiaction *
arch/x86/kernel/nmi.c: note: in included file (through include/linux/smp.h, include/linux/lockdep.h, include/linux/spinlock.h):
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true
vim +189 arch/x86/kernel/nmi.c
177
178 int __register_nmi_handler(unsigned int type, struct nmiaction *action)
179 {
180 struct nmi_desc *desc = nmi_to_desc(type);
181 unsigned long flags;
182
183 if (WARN_ON_ONCE(!action->handler || !list_empty(&action->list)))
184 return -EINVAL;
185
186 raw_spin_lock_irqsave(&desc->lock, flags);
187
188 if (use_nmi_source(type, action))
> 189 rcu_assign_pointer(nmiaction_src_table[action->source_vec], action);
190 /*
191 * Indicate if there are multiple registrations on the
192 * internal NMI handler call chains (SERR and IO_CHECK).
193 */
194 WARN_ON_ONCE(type == NMI_SERR && !list_empty(&desc->head));
195 WARN_ON_ONCE(type == NMI_IO_CHECK && !list_empty(&desc->head));
196
197 /*
198 * some handlers need to be executed first otherwise a fake
199 * event confuses some handlers (kdump uses this flag)
200 */
201 if (action->flags & NMI_FLAG_FIRST)
202 list_add_rcu(&action->list, &desc->head);
203 else
204 list_add_tail_rcu(&action->list, &desc->head);
205
206 raw_spin_unlock_irqrestore(&desc->lock, flags);
207 return 0;
208 }
209 EXPORT_SYMBOL(__register_nmi_handler);
210
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH 5/6] perf/x86: Enable NMI source reporting for perfmon
In reply to Jacob Pankernel test robot noticed the following build errors:
Hi Jacob,
kernel test robot noticed the following build errors:
[auto build test ERROR on perf-tools-next/perf-tools-next]
[also build test ERROR on perf-tools/perf-tools linus/master v6.10-rc1 next-20240529] [cannot apply to tip/perf/core tip/x86/core acme/perf/core]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Jacob-Pan/x86-irq-Add-enumeration-of-NMI-source-reporting-CPU-feature/20240530-043112 base: https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git perf-tools-next patch link: https://lore.kernel.org/r/20240529203325.3039243-6-jacob.jun.pan%40linux.intel.com patch subject: [PATCH 5/6] perf/x86: Enable NMI source reporting for perfmon config: i386-randconfig-051-20240530 (https://download.01.org/0day-ci/archive/20240530/[email protected]/config) compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240530/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> arch/x86/events/intel/core.c:3096:26: error: use of undeclared identifier 'apic_perfmon_ctr'
3096 | apic_write(APIC_LVTPC, apic_perfmon_ctr);
| ^
arch/x86/events/intel/core.c:3133:26: error: use of undeclared identifier 'apic_perfmon_ctr'
3133 | apic_write(APIC_LVTPC, apic_perfmon_ctr);
| ^
arch/x86/events/intel/core.c:3146:26: error: use of undeclared identifier 'apic_perfmon_ctr'
3146 | apic_write(APIC_LVTPC, apic_perfmon_ctr);
| ^3 errors generated.
vim +/apic_perfmon_ctr +3096 arch/x86/events/intel/core.c
3067
3068 /*
3069 * This handler is triggered by the local APIC, so the APIC IRQ handling
3070 * rules apply:
3071 */
3072 static int intel_pmu_handle_irq(struct pt_regs *regs)
3073 {
3074 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
3075 bool late_ack = hybrid_bit(cpuc->pmu, late_ack);
3076 bool mid_ack = hybrid_bit(cpuc->pmu, mid_ack);
3077 int loops;
3078 u64 status;
3079 int handled;
3080 int pmu_enabled;
3081
3082 /*
3083 * Save the PMU state.
3084 * It needs to be restored when leaving the handler.
3085 */
3086 pmu_enabled = cpuc->enabled;
3087 /*
3088 * In general, the early ACK is only applied for old platforms.
3089 * For the big core starts from Haswell, the late ACK should be
3090 * applied.
3091 * For the small core after Tremont, we have to do the ACK right
3092 * before re-enabling counters, which is in the middle of the
3093 * NMI handler.
3094 */
3095 if (!late_ack && !mid_ack)
> 3096 apic_write(APIC_LVTPC, apic_perfmon_ctr);
3097 intel_bts_disable_local();
3098 cpuc->enabled = 0;
3099 __intel_pmu_disable_all(true);
3100 handled = intel_pmu_drain_bts_buffer();
3101 handled += intel_bts_interrupt();
3102 status = intel_pmu_get_status();
3103 if (!status)
3104 goto done;
3105
3106 loops = 0;
3107 again:
3108 intel_pmu_lbr_read();
3109 intel_pmu_ack_status(status);
3110 if (++loops > 100) {
3111 static bool warned;
3112
3113 if (!warned) {
3114 WARN(1, "perfevents: irq loop stuck!\n");
3115 perf_event_print_debug();
3116 warned = true;
3117 }
3118 intel_pmu_reset();
3119 goto done;
3120 }
3121
3122 handled += handle_pmi_common(regs, status);
3123
3124 /*
3125 * Repeat if there is more work to be done:
3126 */
3127 status = intel_pmu_get_status();
3128 if (status)
3129 goto again;
3130
3131 done:
3132 if (mid_ack)
3133 apic_write(APIC_LVTPC, apic_perfmon_ctr);
3134 /* Only restore PMU state when it's active. See x86_pmu_disable(). */
3135 cpuc->enabled = pmu_enabled;
3136 if (pmu_enabled)
3137 __intel_pmu_enable_all(0, true);
3138 intel_bts_enable_local();
3139
3140 /*
3141 * Only unmask the NMI after the overflow counters
3142 * have been reset. This avoids spurious NMIs on
3143 * Haswell CPUs.
3144 */
3145 if (late_ack)
3146 apic_write(APIC_LVTPC, apic_perfmon_ctr);
3147 return handled;
3148 }
3149
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH 4/6] x86/irq: Process nmi sources in NMI handler
In reply to H. Peter AnvinHi Peter,
Quoted 21 lines · H. Peter Anvin
Quoted 20 lines · Jacob Pan
arch/x86/kernel/nmi.c +3
-0+ +/* +* Per NMI source specification, there is no guarantee that a valid
(quoted diff) +1
-0+ * NMI vector is always delivered, even when the sourcespecified
(quoted diff) +1
-0+ * one. It is software's responsibility to check all availableNMI
(quoted diff) +1
-0+ * sources when bit 0 is set in the NMI source bitmap. i.e. wehave
(quoted diff) +2
-0+ * to call every handler as if we have no NMI source. + * On the other hand, if we do get non-zero vectors, we knowexactly
(quoted diff) +1
-0+ * what the sources are. So we only call the handlers with thebit set.
arch/x86/kernel/nmi.c +3
-0+*/ +if (source_bitmask &BIT (NMI_SOURCE_VEC_UNKNOWN )) { + pr_warn_ratelimited("NMI received with unknownsource\n");
(quoted diff) +1
-0+ return 0;
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ } +Note: if bit 0 is set, you can process any other bits first (on the general assumption that if you bother with NMI source then those events are performance sensitive), and you could even exclude them from the poll. This is an optimization, and what you have here is correct from a functional point of view.
Yes, it is a good optimization but also a rare case that bit 0 is set. no?
Quoted 4 lines
Quoted 4 lines
arch/x86/kernel/nmi.c +3
-0+ source_bitmask =fred_event_data (regs); +if (!source_bitmask) { + pr_warn_ratelimited("NMI received without sourceinformation!\n");
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ return 0; + }If the event data word is 0, it probably should be treated as a *permanent* failure, as it is a Should Not Happen[TM] situation, and means there is an implementation (or, perhaps more likely, virtualization!) bug, and as such it may not be safe to trust the NMI source information in the future.
Good point, I will add a flag to permanently disable NMI source reporting for this boot cycle if that happens.
Quoted 6 lines
Review
Quoted 3 lines
(quoted diff) +1
-0+ if (!cpu_feature_enabled(X86_FEATURE_NMI_SOURCE) || type !=
NMI_LOCAL)(quoted diff) +1
-0+ return 0;I'm not sure I understand why you are requiring type to be
NMI_LOCALhere?
It is just for this current implementation I am not including external NMIs. AFAIK, there is no users, i.e. no device MSIs delivered as NMI. I saw effort trying to make HPET NMI watchdog but not materialized.
Thanks,
Jacob
Re: [PATCH 1/6] x86/irq: Add enumeration of NMI source reporting CPU feature
In reply to H. Peter AnvinHi Peter,
Quoted 11 lines · H. Peter Anvin
Quoted 10 lines · Jacob Pan
(quoted diff) +0
-0
b/arch/x86/kernel/cpu/cpuid-deps.cindexb7d9f530ae16..3f1a1a1961fa100644 ---a/arch/x86/kernel/cpu/cpuid-deps.c
+++b/arch/x86/kernel/cpu/cpuid-deps.c(quoted diff) +1
-0@@ -84,6 +84,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_SHSTK, X86_FEATURE_XSAVES }, { X86_FEATURE_FRED, X86_FEATURE_LKGS }, { X86_FEATURE_FRED, X86_FEATURE_WRMSRNS }, + { X86_FEATURE_FRED,
Quoted 9 lines
Review static const struct cpuid_dep cpuid_deps[] = { · line 90
Quoted 3 lines
(quoted diff) +0
-0X86_FEATURE_NMI_SOURCE}, {} };This is incorrect. FRED does *not* inherently depend on
NMI_SOURCE; the dependency is the reverse, but since it *also* depends on FRED being dynamically enabled, there is no need to add it to the static table; the dynamic test:
My misunderstanding of the dependency table, thanks for pointing it out. Will remove.
Quoted 14 lines
Quoted 14 lines
arch/x86/kernel/traps.c +3
-1@@ -1427,8 +1427,10 @@ early_param("fred", fred_setup);void __init trap_init (void ) { -if (cpu_feature_enabled (X86_FEATURE_FRED ) && !enable_fred) +if (cpu_feature_enabled (X86_FEATURE_FRED ) && !enable_fred) {setup_clear_cpu_cap (X86_FEATURE_FRED ); +setup_clear_cpu_cap (X86_FEATURE_NMI_SOURCE ); + }
Quoted 4 lines
arch/x86/kernel/traps.c early_param("fred", fred_setup); · line 1436
Quoted 2 lines
arch/x86/kernel/traps.c +0
-0/* Init cpu_entry_area before IST entries are set up */ setup_cpu_entry_areas ();... suffices just fine on its own.
I am not following, do you mean checking for FRED is sufficient for NMI source? I think it works since NMI source cannot be disabled if FRED is on. Just want to use the architectural CPUID bits to the fullest.
Thanks,
Jacob
Re: [PATCH 2/6] x86/irq: Extend NMI handler registration interface to include source
In reply to H. Peter AnvinHi H.,
Quoted 32 lines · H. Peter Anvin
Quoted 31 lines · Jacob Pan
(quoted diff) +0
-0
b/arch/x86/include/asm/irq_vectors.hindex13aea8fc3d45..b8388bc00cde100644 ---a/arch/x86/include/asm/irq_vectors.h
+++b/arch/x86/include/asm/irq_vectors.harch/x86/include/asm/irq_vectors.h +2
-0@@ -105,6 +105,27 @@#define NR_VECTORS 256 +/* +* The NMI senders specify the NMI source vector as an 8bit integer in their
+ * vector field with NMI delivery mode. A local APIC receiving an NMI will
+ * set the corresponding bit in a 16bit bitmask, which is accumulated until
+ * the NMI is delivered.
+ * When a sender didn't specify an NMI source vector the source vector will
+ * be 0, which will result in bit 0 of the bitmask being set. For out of
+ * bounds vectors >= 16 bit 0 will also be set.
+ * When bit 0 is set, system software must invoke all registered NMI handlers
+ * as if NMI source feature is not enabled.
+ */(quoted diff) +2
-0+#define NMI_SOURCE_VEC_UNKNOWN 0 +#define NMI_SOURCE_VEC_PMI 1 /* PerfMon counters*/ +#define
NMI_SOURCE_VEC_IPI_BT2 /* CPU
backtrace */ +#defineNMI_SOURCE_VEC_IPI_MCE3 /*
MCE injection */ +#defineNMI_SOURCE_VEC_IPI_KGDB4
+#defineNMI_SOURCE_VEC_IPI_REBOOT5 /* Crash reboot */
Quoted 9 lines
Quoted 4 lines
+#define
NMI_SOURCE_VEC_IPI_SMP_STOP6 /* Panic stop CPU
*/ +#defineNMI_SOURCE_VEC_IPI_TEST7 /* For
remote and local IPIs*/ +#defineNR_NMI_SOURCE_VECTORS8
+I would avoid using vector 2; it is at least remotely possible that some third-party chipset sends NMI messages with a hardcoded vector of 2. As long as you don't actively need that slot, it is better to avoid it.
I thought about that as well, then I dropped that when I limit NMIS to local interrupts. Not aware the hardcoded case as you mentioned.
I will reserve vector #2 to avoid future headache.
Quoted 6 lines
Even better is to set the LINT1 (= external NMI) vector to 2 and treat bit 2 as "other"; use vector 2 for anything that you don't have an explicit vector for. You can treat a received bit 2 the same as bit 0, except that you can explicitly trust that any event assigned an explicit vector number is *NOT* triggered and so do not need to be polled.
Make sense, will set LINT1 vector to 2 and avoid polling known sources. That is another optimization.
Quoted 5 lines
I would also recommend sorting these in order of decreasing importance (other than 0 and 2). Although the current intent is there to be 16 vectors indefinitely, defensive design would be to consider that number to be potentially variable (up to 64, obviously). Since any out-of-range vector will set bit 0, the code will still Just Work[TM].
Will sort them.
Thanks,
Jacob
Re: [PATCH 4/6] x86/irq: Process nmi sources in NMI handler
In reply to H. Peter AnvinHi Peter,
Quoted 16 lines · H. Peter Anvin
Review
Quoted 11 lines · Jacob Pan
arch/x86/kernel/nmi.c +4
-0+ +rcu_read_lock (); +/* Bit 0 is for unknown NMI sources, skip it. */ + for_each_set_bit_from(vec, &source_bitmask,
NR_NMI_SOURCE_VECTORS) {arch/x86/kernel/nmi.c +3
-0+ a =rcu_dereference (nmiaction_src_table[vec]); +if (!a) { + pr_warn_ratelimited("NMI received %d nohandler", vec);
(quoted diff) +2
-0+ continue; + }In this case, you should assume some chipset hardware or VMM is giving you garbage in the event bitmask, and treat it as if bit 0 were set.
right, should return 0 and poll all handlers.
Thanks,
Jacob
Re: [PATCH 1/6] x86/irq: Add enumeration of NMI source reporting CPU feature
In reply to Jacob PanHi Jacob,
Quoted 42 lines · Jacob Pan
Hi Peter,
Quoted 11 lines · H. Peter Anvin
Quoted 10 lines · Jacob Pan
(quoted diff) +0
-0
b/arch/x86/kernel/cpu/cpuid-deps.cindexb7d9f530ae16..3f1a1a1961fa100644 ---a/arch/x86/kernel/cpu/cpuid-deps.c
+++b/arch/x86/kernel/cpu/cpuid-deps.c(quoted diff) +1
-0@@ -84,6 +84,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_SHSTK, X86_FEATURE_XSAVES }, { X86_FEATURE_FRED, X86_FEATURE_LKGS }, { X86_FEATURE_FRED, X86_FEATURE_WRMSRNS }, + { X86_FEATURE_FRED,Quoted 9 lines
Review static const struct cpuid_dep cpuid_deps[] = { · line 90
Quoted 3 lines
(quoted diff) +0
-0X86_FEATURE_NMI_SOURCE}, {} };This is incorrect. FRED does *not* inherently depend on
NMI_SOURCE; the dependency is the reverse, but since it *also* depends on FRED being dynamically enabled, there is no need to add it to the static table; the dynamic test:My misunderstanding of the dependency table, thanks for pointing it out. Will remove.
Quoted 14 lines
Quoted 14 lines
arch/x86/kernel/traps.c +3
-1@@ -1427,8 +1427,10 @@ early_param("fred", fred_setup);void __init trap_init (void ) { -if (cpu_feature_enabled (X86_FEATURE_FRED ) && !enable_fred) +if (cpu_feature_enabled (X86_FEATURE_FRED ) && !enable_fred) {setup_clear_cpu_cap (X86_FEATURE_FRED ); +setup_clear_cpu_cap (X86_FEATURE_NMI_SOURCE ); + }
Quoted 8 lines
Quoted 4 lines
arch/x86/kernel/traps.c early_param("fred", fred_setup); · line 1436
Quoted 2 lines
arch/x86/kernel/traps.c +0
-0/* Init cpu_entry_area before IST entries are set up */ setup_cpu_entry_areas ();... suffices just fine on its own.
I am not following, do you mean checking for FRED is sufficient for NMI source? I think it works since NMI source cannot be disabled if FRED is on. Just want to use the architectural CPUID bits to the fullest.
Nevermind, I got it now, will keep the dynamic test.
Thanks,
Jacob