From nobody Thu Nov 28 06:37:24 2024 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A6ABE27735; Thu, 3 Oct 2024 03:54:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727927655; cv=none; b=p/+uHeTP11qdXCjf9H0HrXr6pb0vvmzO3RbBgzG9wb4s6UrK90rLmC4huDhJ4c8umStXZ/H6k/tqxGFiJQpa6okRGfWtCwXHgGWOU0251axkGm0MvDnr8gfi2n3KPviDcUtvUzWazmMEQkCzoxZZl2FpCkZvV5ttBpFz0mN3RY0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727927655; c=relaxed/simple; bh=Ls1umv3AoV9OAW7lcwxqmR9VNmxVlUZafJfEKvveDpc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=de+F3V1dKtp4j2+y64Bn0/r2fTa8K+PuNREgDyim2hE9TKlxQnzsWOthKKVcYuhzFAauJ1UPwxkg3VBjmyfTPO1TcgkEQk+2qk9NmzShXxynKpadYHjvZebvrCV8KueMdFVnXkVEk8rgMkBupb3sWQqi+2euQ9zkDg24qhoGdCA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=l/7j2iFR; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="l/7j2iFR" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-20bc2970df5so3155115ad.3; Wed, 02 Oct 2024 20:54:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727927653; x=1728532453; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=rl3DJj69ip2vtdwvZRZRwJS0N9IcEuELubliNXpPJz0=; b=l/7j2iFR0GgCWKDiZ6tv4ZFk0SvKITW1j9dRrXHsZeq7UKyUeNSKIQ7QKTOEC/odQH K/x87QqiNLZjWfNyabc+E9NxIKWKEFquK42lkewhwPjQTaOVfko30TEYcp046iHyZ+jb yd9YOPjpMPVyYrR5tsv4OKGmgn2c0eAEda1HFMtsupcgtbODB33YaYzlJzey4+wZJh4p UnrgsjJRHmEl+DOocL/LhIZrMZoIIKO3AeMC/KKmOO76dnaFLYsfPrVsuOQgFVc35Etq ZI3TCP5MYAKTy4AziwKQiSmW0PYZMwuL3vojfv0yMiU6NJOpN3Yy4Ey+q04z28xU1bwZ 7G3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727927653; x=1728532453; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=rl3DJj69ip2vtdwvZRZRwJS0N9IcEuELubliNXpPJz0=; b=aJnurEq3ES6zmuXRa3kgVxtA4Arwm35wRdkUuclr5Q7UwF5Lh6c3zwSJlBJcV/Ylo5 hvupTtp4OwUg/LY2EJhO7xY9TNmFFOQ4IIxFM97dAlvIiQvNkOOTLB0iu+1BJbLfe/HX rl92kJsw7I7YhPlzJGkkFRXAWVv5UubSPI5S68CndJsP9vEWFyx7PaOB9BVji1sd6k+d YKdt4JRF+n5SSZReS58sIFv8f2zV5yvQNCzUKp9AW4tnZYHh+WCfhsmcIBzVldFFDITE 2e08PcdH8Nrk0k8f/uXUu6hGX/WmRYTFHr2ksBgtP414AC4PSfpBCE4v1yjr1V2JKxrc 9wQw== X-Forwarded-Encrypted: i=1; AJvYcCV+yTtDhDnlMf2H4fwYfg4xclPfryPCwOWGLnc09+9y3yD+OAvf53468q0M/2w55+0teTH/8V1Y3MVTdg==@vger.kernel.org, AJvYcCVXnOVgsHeUpTcp3MJ1SOg0QiM+492wN+iZpzUbwVLqM9R3Tt5dKpyJHom/bKL/4StY/iIReSzt1zAnDjmW@vger.kernel.org, AJvYcCXH55SsJaZQAZ3flEieV4qeVxUyWsNPT2lPOWyhFVHLD1FR8McUbT4VIVSyZ8fLy+4D50+Ln6/a@vger.kernel.org, AJvYcCXgJA84ecKga8Mi1mp5O/sYrjfVefKyaS3LdqWNI0qrYvC31g/vdWKVLVC8vTnTEP5lfWnYCiU+a+pxqfc=@vger.kernel.org X-Gm-Message-State: AOJu0Yya6zBCVaWqjdrrLmALATWcy75r8uuGKaXKJXJLAwl4LSM8Rh9F mgOrUkhIO5SNmdKElrBtdt1GNz4esXUw1QmHW4atRnRdoB2/hIEw X-Google-Smtp-Source: AGHT+IHU9fSIrpIZr2B/Cle3Jn+UyUSHLNjXtriJssD67baJWW1ZC7PtSkc/R6T/Z0ZRvy1RrGfrSw== X-Received: by 2002:a17:903:944:b0:207:6e8:1d84 with SMTP id d9443c01a7336-20bc5a4e9d2mr68426335ad.42.1727927652878; Wed, 02 Oct 2024 20:54:12 -0700 (PDT) Received: from localhost.localdomain (c-67-160-120-253.hsd1.wa.comcast.net. [67.160.120.253]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20beead8dc2sm906115ad.44.2024.10.02.20.54.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 20:54:12 -0700 (PDT) From: mhkelley58@gmail.com X-Google-Original-From: mhklinux@outlook.com To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, James.Bottomley@HansenPartnership.com, martin.petersen@oracle.com Cc: iommu@lists.linux.dev, netdev@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org Subject: [PATCH 1/5] x86/hyperv: Don't assume cpu_possible_mask is dense Date: Wed, 2 Oct 2024 20:53:29 -0700 Message-Id: <20241003035333.49261-2-mhklinux@outlook.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241003035333.49261-1-mhklinux@outlook.com> References: <20241003035333.49261-1-mhklinux@outlook.com> Reply-To: mhklinux@outlook.com Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Michael Kelley Current code allocates the hv_vp_assist_page array with size num_possible_cpus(). This code assumes cpu_possible_mask is dense, which is not true in the general case per [1]. If cpu_possible_mask is sparse, the array might be indexed by a value beyond the size of the array. However, the configurations that Hyper-V provides to guest VMs on x86 hardware, in combination with how x86 code assigns Linux CPU numbers, *does* always produce a dense cpu_possible_mask. So the dense assumption is not currently causing failures. But for robustness against future changes in how cpu_possible_mask is populated, update the code to no longer assume dense. The correct approach is to allocate the array with size "nr_cpu_ids". While this leaves unused array entries corresponding to holes in cpu_possible_mask, the holes are assumed to be minimal and hence the amount of memory wasted by unused entries is minimal. [1] https://lore.kernel.org/lkml/SN6PR02MB4157210CC36B2593F8572E5ED4692@SN6= PR02MB4157.namprd02.prod.outlook.com/ Signed-off-by: Michael Kelley Acked-by: Peter Zijlstra (Intel) --- arch/x86/hyperv/hv_init.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index 95eada2994e1..2cec4dfec165 100644 --- a/arch/x86/hyperv/hv_init.c +++ b/arch/x86/hyperv/hv_init.c @@ -473,7 +473,7 @@ void __init hyperv_init(void) if (hv_isolation_type_tdx()) hv_vp_assist_page =3D NULL; else - hv_vp_assist_page =3D kcalloc(num_possible_cpus(), + hv_vp_assist_page =3D kcalloc(nr_cpu_ids, sizeof(*hv_vp_assist_page), GFP_KERNEL); if (!hv_vp_assist_page) { --=20 2.25.1 From nobody Thu Nov 28 06:37:24 2024 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 00C3B3FE55; Thu, 3 Oct 2024 03:54:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727927656; cv=none; b=avgnYnDFUWLrD5bSAFb6rCYaO6NgEJcDcGNyXoOsC4HXbpfFaMfrHdJxdurILVryaDawl/HHpEdoQi0jdPtE4CksBcAme03FBjzvh/M1zeIsZVXs2Fcrz331gcDp/iuAu+883xHAHVW5bzmqvgk2vS1Jvdg8VxvpJNPLOdfNF90= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727927656; c=relaxed/simple; bh=pneaunq42UDCs3Bs1TW/8ZciN54md0YfS4FpuQiIs44=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=b6lUd3SoLsbvsh7c7IGpHC5o9vl+A2BPRNRDdP9LkfJbGYPlj8qZEpBdkkUTpxWr4XiL07C1kBkE5S93uz7U1EBsXZfgM/Xry+rMMjqFjK23QIQQ8JCSWFg2v4w3fqgxTiToM+9G+VmjzKHDfcow1LqA+BaQPIuipL7WQVUGRfY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HN7o4/xB; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HN7o4/xB" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-20bc506347dso3436475ad.0; Wed, 02 Oct 2024 20:54:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727927654; x=1728532454; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=cq0XjbGrpMlRn53fkSkj2ZE6QfO3xJ5aVKsySHlaSk8=; b=HN7o4/xBuVpKA/hifZxREWgf7BcmhAJgVp0CEOoBTxoFam8S39rXKbFojJveVPjIKS pNTDiCI0lIQXtq/EvE0361IZnm6n1EmfH/6mh7g8LxzJEwoYDb8K1BYb31u33Bbvbryu LvsMZeyFABIH4EM3y5YTeW9AibI2LlZxL+DwaTEK5J1nJGn+UZSv7xtrxxyIEVZWj4o0 UZqLJls16pBErxAMPMxuoEWepMDrYOiF6mH4yyJAemuh2IDpCsRSYU+UjFO7jbNwL6g8 825JKEbIpcGy7s3hlB2HdMDa0bS9N1g2gPVqp5EBj7q/0C/Q82sfT6HAbzlrixok9coW 014A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727927654; x=1728532454; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=cq0XjbGrpMlRn53fkSkj2ZE6QfO3xJ5aVKsySHlaSk8=; b=hVgr3c38qcRwfTQq2Dnju89ceDMi+rFkNpEb7GUJBoOxnFf39GmLOWFJXrSTlkwSyq RPEQMJ7hqAS4S9Se4qabmTG2t988ZkTuJ5L3jP37sIybknl0oi1mXjHlTqkvAD/XqDSQ OWRrT7/BLON7xLMqO5P126vI4b9/+wzOZIee+VlnQtZJa224inMjb20UhgXxM0+s2NZG cznL4JJQ7hG5+XQewJP0r4tYTTGv7wWVFN6qj4dULyRLN01XkbxOOgQNE2k86wzAqxSC XRVIBpus5TfWuQO6/HfC+rmz9riqXAT2LNny1KV7tx2ru8Lj/TIwZNukVit9aVPR3yrY 6qgw== X-Forwarded-Encrypted: i=1; AJvYcCUCcWThjTBz7vRLY8mNgJsxOCuok+nd6RG7EDV1e8luRaWw60qVJ2ufv+l430CfKCWP7N14Xlzzi/kJgKg=@vger.kernel.org, AJvYcCULUG9q4oUfOjywZW9RLRbmMIpODc3KFLaGDjQ3u0TTT2W2/ylse30gZijfAYOSHQ30V0cWsFiNUmQFCzW+@vger.kernel.org, AJvYcCUexGazJApFmGc00jX8wIEmZPa+UjVeQE90vJCHnlo9/hqJOo5BFb79Sot6uX49c6x8JnabpI7LJqZ/YQ==@vger.kernel.org, AJvYcCVWfN4GuT4tlf9XgxdWk8T/Xwa5sdpW46RkiJ9cG4ESQEwTOFPcEK8ZkFvr2rGYRdr3HcqXVT2S@vger.kernel.org X-Gm-Message-State: AOJu0Ywys/oNzDfs1EELo2KsXGS4BPTJmg9eThlFnRaoHqSN+fnMLtMx KRu3aY241Z4ksAfTU35EI/k0tyA2IkP1PuKpOU/hCb2ygDPi99DU X-Google-Smtp-Source: AGHT+IHQarg1wLyg3MVulXjHU7XNbDYC9jQJBTn5bjTS6CS6tyLfUuDFFuLBjAX11JvWqCMk5JvUAg== X-Received: by 2002:a17:902:d4cd:b0:20b:c264:fde8 with SMTP id d9443c01a7336-20bc59f9fcfmr79593655ad.22.1727927654255; Wed, 02 Oct 2024 20:54:14 -0700 (PDT) Received: from localhost.localdomain (c-67-160-120-253.hsd1.wa.comcast.net. [67.160.120.253]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20beead8dc2sm906115ad.44.2024.10.02.20.54.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 20:54:13 -0700 (PDT) From: mhkelley58@gmail.com X-Google-Original-From: mhklinux@outlook.com To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, James.Bottomley@HansenPartnership.com, martin.petersen@oracle.com Cc: iommu@lists.linux.dev, netdev@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org Subject: [PATCH 2/5] Drivers: hv: Don't assume cpu_possible_mask is dense Date: Wed, 2 Oct 2024 20:53:30 -0700 Message-Id: <20241003035333.49261-3-mhklinux@outlook.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241003035333.49261-1-mhklinux@outlook.com> References: <20241003035333.49261-1-mhklinux@outlook.com> Reply-To: mhklinux@outlook.com Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Michael Kelley Current code allocates the hv_vp_index array with size num_possible_cpus(). This code assumes cpu_possible_mask is dense, which is not true in the general case per [1]. If cpu_possible_mask is sparse, the array might be indexed by a value beyond the size of the array. However, the configurations that Hyper-V provides to guest VMs on x86 and ARM64 hardware, in combination with how architecture specific code assigns Linux CPU numbers, *does* always produce a dense cpu_possible_mask. So the dense assumption is not currently causing failures. But for robustness against future changes in how cpu_possible_mask is populated, update the code to no longer assume dense. The correct approach is to allocate and initialize the array using size "nr_cpu_ids". While this leaves unused array entries corresponding to holes in cpu_possible_mask, the holes are assumed to be minimal and hence the amount of memory wasted by unused entries is minimal. Using nr_cpu_ids also reduces initialization time, in that the loop to initialize the array currently rescans cpu_possible_mask on each iteration. This is n-squared in the number of CPUs, which could be significant for large CPU counts. [1] https://lore.kernel.org/lkml/SN6PR02MB4157210CC36B2593F8572E5ED4692@SN6= PR02MB4157.namprd02.prod.outlook.com/ Signed-off-by: Michael Kelley Acked-by: Peter Zijlstra (Intel) --- drivers/hv/hv_common.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/hv/hv_common.c b/drivers/hv/hv_common.c index d50caf0d723d..8c44938cb084 100644 --- a/drivers/hv/hv_common.c +++ b/drivers/hv/hv_common.c @@ -345,14 +345,14 @@ int __init hv_common_init(void) BUG_ON(!hyperv_pcpu_output_arg); } =20 - hv_vp_index =3D kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index), + hv_vp_index =3D kmalloc_array(nr_cpu_ids, sizeof(*hv_vp_index), GFP_KERNEL); if (!hv_vp_index) { hv_common_free(); return -ENOMEM; } =20 - for (i =3D 0; i < num_possible_cpus(); i++) + for (i =3D 0; i < nr_cpu_ids; i++) hv_vp_index[i] =3D VP_INVAL; =20 return 0; --=20 2.25.1 From nobody Thu Nov 28 06:37:24 2024 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 52D86537F8; Thu, 3 Oct 2024 03:54:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727927657; cv=none; b=NcgKf+IJ1H9R4TsKdPMLIrxijxjDjh3f9hEAQBwKl/Tmt1oP1SmgsYvMdJptfvS+Pgp3mEwSaA4rTJz6XqGUSg7zacSDT2Yz1wUXpnL6l5DICbJQLmlogAQlF6L9L4ckHL4zNdSAqAQH2+8RD7IJ/RVbafGLPLIxDYIz38fJXvc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727927657; c=relaxed/simple; bh=mHoaQT1JoyG6LkyzWkFbXUPdVaWzkJTm5hCach2QWbM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TxVLtz9B0IHJp3B0j9epa0c6W9EtOQ0sMagg0RVndyqczLf6FzCbZleOdAC4C2pykkmnFIyIQWXiEZNqz4w6ALVCchbXKJnCQc7i3XQqg78FHD316aig9nxKBzUbr4a7CQufwviTfpj/yAXjVdoxmL4A8wjUDcYGp57oT0pGL28= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hk+PR3RD; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hk+PR3RD" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-20ba6b39a78so3016695ad.3; Wed, 02 Oct 2024 20:54:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727927655; x=1728532455; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=tDuFxybHODEaeVrwvh+oodf/QVAaDeDVDDTRMXteliU=; b=hk+PR3RDdJxOiqwo2fJLWdB8qDPiHpmfoy+oJJnQoc3qFUTP/w4iwXI6XHddMNDYgu dpTmIHOyEo/iWJooCaUcbyAUeKeLL3CLwobxSVIZn/9G+yO2lxE9dups8UmHoci/8baC tZ62+ffRbBN4wL5Ij/nKxPH39Vn6SaYn17uwLsHXv+jSfXKHB1sh4/Bm9FAi0F71ajLR 3XIDW2m5QdlHZupiwRzXd+NNkA9wn4ZOscPCZsAeIYRTlT61guGmmCSuC1d2NG6qy8G9 IrjvrMCOYuiEYcHKXOSvv3wSrkGilIkPgjXRbzzCvSWjlM+OJzIZ416OPUMyzQDncOqM TzaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727927655; x=1728532455; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=tDuFxybHODEaeVrwvh+oodf/QVAaDeDVDDTRMXteliU=; b=sHM8qs6ZBbf+kzBLeEdrobG1QlN2RBCL9p+ZU9OmdNwkc12FQALnZyqyyqKNZ82Gh1 tyhfZAmpK9kVMXgOjAKDQRc+RLYhb+Kcw8VSad8grRgRIv93WhKMO8pXNyGVo1SKu3LG PK0Hy2NA4CRkAMjA+2NYqQplibxYjpxZWg1VJ0b9Ux1DtjzWtdv2PZ01yENwFvy9dmF3 L+78rzjrZNDk7buFzJI4RuAXhnrVBx94fvpwtdw3+HIo3Rkd/sXwleBT17zGEbmMFasF sGwy2M8tL3cP/d/UgkkSbxGToCN9xZrMpqlACSSICowQCv74iqFMt37me1hMnUYnfXeD blEw== X-Forwarded-Encrypted: i=1; AJvYcCW/V4goKU2joEQ0H4ZRvMEssmPj2zVV5gLbC895qP11E1Dllu51POaaroL2h9IogwpPAVNZgHYg@vger.kernel.org, AJvYcCX/L5uiqGTEmxDiv2cgu+SsiaYdVNhZwcx2NmUCWoHDqxPcvTiK1osgBm/S38q72uvPDfHN+PD6Ktvb+dw=@vger.kernel.org, AJvYcCXmagrxil7bE/HWovt/4vK2TAp2kcaN8YNaQRDJ+mRMHxH7ncyQTm95vLuoSL1LrY3lzr4TnwcZsjIqlbQb@vger.kernel.org, AJvYcCXp0UHnpTqFZPBZb6rFAvuWNGoNvZygtsQ+h9ZD+llv30/rS2nk7Ysg3o3AEYlBDclQRHdG9SookKkY3Q==@vger.kernel.org X-Gm-Message-State: AOJu0YxAmob5gej3qWrUSnabFSety0cLRqytGq/sv1QubshNaXZKRaFK aIk6CCXl5Kcsd8GY4wt4aTyBTD7Zz5ZFgfiyUpeSOfJN3+xNctLN X-Google-Smtp-Source: AGHT+IFKLPlcSp9SUFRml4GIrUDRe7i+pHx0/lLBWbzLd4R0zTcKVcErbP/p5ZwNke1aAWlNDTBqqg== X-Received: by 2002:a17:903:c5:b0:20b:a431:8f2f with SMTP id d9443c01a7336-20bc5a701bbmr52713415ad.55.1727927655583; Wed, 02 Oct 2024 20:54:15 -0700 (PDT) Received: from localhost.localdomain (c-67-160-120-253.hsd1.wa.comcast.net. [67.160.120.253]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20beead8dc2sm906115ad.44.2024.10.02.20.54.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 20:54:15 -0700 (PDT) From: mhkelley58@gmail.com X-Google-Original-From: mhklinux@outlook.com To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, James.Bottomley@HansenPartnership.com, martin.petersen@oracle.com Cc: iommu@lists.linux.dev, netdev@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org Subject: [PATCH 3/5] iommu/hyper-v: Don't assume cpu_possible_mask is dense Date: Wed, 2 Oct 2024 20:53:31 -0700 Message-Id: <20241003035333.49261-4-mhklinux@outlook.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241003035333.49261-1-mhklinux@outlook.com> References: <20241003035333.49261-1-mhklinux@outlook.com> Reply-To: mhklinux@outlook.com Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Michael Kelley Current code gets the APIC IDs for CPUs numbered 255 and lower. This code assumes cpu_possible_mask is dense, which is not true in the general case per [1]. If cpu_possible_mask contains holes, num_possible_cpus() is less than nr_cpu_ids, so some CPUs might get skipped. Furthermore, getting the APIC ID of a CPU that isn't in cpu_possible_mask is invalid. However, the configurations that Hyper-V provides to guest VMs on x86 hardware, in combination with how x86 code assigns Linux CPU numbers, *does* always produce a dense cpu_possible_mask. So the dense assumption is not currently causing failures. But for robustness against future changes in how cpu_possible_mask is populated, update the code to no longer assume dense. The correct approach is to determine the range to scan based on nr_cpu_ids, and skip any CPUs that are not in the cpu_possible_mask. [1] https://lore.kernel.org/lkml/SN6PR02MB4157210CC36B2593F8572E5ED4692@SN6= PR02MB4157.namprd02.prod.outlook.com/ Signed-off-by: Michael Kelley Acked-by: Peter Zijlstra (Intel) --- drivers/iommu/hyperv-iommu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/hyperv-iommu.c b/drivers/iommu/hyperv-iommu.c index 8a5c17b97310..2a86aa5d54c6 100644 --- a/drivers/iommu/hyperv-iommu.c +++ b/drivers/iommu/hyperv-iommu.c @@ -164,8 +164,8 @@ static int __init hyperv_prepare_irq_remapping(void) * max cpu affinity for IOAPIC irqs. Scan cpu 0-255 and set cpu * into ioapic_max_cpumask if its APIC ID is less than 256. */ - for (i =3D min_t(unsigned int, num_possible_cpus() - 1, 255); i >=3D 0; i= --) - if (cpu_physical_id(i) < 256) + for (i =3D min_t(unsigned int, nr_cpu_ids - 1, 255); i >=3D 0; i--) + if (cpu_possible(i) && cpu_physical_id(i) < 256) cpumask_set_cpu(i, &ioapic_max_cpumask); =20 return 0; --=20 2.25.1 From nobody Thu Nov 28 06:37:24 2024 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7B407E563; Thu, 3 Oct 2024 03:54:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727927659; cv=none; b=D6Gg/546MP7WZ/uOG4zI0eyjGGfRrG9OYiOl9hDFgQDFy7fjM56r8YAb17yEkIfe1dmln4mK9ng6n4gnDHJ1wbld+Je6jeHBGYjxjVGofbAvmBF4mbljc4dfkpDKDRRu9Pl50qJGyTwoPbSaJIaJKzKMxjUt0kf9pI4xS2DaG0w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727927659; c=relaxed/simple; bh=x6Zn7HjTzGMYc9FJ3+apAp57nj/OHxiWOSQMFh0tXkM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Tz5xhfQdxw25OIO9pai9tBrngx8qr3owHjxdKoSP/J2u8ATZmCSGbU6qjUqS8PpSW4JiZFVXIIflrrsXaz35Im7E1VtRbHf+K4cRBUHlTgSSdXt4E1lhlmcYsKZTQ37o3Hs2Cqpo9+fpuOa3htqtgBTFCL9NqbOElZAsJRYcnNg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Vye9dtoi; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Vye9dtoi" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-20b5fb2e89dso2859805ad.1; Wed, 02 Oct 2024 20:54:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727927657; x=1728532457; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=DIVjY9QXcz2STFKs3TT5rFa2kP9kKUnHMheJi7enQS4=; b=Vye9dtoiPnko278xZMa47KkEsn74dxqL2J5FOVAMdOl+LOrbKaovnhx8ABLCg1fXWv GE5ewhb7ghelL0FKhrGDOk124/Oy83y4VlnJ3bKjsWxmVFlR2NUKS0KHtzxvple2fOEr 8xXL9PEaio8tjAWXD4GgwmXoFgUOedNZ19kFlTTxZe7c8NqA/7CvIfc2Z8YJk0YcnPk5 JJsQJkgxAmKqjOdZkiiFWzkh8eyVaOdKiPPQh7AK1dxqy+Dg2wKZd5MIu7KOp8/XvvPC 3n6kOCmhjH1eZ/M/QTjWclPJOBPDD7nh92HIMa67Y88okTBnOkJlebeMe2cQmXvsnzmO sYYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727927657; x=1728532457; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=DIVjY9QXcz2STFKs3TT5rFa2kP9kKUnHMheJi7enQS4=; b=jJq4D7KpNgGCh+ygsU5u4270F8UaCmyjqY+vB2lC9y5WtvaIn8lR1pPo1CceLZ6xyG kyQXICEKBhgHWgEXJoY7Lp9BzFboiuMQtu1HrlaTeXEKMdxM1QAMJbWNn2hN6vO5Txii FxWMFfedkfUx/3mGT3mfIk8R/8eXHMxcHv2MjGpMCi/jga1wiPugQ73VjWxwcXPYrD3b g58J0ULsqHWTmm9xRRBqlsfcEYy5kV5DHLhdJAvzXCgnL1SbgBogf2MvSedRS9zh5HGi E3TftkcqmdyLllYmc+mw4y1AMqrKhRK9pMaiNBQ0GN2IH1Smo1EzmxlLlBmYtx7eJcqu kBAw== X-Forwarded-Encrypted: i=1; AJvYcCUBiABG0SF7EsbQxAHXx98u4u3njeyqfV9hoYbvnV/GmPZaXXG/JS8qPDVMX78ZZfR7uV6MZeoj@vger.kernel.org, AJvYcCVImcCHHsSKBcS2b4q5VJpg4xEUFrslr2cRTuyL/AXlDfJZk0J1hQDGaA8EmjDza6PmwgR2CRZDUfdS2Q==@vger.kernel.org, AJvYcCW3zOBGNiChj+y1LAwAUJM8XI4pLmkfRtiaoTO72XzjK2OeoYM7mQ/KQ6XfUBb8eI2stVSbJd6kd4b4iFU=@vger.kernel.org, AJvYcCXSxf9GfllJiiqLNSEr8l8/o7K6CAqck/VQeC+TZgFCyHC86BAX+vhMTA5cGzwD98VACBtcZlbtASvO5t5+@vger.kernel.org X-Gm-Message-State: AOJu0Yx9eM1LGS5GpE2Tu20B31bA6M4LnWGDv/WgvKfabxicmhkEWnpm SJHXRhdTuF+y/zbL4kabgTXe3Cv6M+FqQiP1HAU0IhMBt5vke+5J X-Google-Smtp-Source: AGHT+IGYAsSvN1ppKu/WfTnxiRO2Z7TJtHRQQ6nzYxEJteJVC5HlpEBwNUkwvfBEkQqK2Sk6/i2hLw== X-Received: by 2002:a17:902:d2c5:b0:206:ba20:dd40 with SMTP id d9443c01a7336-20bc59f4da9mr80367265ad.27.1727927656951; Wed, 02 Oct 2024 20:54:16 -0700 (PDT) Received: from localhost.localdomain (c-67-160-120-253.hsd1.wa.comcast.net. [67.160.120.253]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20beead8dc2sm906115ad.44.2024.10.02.20.54.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 20:54:16 -0700 (PDT) From: mhkelley58@gmail.com X-Google-Original-From: mhklinux@outlook.com To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, James.Bottomley@HansenPartnership.com, martin.petersen@oracle.com Cc: iommu@lists.linux.dev, netdev@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org Subject: [PATCH 4/5] scsi: storvsc: Don't assume cpu_possible_mask is dense Date: Wed, 2 Oct 2024 20:53:32 -0700 Message-Id: <20241003035333.49261-5-mhklinux@outlook.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241003035333.49261-1-mhklinux@outlook.com> References: <20241003035333.49261-1-mhklinux@outlook.com> Reply-To: mhklinux@outlook.com Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Michael Kelley Current code allocates the stor_chns array with size num_possible_cpus(). This code assumes cpu_possible_mask is dense, which is not true in the general case per [1]. If cpu_possible_mask is sparse, the array might be indexed by a value beyond the size of the array. However, the configurations that Hyper-V provides to guest VMs on x86 and ARM64 hardware, in combination with how architecture specific code assigns Linux CPU numbers, *does* always produce a dense cpu_possible_mask. So the dense assumption is not currently causing failures. But for robustness against future changes in how cpu_possible_mask is populated, update the code to no longer assume dense. The correct approach is to allocate and initialize the array using size "nr_cpu_ids". While this leaves unused array entries corresponding to holes in cpu_possible_mask, the holes are assumed to be minimal and hence the amount of memory wasted by unused entries is minimal. [1] https://lore.kernel.org/lkml/SN6PR02MB4157210CC36B2593F8572E5ED4692@SN6= PR02MB4157.namprd02.prod.outlook.com/ Signed-off-by: Michael Kelley Acked-by: Peter Zijlstra (Intel) --- drivers/scsi/storvsc_drv.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/drivers/scsi/storvsc_drv.c b/drivers/scsi/storvsc_drv.c index 11b3fc3b24c9..f2beb6b23284 100644 --- a/drivers/scsi/storvsc_drv.c +++ b/drivers/scsi/storvsc_drv.c @@ -917,14 +917,13 @@ static int storvsc_channel_init(struct hv_device *dev= ice, bool is_fc) =20 /* * Allocate state to manage the sub-channels. - * We allocate an array based on the numbers of possible CPUs - * (Hyper-V does not support cpu online/offline). - * This Array will be sparseley populated with unique - * channels - primary + sub-channels. - * We will however populate all the slots to evenly distribute - * the load. + * We allocate an array based on the number of CPU ids. This array + * is initially sparsely populated for the CPUs assigned to channels: + * primary + sub-channels. As I/Os are initiated by different CPUs, + * the slots for all online CPUs are populated to evenly distribute + * the load across all channels. */ - stor_device->stor_chns =3D kcalloc(num_possible_cpus(), sizeof(void *), + stor_device->stor_chns =3D kcalloc(nr_cpu_ids, sizeof(void *), GFP_KERNEL); if (stor_device->stor_chns =3D=3D NULL) return -ENOMEM; --=20 2.25.1 From nobody Thu Nov 28 06:37:24 2024 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 217E083A14; Thu, 3 Oct 2024 03:54:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727927660; cv=none; b=a53IIR8cMcpQc74JVlbViejMnAJJ7FNf2L7tk38hLo36Raes+0ijMLWIjKAnbcUi0FuYpGO3zOgYqEzKrA51g+yGgKZOzJ799wLUH1Z2jx6zqLi0eEWXWZJpb9gSQCFodB8JP77ipSSdKrd+Nh2DkV67JEZL7odKgav6u8772HQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727927660; c=relaxed/simple; bh=mDj2uu52EkY79d3Z3PI3h5sVX5EsQJI7OPSTCaXIcWY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k4ZkjDjwJN82Bi8yrcDxgC0PqSuh3dTnMJ/W7SudLh6fuzp0vol7OtnToI5jMbNIzG8KJDecQLB3IGm336slDj3uGL624lUlTLIgg77dLmP16ddDEQzljS045alOus3Yjsp2txNLi8TJQ5IXSL9+8i0OnoyuXoHlAul5ELrXhEk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FpmmQlYR; arc=none smtp.client-ip=209.85.214.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FpmmQlYR" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-20bc506347dso3436785ad.0; Wed, 02 Oct 2024 20:54:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727927658; x=1728532458; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=GSs10GU2u5qMkicjk65IeToN9GtR5XLc5mZWEtwh55s=; b=FpmmQlYR+XLwuCNxXtx8dKMd/Pr9xJh0QzRMOQwtBolSBCKFn0bAIBv0WddDZV9jtS v4n0d6Ey8L+hCOr7H+gz3Cp8QWdMidiPHUypbI6zFEid4hPdnAGckjEVVAlBDZCWHlp+ z354EL7I0IFyphbKOkCCSzqgBw8Gw2ILS4zZJfjERzHqObSraChVA4ZSGLNUVmifoZQv pYLHA6e6XjGxBqwufXbxYwMTw/klAmnYxGRA89/2n/JThA3kG1wagn8dEs6ZfbFBHI1T Nh3xsCYA6GJEu8mfjzA8lX2gEDk0tDHCnBJd1aZDr9onwLaLH6yeCg8ID+jbahW3sajq dY1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727927658; x=1728532458; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=GSs10GU2u5qMkicjk65IeToN9GtR5XLc5mZWEtwh55s=; b=hxcd4pq6TNs7MNGsuXdoMw1I0m2lR/cB5L2RxYUj5PvBCWdftxCHSKRcQIsfNh1hX9 yGyYi7eRLGZ7loqiRgykNuQnizuqzCDCO5MYaiTPD5t1GTkv9gn6Hdw3hWGQdOMFcfHC oG0wL5R646xi0MPBwiwcpHcYwZRRg2YSsZTn8NS5R8v6OhvYGXH7E1Gq4zqG1CzO+hoO iEJhItBK4omb9CFobeCxKSHri8oI74GqGrBiSoJhJBXloTs9ieWjcESj42a5PIsAJdxM WonaYKPeMx8mssqvzv9USB3whGe0BJlO6ae/0HE/HVmwf1I8rpOZZ2cZgE0jcJ74kixi Az7w== X-Forwarded-Encrypted: i=1; AJvYcCU6drYiDz7K843PuDkE9On8tXB2GRSDQNnioXAONLY+tKwac7DU2Va0BAwzeu4E7tChYYCrpzNh+U3nMA==@vger.kernel.org, AJvYcCW9REvtG11n/CHMoYkkXoAlw7zSBAx0c44rGdzVgS6r7NtOt4ER4CYyOrC4TQdzwXnATijGCQsfdRAI6I4=@vger.kernel.org, AJvYcCWgcuS7CfFnAK9tFY1Hzv/zCb9fWZJNWwcd4CmI81Y2BRNg7bvo1ZB7tO8xNBVAO74yMuX+LKwLYI79LbjX@vger.kernel.org, AJvYcCXcyUQW5MowdD0AT4BqL9FrOCgTi5PGLmh0hoorTK7lJMkp2036MZ9lh19ZW/cIQ64z2tAapbTp@vger.kernel.org X-Gm-Message-State: AOJu0YxVKe9TrSUPoED/vEDuGD2BG2F9jOmNJtYuyr0hWeqFWMw9GckK Kajxde4gfpQ1t9g95IxylcJV8pKDbtAOjeGy6B6T3qKdhho47Dhs X-Google-Smtp-Source: AGHT+IH5HR+8rOf74TLiMWc+piVxUVmSj2sS59cu/Eu9BismoJQet5IB5QGdpPAF2AYaHOgyIyKs/w== X-Received: by 2002:a17:902:d511:b0:20b:84cc:592e with SMTP id d9443c01a7336-20bc5a18d52mr85497045ad.31.1727927658496; Wed, 02 Oct 2024 20:54:18 -0700 (PDT) Received: from localhost.localdomain (c-67-160-120-253.hsd1.wa.comcast.net. [67.160.120.253]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20beead8dc2sm906115ad.44.2024.10.02.20.54.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 20:54:18 -0700 (PDT) From: mhkelley58@gmail.com X-Google-Original-From: mhklinux@outlook.com To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, James.Bottomley@HansenPartnership.com, martin.petersen@oracle.com Cc: iommu@lists.linux.dev, netdev@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org Subject: [PATCH net-next 5/5] hv_netvsc: Don't assume cpu_possible_mask is dense Date: Wed, 2 Oct 2024 20:53:33 -0700 Message-Id: <20241003035333.49261-6-mhklinux@outlook.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241003035333.49261-1-mhklinux@outlook.com> References: <20241003035333.49261-1-mhklinux@outlook.com> Reply-To: mhklinux@outlook.com Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Michael Kelley Current code allocates the pcpu_sum array with size num_possible_cpus(). This code assumes the cpu_possible_mask is dense, which is not true in the general case per [1]. If cpu_possible_mask is sparse, the array might be indexed by a value beyond the size of the array. However, the configurations that Hyper-V provides to guest VMs on x86 and ARM64 hardware, in combination with how architecture specific code assigns Linux CPU numbers, *does* always produce a dense cpu_possible_mask. So the dense assumption is not currently causing failures. But for robustness against future changes in how cpu_possible_mask is populated, update the code to no longer assume dense. The correct approach is to allocate and initialize the array using size "nr_cpu_ids". While this leaves unused array entries corresponding to holes in cpu_possible_mask, the holes are assumed to be minimal and hence the amount of memory wasted by unused entries is minimal. [1] https://lore.kernel.org/lkml/SN6PR02MB4157210CC36B2593F8572E5ED4692@SN6= PR02MB4157.namprd02.prod.outlook.com/ Signed-off-by: Michael Kelley Acked-by: Peter Zijlstra (Intel) --- drivers/net/hyperv/netvsc_drv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/net/hyperv/netvsc_drv.c b/drivers/net/hyperv/netvsc_dr= v.c index 153b97f8ec0d..f8e2dd6d271d 100644 --- a/drivers/net/hyperv/netvsc_drv.c +++ b/drivers/net/hyperv/netvsc_drv.c @@ -1557,7 +1557,7 @@ static void netvsc_get_ethtool_stats(struct net_devic= e *dev, data[i++] =3D xdp_tx; } =20 - pcpu_sum =3D kvmalloc_array(num_possible_cpus(), + pcpu_sum =3D kvmalloc_array(nr_cpu_ids, sizeof(struct netvsc_ethtool_pcpu_stats), GFP_KERNEL); if (!pcpu_sum) --=20 2.25.1