Skip to contentSkip to conversation

irq/arm: Implement arch_trigger_all_cpu_backtrace

PATCH Superseded Daniel Thompson 6 participants 40 messages Mar 24, 2015 – Jul 2, 2015

Subsystems (5)

v19 of 23 Superseded Latest is v23 →

lore
Share & tools
lore
#0 lore raw

v19 cover irq/arm: Implement arch_trigger_all_cpu_backtrace

Jason/Thomas:
This would be a resend except Steven Rostedt noticed a redundant memory barrier I had copied from the x86 code. The redundant barrier is now removed and there are no other changes since the code was posted a fortnight ago. Any chance of taking the first five of these patches via the irqchip route? The x86 patch has an ack from Ingo, printk has no explicit maintainer and I've done plenty of bisectability tests on the patchset so leaving the last patch for the next dev. cycle should be no trouble.

This patchset modifies the GIC driver to allow it, on supported platforms, to route IPI interrupts to FIQ. It then uses this feature to implement arch_trigger_all_cpu_backtrace for arm. In order to neatly bring in the changes for the arm we also rearrange some of the existing x86 NMI code to make it architecture neutral.

The patchset http://thread.gmane.org/gmane.linux.kernel/1897765 , which makes sched_clock() NMI/FIQ-safe, should be treated as a prerequisite for the sixth and final patch in the series (which enables the feature on ARM). Although sched_clock() is not called directly by any of the code that runs from a FIQ handler it is possible for sched_clock() to be called indirectly when the function tracer is enabled.

The patches have been runtime tested on two systems capable of supporting FIQ (Freescale i.MX6 and STiH416) and two that do not (vexpress-a9 and Qualcomm Snapdragon 600), the changes to the x86 logic were tested on qemu and all patches have been compile tested on x86, arm and arm64.

Note: On platforms not capable of supporting FIQ, the IPI to generate a
backtrace will fall back to using IRQ for propagation instead. The backtrace logic contains a timeout to we will not permanently wedge the requesting CPU if other CPUs are not responsive.

v19:

* Remove redundant memory barrier inherited from the x86 code (Steven Rostedt).

v18:

* Move printk_nmi_ functions out of printk.c and into their own file, nmi_callback.c (Joe Perches/Steven Rostedt).

* Rename printk_nmi_ functions so their name matches their new home (Joe Perches)

v17:

* Rename bl_migration_lock/unlock to gic_migration_lock/unlock (Nicolas Pitre).

v16:

* Significant clean up of the printk patches (Thomas Gleixner). Replacing macros with real functions, CONFIG_ARCH_WANT_NMI_PRINTK -> CONFIG_PRINTK_NMI, prefixing global functions with printk_nmi, removing pointless exports, removing cpu_mask from the interfaces, removal of just-in-time initialization of trace buffers, prevented call sites having to save state, rolled up variable declarations into single lines.

* Dropped the sched_clock() patches from *this* patchset and managed them separately (http://thread.gmane.org/gmane.linux.kernel/1879261 ). The cross-dependancies between the patches are minimal; the backtrace code only calls sched_clock() if we are ftracing and backtracing is normally only triggered to report information about about a broken system (although users can type SysRq-l for amusement, most use it to find out why the system it dead).

* Squashed together the final two patches. Essentially these duplicated the x86 code and slavishly avoided changing it before, in the next patch, fixing it to work better on ARM. It seems better that the code just works first time!

v15:

* Added a patch to make sched_clock safe to call from NMI (Stephen Boyd). Note that sched_clock() is not called by the NMI handlers that have been added for the arm but it could be called if tools such as ftrace are deployed.

* Fixed some warnings picked up during bisectability testing.

v14:

* Moved a nmi_vprintk() and friends from arch/x86/kernel/apic/hw_nmi.c to printk.c (Steven Rostedt)

v13:

* Updated the code to print the backtrace to replicate Steven Rostedt's x86 work to make SysRq-l safe. This is pretty much a total rewrite of patches 4 and 5.

v12:

* Squash first two patches into a single one and re-describe (Thomas Gleixner).

* Improve description of "irqchip: gic: Make gic_raise_softirq FIQ-safe" (Thomas Gleixner).

v11:

* Optimized gic_raise_softirq() by replacing a register read with a memory read (Jason Cooper).

v10:

* Add a further patch to optimize away some of the locking on systems where CONFIG_BL_SWITCHER is not set (Marc Zyngier). Compiles OK with exynos_defconfig (which is the only defconfig to set this option).

* Whitespace fixes in patch 4. That patch previously used spaces for alignment of new constants but the rest of the file used tabs.

v9:

* Improved documentation and structure of initial patch (now initial two patches) to make gic_raise_softirq() safe to call from FIQ (Thomas Gleixner).

* Avoid masking interrupts during gic_raise_softirq(). The use of the read lock makes this redundant (because we can safely re-enter the function).

v8:

* Fixed build on arm64 causes by a spurious include file in irq-gic.c.

v7-2 (accidentally released twice with same number):

* Fixed boot regression on vexpress-a9 (reported by Russell King).

* Rebased on v3.18-rc3; removed one patch from set that is already included in mainline.

* Dropped arm64/fiq.h patch from the set (still useful but not related to issuing backtraces).

v7:

* Re-arranged code within the patch series to fix a regression introduced midway through the series and corrected by a later patch (testing by Olof's autobuilder). Tested offending patch in isolation using defconfig identified by the autobuilder.

v6:

* Renamed svc_entry's call_trace argument to just trace (example code from Russell King).

* Fixed mismatched ENDPROC() in __fiq_abt (example code from Russell King).

* Modified usr_entry to optional avoid calling into the trace code and used this in FIQ entry from usr path. Modified corresponding exit code to avoid calling into trace code and the scheduler (example code from Russell King).

* Ensured the default FIQ register state is restored when the default FIQ handler is reinstalled (example code from Russell King).

* Renamed no_fiq_insn to dfl_fiq_insn to reflect the effect of adopting a default FIQ handler.

* Re-instated fiq_safe_migration_lock and associated logic in gic_raise_softirq(). gic_raise_softirq() is called by wake_up_klogd() in the console unlock logic.

v5:

* Rebased on 3.17-rc4.

* Removed a spurious line from the final "glue it together" patch that broke the build.

v4:

* Replaced push/pop with stmfd/ldmfd respectively (review of Nicolas Pitre).

* Really fix bad pt_regs pointer generation in __fiq_abt.

* Remove fiq_safe_migration_lock and associated logic in gic_raise_softirq() (review of Russell King)

* Restructured to introduce the default FIQ handler first, before the new features (review of Russell King).

v3:

* Removed redundant header guards from arch/arm64/include/asm/fiq.h (review of Catalin Marinas).

* Moved svc_exit_via_fiq macro to entry-header.S (review of Nicolas Pitre).

v2:

* Restructured to sit nicely on a similar FYI patchset from Russell King. It now effectively replaces the work in progress final patch with something much more complete.

* Implemented (and tested) a Thumb-2 implementation of svc_exit_via_fiq (review of Nicolas Pitre)

* Dropped the GIC group 0 workaround patch. The issue of FIQ interrupts being acknowledged by the IRQ handler does still exist but should be harmless because the IRQ handler will still wind up calling ipi_cpu_backtrace().

* Removed any dependency on CONFIG_FIQ; all cpu backtrace effectively becomes a platform feature (although the use of non-maskable interrupts to implement it is best effort rather than guaranteed).

* Better comments highlighting usage of RAZ/WI registers (and parts of registers) in the GIC code.

Changes *before* v1:

* This patchset is a hugely cut-down successor to "[PATCH v11 00/19]
arm: KGDB NMI/FIQ support". Thanks to Thomas Gleixner for suggesting the new structure. For historic details see:

        https://lkml.org/lkml/2014/9/2/227

* Fix bug in __fiq_abt (no longer passes a bad struct pt_regs value). In fixing this we also remove the useless indirection previously found in the fiq_handler macro.

* Make default fiq handler "always on" by migrating from fiq.c to traps.c and replace do_unexp_fiq with the new handler (review of Russell King).

* Add arm64 version of fiq.h (review of Russell King)

* Removed conditional branching and code from irq-gic.c, this is replaced by much simpler code that relies on the GIC specification's heavy use of read-as-zero/write-ignored (review of Russell King)

Daniel Thompson (6):
irqchip: gic: Optimize locking in gic_raise_softirq
irqchip: gic: Make gic_raise_softirq FIQ-safe
irqchip: gic: Introduce plumbing for IPI FIQ
printk: Simple implementation for NMI backtracing x86/nmi: Use common printk functions
ARM: Add support for on-demand backtrace of other CPUs

 arch/arm/Kconfig                |   1 +
 arch/arm/include/asm/hardirq.h  |   2 +-
 arch/arm/include/asm/irq.h      |   5 +
 arch/arm/include/asm/smp.h      |   3 +
 arch/arm/kernel/smp.c           |  81 ++++++++++++++++
 arch/arm/kernel/traps.c         |   8 +-
 arch/x86/Kconfig                |   1 +
 arch/x86/kernel/apic/hw_nmi.c   | 101 ++------------------
 drivers/irqchip/irq-gic.c       | 203 +++++++++++++++++++++++++++++++++++++---
 include/linux/irqchip/arm-gic.h |   8 ++
 include/linux/printk.h          |  20 ++++
 init/Kconfig                    |   3 +
 kernel/printk/Makefile          |   1 +
 kernel/printk/nmi_backtrace.c   | 147 +++++++++++++++++++++++++++++
 14 files changed, 473 insertions(+), 111 deletions(-)
 create mode 100644 kernel/printk/nmi_backtrace.c

--
2.1.0

#1 lore raw

PATCH v19 1/6 irqchip: gic: Optimize locking in gic_raise_softirq

Currently gic_raise_softirq() is locked using upon irq_controller_lock. This lock is primarily used to make register read-modify-write sequences atomic but gic_raise_softirq() uses it instead to ensure that the big.LITTLE migration logic can figure out when it is safe to migrate interrupts between physical cores.

This is sub-optimal in closely related ways:

1. No locking at all is required on systems where the b.L switcher is not configured.

2. Finer grain locking can be used on systems where the b.L switcher is present.

This patch resolves both of the above by introducing a separate finer grain lock and providing conditionally compiled inlines to lock/unlock it.

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 drivers/irqchip/irq-gic.c | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)
drivers/irqchip/irq-gic.c +33 -3
index 471e1cdc1933..a181b836d5ea 100644
@@ -73,6 +73,27 @@ struct gic_chip_data {
 static DEFINE_RAW_SPINLOCK(irq_controller_lock);
 
 /*
+ * This lock is used by the big.LITTLE migration code to ensure no IPIs
+ * can be pended on the old core after the map has been updated.
+ */
+#ifdef CONFIG_BL_SWITCHER
+static DEFINE_RAW_SPINLOCK(cpu_map_migration_lock);
+
+static inline void gic_migration_lock(unsigned long *flags)
+{
+	raw_spin_lock_irqsave(&cpu_map_migration_lock, *flags);
+}
+
+static inline void gic_migration_unlock(unsigned long flags)
+{
+	raw_spin_unlock_irqrestore(&cpu_map_migration_lock, flags);
+}
+#else
+static inline void gic_migration_lock(unsigned long *flags) {}
+static inline void gic_migration_unlock(unsigned long flags) {}
+#endif
+
+/*
  * The GIC mapping of CPU interfaces does not necessarily match
  * the logical CPU numbering.  Let's use a mapping as returned
  * by the GIC itself.
@@ -631,7 +652,7 @@ static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
 	int cpu;
 	unsigned long flags, map = 0;
 
-	raw_spin_lock_irqsave(&irq_controller_lock, flags);
+	gic_migration_lock(&flags);
 
 	/* Convert our logical CPU mask into a physical one. */
 	for_each_cpu(cpu, mask)
@@ -646,7 +667,7 @@ static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
 	/* this always happens on GIC0 */
 	writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
 
-	raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
+	gic_migration_unlock(flags);
 }
 #endif
 
@@ -717,8 +738,17 @@ void gic_migrate_target(unsigned int new_cpu_id)
 
 	raw_spin_lock(&irq_controller_lock);
 
-	/* Update the target interface for this logical CPU */
+	/*
+	 * Update the target interface for this logical CPU
+	 *
+	 * From the point we release the cpu_map_migration_lock any new
+	 * SGIs will be pended on the new cpu which makes the set of SGIs
+	 * pending on the old cpu static. That means we can defer the
+	 * migration until after we have released the irq_controller_lock.
+	 */
+	raw_spin_lock(&cpu_map_migration_lock);
 	gic_cpu_map[cpu] = 1 << new_cpu_id;
+	raw_spin_unlock(&cpu_map_migration_lock);
 
 	/*
 	 * Find all the peripheral interrupts targetting the current
Signature · 1 line
2.1.0
#2 lore raw

PATCH v19 2/6 irqchip: gic: Make gic_raise_softirq FIQ-safe

In reply to Daniel Thompson

It is currently possible for FIQ handlers to re-enter gic_raise_softirq() and lock up.

    	gic_raise_softirq()
	   lock(x);
-~-> FIQ
        handle_fiq()
	   gic_raise_softirq()
	      lock(x);		<-- Lockup

arch/arm/ uses IPIs to implement arch_irq_work_raise(), thus this issue renders it difficult for FIQ handlers to safely defer work to less restrictive calling contexts.

This patch fixes the problem by converting the cpu_map_migration_lock into a rwlock making it safe to re-enter the function.

Note that having made it safe to re-enter gic_raise_softirq() we no longer need to mask interrupts during gic_raise_softirq() because the b.L migration is always performed from task context.

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 drivers/irqchip/irq-gic.c | 38 +++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)
drivers/irqchip/irq-gic.c +25 -13
index a181b836d5ea..578ffc5ec087 100644
@@ -75,22 +75,25 @@ static DEFINE_RAW_SPINLOCK(irq_controller_lock);
 /*
  * This lock is used by the big.LITTLE migration code to ensure no IPIs
  * can be pended on the old core after the map has been updated.
+ *
+ * This lock may be locked for reading from both IRQ and FIQ handlers
+ * and therefore must not be locked for writing when these are enabled.
  */
 #ifdef CONFIG_BL_SWITCHER
-static DEFINE_RAW_SPINLOCK(cpu_map_migration_lock);
+static DEFINE_RWLOCK(cpu_map_migration_lock);
 
-static inline void gic_migration_lock(unsigned long *flags)
+static inline void gic_migration_lock(void)
 {
-	raw_spin_lock_irqsave(&cpu_map_migration_lock, *flags);
+	read_lock(&cpu_map_migration_lock);
 }
 
-static inline void gic_migration_unlock(unsigned long flags)
+static inline void gic_migration_unlock(void)
 {
-	raw_spin_unlock_irqrestore(&cpu_map_migration_lock, flags);
+	read_unlock(&cpu_map_migration_lock);
 }
 #else
-static inline void gic_migration_lock(unsigned long *flags) {}
-static inline void gic_migration_unlock(unsigned long flags) {}
+static inline void gic_migration_lock(void) {}
+static inline void gic_migration_unlock(void) {}
 #endif
 
 /*
@@ -647,12 +650,20 @@ static void __init gic_pm_init(struct gic_chip_data *gic)
 #endif
 
 #ifdef CONFIG_SMP
+/*
+ * Raise the specified IPI on all cpus set in mask.
+ *
+ * This function is safe to call from all calling contexts, including
+ * FIQ handlers. It relies on gic_migration_lock() being multiply acquirable
+ * to avoid deadlocks when the function is re-entered at different
+ * exception levels.
+ */
 static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
 {
 	int cpu;
-	unsigned long flags, map = 0;
+	unsigned long map = 0;
 
-	gic_migration_lock(&flags);
+	gic_migration_lock();
 
 	/* Convert our logical CPU mask into a physical one. */
 	for_each_cpu(cpu, mask)
@@ -667,7 +678,7 @@ static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
 	/* this always happens on GIC0 */
 	writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
 
-	gic_migration_unlock(flags);
+	gic_migration_unlock();
 }
 #endif
 
@@ -715,7 +726,8 @@ int gic_get_cpu_id(unsigned int cpu)
  * Migrate all peripheral interrupts with a target matching the current CPU
  * to the interface corresponding to @new_cpu_id.  The CPU interface mapping
  * is also updated.  Targets to other CPU interfaces are unchanged.
- * This must be called with IRQs locally disabled.
+ * This must be called from a task context and with IRQ and FIQ locally
+ * disabled.
  */
 void gic_migrate_target(unsigned int new_cpu_id)
 {
@@ -746,9 +758,9 @@ void gic_migrate_target(unsigned int new_cpu_id)
 	 * pending on the old cpu static. That means we can defer the
 	 * migration until after we have released the irq_controller_lock.
 	 */
-	raw_spin_lock(&cpu_map_migration_lock);
+	write_lock(&cpu_map_migration_lock);
 	gic_cpu_map[cpu] = 1 << new_cpu_id;
-	raw_spin_unlock(&cpu_map_migration_lock);
+	write_unlock(&cpu_map_migration_lock);
 
 	/*
 	 * Find all the peripheral interrupts targetting the current
Signature · 1 line
2.1.0
#3 lore raw

PATCH v19 3/6 irqchip: gic: Introduce plumbing for IPI FIQ

In reply to Daniel Thompson

Currently it is not possible to exploit FIQ for systems with a GIC, even if the systems are otherwise capable of it. This patch makes it possible for IPIs to be delivered using FIQ.

To do so it modifies the register state so that normal interrupts are placed in group 1 and specific IPIs are placed into group 0. It also configures the controller to raise group 0 interrupts using the FIQ signal. It provides a means for architecture code to define which IPIs shall use FIQ and to acknowledge any IPIs that are raised.

All GIC hardware except GICv1-without-TrustZone support provides a means to group exceptions into group 0 and group 1 but the hardware functionality is unavailable to the kernel when a secure monitor is present because access to the grouping registers are prohibited outside "secure world". However when grouping is not available (or in the case of early GICv1 implementations is very hard to configure) the code to change groups does not deploy and all IPIs will be raised via IRQ.

It has been tested and shown working on two systems capable of supporting grouping (Freescale i.MX6 and STiH416). It has also been tested for boot regressions on two systems that do not support grouping (vexpress-a9 and Qualcomm Snapdragon 600).

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm/kernel/traps.c         |   5 +-
 drivers/irqchip/irq-gic.c       | 151 +++++++++++++++++++++++++++++++++++++---
 include/linux/irqchip/arm-gic.h |   8 +++
 3 files changed, 153 insertions(+), 11 deletions(-)
arch/arm/kernel/traps.c +4 -1
index 788e23fe64d8..b35e220ae1b1 100644
@@ -26,6 +26,7 @@
 #include <linux/init.h>
 #include <linux/sched.h>
 #include <linux/irq.h>
+#include <linux/irqchip/arm-gic.h>
 
 #include <linux/atomic.h>
 #include <asm/cacheflush.h>
@@ -479,7 +480,9 @@ asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
 
 	nmi_enter();
 
-	/* nop. FIQ handlers for special arch/arm features can be added here. */
+#ifdef CONFIG_ARM_GIC
+	gic_handle_fiq_ipi();
+#endif
 
 	nmi_exit();
 
drivers/irqchip/irq-gic.c +141 -10

220 lines not on this page · raw message

include/linux/irqchip/arm-gic.h +8 -0
index 71d706d5f169..7690f70049a3 100644
@@ -22,6 +22,10 @@
 #define GIC_CPU_IDENT			0xfc
 
 #define GICC_ENABLE			0x1
+#define GICC_ENABLE_GRP1		0x2
+#define GICC_ACK_CTL			0x4
+#define GICC_FIQ_EN			0x8
+#define GICC_COMMON_BPR			0x10
 #define GICC_INT_PRI_THRESHOLD		0xf0
 #define GICC_IAR_INT_ID_MASK		0x3ff
 #define GICC_INT_SPURIOUS		1023
@@ -44,6 +48,7 @@
 #define GIC_DIST_SGI_PENDING_SET	0xf20
 
 #define GICD_ENABLE			0x1
+#define GICD_ENABLE_GRP1		0x2
 #define GICD_DISABLE			0x0
 #define GICD_INT_ACTLOW_LVLTRIG		0x0
 #define GICD_INT_EN_CLR_X32		0xffffffff
@@ -121,5 +126,8 @@ static inline void __init register_routable_domain_ops
 {
 	gic_routable_irq_domain_ops = ops;
 }
+
+void gic_handle_fiq_ipi(void);
+
 #endif /* __ASSEMBLY */
 #endif
Signature · 1 line
2.1.0
#4 lore raw

PATCH v19 4/6 printk: Simple implementation for NMI backtracing

In reply to Daniel Thompson

Currently there is a quite a pile of code sitting in arch/x86/kernel/apic/hw_nmi.c to support safe all-cpu backtracing from NMI. The code is inaccessible to backtrace implementations for other architectures, which is a shame because they would probably like to be safe too.

Copy this code into printk, reworking it a little as we do so to make it easier to exploit as library code.

We'll port the x86 NMI backtrace logic to it in a later patch.

---
 include/linux/printk.h        |  20 ++++++
 init/Kconfig                  |   3 +
 kernel/printk/Makefile        |   1 +
 kernel/printk/nmi_backtrace.c | 147 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 171 insertions(+)
 create mode 100644 kernel/printk/nmi_backtrace.c
include/linux/printk.h +20 -0
index baa3f97d8ce8..44bb85ad1f62 100644
@@ -228,6 +228,26 @@ static inline void show_regs_print_info(const char *log_lvl)
 }
 #endif
 
+#ifdef CONFIG_PRINTK_NMI_BACKTRACE
+/*
+ * printk_nmi_backtrace_prepare/complete are called to prepare the
+ * system for some or all cores to issue trace from NMI.
+ * printk_nmi_backtrace_complete will print buffered output and cannot
+ * (safely) be called from NMI.
+ */
+extern int printk_nmi_backtrace_prepare(void);
+extern void printk_nmi_backtrace_complete(void);
+
+/*
+ * printk_nmi_backtrace_this_cpu_begin/end are used divert/restore printk
+ * on this cpu. The result is the output of printk() (by this CPU) will be
+ * stored in temporary buffers for later printing by
+ * printk_nmi_backtrace_complete.
+ */
+extern void printk_nmi_backtrace_this_cpu_begin(void);
+extern void printk_nmi_backtrace_this_cpu_end(void);
+#endif
+
 extern asmlinkage void dump_stack(void) __cold;
 
 #ifndef pr_fmt
init/Kconfig +3 -0
index f5dbc6d4261b..0107e9b4d2cf 100644
@@ -1421,6 +1421,9 @@ config PRINTK
 	  very difficult to diagnose system problems, saying N here is
 	  strongly discouraged.
 
+config PRINTK_NMI_BACKTRACE
+	bool
+
 config BUG
 	bool "BUG() support" if EXPERT
 	default y
kernel/printk/Makefile +1 -0
index 85405bdcf2b3..1849b001384a 100644
@@ -1,2 +1,3 @@
 obj-y	= printk.o
+obj-$(CONFIG_PRINTK_NMI_BACKTRACE)	+= nmi_backtrace.o
 obj-$(CONFIG_A11Y_BRAILLE_CONSOLE)	+= braille.o
kernel/printk/nmi_backtrace.c new file +147 -0

147 lines not on this page · raw message

Signature · 1 line
2.1.0
#5 lore raw

PATCH v19 5/6 x86/nmi: Use common printk functions

In reply to Daniel Thompson

Much of the code sitting in arch/x86/kernel/apic/hw_nmi.c to support safe all-cpu backtracing from NMI has been copied to printk.c to make it accessible to other architectures.

Port the x86 NMI backtrace to the generic code.

Cc Steven Rostedt <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc x86 at kernel.org
---
 arch/x86/Kconfig              |   1 +
 arch/x86/kernel/apic/hw_nmi.c | 101 +++---------------------------------------
 2 files changed, 8 insertions(+), 94 deletions(-)
arch/x86/Kconfig +1 -0
index b7d31ca55187..a1a54570f2d0 100644
@@ -141,6 +141,7 @@ config X86
 	select ACPI_LEGACY_TABLES_LOOKUP if ACPI
 	select X86_FEATURE_NAMES if PROC_FS
 	select SRCU
+	select PRINTK_NMI_BACKTRACE if X86_LOCAL_APIC
 
 config INSTRUCTION_DECODER
 	def_bool y
arch/x86/kernel/apic/hw_nmi.c +7 -94

136 lines not on this page · raw message

Signature · 1 line
2.1.0
#6 lore raw

PATCH v19 6/6 ARM: Add support for on-demand backtrace of other CPUs

In reply to Daniel Thompson

Replicate the x86 code to trigger a backtrace using an NMI and hook it up to IPI on ARM.

The code differs slightly from the code on x86 because, on ARM, we do now know at compile time whether a platform is capable of supporting FIQ. We must avoid using an IPI to request a backtrace from the CPU on which the backtrace was requested if interrupts are disabled and fall back to generating it directly.

In addition the implementation of arch_trigger_all_cpu_backtrace() the patch also includes a few small items of plumbing that must be hooked up for the new code to work.

Credit:
Russell King provided the initial prototype implementing this feature for ARM. Today the patch has been reworked and, mostly, rewriten to keep it aligned with x86. However this patch does still include some code from Russell's original prototype.

Cc Russell King <[email protected]>
Cc Steven Rostedt <[email protected]>
---
 arch/arm/Kconfig               |  1 +
 arch/arm/include/asm/hardirq.h |  2 +-
 arch/arm/include/asm/irq.h     |  5 +++
 arch/arm/include/asm/smp.h     |  3 ++
 arch/arm/kernel/smp.c          | 81 ++++++++++++++++++++++++++++++++++++++++++
 arch/arm/kernel/traps.c        |  3 ++
 6 files changed, 94 insertions(+), 1 deletion(-)
arch/arm/Kconfig +1 -0
index 9f1f09a2bc9b..f3c95a44945d 100644
@@ -76,6 +76,7 @@ config ARM
 	select OLD_SIGACTION
 	select OLD_SIGSUSPEND3
 	select PERF_USE_VMALLOC
+	select PRINTK_NMI_BACKTRACE
 	select RTC_LIB
 	select SYS_SUPPORTS_APM_EMULATION
 	# Above selects are sorted alphabetically; please add new ones
arch/arm/include/asm/hardirq.h +1 -1
index fe3ea776dc34..5df33e30ae1b 100644
@@ -5,7 +5,7 @@
 #include <linux/threads.h>
 #include <asm/irq.h>
 
-#define NR_IPI	8
+#define NR_IPI	9
 
 typedef struct {
 	unsigned int __softirq_pending;
arch/arm/include/asm/irq.h +5 -0
index 53c15dec7af6..be1d07d59ee9 100644
@@ -35,6 +35,11 @@ extern void (*handle_arch_irq)(struct pt_regs *);
 extern void set_handle_irq(void (*handle_irq)(struct pt_regs *));
 #endif
 
+#ifdef CONFIG_SMP
+extern void arch_trigger_all_cpu_backtrace(bool);
+#define arch_trigger_all_cpu_backtrace(x) arch_trigger_all_cpu_backtrace(x)
+#endif
+
 #endif
 
 #endif
arch/arm/include/asm/smp.h +3 -0
index 18f5a554134f..b076584ac0fa 100644
@@ -18,6 +18,8 @@
 # error "<asm/smp.h> included in non-SMP build"
 #endif
 
+#define SMP_IPI_FIQ_MASK 0x0100
+
 #define raw_smp_processor_id() (current_thread_info()->cpu)
 
 struct seq_file;
@@ -79,6 +81,7 @@ extern void arch_send_call_function_single_ipi(int cpu);
 extern void arch_send_call_function_ipi_mask(const struct cpumask *mask);
 extern void arch_send_wakeup_ipi_mask(const struct cpumask *mask);
 
+extern void ipi_cpu_backtrace(struct pt_regs *regs);
 extern int register_ipi_completion(struct completion *completion, int cpu);
 
 struct smp_operations {
arch/arm/kernel/smp.c +81 -0

114 lines not on this page · raw message

arch/arm/kernel/traps.c +3 -0
index b35e220ae1b1..1836415b8a5c 100644
@@ -483,6 +483,9 @@ asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
 #ifdef CONFIG_ARM_GIC
 	gic_handle_fiq_ipi();
 #endif
+#ifdef CONFIG_SMP
+	ipi_cpu_backtrace(regs);
+#endif
 
 	nmi_exit();
 
Signature · 1 line
2.1.0
#7 lore raw

v19 cover irq/arm: Implement arch_trigger_all_cpu_backtrace

In reply to Daniel Thompson

Jason/Thomas:
Any chance of taking the first five of these patches via the irqchip route? The x86 patch has an ack from Ingo, printk has no explicit maintainer and I've done plenty of bisectability tests on the patchset so leaving the last patch for the next dev. cycle should be no trouble.

This patchset modifies the GIC driver to allow it, on supported platforms, to route IPI interrupts to FIQ. It then uses this feature to implement arch_trigger_all_cpu_backtrace for arm. In order to neatly bring in the changes for the arm we also rearrange some of the existing x86 NMI code to make it architecture neutral.

The patchset http://thread.gmane.org/gmane.linux.kernel/1897765 , which makes sched_clock() NMI/FIQ-safe, should be treated as a prerequisite for the sixth and final patch in the series (which enables the feature on ARM). Although sched_clock() is not called directly by any of the code that runs from a FIQ handler it is possible for sched_clock() to be called indirectly when the function tracer is enabled.

The patches have been runtime tested on two systems capable of supporting FIQ (Freescale i.MX6 and STiH416) and two that do not (vexpress-a9 and Qualcomm Snapdragon 600), the changes to the x86 logic were tested on qemu and all patches have been compile tested on x86, arm and arm64.

Note: On platforms not capable of supporting FIQ, the IPI to generate a
backtrace will fall back to using IRQ for propagation instead. The backtrace logic contains a timeout to we will not permanently wedge the requesting CPU if other CPUs are not responsive.

v19:

* Remove redundant memory barrier inherited from the x86 code (Steven Rostedt).

v18:

* Move printk_nmi_ functions out of printk.c and into their own file, nmi_callback.c (Joe Perches/Steven Rostedt).

* Rename printk_nmi_ functions so their name matches their new home (Joe Perches)

v17:

* Rename bl_migration_lock/unlock to gic_migration_lock/unlock (Nicolas Pitre).

v16:

* Significant clean up of the printk patches (Thomas Gleixner). Replacing macros with real functions, CONFIG_ARCH_WANT_NMI_PRINTK -> CONFIG_PRINTK_NMI, prefixing global functions with printk_nmi, removing pointless exports, removing cpu_mask from the interfaces, removal of just-in-time initialization of trace buffers, prevented call sites having to save state, rolled up variable declarations into single lines.

* Dropped the sched_clock() patches from *this* patchset and managed them separately (http://thread.gmane.org/gmane.linux.kernel/1879261 ). The cross-dependancies between the patches are minimal; the backtrace code only calls sched_clock() if we are ftracing and backtracing is normally only triggered to report information about about a broken system (although users can type SysRq-l for amusement, most use it to find out why the system it dead).

* Squashed together the final two patches. Essentially these duplicated the x86 code and slavishly avoided changing it before, in the next patch, fixing it to work better on ARM. It seems better that the code just works first time!

v15:

* Added a patch to make sched_clock safe to call from NMI (Stephen Boyd). Note that sched_clock() is not called by the NMI handlers that have been added for the arm but it could be called if tools such as ftrace are deployed.

* Fixed some warnings picked up during bisectability testing.

v14:

* Moved a nmi_vprintk() and friends from arch/x86/kernel/apic/hw_nmi.c to printk.c (Steven Rostedt)

v13:

* Updated the code to print the backtrace to replicate Steven Rostedt's x86 work to make SysRq-l safe. This is pretty much a total rewrite of patches 4 and 5.

v12:

* Squash first two patches into a single one and re-describe (Thomas Gleixner).

* Improve description of "irqchip: gic: Make gic_raise_softirq FIQ-safe" (Thomas Gleixner).

v11:

* Optimized gic_raise_softirq() by replacing a register read with a memory read (Jason Cooper).

v10:

* Add a further patch to optimize away some of the locking on systems where CONFIG_BL_SWITCHER is not set (Marc Zyngier). Compiles OK with exynos_defconfig (which is the only defconfig to set this option).

* Whitespace fixes in patch 4. That patch previously used spaces for alignment of new constants but the rest of the file used tabs.

v9:

* Improved documentation and structure of initial patch (now initial two patches) to make gic_raise_softirq() safe to call from FIQ (Thomas Gleixner).

* Avoid masking interrupts during gic_raise_softirq(). The use of the read lock makes this redundant (because we can safely re-enter the function).

v8:

* Fixed build on arm64 causes by a spurious include file in irq-gic.c.

v7-2 (accidentally released twice with same number):

* Fixed boot regression on vexpress-a9 (reported by Russell King).

* Rebased on v3.18-rc3; removed one patch from set that is already included in mainline.

* Dropped arm64/fiq.h patch from the set (still useful but not related to issuing backtraces).

v7:

* Re-arranged code within the patch series to fix a regression introduced midway through the series and corrected by a later patch (testing by Olof's autobuilder). Tested offending patch in isolation using defconfig identified by the autobuilder.

v6:

* Renamed svc_entry's call_trace argument to just trace (example code from Russell King).

* Fixed mismatched ENDPROC() in __fiq_abt (example code from Russell King).

* Modified usr_entry to optional avoid calling into the trace code and used this in FIQ entry from usr path. Modified corresponding exit code to avoid calling into trace code and the scheduler (example code from Russell King).

* Ensured the default FIQ register state is restored when the default FIQ handler is reinstalled (example code from Russell King).

* Renamed no_fiq_insn to dfl_fiq_insn to reflect the effect of adopting a default FIQ handler.

* Re-instated fiq_safe_migration_lock and associated logic in gic_raise_softirq(). gic_raise_softirq() is called by wake_up_klogd() in the console unlock logic.

v5:

* Rebased on 3.17-rc4.

* Removed a spurious line from the final "glue it together" patch that broke the build.

v4:

* Replaced push/pop with stmfd/ldmfd respectively (review of Nicolas Pitre).

* Really fix bad pt_regs pointer generation in __fiq_abt.

* Remove fiq_safe_migration_lock and associated logic in gic_raise_softirq() (review of Russell King)

* Restructured to introduce the default FIQ handler first, before the new features (review of Russell King).

v3:

* Removed redundant header guards from arch/arm64/include/asm/fiq.h (review of Catalin Marinas).

* Moved svc_exit_via_fiq macro to entry-header.S (review of Nicolas Pitre).

v2:

* Restructured to sit nicely on a similar FYI patchset from Russell King. It now effectively replaces the work in progress final patch with something much more complete.

* Implemented (and tested) a Thumb-2 implementation of svc_exit_via_fiq (review of Nicolas Pitre)

* Dropped the GIC group 0 workaround patch. The issue of FIQ interrupts being acknowledged by the IRQ handler does still exist but should be harmless because the IRQ handler will still wind up calling ipi_cpu_backtrace().

* Removed any dependency on CONFIG_FIQ; all cpu backtrace effectively becomes a platform feature (although the use of non-maskable interrupts to implement it is best effort rather than guaranteed).

* Better comments highlighting usage of RAZ/WI registers (and parts of registers) in the GIC code.

Changes *before* v1:

* This patchset is a hugely cut-down successor to "[PATCH v11 00/19]
arm: KGDB NMI/FIQ support". Thanks to Thomas Gleixner for suggesting the new structure. For historic details see:

        https://lkml.org/lkml/2014/9/2/227

* Fix bug in __fiq_abt (no longer passes a bad struct pt_regs value). In fixing this we also remove the useless indirection previously found in the fiq_handler macro.

* Make default fiq handler "always on" by migrating from fiq.c to traps.c and replace do_unexp_fiq with the new handler (review of Russell King).

* Add arm64 version of fiq.h (review of Russell King)

* Removed conditional branching and code from irq-gic.c, this is replaced by much simpler code that relies on the GIC specification's heavy use of read-as-zero/write-ignored (review of Russell King)

Daniel Thompson (6):
irqchip: gic: Optimize locking in gic_raise_softirq
irqchip: gic: Make gic_raise_softirq FIQ-safe
irqchip: gic: Introduce plumbing for IPI FIQ
printk: Simple implementation for NMI backtracing x86/nmi: Use common printk functions
ARM: Add support for on-demand backtrace of other CPUs

 arch/arm/Kconfig                |   1 +
 arch/arm/include/asm/hardirq.h  |   2 +-
 arch/arm/include/asm/irq.h      |   5 +
 arch/arm/include/asm/smp.h      |   3 +
 arch/arm/kernel/smp.c           |  81 ++++++++++++++++
 arch/arm/kernel/traps.c         |   8 +-
 arch/x86/Kconfig                |   1 +
 arch/x86/kernel/apic/hw_nmi.c   | 101 ++------------------
 drivers/irqchip/irq-gic.c       | 203 +++++++++++++++++++++++++++++++++++++---
 include/linux/irqchip/arm-gic.h |   8 ++
 include/linux/printk.h          |  20 ++++
 init/Kconfig                    |   3 +
 kernel/printk/Makefile          |   1 +
 kernel/printk/nmi_backtrace.c   | 147 +++++++++++++++++++++++++++++
 14 files changed, 473 insertions(+), 111 deletions(-)
 create mode 100644 kernel/printk/nmi_backtrace.c

--
2.1.0

#8 lore raw

PATCH RESEND v19 1/6 irqchip: gic: Optimize locking in gic_raise_softirq

Currently gic_raise_softirq() is locked using upon irq_controller_lock. This lock is primarily used to make register read-modify-write sequences atomic but gic_raise_softirq() uses it instead to ensure that the big.LITTLE migration logic can figure out when it is safe to migrate interrupts between physical cores.

This is sub-optimal in closely related ways:

1. No locking at all is required on systems where the b.L switcher is not configured.

2. Finer grain locking can be used on systems where the b.L switcher is present.

This patch resolves both of the above by introducing a separate finer grain lock and providing conditionally compiled inlines to lock/unlock it.

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 drivers/irqchip/irq-gic.c | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)
drivers/irqchip/irq-gic.c +33 -3
index 471e1cdc1933..a181b836d5ea 100644
@@ -73,6 +73,27 @@ struct gic_chip_data {
 static DEFINE_RAW_SPINLOCK(irq_controller_lock);
 
 /*
+ * This lock is used by the big.LITTLE migration code to ensure no IPIs
+ * can be pended on the old core after the map has been updated.
+ */
+#ifdef CONFIG_BL_SWITCHER
+static DEFINE_RAW_SPINLOCK(cpu_map_migration_lock);
+
+static inline void gic_migration_lock(unsigned long *flags)
+{
+	raw_spin_lock_irqsave(&cpu_map_migration_lock, *flags);
+}
+
+static inline void gic_migration_unlock(unsigned long flags)
+{
+	raw_spin_unlock_irqrestore(&cpu_map_migration_lock, flags);
+}
+#else
+static inline void gic_migration_lock(unsigned long *flags) {}
+static inline void gic_migration_unlock(unsigned long flags) {}
+#endif
+
+/*
  * The GIC mapping of CPU interfaces does not necessarily match
  * the logical CPU numbering.  Let's use a mapping as returned
  * by the GIC itself.
@@ -631,7 +652,7 @@ static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
 	int cpu;
 	unsigned long flags, map = 0;
 
-	raw_spin_lock_irqsave(&irq_controller_lock, flags);
+	gic_migration_lock(&flags);
 
 	/* Convert our logical CPU mask into a physical one. */
 	for_each_cpu(cpu, mask)
@@ -646,7 +667,7 @@ static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
 	/* this always happens on GIC0 */
 	writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
 
-	raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
+	gic_migration_unlock(flags);
 }
 #endif
 
@@ -717,8 +738,17 @@ void gic_migrate_target(unsigned int new_cpu_id)
 
 	raw_spin_lock(&irq_controller_lock);
 
-	/* Update the target interface for this logical CPU */
+	/*
+	 * Update the target interface for this logical CPU
+	 *
+	 * From the point we release the cpu_map_migration_lock any new
+	 * SGIs will be pended on the new cpu which makes the set of SGIs
+	 * pending on the old cpu static. That means we can defer the
+	 * migration until after we have released the irq_controller_lock.
+	 */
+	raw_spin_lock(&cpu_map_migration_lock);
 	gic_cpu_map[cpu] = 1 << new_cpu_id;
+	raw_spin_unlock(&cpu_map_migration_lock);
 
 	/*
 	 * Find all the peripheral interrupts targetting the current
Signature · 1 line
2.1.0
#9 lore raw

PATCH RESEND v19 2/6 irqchip: gic: Make gic_raise_softirq FIQ-safe

In reply to Daniel Thompson

It is currently possible for FIQ handlers to re-enter gic_raise_softirq() and lock up.

    	gic_raise_softirq()
	   lock(x);
-~-> FIQ
        handle_fiq()
	   gic_raise_softirq()
	      lock(x);		<-- Lockup

arch/arm/ uses IPIs to implement arch_irq_work_raise(), thus this issue renders it difficult for FIQ handlers to safely defer work to less restrictive calling contexts.

This patch fixes the problem by converting the cpu_map_migration_lock into a rwlock making it safe to re-enter the function.

Note that having made it safe to re-enter gic_raise_softirq() we no longer need to mask interrupts during gic_raise_softirq() because the b.L migration is always performed from task context.

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 drivers/irqchip/irq-gic.c | 38 +++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)
drivers/irqchip/irq-gic.c +25 -13
index a181b836d5ea..578ffc5ec087 100644
@@ -75,22 +75,25 @@ static DEFINE_RAW_SPINLOCK(irq_controller_lock);
 /*
  * This lock is used by the big.LITTLE migration code to ensure no IPIs
  * can be pended on the old core after the map has been updated.
+ *
+ * This lock may be locked for reading from both IRQ and FIQ handlers
+ * and therefore must not be locked for writing when these are enabled.
  */
 #ifdef CONFIG_BL_SWITCHER
-static DEFINE_RAW_SPINLOCK(cpu_map_migration_lock);
+static DEFINE_RWLOCK(cpu_map_migration_lock);
 
-static inline void gic_migration_lock(unsigned long *flags)
+static inline void gic_migration_lock(void)
 {
-	raw_spin_lock_irqsave(&cpu_map_migration_lock, *flags);
+	read_lock(&cpu_map_migration_lock);
 }
 
-static inline void gic_migration_unlock(unsigned long flags)
+static inline void gic_migration_unlock(void)
 {
-	raw_spin_unlock_irqrestore(&cpu_map_migration_lock, flags);
+	read_unlock(&cpu_map_migration_lock);
 }
 #else
-static inline void gic_migration_lock(unsigned long *flags) {}
-static inline void gic_migration_unlock(unsigned long flags) {}
+static inline void gic_migration_lock(void) {}
+static inline void gic_migration_unlock(void) {}
 #endif
 
 /*
@@ -647,12 +650,20 @@ static void __init gic_pm_init(struct gic_chip_data *gic)
 #endif
 
 #ifdef CONFIG_SMP
+/*
+ * Raise the specified IPI on all cpus set in mask.
+ *
+ * This function is safe to call from all calling contexts, including
+ * FIQ handlers. It relies on gic_migration_lock() being multiply acquirable
+ * to avoid deadlocks when the function is re-entered at different
+ * exception levels.
+ */
 static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
 {
 	int cpu;
-	unsigned long flags, map = 0;
+	unsigned long map = 0;
 
-	gic_migration_lock(&flags);
+	gic_migration_lock();
 
 	/* Convert our logical CPU mask into a physical one. */
 	for_each_cpu(cpu, mask)
@@ -667,7 +678,7 @@ static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
 	/* this always happens on GIC0 */
 	writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
 
-	gic_migration_unlock(flags);
+	gic_migration_unlock();
 }
 #endif
 
@@ -715,7 +726,8 @@ int gic_get_cpu_id(unsigned int cpu)
  * Migrate all peripheral interrupts with a target matching the current CPU
  * to the interface corresponding to @new_cpu_id.  The CPU interface mapping
  * is also updated.  Targets to other CPU interfaces are unchanged.
- * This must be called with IRQs locally disabled.
+ * This must be called from a task context and with IRQ and FIQ locally
+ * disabled.
  */
 void gic_migrate_target(unsigned int new_cpu_id)
 {
@@ -746,9 +758,9 @@ void gic_migrate_target(unsigned int new_cpu_id)
 	 * pending on the old cpu static. That means we can defer the
 	 * migration until after we have released the irq_controller_lock.
 	 */
-	raw_spin_lock(&cpu_map_migration_lock);
+	write_lock(&cpu_map_migration_lock);
 	gic_cpu_map[cpu] = 1 << new_cpu_id;
-	raw_spin_unlock(&cpu_map_migration_lock);
+	write_unlock(&cpu_map_migration_lock);
 
 	/*
 	 * Find all the peripheral interrupts targetting the current
Signature · 1 line
2.1.0
#10 lore raw

PATCH RESEND v19 3/6 irqchip: gic: Introduce plumbing for IPI FIQ

In reply to Daniel Thompson

Currently it is not possible to exploit FIQ for systems with a GIC, even if the systems are otherwise capable of it. This patch makes it possible for IPIs to be delivered using FIQ.

To do so it modifies the register state so that normal interrupts are placed in group 1 and specific IPIs are placed into group 0. It also configures the controller to raise group 0 interrupts using the FIQ signal. It provides a means for architecture code to define which IPIs shall use FIQ and to acknowledge any IPIs that are raised.

All GIC hardware except GICv1-without-TrustZone support provides a means to group exceptions into group 0 and group 1 but the hardware functionality is unavailable to the kernel when a secure monitor is present because access to the grouping registers are prohibited outside "secure world". However when grouping is not available (or in the case of early GICv1 implementations is very hard to configure) the code to change groups does not deploy and all IPIs will be raised via IRQ.

It has been tested and shown working on two systems capable of supporting grouping (Freescale i.MX6 and STiH416). It has also been tested for boot regressions on two systems that do not support grouping (vexpress-a9 and Qualcomm Snapdragon 600).

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm/kernel/traps.c         |   5 +-
 drivers/irqchip/irq-gic.c       | 151 +++++++++++++++++++++++++++++++++++++---
 include/linux/irqchip/arm-gic.h |   8 +++
 3 files changed, 153 insertions(+), 11 deletions(-)
arch/arm/kernel/traps.c +4 -1
index 788e23fe64d8..b35e220ae1b1 100644
@@ -26,6 +26,7 @@
 #include <linux/init.h>
 #include <linux/sched.h>
 #include <linux/irq.h>
+#include <linux/irqchip/arm-gic.h>
 
 #include <linux/atomic.h>
 #include <asm/cacheflush.h>
@@ -479,7 +480,9 @@ asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
 
 	nmi_enter();
 
-	/* nop. FIQ handlers for special arch/arm features can be added here. */
+#ifdef CONFIG_ARM_GIC
+	gic_handle_fiq_ipi();
+#endif
 
 	nmi_exit();
 
drivers/irqchip/irq-gic.c +141 -10

220 lines not on this page · raw message

include/linux/irqchip/arm-gic.h +8 -0
index 71d706d5f169..7690f70049a3 100644
@@ -22,6 +22,10 @@
 #define GIC_CPU_IDENT			0xfc
 
 #define GICC_ENABLE			0x1
+#define GICC_ENABLE_GRP1		0x2
+#define GICC_ACK_CTL			0x4
+#define GICC_FIQ_EN			0x8
+#define GICC_COMMON_BPR			0x10
 #define GICC_INT_PRI_THRESHOLD		0xf0
 #define GICC_IAR_INT_ID_MASK		0x3ff
 #define GICC_INT_SPURIOUS		1023
@@ -44,6 +48,7 @@
 #define GIC_DIST_SGI_PENDING_SET	0xf20
 
 #define GICD_ENABLE			0x1
+#define GICD_ENABLE_GRP1		0x2
 #define GICD_DISABLE			0x0
 #define GICD_INT_ACTLOW_LVLTRIG		0x0
 #define GICD_INT_EN_CLR_X32		0xffffffff
@@ -121,5 +126,8 @@ static inline void __init register_routable_domain_ops
 {
 	gic_routable_irq_domain_ops = ops;
 }
+
+void gic_handle_fiq_ipi(void);
+
 #endif /* __ASSEMBLY */
 #endif
Signature · 1 line
2.1.0
#11 lore raw

PATCH RESEND v19 4/6 printk: Simple implementation for NMI backtracing

In reply to Daniel Thompson

Currently there is a quite a pile of code sitting in arch/x86/kernel/apic/hw_nmi.c to support safe all-cpu backtracing from NMI. The code is inaccessible to backtrace implementations for other architectures, which is a shame because they would probably like to be safe too.

Copy this code into printk, reworking it a little as we do so to make it easier to exploit as library code.

We'll port the x86 NMI backtrace logic to it in a later patch.

---
 include/linux/printk.h        |  20 ++++++
 init/Kconfig                  |   3 +
 kernel/printk/Makefile        |   1 +
 kernel/printk/nmi_backtrace.c | 147 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 171 insertions(+)
 create mode 100644 kernel/printk/nmi_backtrace.c
include/linux/printk.h +20 -0
index baa3f97d8ce8..44bb85ad1f62 100644
@@ -228,6 +228,26 @@ static inline void show_regs_print_info(const char *log_lvl)
 }
 #endif
 
+#ifdef CONFIG_PRINTK_NMI_BACKTRACE
+/*
+ * printk_nmi_backtrace_prepare/complete are called to prepare the
+ * system for some or all cores to issue trace from NMI.
+ * printk_nmi_backtrace_complete will print buffered output and cannot
+ * (safely) be called from NMI.
+ */
+extern int printk_nmi_backtrace_prepare(void);
+extern void printk_nmi_backtrace_complete(void);
+
+/*
+ * printk_nmi_backtrace_this_cpu_begin/end are used divert/restore printk
+ * on this cpu. The result is the output of printk() (by this CPU) will be
+ * stored in temporary buffers for later printing by
+ * printk_nmi_backtrace_complete.
+ */
+extern void printk_nmi_backtrace_this_cpu_begin(void);
+extern void printk_nmi_backtrace_this_cpu_end(void);
+#endif
+
 extern asmlinkage void dump_stack(void) __cold;
 
 #ifndef pr_fmt
init/Kconfig +3 -0
index f5dbc6d4261b..0107e9b4d2cf 100644
@@ -1421,6 +1421,9 @@ config PRINTK
 	  very difficult to diagnose system problems, saying N here is
 	  strongly discouraged.
 
+config PRINTK_NMI_BACKTRACE
+	bool
+
 config BUG
 	bool "BUG() support" if EXPERT
 	default y
kernel/printk/Makefile +1 -0
index 85405bdcf2b3..1849b001384a 100644
@@ -1,2 +1,3 @@
 obj-y	= printk.o
+obj-$(CONFIG_PRINTK_NMI_BACKTRACE)	+= nmi_backtrace.o
 obj-$(CONFIG_A11Y_BRAILLE_CONSOLE)	+= braille.o
kernel/printk/nmi_backtrace.c new file +147 -0

147 lines not on this page · raw message

Signature · 1 line
2.1.0
#12 lore raw

PATCH RESEND v19 5/6 x86/nmi: Use common printk functions

In reply to Daniel Thompson

Much of the code sitting in arch/x86/kernel/apic/hw_nmi.c to support safe all-cpu backtracing from NMI has been copied to printk.c to make it accessible to other architectures.

Port the x86 NMI backtrace to the generic code.

Cc Steven Rostedt <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc x86 at kernel.org
---
 arch/x86/Kconfig              |   1 +
 arch/x86/kernel/apic/hw_nmi.c | 101 +++---------------------------------------
 2 files changed, 8 insertions(+), 94 deletions(-)
arch/x86/Kconfig +1 -0
index b7d31ca55187..a1a54570f2d0 100644
@@ -141,6 +141,7 @@ config X86
 	select ACPI_LEGACY_TABLES_LOOKUP if ACPI
 	select X86_FEATURE_NAMES if PROC_FS
 	select SRCU
+	select PRINTK_NMI_BACKTRACE if X86_LOCAL_APIC
 
 config INSTRUCTION_DECODER
 	def_bool y
arch/x86/kernel/apic/hw_nmi.c +7 -94

136 lines not on this page · raw message

Signature · 1 line
2.1.0
#13 lore raw

Re: [RESEND PATCH 4.0-rc5 v19 5/6] x86/nmi: Use common printk functions

arch/x86/kernel/apic/hw_nmi.c u64 hw_nmi_get_sample_period(int watchdog_thresh) · line 39

Quoted 10 lines · On Tue, 7 Apr 2015 16:38:02 +0100 Daniel Thompson…

void arch_trigger_all_cpu_backtrace(bool include_self) {

arch/x86/kernel/apic/hw_nmi.c +1 -4
-	struct nmi_seq_buf *s;
-	int len;
-	int cpu;
 	int i;
 	int this_cpu = get_cpu();
 
-	if (test_and_set_bit(0, &backtrace_flag)) {
+	if (0 != printk_nmi_backtrace_prepare()) {

Not sure what the others think, but I hate this polish notation for compares. One does not say "if zero does not equal printk_nmi_backtrace_prepare()", they say "if printk_nmi_backtrace_prepare() does not return zero".

And the reason for polish notation is to prevent the:

	if (x = 0)

mistake. Which gcc warns about anyway. Also, this doesn't even pertain to this code because:

if (printk_nmi_backtrace_prepare() = 0)

would fail to compile.

-- Steve

maintainer
#14 lore raw

Re: [RESEND PATCH 4.0-rc5 v19 5/6] x86/nmi: Use common printk functions

Quoted 7 lines · Steven Rostedt

Not sure what the others think, but I hate this polish notation for compares. One does not say "if zero does not equal printk_nmi_backtrace_prepare()", they say "if printk_nmi_backtrace_prepare() does not return zero".

And the reason for polish notation is to prevent the:

Quoted 8 lines
	if (x = 0)

mistake. Which gcc warns about anyway. Also, this doesn't even pertain to this code because:

if (printk_nmi_backtrace_prepare() = 0)

would fail to compile.

I would simply say:

	err = printk_nmi_backtrace_prepare();
	if (err)

like sane kernel code does.

Besides, there's not a lot of such comparisons in the kernel anyway:

$ git grep -E "if\s+\(+[0-9]+\!?=.*"
drivers/ide/au1xxx-ide.c:246:                   if (1==i)

but my regex doesn't cover all possible variants, just the single-line ones.

Signature · 5 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--
#15 lore raw

Re: [RESEND PATCH 4.0-rc5 v19 5/6] x86/nmi: Use common printk functions

Quoted 7 lines · On Tue, 7 Apr 2015 18:37:46 +0200 Borislav Petkov <[email protected]> wrote:

I would simply say:

	err = printk_nmi_backtrace_prepare();
	if (err)

like sane kernel code does.

Yes, that is even the better solution.

-- Steve

#16 lore raw

Re: [RESEND PATCH 4.0-rc5 v19 5/6] x86/nmi: Use common printk functions

Quoted 6 lines · Steven Rostedt
Quoted 2 lines · On Tue, 7 Apr 2015 18:37:46 +0200 Borislav Petkov <[email protected]> wrote:

I would simply say:

Quoted 7 lines
Quoted 5 lines
	err = printk_nmi_backtrace_prepare();
	if (err)

like sane kernel code does.

Yes, that is even the better solution.

Ok. I'll do this.

The constant on the left habit is so ingrained for me I often don't notice it. I agree with Steven that modern compiler warnings render it an obsolete habit...

Thanks.

Daniel.

#17 lore raw

PATCH RESEND v19 6/6 ARM: Add support for on-demand backtrace of other CPUs

In reply to Daniel Thompson

Replicate the x86 code to trigger a backtrace using an NMI and hook it up to IPI on ARM.

The code differs slightly from the code on x86 because, on ARM, we do now know at compile time whether a platform is capable of supporting FIQ. We must avoid using an IPI to request a backtrace from the CPU on which the backtrace was requested if interrupts are disabled and fall back to generating it directly.

In addition the implementation of arch_trigger_all_cpu_backtrace() the patch also includes a few small items of plumbing that must be hooked up for the new code to work.

Credit:
Russell King provided the initial prototype implementing this feature for ARM. Today the patch has been reworked and, mostly, rewriten to keep it aligned with x86. However this patch does still include some code from Russell's original prototype.

Cc Russell King <[email protected]>
Cc Steven Rostedt <[email protected]>
---
 arch/arm/Kconfig               |  1 +
 arch/arm/include/asm/hardirq.h |  2 +-
 arch/arm/include/asm/irq.h     |  5 +++
 arch/arm/include/asm/smp.h     |  3 ++
 arch/arm/kernel/smp.c          | 81 ++++++++++++++++++++++++++++++++++++++++++
 arch/arm/kernel/traps.c        |  3 ++
 6 files changed, 94 insertions(+), 1 deletion(-)
arch/arm/Kconfig +1 -0
index 9f1f09a2bc9b..f3c95a44945d 100644
@@ -76,6 +76,7 @@ config ARM
 	select OLD_SIGACTION
 	select OLD_SIGSUSPEND3
 	select PERF_USE_VMALLOC
+	select PRINTK_NMI_BACKTRACE
 	select RTC_LIB
 	select SYS_SUPPORTS_APM_EMULATION
 	# Above selects are sorted alphabetically; please add new ones
arch/arm/include/asm/hardirq.h +1 -1
index fe3ea776dc34..5df33e30ae1b 100644
@@ -5,7 +5,7 @@
 #include <linux/threads.h>
 #include <asm/irq.h>
 
-#define NR_IPI	8
+#define NR_IPI	9
 
 typedef struct {
 	unsigned int __softirq_pending;
arch/arm/include/asm/irq.h +5 -0
index 53c15dec7af6..be1d07d59ee9 100644
@@ -35,6 +35,11 @@ extern void (*handle_arch_irq)(struct pt_regs *);
 extern void set_handle_irq(void (*handle_irq)(struct pt_regs *));
 #endif
 
+#ifdef CONFIG_SMP
+extern void arch_trigger_all_cpu_backtrace(bool);
+#define arch_trigger_all_cpu_backtrace(x) arch_trigger_all_cpu_backtrace(x)
+#endif
+
 #endif
 
 #endif
arch/arm/include/asm/smp.h +3 -0
index 18f5a554134f..b076584ac0fa 100644
@@ -18,6 +18,8 @@
 # error "<asm/smp.h> included in non-SMP build"
 #endif
 
+#define SMP_IPI_FIQ_MASK 0x0100
+
 #define raw_smp_processor_id() (current_thread_info()->cpu)
 
 struct seq_file;
@@ -79,6 +81,7 @@ extern void arch_send_call_function_single_ipi(int cpu);
 extern void arch_send_call_function_ipi_mask(const struct cpumask *mask);
 extern void arch_send_wakeup_ipi_mask(const struct cpumask *mask);
 
+extern void ipi_cpu_backtrace(struct pt_regs *regs);
 extern int register_ipi_completion(struct completion *completion, int cpu);
 
 struct smp_operations {
arch/arm/kernel/smp.c +81 -0

114 lines not on this page · raw message

arch/arm/kernel/traps.c +3 -0
index b35e220ae1b1..1836415b8a5c 100644
@@ -483,6 +483,9 @@ asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
 #ifdef CONFIG_ARM_GIC
 	gic_handle_fiq_ipi();
 #endif
+#ifdef CONFIG_SMP
+	ipi_cpu_backtrace(regs);
+#endif
 
 	nmi_exit();
 
Signature · 1 line
2.1.0
#18 lore raw

v20 cover irq/arm: Implement arch_trigger_all_cpu_backtrace

In reply to Daniel Thompson

Jason/Thomas:
Any chance of taking the first five of these patches via the irqchip route? The x86 patch has an ack from Ingo, printk has no explicit maintainer and I've done plenty of bisectability tests on the patchset so leaving the last patch for the next dev. cycle should be no trouble.

This patchset modifies the GIC driver to allow it, on supported platforms, to route IPI interrupts to FIQ. It then uses this feature to implement arch_trigger_all_cpu_backtrace for arm. In order to neatly bring in the changes for the arm we also rearrange some of the existing x86 NMI code to make it architecture neutral.

The patchset http://thread.gmane.org/gmane.linux.kernel/1897765 , which makes sched_clock() NMI/FIQ-safe, is a prerequisite for the sixth and final patch in the series. It is already included in tip.git (timers/core).

The patches have been runtime tested on two systems capable of supporting FIQ (Freescale i.MX6 and STiH416) and two that do not (vexpress-a9 and Qualcomm Snapdragon 600), the changes to the x86 logic were tested on qemu and all patches have been compile tested on x86, arm and arm64.

Note: On platforms not capable of supporting FIQ, the IPI to generate a
backtrace will fall back to using IRQ for propagation instead. The backtrace logic contains a timeout to we will not permanently wedge the requesting CPU if other CPUs are not responsive.

v20:

* Fixed bad coding style (removed "if (0 != func())") (Steven Rostedt and Borislav Petkov).
* Rebased on v4.0-rc7.

v19:

* Remove redundant memory barrier inherited from the x86 code (Steven Rostedt).

v18:

* Move printk_nmi_ functions out of printk.c and into their own file, nmi_callback.c (Joe Perches/Steven Rostedt).

* Rename printk_nmi_ functions so their name matches their new home (Joe Perches)

v17:

* Rename bl_migration_lock/unlock to gic_migration_lock/unlock (Nicolas Pitre).

v16:

* Significant clean up of the printk patches (Thomas Gleixner). Replacing macros with real functions, CONFIG_ARCH_WANT_NMI_PRINTK -> CONFIG_PRINTK_NMI, prefixing global functions with printk_nmi, removing pointless exports, removing cpu_mask from the interfaces, removal of just-in-time initialization of trace buffers, prevented call sites having to save state, rolled up variable declarations into single lines.

* Dropped the sched_clock() patches from *this* patchset and managed them separately (http://thread.gmane.org/gmane.linux.kernel/1879261 ). The cross-dependancies between the patches are minimal; the backtrace code only calls sched_clock() if we are ftracing and backtracing is normally only triggered to report information about about a broken system (although users can type SysRq-l for amusement, most use it to find out why the system it dead).

* Squashed together the final two patches. Essentially these duplicated the x86 code and slavishly avoided changing it before, in the next patch, fixing it to work better on ARM. It seems better that the code just works first time!

v15:

* Added a patch to make sched_clock safe to call from NMI (Stephen Boyd). Note that sched_clock() is not called by the NMI handlers that have been added for the arm but it could be called if tools such as ftrace are deployed.

* Fixed some warnings picked up during bisectability testing.

v14:

* Moved a nmi_vprintk() and friends from arch/x86/kernel/apic/hw_nmi.c to printk.c (Steven Rostedt)

v13:

* Updated the code to print the backtrace to replicate Steven Rostedt's x86 work to make SysRq-l safe. This is pretty much a total rewrite of patches 4 and 5.

v12:

* Squash first two patches into a single one and re-describe (Thomas Gleixner).

* Improve description of "irqchip: gic: Make gic_raise_softirq FIQ-safe" (Thomas Gleixner).

v11:

* Optimized gic_raise_softirq() by replacing a register read with a memory read (Jason Cooper).

v10:

* Add a further patch to optimize away some of the locking on systems where CONFIG_BL_SWITCHER is not set (Marc Zyngier). Compiles OK with exynos_defconfig (which is the only defconfig to set this option).

* Whitespace fixes in patch 4. That patch previously used spaces for alignment of new constants but the rest of the file used tabs.

v9:

* Improved documentation and structure of initial patch (now initial two patches) to make gic_raise_softirq() safe to call from FIQ (Thomas Gleixner).

* Avoid masking interrupts during gic_raise_softirq(). The use of the read lock makes this redundant (because we can safely re-enter the function).

v8:

* Fixed build on arm64 causes by a spurious include file in irq-gic.c.

v7-2 (accidentally released twice with same number):

* Fixed boot regression on vexpress-a9 (reported by Russell King).

* Rebased on v3.18-rc3; removed one patch from set that is already included in mainline.

* Dropped arm64/fiq.h patch from the set (still useful but not related to issuing backtraces).

v7:

* Re-arranged code within the patch series to fix a regression introduced midway through the series and corrected by a later patch (testing by Olof's autobuilder). Tested offending patch in isolation using defconfig identified by the autobuilder.

v6:

* Renamed svc_entry's call_trace argument to just trace (example code from Russell King).

* Fixed mismatched ENDPROC() in __fiq_abt (example code from Russell King).

* Modified usr_entry to optional avoid calling into the trace code and used this in FIQ entry from usr path. Modified corresponding exit code to avoid calling into trace code and the scheduler (example code from Russell King).

* Ensured the default FIQ register state is restored when the default FIQ handler is reinstalled (example code from Russell King).

* Renamed no_fiq_insn to dfl_fiq_insn to reflect the effect of adopting a default FIQ handler.

* Re-instated fiq_safe_migration_lock and associated logic in gic_raise_softirq(). gic_raise_softirq() is called by wake_up_klogd() in the console unlock logic.

v5:

* Rebased on 3.17-rc4.

* Removed a spurious line from the final "glue it together" patch that broke the build.

v4:

* Replaced push/pop with stmfd/ldmfd respectively (review of Nicolas Pitre).

* Really fix bad pt_regs pointer generation in __fiq_abt.

* Remove fiq_safe_migration_lock and associated logic in gic_raise_softirq() (review of Russell King)

* Restructured to introduce the default FIQ handler first, before the new features (review of Russell King).

v3:

* Removed redundant header guards from arch/arm64/include/asm/fiq.h (review of Catalin Marinas).

* Moved svc_exit_via_fiq macro to entry-header.S (review of Nicolas Pitre).

v2:

* Restructured to sit nicely on a similar FYI patchset from Russell King. It now effectively replaces the work in progress final patch with something much more complete.

* Implemented (and tested) a Thumb-2 implementation of svc_exit_via_fiq (review of Nicolas Pitre)

* Dropped the GIC group 0 workaround patch. The issue of FIQ interrupts being acknowledged by the IRQ handler does still exist but should be harmless because the IRQ handler will still wind up calling ipi_cpu_backtrace().

* Removed any dependency on CONFIG_FIQ; all cpu backtrace effectively becomes a platform feature (although the use of non-maskable interrupts to implement it is best effort rather than guaranteed).

* Better comments highlighting usage of RAZ/WI registers (and parts of registers) in the GIC code.

Changes *before* v1:

* This patchset is a hugely cut-down successor to "[PATCH v11 00/19]
arm: KGDB NMI/FIQ support". Thanks to Thomas Gleixner for suggesting the new structure. For historic details see:

        https://lkml.org/lkml/2014/9/2/227

* Fix bug in __fiq_abt (no longer passes a bad struct pt_regs value). In fixing this we also remove the useless indirection previously found in the fiq_handler macro.

* Make default fiq handler "always on" by migrating from fiq.c to traps.c and replace do_unexp_fiq with the new handler (review of Russell King).

* Add arm64 version of fiq.h (review of Russell King)

* Removed conditional branching and code from irq-gic.c, this is replaced by much simpler code that relies on the GIC specification's heavy use of read-as-zero/write-ignored (review of Russell King)

Daniel Thompson (6):
irqchip: gic: Optimize locking in gic_raise_softirq
irqchip: gic: Make gic_raise_softirq FIQ-safe
irqchip: gic: Introduce plumbing for IPI FIQ
printk: Simple implementation for NMI backtracing x86/nmi: Use common printk functions
ARM: Add support for on-demand backtrace of other CPUs

 arch/arm/Kconfig                |   1 +
 arch/arm/include/asm/hardirq.h  |   2 +-
 arch/arm/include/asm/irq.h      |   5 +
 arch/arm/include/asm/smp.h      |   3 +
 arch/arm/kernel/smp.c           |  82 ++++++++++++++++
 arch/arm/kernel/traps.c         |   8 +-
 arch/x86/Kconfig                |   1 +
 arch/x86/kernel/apic/hw_nmi.c   | 104 ++------------------
 drivers/irqchip/irq-gic.c       | 203 +++++++++++++++++++++++++++++++++++++---
 include/linux/irqchip/arm-gic.h |   8 ++
 include/linux/printk.h          |  20 ++++
 init/Kconfig                    |   3 +
 kernel/printk/Makefile          |   1 +
 kernel/printk/nmi_backtrace.c   | 147 +++++++++++++++++++++++++++++
 14 files changed, 476 insertions(+), 112 deletions(-)
 create mode 100644 kernel/printk/nmi_backtrace.c

--
2.1.0

#19 lore raw

PATCH RESEND v20 1/6 irqchip: gic: Optimize locking in gic_raise_softirq

Currently gic_raise_softirq() is locked using upon irq_controller_lock. This lock is primarily used to make register read-modify-write sequences atomic but gic_raise_softirq() uses it instead to ensure that the big.LITTLE migration logic can figure out when it is safe to migrate interrupts between physical cores.

This is sub-optimal in closely related ways:

1. No locking at all is required on systems where the b.L switcher is not configured.

2. Finer grain locking can be used on systems where the b.L switcher is present.

This patch resolves both of the above by introducing a separate finer grain lock and providing conditionally compiled inlines to lock/unlock it.

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 drivers/irqchip/irq-gic.c | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)
drivers/irqchip/irq-gic.c +33 -3
index 471e1cdc1933..a181b836d5ea 100644
@@ -73,6 +73,27 @@ struct gic_chip_data {
 static DEFINE_RAW_SPINLOCK(irq_controller_lock);
 
 /*
+ * This lock is used by the big.LITTLE migration code to ensure no IPIs
+ * can be pended on the old core after the map has been updated.
+ */
+#ifdef CONFIG_BL_SWITCHER
+static DEFINE_RAW_SPINLOCK(cpu_map_migration_lock);
+
+static inline void gic_migration_lock(unsigned long *flags)
+{
+	raw_spin_lock_irqsave(&cpu_map_migration_lock, *flags);
+}
+
+static inline void gic_migration_unlock(unsigned long flags)
+{
+	raw_spin_unlock_irqrestore(&cpu_map_migration_lock, flags);
+}
+#else
+static inline void gic_migration_lock(unsigned long *flags) {}
+static inline void gic_migration_unlock(unsigned long flags) {}
+#endif
+
+/*
  * The GIC mapping of CPU interfaces does not necessarily match
  * the logical CPU numbering.  Let's use a mapping as returned
  * by the GIC itself.
@@ -631,7 +652,7 @@ static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
 	int cpu;
 	unsigned long flags, map = 0;
 
-	raw_spin_lock_irqsave(&irq_controller_lock, flags);
+	gic_migration_lock(&flags);
 
 	/* Convert our logical CPU mask into a physical one. */
 	for_each_cpu(cpu, mask)
@@ -646,7 +667,7 @@ static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
 	/* this always happens on GIC0 */
 	writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
 
-	raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
+	gic_migration_unlock(flags);
 }
 #endif
 
@@ -717,8 +738,17 @@ void gic_migrate_target(unsigned int new_cpu_id)
 
 	raw_spin_lock(&irq_controller_lock);
 
-	/* Update the target interface for this logical CPU */
+	/*
+	 * Update the target interface for this logical CPU
+	 *
+	 * From the point we release the cpu_map_migration_lock any new
+	 * SGIs will be pended on the new cpu which makes the set of SGIs
+	 * pending on the old cpu static. That means we can defer the
+	 * migration until after we have released the irq_controller_lock.
+	 */
+	raw_spin_lock(&cpu_map_migration_lock);
 	gic_cpu_map[cpu] = 1 << new_cpu_id;
+	raw_spin_unlock(&cpu_map_migration_lock);
 
 	/*
 	 * Find all the peripheral interrupts targetting the current
Signature · 1 line
2.1.0
Acked-by on v20 1/6
#20 lore raw

Re: [RESEND PATCH 4.0-rc7 v20 1/6] irqchip: gic: Optimize locking in gic_raise_softirq

Quoted 18 lines · Daniel Thompson

Currently gic_raise_softirq() is locked using upon irq_controller_lock. This lock is primarily used to make register read-modify-write sequences atomic but gic_raise_softirq() uses it instead to ensure that the big.LITTLE migration logic can figure out when it is safe to migrate interrupts between physical cores.

This is sub-optimal in closely related ways:

1. No locking at all is required on systems where the b.L switcher is not configured.

2. Finer grain locking can be used on systems where the b.L switcher is present.

This patch resolves both of the above by introducing a separate finer grain lock and providing conditionally compiled inlines to lock/unlock it.

Quoted 6 lines
Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>

Looks good to me.

	M.
Signature · 1 line
Jazz is not dead. It just smells funny...
#21 lore raw

PATCH RESEND v20 2/6 irqchip: gic: Make gic_raise_softirq FIQ-safe

In reply to Daniel Thompson

It is currently possible for FIQ handlers to re-enter gic_raise_softirq() and lock up.

    	gic_raise_softirq()
	   lock(x);
-~-> FIQ
        handle_fiq()
	   gic_raise_softirq()
	      lock(x);		<-- Lockup

arch/arm/ uses IPIs to implement arch_irq_work_raise(), thus this issue renders it difficult for FIQ handlers to safely defer work to less restrictive calling contexts.

This patch fixes the problem by converting the cpu_map_migration_lock into a rwlock making it safe to re-enter the function.

Note that having made it safe to re-enter gic_raise_softirq() we no longer need to mask interrupts during gic_raise_softirq() because the b.L migration is always performed from task context.

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 drivers/irqchip/irq-gic.c | 38 +++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)
drivers/irqchip/irq-gic.c +25 -13
index a181b836d5ea..578ffc5ec087 100644
@@ -75,22 +75,25 @@ static DEFINE_RAW_SPINLOCK(irq_controller_lock);
 /*
  * This lock is used by the big.LITTLE migration code to ensure no IPIs
  * can be pended on the old core after the map has been updated.
+ *
+ * This lock may be locked for reading from both IRQ and FIQ handlers
+ * and therefore must not be locked for writing when these are enabled.
  */
 #ifdef CONFIG_BL_SWITCHER
-static DEFINE_RAW_SPINLOCK(cpu_map_migration_lock);
+static DEFINE_RWLOCK(cpu_map_migration_lock);
 
-static inline void gic_migration_lock(unsigned long *flags)
+static inline void gic_migration_lock(void)
 {
-	raw_spin_lock_irqsave(&cpu_map_migration_lock, *flags);
+	read_lock(&cpu_map_migration_lock);
 }
 
-static inline void gic_migration_unlock(unsigned long flags)
+static inline void gic_migration_unlock(void)
 {
-	raw_spin_unlock_irqrestore(&cpu_map_migration_lock, flags);
+	read_unlock(&cpu_map_migration_lock);
 }
 #else
-static inline void gic_migration_lock(unsigned long *flags) {}
-static inline void gic_migration_unlock(unsigned long flags) {}
+static inline void gic_migration_lock(void) {}
+static inline void gic_migration_unlock(void) {}
 #endif
 
 /*
@@ -647,12 +650,20 @@ static void __init gic_pm_init(struct gic_chip_data *gic)
 #endif
 
 #ifdef CONFIG_SMP
+/*
+ * Raise the specified IPI on all cpus set in mask.
+ *
+ * This function is safe to call from all calling contexts, including
+ * FIQ handlers. It relies on gic_migration_lock() being multiply acquirable
+ * to avoid deadlocks when the function is re-entered at different
+ * exception levels.
+ */
 static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
 {
 	int cpu;
-	unsigned long flags, map = 0;
+	unsigned long map = 0;
 
-	gic_migration_lock(&flags);
+	gic_migration_lock();
 
 	/* Convert our logical CPU mask into a physical one. */
 	for_each_cpu(cpu, mask)
@@ -667,7 +678,7 @@ static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
 	/* this always happens on GIC0 */
 	writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
 
-	gic_migration_unlock(flags);
+	gic_migration_unlock();
 }
 #endif
 
@@ -715,7 +726,8 @@ int gic_get_cpu_id(unsigned int cpu)
  * Migrate all peripheral interrupts with a target matching the current CPU
  * to the interface corresponding to @new_cpu_id.  The CPU interface mapping
  * is also updated.  Targets to other CPU interfaces are unchanged.
- * This must be called with IRQs locally disabled.
+ * This must be called from a task context and with IRQ and FIQ locally
+ * disabled.
  */
 void gic_migrate_target(unsigned int new_cpu_id)
 {
@@ -746,9 +758,9 @@ void gic_migrate_target(unsigned int new_cpu_id)
 	 * pending on the old cpu static. That means we can defer the
 	 * migration until after we have released the irq_controller_lock.
 	 */
-	raw_spin_lock(&cpu_map_migration_lock);
+	write_lock(&cpu_map_migration_lock);
 	gic_cpu_map[cpu] = 1 << new_cpu_id;
-	raw_spin_unlock(&cpu_map_migration_lock);
+	write_unlock(&cpu_map_migration_lock);
 
 	/*
 	 * Find all the peripheral interrupts targetting the current
Signature · 1 line
2.1.0
Acked-by on v20 2/6
#22 lore raw

Re: [RESEND PATCH 4.0-rc7 v20 2/6] irqchip: gic: Make gic_raise_softirq FIQ-safe

Quoted 21 lines · Daniel Thompson

It is currently possible for FIQ handlers to re-enter gic_raise_softirq() and lock up.

    	gic_raise_softirq()
	   lock(x);
-~-> FIQ
        handle_fiq()
	   gic_raise_softirq()
	      lock(x);		<-- Lockup

arch/arm/ uses IPIs to implement arch_irq_work_raise(), thus this issue renders it difficult for FIQ handlers to safely defer work to less restrictive calling contexts.

This patch fixes the problem by converting the cpu_map_migration_lock into a rwlock making it safe to re-enter the function.

Note that having made it safe to re-enter gic_raise_softirq() we no longer need to mask interrupts during gic_raise_softirq() because the b.L migration is always performed from task context.

Quoted 6 lines
Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
	M.
Signature · 1 line
Jazz is not dead. It just smells funny...
#23 lore raw

PATCH RESEND v20 3/6 irqchip: gic: Introduce plumbing for IPI FIQ

In reply to Daniel Thompson

Currently it is not possible to exploit FIQ for systems with a GIC, even if the systems are otherwise capable of it. This patch makes it possible for IPIs to be delivered using FIQ.

To do so it modifies the register state so that normal interrupts are placed in group 1 and specific IPIs are placed into group 0. It also configures the controller to raise group 0 interrupts using the FIQ signal. It provides a means for architecture code to define which IPIs shall use FIQ and to acknowledge any IPIs that are raised.

All GIC hardware except GICv1-without-TrustZone support provides a means to group exceptions into group 0 and group 1 but the hardware functionality is unavailable to the kernel when a secure monitor is present because access to the grouping registers are prohibited outside "secure world". However when grouping is not available (or in the case of early GICv1 implementations is very hard to configure) the code to change groups does not deploy and all IPIs will be raised via IRQ.

It has been tested and shown working on two systems capable of supporting grouping (Freescale i.MX6 and STiH416). It has also been tested for boot regressions on two systems that do not support grouping (vexpress-a9 and Qualcomm Snapdragon 600).

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm/kernel/traps.c         |   5 +-
 drivers/irqchip/irq-gic.c       | 151 +++++++++++++++++++++++++++++++++++++---
 include/linux/irqchip/arm-gic.h |   8 +++
 3 files changed, 153 insertions(+), 11 deletions(-)
arch/arm/kernel/traps.c +4 -1
index 788e23fe64d8..b35e220ae1b1 100644
@@ -26,6 +26,7 @@
 #include <linux/init.h>
 #include <linux/sched.h>
 #include <linux/irq.h>
+#include <linux/irqchip/arm-gic.h>
 
 #include <linux/atomic.h>
 #include <asm/cacheflush.h>
@@ -479,7 +480,9 @@ asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
 
 	nmi_enter();
 
-	/* nop. FIQ handlers for special arch/arm features can be added here. */
+#ifdef CONFIG_ARM_GIC
+	gic_handle_fiq_ipi();
+#endif
 
 	nmi_exit();
 
drivers/irqchip/irq-gic.c +141 -10

220 lines not on this page · raw message

include/linux/irqchip/arm-gic.h +8 -0
index 71d706d5f169..7690f70049a3 100644
@@ -22,6 +22,10 @@
 #define GIC_CPU_IDENT			0xfc
 
 #define GICC_ENABLE			0x1
+#define GICC_ENABLE_GRP1		0x2
+#define GICC_ACK_CTL			0x4
+#define GICC_FIQ_EN			0x8
+#define GICC_COMMON_BPR			0x10
 #define GICC_INT_PRI_THRESHOLD		0xf0
 #define GICC_IAR_INT_ID_MASK		0x3ff
 #define GICC_INT_SPURIOUS		1023
@@ -44,6 +48,7 @@
 #define GIC_DIST_SGI_PENDING_SET	0xf20
 
 #define GICD_ENABLE			0x1
+#define GICD_ENABLE_GRP1		0x2
 #define GICD_DISABLE			0x0
 #define GICD_INT_ACTLOW_LVLTRIG		0x0
 #define GICD_INT_EN_CLR_X32		0xffffffff
@@ -121,5 +126,8 @@ static inline void __init register_routable_domain_ops
 {
 	gic_routable_irq_domain_ops = ops;
 }
+
+void gic_handle_fiq_ipi(void);
+
 #endif /* __ASSEMBLY */
 #endif
Signature · 1 line
2.1.0
#24 lore raw

Re: [RESEND PATCH 4.0-rc7 v20 3/6] irqchip: gic: Introduce plumbing for IPI FIQ

Quoted 48 lines · Daniel Thompson

Currently it is not possible to exploit FIQ for systems with a GIC, even if the systems are otherwise capable of it. This patch makes it possible for IPIs to be delivered using FIQ.

To do so it modifies the register state so that normal interrupts are placed in group 1 and specific IPIs are placed into group 0. It also configures the controller to raise group 0 interrupts using the FIQ signal. It provides a means for architecture code to define which IPIs shall use FIQ and to acknowledge any IPIs that are raised.

All GIC hardware except GICv1-without-TrustZone support provides a means to group exceptions into group 0 and group 1 but the hardware functionality is unavailable to the kernel when a secure monitor is present because access to the grouping registers are prohibited outside "secure world". However when grouping is not available (or in the case of early GICv1 implementations is very hard to configure) the code to change groups does not deploy and all IPIs will be raised via IRQ.

It has been tested and shown working on two systems capable of supporting grouping (Freescale i.MX6 and STiH416). It has also been tested for boot regressions on two systems that do not support grouping (vexpress-a9 and Qualcomm Snapdragon 600).

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm/kernel/traps.c         |   5 +-
 drivers/irqchip/irq-gic.c       | 151 +++++++++++++++++++++++++++++++++++++---
 include/linux/irqchip/arm-gic.h |   8 +++
 3 files changed, 153 insertions(+), 11 deletions(-)
arch/arm/kernel/traps.c +1 -0
index 788e23fe64d8..b35e220ae1b1 100644
@@ -26,6 +26,7 @@
 #include <linux/init.h>
 #include <linux/sched.h>
 #include <linux/irq.h>
+#include <linux/irqchip/arm-gic.h>
 
 #include <linux/atomic.h>
 #include <asm/cacheflush.h>

arch/arm/kernel/traps.c asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs) · line 485

Quoted 7 lines
arch/arm/kernel/traps.c +3 -1
@@ -479,7 +480,9 @@ asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
 
        nmi_enter();
 
-       /* nop. FIQ handlers for special arch/arm features can be added here. */
+#ifdef CONFIG_ARM_GIC
+       gic_handle_fiq_ipi();
+#endif

This hunk is what irritates me. It creates a hard dependency between core ARM code and the GIC, and I don't really see how this works with multiplatform, where the interrupt controller is not necessarily a GIC. In that case, you will die a horrible death.

Why can't we just call handle_arch_irq(), and let the normal handler do its thing? You can have a "if (in_nmi())" in there, and call your FIQ function. It would at least save us the above problem.

Quoted 55 lines
arch/arm/kernel/traps.c +0 -0
        nmi_exit();
 
drivers/irqchip/irq-gic.c +23 -0
index 578ffc5ec087..ffd1c0fe44b2 100644
@@ -39,6 +39,7 @@
 #include <linux/slab.h>
 #include <linux/irqchip/chained_irq.h>
 #include <linux/irqchip/arm-gic.h>
+#include <linux/ratelimit.h>
 
 #include <asm/cputype.h>
 #include <asm/irq.h>
@@ -48,6 +49,10 @@
 #include "irq-gic-common.h"
 #include "irqchip.h"
 
+#ifndef SMP_IPI_FIQ_MASK
+#define SMP_IPI_FIQ_MASK 0
+#endif
+
 union gic_base {
        void __iomem *common_base;
        void __percpu * __iomem *percpu_base;
@@ -65,6 +70,7 @@ struct gic_chip_data {
 #endif
        struct irq_domain *domain;
        unsigned int gic_irqs;
+       u32 igroup0_shadow;
 #ifdef CONFIG_GIC_NON_BANKED
        void __iomem *(*get_base)(union gic_base *);
 #endif
@@ -355,6 +361,83 @@ static struct irq_chip gic_chip = {
        .irq_set_wake           = gic_set_wake,
 };
 
+/*
+ * Shift an interrupt between Group 0 and Group 1.
+ *
+ * In addition to changing the group we also modify the priority to
+ * match what "ARM strongly recommends" for a system where no Group 1
+ * interrupt must ever preempt a Group 0 interrupt.
+ *
+ * If is safe to call this function on systems which do not support
+ * grouping (it will have no effect).
+ */
+static void gic_set_group_irq(struct gic_chip_data *gic, unsigned int hwirq,
+                             int group)
+{
+       void __iomem *base = gic_data_dist_base(gic);
+       unsigned int grp_reg = hwirq / 32 * 4;
+       u32 grp_mask = BIT(hwirq % 32);
+       u32 grp_val;

drivers/irqchip/irq-gic.c static struct irq_chip gic_chip = { · line 390

Quoted 10 lines
drivers/irqchip/irq-gic.c +10 -0
+
+       unsigned int pri_reg = (hwirq / 4) * 4;
+       u32 pri_mask = BIT(7 + ((hwirq % 4) * 8));
+       u32 pri_val;
+
+       /*
+        * Systems which do not support grouping will have not have
+        * the EnableGrp1 bit set.
+        */
+       if (!(GICD_ENABLE_GRP1 & readl_relaxed(base + GIC_DIST_CTRL)))

I took me a moment to parse this. Can we please have the constants on the right hand side? It is very much a nitpick, but still...

Quoted 110 lines
drivers/irqchip/irq-gic.c +82 -7
+               return;
+
+       raw_spin_lock(&irq_controller_lock);
+
+       grp_val = readl_relaxed(base + GIC_DIST_IGROUP + grp_reg);
+       pri_val = readl_relaxed(base + GIC_DIST_PRI + pri_reg);
+
+       if (group) {
+               grp_val |= grp_mask;
+               pri_val |= pri_mask;
+       } else {
+               grp_val &= ~grp_mask;
+               pri_val &= ~pri_mask;
+       }
+
+       writel_relaxed(grp_val, base + GIC_DIST_IGROUP + grp_reg);
+       if (grp_reg == 0)
+               gic->igroup0_shadow = grp_val;
+
+       writel_relaxed(pri_val, base + GIC_DIST_PRI + pri_reg);
+
+       raw_spin_unlock(&irq_controller_lock);
+}
+
+
+/*
+ * Fully acknowledge (both ack and eoi) any outstanding FIQ-based IPI,
+ * otherwise do nothing.
+ */
+void gic_handle_fiq_ipi(void)
+{
+       struct gic_chip_data *gic = &gic_data[0];
+       void __iomem *cpu_base = gic_data_cpu_base(gic);
+       unsigned long irqstat, irqnr;
+
+       if (WARN_ON(!in_nmi()))
+               return;
+
+       while ((1u << readl_relaxed(cpu_base + GIC_CPU_HIGHPRI)) &
+              SMP_IPI_FIQ_MASK) {
+               irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
+               writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
+
+               irqnr = irqstat & GICC_IAR_INT_ID_MASK;
+               WARN_RATELIMIT(irqnr > 16,
+                              "Unexpected irqnr %lu (bad prioritization?)\n",
+                              irqnr);
+       }
+}
+
 void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
 {
        if (gic_nr >= MAX_GIC_NR)
@@ -386,15 +469,24 @@ static u8 gic_get_cpumask(struct gic_chip_data *gic)
 static void gic_cpu_if_up(void)
 {
        void __iomem *cpu_base = gic_data_cpu_base(&gic_data[0]);
-       u32 bypass = 0;
+       void __iomem *dist_base = gic_data_dist_base(&gic_data[0]);
+       u32 ctrl = 0;
 
        /*
-       * Preserve bypass disable bits to be written back later
-       */
-       bypass = readl(cpu_base + GIC_CPU_CTRL);
-       bypass &= GICC_DIS_BYPASS_MASK;
+        * Preserve bypass disable bits to be written back later
+        */
+       ctrl = readl(cpu_base + GIC_CPU_CTRL);
+       ctrl &= GICC_DIS_BYPASS_MASK;
 
-       writel_relaxed(bypass | GICC_ENABLE, cpu_base + GIC_CPU_CTRL);
+       /*
+        * If EnableGrp1 is set in the distributor then enable group 1
+        * support for this CPU (and route group 0 interrupts to FIQ).
+        */
+       if (GICD_ENABLE_GRP1 & readl_relaxed(dist_base + GIC_DIST_CTRL))
+               ctrl |= GICC_COMMON_BPR | GICC_FIQ_EN | GICC_ACK_CTL |
+                       GICC_ENABLE_GRP1;
+
+       writel_relaxed(ctrl | GICC_ENABLE, cpu_base + GIC_CPU_CTRL);
 }
 
 
@@ -418,7 +510,23 @@ static void __init gic_dist_init(struct gic_chip_data *gic)
 
        gic_dist_config(base, gic_irqs, NULL);
 
-       writel_relaxed(GICD_ENABLE, base + GIC_DIST_CTRL);
+       /*
+        * Set EnableGrp1/EnableGrp0 (bit 1 and 0) or EnableGrp (bit 0 only,
+        * bit 1 ignored) depending on current mode.
+        */
+       writel_relaxed(GICD_ENABLE_GRP1 | GICD_ENABLE, base + GIC_DIST_CTRL);
+
+       /*
+        * Set all global interrupts to be group 1 if (and only if) it
+        * is possible to enable group 1 interrupts. This register is RAZ/WI
+        * if not accessible or not implemented, however some GICv1 devices
+        * do not implement the EnableGrp1 bit making it unsafe to set
+        * this register unconditionally.
+        */
+       if (GICD_ENABLE_GRP1 & readl_relaxed(base + GIC_DIST_CTRL))
+               for (i = 32; i < gic_irqs; i += 32)
+                       writel_relaxed(0xffffffff,
+                                      base + GIC_DIST_IGROUP + i * 4 / 32);
 }
 
 static void gic_cpu_init(struct gic_chip_data *gic)

drivers/irqchip/irq-gic.c static void gic_cpu_init(struct gic_chip_data *gic) · line 538

Quoted 4 lines
drivers/irqchip/irq-gic.c +1 -0
@@ -427,6 +535,7 @@ static void gic_cpu_init(struct gic_chip_data *gic)
        void __iomem *base = gic_data_cpu_base(gic);
        unsigned int cpu_mask, cpu = smp_processor_id();
        int i;
+       unsigned long secure_irqs, secure_irq;

I'd rather see u32 being used for secure_irqs. Remember that this used on arm64 as well.

Quoted 35 lines
drivers/irqchip/irq-gic.c +16 -1
        /*
         * Get what the GIC says our CPU mask is.
@@ -445,6 +554,20 @@ static void gic_cpu_init(struct gic_chip_data *gic)
 
        gic_cpu_config(dist_base, NULL);
 
+       /*
+        * If the distributor is configured to support interrupt grouping
+        * then set any PPI and SGI interrupts not set in SMP_IPI_FIQ_MASK
+        * to be group1 and ensure any remaining group 0 interrupts have
+        * the right priority.
+        */
+       if (GICD_ENABLE_GRP1 & readl_relaxed(dist_base + GIC_DIST_CTRL)) {
+               secure_irqs = SMP_IPI_FIQ_MASK;
+               writel_relaxed(~secure_irqs, dist_base + GIC_DIST_IGROUP + 0);
+               gic->igroup0_shadow = ~secure_irqs;
+               for_each_set_bit(secure_irq, &secure_irqs, 16)
+                       gic_set_group_irq(gic, secure_irq, 0);
+       }
+
        writel_relaxed(GICC_INT_PRI_THRESHOLD, base + GIC_CPU_PRIMASK);
        gic_cpu_if_up();
 }
@@ -534,7 +657,8 @@ static void gic_dist_restore(unsigned int gic_nr)
                writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
                        dist_base + GIC_DIST_ENABLE_SET + i * 4);
 
-       writel_relaxed(GICD_ENABLE, dist_base + GIC_DIST_CTRL);
+       writel_relaxed(GICD_ENABLE_GRP1 | GICD_ENABLE,
+                      dist_base + GIC_DIST_CTRL);
 }
 
 static void gic_cpu_save(unsigned int gic_nr)

drivers/irqchip/irq-gic.c static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq) · line 789

Quoted 4 lines
drivers/irqchip/irq-gic.c +1 -0
@@ -662,6 +786,7 @@ static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
 {
        int cpu;
        unsigned long map = 0;
+       unsigned long softint;

u32, please.

Quoted 53 lines
drivers/irqchip/irq-gic.c +8 -2
        gic_migration_lock();
 
@@ -675,8 +800,14 @@ static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
         */
        dmb(ishst);
 
-       /* this always happens on GIC0 */
-       writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
+       /* We avoid a readl here by using the shadow copy of IGROUP[0] */
+       softint = map << 16 | irq;
+       if (gic_data[0].igroup0_shadow & BIT(irq))
+               softint |= 0x8000;
+
+       /* This always happens on GIC0 */
+       writel_relaxed(softint,
+                      gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
 
        gic_migration_unlock();
 }
include/linux/irqchip/arm-gic.h +8 -1
index 71d706d5f169..7690f70049a3 100644
@@ -22,6 +22,10 @@
 #define GIC_CPU_IDENT                  0xfc
 
 #define GICC_ENABLE                    0x1
+#define GICC_ENABLE_GRP1               0x2
+#define GICC_ACK_CTL                   0x4
+#define GICC_FIQ_EN                    0x8
+#define GICC_COMMON_BPR                        0x10
 #define GICC_INT_PRI_THRESHOLD         0xf0
 #define GICC_IAR_INT_ID_MASK           0x3ff
 #define GICC_INT_SPURIOUS              1023
@@ -44,6 +48,7 @@
 #define GIC_DIST_SGI_PENDING_SET       0xf20
 
 #define GICD_ENABLE                    0x1
+#define GICD_ENABLE_GRP1               0x2
 #define GICD_DISABLE                   0x0
 #define GICD_INT_ACTLOW_LVLTRIG                0x0
 #define GICD_INT_EN_CLR_X32            0xffffffff
@@ -121,5 +126,8 @@ static inline void __init register_routable_domain_ops
 {
        gic_routable_irq_domain_ops = ops;
 }
+
+void gic_handle_fiq_ipi(void);
+
 #endif /* __ASSEMBLY */
 #endif
--
Quoted 2 lines

2.1.0

Thanks,

	M.
Signature · 1 line
Jazz is not dead. It just smells funny...
#25 lore raw

Re: [RESEND PATCH 4.0-rc7 v20 3/6] irqchip: gic: Introduce plumbing for IPI FIQ

Quoted 52 lines · Marc Zyngier
Quoted 51 lines · Daniel Thompson

Currently it is not possible to exploit FIQ for systems with a GIC, even if the systems are otherwise capable of it. This patch makes it possible for IPIs to be delivered using FIQ.

To do so it modifies the register state so that normal interrupts are placed in group 1 and specific IPIs are placed into group 0. It also configures the controller to raise group 0 interrupts using the FIQ signal. It provides a means for architecture code to define which IPIs shall use FIQ and to acknowledge any IPIs that are raised.

All GIC hardware except GICv1-without-TrustZone support provides a means to group exceptions into group 0 and group 1 but the hardware functionality is unavailable to the kernel when a secure monitor is present because access to the grouping registers are prohibited outside "secure world". However when grouping is not available (or in the case of early GICv1 implementations is very hard to configure) the code to change groups does not deploy and all IPIs will be raised via IRQ.

It has been tested and shown working on two systems capable of supporting grouping (Freescale i.MX6 and STiH416). It has also been tested for boot regressions on two systems that do not support grouping (vexpress-a9 and Qualcomm Snapdragon 600).

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
  arch/arm/kernel/traps.c         |   5 +-
  drivers/irqchip/irq-gic.c       | 151 +++++++++++++++++++++++++++++++++++++---
  include/linux/irqchip/arm-gic.h |   8 +++
  3 files changed, 153 insertions(+), 11 deletions(-)
arch/arm/kernel/traps.c +1 -0
index 788e23fe64d8..b35e220ae1b1 100644
@@ -26,6 +26,7 @@
  #include <linux/init.h>
  #include <linux/sched.h>
  #include <linux/irq.h>
+#include <linux/irqchip/arm-gic.h>
 
  #include <linux/atomic.h>
  #include <asm/cacheflush.h>
@@ -479,7 +480,9 @@ asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
 
         nmi_enter();
 
Quoted 9 lines

arch/arm/kernel/traps.c asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs) · line 485

Quoted 4 lines
arch/arm/kernel/traps.c +3 -1
-       /* nop. FIQ handlers for special arch/arm features can be added here. */
+#ifdef CONFIG_ARM_GIC
+       gic_handle_fiq_ipi();
+#endif

This hunk is what irritates me. It creates a hard dependency between core ARM code and the GIC, and I don't really see how this works with multiplatform, where the interrupt controller is not necessarily a GIC. In that case, you will die a horrible death.

I was just about to reassure you that there is no bug here... but then I read the code.

gic_handle_fiq_ipi() was *supposed* to do a check to make it safe to call when there is no gic meaning multi-platform support could be achieved by calling into multiple handlers.

It looks like I forgot to write the code that would make this possible. Maybe I was too disgusted with the approach to implement it correctly. Looking at this with fresher eyes (I've been having a bit of a break from FIQ recently) I can see how bad the current approach is.

Quoted 3 lines

Why can't we just call handle_arch_irq(), and let the normal handler do its thing? You can have a "if (in_nmi())" in there, and call your FIQ function. It would at least save us the above problem.

It should certainly work although it feels odd to reuse the IRQ handler for FIQ.

All other comments below are fine. I'll fix these.

Daniel.

Quoted 290 lines
Quoted 56 lines
arch/arm/kernel/traps.c +0 -0
         nmi_exit();
 
drivers/irqchip/irq-gic.c +23 -0
index 578ffc5ec087..ffd1c0fe44b2 100644
@@ -39,6 +39,7 @@
  #include <linux/slab.h>
  #include <linux/irqchip/chained_irq.h>
  #include <linux/irqchip/arm-gic.h>
+#include <linux/ratelimit.h>
 
  #include <asm/cputype.h>
  #include <asm/irq.h>
@@ -48,6 +49,10 @@
  #include "irq-gic-common.h"
  #include "irqchip.h"
 
+#ifndef SMP_IPI_FIQ_MASK
+#define SMP_IPI_FIQ_MASK 0
+#endif
+
  union gic_base {
         void __iomem *common_base;
         void __percpu * __iomem *percpu_base;
@@ -65,6 +70,7 @@ struct gic_chip_data {
  #endif
         struct irq_domain *domain;
         unsigned int gic_irqs;
+       u32 igroup0_shadow;
  #ifdef CONFIG_GIC_NON_BANKED
         void __iomem *(*get_base)(union gic_base *);
  #endif
@@ -355,6 +361,83 @@ static struct irq_chip gic_chip = {
         .irq_set_wake           = gic_set_wake,
  };
 
+/*
+ * Shift an interrupt between Group 0 and Group 1.
+ *
+ * In addition to changing the group we also modify the priority to
+ * match what "ARM strongly recommends" for a system where no Group 1
+ * interrupt must ever preempt a Group 0 interrupt.
+ *
+ * If is safe to call this function on systems which do not support
+ * grouping (it will have no effect).
+ */
+static void gic_set_group_irq(struct gic_chip_data *gic, unsigned int hwirq,
+                             int group)
+{
+       void __iomem *base = gic_data_dist_base(gic);
+       unsigned int grp_reg = hwirq / 32 * 4;
+       u32 grp_mask = BIT(hwirq % 32);
+       u32 grp_val;

drivers/irqchip/irq-gic.c static struct irq_chip gic_chip = { · line 390

Quoted 10 lines
drivers/irqchip/irq-gic.c +10 -0
+
+       unsigned int pri_reg = (hwirq / 4) * 4;
+       u32 pri_mask = BIT(7 + ((hwirq % 4) * 8));
+       u32 pri_val;
+
+       /*
+        * Systems which do not support grouping will have not have
+        * the EnableGrp1 bit set.
+        */
+       if (!(GICD_ENABLE_GRP1 & readl_relaxed(base + GIC_DIST_CTRL)))

I took me a moment to parse this. Can we please have the constants on the right hand side? It is very much a nitpick, but still...

Quoted 110 lines
drivers/irqchip/irq-gic.c +82 -7
+               return;
+
+       raw_spin_lock(&irq_controller_lock);
+
+       grp_val = readl_relaxed(base + GIC_DIST_IGROUP + grp_reg);
+       pri_val = readl_relaxed(base + GIC_DIST_PRI + pri_reg);
+
+       if (group) {
+               grp_val |= grp_mask;
+               pri_val |= pri_mask;
+       } else {
+               grp_val &= ~grp_mask;
+               pri_val &= ~pri_mask;
+       }
+
+       writel_relaxed(grp_val, base + GIC_DIST_IGROUP + grp_reg);
+       if (grp_reg == 0)
+               gic->igroup0_shadow = grp_val;
+
+       writel_relaxed(pri_val, base + GIC_DIST_PRI + pri_reg);
+
+       raw_spin_unlock(&irq_controller_lock);
+}
+
+
+/*
+ * Fully acknowledge (both ack and eoi) any outstanding FIQ-based IPI,
+ * otherwise do nothing.
+ */
+void gic_handle_fiq_ipi(void)
+{
+       struct gic_chip_data *gic = &gic_data[0];
+       void __iomem *cpu_base = gic_data_cpu_base(gic);
+       unsigned long irqstat, irqnr;
+
+       if (WARN_ON(!in_nmi()))
+               return;
+
+       while ((1u << readl_relaxed(cpu_base + GIC_CPU_HIGHPRI)) &
+              SMP_IPI_FIQ_MASK) {
+               irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
+               writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
+
+               irqnr = irqstat & GICC_IAR_INT_ID_MASK;
+               WARN_RATELIMIT(irqnr > 16,
+                              "Unexpected irqnr %lu (bad prioritization?)\n",
+                              irqnr);
+       }
+}
+
  void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
  {
         if (gic_nr >= MAX_GIC_NR)
@@ -386,15 +469,24 @@ static u8 gic_get_cpumask(struct gic_chip_data *gic)
  static void gic_cpu_if_up(void)
  {
         void __iomem *cpu_base = gic_data_cpu_base(&gic_data[0]);
-       u32 bypass = 0;
+       void __iomem *dist_base = gic_data_dist_base(&gic_data[0]);
+       u32 ctrl = 0;
 
         /*
-       * Preserve bypass disable bits to be written back later
-       */
-       bypass = readl(cpu_base + GIC_CPU_CTRL);
-       bypass &= GICC_DIS_BYPASS_MASK;
+        * Preserve bypass disable bits to be written back later
+        */
+       ctrl = readl(cpu_base + GIC_CPU_CTRL);
+       ctrl &= GICC_DIS_BYPASS_MASK;
 
-       writel_relaxed(bypass | GICC_ENABLE, cpu_base + GIC_CPU_CTRL);
+       /*
+        * If EnableGrp1 is set in the distributor then enable group 1
+        * support for this CPU (and route group 0 interrupts to FIQ).
+        */
+       if (GICD_ENABLE_GRP1 & readl_relaxed(dist_base + GIC_DIST_CTRL))
+               ctrl |= GICC_COMMON_BPR | GICC_FIQ_EN | GICC_ACK_CTL |
+                       GICC_ENABLE_GRP1;
+
+       writel_relaxed(ctrl | GICC_ENABLE, cpu_base + GIC_CPU_CTRL);
  }
 
 
@@ -418,7 +510,23 @@ static void __init gic_dist_init(struct gic_chip_data *gic)
 
         gic_dist_config(base, gic_irqs, NULL);
 
-       writel_relaxed(GICD_ENABLE, base + GIC_DIST_CTRL);
+       /*
+        * Set EnableGrp1/EnableGrp0 (bit 1 and 0) or EnableGrp (bit 0 only,
+        * bit 1 ignored) depending on current mode.
+        */
+       writel_relaxed(GICD_ENABLE_GRP1 | GICD_ENABLE, base + GIC_DIST_CTRL);
+
+       /*
+        * Set all global interrupts to be group 1 if (and only if) it
+        * is possible to enable group 1 interrupts. This register is RAZ/WI
+        * if not accessible or not implemented, however some GICv1 devices
+        * do not implement the EnableGrp1 bit making it unsafe to set
+        * this register unconditionally.
+        */
+       if (GICD_ENABLE_GRP1 & readl_relaxed(base + GIC_DIST_CTRL))
+               for (i = 32; i < gic_irqs; i += 32)
+                       writel_relaxed(0xffffffff,
+                                      base + GIC_DIST_IGROUP + i * 4 / 32);
  }
 
  static void gic_cpu_init(struct gic_chip_data *gic)

drivers/irqchip/irq-gic.c static void gic_cpu_init(struct gic_chip_data *gic) · line 538

Quoted 4 lines
drivers/irqchip/irq-gic.c +1 -0
@@ -427,6 +535,7 @@ static void gic_cpu_init(struct gic_chip_data *gic)
         void __iomem *base = gic_data_cpu_base(gic);
         unsigned int cpu_mask, cpu = smp_processor_id();
         int i;
+       unsigned long secure_irqs, secure_irq;

I'd rather see u32 being used for secure_irqs. Remember that this used on arm64 as well.

Quoted 35 lines
drivers/irqchip/irq-gic.c +16 -1
         /*
          * Get what the GIC says our CPU mask is.
@@ -445,6 +554,20 @@ static void gic_cpu_init(struct gic_chip_data *gic)
 
         gic_cpu_config(dist_base, NULL);
 
+       /*
+        * If the distributor is configured to support interrupt grouping
+        * then set any PPI and SGI interrupts not set in SMP_IPI_FIQ_MASK
+        * to be group1 and ensure any remaining group 0 interrupts have
+        * the right priority.
+        */
+       if (GICD_ENABLE_GRP1 & readl_relaxed(dist_base + GIC_DIST_CTRL)) {
+               secure_irqs = SMP_IPI_FIQ_MASK;
+               writel_relaxed(~secure_irqs, dist_base + GIC_DIST_IGROUP + 0);
+               gic->igroup0_shadow = ~secure_irqs;
+               for_each_set_bit(secure_irq, &secure_irqs, 16)
+                       gic_set_group_irq(gic, secure_irq, 0);
+       }
+
         writel_relaxed(GICC_INT_PRI_THRESHOLD, base + GIC_CPU_PRIMASK);
         gic_cpu_if_up();
  }
@@ -534,7 +657,8 @@ static void gic_dist_restore(unsigned int gic_nr)
                 writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
                         dist_base + GIC_DIST_ENABLE_SET + i * 4);
 
-       writel_relaxed(GICD_ENABLE, dist_base + GIC_DIST_CTRL);
+       writel_relaxed(GICD_ENABLE_GRP1 | GICD_ENABLE,
+                      dist_base + GIC_DIST_CTRL);
  }
 
  static void gic_cpu_save(unsigned int gic_nr)

drivers/irqchip/irq-gic.c static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq) · line 789

Quoted 4 lines
drivers/irqchip/irq-gic.c +1 -0
@@ -662,6 +786,7 @@ static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  {
         int cpu;
         unsigned long map = 0;
+       unsigned long softint;

u32, please.

Quoted 53 lines
drivers/irqchip/irq-gic.c +8 -2
         gic_migration_lock();
 
@@ -675,8 +800,14 @@ static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
          */
         dmb(ishst);
 
-       /* this always happens on GIC0 */
-       writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
+       /* We avoid a readl here by using the shadow copy of IGROUP[0] */
+       softint = map << 16 | irq;
+       if (gic_data[0].igroup0_shadow & BIT(irq))
+               softint |= 0x8000;
+
+       /* This always happens on GIC0 */
+       writel_relaxed(softint,
+                      gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
 
         gic_migration_unlock();
  }
include/linux/irqchip/arm-gic.h +8 -1
index 71d706d5f169..7690f70049a3 100644
@@ -22,6 +22,10 @@
  #define GIC_CPU_IDENT                  0xfc
 
  #define GICC_ENABLE                    0x1
+#define GICC_ENABLE_GRP1               0x2
+#define GICC_ACK_CTL                   0x4
+#define GICC_FIQ_EN                    0x8
+#define GICC_COMMON_BPR                        0x10
  #define GICC_INT_PRI_THRESHOLD         0xf0
  #define GICC_IAR_INT_ID_MASK           0x3ff
  #define GICC_INT_SPURIOUS              1023
@@ -44,6 +48,7 @@
  #define GIC_DIST_SGI_PENDING_SET       0xf20
 
  #define GICD_ENABLE                    0x1
+#define GICD_ENABLE_GRP1               0x2
  #define GICD_DISABLE                   0x0
  #define GICD_INT_ACTLOW_LVLTRIG                0x0
  #define GICD_INT_EN_CLR_X32            0xffffffff
@@ -121,5 +126,8 @@ static inline void __init register_routable_domain_ops
  {
         gic_routable_irq_domain_ops = ops;
  }
+
+void gic_handle_fiq_ipi(void);
+
  #endif /* __ASSEMBLY */
  #endif
--
Quoted 2 lines

2.1.0

Thanks,

	M.
#26 lore raw

Re: [RESEND PATCH 4.0-rc7 v20 3/6] irqchip: gic: Introduce plumbing for IPI FIQ

On Tue, 21 Apr 2015 22:03:25 +0100
Daniel Thompson <[email protected]> wrote:

Hi Daniel,

Quoted 76 lines · Marc Zyngier
Quoted 52 lines · Daniel Thompson
Quoted 51 lines

Currently it is not possible to exploit FIQ for systems with a GIC, even if the systems are otherwise capable of it. This patch makes it possible for IPIs to be delivered using FIQ.

To do so it modifies the register state so that normal interrupts are placed in group 1 and specific IPIs are placed into group 0. It also configures the controller to raise group 0 interrupts using the FIQ signal. It provides a means for architecture code to define which IPIs shall use FIQ and to acknowledge any IPIs that are raised.

All GIC hardware except GICv1-without-TrustZone support provides a means to group exceptions into group 0 and group 1 but the hardware functionality is unavailable to the kernel when a secure monitor is present because access to the grouping registers are prohibited outside "secure world". However when grouping is not available (or in the case of early GICv1 implementations is very hard to configure) the code to change groups does not deploy and all IPIs will be raised via IRQ.

It has been tested and shown working on two systems capable of supporting grouping (Freescale i.MX6 and STiH416). It has also been tested for boot regressions on two systems that do not support grouping (vexpress-a9 and Qualcomm Snapdragon 600).

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
  arch/arm/kernel/traps.c         |   5 +-
  drivers/irqchip/irq-gic.c       | 151 +++++++++++++++++++++++++++++++++++++---
  include/linux/irqchip/arm-gic.h |   8 +++
  3 files changed, 153 insertions(+), 11 deletions(-)
arch/arm/kernel/traps.c +1 -0
index 788e23fe64d8..b35e220ae1b1 100644
@@ -26,6 +26,7 @@
  #include <linux/init.h>
  #include <linux/sched.h>
  #include <linux/irq.h>
+#include <linux/irqchip/arm-gic.h>
 
  #include <linux/atomic.h>
  #include <asm/cacheflush.h>
@@ -479,7 +480,9 @@ asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
 
         nmi_enter();
 
Quoted 9 lines

arch/arm/kernel/traps.c asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs) · line 485

Quoted 4 lines
arch/arm/kernel/traps.c +3 -1
-       /* nop. FIQ handlers for special arch/arm features can be added here. */
+#ifdef CONFIG_ARM_GIC
+       gic_handle_fiq_ipi();
+#endif

This hunk is what irritates me. It creates a hard dependency between core ARM code and the GIC, and I don't really see how this works with multiplatform, where the interrupt controller is not necessarily a GIC. In that case, you will die a horrible death.

I was just about to reassure you that there is no bug here... but then I read the code.

gic_handle_fiq_ipi() was *supposed* to do a check to make it safe to call when there is no gic meaning multi-platform support could be achieved by calling into multiple handlers.

It looks like I forgot to write the code that would make this possible. Maybe I was too disgusted with the approach to implement it correctly. Looking at this with fresher eyes (I've been having a bit of a break from FIQ recently) I can see how bad the current approach is.

Quoted 6 lines
Quoted 3 lines

Why can't we just call handle_arch_irq(), and let the normal handler do its thing? You can have a "if (in_nmi())" in there, and call your FIQ function. It would at least save us the above problem.

It should certainly work although it feels odd to reuse the IRQ handler for FIQ.

I can see three options:

- (a) Either we have an interrupt controller specific, FIQ only entry point, and we add calls in traps.c: this implies that each driver has to defend itself against spurious calls.

- (b) We add a separate handle_arch_fiq() indirection that only deals with FIQ. Much better, but it also means that we have to keep this in sync with arm64, for which the interest is relatively limited (FIQ only works if you have a single security domain like XGene, or for a VM).

- (c) We call handle_arch_irq(), and let the interrupt controller code sort the mess.

I really hate (a) with a passion, because it litters both the ARM core code with IC specific code *and* introduce some defensive programming in the IC code, which is a waste...

Option (b) is nicer, but requires additional work and buy-in from the arm64 maintainers, for a non obvious gain (I quite like the idea of injecting FIQs in a VM though, just for fun...).

Option (c) is the simplest, if a little ugly on the side.

Thoughts?

	M.
Signature · 1 line
Jazz is not dead. It just smells funny.
#27 lore raw

Re: [RESEND PATCH 4.0-rc7 v20 3/6] irqchip: gic: Introduce plumbing for IPI FIQ

Quoted 107 lines · Marc Zyngier

On Tue, 21 Apr 2015 22:03:25 +0100
Daniel Thompson <[email protected]> wrote:

Hi Daniel,

Quoted 76 lines · Marc Zyngier
Quoted 52 lines · Daniel Thompson
Quoted 51 lines

Currently it is not possible to exploit FIQ for systems with a GIC, even if the systems are otherwise capable of it. This patch makes it possible for IPIs to be delivered using FIQ.

To do so it modifies the register state so that normal interrupts are placed in group 1 and specific IPIs are placed into group 0. It also configures the controller to raise group 0 interrupts using the FIQ signal. It provides a means for architecture code to define which IPIs shall use FIQ and to acknowledge any IPIs that are raised.

All GIC hardware except GICv1-without-TrustZone support provides a means to group exceptions into group 0 and group 1 but the hardware functionality is unavailable to the kernel when a secure monitor is present because access to the grouping registers are prohibited outside "secure world". However when grouping is not available (or in the case of early GICv1 implementations is very hard to configure) the code to change groups does not deploy and all IPIs will be raised via IRQ.

It has been tested and shown working on two systems capable of supporting grouping (Freescale i.MX6 and STiH416). It has also been tested for boot regressions on two systems that do not support grouping (vexpress-a9 and Qualcomm Snapdragon 600).

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
   arch/arm/kernel/traps.c         |   5 +-
   drivers/irqchip/irq-gic.c       | 151 +++++++++++++++++++++++++++++++++++++---
   include/linux/irqchip/arm-gic.h |   8 +++
   3 files changed, 153 insertions(+), 11 deletions(-)
arch/arm/kernel/traps.c +1 -0
index 788e23fe64d8..b35e220ae1b1 100644
@@ -26,6 +26,7 @@
   #include <linux/init.h>
   #include <linux/sched.h>
   #include <linux/irq.h>
+#include <linux/irqchip/arm-gic.h>
 
   #include <linux/atomic.h>
   #include <asm/cacheflush.h>
@@ -479,7 +480,9 @@ asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
 
          nmi_enter();
 
Quoted 9 lines

arch/arm/kernel/traps.c asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs) · line 485

Quoted 4 lines
arch/arm/kernel/traps.c +3 -1
-       /* nop. FIQ handlers for special arch/arm features can be added here. */
+#ifdef CONFIG_ARM_GIC
+       gic_handle_fiq_ipi();
+#endif

This hunk is what irritates me. It creates a hard dependency between core ARM code and the GIC, and I don't really see how this works with multiplatform, where the interrupt controller is not necessarily a GIC. In that case, you will die a horrible death.

I was just about to reassure you that there is no bug here... but then I read the code.

gic_handle_fiq_ipi() was *supposed* to do a check to make it safe to call when there is no gic meaning multi-platform support could be achieved by calling into multiple handlers.

It looks like I forgot to write the code that would make this possible. Maybe I was too disgusted with the approach to implement it correctly. Looking at this with fresher eyes (I've been having a bit of a break from FIQ recently) I can see how bad the current approach is.

Quoted 6 lines
Quoted 3 lines

Why can't we just call handle_arch_irq(), and let the normal handler do its thing? You can have a "if (in_nmi())" in there, and call your FIQ function. It would at least save us the above problem.

It should certainly work although it feels odd to reuse the IRQ handler for FIQ.

I can see three options:

- (a) Either we have an interrupt controller specific, FIQ only entry point, and we add calls in traps.c: this implies that each driver has to defend itself against spurious calls.

- (b) We add a separate handle_arch_fiq() indirection that only deals with FIQ. Much better, but it also means that we have to keep this in sync with arm64, for which the interest is relatively limited (FIQ only works if you have a single security domain like XGene, or for a VM).

- (c) We call handle_arch_irq(), and let the interrupt controller code sort the mess.

I really hate (a) with a passion, because it litters both the ARM core code with IC specific code *and* introduce some defensive programming in the IC code, which is a waste...

Quoted 7 lines

Option (b) is nicer, but requires additional work and buy-in from the arm64 maintainers, for a non obvious gain (I quite like the idea of injecting FIQs in a VM though, just for fun...).

Option (c) is the simplest, if a little ugly on the side.

Thoughts?

For FIQs, do you anticipate handle_arch_irq() having a role like the current gic_handle_fiq_ipi(), which is acknowledge an IPI and get out? Alternatively it could behave more like its current role for IRQ and call into the handlers itself.

The later seems more likely to work out well when I take another look at hooking up the perf interrupt.

Daniel.

#28 lore raw

Re: [RESEND PATCH 4.0-rc7 v20 3/6] irqchip: gic: Introduce plumbing for IPI FIQ

Quoted 116 lines · On Wed, 22 Apr 2015 13:45:33 +0100 Daniel Thompson…
Quoted 107 lines · Marc Zyngier

On Tue, 21 Apr 2015 22:03:25 +0100
Daniel Thompson <[email protected]> wrote:

Hi Daniel,

Quoted 76 lines · Marc Zyngier
Quoted 52 lines · Daniel Thompson
Quoted 51 lines

Currently it is not possible to exploit FIQ for systems with a GIC, even if the systems are otherwise capable of it. This patch makes it possible for IPIs to be delivered using FIQ.

To do so it modifies the register state so that normal interrupts are placed in group 1 and specific IPIs are placed into group 0. It also configures the controller to raise group 0 interrupts using the FIQ signal. It provides a means for architecture code to define which IPIs shall use FIQ and to acknowledge any IPIs that are raised.

All GIC hardware except GICv1-without-TrustZone support provides a means to group exceptions into group 0 and group 1 but the hardware functionality is unavailable to the kernel when a secure monitor is present because access to the grouping registers are prohibited outside "secure world". However when grouping is not available (or in the case of early GICv1 implementations is very hard to configure) the code to change groups does not deploy and all IPIs will be raised via IRQ.

It has been tested and shown working on two systems capable of supporting grouping (Freescale i.MX6 and STiH416). It has also been tested for boot regressions on two systems that do not support grouping (vexpress-a9 and Qualcomm Snapdragon 600).

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
   arch/arm/kernel/traps.c         |   5 +-
   drivers/irqchip/irq-gic.c       | 151 +++++++++++++++++++++++++++++++++++++---
   include/linux/irqchip/arm-gic.h |   8 +++
   3 files changed, 153 insertions(+), 11 deletions(-)
arch/arm/kernel/traps.c +1 -0
index 788e23fe64d8..b35e220ae1b1 100644
@@ -26,6 +26,7 @@
   #include <linux/init.h>
   #include <linux/sched.h>
   #include <linux/irq.h>
+#include <linux/irqchip/arm-gic.h>
 
   #include <linux/atomic.h>
   #include <asm/cacheflush.h>
@@ -479,7 +480,9 @@ asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
 
          nmi_enter();
 
Quoted 9 lines

arch/arm/kernel/traps.c asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs) · line 485

Quoted 4 lines
arch/arm/kernel/traps.c +3 -1
-       /* nop. FIQ handlers for special arch/arm features can be added here. */
+#ifdef CONFIG_ARM_GIC
+       gic_handle_fiq_ipi();
+#endif

This hunk is what irritates me. It creates a hard dependency between core ARM code and the GIC, and I don't really see how this works with multiplatform, where the interrupt controller is not necessarily a GIC. In that case, you will die a horrible death.

I was just about to reassure you that there is no bug here... but then I read the code.

gic_handle_fiq_ipi() was *supposed* to do a check to make it safe to call when there is no gic meaning multi-platform support could be achieved by calling into multiple handlers.

It looks like I forgot to write the code that would make this possible. Maybe I was too disgusted with the approach to implement it correctly. Looking at this with fresher eyes (I've been having a bit of a break from FIQ recently) I can see how bad the current approach is.

Quoted 6 lines
Quoted 3 lines

Why can't we just call handle_arch_irq(), and let the normal handler do its thing? You can have a "if (in_nmi())" in there, and call your FIQ function. It would at least save us the above problem.

It should certainly work although it feels odd to reuse the IRQ handler for FIQ.

I can see three options:

- (a) Either we have an interrupt controller specific, FIQ only entry point, and we add calls in traps.c: this implies that each driver has to defend itself against spurious calls.

- (b) We add a separate handle_arch_fiq() indirection that only deals with FIQ. Much better, but it also means that we have to keep this in sync with arm64, for which the interest is relatively limited (FIQ only works if you have a single security domain like XGene, or for a VM).

- (c) We call handle_arch_irq(), and let the interrupt controller code sort the mess.

I really hate (a) with a passion, because it litters both the ARM core code with IC specific code *and* introduce some defensive programming in the IC code, which is a waste...

Quoted 7 lines

Option (b) is nicer, but requires additional work and buy-in from the arm64 maintainers, for a non obvious gain (I quite like the idea of injecting FIQs in a VM though, just for fun...).

Option (c) is the simplest, if a little ugly on the side.

Thoughts?

Quoted 7 lines

For FIQs, do you anticipate handle_arch_irq() having a role like the current gic_handle_fiq_ipi(), which is acknowledge an IPI and get out? Alternatively it could behave more like its current role for IRQ and call into the handlers itself.

The later seems more likely to work out well when I take another look at hooking up the perf interrupt.

Assuming your mention of handle_arch_irq() is actually handle_arch_fiq(), I'd expect some interesting problems if you try to handle a Linux interrupt while already handling one, as the core IRQ code is not designed to be reentrant... Your code works so far because you have been careful to keep the IRQ code at bay. Putting it back into the equation is going to be hairy at best.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#29 lore raw

Re: [RESEND PATCH 4.0-rc7 v20 3/6] irqchip: gic: Introduce plumbing for IPI FIQ

Quoted 127 lines · Marc Zyngier
Quoted 116 lines · On Wed, 22 Apr 2015 13:45:33 +0100 Daniel Thompson…
Quoted 107 lines · Marc Zyngier

On Tue, 21 Apr 2015 22:03:25 +0100
Daniel Thompson <[email protected]> wrote:

Hi Daniel,

Quoted 76 lines · Marc Zyngier
Quoted 52 lines · Daniel Thompson
Quoted 51 lines

Currently it is not possible to exploit FIQ for systems with a GIC, even if the systems are otherwise capable of it. This patch makes it possible for IPIs to be delivered using FIQ.

To do so it modifies the register state so that normal interrupts are placed in group 1 and specific IPIs are placed into group 0. It also configures the controller to raise group 0 interrupts using the FIQ signal. It provides a means for architecture code to define which IPIs shall use FIQ and to acknowledge any IPIs that are raised.

All GIC hardware except GICv1-without-TrustZone support provides a means to group exceptions into group 0 and group 1 but the hardware functionality is unavailable to the kernel when a secure monitor is present because access to the grouping registers are prohibited outside "secure world". However when grouping is not available (or in the case of early GICv1 implementations is very hard to configure) the code to change groups does not deploy and all IPIs will be raised via IRQ.

It has been tested and shown working on two systems capable of supporting grouping (Freescale i.MX6 and STiH416). It has also been tested for boot regressions on two systems that do not support grouping (vexpress-a9 and Qualcomm Snapdragon 600).

Cc Thomas Gleixner <[email protected]>
Cc Jason Cooper <[email protected]>
Cc Russell King <[email protected]>
Cc Marc Zyngier <[email protected]>
---
    arch/arm/kernel/traps.c         |   5 +-
    drivers/irqchip/irq-gic.c       | 151 +++++++++++++++++++++++++++++++++++++---
    include/linux/irqchip/arm-gic.h |   8 +++
    3 files changed, 153 insertions(+), 11 deletions(-)
arch/arm/kernel/traps.c +1 -0
index 788e23fe64d8..b35e220ae1b1 100644
@@ -26,6 +26,7 @@
    #include <linux/init.h>
    #include <linux/sched.h>
    #include <linux/irq.h>
+#include <linux/irqchip/arm-gic.h>
 
    #include <linux/atomic.h>
    #include <asm/cacheflush.h>
@@ -479,7 +480,9 @@ asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
 
           nmi_enter();
 
Quoted 9 lines

arch/arm/kernel/traps.c asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs) · line 485

Quoted 4 lines
arch/arm/kernel/traps.c +3 -1
-       /* nop. FIQ handlers for special arch/arm features can be added here. */
+#ifdef CONFIG_ARM_GIC
+       gic_handle_fiq_ipi();
+#endif

This hunk is what irritates me. It creates a hard dependency between core ARM code and the GIC, and I don't really see how this works with multiplatform, where the interrupt controller is not necessarily a GIC. In that case, you will die a horrible death.

I was just about to reassure you that there is no bug here... but then I read the code.

gic_handle_fiq_ipi() was *supposed* to do a check to make it safe to call when there is no gic meaning multi-platform support could be achieved by calling into multiple handlers.

It looks like I forgot to write the code that would make this possible. Maybe I was too disgusted with the approach to implement it correctly. Looking at this with fresher eyes (I've been having a bit of a break from FIQ recently) I can see how bad the current approach is.

Quoted 6 lines
Quoted 3 lines

Why can't we just call handle_arch_irq(), and let the normal handler do its thing? You can have a "if (in_nmi())" in there, and call your FIQ function. It would at least save us the above problem.

It should certainly work although it feels odd to reuse the IRQ handler for FIQ.

I can see three options:

- (a) Either we have an interrupt controller specific, FIQ only entry point, and we add calls in traps.c: this implies that each driver has to defend itself against spurious calls.

- (b) We add a separate handle_arch_fiq() indirection that only deals with FIQ. Much better, but it also means that we have to keep this in sync with arm64, for which the interest is relatively limited (FIQ only works if you have a single security domain like XGene, or for a VM).

- (c) We call handle_arch_irq(), and let the interrupt controller code sort the mess.

I really hate (a) with a passion, because it litters both the ARM core code with IC specific code *and* introduce some defensive programming in the IC code, which is a waste...

Quoted 7 lines

Option (b) is nicer, but requires additional work and buy-in from the arm64 maintainers, for a non obvious gain (I quite like the idea of injecting FIQs in a VM though, just for fun...).

Option (c) is the simplest, if a little ugly on the side.

Thoughts?

Quoted 7 lines

For FIQs, do you anticipate handle_arch_irq() having a role like the current gic_handle_fiq_ipi(), which is acknowledge an IPI and get out? Alternatively it could behave more like its current role for IRQ and call into the handlers itself.

The later seems more likely to work out well when I take another look at hooking up the perf interrupt.

Quoted 6 lines

Assuming your mention of handle_arch_irq() is actually handle_arch_fiq(), I'd expect some interesting problems if you try to handle a Linux interrupt while already handling one, as the core IRQ code is not designed to be reentrant... Your code works so far because you have been careful to keep the IRQ code at bay. Putting it back into the equation is going to be hairy at best.

I was actually thinking of option (c) but the question would apply in both cases.

To be clear, I agree we cannot call into big piles of irq code from an NMI. We'd have to introduce new NMI-only ways to dispatch FIQs from real hwirqs (SPIs and PPIs).

In fact, at present we can't even call into handle_IPI() at the moment (because it will call irq_enter) although we could try to modify things and make that possible.

These issues apply whether we have conditional code in handle_arch_irq() or if we introduce handle_arch_fiq().

#30 lore raw

Re: [RESEND PATCH 4.0-rc7 v20 3/6] irqchip: gic: Introduce plumbing for IPI FIQ

In reply to Daniel Thompson

Hi,

Quoted 18 lines · Daniel Thompson

Currently it is not possible to exploit FIQ for systems with a GIC, even if the systems are otherwise capable of it. This patch makes it possible for IPIs to be delivered using FIQ.

To do so it modifies the register state so that normal interrupts are placed in group 1 and specific IPIs are placed into group 0. It also configures the controller to raise group 0 interrupts using the FIQ signal. It provides a means for architecture code to define which IPIs shall use FIQ and to acknowledge any IPIs that are raised.

All GIC hardware except GICv1-without-TrustZone support provides a means to group exceptions into group 0 and group 1 but the hardware functionality is unavailable to the kernel when a secure monitor is present because access to the grouping registers are prohibited outside "secure world". However when grouping is not available (or in the case of early GICv1 implementations is very hard to configure) the code to change groups does not deploy and all IPIs will be raised via IRQ.

Quoted 4 lines

It has been tested and shown working on two systems capable of supporting grouping (Freescale i.MX6 and STiH416). It has also been tested for boot regressions on two systems that do not support grouping (vexpress-a9 and Qualcomm Snapdragon 600).

I just gave this a spin on my (non-MCPM) TC2, and secondaries don't come up:

CPU1: failed to boot: -38
CPU2: failed to boot: -38
CPU3: failed to boot: -38
CPU4: failed to boot: -38
Brought up 1 CPUs
SMP: Total of 1 processors activated (48.00 BogoMIPS).

I tried investigating with a debugger. The unbooted CPUs look to be stuck at the FW's spin loop, but the text doesn't look right (I see a load of ADDEQ r0, r0, r0, #LSL 1 where there was previously a WFI loop). That could be a bug with my debugger though.

If I pause the CPUs at the right point, they sometimes enter the kernel successfully. I don't have a good explanation for that.

[...]

drivers/irqchip/irq-gic.c static void gic_cpu_init(struct gic_chip_data *gic) · line 538

Quoted 5 lines
drivers/irqchip/irq-gic.c +1 -0
@@ -427,6 +535,7 @@ static void gic_cpu_init(struct gic_chip_data *gic)
        void __iomem *base = gic_data_cpu_base(gic);
        unsigned int cpu_mask, cpu = smp_processor_id();
        int i;
+       unsigned long secure_irqs, secure_irq;

I think secure_irq(s) is a misnomer here. It's just a mask of FIQ bits.

Quoted 10 lines
drivers/irqchip/irq-gic.c +3 -0
        /*
         * Get what the GIC says our CPU mask is.
@@ -445,6 +554,20 @@ static void gic_cpu_init(struct gic_chip_data *gic)
 
        gic_cpu_config(dist_base, NULL);
 
+       /*
+        * If the distributor is configured to support interrupt grouping
+        * then set any PPI and SGI interrupts not set in SMP_IPI_FIQ_MASK

drivers/irqchip/irq-gic.c static void gic_cpu_init(struct gic_chip_data *gic) · line 569

Quoted 10 lines
drivers/irqchip/irq-gic.c +10 -0
+        * to be group1 and ensure any remaining group 0 interrupts have
+        * the right priority.
+        */
+       if (GICD_ENABLE_GRP1 & readl_relaxed(dist_base + GIC_DIST_CTRL)) {
+               secure_irqs = SMP_IPI_FIQ_MASK;
+               writel_relaxed(~secure_irqs, dist_base + GIC_DIST_IGROUP + 0);
+               gic->igroup0_shadow = ~secure_irqs;
+               for_each_set_bit(secure_irq, &secure_irqs, 16)
+                       gic_set_group_irq(gic, secure_irq, 0);
+       }

This only pokes GICD registers. Why isn't this in gic_dist_init?

Mark.

#31 lore raw

Re: [RESEND PATCH 4.0-rc7 v20 3/6] irqchip: gic: Introduce plumbing for IPI FIQ

Quoted 41 lines · Mark Rutland

Hi,

Quoted 18 lines · Daniel Thompson

Currently it is not possible to exploit FIQ for systems with a GIC, even if the systems are otherwise capable of it. This patch makes it possible for IPIs to be delivered using FIQ.

To do so it modifies the register state so that normal interrupts are placed in group 1 and specific IPIs are placed into group 0. It also configures the controller to raise group 0 interrupts using the FIQ signal. It provides a means for architecture code to define which IPIs shall use FIQ and to acknowledge any IPIs that are raised.

All GIC hardware except GICv1-without-TrustZone support provides a means to group exceptions into group 0 and group 1 but the hardware functionality is unavailable to the kernel when a secure monitor is present because access to the grouping registers are prohibited outside "secure world". However when grouping is not available (or in the case of early GICv1 implementations is very hard to configure) the code to change groups does not deploy and all IPIs will be raised via IRQ.

Quoted 4 lines

It has been tested and shown working on two systems capable of supporting grouping (Freescale i.MX6 and STiH416). It has also been tested for boot regressions on two systems that do not support grouping (vexpress-a9 and Qualcomm Snapdragon 600).

I just gave this a spin on my (non-MCPM) TC2, and secondaries don't come up:

CPU1: failed to boot: -38
CPU2: failed to boot: -38
CPU3: failed to boot: -38
CPU4: failed to boot: -38
Brought up 1 CPUs
SMP: Total of 1 processors activated (48.00 BogoMIPS).

I tried investigating with a debugger. The unbooted CPUs look to be stuck at the FW's spin loop, but the text doesn't look right (I see a load of ADDEQ r0, r0, r0, #LSL 1 where there was previously a WFI loop). That could be a bug with my debugger though.

Quoted 4 lines

If I pause the CPUs at the right point, they sometimes enter the kernel successfully. I don't have a good explanation for that.

[...]

Rats!

I presume it is patch 3 that causes the regression? Patch 3 is the one that causes the GIC to adopt a different configuration if it find the kernel running in secure world (it sets all interrupts to group 1 and routes group 0 to FIQ).

I only ask because it isn't until patch 6 that we actually place any interrupt sources into group 0.

Quoted 7 lines

drivers/irqchip/irq-gic.c static void gic_cpu_init(struct gic_chip_data *gic) · line 538

Quoted 5 lines
drivers/irqchip/irq-gic.c +1 -0
@@ -427,6 +535,7 @@ static void gic_cpu_init(struct gic_chip_data *gic)
         void __iomem *base = gic_data_cpu_base(gic);
         unsigned int cpu_mask, cpu = smp_processor_id();
         int i;
+       unsigned long secure_irqs, secure_irq;

I think secure_irq(s) is a misnomer here. It's just a mask of FIQ bits.

I guess so, on GICv2 without security extentions these are not secure irqs. This is one of the places were IRQ, FIQ, irq and hwirq meet together and naming things is hard.

What sort of name do you like: fiq(s), fiq_hwirq(s)?

Quoted 7 lines
Quoted 7 lines
drivers/irqchip/irq-gic.c +0 -0
         /*
          * Get what the GIC says our CPU mask is.
@@ -445,6 +554,20 @@ static void gic_cpu_init(struct gic_chip_data *gic)
 
         gic_cpu_config(dist_base, NULL);
 
Quoted 15 lines

drivers/irqchip/irq-gic.c static void gic_cpu_init(struct gic_chip_data *gic) · line 569

Quoted 13 lines
drivers/irqchip/irq-gic.c +13 -0
+       /*
+        * If the distributor is configured to support interrupt grouping
+        * then set any PPI and SGI interrupts not set in SMP_IPI_FIQ_MASK
+        * to be group1 and ensure any remaining group 0 interrupts have
+        * the right priority.
+        */
+       if (GICD_ENABLE_GRP1 & readl_relaxed(dist_base + GIC_DIST_CTRL)) {
+               secure_irqs = SMP_IPI_FIQ_MASK;
+               writel_relaxed(~secure_irqs, dist_base + GIC_DIST_IGROUP + 0);
+               gic->igroup0_shadow = ~secure_irqs;
+               for_each_set_bit(secure_irq, &secure_irqs, 16)
+                       gic_set_group_irq(gic, secure_irq, 0);
+       }

This only pokes GICD registers. Why isn't this in gic_dist_init?

GIC_DIST_IGROUP[0] (which controls grouping for SGIs and PPIs) is banked per-cpu and form part of the per-cpu configuration.

Daniel.

#32 lore raw

Re: [RESEND PATCH 4.0-rc7 v20 3/6] irqchip: gic: Introduce plumbing for IPI FIQ

Quoted 22 lines
Quoted 15 lines

I just gave this a spin on my (non-MCPM) TC2, and secondaries don't come up:

CPU1: failed to boot: -38
CPU2: failed to boot: -38
CPU3: failed to boot: -38
CPU4: failed to boot: -38
Brought up 1 CPUs
SMP: Total of 1 processors activated (48.00 BogoMIPS).

I tried investigating with a debugger. The unbooted CPUs look to be stuck at the FW's spin loop, but the text doesn't look right (I see a load of ADDEQ r0, r0, r0, #LSL 1 where there was previously a WFI loop). That could be a bug with my debugger though.

Quoted 4 lines

If I pause the CPUs at the right point, they sometimes enter the kernel successfully. I don't have a good explanation for that.

[...]

Rats!

Quoted 7 lines

I presume it is patch 3 that causes the regression? Patch 3 is the one that causes the GIC to adopt a different configuration if it find the kernel running in secure world (it sets all interrupts to group 1 and routes group 0 to FIQ).

I only ask because it isn't until patch 6 that we actually place any interrupt sources into group 0.

Patch 3 appears to be to blame. I see the issue with patches 1-3 alone applied atop of v4.0. With patch 3 reverted secondaries come up as expected.

Quoted 8 lines
Quoted 7 lines

drivers/irqchip/irq-gic.c static void gic_cpu_init(struct gic_chip_data *gic) · line 538

Quoted 5 lines
drivers/irqchip/irq-gic.c +1 -0
@@ -427,6 +535,7 @@ static void gic_cpu_init(struct gic_chip_data *gic)
         void __iomem *base = gic_data_cpu_base(gic);
         unsigned int cpu_mask, cpu = smp_processor_id();
         int i;
+       unsigned long secure_irqs, secure_irq;

I think secure_irq(s) is a misnomer here. It's just a mask of FIQ bits.

Quoted 5 lines

I guess so, on GICv2 without security extentions these are not secure irqs. This is one of the places were IRQ, FIQ, irq and hwirq meet together and naming things is hard.

What sort of name do you like: fiq(s), fiq_hwirq(s)?

I'd go for fiq_mask and fiq, or group1_mask and group1_irq.

[...]

Quoted 20 lines
Quoted 4 lines
Quoted 4 lines
drivers/irqchip/irq-gic.c +0 -0
@@ -445,6 +554,20 @@ static void gic_cpu_init(struct gic_chip_data *gic)
 
         gic_cpu_config(dist_base, NULL);
 
Quoted 15 lines

drivers/irqchip/irq-gic.c static void gic_cpu_init(struct gic_chip_data *gic) · line 569

Quoted 13 lines
drivers/irqchip/irq-gic.c +13 -0
+       /*
+        * If the distributor is configured to support interrupt grouping
+        * then set any PPI and SGI interrupts not set in SMP_IPI_FIQ_MASK
+        * to be group1 and ensure any remaining group 0 interrupts have
+        * the right priority.
+        */
+       if (GICD_ENABLE_GRP1 & readl_relaxed(dist_base + GIC_DIST_CTRL)) {
+               secure_irqs = SMP_IPI_FIQ_MASK;
+               writel_relaxed(~secure_irqs, dist_base + GIC_DIST_IGROUP + 0);
+               gic->igroup0_shadow = ~secure_irqs;
+               for_each_set_bit(secure_irq, &secure_irqs, 16)
+                       gic_set_group_irq(gic, secure_irq, 0);
+       }

This only pokes GICD registers. Why isn't this in gic_dist_init?

Quoted 2 lines

GIC_DIST_IGROUP[0] (which controls grouping for SGIs and PPIs) is banked per-cpu and form part of the per-cpu configuration.

Ah. Would you mind adding a note to the comment w.r.t. GICD_IGROUPR0 being banked per-cpu? I suspect I won't be the only one who fails to recall that being the case.

We might want to rethink the gic_dist_init/gic_cpu_init naming if they're no longer cleanly split across distributor and cpu interface initialisation.

Thanks,
Mark.

#33 lore raw

Re: [RESEND PATCH 4.0-rc7 v20 3/6] irqchip: gic: Introduce plumbing for IPI FIQ

Quoted 27 lines · Mark Rutland
Quoted 27 lines
Quoted 15 lines

I just gave this a spin on my (non-MCPM) TC2, and secondaries don't come up:

CPU1: failed to boot: -38
CPU2: failed to boot: -38
CPU3: failed to boot: -38
CPU4: failed to boot: -38
Brought up 1 CPUs
SMP: Total of 1 processors activated (48.00 BogoMIPS).

I tried investigating with a debugger. The unbooted CPUs look to be stuck at the FW's spin loop, but the text doesn't look right (I see a load of ADDEQ r0, r0, r0, #LSL 1 where there was previously a WFI loop). That could be a bug with my debugger though.

Quoted 4 lines

If I pause the CPUs at the right point, they sometimes enter the kernel successfully. I don't have a good explanation for that.

[...]

Rats!

I presume it is patch 3 that causes the regression? Patch 3 is the one that causes the GIC to adopt a different configuration if it find the kernel running in secure world (it sets all interrupts to group 1 and routes group 0 to FIQ).

Quoted 6 lines
Quoted 2 lines

I only ask because it isn't until patch 6 that we actually place any interrupt sources into group 0.

Patch 3 appears to be to blame. I see the issue with patches 1-3 alone applied atop of v4.0. With patch 3 reverted secondaries come up as expected.

So I'm back looking at this after a bit of a break.

The problem is almost certainly due to mismanaging the NSATT bit within GICD_SGIR. Specifically we must use a different value for NSATT before a CPU is booted for the first time because that CPU will not have setup its banked copy of IGROUP[0] yet.

I have played with a couple of fixes but I think the simplest is to detect if we are running from secure mode and, if we are, to write to GICD_SGIR twice (once without NSATT, once with).

Note that we do have to detect ourselves to be running from secure mode before trying the double-write approach. If we were running from non-secure mode then the double write could risk two IPIs being generated.

Anyhow the main benefit of this approach is that it is stateless so we don't have to do any state tracking (which I think would require using rwlocks).

I plan to react to the outstanding review comments and roll the fix into the existing patches but, for clarity, here are the fixes that I think are needed to solve the TC2 boot problems. I have tested both from secure and non-secure modes but have not been able to test on TC2.

drivers/irqchip/irq-gic.c +25 -12
index 4f9e4296438c..a7d721e43db6 100644
@@ -73,6 +73,7 @@ struct gic_chip_data {
  	struct irq_domain *domain;
  	unsigned int gic_irqs;
  	u32 igroup0_shadow;
+	bool sgi_with_nsatt;
  #ifdef CONFIG_GIC_NON_BANKED
  	void __iomem *(*get_base)(union gic_base *);
  #endif
@@ -512,16 +513,27 @@ static void __init gic_dist_init(struct gic_chip_data
  	writel_relaxed(GICD_ENABLE_GRP1 | GICD_ENABLE, base + GIC_DIST_CTRL);
 
  	/*
-	 * Set all global interrupts to be group 1 if (and only if) it
-	 * is possible to enable group 1 interrupts. This register is RAZ/WI
-	 * if not accessible or not implemented, however some GICv1 devices
-	 * do not implement the EnableGrp1 bit making it unsafe to set
-	 * this register unconditionally.
+	 * Some GICv1 devices (even those with security extensions) do not
+	 * implement EnableGrp1 meaning some parts of the above write might
+	 * be ignored. We will only enable FIQ support if the bit can be set.
  	 */
-	if (GICD_ENABLE_GRP1 & readl_relaxed(base + GIC_DIST_CTRL))
+	if (GICD_ENABLE_GRP1 & readl_relaxed(base + GIC_DIST_CTRL)) {
+		/*
+		 * Set all global interrupts to be group 1 (signalled with
+		 * IRQ).
+		 */
  		for (i = 32; i < gic_irqs; i += 32)
  			writel_relaxed(0xffffffff,
  				       base + GIC_DIST_IGROUP + i * 4 / 32);
+
+		/*
+		 * If the GIC supports the security extension then SGIs
+		 * will be filtered based on the value of NSATT. If the
+		 * GIC has this support then enable NSATT support.
+		 */
+		if (GICD_SECURITY_EXTN & readl_relaxed(base + GIC_DIST_CTR))
+			gic->sgi_with_nsatt = true;
+	}
  }
 
  static void gic_cpu_init(struct gic_chip_data *gic)
@@ -782,6 +794,7 @@ static void gic_raise_softirq(const struct cpumask *mask,
  	int cpu;
  	unsigned long map = 0;
  	unsigned long softint;
+	void __iomem *dist_base;
 
  	gic_migration_lock();
 
@@ -789,20 +802,20 @@ static void gic_raise_softirq(const struct cpumask *mask,
  	for_each_cpu(cpu, mask)
  		map |= gic_cpu_map[cpu];
 
+	/* This always happens on GIC0 */
+	dist_base = gic_data_dist_base(&gic_data[0]);
+
  	/*
  	 * Ensure that stores to Normal memory are visible to the
  	 * other CPUs before they observe us issuing the IPI.
  	 */
  	dmb(ishst);
 
-	/* We avoid a readl here by using the shadow copy of IGROUP[0] */
  	softint = map << 16 | irq;
-	if (gic_data[0].igroup0_shadow & BIT(irq))
-		softint |= 0x8000;
 
-	/* This always happens on GIC0 */
-	writel_relaxed(softint,
-		       gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
+	writel_relaxed(softint, dist_base + GIC_DIST_SOFTINT);
+	if (gic_data[0].sgi_with_nsatt)
+		writel_relaxed(softint | 0x8000, dist_base + GIC_DIST_SOFTINT);
 
  	gic_migration_unlock();
  }
include/linux/irqchip/arm-gic.h +1 -0
index 361dddfe205a..22cf475e1deb 100644
@@ -50,6 +50,7 @@
  #define GICD_ENABLE			0x1
  #define GICD_ENABLE_GRP1		0x2
  #define GICD_DISABLE			0x0
+#define GICD_SECURITY_EXTN		0x400
  #define GICD_INT_ACTLOW_LVLTRIG		0x0
  #define GICD_INT_EN_CLR_X32		0xffffffff
  #define GICD_INT_EN_SET_SGI		0x0000ffff

--

Daniel.

#34 lore raw

PATCH RESEND v20 4/6 printk: Simple implementation for NMI backtracing

In reply to Daniel Thompson

Currently there is a quite a pile of code sitting in arch/x86/kernel/apic/hw_nmi.c to support safe all-cpu backtracing from NMI. The code is inaccessible to backtrace implementations for other architectures, which is a shame because they would probably like to be safe too.

Copy this code into printk, reworking it a little as we do so to make it easier to exploit as library code.

We'll port the x86 NMI backtrace logic to it in a later patch.

---
 include/linux/printk.h        |  20 ++++++
 init/Kconfig                  |   3 +
 kernel/printk/Makefile        |   1 +
 kernel/printk/nmi_backtrace.c | 147 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 171 insertions(+)
 create mode 100644 kernel/printk/nmi_backtrace.c
include/linux/printk.h +20 -0
index baa3f97d8ce8..44bb85ad1f62 100644
@@ -228,6 +228,26 @@ static inline void show_regs_print_info(const char *log_lvl)
 }
 #endif
 
+#ifdef CONFIG_PRINTK_NMI_BACKTRACE
+/*
+ * printk_nmi_backtrace_prepare/complete are called to prepare the
+ * system for some or all cores to issue trace from NMI.
+ * printk_nmi_backtrace_complete will print buffered output and cannot
+ * (safely) be called from NMI.
+ */
+extern int printk_nmi_backtrace_prepare(void);
+extern void printk_nmi_backtrace_complete(void);
+
+/*
+ * printk_nmi_backtrace_this_cpu_begin/end are used divert/restore printk
+ * on this cpu. The result is the output of printk() (by this CPU) will be
+ * stored in temporary buffers for later printing by
+ * printk_nmi_backtrace_complete.
+ */
+extern void printk_nmi_backtrace_this_cpu_begin(void);
+extern void printk_nmi_backtrace_this_cpu_end(void);
+#endif
+
 extern asmlinkage void dump_stack(void) __cold;
 
 #ifndef pr_fmt
init/Kconfig +3 -0
index f5dbc6d4261b..0107e9b4d2cf 100644
@@ -1421,6 +1421,9 @@ config PRINTK
 	  very difficult to diagnose system problems, saying N here is
 	  strongly discouraged.
 
+config PRINTK_NMI_BACKTRACE
+	bool
+
 config BUG
 	bool "BUG() support" if EXPERT
 	default y
kernel/printk/Makefile +1 -0
index 85405bdcf2b3..1849b001384a 100644
@@ -1,2 +1,3 @@
 obj-y	= printk.o
+obj-$(CONFIG_PRINTK_NMI_BACKTRACE)	+= nmi_backtrace.o
 obj-$(CONFIG_A11Y_BRAILLE_CONSOLE)	+= braille.o
kernel/printk/nmi_backtrace.c new file +147 -0

147 lines not on this page · raw message

Signature · 1 line
2.1.0
#35 lore raw

PATCH RESEND v20 5/6 x86/nmi: Use common printk functions

In reply to Daniel Thompson

Much of the code sitting in arch/x86/kernel/apic/hw_nmi.c to support safe all-cpu backtracing from NMI has been copied to printk.c to make it accessible to other architectures.

Port the x86 NMI backtrace to the generic code.

Cc Steven Rostedt <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc x86 at kernel.org
---
 arch/x86/Kconfig              |   1 +
 arch/x86/kernel/apic/hw_nmi.c | 104 ++++--------------------------------------
 2 files changed, 10 insertions(+), 95 deletions(-)
arch/x86/Kconfig +1 -0
index b7d31ca55187..a1a54570f2d0 100644
@@ -141,6 +141,7 @@ config X86
 	select ACPI_LEGACY_TABLES_LOOKUP if ACPI
 	select X86_FEATURE_NAMES if PROC_FS
 	select SRCU
+	select PRINTK_NMI_BACKTRACE if X86_LOCAL_APIC
 
 config INSTRUCTION_DECODER
 	def_bool y
arch/x86/kernel/apic/hw_nmi.c +9 -95

138 lines not on this page · raw message

Signature · 1 line
2.1.0
#36 lore raw

PATCH RESEND v20 6/6 ARM: Add support for on-demand backtrace of other CPUs

In reply to Daniel Thompson

Replicate the x86 code to trigger a backtrace using an NMI and hook it up to IPI on ARM.

The code differs slightly from the code on x86 because, on ARM, we do now know at compile time whether a platform is capable of supporting FIQ. We must avoid using an IPI to request a backtrace from the CPU on which the backtrace was requested if interrupts are disabled and fall back to generating it directly.

In addition the implementation of arch_trigger_all_cpu_backtrace() the patch also includes a few small items of plumbing that must be hooked up for the new code to work.

Credit:
Russell King provided the initial prototype implementing this feature for ARM. Today the patch has been reworked and, mostly, rewriten to keep it aligned with x86. However this patch does still include some code from Russell's original prototype.

Cc Russell King <[email protected]>
Cc Steven Rostedt <[email protected]>
---
 arch/arm/Kconfig               |  1 +
 arch/arm/include/asm/hardirq.h |  2 +-
 arch/arm/include/asm/irq.h     |  5 +++
 arch/arm/include/asm/smp.h     |  3 ++
 arch/arm/kernel/smp.c          | 82 ++++++++++++++++++++++++++++++++++++++++++
 arch/arm/kernel/traps.c        |  3 ++
 6 files changed, 95 insertions(+), 1 deletion(-)
arch/arm/Kconfig +1 -0
index cf4c0c99aa25..6f412a347cc1 100644
@@ -76,6 +76,7 @@ config ARM
 	select OLD_SIGACTION
 	select OLD_SIGSUSPEND3
 	select PERF_USE_VMALLOC
+	select PRINTK_NMI_BACKTRACE
 	select RTC_LIB
 	select SYS_SUPPORTS_APM_EMULATION
 	# Above selects are sorted alphabetically; please add new ones
arch/arm/include/asm/hardirq.h +1 -1
index fe3ea776dc34..5df33e30ae1b 100644
@@ -5,7 +5,7 @@
 #include <linux/threads.h>
 #include <asm/irq.h>
 
-#define NR_IPI	8
+#define NR_IPI	9
 
 typedef struct {
 	unsigned int __softirq_pending;
arch/arm/include/asm/irq.h +5 -0
index 53c15dec7af6..be1d07d59ee9 100644
@@ -35,6 +35,11 @@ extern void (*handle_arch_irq)(struct pt_regs *);
 extern void set_handle_irq(void (*handle_irq)(struct pt_regs *));
 #endif
 
+#ifdef CONFIG_SMP
+extern void arch_trigger_all_cpu_backtrace(bool);
+#define arch_trigger_all_cpu_backtrace(x) arch_trigger_all_cpu_backtrace(x)
+#endif
+
 #endif
 
 #endif
arch/arm/include/asm/smp.h +3 -0
index 18f5a554134f..b076584ac0fa 100644
@@ -18,6 +18,8 @@
 # error "<asm/smp.h> included in non-SMP build"
 #endif
 
+#define SMP_IPI_FIQ_MASK 0x0100
+
 #define raw_smp_processor_id() (current_thread_info()->cpu)
 
 struct seq_file;
@@ -79,6 +81,7 @@ extern void arch_send_call_function_single_ipi(int cpu);
 extern void arch_send_call_function_ipi_mask(const struct cpumask *mask);
 extern void arch_send_wakeup_ipi_mask(const struct cpumask *mask);
 
+extern void ipi_cpu_backtrace(struct pt_regs *regs);
 extern int register_ipi_completion(struct completion *completion, int cpu);
 
 struct smp_operations {
arch/arm/kernel/smp.c +82 -0

115 lines not on this page · raw message

arch/arm/kernel/traps.c +3 -0
index b35e220ae1b1..1836415b8a5c 100644
@@ -483,6 +483,9 @@ asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
 #ifdef CONFIG_ARM_GIC
 	gic_handle_fiq_ipi();
 #endif
+#ifdef CONFIG_SMP
+	ipi_cpu_backtrace(regs);
+#endif
 
 	nmi_exit();
 
Signature · 1 line
2.1.0
#37 lore raw
In reply to Daniel Thompson
Quoted 6 lines · Daniel Thompson

...
v20:

* Fixed bad coding style (removed "if (0 != func())") (Steven Rostedt and Borislav Petkov).
* Rebased on v4.0-rc7.

I just spotted I incorrectly tagged this with RESEND. That was a mistake on my part (forgot to remove it when I bumped the revision to v20). Sorry.

Today's mail is the first time I have posted v20.

Daniel.

#38 lore raw
In reply to Daniel Thompson
Quoted 6 lines · Daniel Thompson

Jason/Thomas:
Any chance of taking the first five of these patches via the irqchip route? The x86 patch has an ack from Ingo, printk has no explicit maintainer and I've done plenty of bisectability tests on the patchset so leaving the last patch for the next dev. cycle should be no trouble.

First of all I need an acked/reviewed from Marc or Russell, preferrably both of them.

Once that is sorted out, I can move that lot through a seperate branch.

Thanks,

tglx

#39 lore raw
Quoted 11 lines · Thomas Gleixner
Quoted 6 lines · Daniel Thompson

Jason/Thomas:
Any chance of taking the first five of these patches via the irqchip route? The x86 patch has an ack from Ingo, printk has no explicit maintainer and I've done plenty of bisectability tests on the patchset so leaving the last patch for the next dev. cycle should be no trouble.

First of all I need an acked/reviewed from Marc or Russell, preferrably both of them.

I'm on it.

Thanks,

	M.
Signature · 1 line
Jazz is not dead. It just smells funny...