Commit 8b5d6d3b authored by Haiyang Zhang's avatar Haiyang Zhang Committed by Greg Kroah-Hartman

staging: hv: Fix race condition on vmbus channel initialization

There is a possible race condition when hv_utils starts to load immediately
after hv_vmbus is loading - null pointer error could happen.
This patch added wait/completion to ensure all channels are ready before
vmbus loading completes. So another module won't have any uninitialized channel.
Signed-off-by: default avatarHaiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: default avatarHank Janssen <hjanssen@microsoft.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 6c2fd308
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/list.h> #include <linux/list.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/completion.h>
#include "osd.h" #include "osd.h"
#include "logging.h" #include "logging.h"
#include "vmbus_private.h" #include "vmbus_private.h"
...@@ -293,6 +294,25 @@ void FreeVmbusChannel(struct vmbus_channel *Channel) ...@@ -293,6 +294,25 @@ void FreeVmbusChannel(struct vmbus_channel *Channel)
Channel); Channel);
} }
DECLARE_COMPLETION(hv_channel_ready);
/*
* Count initialized channels, and ensure all channels are ready when hv_vmbus
* module loading completes.
*/
static void count_hv_channel(void)
{
static int counter;
unsigned long flags;
spin_lock_irqsave(&gVmbusConnection.channel_lock, flags);
if (++counter == MAX_MSG_TYPES)
complete(&hv_channel_ready);
spin_unlock_irqrestore(&gVmbusConnection.channel_lock, flags);
}
/* /*
* VmbusChannelProcessOffer - Process the offer by creating a channel/device * VmbusChannelProcessOffer - Process the offer by creating a channel/device
* associated with this offer * associated with this offer
...@@ -373,22 +393,21 @@ static void VmbusChannelProcessOffer(void *context) ...@@ -373,22 +393,21 @@ static void VmbusChannelProcessOffer(void *context)
* can cleanup properly * can cleanup properly
*/ */
newChannel->State = CHANNEL_OPEN_STATE; newChannel->State = CHANNEL_OPEN_STATE;
cnt = 0;
while (cnt != MAX_MSG_TYPES) { /* Open IC channels */
for (cnt = 0; cnt < MAX_MSG_TYPES; cnt++) {
if (memcmp(&newChannel->OfferMsg.Offer.InterfaceType, if (memcmp(&newChannel->OfferMsg.Offer.InterfaceType,
&hv_cb_utils[cnt].data, &hv_cb_utils[cnt].data,
sizeof(struct hv_guid)) == 0) { sizeof(struct hv_guid)) == 0 &&
VmbusChannelOpen(newChannel, 2 * PAGE_SIZE,
2 * PAGE_SIZE, NULL, 0,
hv_cb_utils[cnt].callback,
newChannel) == 0) {
hv_cb_utils[cnt].channel = newChannel;
DPRINT_INFO(VMBUS, "%s", DPRINT_INFO(VMBUS, "%s",
hv_cb_utils[cnt].log_msg); hv_cb_utils[cnt].log_msg);
count_hv_channel();
if (VmbusChannelOpen(newChannel, 2 * PAGE_SIZE,
2 * PAGE_SIZE, NULL, 0,
hv_cb_utils[cnt].callback,
newChannel) == 0)
hv_cb_utils[cnt].channel = newChannel;
} }
cnt++;
} }
} }
DPRINT_EXIT(VMBUS); DPRINT_EXIT(VMBUS);
......
...@@ -74,4 +74,6 @@ int vmbus_child_driver_register(struct driver_context *driver_ctx); ...@@ -74,4 +74,6 @@ int vmbus_child_driver_register(struct driver_context *driver_ctx);
void vmbus_child_driver_unregister(struct driver_context *driver_ctx); void vmbus_child_driver_unregister(struct driver_context *driver_ctx);
void vmbus_get_interface(struct vmbus_channel_interface *interface); void vmbus_get_interface(struct vmbus_channel_interface *interface);
extern struct completion hv_channel_ready;
#endif /* _VMBUS_H_ */ #endif /* _VMBUS_H_ */
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <linux/pci.h> #include <linux/pci.h>
#include <linux/dmi.h> #include <linux/dmi.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/completion.h>
#include "version_info.h" #include "version_info.h"
#include "osd.h" #include "osd.h"
#include "logging.h" #include "logging.h"
...@@ -356,6 +357,8 @@ static int vmbus_bus_init(int (*drv_init)(struct hv_driver *drv)) ...@@ -356,6 +357,8 @@ static int vmbus_bus_init(int (*drv_init)(struct hv_driver *drv))
vmbus_drv_obj->GetChannelOffers(); vmbus_drv_obj->GetChannelOffers();
wait_for_completion(&hv_channel_ready);
cleanup: cleanup:
DPRINT_EXIT(VMBUS_DRV); DPRINT_EXIT(VMBUS_DRV);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment