Skip to content

Commit dfb5c1e

Browse files
committed
x86/hyperv: remove on-stack cpumask from hv_send_ipi_mask_allbutself
It is not a good practice to allocate a cpumask on stack, given it may consume up to 1 kilobytes of stack space if the kernel is configured to have 8192 cpus. The internal helper functions __send_ipi_mask{,_ex} need to loop over the provided mask anyway, so it is not too difficult to skip `self' there. We can thus do away with the on-stack cpumask in hv_send_ipi_mask_allbutself. Adjust call sites of __send_ipi_mask as needed. Reported-by: Linus Torvalds <[email protected]> Suggested-by: Michael Kelley <[email protected]> Suggested-by: Linus Torvalds <[email protected]> Fixes: 68bb7bf ("X86/Hyper-V: Enable IPI enlightenments") Signed-off-by: Wei Liu <[email protected]> Reviewed-by: Michael Kelley <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 7ad9bb9 commit dfb5c1e

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

arch/x86/hyperv/hv_apic.c

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ static void hv_apic_eoi_write(u32 reg, u32 val)
9999
/*
100100
* IPI implementation on Hyper-V.
101101
*/
102-
static bool __send_ipi_mask_ex(const struct cpumask *mask, int vector)
102+
static bool __send_ipi_mask_ex(const struct cpumask *mask, int vector,
103+
bool exclude_self)
103104
{
104105
struct hv_send_ipi_ex **arg;
105106
struct hv_send_ipi_ex *ipi_arg;
@@ -123,7 +124,10 @@ static bool __send_ipi_mask_ex(const struct cpumask *mask, int vector)
123124

124125
if (!cpumask_equal(mask, cpu_present_mask)) {
125126
ipi_arg->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
126-
nr_bank = cpumask_to_vpset(&(ipi_arg->vp_set), mask);
127+
if (exclude_self)
128+
nr_bank = cpumask_to_vpset_noself(&(ipi_arg->vp_set), mask);
129+
else
130+
nr_bank = cpumask_to_vpset(&(ipi_arg->vp_set), mask);
127131
}
128132
if (nr_bank < 0)
129133
goto ipi_mask_ex_done;
@@ -138,15 +142,25 @@ static bool __send_ipi_mask_ex(const struct cpumask *mask, int vector)
138142
return hv_result_success(status);
139143
}
140144

141-
static bool __send_ipi_mask(const struct cpumask *mask, int vector)
145+
static bool __send_ipi_mask(const struct cpumask *mask, int vector,
146+
bool exclude_self)
142147
{
143-
int cur_cpu, vcpu;
148+
int cur_cpu, vcpu, this_cpu = smp_processor_id();
144149
struct hv_send_ipi ipi_arg;
145150
u64 status;
151+
unsigned int weight;
146152

147153
trace_hyperv_send_ipi_mask(mask, vector);
148154

149-
if (cpumask_empty(mask))
155+
weight = cpumask_weight(mask);
156+
157+
/*
158+
* Do nothing if
159+
* 1. the mask is empty
160+
* 2. the mask only contains self when exclude_self is true
161+
*/
162+
if (weight == 0 ||
163+
(exclude_self && weight == 1 && cpumask_test_cpu(this_cpu, mask)))
150164
return true;
151165

152166
if (!hv_hypercall_pg)
@@ -172,6 +186,8 @@ static bool __send_ipi_mask(const struct cpumask *mask, int vector)
172186
ipi_arg.cpu_mask = 0;
173187

174188
for_each_cpu(cur_cpu, mask) {
189+
if (exclude_self && cur_cpu == this_cpu)
190+
continue;
175191
vcpu = hv_cpu_number_to_vp_number(cur_cpu);
176192
if (vcpu == VP_INVAL)
177193
return false;
@@ -191,7 +207,7 @@ static bool __send_ipi_mask(const struct cpumask *mask, int vector)
191207
return hv_result_success(status);
192208

193209
do_ex_hypercall:
194-
return __send_ipi_mask_ex(mask, vector);
210+
return __send_ipi_mask_ex(mask, vector, exclude_self);
195211
}
196212

197213
static bool __send_ipi_one(int cpu, int vector)
@@ -208,7 +224,7 @@ static bool __send_ipi_one(int cpu, int vector)
208224
return false;
209225

210226
if (vp >= 64)
211-
return __send_ipi_mask_ex(cpumask_of(cpu), vector);
227+
return __send_ipi_mask_ex(cpumask_of(cpu), vector, false);
212228

213229
status = hv_do_fast_hypercall16(HVCALL_SEND_IPI, vector, BIT_ULL(vp));
214230
return hv_result_success(status);
@@ -222,20 +238,13 @@ static void hv_send_ipi(int cpu, int vector)
222238

223239
static void hv_send_ipi_mask(const struct cpumask *mask, int vector)
224240
{
225-
if (!__send_ipi_mask(mask, vector))
241+
if (!__send_ipi_mask(mask, vector, false))
226242
orig_apic.send_IPI_mask(mask, vector);
227243
}
228244

229245
static void hv_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
230246
{
231-
unsigned int this_cpu = smp_processor_id();
232-
struct cpumask new_mask;
233-
const struct cpumask *local_mask;
234-
235-
cpumask_copy(&new_mask, mask);
236-
cpumask_clear_cpu(this_cpu, &new_mask);
237-
local_mask = &new_mask;
238-
if (!__send_ipi_mask(local_mask, vector))
247+
if (!__send_ipi_mask(mask, vector, true))
239248
orig_apic.send_IPI_mask_allbutself(mask, vector);
240249
}
241250

@@ -246,7 +255,7 @@ static void hv_send_ipi_allbutself(int vector)
246255

247256
static void hv_send_ipi_all(int vector)
248257
{
249-
if (!__send_ipi_mask(cpu_online_mask, vector))
258+
if (!__send_ipi_mask(cpu_online_mask, vector, false))
250259
orig_apic.send_IPI_all(vector);
251260
}
252261

0 commit comments

Comments
 (0)