Skip to content

Commit 8bf0a80

Browse files
mrutland-armctmarinas
authored andcommitted
arm64: add ARM64_HAS_GIC_PRIO_RELAXED_SYNC cpucap
When Priority Mask Hint Enable (PMHE) == 0b1, the GIC may use the PMR value to determine whether to signal an IRQ to a PE, and consequently after a change to the PMR value, a DSB SY may be required to ensure that interrupts are signalled to a CPU in finite time. When PMHE == 0b0, interrupts are always signalled to the relevant PE, and all masking occurs locally, without requiring a DSB SY. Since commit: f226650 ("arm64: Relax ICC_PMR_EL1 accesses when ICC_CTLR_EL1.PMHE is clear") ... we handle this dynamically: in most cases a static key is used to determine whether to issue a DSB SY, but the entry code must read from ICC_CTLR_EL1 as static keys aren't accessible from plain assembly. It would be much nicer to use an alternative instruction sequence for the DSB, as this would avoid the need to read from ICC_CTLR_EL1 in the entry code, and for most other code this will result in simpler code generation with fewer instructions and fewer branches. This patch adds a new ARM64_HAS_GIC_PRIO_RELAXED_SYNC cpucap which is only set when ICC_CTLR_EL1.PMHE == 0b0 (and GIC priority masking is in use). This allows us to replace the existing users of the `gic_pmr_sync` static key with alternative sequences which default to a DSB SY and are relaxed to a NOP when PMHE is not in use. The entry assembly management of the PMR is slightly restructured to use a branch (rather than multiple NOPs) when priority masking is not in use. This is more in keeping with other alternatives in the entry assembly, and permits the use of a separate alternatives for the PMHE-dependent DSB SY (and removal of the conditional branch this currently requires). For consistency I've adjusted both the save and restore paths. According to bloat-o-meter, when building defconfig + CONFIG_ARM64_PSEUDO_NMI=y this shrinks the kernel text by ~4KiB: | add/remove: 4/2 grow/shrink: 42/310 up/down: 332/-5032 (-4700) The resulting vmlinux is ~66KiB smaller, though the resulting Image size is unchanged due to padding and alignment: | [mark@lakrids:~/src/linux]% ls -al vmlinux-* | -rwxr-xr-x 1 mark mark 137508344 Jan 17 14:11 vmlinux-after | -rwxr-xr-x 1 mark mark 137575440 Jan 17 13:49 vmlinux-before | [mark@lakrids:~/src/linux]% ls -al Image-* | -rw-r--r-- 1 mark mark 38777344 Jan 17 14:11 Image-after | -rw-r--r-- 1 mark mark 38777344 Jan 17 13:49 Image-before Prior to this patch we did not verify the state of ICC_CTLR_EL1.PMHE on secondary CPUs. As of this patch this is verified by the cpufeature code when using GIC priority masking (i.e. when using pseudo-NMIs). Note that since commit: 7e3a57f ("arm64: Document ICC_CTLR_EL3.PMHE setting requirements") ... Documentation/arm64/booting.rst specifies: | - ICC_CTLR_EL3.PMHE (bit 6) must be set to the same value across | all CPUs the kernel is executing on, and must stay constant | for the lifetime of the kernel. ... so that should not adversely affect any compliant systems, and as we'll only check for the absense of PMHE when using pseudo-NMIs, this will only fire when such mismatch will adversely affect the system. Signed-off-by: Mark Rutland <[email protected]> Reviewed-by: Marc Zyngier <[email protected]> Cc: Mark Brown <[email protected]> Cc: Will Deacon <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent 4b43f1c commit 8bf0a80

File tree

8 files changed

+71
-33
lines changed

8 files changed

+71
-33
lines changed

arch/arm/include/asm/arch_gicv3.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,10 @@ static inline void gic_arch_enable_irqs(void)
252252
WARN_ON_ONCE(true);
253253
}
254254

255+
static inline bool gic_has_relaxed_pmr_sync(void)
256+
{
257+
return false;
258+
}
259+
255260
#endif /* !__ASSEMBLY__ */
256261
#endif /* !__ASM_ARCH_GICV3_H */

arch/arm64/include/asm/arch_gicv3.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,10 @@ static inline void gic_arch_enable_irqs(void)
190190
asm volatile ("msr daifclr, #3" : : : "memory");
191191
}
192192

193+
static inline bool gic_has_relaxed_pmr_sync(void)
194+
{
195+
return cpus_have_cap(ARM64_HAS_GIC_PRIO_RELAXED_SYNC);
196+
}
197+
193198
#endif /* __ASSEMBLY__ */
194199
#endif /* __ASM_ARCH_GICV3_H */

arch/arm64/include/asm/barrier.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <linux/kasan-checks.h>
1313

14+
#include <asm/alternative-macros.h>
15+
1416
#define __nops(n) ".rept " #n "\nnop\n.endr\n"
1517
#define nops(n) asm volatile(__nops(n))
1618

@@ -41,10 +43,11 @@
4143
#ifdef CONFIG_ARM64_PSEUDO_NMI
4244
#define pmr_sync() \
4345
do { \
44-
extern struct static_key_false gic_pmr_sync; \
45-
\
46-
if (static_branch_unlikely(&gic_pmr_sync)) \
47-
dsb(sy); \
46+
asm volatile( \
47+
ALTERNATIVE_CB("dsb sy", \
48+
ARM64_HAS_GIC_PRIO_RELAXED_SYNC, \
49+
alt_cb_patch_nops) \
50+
); \
4851
} while(0)
4952
#else
5053
#define pmr_sync() do {} while (0)

arch/arm64/kernel/cpufeature.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,6 +2056,34 @@ static bool can_use_gic_priorities(const struct arm64_cpu_capabilities *entry,
20562056

20572057
return enable_pseudo_nmi;
20582058
}
2059+
2060+
static bool has_gic_prio_relaxed_sync(const struct arm64_cpu_capabilities *entry,
2061+
int scope)
2062+
{
2063+
/*
2064+
* If we're not using priority masking then we won't be poking PMR_EL1,
2065+
* and there's no need to relax synchronization of writes to it, and
2066+
* ICC_CTLR_EL1 might not be accessible and we must avoid reads from
2067+
* that.
2068+
*
2069+
* ARM64_HAS_GIC_PRIO_MASKING has a lower index, and is a boot CPU
2070+
* feature, so will be detected earlier.
2071+
*/
2072+
BUILD_BUG_ON(ARM64_HAS_GIC_PRIO_RELAXED_SYNC <= ARM64_HAS_GIC_PRIO_MASKING);
2073+
if (!cpus_have_cap(ARM64_HAS_GIC_PRIO_MASKING))
2074+
return false;
2075+
2076+
/*
2077+
* When Priority Mask Hint Enable (PMHE) == 0b0, PMR is not used as a
2078+
* hint for interrupt distribution, a DSB is not necessary when
2079+
* unmasking IRQs via PMR, and we can relax the barrier to a NOP.
2080+
*
2081+
* Linux itself doesn't use 1:N distribution, so has no need to
2082+
* set PMHE. The only reason to have it set is if EL3 requires it
2083+
* (and we can't change it).
2084+
*/
2085+
return (gic_read_ctlr() & ICC_CTLR_EL1_PMHE_MASK) == 0;
2086+
}
20592087
#endif
20602088

20612089
#ifdef CONFIG_ARM64_BTI
@@ -2546,6 +2574,14 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
25462574
.type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
25472575
.matches = can_use_gic_priorities,
25482576
},
2577+
{
2578+
/*
2579+
* Depends on ARM64_HAS_GIC_PRIO_MASKING
2580+
*/
2581+
.capability = ARM64_HAS_GIC_PRIO_RELAXED_SYNC,
2582+
.type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
2583+
.matches = has_gic_prio_relaxed_sync,
2584+
},
25492585
#endif
25502586
#ifdef CONFIG_ARM64_E0PD
25512587
{

arch/arm64/kernel/entry.S

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,16 @@ alternative_else_nop_endif
311311
.endif
312312

313313
#ifdef CONFIG_ARM64_PSEUDO_NMI
314-
/* Save pmr */
315-
alternative_if ARM64_HAS_GIC_PRIO_MASKING
314+
alternative_if_not ARM64_HAS_GIC_PRIO_MASKING
315+
b .Lskip_pmr_save\@
316+
alternative_else_nop_endif
317+
316318
mrs_s x20, SYS_ICC_PMR_EL1
317319
str x20, [sp, #S_PMR_SAVE]
318320
mov x20, #GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET
319321
msr_s SYS_ICC_PMR_EL1, x20
320-
alternative_else_nop_endif
322+
323+
.Lskip_pmr_save\@:
321324
#endif
322325

323326
/*
@@ -336,15 +339,19 @@ alternative_else_nop_endif
336339
.endif
337340

338341
#ifdef CONFIG_ARM64_PSEUDO_NMI
339-
/* Restore pmr */
340-
alternative_if ARM64_HAS_GIC_PRIO_MASKING
342+
alternative_if_not ARM64_HAS_GIC_PRIO_MASKING
343+
b .Lskip_pmr_restore\@
344+
alternative_else_nop_endif
345+
341346
ldr x20, [sp, #S_PMR_SAVE]
342347
msr_s SYS_ICC_PMR_EL1, x20
343-
mrs_s x21, SYS_ICC_CTLR_EL1
344-
tbz x21, #6, .L__skip_pmr_sync\@ // Check for ICC_CTLR_EL1.PMHE
345-
dsb sy // Ensure priority change is seen by redistributor
346-
.L__skip_pmr_sync\@:
348+
349+
/* Ensure priority change is seen by redistributor */
350+
alternative_if_not ARM64_HAS_GIC_PRIO_RELAXED_SYNC
351+
dsb sy
347352
alternative_else_nop_endif
353+
354+
.Lskip_pmr_restore\@:
348355
#endif
349356

350357
ldp x21, x22, [sp, #S_PC] // load ELR, SPSR

arch/arm64/kernel/image-vars.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ KVM_NVHE_ALIAS(__hyp_stub_vectors);
6767
KVM_NVHE_ALIAS(vgic_v2_cpuif_trap);
6868
KVM_NVHE_ALIAS(vgic_v3_cpuif_trap);
6969

70-
/* Static key checked in pmr_sync(). */
7170
#ifdef CONFIG_ARM64_PSEUDO_NMI
72-
KVM_NVHE_ALIAS(gic_pmr_sync);
7371
/* Static key checked in GIC_PRIO_IRQOFF. */
7472
KVM_NVHE_ALIAS(gic_nonsecure_priorities);
7573
#endif

arch/arm64/tools/cpucaps

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ HAS_GENERIC_AUTH_ARCH_QARMA5
3030
HAS_GENERIC_AUTH_IMP_DEF
3131
HAS_GIC_CPUIF_SYSREGS
3232
HAS_GIC_PRIO_MASKING
33+
HAS_GIC_PRIO_RELAXED_SYNC
3334
HAS_LDAPR
3435
HAS_LSE_ATOMICS
3536
HAS_NO_FPSIMD

drivers/irqchip/irq-gic-v3.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,6 @@ static DEFINE_STATIC_KEY_TRUE(supports_deactivate_key);
8989
*/
9090
static DEFINE_STATIC_KEY_FALSE(supports_pseudo_nmis);
9191

92-
/*
93-
* Global static key controlling whether an update to PMR allowing more
94-
* interrupts requires to be propagated to the redistributor (DSB SY).
95-
* And this needs to be exported for modules to be able to enable
96-
* interrupts...
97-
*/
98-
DEFINE_STATIC_KEY_FALSE(gic_pmr_sync);
99-
EXPORT_SYMBOL(gic_pmr_sync);
100-
10192
DEFINE_STATIC_KEY_FALSE(gic_nonsecure_priorities);
10293
EXPORT_SYMBOL(gic_nonsecure_priorities);
10394

@@ -1768,16 +1759,8 @@ static void gic_enable_nmi_support(void)
17681759
for (i = 0; i < gic_data.ppi_nr; i++)
17691760
refcount_set(&ppi_nmi_refs[i], 0);
17701761

1771-
/*
1772-
* Linux itself doesn't use 1:N distribution, so has no need to
1773-
* set PMHE. The only reason to have it set is if EL3 requires it
1774-
* (and we can't change it).
1775-
*/
1776-
if (gic_read_ctlr() & ICC_CTLR_EL1_PMHE_MASK)
1777-
static_branch_enable(&gic_pmr_sync);
1778-
17791762
pr_info("Pseudo-NMIs enabled using %s ICC_PMR_EL1 synchronisation\n",
1780-
static_branch_unlikely(&gic_pmr_sync) ? "forced" : "relaxed");
1763+
gic_has_relaxed_pmr_sync() ? "relaxed" : "forced");
17811764

17821765
/*
17831766
* How priority values are used by the GIC depends on two things:

0 commit comments

Comments
 (0)