]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
Drivers: hv: vmbus: Suspend after cleaning up hv_sock and sub channels
authorDexuan Cui <decui@microsoft.com>
Thu, 5 Sep 2019 23:01:21 +0000 (23:01 +0000)
committerSasha Levin <sashal@kernel.org>
Fri, 6 Sep 2019 18:52:44 +0000 (14:52 -0400)
Before suspend, Linux must make sure all the hv_sock channels have been
properly cleaned up, because a hv_sock connection can not persist across
hibernation, and the user-space app must be properly notified of the
state change of the connection.

Before suspend, Linux also must make sure all the sub-channels have been
destroyed, i.e. the related channel structs of the sub-channels must be
properly removed, otherwise they would cause a conflict when the
sub-channels are recreated upon resume.

Add a counter to track such channels, and vmbus_bus_suspend() should wait
for the counter to drop to zero.

Signed-off-by: Dexuan Cui <decui@microsoft.com>
Reviewed-by: Michael Kelley <mikelley@microsoft.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/hv/channel_mgmt.c
drivers/hv/connection.c
drivers/hv/hyperv_vmbus.h
drivers/hv/vmbus_drv.c

index 44b92fa1b7fa70bc52a08cca3fafb769d8363f58..5518d031f62a818e195be7b44c58158e5569fe66 100644 (file)
@@ -545,6 +545,10 @@ static void vmbus_process_offer(struct vmbus_channel *newchannel)
 
        mutex_lock(&vmbus_connection.channel_mutex);
 
+       /* Remember the channels that should be cleaned up upon suspend. */
+       if (is_hvsock_channel(newchannel) || is_sub_channel(newchannel))
+               atomic_inc(&vmbus_connection.nr_chan_close_on_suspend);
+
        /*
         * Now that we have acquired the channel_mutex,
         * we can release the potentially racing rescind thread.
@@ -944,6 +948,16 @@ static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
        vmbus_process_offer(newchannel);
 }
 
+static void check_ready_for_suspend_event(void)
+{
+       /*
+        * If all the sub-channels or hv_sock channels have been cleaned up,
+        * then it's safe to suspend.
+        */
+       if (atomic_dec_and_test(&vmbus_connection.nr_chan_close_on_suspend))
+               complete(&vmbus_connection.ready_for_suspend_event);
+}
+
 /*
  * vmbus_onoffer_rescind - Rescind offer handler.
  *
@@ -954,6 +968,7 @@ static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
        struct vmbus_channel_rescind_offer *rescind;
        struct vmbus_channel *channel;
        struct device *dev;
+       bool clean_up_chan_for_suspend;
 
        rescind = (struct vmbus_channel_rescind_offer *)hdr;
 
@@ -993,6 +1008,8 @@ static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
                return;
        }
 
+       clean_up_chan_for_suspend = is_hvsock_channel(channel) ||
+                                   is_sub_channel(channel);
        /*
         * Before setting channel->rescind in vmbus_rescind_cleanup(), we
         * should make sure the channel callback is not running any more.
@@ -1018,6 +1035,10 @@ static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
        if (channel->device_obj) {
                if (channel->chn_rescind_callback) {
                        channel->chn_rescind_callback(channel);
+
+                       if (clean_up_chan_for_suspend)
+                               check_ready_for_suspend_event();
+
                        return;
                }
                /*
@@ -1050,6 +1071,11 @@ static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
                }
                mutex_unlock(&vmbus_connection.channel_mutex);
        }
+
+       /* The "channel" may have been freed. Do not access it any longer. */
+
+       if (clean_up_chan_for_suspend)
+               check_ready_for_suspend_event();
 }
 
 void vmbus_hvsock_device_unregister(struct vmbus_channel *channel)
index 806319cd5ccffe91aafb4e6314f8b41c68720a1a..99851ea682eb26515a826d469a8c64893bd9e1b3 100644 (file)
@@ -26,6 +26,9 @@
 struct vmbus_connection vmbus_connection = {
        .conn_state             = DISCONNECTED,
        .next_gpadl_handle      = ATOMIC_INIT(0xE1E10),
+
+       .ready_for_suspend_event= COMPLETION_INITIALIZER(
+                                 vmbus_connection.ready_for_suspend_event),
 };
 EXPORT_SYMBOL_GPL(vmbus_connection);
 
index e657197a027a2843d47c77093be6558b9611dd01..974b747ca1fc03c1d3bf989fa5acc7c7e5c46418 100644 (file)
@@ -260,6 +260,18 @@ struct vmbus_connection {
        struct workqueue_struct *work_queue;
        struct workqueue_struct *handle_primary_chan_wq;
        struct workqueue_struct *handle_sub_chan_wq;
+
+       /*
+        * The number of sub-channels and hv_sock channels that should be
+        * cleaned up upon suspend: sub-channels will be re-created upon
+        * resume, and hv_sock channels should not survive suspend.
+        */
+       atomic_t nr_chan_close_on_suspend;
+       /*
+        * vmbus_bus_suspend() waits for "nr_chan_close_on_suspend" to
+        * drop to zero.
+        */
+       struct completion ready_for_suspend_event;
 };
 
 
index 45b976ec6e79977a6d85b75afad438ffbf084004..32ec951d334f61bfd193451eb56a35ab152e79ce 100644 (file)
@@ -2127,7 +2127,8 @@ acpi_walk_err:
 
 static int vmbus_bus_suspend(struct device *dev)
 {
-       struct vmbus_channel *channel;
+       struct vmbus_channel *channel, *sc;
+       unsigned long flags;
 
        while (atomic_read(&vmbus_connection.offer_in_progress) != 0) {
                /*
@@ -2146,6 +2147,44 @@ static int vmbus_bus_suspend(struct device *dev)
        }
        mutex_unlock(&vmbus_connection.channel_mutex);
 
+       /*
+        * Wait until all the sub-channels and hv_sock channels have been
+        * cleaned up. Sub-channels should be destroyed upon suspend, otherwise
+        * they would conflict with the new sub-channels that will be created
+        * in the resume path. hv_sock channels should also be destroyed, but
+        * a hv_sock channel of an established hv_sock connection can not be
+        * really destroyed since it may still be referenced by the userspace
+        * application, so we just force the hv_sock channel to be rescinded
+        * by vmbus_force_channel_rescinded(), and the userspace application
+        * will thoroughly destroy the channel after hibernation.
+        *
+        * Note: the counter nr_chan_close_on_suspend may never go above 0 if
+        * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
+        */
+       if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
+               wait_for_completion(&vmbus_connection.ready_for_suspend_event);
+
+       mutex_lock(&vmbus_connection.channel_mutex);
+
+       list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
+               if (is_hvsock_channel(channel)) {
+                       if (!channel->rescind) {
+                               pr_err("hv_sock channel not rescinded!\n");
+                               WARN_ON_ONCE(1);
+                       }
+                       continue;
+               }
+
+               spin_lock_irqsave(&channel->lock, flags);
+               list_for_each_entry(sc, &channel->sc_list, sc_list) {
+                       pr_err("Sub-channel not deleted!\n");
+                       WARN_ON_ONCE(1);
+               }
+               spin_unlock_irqrestore(&channel->lock, flags);
+       }
+
+       mutex_unlock(&vmbus_connection.channel_mutex);
+
        vmbus_initiate_unload(false);
 
        vmbus_connection.conn_state = DISCONNECTED;
@@ -2186,6 +2225,9 @@ static int vmbus_bus_resume(struct device *dev)
 
        vmbus_request_offers();
 
+       /* Reset the event for the next suspend. */
+       reinit_completion(&vmbus_connection.ready_for_suspend_event);
+
        return 0;
 }