wrmsr(HV_X64_MSR_EOI, val, 0);
 }
 
+static bool cpu_is_self(int cpu)
+{
+       return cpu == smp_processor_id();
+}
+
 /*
  * IPI implementation on Hyper-V.
  */
         */
        if (!cpumask_equal(mask, cpu_present_mask) || exclude_self) {
                ipi_arg->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
-               if (exclude_self)
-                       nr_bank = cpumask_to_vpset_noself(&(ipi_arg->vp_set), mask);
-               else
-                       nr_bank = cpumask_to_vpset(&(ipi_arg->vp_set), mask);
+
+               nr_bank = cpumask_to_vpset_skip(&(ipi_arg->vp_set), mask,
+                               exclude_self ? cpu_is_self : NULL);
 
                /*
                 * 'nr_bank <= 0' means some CPUs in cpumask can't be
 
 
 static inline int __cpumask_to_vpset(struct hv_vpset *vpset,
                                    const struct cpumask *cpus,
-                                   bool exclude_self)
+                                   bool (*func)(int cpu))
 {
        int cpu, vcpu, vcpu_bank, vcpu_offset, nr_bank = 1;
-       int this_cpu = smp_processor_id();
        int max_vcpu_bank = hv_max_vp_index / HV_VCPUS_PER_SPARSE_BANK;
 
        /* vpset.valid_bank_mask can represent up to HV_MAX_SPARSE_VCPU_BANKS banks */
         * Some banks may end up being empty but this is acceptable.
         */
        for_each_cpu(cpu, cpus) {
-               if (exclude_self && cpu == this_cpu)
+               if (func && func(cpu))
                        continue;
                vcpu = hv_cpu_number_to_vp_number(cpu);
                if (vcpu == VP_INVAL)
        return nr_bank;
 }
 
+/*
+ * Convert a Linux cpumask into a Hyper-V VPset. In the _skip variant,
+ * 'func' is called for each CPU present in cpumask.  If 'func' returns
+ * true, that CPU is skipped -- i.e., that CPU from cpumask is *not*
+ * added to the Hyper-V VPset. If 'func' is NULL, no CPUs are
+ * skipped.
+ */
 static inline int cpumask_to_vpset(struct hv_vpset *vpset,
                                    const struct cpumask *cpus)
 {
-       return __cpumask_to_vpset(vpset, cpus, false);
+       return __cpumask_to_vpset(vpset, cpus, NULL);
 }
 
-static inline int cpumask_to_vpset_noself(struct hv_vpset *vpset,
-                                   const struct cpumask *cpus)
+static inline int cpumask_to_vpset_skip(struct hv_vpset *vpset,
+                                   const struct cpumask *cpus,
+                                   bool (*func)(int cpu))
 {
-       WARN_ON_ONCE(preemptible());
-       return __cpumask_to_vpset(vpset, cpus, true);
+       return __cpumask_to_vpset(vpset, cpus, func);
 }
 
 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die);