1 From 25f1f17ea35f080919b29e3993c2c00a7d0e27f9 Mon Sep 17 00:00:00 2001
2 From: Michael Kelley <mikelley@microsoft.com>
3 Date: Mon, 1 Apr 2019 16:10:52 +0000
4 Subject: scsi: storvsc: Fix calculation of sub-channel count
6 [ Upstream commit 382e06d11e075a40b4094b6ef809f8d4bcc7ab2a ]
8 When the number of sub-channels offered by Hyper-V is >= the number of CPUs
9 in the VM, calculate the correct number of sub-channels. The current code
10 produces one too many.
12 This scenario arises only when the number of CPUs is artificially
13 restricted (for example, with maxcpus=<n> on the kernel boot line), because
14 Hyper-V normally offers a sub-channel count < number of CPUs. While the
15 current code doesn't break, the extra sub-channel is unbalanced across the
16 CPUs (for example, a total of 5 channels on a VM with 4 CPUs).
18 Signed-off-by: Michael Kelley <mikelley@microsoft.com>
19 Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>
20 Reviewed-by: Long Li <longli@microsoft.com>
21 Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
22 Signed-off-by: Sasha Levin <sashal@kernel.org>
24 drivers/scsi/storvsc_drv.c | 13 +++++++++++--
25 1 file changed, 11 insertions(+), 2 deletions(-)
27 diff --git a/drivers/scsi/storvsc_drv.c b/drivers/scsi/storvsc_drv.c
28 index beb585ddc07d..5adeb1e4b186 100644
29 --- a/drivers/scsi/storvsc_drv.c
30 +++ b/drivers/scsi/storvsc_drv.c
31 @@ -658,13 +658,22 @@ static void handle_sc_creation(struct vmbus_channel *new_sc)
32 static void handle_multichannel_storage(struct hv_device *device, int max_chns)
34 struct storvsc_device *stor_device;
35 - int num_cpus = num_online_cpus();
37 struct storvsc_cmd_request *request;
38 struct vstor_packet *vstor_packet;
41 - num_sc = ((max_chns > num_cpus) ? num_cpus : max_chns);
43 + * If the number of CPUs is artificially restricted, such as
44 + * with maxcpus=1 on the kernel boot line, Hyper-V could offer
45 + * sub-channels >= the number of CPUs. These sub-channels
46 + * should not be created. The primary channel is already created
47 + * and assigned to one CPU, so check against # CPUs - 1.
49 + num_sc = min((int)(num_online_cpus() - 1), max_chns);
53 stor_device = get_out_stor_device(device);