Currently, it is tedious to offline CPUs. Since, most CPUs will have
vmbus channels attached to them that a user would have to manually
rebind elsewhere. So, as made mention of in
commit d570aec0f2154 ("Drivers: hv: vmbus: Synchronize init_vp_index()
vs. CPU hotplug"), rebind channels associated with CPUs that a user is
trying to offline to a new "randomly" selected CPU.

Cc: Boqun Feng <boqun.f...@gmail.com>
Cc: Michael Kelley <mhkli...@outlook.com>
Cc: Wei Liu <wei....@kernel.org>
Signed-off-by: Hamza Mahfooz <hamzamahf...@linux.microsoft.com>
---
v2: remove cpus_read_{un,}lock() from hv_pick_new_cpu() and add
    lockdep_assert_cpus_held().

v3: use for_each_cpu_wrap() in hv_pick_new_cpu().

v4: store get_random_u32_below() in start.
---
 drivers/hv/hv.c | 65 +++++++++++++++++++++++++++++++++++++------------
 1 file changed, 50 insertions(+), 15 deletions(-)

diff --git a/drivers/hv/hv.c b/drivers/hv/hv.c
index 36d9ba097ff5..4388c0030a59 100644
--- a/drivers/hv/hv.c
+++ b/drivers/hv/hv.c
@@ -433,13 +433,48 @@ static bool hv_synic_event_pending(void)
        return pending;
 }
 
+static int hv_pick_new_cpu(struct vmbus_channel *channel,
+                          unsigned int current_cpu)
+{
+       int ret = 0;
+       int start;
+       int cpu;
+
+       lockdep_assert_cpus_held();
+       lockdep_assert_held(&vmbus_connection.channel_mutex);
+
+       /*
+        * We can't assume that the relevant interrupts will be sent before
+        * the cpu is offlined on older versions of hyperv.
+        */
+       if (vmbus_proto_version < VERSION_WIN10_V5_3)
+               return -EBUSY;
+
+       start = get_random_u32_below(nr_cpu_ids);
+
+       for_each_cpu_wrap(cpu, cpu_online_mask, start) {
+               if (cpu == current_cpu || cpu == VMBUS_CONNECT_CPU)
+                       continue;
+
+               ret = vmbus_channel_set_cpu(channel, cpu);
+
+               if (!ret)
+                       break;
+       }
+
+       if (ret)
+               ret = vmbus_channel_set_cpu(channel, VMBUS_CONNECT_CPU);
+
+       return ret;
+}
+
 /*
  * hv_synic_cleanup - Cleanup routine for hv_synic_init().
  */
 int hv_synic_cleanup(unsigned int cpu)
 {
        struct vmbus_channel *channel, *sc;
-       bool channel_found = false;
+       int ret = 0;
 
        if (vmbus_connection.conn_state != CONNECTED)
                goto always_cleanup;
@@ -456,31 +491,31 @@ int hv_synic_cleanup(unsigned int cpu)
 
        /*
         * Search for channels which are bound to the CPU we're about to
-        * cleanup.  In case we find one and vmbus is still connected, we
-        * fail; this will effectively prevent CPU offlining.
-        *
-        * TODO: Re-bind the channels to different CPUs.
+        * cleanup.
         */
        mutex_lock(&vmbus_connection.channel_mutex);
        list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
                if (channel->target_cpu == cpu) {
-                       channel_found = true;
-                       break;
+                       ret = hv_pick_new_cpu(channel, cpu);
+
+                       if (ret) {
+                               mutex_unlock(&vmbus_connection.channel_mutex);
+                               return ret;
+                       }
                }
                list_for_each_entry(sc, &channel->sc_list, sc_list) {
                        if (sc->target_cpu == cpu) {
-                               channel_found = true;
-                               break;
+                               ret = hv_pick_new_cpu(channel, cpu);
+
+                               if (ret) {
+                                       
mutex_unlock(&vmbus_connection.channel_mutex);
+                                       return ret;
+                               }
                        }
                }
-               if (channel_found)
-                       break;
        }
        mutex_unlock(&vmbus_connection.channel_mutex);
 
-       if (channel_found)
-               return -EBUSY;
-
        /*
         * channel_found == false means that any channels that were previously
         * assigned to the CPU have been reassigned elsewhere with a call of
@@ -497,5 +532,5 @@ int hv_synic_cleanup(unsigned int cpu)
 
        hv_synic_disable_regs(cpu);
 
-       return 0;
+       return ret;
 }
-- 
2.47.1


Reply via email to