Skip to content

Commit 68bb7bf

Browse files
kattisrinivasanKAGA-KOKO
authored andcommitted
X86/Hyper-V: Enable IPI enlightenments
Hyper-V supports hypercalls to implement IPI; use them. Signed-off-by: K. Y. Srinivasan <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Michael Kelley <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent 6b48cb5 commit 68bb7bf

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

arch/x86/hyperv/hv_apic.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#ifdef CONFIG_X86_64
3434
#if IS_ENABLED(CONFIG_HYPERV)
3535

36+
static struct apic orig_apic;
37+
3638
static u64 hv_apic_icr_read(void)
3739
{
3840
u64 reg_val;
@@ -88,8 +90,123 @@ static void hv_apic_eoi_write(u32 reg, u32 val)
8890
wrmsr(HV_X64_MSR_EOI, val, 0);
8991
}
9092

93+
/*
94+
* IPI implementation on Hyper-V.
95+
*/
96+
static bool __send_ipi_mask(const struct cpumask *mask, int vector)
97+
{
98+
int cur_cpu, vcpu;
99+
struct ipi_arg_non_ex **arg;
100+
struct ipi_arg_non_ex *ipi_arg;
101+
int ret = 1;
102+
unsigned long flags;
103+
104+
if (cpumask_empty(mask))
105+
return true;
106+
107+
if (!hv_hypercall_pg)
108+
return false;
109+
110+
if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
111+
return false;
112+
113+
local_irq_save(flags);
114+
arg = (struct ipi_arg_non_ex **)this_cpu_ptr(hyperv_pcpu_input_arg);
115+
116+
ipi_arg = *arg;
117+
if (unlikely(!ipi_arg))
118+
goto ipi_mask_done;
119+
120+
ipi_arg->vector = vector;
121+
ipi_arg->reserved = 0;
122+
ipi_arg->cpu_mask = 0;
123+
124+
for_each_cpu(cur_cpu, mask) {
125+
vcpu = hv_cpu_number_to_vp_number(cur_cpu);
126+
/*
127+
* This particular version of the IPI hypercall can
128+
* only target upto 64 CPUs.
129+
*/
130+
if (vcpu >= 64)
131+
goto ipi_mask_done;
132+
133+
__set_bit(vcpu, (unsigned long *)&ipi_arg->cpu_mask);
134+
}
135+
136+
ret = hv_do_hypercall(HVCALL_SEND_IPI, ipi_arg, NULL);
137+
138+
ipi_mask_done:
139+
local_irq_restore(flags);
140+
return ((ret == 0) ? true : false);
141+
}
142+
143+
static bool __send_ipi_one(int cpu, int vector)
144+
{
145+
struct cpumask mask = CPU_MASK_NONE;
146+
147+
cpumask_set_cpu(cpu, &mask);
148+
return __send_ipi_mask(&mask, vector);
149+
}
150+
151+
static void hv_send_ipi(int cpu, int vector)
152+
{
153+
if (!__send_ipi_one(cpu, vector))
154+
orig_apic.send_IPI(cpu, vector);
155+
}
156+
157+
static void hv_send_ipi_mask(const struct cpumask *mask, int vector)
158+
{
159+
if (!__send_ipi_mask(mask, vector))
160+
orig_apic.send_IPI_mask(mask, vector);
161+
}
162+
163+
static void hv_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
164+
{
165+
unsigned int this_cpu = smp_processor_id();
166+
struct cpumask new_mask;
167+
const struct cpumask *local_mask;
168+
169+
cpumask_copy(&new_mask, mask);
170+
cpumask_clear_cpu(this_cpu, &new_mask);
171+
local_mask = &new_mask;
172+
if (!__send_ipi_mask(local_mask, vector))
173+
orig_apic.send_IPI_mask_allbutself(mask, vector);
174+
}
175+
176+
static void hv_send_ipi_allbutself(int vector)
177+
{
178+
hv_send_ipi_mask_allbutself(cpu_online_mask, vector);
179+
}
180+
181+
static void hv_send_ipi_all(int vector)
182+
{
183+
if (!__send_ipi_mask(cpu_online_mask, vector))
184+
orig_apic.send_IPI_all(vector);
185+
}
186+
187+
static void hv_send_ipi_self(int vector)
188+
{
189+
if (!__send_ipi_one(smp_processor_id(), vector))
190+
orig_apic.send_IPI_self(vector);
191+
}
192+
91193
void __init hv_apic_init(void)
92194
{
195+
if (ms_hyperv.hints & HV_X64_CLUSTER_IPI_RECOMMENDED) {
196+
pr_info("Hyper-V: Using IPI hypercalls\n");
197+
/*
198+
* Set the IPI entry points.
199+
*/
200+
orig_apic = *apic;
201+
202+
apic->send_IPI = hv_send_ipi;
203+
apic->send_IPI_mask = hv_send_ipi_mask;
204+
apic->send_IPI_mask_allbutself = hv_send_ipi_mask_allbutself;
205+
apic->send_IPI_allbutself = hv_send_ipi_allbutself;
206+
apic->send_IPI_all = hv_send_ipi_all;
207+
apic->send_IPI_self = hv_send_ipi_self;
208+
}
209+
93210
if (ms_hyperv.hints & HV_X64_APIC_ACCESS_RECOMMENDED) {
94211
pr_info("Hyper-V: Using MSR based APIC access\n");
95212
apic_set_eoi_write(hv_apic_eoi_write);

arch/x86/hyperv/hv_init.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,19 @@ EXPORT_SYMBOL_GPL(hv_vp_index);
9191
struct hv_vp_assist_page **hv_vp_assist_page;
9292
EXPORT_SYMBOL_GPL(hv_vp_assist_page);
9393

94+
void __percpu **hyperv_pcpu_input_arg;
95+
EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
96+
9497
u32 hv_max_vp_index;
9598

9699
static int hv_cpu_init(unsigned int cpu)
97100
{
98101
u64 msr_vp_index;
99102
struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
103+
void **input_arg;
104+
105+
input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
106+
*input_arg = page_address(alloc_page(GFP_KERNEL));
100107

101108
hv_get_vp_index(msr_vp_index);
102109

@@ -217,6 +224,16 @@ static int hv_cpu_die(unsigned int cpu)
217224
{
218225
struct hv_reenlightenment_control re_ctrl;
219226
unsigned int new_cpu;
227+
unsigned long flags;
228+
void **input_arg;
229+
void *input_pg = NULL;
230+
231+
local_irq_save(flags);
232+
input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
233+
input_pg = *input_arg;
234+
*input_arg = NULL;
235+
local_irq_restore(flags);
236+
free_page((unsigned long)input_pg);
220237

221238
if (hv_vp_assist_page && hv_vp_assist_page[cpu])
222239
wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
@@ -260,6 +277,16 @@ void __init hyperv_init(void)
260277
if ((ms_hyperv.features & required_msrs) != required_msrs)
261278
return;
262279

280+
/*
281+
* Allocate the per-CPU state for the hypercall input arg.
282+
* If this allocation fails, we will not be able to setup
283+
* (per-CPU) hypercall input page and thus this failure is
284+
* fatal on Hyper-V.
285+
*/
286+
hyperv_pcpu_input_arg = alloc_percpu(void *);
287+
288+
BUG_ON(hyperv_pcpu_input_arg == NULL);
289+
263290
/* Allocate percpu VP index */
264291
hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
265292
GFP_KERNEL);

arch/x86/include/asm/hyperv-tlfs.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@
164164
*/
165165
#define HV_X64_DEPRECATING_AEOI_RECOMMENDED (1 << 9)
166166

167+
/*
168+
* Recommend using cluster IPI hypercalls.
169+
*/
170+
#define HV_X64_CLUSTER_IPI_RECOMMENDED (1 << 10)
171+
167172
/* Recommend using the newer ExProcessorMasks interface */
168173
#define HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED (1 << 11)
169174

@@ -329,10 +334,14 @@ struct hv_tsc_emulation_status {
329334
#define HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK \
330335
(~((1ull << HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT) - 1))
331336

337+
#define HV_IPI_LOW_VECTOR 0x10
338+
#define HV_IPI_HIGH_VECTOR 0xff
339+
332340
/* Declare the various hypercall operations. */
333341
#define HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE 0x0002
334342
#define HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST 0x0003
335343
#define HVCALL_NOTIFY_LONG_SPIN_WAIT 0x0008
344+
#define HVCALL_SEND_IPI 0x000b
336345
#define HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX 0x0013
337346
#define HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX 0x0014
338347
#define HVCALL_POST_MESSAGE 0x005c
@@ -706,4 +715,10 @@ struct hv_enlightened_vmcs {
706715
#define HV_STIMER_AUTOENABLE (1ULL << 3)
707716
#define HV_STIMER_SINT(config) (__u8)(((config) >> 16) & 0x0F)
708717

718+
struct ipi_arg_non_ex {
719+
u32 vector;
720+
u32 reserved;
721+
u64 cpu_mask;
722+
};
723+
709724
#endif

arch/x86/include/asm/mshyperv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ static inline void hv_disable_stimer0_percpu_irq(int irq) {}
122122
#if IS_ENABLED(CONFIG_HYPERV)
123123
extern struct clocksource *hyperv_cs;
124124
extern void *hv_hypercall_pg;
125+
extern void __percpu **hyperv_pcpu_input_arg;
125126

126127
static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
127128
{

0 commit comments

Comments
 (0)