Skip to content

Commit b307b38

Browse files
dcuiSasha Levin
authored andcommitted
Drivers: hv: vmbus: Suspend after cleaning up hv_sock and sub channels
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>
1 parent 1f48dcf commit b307b38

4 files changed

Lines changed: 84 additions & 1 deletion

File tree

‎drivers/hv/channel_mgmt.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,10 @@ static void vmbus_process_offer(struct vmbus_channel *newchannel)
545545

546546
mutex_lock(&vmbus_connection.channel_mutex);
547547

548+
/* Remember the channels that should be cleaned up upon suspend. */
549+
if (is_hvsock_channel(newchannel) || is_sub_channel(newchannel))
550+
atomic_inc(&vmbus_connection.nr_chan_close_on_suspend);
551+
548552
/*
549553
* Now that we have acquired the channel_mutex,
550554
* we can release the potentially racing rescind thread.
@@ -944,6 +948,16 @@ static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
944948
vmbus_process_offer(newchannel);
945949
}
946950

951+
static void check_ready_for_suspend_event(void)
952+
{
953+
/*
954+
* If all the sub-channels or hv_sock channels have been cleaned up,
955+
* then it's safe to suspend.
956+
*/
957+
if (atomic_dec_and_test(&vmbus_connection.nr_chan_close_on_suspend))
958+
complete(&vmbus_connection.ready_for_suspend_event);
959+
}
960+
947961
/*
948962
* vmbus_onoffer_rescind - Rescind offer handler.
949963
*
@@ -954,6 +968,7 @@ static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
954968
struct vmbus_channel_rescind_offer *rescind;
955969
struct vmbus_channel *channel;
956970
struct device *dev;
971+
bool clean_up_chan_for_suspend;
957972

958973
rescind = (struct vmbus_channel_rescind_offer *)hdr;
959974

@@ -993,6 +1008,8 @@ static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
9931008
return;
9941009
}
9951010

1011+
clean_up_chan_for_suspend = is_hvsock_channel(channel) ||
1012+
is_sub_channel(channel);
9961013
/*
9971014
* Before setting channel->rescind in vmbus_rescind_cleanup(), we
9981015
* 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)
10181035
if (channel->device_obj) {
10191036
if (channel->chn_rescind_callback) {
10201037
channel->chn_rescind_callback(channel);
1038+
1039+
if (clean_up_chan_for_suspend)
1040+
check_ready_for_suspend_event();
1041+
10211042
return;
10221043
}
10231044
/*
@@ -1050,6 +1071,11 @@ static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
10501071
}
10511072
mutex_unlock(&vmbus_connection.channel_mutex);
10521073
}
1074+
1075+
/* The "channel" may have been freed. Do not access it any longer. */
1076+
1077+
if (clean_up_chan_for_suspend)
1078+
check_ready_for_suspend_event();
10531079
}
10541080

10551081
void vmbus_hvsock_device_unregister(struct vmbus_channel *channel)

‎drivers/hv/connection.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
struct vmbus_connection vmbus_connection = {
2727
.conn_state = DISCONNECTED,
2828
.next_gpadl_handle = ATOMIC_INIT(0xE1E10),
29+
30+
.ready_for_suspend_event= COMPLETION_INITIALIZER(
31+
vmbus_connection.ready_for_suspend_event),
2932
};
3033
EXPORT_SYMBOL_GPL(vmbus_connection);
3134

‎drivers/hv/hyperv_vmbus.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,18 @@ struct vmbus_connection {
260260
struct workqueue_struct *work_queue;
261261
struct workqueue_struct *handle_primary_chan_wq;
262262
struct workqueue_struct *handle_sub_chan_wq;
263+
264+
/*
265+
* The number of sub-channels and hv_sock channels that should be
266+
* cleaned up upon suspend: sub-channels will be re-created upon
267+
* resume, and hv_sock channels should not survive suspend.
268+
*/
269+
atomic_t nr_chan_close_on_suspend;
270+
/*
271+
* vmbus_bus_suspend() waits for "nr_chan_close_on_suspend" to
272+
* drop to zero.
273+
*/
274+
struct completion ready_for_suspend_event;
263275
};
264276

265277

‎drivers/hv/vmbus_drv.c‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2127,7 +2127,8 @@ static int vmbus_acpi_add(struct acpi_device *device)
21272127

21282128
static int vmbus_bus_suspend(struct device *dev)
21292129
{
2130-
struct vmbus_channel *channel;
2130+
struct vmbus_channel *channel, *sc;
2131+
unsigned long flags;
21312132

21322133
while (atomic_read(&vmbus_connection.offer_in_progress) != 0) {
21332134
/*
@@ -2146,6 +2147,44 @@ static int vmbus_bus_suspend(struct device *dev)
21462147
}
21472148
mutex_unlock(&vmbus_connection.channel_mutex);
21482149

2150+
/*
2151+
* Wait until all the sub-channels and hv_sock channels have been
2152+
* cleaned up. Sub-channels should be destroyed upon suspend, otherwise
2153+
* they would conflict with the new sub-channels that will be created
2154+
* in the resume path. hv_sock channels should also be destroyed, but
2155+
* a hv_sock channel of an established hv_sock connection can not be
2156+
* really destroyed since it may still be referenced by the userspace
2157+
* application, so we just force the hv_sock channel to be rescinded
2158+
* by vmbus_force_channel_rescinded(), and the userspace application
2159+
* will thoroughly destroy the channel after hibernation.
2160+
*
2161+
* Note: the counter nr_chan_close_on_suspend may never go above 0 if
2162+
* the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
2163+
*/
2164+
if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
2165+
wait_for_completion(&vmbus_connection.ready_for_suspend_event);
2166+
2167+
mutex_lock(&vmbus_connection.channel_mutex);
2168+
2169+
list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2170+
if (is_hvsock_channel(channel)) {
2171+
if (!channel->rescind) {
2172+
pr_err("hv_sock channel not rescinded!\n");
2173+
WARN_ON_ONCE(1);
2174+
}
2175+
continue;
2176+
}
2177+
2178+
spin_lock_irqsave(&channel->lock, flags);
2179+
list_for_each_entry(sc, &channel->sc_list, sc_list) {
2180+
pr_err("Sub-channel not deleted!\n");
2181+
WARN_ON_ONCE(1);
2182+
}
2183+
spin_unlock_irqrestore(&channel->lock, flags);
2184+
}
2185+
2186+
mutex_unlock(&vmbus_connection.channel_mutex);
2187+
21492188
vmbus_initiate_unload(false);
21502189

21512190
vmbus_connection.conn_state = DISCONNECTED;
@@ -2186,6 +2225,9 @@ static int vmbus_bus_resume(struct device *dev)
21862225

21872226
vmbus_request_offers();
21882227

2228+
/* Reset the event for the next suspend. */
2229+
reinit_completion(&vmbus_connection.ready_for_suspend_event);
2230+
21892231
return 0;
21902232
}
21912233

0 commit comments

Comments
 (0)