2024-09-09 08:52:07 +00:00
|
|
|
/*
|
|
|
|
* Split spinlock implementation out into its own file, so it can be
|
|
|
|
* compiled in a FTRACE-compatible way.
|
|
|
|
*/
|
|
|
|
#include <linux/kernel_stat.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/log2.h>
|
|
|
|
#include <linux/gfp.h>
|
2024-09-09 08:57:42 +00:00
|
|
|
#include <linux/slab.h>
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
#include <asm/paravirt.h>
|
|
|
|
|
|
|
|
#include <xen/interface/xen.h>
|
|
|
|
#include <xen/events.h>
|
|
|
|
|
|
|
|
#include "xen-ops.h"
|
|
|
|
#include "debugfs.h"
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
enum xen_contention_stat {
|
|
|
|
TAKEN_SLOW,
|
|
|
|
TAKEN_SLOW_PICKUP,
|
|
|
|
TAKEN_SLOW_SPURIOUS,
|
|
|
|
RELEASED_SLOW,
|
|
|
|
RELEASED_SLOW_KICKED,
|
|
|
|
NR_CONTENTION_STATS
|
|
|
|
};
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
#ifdef CONFIG_XEN_DEBUG_FS
|
2024-09-09 08:52:07 +00:00
|
|
|
#define HISTO_BUCKETS 30
|
2024-09-09 08:57:42 +00:00
|
|
|
static struct xen_spinlock_stats
|
|
|
|
{
|
|
|
|
u32 contention_stats[NR_CONTENTION_STATS];
|
2024-09-09 08:52:07 +00:00
|
|
|
u32 histo_spin_blocked[HISTO_BUCKETS+1];
|
|
|
|
u64 time_blocked;
|
|
|
|
} spinlock_stats;
|
|
|
|
|
|
|
|
static u8 zero_stats;
|
|
|
|
|
|
|
|
static inline void check_zero(void)
|
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
u8 ret;
|
|
|
|
u8 old = ACCESS_ONCE(zero_stats);
|
|
|
|
if (unlikely(old)) {
|
|
|
|
ret = cmpxchg(&zero_stats, old, 0);
|
|
|
|
/* This ensures only one fellow resets the stat */
|
|
|
|
if (ret == old)
|
|
|
|
memset(&spinlock_stats, 0, sizeof(spinlock_stats));
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static inline void add_stats(enum xen_contention_stat var, u32 val)
|
|
|
|
{
|
|
|
|
check_zero();
|
|
|
|
spinlock_stats.contention_stats[var] += val;
|
|
|
|
}
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
static inline u64 spin_time_start(void)
|
|
|
|
{
|
|
|
|
return xen_clocksource_read();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __spin_time_accum(u64 delta, u32 *array)
|
|
|
|
{
|
|
|
|
unsigned index = ilog2(delta);
|
|
|
|
|
|
|
|
check_zero();
|
|
|
|
|
|
|
|
if (index < HISTO_BUCKETS)
|
|
|
|
array[index]++;
|
|
|
|
else
|
|
|
|
array[HISTO_BUCKETS]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void spin_time_accum_blocked(u64 start)
|
|
|
|
{
|
|
|
|
u32 delta = xen_clocksource_read() - start;
|
|
|
|
|
|
|
|
__spin_time_accum(delta, spinlock_stats.histo_spin_blocked);
|
|
|
|
spinlock_stats.time_blocked += delta;
|
|
|
|
}
|
|
|
|
#else /* !CONFIG_XEN_DEBUG_FS */
|
2024-09-09 08:57:42 +00:00
|
|
|
static inline void add_stats(enum xen_contention_stat var, u32 val)
|
|
|
|
{
|
|
|
|
}
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
static inline u64 spin_time_start(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void spin_time_accum_blocked(u64 start)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_XEN_DEBUG_FS */
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
struct xen_lock_waiting {
|
|
|
|
struct arch_spinlock *lock;
|
|
|
|
__ticket_t want;
|
2024-09-09 08:52:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
|
2024-09-09 08:57:42 +00:00
|
|
|
static DEFINE_PER_CPU(char *, irq_name);
|
|
|
|
static DEFINE_PER_CPU(struct xen_lock_waiting, lock_waiting);
|
|
|
|
static cpumask_t waiting_cpus;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static bool xen_pvspin = true;
|
|
|
|
__visible void xen_lock_spinning(struct arch_spinlock *lock, __ticket_t want)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
|
|
|
int irq = __this_cpu_read(lock_kicker_irq);
|
2024-09-09 08:57:42 +00:00
|
|
|
struct xen_lock_waiting *w = this_cpu_ptr(&lock_waiting);
|
|
|
|
int cpu = smp_processor_id();
|
2024-09-09 08:52:07 +00:00
|
|
|
u64 start;
|
2024-09-09 08:57:42 +00:00
|
|
|
unsigned long flags;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
/* If kicker interrupts not initialized yet, just spin */
|
|
|
|
if (irq == -1)
|
2024-09-09 08:57:42 +00:00
|
|
|
return;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
start = spin_time_start();
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/*
|
|
|
|
* Make sure an interrupt handler can't upset things in a
|
|
|
|
* partially setup state.
|
|
|
|
*/
|
|
|
|
local_irq_save(flags);
|
|
|
|
/*
|
|
|
|
* We don't really care if we're overwriting some other
|
|
|
|
* (lock,want) pair, as that would mean that we're currently
|
|
|
|
* in an interrupt context, and the outer context had
|
|
|
|
* interrupts enabled. That has already kicked the VCPU out
|
|
|
|
* of xen_poll_irq(), so it will just return spuriously and
|
|
|
|
* retry with newly setup (lock,want).
|
|
|
|
*
|
|
|
|
* The ordering protocol on this is that the "lock" pointer
|
|
|
|
* may only be set non-NULL if the "want" ticket is correct.
|
|
|
|
* If we're updating "want", we must first clear "lock".
|
|
|
|
*/
|
|
|
|
w->lock = NULL;
|
|
|
|
smp_wmb();
|
|
|
|
w->want = want;
|
|
|
|
smp_wmb();
|
|
|
|
w->lock = lock;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* This uses set_bit, which atomic and therefore a barrier */
|
|
|
|
cpumask_set_cpu(cpu, &waiting_cpus);
|
|
|
|
add_stats(TAKEN_SLOW, 1);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* clear pending */
|
|
|
|
xen_clear_irq_pending(irq);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* Only check lock once pending cleared */
|
|
|
|
barrier();
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/*
|
|
|
|
* Mark entry to slowpath before doing the pickup test to make
|
|
|
|
* sure we don't deadlock with an unlocker.
|
|
|
|
*/
|
|
|
|
__ticket_enter_slowpath(lock);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/*
|
|
|
|
* check again make sure it didn't become free while
|
|
|
|
* we weren't looking
|
|
|
|
*/
|
|
|
|
if (ACCESS_ONCE(lock->tickets.head) == want) {
|
|
|
|
add_stats(TAKEN_SLOW_PICKUP, 1);
|
|
|
|
goto out;
|
|
|
|
}
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* Allow interrupts while blocked */
|
|
|
|
local_irq_restore(flags);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/*
|
|
|
|
* If an interrupt happens here, it will leave the wakeup irq
|
|
|
|
* pending, which will cause xen_poll_irq() to return
|
|
|
|
* immediately.
|
|
|
|
*/
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* Block until irq becomes pending (or perhaps a spurious wakeup) */
|
|
|
|
xen_poll_irq(irq);
|
|
|
|
add_stats(TAKEN_SLOW_SPURIOUS, !xen_test_irq_pending(irq));
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
local_irq_save(flags);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
kstat_incr_irq_this_cpu(irq);
|
2024-09-09 08:52:07 +00:00
|
|
|
out:
|
2024-09-09 08:57:42 +00:00
|
|
|
cpumask_clear_cpu(cpu, &waiting_cpus);
|
|
|
|
w->lock = NULL;
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
local_irq_restore(flags);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
spin_time_accum_blocked(start);
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
2024-09-09 08:57:42 +00:00
|
|
|
PV_CALLEE_SAVE_REGS_THUNK(xen_lock_spinning);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
static void xen_unlock_kick(struct arch_spinlock *lock, __ticket_t next)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
add_stats(RELEASED_SLOW, 1);
|
|
|
|
|
|
|
|
for_each_cpu(cpu, &waiting_cpus) {
|
|
|
|
const struct xen_lock_waiting *w = &per_cpu(lock_waiting, cpu);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/* Make sure we read lock before want */
|
|
|
|
if (ACCESS_ONCE(w->lock) == lock &&
|
|
|
|
ACCESS_ONCE(w->want) == next) {
|
|
|
|
add_stats(RELEASED_SLOW_KICKED, 1);
|
2024-09-09 08:52:07 +00:00
|
|
|
xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t dummy_handler(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
BUG();
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
void xen_init_lock_cpu(int cpu)
|
2024-09-09 08:52:07 +00:00
|
|
|
{
|
|
|
|
int irq;
|
2024-09-09 08:57:42 +00:00
|
|
|
char *name;
|
|
|
|
|
|
|
|
if (!xen_pvspin)
|
|
|
|
return;
|
|
|
|
|
|
|
|
WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
|
|
|
|
cpu, per_cpu(lock_kicker_irq, cpu));
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
|
|
|
|
irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
|
|
|
|
cpu,
|
|
|
|
dummy_handler,
|
2024-09-09 08:57:42 +00:00
|
|
|
IRQF_PERCPU|IRQF_NOBALANCING,
|
2024-09-09 08:52:07 +00:00
|
|
|
name,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (irq >= 0) {
|
|
|
|
disable_irq(irq); /* make sure it's never delivered */
|
|
|
|
per_cpu(lock_kicker_irq, cpu) = irq;
|
2024-09-09 08:57:42 +00:00
|
|
|
per_cpu(irq_name, cpu) = name;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printk("cpu %d spinlock event irq %d\n", cpu, irq);
|
|
|
|
}
|
|
|
|
|
|
|
|
void xen_uninit_lock_cpu(int cpu)
|
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
if (!xen_pvspin)
|
|
|
|
return;
|
|
|
|
|
2024-09-09 08:52:07 +00:00
|
|
|
unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL);
|
2024-09-09 08:57:42 +00:00
|
|
|
per_cpu(lock_kicker_irq, cpu) = -1;
|
|
|
|
kfree(per_cpu(irq_name, cpu));
|
|
|
|
per_cpu(irq_name, cpu) = NULL;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Our init of PV spinlocks is split in two init functions due to us
|
|
|
|
* using paravirt patching and jump labels patching and having to do
|
|
|
|
* all of this before SMP code is invoked.
|
|
|
|
*
|
|
|
|
* The paravirt patching needs to be done _before_ the alternative asm code
|
|
|
|
* is started, otherwise we would not patch the core kernel code.
|
|
|
|
*/
|
2024-09-09 08:52:07 +00:00
|
|
|
void __init xen_init_spinlocks(void)
|
|
|
|
{
|
2024-09-09 08:57:42 +00:00
|
|
|
|
|
|
|
if (!xen_pvspin) {
|
|
|
|
printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printk(KERN_DEBUG "xen: PV spinlocks enabled\n");
|
|
|
|
pv_lock_ops.lock_spinning = PV_CALLEE_SAVE(xen_lock_spinning);
|
|
|
|
pv_lock_ops.unlock_kick = xen_unlock_kick;
|
2024-09-09 08:52:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
/*
|
|
|
|
* While the jump_label init code needs to happend _after_ the jump labels are
|
|
|
|
* enabled and before SMP is started. Hence we use pre-SMP initcall level
|
|
|
|
* init. We cannot do it in xen_init_spinlocks as that is done before
|
|
|
|
* jump labels are activated.
|
|
|
|
*/
|
|
|
|
static __init int xen_init_spinlocks_jump(void)
|
|
|
|
{
|
|
|
|
if (!xen_pvspin)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!xen_domain())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
static_key_slow_inc(¶virt_ticketlocks_enabled);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
early_initcall(xen_init_spinlocks_jump);
|
|
|
|
|
|
|
|
static __init int xen_parse_nopvspin(char *arg)
|
|
|
|
{
|
|
|
|
xen_pvspin = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
early_param("xen_nopvspin", xen_parse_nopvspin);
|
|
|
|
|
2024-09-09 08:52:07 +00:00
|
|
|
#ifdef CONFIG_XEN_DEBUG_FS
|
|
|
|
|
|
|
|
static struct dentry *d_spin_debug;
|
|
|
|
|
|
|
|
static int __init xen_spinlock_debugfs(void)
|
|
|
|
{
|
|
|
|
struct dentry *d_xen = xen_init_debugfs();
|
|
|
|
|
|
|
|
if (d_xen == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2024-09-09 08:57:42 +00:00
|
|
|
if (!xen_pvspin)
|
|
|
|
return 0;
|
|
|
|
|
2024-09-09 08:52:07 +00:00
|
|
|
d_spin_debug = debugfs_create_dir("spinlocks", d_xen);
|
|
|
|
|
|
|
|
debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);
|
|
|
|
|
|
|
|
debugfs_create_u32("taken_slow", 0444, d_spin_debug,
|
2024-09-09 08:57:42 +00:00
|
|
|
&spinlock_stats.contention_stats[TAKEN_SLOW]);
|
2024-09-09 08:52:07 +00:00
|
|
|
debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
|
2024-09-09 08:57:42 +00:00
|
|
|
&spinlock_stats.contention_stats[TAKEN_SLOW_PICKUP]);
|
2024-09-09 08:52:07 +00:00
|
|
|
debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug,
|
2024-09-09 08:57:42 +00:00
|
|
|
&spinlock_stats.contention_stats[TAKEN_SLOW_SPURIOUS]);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
debugfs_create_u32("released_slow", 0444, d_spin_debug,
|
2024-09-09 08:57:42 +00:00
|
|
|
&spinlock_stats.contention_stats[RELEASED_SLOW]);
|
2024-09-09 08:52:07 +00:00
|
|
|
debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
|
2024-09-09 08:57:42 +00:00
|
|
|
&spinlock_stats.contention_stats[RELEASED_SLOW_KICKED]);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
debugfs_create_u64("time_blocked", 0444, d_spin_debug,
|
|
|
|
&spinlock_stats.time_blocked);
|
2024-09-09 08:57:42 +00:00
|
|
|
|
|
|
|
debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
|
|
|
|
spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
|
2024-09-09 08:52:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fs_initcall(xen_spinlock_debugfs);
|
|
|
|
|
|
|
|
#endif /* CONFIG_XEN_DEBUG_FS */
|