Skip to content

Commit a949e86

Browse files
andrea-parriliuw
authored andcommitted
Drivers: hv: vmbus: Resolve race between init_vp_index() and CPU hotplug
vmbus_process_offer() does two things (among others): 1) first, it sets the channel's target CPU with cpu_hotplug_lock; 2) it then adds the channel to the channel list(s) with channel_mutex. Since cpu_hotplug_lock is released before (2), the channel's target CPU (as designated in (1)) can be deemed "free" by hv_synic_cleanup() and go offline before the channel is added to the list. Fix the race condition by "extending" the cpu_hotplug_lock critical section to include (2) (and (1)), nesting the channel_mutex critical section within the cpu_hotplug_lock critical section as done elsewhere (hv_synic_cleanup(), target_cpu_store()) in the hyperv drivers code. Move even further by extending the channel_mutex critical section to include (1) (and (2)): this change allows to remove (the now redundant) bind_channel_to_cpu_lock, and generally simplifies the handling of the target CPUs (that are now always modified with channel_mutex held). Fixes: d570aec ("Drivers: hv: vmbus: Synchronize init_vp_index() vs. CPU hotplug") Signed-off-by: Andrea Parri (Microsoft) <[email protected]> Reviewed-by: Michael Kelley <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Wei Liu <[email protected]>
1 parent db5871e commit a949e86

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

drivers/hv/channel_mgmt.c

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -554,26 +554,34 @@ static void vmbus_process_offer(struct vmbus_channel *newchannel)
554554
bool fnew = true;
555555

556556
/*
557-
* Initialize the target_CPU before inserting the channel in
558-
* the chn_list and sc_list lists, within the channel_mutex
559-
* critical section:
557+
* Synchronize vmbus_process_offer() and CPU hotplugging:
560558
*
561559
* CPU1 CPU2
562560
*
563-
* [vmbus_process_offer()] [hv_syninc_cleanup()]
561+
* [vmbus_process_offer()] [Hot removal of the CPU]
564562
*
565-
* STORE target_cpu LOCK channel_mutex
566-
* LOCK channel_mutex SEARCH chn_list
567-
* INSERT chn_list LOAD target_cpu
568-
* UNLOCK channel_mutex UNLOCK channel_mutex
563+
* CPU_READ_LOCK CPUS_WRITE_LOCK
564+
* LOAD cpu_online_mask SEARCH chn_list
565+
* STORE target_cpu LOAD target_cpu
566+
* INSERT chn_list STORE cpu_online_mask
567+
* CPUS_READ_UNLOCK CPUS_WRITE_UNLOCK
568+
*
569+
* Forbids: CPU1's LOAD from *not* seing CPU2's STORE &&
570+
* CPU2's SEARCH from *not* seeing CPU1's INSERT
569571
*
570572
* Forbids: CPU2's SEARCH from seeing CPU1's INSERT &&
571573
* CPU2's LOAD from *not* seing CPU1's STORE
572574
*/
573-
init_vp_index(newchannel, hv_get_dev_type(newchannel));
575+
cpus_read_lock();
574576

577+
/*
578+
* Serializes the modifications of the chn_list list as well as
579+
* the accesses to next_numa_node_id in init_vp_index().
580+
*/
575581
mutex_lock(&vmbus_connection.channel_mutex);
576582

583+
init_vp_index(newchannel, hv_get_dev_type(newchannel));
584+
577585
/* Remember the channels that should be cleaned up upon suspend. */
578586
if (is_hvsock_channel(newchannel) || is_sub_channel(newchannel))
579587
atomic_inc(&vmbus_connection.nr_chan_close_on_suspend);
@@ -623,6 +631,7 @@ static void vmbus_process_offer(struct vmbus_channel *newchannel)
623631
vmbus_channel_map_relid(newchannel);
624632

625633
mutex_unlock(&vmbus_connection.channel_mutex);
634+
cpus_read_unlock();
626635

627636
/*
628637
* vmbus_process_offer() mustn't call channel->sc_creation_callback()
@@ -655,13 +664,6 @@ static void vmbus_process_offer(struct vmbus_channel *newchannel)
655664
* We use this state to statically distribute the channel interrupt load.
656665
*/
657666
static int next_numa_node_id;
658-
/*
659-
* init_vp_index() accesses global variables like next_numa_node_id, and
660-
* it can run concurrently for primary channels and sub-channels: see
661-
* vmbus_process_offer(), so we need the lock to protect the global
662-
* variables.
663-
*/
664-
static DEFINE_SPINLOCK(bind_channel_to_cpu_lock);
665667

666668
/*
667669
* Starting with Win8, we can statically distribute the incoming
@@ -700,15 +702,6 @@ static void init_vp_index(struct vmbus_channel *channel, u16 dev_type)
700702
return;
701703
}
702704

703-
/* No CPUs can come up or down during this. */
704-
cpus_read_lock();
705-
706-
/*
707-
* Serializes the accesses to the global variable next_numa_node_id.
708-
* See also the header comment of the spin lock declaration.
709-
*/
710-
spin_lock(&bind_channel_to_cpu_lock);
711-
712705
while (true) {
713706
numa_node = next_numa_node_id++;
714707
if (numa_node == nr_node_ids) {
@@ -739,9 +732,6 @@ static void init_vp_index(struct vmbus_channel *channel, u16 dev_type)
739732
channel->target_cpu = target_cpu;
740733
channel->target_vp = hv_cpu_number_to_vp_number(target_cpu);
741734

742-
spin_unlock(&bind_channel_to_cpu_lock);
743-
cpus_read_unlock();
744-
745735
free_cpumask_var(available_mask);
746736
}
747737

0 commit comments

Comments
 (0)