Skip to contentSkip to conversation

Drivers: hv: vmbus: protect Offer/Rescind offer processing

PATCH In review Vitaly Kuznetsov 4 participants 20 messages Jan 20, 2015 – Feb 1, 2015 Hyper-V/Azure core and drivers

lore
Share & tools
lore
#0 lore raw

v3 cover Drivers: hv: vmbus: protect Offer/Rescind offer processing

This patch series is a renamed successor of "[PATCH] Drivers: hv: vmbus:
serialize Offer and Rescind offer processing".

Changes from v2:
- Rename labels in vmbus_process_offer() ("out" -> "done_init_rescind" in PATCH 3/3, "error" -> "err_free_chan" in PATCH 1/3) [Dan Carpenter]
- Invert condition, update comment, and remove "out" label in vmbus_onoffer_rescind()

Changes from v1:
- Separate vmbus_device_create() return value check [K. Y. Srinivasan]
- Do not lose a rescind offer received during offer processing. Use renamed (in [PATCH v2 2/3]) spinlock to protect simulteneous test-and-set workflow for rescind and work fields. [K. Y. Srinivasan]

Vitaly Kuznetsov (3):
Drivers: hv: check vmbus_device_create() return value in
vmbus_process_offer()
Drivers: hv: rename sc_lock to the more generic lock
Drivers: hv: vmbus: serialize Offer and Rescind offer

 drivers/hv/channel.c      |  6 +++---
 drivers/hv/channel_mgmt.c | 50 ++++++++++++++++++++++++++++++++---------------
 include/linux/hyperv.h    |  7 ++++++-
 3 files changed, 43 insertions(+), 20 deletions(-)
Signature · 1 line
1.9.3
#1 lore raw

PATCH v3 1/3 Drivers: hv: check vmbus_device_create() return value in vmbus_process_offer()

vmbus_device_create() result is not being checked in vmbus_process_offer() and it can fail if kzalloc() fails. Add the check and do minor cleanup to avoid additional duplication of "free_channel(); return;" block.

---
 drivers/hv/channel_mgmt.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/hv/channel_mgmt.c +9 -5
index 2c59f03..01f2c2b 100644
@@ -341,11 +341,10 @@ static void vmbus_process_offer(struct work_struct *work)
 			if (channel->sc_creation_callback != NULL)
 				channel->sc_creation_callback(newchannel);
 
-			return;
+			goto out;
 		}
 
-		free_channel(newchannel);
-		return;
+		goto err_free_chan;
 	}
 
 	/*
@@ -364,6 +363,8 @@ static void vmbus_process_offer(struct work_struct *work)
 		&newchannel->offermsg.offer.if_type,
 		&newchannel->offermsg.offer.if_instance,
 		newchannel);
+	if (!newchannel->device_obj)
+		goto err_free_chan;
 
 	/*
 	 * Add the new device to the bus. This will kick off device-driver
@@ -379,9 +380,12 @@ static void vmbus_process_offer(struct work_struct *work)
 		list_del(&newchannel->listentry);
 		spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
 		kfree(newchannel->device_obj);
-
-		free_channel(newchannel);
+		goto err_free_chan;
 	}
+out:
+	return;
+err_free_chan:
+	free_channel(newchannel);
 }
 
 enum {
Signature · 1 line
1.9.3
#2 lore raw

RE: [PATCH v3 1/3] Drivers: hv: check vmbus_device_create() return value in vmbus_process_offer()

Quoted 11 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Tuesday, January 20, 2015 7:45 AM
To: KY Srinivasan; [email protected]
Cc: Haiyang Zhang; [email protected]; Dexuan Cui; Jason Wang; Radim Krčmář; Dan Carpenter
Subject: [PATCH v3 1/3] Drivers: hv: check vmbus_device_create() return value in vmbus_process_offer()

Quoted 7 lines

vmbus_device_create() result is not being checked in vmbus_process_offer() and it can fail if kzalloc() fails. Add the check and do minor cleanup to avoid additional duplication of "free_channel(); return;" block.

Thanks Vitaly.

Quoted 52 lines
---
 drivers/hv/channel_mgmt.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/hv/channel_mgmt.c index renamed from drivers/hv/channel_mgmt.c +0 -0

2c59f03..01f2c2b 100644

drivers/hv/channel_mgmt.c +2 -3
@@ -341,11 +341,10 @@ static void vmbus_process_offer(struct
 work_struct *work)
 			if (channel->sc_creation_callback != NULL)
 				channel->sc_creation_callback(newchannel);
 
-			return;
+			goto out;
 		}
 
-		free_channel(newchannel);
-		return;
+		goto err_free_chan;
 	}
 
drivers/hv/channel_mgmt.c +2 -0
 	/*
@@ -364,6 +363,8 @@ static void vmbus_process_offer(struct work_struct
 *work)
 		&newchannel->offermsg.offer.if_type,
 		&newchannel->offermsg.offer.if_instance,
 		newchannel);
+	if (!newchannel->device_obj)
+		goto err_free_chan;
 
 	/*

* Add the new device to the bus. This will kick off device-driver @@
-379,9 +380,12 @@ static void vmbus_process_offer(struct work_struct *work)

drivers/hv/channel_mgmt.c +0 -0
 		list_del(&newchannel->listentry);
 		spin_unlock_irqrestore(&vmbus_connection.channel_lock,

flags);

drivers/hv/channel_mgmt.c +5 -3
 		kfree(newchannel->device_obj);
-
-		free_channel(newchannel);
+		goto err_free_chan;
 	}
+out:
+	return;
+err_free_chan:
+	free_channel(newchannel);
 }
 
 enum {
--

1.9.3

Acked-by on v3 1/3
#3 lore raw

Re: [PATCH v3 1/3] Drivers: hv: check vmbus_device_create() return value in vmbus_process_offer()

In reply to Vitaly Kuznetsov
Quoted 9 lines · Vitaly Kuznetsov

vmbus_device_create() result is not being checked in vmbus_process_offer() and it can fail if kzalloc() fails. Add the check and do minor cleanup to avoid additional duplication of "free_channel(); return;" block.

---
 drivers/hv/channel_mgmt.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
Quoted 49 lines
drivers/hv/channel_mgmt.c +2 -3
index 2c59f03..01f2c2b 100644
@@ -341,11 +341,10 @@ static void vmbus_process_offer(struct 
 work_struct *work)
 			if (channel->sc_creation_callback != NULL)
 				channel->sc_creation_callback(newchannel);
 
-			return;
+			goto out;
 		}
 
-		free_channel(newchannel);
-		return;
+		goto err_free_chan;
 	}
 
drivers/hv/channel_mgmt.c +2 -0
 	/*
@@ -364,6 +363,8 @@ static void vmbus_process_offer(struct 
 work_struct *work)
 		&newchannel->offermsg.offer.if_type,
 		&newchannel->offermsg.offer.if_instance,
 		newchannel);
+	if (!newchannel->device_obj)
+		goto err_free_chan;
 
 	/*
drivers/hv/channel_mgmt.c +5 -2
 	 * Add the new device to the bus. This will kick off device-driver
@@ -379,9 +380,12 @@ static void vmbus_process_offer(struct 
 work_struct *work)
 		list_del(&newchannel->listentry);
 		spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
 		kfree(newchannel->device_obj);
-
-		free_channel(newchannel);
+		goto err_free_chan;
 	}
+out:
+	return;
+err_free_chan:
+	free_channel(newchannel);
 }
 

enum {

Signature · 1 line
1.9.3
#4 lore raw

PATCH v3 2/3 Drivers: hv: rename sc_lock to the more generic lock

In reply to Vitaly Kuznetsov

sc_lock spinlock in struct vmbus_channel is being used to not only protect the sc_list field, e.g. vmbus_open() function uses it to implement test-and-set access to the state field. Rename it to the more generic 'lock' and add the description.

---
 drivers/hv/channel.c      |  6 +++---
 drivers/hv/channel_mgmt.c | 10 +++++-----
 include/linux/hyperv.h    |  7 ++++++-
 3 files changed, 14 insertions(+), 9 deletions(-)
drivers/hv/channel.c +3 -3
index 433f72a..8608ed1 100644
@@ -73,14 +73,14 @@ int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size,
 	unsigned long flags;
 	int ret, t, err = 0;
 
-	spin_lock_irqsave(&newchannel->sc_lock, flags);
+	spin_lock_irqsave(&newchannel->lock, flags);
 	if (newchannel->state == CHANNEL_OPEN_STATE) {
 		newchannel->state = CHANNEL_OPENING_STATE;
 	} else {
-		spin_unlock_irqrestore(&newchannel->sc_lock, flags);
+		spin_unlock_irqrestore(&newchannel->lock, flags);
 		return -EINVAL;
 	}
-	spin_unlock_irqrestore(&newchannel->sc_lock, flags);
+	spin_unlock_irqrestore(&newchannel->lock, flags);
 
 	newchannel->onchannel_callback = onchannelcallback;
 	newchannel->channel_callback_context = context;
drivers/hv/channel_mgmt.c +5 -5
index 01f2c2b..c6fdd74 100644
@@ -146,7 +146,7 @@ static struct vmbus_channel *alloc_channel(void)
 		return NULL;
 
 	spin_lock_init(&channel->inbound_lock);
-	spin_lock_init(&channel->sc_lock);
+	spin_lock_init(&channel->lock);
 
 	INIT_LIST_HEAD(&channel->sc_list);
 	INIT_LIST_HEAD(&channel->percpu_list);
@@ -246,9 +246,9 @@ static void vmbus_process_rescind_offer(struct work_struct *work)
 		spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
 	} else {
 		primary_channel = channel->primary_channel;
-		spin_lock_irqsave(&primary_channel->sc_lock, flags);
+		spin_lock_irqsave(&primary_channel->lock, flags);
 		list_del(&channel->sc_list);
-		spin_unlock_irqrestore(&primary_channel->sc_lock, flags);
+		spin_unlock_irqrestore(&primary_channel->lock, flags);
 	}
 	free_channel(channel);
 }
@@ -323,9 +323,9 @@ static void vmbus_process_offer(struct work_struct *work)
 			 * Process the sub-channel.
 			 */
 			newchannel->primary_channel = channel;
-			spin_lock_irqsave(&channel->sc_lock, flags);
+			spin_lock_irqsave(&channel->lock, flags);
 			list_add_tail(&newchannel->sc_list, &channel->sc_list);
-			spin_unlock_irqrestore(&channel->sc_lock, flags);
+			spin_unlock_irqrestore(&channel->lock, flags);
 
 			if (newchannel->target_cpu != get_cpu()) {
 				put_cpu();
include/linux/hyperv.h +6 -1
index 476c685..02dd978 100644
@@ -722,7 +722,12 @@ struct vmbus_channel {
 	 */
 	void (*sc_creation_callback)(struct vmbus_channel *new_sc);
 
-	spinlock_t sc_lock;
+	/*
+	 * The spinlock to protect the structure. It is being used to protect
+	 * test-and-set access to various attributes of the structure as well
+	 * as all sc_list operations.
+	 */
+	spinlock_t lock;
 	/*
 	 * All Sub-channels of a primary channel are linked here.
 	 */
Signature · 1 line
1.9.3
#5 lore raw

RE: [PATCH v3 2/3] Drivers: hv: rename sc_lock to the more generic lock

Quoted 10 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Tuesday, January 20, 2015 7:45 AM
To: KY Srinivasan; [email protected]
Cc: Haiyang Zhang; [email protected]; Dexuan Cui; Jason Wang; Radim Krčmář; Dan Carpenter
Subject: [PATCH v3 2/3] Drivers: hv: rename sc_lock to the more generic lock

Quoted 6 lines

sc_lock spinlock in struct vmbus_channel is being used to not only protect the sc_list field, e.g. vmbus_open() function uses it to implement test-and- set access to the state field. Rename it to the more generic 'lock' and add the description.

Thanks Vitaly.

Quoted 90 lines
---
 drivers/hv/channel.c      |  6 +++---
 drivers/hv/channel_mgmt.c | 10 +++++-----
 include/linux/hyperv.h    |  7 ++++++-
 3 files changed, 14 insertions(+), 9 deletions(-)
drivers/hv/channel.c index renamed from drivers/hv/channel.c +0 -0

433f72a..8608ed1 100644

drivers/hv/channel.c +3 -3
@@ -73,14 +73,14 @@ int vmbus_open(struct vmbus_channel
 *newchannel, u32 send_ringbuffer_size,
 	unsigned long flags;
 	int ret, t, err = 0;
 
-	spin_lock_irqsave(&newchannel->sc_lock, flags);
+	spin_lock_irqsave(&newchannel->lock, flags);
 	if (newchannel->state == CHANNEL_OPEN_STATE) {
 		newchannel->state = CHANNEL_OPENING_STATE;
 	} else {
-		spin_unlock_irqrestore(&newchannel->sc_lock, flags);
+		spin_unlock_irqrestore(&newchannel->lock, flags);
 		return -EINVAL;
 	}
-	spin_unlock_irqrestore(&newchannel->sc_lock, flags);
+	spin_unlock_irqrestore(&newchannel->lock, flags);
 
 	newchannel->onchannel_callback = onchannelcallback;

newchannel->channel_callback_context = context; diff --git
a/drivers/hv/channel_mgmt.c b/drivers/hv/channel_mgmt.c index 01f2c2b..c6fdd74 100644

drivers/hv/channel_mgmt.c +3 -3
@@ -146,7 +146,7 @@ static struct vmbus_channel *alloc_channel(void)
 		return NULL;
 
 	spin_lock_init(&channel->inbound_lock);
-	spin_lock_init(&channel->sc_lock);
+	spin_lock_init(&channel->lock);
 
 	INIT_LIST_HEAD(&channel->sc_list);
 	INIT_LIST_HEAD(&channel->percpu_list);
@@ -246,9 +246,9 @@ static void vmbus_process_rescind_offer(struct
 work_struct *work)
 		spin_unlock_irqrestore(&vmbus_connection.channel_lock,
 flags);
 	} else {
 		primary_channel = channel->primary_channel;
-		spin_lock_irqsave(&primary_channel->sc_lock, flags);
+		spin_lock_irqsave(&primary_channel->lock, flags);
 		list_del(&channel->sc_list);
-		spin_unlock_irqrestore(&primary_channel->sc_lock, flags);
+		spin_unlock_irqrestore(&primary_channel->lock, flags);
 	}
drivers/hv/channel_mgmt.c +2 -2
 	free_channel(channel);
 }
@@ -323,9 +323,9 @@ static void vmbus_process_offer(struct work_struct
 *work)
 			 * Process the sub-channel.
 			 */
 			newchannel->primary_channel = channel;
-			spin_lock_irqsave(&channel->sc_lock, flags);
+			spin_lock_irqsave(&channel->lock, flags);
 			list_add_tail(&newchannel->sc_list, &channel-
 >sc_list);
-			spin_unlock_irqrestore(&channel->sc_lock, flags);
+			spin_unlock_irqrestore(&channel->lock, flags);
 
drivers/hv/channel_mgmt.c +0 -0
 			if (newchannel->target_cpu != get_cpu()) {
 				put_cpu();
include/linux/hyperv.h index renamed from include/linux/hyperv.h +0 -0

476c685..02dd978 100644

include/linux/hyperv.h +6 -2
@@ -722,7 +722,12 @@ struct vmbus_channel {
 	 */
 	void (*sc_creation_callback)(struct vmbus_channel *new_sc);
 
-	spinlock_t sc_lock;
+	/*
+	 * The spinlock to protect the structure. It is being used to protect
+	 * test-and-set access to various attributes of the structure as well
+	 * as all sc_list operations.
+	 */
+	spinlock_t lock;
 	/*
 	 * All Sub-channels of a primary channel are linked here.
 	 */
--

1.9.3

Acked-by on v3 2/3
#6 lore raw

Re: [PATCH v3 2/3] Drivers: hv: rename sc_lock to the more generic lock

In reply to Vitaly Kuznetsov
Quoted 11 lines · Vitaly Kuznetsov

sc_lock spinlock in struct vmbus_channel is being used to not only protect the sc_list field, e.g. vmbus_open() function uses it to implement test-and-set access to the state field. Rename it to the more generic 'lock' and add the description.

---
 drivers/hv/channel.c      |  6 +++---
 drivers/hv/channel_mgmt.c | 10 +++++-----
 include/linux/hyperv.h    |  7 ++++++-
 3 files changed, 14 insertions(+), 9 deletions(-)
Quoted 86 lines
drivers/hv/channel.c +3 -3
index 433f72a..8608ed1 100644
@@ -73,14 +73,14 @@ int vmbus_open(struct vmbus_channel *newchannel, 
 u32 send_ringbuffer_size,
 	unsigned long flags;
 	int ret, t, err = 0;
 
-	spin_lock_irqsave(&newchannel->sc_lock, flags);
+	spin_lock_irqsave(&newchannel->lock, flags);
 	if (newchannel->state == CHANNEL_OPEN_STATE) {
 		newchannel->state = CHANNEL_OPENING_STATE;
 	} else {
-		spin_unlock_irqrestore(&newchannel->sc_lock, flags);
+		spin_unlock_irqrestore(&newchannel->lock, flags);
 		return -EINVAL;
 	}
-	spin_unlock_irqrestore(&newchannel->sc_lock, flags);
+	spin_unlock_irqrestore(&newchannel->lock, flags);
 
 	newchannel->onchannel_callback = onchannelcallback;
drivers/hv/channel.c +0 -0
 	newchannel->channel_callback_context = context;
drivers/hv/channel_mgmt.c +3 -3
index 01f2c2b..c6fdd74 100644
@@ -146,7 +146,7 @@ static struct vmbus_channel *alloc_channel(void)
 		return NULL;
 
 	spin_lock_init(&channel->inbound_lock);
-	spin_lock_init(&channel->sc_lock);
+	spin_lock_init(&channel->lock);
 
 	INIT_LIST_HEAD(&channel->sc_list);
 	INIT_LIST_HEAD(&channel->percpu_list);
@@ -246,9 +246,9 @@ static void vmbus_process_rescind_offer(struct 
 work_struct *work)
 		spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
 	} else {
 		primary_channel = channel->primary_channel;
-		spin_lock_irqsave(&primary_channel->sc_lock, flags);
+		spin_lock_irqsave(&primary_channel->lock, flags);
 		list_del(&channel->sc_list);
-		spin_unlock_irqrestore(&primary_channel->sc_lock, flags);
+		spin_unlock_irqrestore(&primary_channel->lock, flags);
 	}
 	free_channel(channel);

}

drivers/hv/channel_mgmt.c +2 -2
@@ -323,9 +323,9 @@ static void vmbus_process_offer(struct 
 work_struct *work)
 			 * Process the sub-channel.
 			 */
 			newchannel->primary_channel = channel;
-			spin_lock_irqsave(&channel->sc_lock, flags);
+			spin_lock_irqsave(&channel->lock, flags);
 			list_add_tail(&newchannel->sc_list, &channel->sc_list);
-			spin_unlock_irqrestore(&channel->sc_lock, flags);
+			spin_unlock_irqrestore(&channel->lock, flags);
 
 			if (newchannel->target_cpu != get_cpu()) {
drivers/hv/channel_mgmt.c +0 -0
 				put_cpu();
include/linux/hyperv.h +2 -1
index 476c685..02dd978 100644
@@ -722,7 +722,12 @@ struct vmbus_channel {
 	 */
 	void (*sc_creation_callback)(struct vmbus_channel *new_sc);
 
-	spinlock_t sc_lock;
+	/*
+	 * The spinlock to protect the structure. It is being used to 

protect

include/linux/hyperv.h +1 -0
+	 * test-and-set access to various attributes of the structure as 

well

include/linux/hyperv.h +3 -0
+	 * as all sc_list operations.
+	 */
+	spinlock_t lock;
 	/*
 	 * All Sub-channels of a primary channel are linked here.
 	 */
Signature · 1 line
1.9.3
#7 lore raw

PATCH v3 3/3 Drivers: hv: vmbus: serialize Offer and Rescind offer

In reply to Vitaly Kuznetsov

Commit 4b2f9abea52a ("staging: hv: convert channel_mgmt.c to not call osd_schedule_callback")' was written under an assumption that we never receive Rescind offer while we're still processing the initial Offer request. However, the issue we fixed in 04a258c162a8 could be caused by this assumption not always being true.

In particular, we need to protect against the following:
1) Receiving a Rescind offer after we do queue_work() for processing an Offer request and before we actually enter vmbus_process_offer(). work.func points to vmbus_process_offer() at this moment and in vmbus_onoffer_rescind() we do another queue_work() without a check so we'll enter vmbus_process_offer() twice.
2) Receiving a Rescind offer after we enter vmbus_process_offer() and especially after we set >state = CHANNEL_OPEN_STATE. Many things can go wrong in that case, e.g. we can call free_channel() while we're still using it.

Implement the required protection by changing work->func at the very end of vmbus_process_offer() and checking work->func in vmbus_onoffer_rescind(). In case we receive rescind offer during or before vmbus_process_offer() is done we set rescind flag to true and we check it at the end of vmbus_process_offer() so such offer will not get lost.

---
 drivers/hv/channel_mgmt.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
drivers/hv/channel_mgmt.c +22 -8
index c6fdd74..877a944 100644
@@ -279,9 +279,6 @@ static void vmbus_process_offer(struct work_struct *work)
 	int ret;
 	unsigned long flags;
 
-	/* The next possible work is rescind handling */
-	INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
-
 	/* Make sure this is a new offer */
 	spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
 
@@ -341,7 +338,7 @@ static void vmbus_process_offer(struct work_struct *work)
 			if (channel->sc_creation_callback != NULL)
 				channel->sc_creation_callback(newchannel);
 
-			goto out;
+			goto done_init_rescind;
 		}
 
 		goto err_free_chan;
@@ -382,7 +379,14 @@ static void vmbus_process_offer(struct work_struct *work)
 		kfree(newchannel->device_obj);
 		goto err_free_chan;
 	}
-out:
+done_init_rescind:
+	spin_lock_irqsave(&newchannel->lock, flags);
+	/* The next possible work is rescind handling */
+	INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
+	/* Check if rescind offer was already received */
+	if (newchannel->rescind)
+		queue_work(newchannel->controlwq, &newchannel->work);
+	spin_unlock_irqrestore(&newchannel->lock, flags);
 	return;
 err_free_chan:
 	free_channel(newchannel);
@@ -520,6 +524,7 @@ static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
 {
 	struct vmbus_channel_rescind_offer *rescind;
 	struct vmbus_channel *channel;
+	unsigned long flags;
 
 	rescind = (struct vmbus_channel_rescind_offer *)hdr;
 	channel = relid2channel(rescind->child_relid);
@@ -528,11 +533,20 @@ static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
 		/* Just return here, no channel found */
 		return;
 
+	spin_lock_irqsave(&channel->lock, flags);
 	channel->rescind = true;
+	/*
+	 * channel->work.func != vmbus_process_rescind_offer means we are still
+	 * processing offer request and the rescind offer processing should be
+	 * postponed. It will be done at the very end of vmbus_process_offer()
+	 * as rescind flag is being checked there.
+	 */
+	if (channel->work.func == vmbus_process_rescind_offer)
+		/* work is initialized for vmbus_process_rescind_offer() from
+		 * vmbus_process_offer() where the channel got created */
+		queue_work(channel->controlwq, &channel->work);
 
-	/* work is initialized for vmbus_process_rescind_offer() from
-	 * vmbus_process_offer() where the channel got created */
-	queue_work(channel->controlwq, &channel->work);
+	spin_unlock_irqrestore(&channel->lock, flags);
 }
 
 /*
Signature · 1 line
1.9.3
#8 lore raw

RE: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Quoted 39 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Tuesday, January 20, 2015 7:45 AM
To: KY Srinivasan; [email protected]
Cc: Haiyang Zhang; [email protected]; Dexuan Cui; Jason Wang; Radim Krčmář; Dan Carpenter
Subject: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Commit 4b2f9abea52a ("staging: hv: convert channel_mgmt.c to not call osd_schedule_callback")' was written under an assumption that we never receive Rescind offer while we're still processing the initial Offer request. However, the issue we fixed in 04a258c162a8 could be caused by this assumption not always being true.

In particular, we need to protect against the following:
1) Receiving a Rescind offer after we do queue_work() for processing an Offer
request and before we actually enter vmbus_process_offer(). work.func
points
to vmbus_process_offer() at this moment and in vmbus_onoffer_rescind()
we do
another queue_work() without a check so we'll enter
vmbus_process_offer()
twice.
2) Receiving a Rescind offer after we enter vmbus_process_offer() and especially after we set >state = CHANNEL_OPEN_STATE. Many things can
go
wrong in that case, e.g. we can call free_channel() while we're still using it.

Implement the required protection by changing work->func at the very end of
vmbus_process_offer() and checking work->func in vmbus_onoffer_rescind(). In case we receive rescind offer during or before vmbus_process_offer() is done we set rescind flag to true and we check it at the end of vmbus_process_offer() so such offer will not get lost.

Quoted 2 lines

Thanks Vitaly.

Quoted 84 lines
---
 drivers/hv/channel_mgmt.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
drivers/hv/channel_mgmt.c index renamed from drivers/hv/channel_mgmt.c +0 -0

c6fdd74..877a944 100644

drivers/hv/channel_mgmt.c +1 -4
@@ -279,9 +279,6 @@ static void vmbus_process_offer(struct work_struct
 *work)
 	int ret;
 	unsigned long flags;
 
-	/* The next possible work is rescind handling */
-	INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
-
 	/* Make sure this is a new offer */
 	spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
@@ -341,7 +338,7 @@ static void vmbus_process_offer(struct work_struct
 *work)
 			if (channel->sc_creation_callback != NULL)
 				channel->sc_creation_callback(newchannel);
 
-			goto out;
+			goto done_init_rescind;
 		}
 
drivers/hv/channel_mgmt.c +8 -1
 		goto err_free_chan;
@@ -382,7 +379,14 @@ static void vmbus_process_offer(struct work_struct
 *work)
 		kfree(newchannel->device_obj);
 		goto err_free_chan;
 	}
-out:
+done_init_rescind:
+	spin_lock_irqsave(&newchannel->lock, flags);
+	/* The next possible work is rescind handling */
+	INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
+	/* Check if rescind offer was already received */
+	if (newchannel->rescind)
+		queue_work(newchannel->controlwq, &newchannel-
 >work);
+	spin_unlock_irqrestore(&newchannel->lock, flags);
 	return;

err_free_chan:

drivers/hv/channel_mgmt.c +4 -0
 	free_channel(newchannel);
@@ -520,6 +524,7 @@ static void vmbus_onoffer_rescind(struct
 vmbus_channel_message_header *hdr)  {
 	struct vmbus_channel_rescind_offer *rescind;
 	struct vmbus_channel *channel;
+	unsigned long flags;
 
 	rescind = (struct vmbus_channel_rescind_offer *)hdr;
 	channel = relid2channel(rescind->child_relid);
@@ -528,11 +533,20 @@ static void vmbus_onoffer_rescind(struct
 vmbus_channel_message_header *hdr)
 		/* Just return here, no channel found */
 		return;
 
+	spin_lock_irqsave(&channel->lock, flags);
 	channel->rescind = true;
+	/*
+	 * channel->work.func != vmbus_process_rescind_offer means we

are still

drivers/hv/channel_mgmt.c +1 -0
+	 * processing offer request and the rescind offer processing should

be

drivers/hv/channel_mgmt.c +1 -0
+	 * postponed. It will be done at the very end of

vmbus_process_offer()

drivers/hv/channel_mgmt.c +7 -4
+	 * as rescind flag is being checked there.
+	 */
+	if (channel->work.func == vmbus_process_rescind_offer)
+		/* work is initialized for vmbus_process_rescind_offer() from
+		 * vmbus_process_offer() where the channel got created */
+		queue_work(channel->controlwq, &channel->work);
 
-	/* work is initialized for vmbus_process_rescind_offer() from
-	 * vmbus_process_offer() where the channel got created */
-	queue_work(channel->controlwq, &channel->work);
+	spin_unlock_irqrestore(&channel->lock, flags);
 }
 
 /*
--
Quoted 1 line

1.9.3

���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

Acked-by on v3 3/3
#9 lore raw

Re: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

In reply to Vitaly Kuznetsov
Quoted 28 lines · Vitaly Kuznetsov

Commit 4b2f9abea52a ("staging: hv: convert channel_mgmt.c to not call osd_schedule_callback")' was written under an assumption that we never receive Rescind offer while we're still processing the initial Offer request. However, the issue we fixed in 04a258c162a8 could be caused by this assumption not always being true.

In particular, we need to protect against the following:
1) Receiving a Rescind offer after we do queue_work() for processing an Offer request and before we actually enter vmbus_process_offer().
work.func points
to vmbus_process_offer() at this moment and in
vmbus_onoffer_rescind() we do
another queue_work() without a check so we'll enter
vmbus_process_offer()
twice.
2) Receiving a Rescind offer after we enter vmbus_process_offer() and especially after we set >state = CHANNEL_OPEN_STATE. Many things
can go
wrong in that case, e.g. we can call free_channel() while we're
still using
it.

Implement the required protection by changing work->func at the very end of vmbus_process_offer() and checking work->func in vmbus_onoffer_rescind(). In case we receive rescind offer during or before vmbus_process_offer() is done we set rescind flag to true and we check it at the end of vmbus_process_offer() so such offer will not get lost.

Quoted 3 lines

---

Quoted 86 lines
 drivers/hv/channel_mgmt.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
drivers/hv/channel_mgmt.c +1 -4
index c6fdd74..877a944 100644
@@ -279,9 +279,6 @@ static void vmbus_process_offer(struct 
 work_struct *work)
 	int ret;
 	unsigned long flags;
 
-	/* The next possible work is rescind handling */
-	INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
-
 	/* Make sure this is a new offer */
 	spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
@@ -341,7 +338,7 @@ static void vmbus_process_offer(struct 
 work_struct *work)
 			if (channel->sc_creation_callback != NULL)
 				channel->sc_creation_callback(newchannel);
 
-			goto out;
+			goto done_init_rescind;
 		}
 
drivers/hv/channel_mgmt.c +8 -1
 		goto err_free_chan;
@@ -382,7 +379,14 @@ static void vmbus_process_offer(struct 
 work_struct *work)
 		kfree(newchannel->device_obj);
 		goto err_free_chan;
 	}
-out:
+done_init_rescind:
+	spin_lock_irqsave(&newchannel->lock, flags);
+	/* The next possible work is rescind handling */
+	INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
+	/* Check if rescind offer was already received */
+	if (newchannel->rescind)
+		queue_work(newchannel->controlwq, &newchannel->work);
+	spin_unlock_irqrestore(&newchannel->lock, flags);
 	return;
 err_free_chan:
drivers/hv/channel_mgmt.c +1 -0
 	free_channel(newchannel);
@@ -520,6 +524,7 @@ static void vmbus_onoffer_rescind(struct 
 vmbus_channel_message_header *hdr)
 {
 	struct vmbus_channel_rescind_offer *rescind;
 	struct vmbus_channel *channel;
+	unsigned long flags;
 
 	rescind = (struct vmbus_channel_rescind_offer *)hdr;
drivers/hv/channel_mgmt.c +3 -0
 	channel = relid2channel(rescind->child_relid);
@@ -528,11 +533,20 @@ static void vmbus_onoffer_rescind(struct 
 vmbus_channel_message_header *hdr)
 		/* Just return here, no channel found */
 		return;
 
+	spin_lock_irqsave(&channel->lock, flags);
 	channel->rescind = true;
+	/*
+	 * channel->work.func != vmbus_process_rescind_offer means we are 

still

drivers/hv/channel_mgmt.c +1 -0
+	 * processing offer request and the rescind offer processing should 

be

drivers/hv/channel_mgmt.c +1 -0
+	 * postponed. It will be done at the very end of 

vmbus_process_offer()

drivers/hv/channel_mgmt.c +7 -3
+	 * as rescind flag is being checked there.
+	 */
+	if (channel->work.func == vmbus_process_rescind_offer)
+		/* work is initialized for vmbus_process_rescind_offer() from
+		 * vmbus_process_offer() where the channel got created */
+		queue_work(channel->controlwq, &channel->work);
 
-	/* work is initialized for vmbus_process_rescind_offer() from
-	 * vmbus_process_offer() where the channel got created */
-	queue_work(channel->controlwq, &channel->work);
+	spin_unlock_irqrestore(&channel->lock, flags);
 }
 
 /*
Signature · 1 line
1.9.3
maintainer
#10 lore raw

RE: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

In reply to Vitaly Kuznetsov
Quoted 116 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Tuesday, January 20, 2015 23:45 PM
To: KY Srinivasan; [email protected]
Cc: Haiyang Zhang; [email protected]; Dexuan Cui; Jason Wang; Radim Krčmář; Dan Carpenter
Subject: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Commit 4b2f9abea52a ("staging: hv: convert channel_mgmt.c to not call osd_schedule_callback")' was written under an assumption that we never receive
Rescind offer while we're still processing the initial Offer request. However, the issue we fixed in 04a258c162a8 could be caused by this assumption not always being true.

In particular, we need to protect against the following:
1) Receiving a Rescind offer after we do queue_work() for processing an Offer
request and before we actually enter vmbus_process_offer(). work.func
points
to vmbus_process_offer() at this moment and in vmbus_onoffer_rescind()
we do
another queue_work() without a check so we'll enter
vmbus_process_offer()
twice.
2) Receiving a Rescind offer after we enter vmbus_process_offer() and especially after we set >state = CHANNEL_OPEN_STATE. Many things can go wrong in that case, e.g. we can call free_channel() while we're still using it.

Implement the required protection by changing work->func at the very end of
vmbus_process_offer() and checking work->func in vmbus_onoffer_rescind(). In
case we receive rescind offer during or before vmbus_process_offer() is done
we set rescind flag to true and we check it at the end of vmbus_process_offer()
so such offer will not get lost.

---
 drivers/hv/channel_mgmt.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
drivers/hv/channel_mgmt.c +1 -4
index c6fdd74..877a944 100644
@@ -279,9 +279,6 @@ static void vmbus_process_offer(struct work_struct
 *work)
 	int ret;
 	unsigned long flags;
 
-	/* The next possible work is rescind handling */
-	INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
-
 	/* Make sure this is a new offer */
 	spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
@@ -341,7 +338,7 @@ static void vmbus_process_offer(struct work_struct
 *work)
 			if (channel->sc_creation_callback != NULL)
 				channel->sc_creation_callback(newchannel);
 
-			goto out;
+			goto done_init_rescind;
 		}
 
drivers/hv/channel_mgmt.c +8 -1
 		goto err_free_chan;
@@ -382,7 +379,14 @@ static void vmbus_process_offer(struct work_struct
 *work)
 		kfree(newchannel->device_obj);
 		goto err_free_chan;
 	}
-out:
+done_init_rescind:
+	spin_lock_irqsave(&newchannel->lock, flags);
+	/* The next possible work is rescind handling */
+	INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
+	/* Check if rescind offer was already received */
+	if (newchannel->rescind)
+		queue_work(newchannel->controlwq, &newchannel->work);
+	spin_unlock_irqrestore(&newchannel->lock, flags);
 	return;
 err_free_chan:
drivers/hv/channel_mgmt.c +1 -0
 	free_channel(newchannel);
@@ -520,6 +524,7 @@ static void vmbus_onoffer_rescind(struct
 vmbus_channel_message_header *hdr)
 {
 	struct vmbus_channel_rescind_offer *rescind;
 	struct vmbus_channel *channel;
+	unsigned long flags;
 
 	rescind = (struct vmbus_channel_rescind_offer *)hdr;
drivers/hv/channel_mgmt.c +3 -0
 	channel = relid2channel(rescind->child_relid);
@@ -528,11 +533,20 @@ static void vmbus_onoffer_rescind(struct
 vmbus_channel_message_header *hdr)
 		/* Just return here, no channel found */
 		return;
 
+	spin_lock_irqsave(&channel->lock, flags);
 	channel->rescind = true;
+	/*
+	 * channel->work.func != vmbus_process_rescind_offer means we

are still

drivers/hv/channel_mgmt.c +1 -0
+	 * processing offer request and the rescind offer processing should

be

drivers/hv/channel_mgmt.c +1 -0
+	 * postponed. It will be done at the very end of

vmbus_process_offer()

drivers/hv/channel_mgmt.c +5 -0
+	 * as rescind flag is being checked there.
+	 */
+	if (channel->work.func == vmbus_process_rescind_offer)
+		/* work is initialized for vmbus_process_rescind_offer() from
+		 * vmbus_process_offer() where the channel got created */

drivers/hv/channel_mgmt.c static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)

Quoted 10 lines
drivers/hv/channel_mgmt.c +2 -4
+		queue_work(channel->controlwq, &channel->work);
 
-	/* work is initialized for vmbus_process_rescind_offer() from
-	 * vmbus_process_offer() where the channel got created */
-	queue_work(channel->controlwq, &channel->work);
+	spin_unlock_irqrestore(&channel->lock, flags);
 }
 
 /*
--

Hi Vitaly and all,
I have 2 questions:
In vmbus_process_offer(), in the cases of "goto err_free_chan", should we consider the possibility a rescind message could be pending for the new channel?
In the cases, because we don't run "INIT_WORK(&newchannel->work, vmbus_process_rescind_offer); ", vmbus_onoffer_rescind() will do nothing and as a result, vmbus_process_rescind_offer() won't be invoked.

Question 2: in vmbus_process_offer(), in the case vmbus_device_register() fails, we'll run "list_del(&newchannel->listentry);" -- just after this line,
what will happen at this time if relid2channel() returns NULL
in vmbus_onoffer_rescind()?

I think we'll lose the rescind message.

Thanks,
-- Dexuan ���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#11 lore raw

Re: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Quoted 124 lines · Dexuan Cui
Quoted 124 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Tuesday, January 20, 2015 23:45 PM
To: KY Srinivasan; [email protected]
Cc: Haiyang Zhang; [email protected]; Dexuan Cui; Jason Wang; Radim Krčmář; Dan Carpenter
Subject: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Commit 4b2f9abea52a ("staging: hv: convert channel_mgmt.c to not call osd_schedule_callback")' was written under an assumption that we never receive
Rescind offer while we're still processing the initial Offer request. However, the issue we fixed in 04a258c162a8 could be caused by this assumption not always being true.

In particular, we need to protect against the following:
1) Receiving a Rescind offer after we do queue_work() for processing an Offer
request and before we actually enter vmbus_process_offer(). work.func
points
to vmbus_process_offer() at this moment and in vmbus_onoffer_rescind()
we do
another queue_work() without a check so we'll enter
vmbus_process_offer()
twice.
2) Receiving a Rescind offer after we enter vmbus_process_offer() and especially after we set >state = CHANNEL_OPEN_STATE. Many things can go wrong in that case, e.g. we can call free_channel() while we're still using it.

Implement the required protection by changing work->func at the very end of
vmbus_process_offer() and checking work->func in vmbus_onoffer_rescind(). In
case we receive rescind offer during or before vmbus_process_offer() is done
we set rescind flag to true and we check it at the end of vmbus_process_offer()
so such offer will not get lost.

---
 drivers/hv/channel_mgmt.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
drivers/hv/channel_mgmt.c +1 -4
index c6fdd74..877a944 100644
@@ -279,9 +279,6 @@ static void vmbus_process_offer(struct work_struct
 *work)
 	int ret;
 	unsigned long flags;
 
-	/* The next possible work is rescind handling */
-	INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
-
 	/* Make sure this is a new offer */
 	spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
@@ -341,7 +338,7 @@ static void vmbus_process_offer(struct work_struct
 *work)
 			if (channel->sc_creation_callback != NULL)
 				channel->sc_creation_callback(newchannel);
 
-			goto out;
+			goto done_init_rescind;
 		}
 
drivers/hv/channel_mgmt.c +8 -1
 		goto err_free_chan;
@@ -382,7 +379,14 @@ static void vmbus_process_offer(struct work_struct
 *work)
 		kfree(newchannel->device_obj);
 		goto err_free_chan;
 	}
-out:
+done_init_rescind:
+	spin_lock_irqsave(&newchannel->lock, flags);
+	/* The next possible work is rescind handling */
+	INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
+	/* Check if rescind offer was already received */
+	if (newchannel->rescind)
+		queue_work(newchannel->controlwq, &newchannel->work);
+	spin_unlock_irqrestore(&newchannel->lock, flags);
 	return;
 err_free_chan:
drivers/hv/channel_mgmt.c +1 -0
 	free_channel(newchannel);
@@ -520,6 +524,7 @@ static void vmbus_onoffer_rescind(struct
 vmbus_channel_message_header *hdr)
 {
 	struct vmbus_channel_rescind_offer *rescind;
 	struct vmbus_channel *channel;
+	unsigned long flags;
 
 	rescind = (struct vmbus_channel_rescind_offer *)hdr;
drivers/hv/channel_mgmt.c +3 -0
 	channel = relid2channel(rescind->child_relid);
@@ -528,11 +533,20 @@ static void vmbus_onoffer_rescind(struct
 vmbus_channel_message_header *hdr)
 		/* Just return here, no channel found */
 		return;
 
+	spin_lock_irqsave(&channel->lock, flags);
 	channel->rescind = true;
+	/*
+	 * channel->work.func != vmbus_process_rescind_offer means we

are still

drivers/hv/channel_mgmt.c +1 -0
+	 * processing offer request and the rescind offer processing should

be

drivers/hv/channel_mgmt.c +1 -0
+	 * postponed. It will be done at the very end of

vmbus_process_offer()

drivers/hv/channel_mgmt.c +7 -3
+	 * as rescind flag is being checked there.
+	 */
+	if (channel->work.func == vmbus_process_rescind_offer)
+		/* work is initialized for vmbus_process_rescind_offer() from
+		 * vmbus_process_offer() where the channel got created */
+		queue_work(channel->controlwq, &channel->work);
 
-	/* work is initialized for vmbus_process_rescind_offer() from
-	 * vmbus_process_offer() where the channel got created */
-	queue_work(channel->controlwq, &channel->work);
+	spin_unlock_irqrestore(&channel->lock, flags);
 }
Quoted 12 lines
Quoted 2 lines

/*
--

Hi Vitaly and all,
I have 2 questions:
In vmbus_process_offer(), in the cases of "goto err_free_chan", should we consider the possibility a rescind message could be pending for the new channel?
In the cases, because we don't run "INIT_WORK(&newchannel->work, vmbus_process_rescind_offer); ", vmbus_onoffer_rescind() will do nothing and as a result, vmbus_process_rescind_offer() won't be invoked.

Yes, but processing the rescind offer results in freeing the channel (and this processing supposes the channel wasn't freed before) so there is no difference... or is it?

Quoted 9 lines

Question 2: in vmbus_process_offer(), in the case vmbus_device_register() fails, we'll run "list_del(&newchannel->listentry);" -- just after this line,
what will happen at this time if relid2channel() returns NULL
in vmbus_onoffer_rescind()?

I think we'll lose the rescind message.

Yes, but same logic applies - we already freed the channes so no rescind proccessing required. If we still need to do something we need to add support for already freed channel to the rescind offer processing path.

Quoted 2 lines

Thanks,
-- Dexuan

Signature · 1 line
  Vitaly
maintainer
#12 lore raw

RE: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Quoted 42 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Wednesday, January 28, 2015 20:09 PM
To: Dexuan Cui
Cc: KY Srinivasan; [email protected]; Haiyang Zhang; linux- [email protected]; Jason Wang; Radim Krčmář; Dan Carpenter
Subject: Re: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Quoted 7 lines · Dexuan Cui
Quoted 7 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Tuesday, January 20, 2015 23:45 PM
To: KY Srinivasan; [email protected]
Cc: Haiyang Zhang; [email protected]; Dexuan Cui; Jason Wang; Radim Krčmář; Dan Carpenter
Subject: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind

offer
...

Quoted 10 lines

Hi Vitaly and all,
I have 2 questions:
In vmbus_process_offer(), in the cases of "goto err_free_chan", should we consider the possibility a rescind message could be pending for the new channel?
In the cases, because we don't run "INIT_WORK(&newchannel->work, vmbus_process_rescind_offer); ", vmbus_onoffer_rescind() will do nothing and as a result, vmbus_process_rescind_offer() won't be invoked.

Yes, but processing the rescind offer results in freeing the channel (and this processing supposes the channel wasn't freed before) so there is no difference... or is it?

Quoted 7 lines

Question 2: in vmbus_process_offer(), in the case vmbus_device_register() fails, we'll run "list_del(&newchannel->listentry);" -- just after this line,
what will happen at this time if relid2channel() returns NULL
in vmbus_onoffer_rescind()?

Quoted 5 lines
Quoted 2 lines

I think we'll lose the rescind message.

Yes, but same logic applies - we already freed the channes so no rescind proccessing required.

free_channel() and vmbus_process_rescind_offer() are different, because
the latter does more work, e.g., sending the host a message
CHANNELMSG_RELID_RELEASED.

In the cases of "goto err_free_chan" + "a pending rescind message", the host may expect the message CHANNELMSG_RELID_RELEASED and could reoffer the channel once the message is received.

It would be better if the VM doesn't lose the rescind message here. :-)

Quoted 3 lines

If we still need to do something we need to
add support for already freed channel to the rescind offer processing path.

Thanks,
-- Dexuan ���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#13 lore raw

Re: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Quoted 51 lines · Dexuan Cui
Quoted 42 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Wednesday, January 28, 2015 20:09 PM
To: Dexuan Cui
Cc: KY Srinivasan; [email protected]; Haiyang Zhang; linux- [email protected]; Jason Wang; Radim Krčmář; Dan Carpenter
Subject: Re: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Quoted 7 lines · Dexuan Cui
Quoted 7 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Tuesday, January 20, 2015 23:45 PM
To: KY Srinivasan; [email protected]
Cc: Haiyang Zhang; [email protected]; Dexuan Cui; Jason Wang; Radim Krčmář; Dan Carpenter
Subject: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind

offer
...

Quoted 10 lines

Hi Vitaly and all,
I have 2 questions:
In vmbus_process_offer(), in the cases of "goto err_free_chan", should we consider the possibility a rescind message could be pending for the new channel?
In the cases, because we don't run "INIT_WORK(&newchannel->work, vmbus_process_rescind_offer); ", vmbus_onoffer_rescind() will do nothing and as a result, vmbus_process_rescind_offer() won't be invoked.

Yes, but processing the rescind offer results in freeing the channel (and this processing supposes the channel wasn't freed before) so there is no difference... or is it?

Quoted 7 lines

Question 2: in vmbus_process_offer(), in the case vmbus_device_register() fails, we'll run "list_del(&newchannel->listentry);" -- just after this line,
what will happen at this time if relid2channel() returns NULL
in vmbus_onoffer_rescind()?

Quoted 5 lines
Quoted 2 lines

I think we'll lose the rescind message.

Yes, but same logic applies - we already freed the channes so no rescind proccessing required.

free_channel() and vmbus_process_rescind_offer() are different, because
the latter does more work, e.g., sending the host a message
CHANNELMSG_RELID_RELEASED.

Quoted 6 lines

In the cases of "goto err_free_chan" + "a pending rescind message", the host may expect the message CHANNELMSG_RELID_RELEASED and could reoffer the channel once the message is received.

It would be better if the VM doesn't lose the rescind message here. :-)

Ah, I see, CHANNELMSG_RELID_RELEASED is expected from us in any case. I'll doing that in a separate patch is noone objects.

Thanks for the review,

Quoted 7 lines
Quoted 4 lines

If we still need to do something we need to
add support for already freed channel to the rescind offer processing path.

Thanks,
-- Dexuan

Signature · 1 line
  Vitaly
#14 lore raw

Re: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Quoted 65 lines · Vitaly Kuznetsov
Quoted 63 lines · Dexuan Cui
Quoted 48 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Wednesday, January 28, 2015 20:09 PM
To: Dexuan Cui
Cc: KY Srinivasan; [email protected]; Haiyang Zhang;
linux-
[email protected]; Jason Wang; Radim Krčmář; Dan Carpenter
Subject: Re: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer
and Rescind
offer

Quoted 5 lines · Dexuan Cui
Quoted 5 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Tuesday, January 20, 2015 23:45 PM
To: KY Srinivasan; [email protected]
Cc: Haiyang Zhang; [email protected]; Dexuan Cui;

Jason Wang;

Quoted 2 lines
Quoted 2 lines

Radim Krčmář; Dan Carpenter
Subject: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and

Rescind
offer
...

Quoted 5 lines

Hi Vitaly and all,
I have 2 questions:
In vmbus_process_offer(), in the cases of "goto err_free_chan", should we consider the possibility a rescind message could be

pending for

Quoted 5 lines

the new channel?
In the cases, because we don't run "INIT_WORK(&newchannel->work, vmbus_process_rescind_offer); ", vmbus_onoffer_rescind() will do nothing and as a result, vmbus_process_rescind_offer() won't be invoked.

Yes, but processing the rescind offer results in freeing the
channel
(and this processing supposes the channel wasn't freed before) so there is no difference... or is it?

Quoted 7 lines

Question 2: in vmbus_process_offer(), in the case vmbus_device_register() fails, we'll run "list_del(&newchannel->listentry);" -- just after this line,
what will happen at this time if relid2channel() returns NULL
in vmbus_onoffer_rescind()?

Quoted 6 lines
Quoted 2 lines

I think we'll lose the rescind message.

Yes, but same logic applies - we already freed the channes so no
rescind
proccessing required.

free_channel() and vmbus_process_rescind_offer() are different,
because
the latter does more work, e.g., sending the host a message
CHANNELMSG_RELID_RELEASED.

In the cases of "goto err_free_chan" + "a pending rescind message", the host may expect the message CHANNELMSG_RELID_RELEASED and could reoffer the channel once the message is received.

Quoted 5 lines
Quoted 2 lines

It would be better if the VM doesn't lose the rescind message here. :-)

Ah, I see, CHANNELMSG_RELID_RELEASED is expected from us in any case. I'll doing that in a separate patch is noone objects.

All the evil come from the un-serialized processing of message. I wonder whether we can do all the processing in workqueue and then those were automatically serialized.

Quoted 19 lines

Thanks for the review,

Quoted 8 lines
Quoted 5 lines

If we still need to do something we need to
add support for already freed channel to the rescind offer
processing path.

Thanks,
-- Dexuan

Signature · 6 lines
  Vitaly
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
#15 lore raw

RE: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

In reply to Dexuan Cui
Quoted 57 lines · Dexuan Cui
Quoted 47 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Wednesday, January 28, 2015 20:09 PM
To: Dexuan Cui
Cc: KY Srinivasan; [email protected]; Haiyang Zhang;
linux-
[email protected]; Jason Wang; Radim Krčmář; Dan Carpenter
Subject: Re: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and
Rescind
offer

Quoted 5 lines · Dexuan Cui
Quoted 5 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Tuesday, January 20, 2015 23:45 PM
To: KY Srinivasan; [email protected]
Cc: Haiyang Zhang; [email protected]; Dexuan Cui;

Jason Wang;

Quoted 2 lines
Quoted 2 lines

Radim Krčmář; Dan Carpenter
Subject: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and

Rescind
offer
...

Quoted 5 lines

Hi Vitaly and all,
I have 2 questions:
In vmbus_process_offer(), in the cases of "goto err_free_chan", should we consider the possibility a rescind message could be

pending for

Quoted 5 lines

the new channel?
In the cases, because we don't run "INIT_WORK(&newchannel->work, vmbus_process_rescind_offer); ", vmbus_onoffer_rescind() will do nothing and as a result, vmbus_process_rescind_offer() won't be invoked.

Yes, but processing the rescind offer results in freeing the channel (and this processing supposes the channel wasn't freed before) so there is no difference... or is it?

Quoted 7 lines

Question 2: in vmbus_process_offer(), in the case vmbus_device_register() fails, we'll run "list_del(&newchannel->listentry);" -- just after this line,
what will happen at this time if relid2channel() returns NULL
in vmbus_onoffer_rescind()?

Quoted 6 lines
Quoted 2 lines

I think we'll lose the rescind message.

Yes, but same logic applies - we already freed the channes so no
rescind
proccessing required.

free_channel() and vmbus_process_rescind_offer() are different, because
the latter does more work, e.g., sending the host a message
CHANNELMSG_RELID_RELEASED.

Quoted 5 lines

In the cases of "goto err_free_chan" + "a pending rescind message", the host may expect the message CHANNELMSG_RELID_RELEASED and could reoffer the channel once the message is received.

It would be better if the VM doesn't lose the rescind message here. :-)

It's interesting that rescind needs a ack from guest. But looks like the offer does not need it? Is there a spec for this for us for reference?

Thanks

Quoted 9 lines
Quoted 6 lines

If we still need to do something we need to
add support for already freed channel to the rescind offer
processing path.

Thanks,
-- Dexuan

maintainer
#16 lore raw

RE: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Quoted 73 lines

-----Original Message-----
From: Jason Wang [mailto:[email protected]]
Sent: Thursday, January 29, 2015 18:09 PM
To: Dexuan Cui
Cc: Vitaly Kuznetsov; KY Srinivasan; [email protected]; Haiyang Zhang; [email protected]; Radim Krčmář; Dan Carpenter
Subject: RE: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Quoted 62 lines · Dexuan Cui
Quoted 47 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Wednesday, January 28, 2015 20:09 PM
To: Dexuan Cui
Cc: KY Srinivasan; [email protected]; Haiyang Zhang;
linux-
[email protected]; Jason Wang; Radim Krčmář; Dan Carpenter
Subject: Re: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and
Rescind
offer

Quoted 5 lines · Dexuan Cui
Quoted 5 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Tuesday, January 20, 2015 23:45 PM
To: KY Srinivasan; [email protected]
Cc: Haiyang Zhang; [email protected]; Dexuan Cui;

Jason Wang;

Quoted 2 lines
Quoted 2 lines

Radim Krčmář; Dan Carpenter
Subject: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and

Rescind
offer
...

Quoted 5 lines

Hi Vitaly and all,
I have 2 questions:
In vmbus_process_offer(), in the cases of "goto err_free_chan", should we consider the possibility a rescind message could be

pending for

Quoted 5 lines

the new channel?
In the cases, because we don't run "INIT_WORK(&newchannel->work, vmbus_process_rescind_offer); ", vmbus_onoffer_rescind() will do nothing and as a result, vmbus_process_rescind_offer() won't be invoked.

Yes, but processing the rescind offer results in freeing the channel (and this processing supposes the channel wasn't freed before) so there is no difference... or is it?

Quoted 7 lines

Question 2: in vmbus_process_offer(), in the case vmbus_device_register() fails, we'll run "list_del(&newchannel->listentry);" -- just after this line,
what will happen at this time if relid2channel() returns NULL
in vmbus_onoffer_rescind()?

Quoted 6 lines
Quoted 2 lines

I think we'll lose the rescind message.

Yes, but same logic applies - we already freed the channes so no
rescind
proccessing required.

free_channel() and vmbus_process_rescind_offer() are different, because
the latter does more work, e.g., sending the host a message
CHANNELMSG_RELID_RELEASED.

In the cases of "goto err_free_chan" + "a pending rescind message", the host may expect the message CHANNELMSG_RELID_RELEASED and could reoffer the channel once the message is received.

Quoted 6 lines
Quoted 2 lines

It would be better if the VM doesn't lose the rescind message here. :-)

It's interesting that rescind needs a ack from guest. But looks like the offer does not need it? Is there a spec for this for us for reference?

My understanding is:
The host may reuse the same relid after the VM acks the rescind message.

I don't have a VMBus spec either.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

If we still need to do something we need to
add support for already freed channel to the rescind offer
processing path.

This sounds reasonable to me.
Error handling is always full of various corner cases...

Thanks,
-- Dexuan ���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#17 lore raw

RE: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Quoted 84 lines

-----Original Message-----
From: Dexuan Cui
Sent: Thursday, January 29, 2015 8:21 PM
To: Jason Wang
Cc: Vitaly Kuznetsov; KY Srinivasan; [email protected]; Haiyang Zhang; [email protected]; Radim Krčmář; Dan Carpenter
Subject: RE: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Quoted 13 lines

-----Original Message-----
From: Jason Wang [mailto:[email protected]]
Sent: Thursday, January 29, 2015 18:09 PM
To: Dexuan Cui
Cc: Vitaly Kuznetsov; KY Srinivasan; [email protected]; Haiyang Zhang; [email protected]; Radim Krčmář; Dan Carpenter
Subject: RE: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

On Wed, Jan 28, 2015 at 8:51 PM, Dexuan Cui <[email protected]>

Quoted 45 lines · wrote:
Quoted 45 lines
Quoted 45 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Wednesday, January 28, 2015 20:09 PM
To: Dexuan Cui
Cc: KY Srinivasan; [email protected]; Haiyang Zhang;
linux-
[email protected]; Jason Wang; Radim Krčmář; Dan Carpenter
Subject: Re: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer
and Rescind offer

Quoted 2 lines · Dexuan Cui
Quoted 2 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]] >> Sent:

Tuesday, January 20, 2015 23:45 PM >> To: KY Srinivasan; [email protected] >> Cc: Haiyang Zhang; [email protected]; Dexuan Cui; Jason Wang; >> Radim Krčmář; Dan Carpenter >> Subject: [PATCH v3 3/3] Drivers: hv:
vmbus: serialize Offer and Rescind offer ...

Quoted 5 lines

Hi Vitaly and all,
I have 2 questions:
In vmbus_process_offer(), in the cases of "goto err_free_chan", should we consider the possibility a rescind message could be

pending for > the new channel?

Quoted 1 line

In the cases, because we don't run >

"INIT_WORK(&newchannel->work, vmbus_process_rescind_offer); ",

vmbus_onoffer_rescind() will do nothing and as a result, > vmbus_process_rescind_offer() won't be invoked.

Yes, but processing the rescind offer results in freeing the
channel (and this processing supposes the channel wasn't freed before) so there is no difference... or is it?

Quoted 2 lines

Question 2: in vmbus_process_offer(), in the case >

vmbus_device_register() fails, we'll run > "list_del(&newchannel->listentry);" -- just after this line, > what will happen at this time if relid2channel() returns NULL > in vmbus_onoffer_rescind()?

Quoted 3 lines

I think we'll lose the rescind message.

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

Yes, but same logic applies - we already freed the channes so no
rescind proccessing required.

free_channel() and vmbus_process_rescind_offer() are different, because the latter does more work, e.g., sending the host a message CHANNELMSG_RELID_RELEASED.

In the cases of "goto err_free_chan" + "a pending rescind message", the host may expect the message CHANNELMSG_RELID_RELEASED and

could

Quoted 5 lines
Quoted 4 lines

reoffer the channel once the message is received.

It would be better if the VM doesn't lose the rescind message here. :-)

Quoted 8 lines
Quoted 3 lines

It's interesting that rescind needs a ack from guest. But looks like the offer does not need it? Is there a spec for this for us for reference?

My understanding is:
The host may reuse the same relid after the VM acks the rescind message.

I don't have a VMBus spec either.

That is correct. This change was I think introduced recently (requiring the guest to release the RelID).

K. Y

Quoted 11 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

If we still need to do something we need to add support for
already freed channel to the rescind offer processing path.

This sounds reasonable to me.
Error handling is always full of various corner cases...

Thanks,
-- Dexuan

���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#18 lore raw

RE: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

In reply to Dexuan Cui
Quoted 153 lines · Dexuan Cui
Quoted 137 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Tuesday, January 20, 2015 23:45 PM
To: KY Srinivasan; [email protected]
Cc: Haiyang Zhang; [email protected]; Dexuan Cui; Jason
Wang;
Radim Krčmář; Dan Carpenter
Subject: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and
Rescind offer

Commit 4b2f9abea52a ("staging: hv: convert channel_mgmt.c to not
call
osd_schedule_callback")' was written under an assumption that we
never
receive
Rescind offer while we're still processing the initial Offer
request. However,
the issue we fixed in 04a258c162a8 could be caused by this
assumption not
always being true.

In particular, we need to protect against the following:
1) Receiving a Rescind offer after we do queue_work() for
processing an
Offer
request and before we actually enter vmbus_process_offer().
work.func
points
to vmbus_process_offer() at this moment and in
vmbus_onoffer_rescind()
we do
another queue_work() without a check so we'll enter
vmbus_process_offer()
twice.
2) Receiving a Rescind offer after we enter vmbus_process_offer()
and
especially after we set >state = CHANNEL_OPEN_STATE. Many things
can go
wrong in that case, e.g. we can call free_channel() while we're
still using

    it.

Implement the required protection by changing work->func at the
very end
of
vmbus_process_offer() and checking work->func in
vmbus_onoffer_rescind().
In
case we receive rescind offer during or before
vmbus_process_offer() is
done
we set rescind flag to true and we check it at the end of vmbus_process_offer()
so such offer will not get lost.

---

  drivers/hv/channel_mgmt.c | 30 ++++++++++++++++++++++--------
  1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/drivers/hv/channel_mgmt.c b/drivers/hv/channel_mgmt.c index c6fdd74..877a944 100644
--- a/drivers/hv/channel_mgmt.c
+++ b/drivers/hv/channel_mgmt.c
@@ -279,9 +279,6 @@ static void vmbus_process_offer(struct
work_struct
*work)
int ret;
unsigned long flags;

- /* The next possible work is rescind handling */
- INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
-
/* Make sure this is a new offer */ spin_lock_irqsave(&vmbus_connection.channel_lock, flags);

@@ -341,7 +338,7 @@ static void vmbus_process_offer(struct
work_struct
*work)
if (channel->sc_creation_callback != NULL)
channel->sc_creation_callback(newchannel);

- goto out;
+ goto done_init_rescind;
}

goto err_free_chan;
@@ -382,7 +379,14 @@ static void vmbus_process_offer(struct
work_struct
*work)
kfree(newchannel->device_obj);
goto err_free_chan;
}
-out:
+done_init_rescind:
+ spin_lock_irqsave(&newchannel->lock, flags);
+ /* The next possible work is rescind handling */
+ INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
+ /* Check if rescind offer was already received */
+ if (newchannel->rescind)
+ queue_work(newchannel->controlwq, &newchannel->work);
+ spin_unlock_irqrestore(&newchannel->lock, flags);
return;
err_free_chan:
free_channel(newchannel);
@@ -520,6 +524,7 @@ static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
{
struct vmbus_channel_rescind_offer *rescind; struct vmbus_channel *channel;
+ unsigned long flags;

rescind = (struct vmbus_channel_rescind_offer *)hdr; channel = relid2channel(rescind->child_relid);
@@ -528,11 +533,20 @@ static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
/* Just return here, no channel found */ return;

+ spin_lock_irqsave(&channel->lock, flags);
channel->rescind = true;
+ /*
+ * channel->work.func != vmbus_process_rescind_offer means we
are still
+ * processing offer request and the rescind offer processing
should
be
+ * postponed. It will be done at the very end of
vmbus_process_offer()
+ * as rescind flag is being checked there.
+ */
+ if (channel->work.func == vmbus_process_rescind_offer)
+ /* work is initialized for vmbus_process_rescind_offer() from
+ * vmbus_process_offer() where the channel got created */
+ queue_work(channel->controlwq, &channel->work);

Quoted 8 lines

- /* work is initialized for vmbus_process_rescind_offer() from
- * vmbus_process_offer() where the channel got created */
- queue_work(channel->controlwq, &channel->work);
+ spin_unlock_irqrestore(&channel->lock, flags);
}

/*
--

Hi Vitaly and all,
I have 2 questions:
In vmbus_process_offer(), in the cases of "goto err_free_chan",
should we consider the possibility a rescind message could be pending for the new channel? In the cases, because we don't run "INIT_WORK(&newchannel->work, vmbus_process_rescind_offer); ", vmbus_onoffer_rescind() will do nothing and as a result, vmbus_process_rescind_offer() won't be invoked.

Quoted 7 lines

Question 2: in vmbus_process_offer(), in the case vmbus_device_register() fails, we'll run "list_del(&newchannel->listentry);" -- just after this line,
what will happen at this time if relid2channel() returns NULL
in vmbus_onoffer_rescind()?

I think we'll lose the rescind message.

If CHANNELMSG_RELID_RELEASED is mandatory, need INIT_WORK during onoffer_rescind() unconditionally and and we need post this message without the help of relid2channel() since:

- relid2channel() only valid when the channel was added to list, so either in the case of question 2 or before list_add_tail() in vmbus_process_offer()
- the channel rescind offer message has a relid

Quoted 4 lines

Thanks,
-- Dexuan

#19 lore raw

RE: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Quoted 171 lines

-----Original Message-----
From: Jason Wang [mailto:[email protected]]
Sent: Thursday, January 29, 2015 2:07 AM
To: Dexuan Cui
Cc: Vitaly Kuznetsov; KY Srinivasan; [email protected]; Haiyang Zhang; [email protected]; Radim Krčmář; Dan Carpenter
Subject: RE: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and Rescind offer

Quoted 149 lines · Dexuan Cui
Quoted 79 lines

-----Original Message-----
From: Vitaly Kuznetsov [mailto:[email protected]]
Sent: Tuesday, January 20, 2015 23:45 PM
To: KY Srinivasan; [email protected]
Cc: Haiyang Zhang; [email protected]; Dexuan Cui; Jason
Wang; Radim Krčmář; Dan Carpenter
Subject: [PATCH v3 3/3] Drivers: hv: vmbus: serialize Offer and
Rescind offer

Commit 4b2f9abea52a ("staging: hv: convert channel_mgmt.c to not
call osd_schedule_callback")' was written under an assumption that we never receive Rescind offer while we're still processing the initial Offer request. However, the issue we fixed in 04a258c162a8 could be caused by this assumption not always being true.

In particular, we need to protect against the following:
1) Receiving a Rescind offer after we do queue_work() for processing
an Offer
request and before we actually enter vmbus_process_offer().
work.func
points
to vmbus_process_offer() at this moment and in
vmbus_onoffer_rescind()
we do
another queue_work() without a check so we'll enter
vmbus_process_offer()
twice.
2) Receiving a Rescind offer after we enter vmbus_process_offer()
and
especially after we set >state = CHANNEL_OPEN_STATE. Many things
can go
wrong in that case, e.g. we can call free_channel() while we're
still using

    it.

Implement the required protection by changing work->func at the very
end of
vmbus_process_offer() and checking work->func in
vmbus_onoffer_rescind().
In
case we receive rescind offer during or before
vmbus_process_offer() is
done
we set rescind flag to true and we check it at the end of vmbus_process_offer()
so such offer will not get lost.

---

  drivers/hv/channel_mgmt.c | 30 ++++++++++++++++++++++--------
  1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/drivers/hv/channel_mgmt.c b/drivers/hv/channel_mgmt.c
index c6fdd74..877a944 100644
--- a/drivers/hv/channel_mgmt.c
+++ b/drivers/hv/channel_mgmt.c
@@ -279,9 +279,6 @@ static void vmbus_process_offer(struct
work_struct
*work)
int ret;
unsigned long flags;

- /* The next possible work is rescind handling */
- INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
-
/* Make sure this is a new offer */ spin_lock_irqsave(&vmbus_connection.channel_lock, flags);

@@ -341,7 +338,7 @@ static void vmbus_process_offer(struct
work_struct
*work)
if (channel->sc_creation_callback != NULL)
channel->sc_creation_callback(newchannel);

- goto out;
+ goto done_init_rescind;
}

Quoted 15 lines

goto err_free_chan;
@@ -382,7 +379,14 @@ static void vmbus_process_offer(struct
work_struct
*work)
kfree(newchannel->device_obj); goto err_free_chan;
}
-out:
+done_init_rescind:
+ spin_lock_irqsave(&newchannel->lock, flags);
+ /* The next possible work is rescind handling */
+ INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
+ /* Check if rescind offer was already received */
+ if (newchannel->rescind)
+ queue_work(newchannel->controlwq, &newchannel-

work);

Quoted 35 lines

+ spin_unlock_irqrestore(&newchannel->lock, flags);
return;
err_free_chan:
free_channel(newchannel);
@@ -520,6 +524,7 @@ static void vmbus_onoffer_rescind(struct
vmbus_channel_message_header *hdr)
{
struct vmbus_channel_rescind_offer *rescind; struct vmbus_channel *channel;
+ unsigned long flags;

rescind = (struct vmbus_channel_rescind_offer *)hdr; channel = relid2channel(rescind->child_relid);
@@ -528,11 +533,20 @@ static void vmbus_onoffer_rescind(struct
vmbus_channel_message_header *hdr)
/* Just return here, no channel found */ return;

+ spin_lock_irqsave(&channel->lock, flags);
channel->rescind = true;
+ /*
+ * channel->work.func != vmbus_process_rescind_offer means we
are still
+ * processing offer request and the rescind offer processing
should
be
+ * postponed. It will be done at the very end of
vmbus_process_offer()
+ * as rescind flag is being checked there.
+ */
+ if (channel->work.func == vmbus_process_rescind_offer)
+ /* work is initialized for vmbus_process_rescind_offer() from
+ * vmbus_process_offer() where the channel got created */
+ queue_work(channel->controlwq, &channel->work);

Quoted 8 lines

- /* work is initialized for vmbus_process_rescind_offer() from
- * vmbus_process_offer() where the channel got created */
- queue_work(channel->controlwq, &channel->work);
+ spin_unlock_irqrestore(&channel->lock, flags);
}

/*
--

Hi Vitaly and all,
I have 2 questions:
In vmbus_process_offer(), in the cases of "goto err_free_chan", should we consider the possibility a rescind message could be pending for the new channel?
In the cases, because we don't run "INIT_WORK(&newchannel->work, vmbus_process_rescind_offer); ", vmbus_onoffer_rescind() will do nothing and as a result, vmbus_process_rescind_offer() won't be invoked.

Quoted 7 lines

Question 2: in vmbus_process_offer(), in the case vmbus_device_register() fails, we'll run "list_del(&newchannel->listentry);" -- just after this line, what will happen at this time if relid2channel() returns NULL in vmbus_onoffer_rescind()?

I think we'll lose the rescind message.

Quoted 9 lines

If CHANNELMSG_RELID_RELEASED is mandatory, need INIT_WORK during onoffer_rescind() unconditionally and and we need post this message without the help of relid2channel() since:

- relid2channel() only valid when the channel was added to list, so either in the case of question 2 or before list_add_tail() in vmbus_process_offer()
- the channel rescind offer message has a relid

Once the channel is rescinded from the host, we need to send the RELID_RELEASED message to the host for the host to potentially resend the offer.

K. Y

Quoted 4 lines
Quoted 4 lines

Thanks,
-- Dexuan

���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�