Drivers: hv: vmbus: Miscellaneous Fixes
cover Drivers: hv: vmbus: Miscellaneous Fixes
From: K. Y. Srinivasan <[email protected]>
These patches fix several issues introduced in 4.11. Greg, Please pick these patches for 4.11.
Dexuan Cui (2):
vmbus: remove hv_event_tasklet_disable/enable
Drivers: hv: util: don't forget to init host_ts.lock
Stephen Hemminger (1):
vmbus: use rcu for per-cpu channel list
Vitaly Kuznetsov (1):
Drivers: hv: util: move waiting for release to hv_utils_transport
itself
drivers/hv/channel.c | 12 ++++-------- drivers/hv/channel_mgmt.c | 26 ++++---------------------- drivers/hv/hv_fcopy.c | 4 ---- drivers/hv/hv_kvp.c | 4 ---- drivers/hv/hv_snapshot.c | 4 ---- drivers/hv/hv_util.c | 2 ++ drivers/hv/hv_utils_transport.c | 12 ++++++++---- drivers/hv/hv_utils_transport.h | 1 + drivers/hv/vmbus_drv.c | 6 +++++- include/linux/hyperv.h | 10 +++++++--- 10 files changed, 31 insertions(+), 50 deletions(-)
PATCH 1/4 vmbus: use rcu for per-cpu channel list
From: Stephen Hemminger <[email protected]>
The per-cpu channel list is now referred to in the interrupt routine. This is mostly safe since the host will not normally generate an interrupt when channel is being deleted but if it did then there would be a use after free problem.
To solve, this use RCU protection on ther per-cpu list.
--- drivers/hv/channel_mgmt.c | 7 ++++--- drivers/hv/vmbus_drv.c | 6 +++++- include/linux/hyperv.h | 7 +++++++ 3 files changed, 16 insertions(+), 4 deletions(-)
drivers/hv/channel_mgmt.c +4 -3
@@ -350,7 +350,8 @@ bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,static void free_channel (struct vmbus_channel *channel) {tasklet_kill (&channel->callback_event); -kfree (channel); + +kfree_rcu (channel, rcu); }static void percpu_channel_enq (void *arg)
@@ -359,14 +360,14 @@ static void percpu_channel_enq(void *arg)struct hv_per_cpu_context *hv_cpu =this_cpu_ptr (hv_context.cpu_context); -list_add_tail (&channel->percpu_list, &hv_cpu->chan_list); +list_add_tail_rcu (&channel->percpu_list, &hv_cpu->chan_list); }static void percpu_channel_deq (void *arg) {struct vmbus_channel *channel = arg; -list_del (&channel->percpu_list); +list_del_rcu (&channel->percpu_list); }
drivers/hv/vmbus_drv.c +5 -1
@@ -937,8 +937,10 @@ static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)if (relid ==0 )continue ; +rcu_read_lock (); +/* Find channel based on relid */ -list_for_each_entry (channel, &hv_cpu->chan_list, percpu_list) { +list_for_each_entry_rcu (channel, &hv_cpu->chan_list, percpu_list) {if (channel->offermsg.child_relid != relid)continue ;
@@ -954,6 +956,8 @@ static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)tasklet_schedule (&channel->callback_event); } } + +rcu_read_unlock (); } }
include/linux/hyperv.h +7 -0
@@ -845,6 +845,13 @@ struct vmbus_channel {* link up channels based on their CPU affinity. */ struct list_head percpu_list; + +/* +* Defer freeing channel until after all cpu's have +* gone through grace period. +*/ +struct rcu_head rcu; +/* * For performance critical channels (storage, networking * etc,), Hyper-V has a mechanism to enhance the throughput
Signature · 1 line
1.7.1
PATCH 2/4 vmbus: remove hv_event_tasklet_disable/enable
In reply to [email protected]From: Dexuan Cui <[email protected]>
With the recent introduction of per-channel tasklet, we need to update the way we handle the 3 concurrency issues:
1. hv_process_channel_removal -> percpu_channel_deq vs. vmbus_chan_sched -> list_for_each_entry(..., percpu_list);
2. vmbus_process_offer -> percpu_channel_enq/deq vs. vmbus_chan_sched.
3. vmbus_close_internal vs. the per-channel tasklet vmbus_on_event;
The first 2 issues can be handled by Stephen's recent patch "vmbus: use rcu for per-cpu channel list", and the third issue can be handled by calling tasklet_disable in vmbus_close_internal here.
We don't need the original hv_event_tasklet_disable/enable since we now use per-channel tasklet instead of the previous per-CPU tasklet, and actually we must remove them due to the side effect now:vmbus_process_offer -> hv_event_tasklet_enable -> tasklet_schedule will start the per-channel callback prematurely, cauing NULL dereferencing (the channel may haven't been properly configured to run the callback yet).
--- drivers/hv/channel.c | 12 ++++-------- drivers/hv/channel_mgmt.c | 19 ------------------- include/linux/hyperv.h | 3 --- 3 files changed, 4 insertions(+), 30 deletions(-)
drivers/hv/channel.c +4 -8
@@ -530,15 +530,13 @@ static int vmbus_close_internal(struct vmbus_channel *channel)int ret;/* -* vmbus_on_event(), running in the tasklet, can race +* vmbus_on_event(), running in the per-channel tasklet, can race * with vmbus_close_internal() in the case of SMP guest, e.g., when * the former is accessing channel->inbound.ring_buffer, the latter -* could be freeing the ring_buffer pages. -* -* To resolve the race, we can serialize them by disabling the -* tasklet when the latter is running here. +* could be freeing the ring_buffer pages, so here we must stop it +* first. */ -hv_event_tasklet_disable (channel); +tasklet_disable (&channel->callback_event);/* * In case a device driver's probe() fails (e.g.,
@@ -605,8 +603,6 @@ static int vmbus_close_internal(struct vmbus_channel *channel) get_order(channel->ringbuffer_pagecount * PAGE_SIZE)); out: -hv_event_tasklet_enable (channel); -return ret; }
drivers/hv/channel_mgmt.c +0 -19
@@ -382,19 +382,6 @@ static void vmbus_release_relid(u32 relid) true); } -void hv_event_tasklet_disable (struct vmbus_channel *channel) -{ -tasklet_disable (&channel->callback_event); -} - -void hv_event_tasklet_enable (struct vmbus_channel *channel) -{ -tasklet_enable (&channel->callback_event); - -/* In case there is any pending event */ -tasklet_schedule (&channel->callback_event); -} -void hv_process_channel_removal (struct vmbus_channel *channel,u32 relid) {unsigned long flags;
@@ -403,7 +390,6 @@ void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid)BUG_ON (!channel->rescind);BUG_ON (!mutex_is_locked (&vmbus_connection.channel_mutex)); -hv_event_tasklet_disable (channel);if (channel->target_cpu !=get_cpu ()) {put_cpu (); smp_call_function_single(channel->target_cpu,
@@ -412,7 +398,6 @@ void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid)percpu_channel_deq (channel);put_cpu (); } -hv_event_tasklet_enable (channel);if (channel->primary_channel ==NULL ) {list_del (&channel->listentry);
@@ -506,7 +491,6 @@ static void vmbus_process_offer(struct vmbus_channel *newchannel)init_vp_index (newchannel, dev_type); -hv_event_tasklet_disable (newchannel);if (newchannel->target_cpu !=get_cpu ()) {put_cpu (); smp_call_function_single(newchannel->target_cpu,
@@ -516,7 +500,6 @@ static void vmbus_process_offer(struct vmbus_channel *newchannel)percpu_channel_enq (newchannel);put_cpu (); } -hv_event_tasklet_enable (newchannel);/* * This state is used to indicate a successful open
@@ -566,7 +549,6 @@ static void vmbus_process_offer(struct vmbus_channel *newchannel)list_del (&newchannel->listentry);mutex_unlock (&vmbus_connection.channel_mutex); -hv_event_tasklet_disable (newchannel);if (newchannel->target_cpu !=get_cpu ()) {put_cpu (); smp_call_function_single(newchannel->target_cpu,
@@ -575,7 +557,6 @@ static void vmbus_process_offer(struct vmbus_channel *newchannel)percpu_channel_deq (newchannel);put_cpu (); } -hv_event_tasklet_enable (newchannel);vmbus_release_relid (newchannel->offermsg.child_relid);
include/linux/hyperv.h +0 -3
@@ -1437,9 +1437,6 @@ extern bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, u8 *buf, const int *srv_version, int srv_vercnt, int *nego_fw_version, int *nego_srv_version); -void hv_event_tasklet_disable (struct vmbus_channel *channel); -void hv_event_tasklet_enable (struct vmbus_channel *channel); -void hv_process_channel_removal (struct vmbus_channel *channel,u32 relid);void vmbus_setevent (struct vmbus_channel *channel);
Signature · 1 line
1.7.1
PATCH 3/4 Drivers: hv: util: move waiting for release to hv_utils_transport itself
In reply to [email protected]From: Vitaly Kuznetsov <[email protected]>
Waiting for release_event in all three drivers introduced issues on release as on_reset() hook is not always called. E.g. if the device was never opened we will never get the completion.
Move the waiting code to hvutil_transport_destroy() and make sure it is only called when the device is open. hvt->lock serialization should guarantee the absence of races.
--- drivers/hv/hv_fcopy.c | 4 ---- drivers/hv/hv_kvp.c | 4 ---- drivers/hv/hv_snapshot.c | 4 ---- drivers/hv/hv_utils_transport.c | 12 ++++++++---- drivers/hv/hv_utils_transport.h | 1 + 5 files changed, 9 insertions(+), 16 deletions(-)
drivers/hv/hv_fcopy.c +0 -4
@@ -71,7 +71,6 @@static const char fcopy_devname[] ="vmbus/hv_fcopy" ;static u8 *recv_buffer;static struct hvutil_transport *hvt; -static struct completion release_event;/* * This state maintains the version number registered by the daemon. */
@@ -331,7 +330,6 @@ static void fcopy_on_reset(void)if (cancel_delayed_work_sync (&fcopy_timeout_work))fcopy_respond_to_host (HV_E_FAIL ); -complete (&release_event); }int hv_fcopy_init (struct hv_util_service *srv)
@@ -339,7 +337,6 @@ int hv_fcopy_init(struct hv_util_service *srv) recv_buffer = srv->recv_buffer; fcopy_transaction.recv_channel = srv->channel; -init_completion (&release_event);/* * When this driver loads, the user level daemon that * processes the host requests may not yet be running.
@@ -361,5 +358,4 @@ void hv_fcopy_deinit(void) fcopy_transaction.state =HVUTIL_DEVICE_DYING ;cancel_delayed_work_sync (&fcopy_timeout_work);hvutil_transport_destroy (hvt); -wait_for_completion (&release_event); }
drivers/hv/hv_kvp.c +0 -4
@@ -101,7 +101,6 @@static const char kvp_devname[] ="vmbus/hv_kvp" ;static u8 *recv_buffer;static struct hvutil_transport *hvt; -static struct completion release_event;/* * Register the kernel component with the user-level daemon. * As part of this registration, pass the LIC version number.
@@ -714,7 +713,6 @@ static void kvp_on_reset(void)if (cancel_delayed_work_sync (&kvp_timeout_work))kvp_respond_to_host (NULL ,HV_E_FAIL ); kvp_transaction.state =HVUTIL_DEVICE_INIT ; -complete (&release_event); }int
@@ -723,7 +721,6 @@ static void kvp_on_reset(void) recv_buffer = srv->recv_buffer; kvp_transaction.recv_channel = srv->channel; -init_completion (&release_event);/* * When this driver loads, the user level daemon that * processes the host requests may not yet be running.
@@ -747,5 +744,4 @@ void hv_kvp_deinit(void)cancel_delayed_work_sync (&kvp_timeout_work);cancel_work_sync (&kvp_sendkey_work);hvutil_transport_destroy (hvt); -wait_for_completion (&release_event); }
drivers/hv/hv_snapshot.c +0 -4
@@ -79,7 +79,6 @@static const char vss_devname[] ="vmbus/hv_vss" ;static __u8 *recv_buffer;static struct hvutil_transport *hvt; -static struct completion release_event;static void vss_timeout_func (struct work_struct *dummy);static void vss_handle_request (struct work_struct *dummy);
@@ -361,13 +360,11 @@ static void vss_on_reset(void)if (cancel_delayed_work_sync (&vss_timeout_work))vss_respond_to_host (HV_E_FAIL ); vss_transaction.state =HVUTIL_DEVICE_INIT ; -complete (&release_event); }int hv_vss_init (struct hv_util_service *srv) { -init_completion (&release_event);if (vmbus_proto_version <VERSION_WIN8_1 ) {pr_warn ("Integration service 'Backup (volume snapshot)'" " not supported on this host version.\n" );
@@ -400,5 +397,4 @@ void hv_vss_deinit(void)cancel_delayed_work_sync (&vss_timeout_work);cancel_work_sync (&vss_handle_request_work);hvutil_transport_destroy (hvt); -wait_for_completion (&release_event); }
drivers/hv/hv_utils_transport.c +8 -4
@@ -182,10 +182,11 @@ static int hvt_op_release(struct inode *inode, struct file *file)* connects back. */ hvt_reset (hvt); -mutex_unlock (&hvt->lock);if (mode_old ==HVUTIL_TRANSPORT_DESTROY ) -hvt_transport_free (hvt); +complete (&hvt->release); + +mutex_unlock (&hvt->lock);return 0 ; }
@@ -304,6 +305,7 @@ struct hvutil_transport *hvutil_transport_init(const char *name,init_waitqueue_head (&hvt->outmsg_q);mutex_init (&hvt->lock); +init_completion (&hvt->release);spin_lock (&hvt_list_lock);list_add (&hvt->list, &hvt_list);
@@ -351,6 +353,8 @@ void hvutil_transport_destroy(struct hvutil_transport *hvt)if (hvt->cn_id.idx >0 && hvt->cn_id.val >0 )cn_del_callback (&hvt->cn_id); -if (mode_old !=HVUTIL_TRANSPORT_CHARDEV ) -hvt_transport_free (hvt); +if (mode_old ==HVUTIL_TRANSPORT_CHARDEV ) +wait_for_completion (&hvt->release); + +hvt_transport_free (hvt); }
drivers/hv/hv_utils_transport.h +1 -0
@@ -41,6 +41,7 @@ struct hvutil_transport {int outmsg_len;/* its length */ wait_queue_head_t outmsg_q;/* poll/read wait queue */ struct mutex lock;/* protects struct members */ +struct completion release;/* synchronize with fd release */ }; struct hvutil_transport *hvutil_transport_init(const char *name,
Signature · 1 line
1.7.1
PATCH 4/4 Drivers: hv: util: don't forget to init host_ts.lock
In reply to [email protected]From: Dexuan Cui <[email protected]>
Without the patch, I always get a "BUG: spinlock bad magic" warning.
--- drivers/hv/hv_util.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
drivers/hv/hv_util.c +2 -0
@@ -590,6 +590,8 @@ static int hv_timesync_init(struct hv_util_service *srv)if (!hyperv_cs)return -ENODEV ; +spin_lock_init (&host_ts.lock); +INIT_WORK (&wrk.work, hv_set_host_time);/*
Signature · 1 line
1.7.1