lkml.org 
[lkml]   [2021]   [Oct]   [13]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Date
SubjectRe: [PATCH] x86/apic: reduce cache line misses in __x2apic_send_IPI_mask()
On Tue, Oct 12, 2021 at 5:46 AM Peter Zijlstra <peterz@infradead.org> wrote:

> I'm really conflicted about this. On the one hand, yes absolutely. On
> the other hand, urgh, code ugly :-)

That was indeed some ugly hack.

I cooked this more generic patch instead, I am currently testing it.
(generic as : we no longer disable hard irqs, regardless of some CONFIG option )

diff --git a/arch/x86/kernel/apic/x2apic_cluster.c
b/arch/x86/kernel/apic/x2apic_cluster.c
index e696e22d0531976f7cba72ed17443592eac72c13..7ad81467ce33349dee1ceaf0cefc8375d60213f6
100644
--- a/arch/x86/kernel/apic/x2apic_cluster.c
+++ b/arch/x86/kernel/apic/x2apic_cluster.c
@@ -22,7 +22,10 @@ struct cluster_mask {
*/
static u32 *x86_cpu_to_logical_apicid __read_mostly;

-static DEFINE_PER_CPU(cpumask_var_t, ipi_mask);
+#define IPI_NEST_MAX 3
+static DEFINE_PER_CPU(cpumask_var_t, ipi_mask[IPI_NEST_MAX]);
+static DEFINE_PER_CPU(int, ipi_nest_level);
+
static DEFINE_PER_CPU_READ_MOSTLY(struct cluster_mask *, cluster_masks);
static struct cluster_mask *cluster_hotplug_mask;

@@ -45,14 +48,18 @@ __x2apic_send_IPI_mask(const struct cpumask *mask,
int vector, int apic_dest)
{
unsigned int cpu, clustercpu;
struct cpumask *tmpmsk;
- unsigned long flags;
+ int nest_level;
u32 dest;

/* x2apic MSRs are special and need a special fence: */
weak_wrmsr_fence();
- local_irq_save(flags);

- tmpmsk = this_cpu_cpumask_var_ptr(ipi_mask);
+ preempt_disable();
+ nest_level = this_cpu_inc_return(ipi_nest_level) - 1;
+ if (WARN_ON_ONCE(nest_level >= IPI_NEST_MAX))
+ goto end;
+
+ tmpmsk = this_cpu_cpumask_var_ptr(ipi_mask[nest_level]);
cpumask_copy(tmpmsk, mask);
/* If IPI should not be sent to self, clear current CPU */
if (apic_dest != APIC_DEST_ALLINC)
@@ -74,7 +81,9 @@ __x2apic_send_IPI_mask(const struct cpumask *mask,
int vector, int apic_dest)
cpumask_andnot(tmpmsk, tmpmsk, &cmsk->mask);
}

- local_irq_restore(flags);
+end:
+ this_cpu_dec(ipi_nest_level);
+ preempt_enable();
}

static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector)
@@ -153,20 +162,26 @@ static int alloc_clustermask(unsigned int cpu, int node)

static int x2apic_prepare_cpu(unsigned int cpu)
{
+ int i;
+
if (alloc_clustermask(cpu, cpu_to_node(cpu)) < 0)
return -ENOMEM;
- if (!zalloc_cpumask_var(&per_cpu(ipi_mask, cpu), GFP_KERNEL))
- return -ENOMEM;
+ for (i = 0 ; i < IPI_NEST_MAX; i++) {
+ if (!zalloc_cpumask_var(&per_cpu(ipi_mask[i], cpu), GFP_KERNEL))
+ return -ENOMEM;
+ }
return 0;
}

static int x2apic_dead_cpu(unsigned int dead_cpu)
{
struct cluster_mask *cmsk = per_cpu(cluster_masks, dead_cpu);
+ int i;

if (cmsk)
cpumask_clear_cpu(dead_cpu, &cmsk->mask);
- free_cpumask_var(per_cpu(ipi_mask, dead_cpu));
+ for (i = 0; i < IPI_NEST_MAX; i++)
+ free_cpumask_var(per_cpu(ipi_mask[i], dead_cpu));
return 0;
}
\
 
 \ /
  Last update: 2021-10-13 20:04    [W:0.085 / U:0.132 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site