[PATCH] drm/i915/gvt: Use ARRAY_SIZE instead of hardcoded size

Joe Perches joe at perches.com
Mon Apr 13 17:55:03 UTC 2020


On Mon, 2020-04-13 at 22:32 +0800, Jason Yan wrote:
> Fix the following coccicheck warning:
> 
> drivers/gpu/drm/i915/gvt/vgpu.c:127:30-31: WARNING: Use ARRAY_SIZE
> 
> Signed-off-by: Jason Yan <yanaijie at huawei.com>
> ---
>  drivers/gpu/drm/i915/gvt/vgpu.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/i915/gvt/vgpu.c b/drivers/gpu/drm/i915/gvt/vgpu.c
> index 1d5ff88078bd..7d361623ff67 100644
> --- a/drivers/gpu/drm/i915/gvt/vgpu.c
> +++ b/drivers/gpu/drm/i915/gvt/vgpu.c
> @@ -124,7 +124,7 @@ int intel_gvt_init_vgpu_types(struct intel_gvt *gvt)
>  	 */
>  	low_avail = gvt_aperture_sz(gvt) - HOST_LOW_GM_SIZE;
>  	high_avail = gvt_hidden_sz(gvt) - HOST_HIGH_GM_SIZE;
> -	num_types = sizeof(vgpu_types) / sizeof(vgpu_types[0]);
> +	num_types = ARRAY_SIZE(vgpu_types);
>  
>  	gvt->types = kcalloc(num_types, sizeof(struct intel_vgpu_type),
>  			     GFP_KERNEL);

It's probably better to remove num_types altogether and just
use ARRAY_SIZE in both places num_types is used.

Perhaps refactoring the function a bit more is also better.

Perhaps:

o Use ARRAY_SIZE
o Make vgpu_types static const to reduce data size and
  move the definition into the function where it's used
o Use temporaries to shorten the code indirections.

---
 drivers/gpu/drm/i915/gvt/vgpu.c | 92 +++++++++++++++++++++--------------------
 1 file changed, 47 insertions(+), 45 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/vgpu.c b/drivers/gpu/drm/i915/gvt/vgpu.c
index 1d5ff8..e56f59d 100644
--- a/drivers/gpu/drm/i915/gvt/vgpu.c
+++ b/drivers/gpu/drm/i915/gvt/vgpu.c
@@ -77,26 +77,6 @@ void populate_pvinfo_page(struct intel_vgpu *vgpu)
 #define VGPU_WEIGHT(vgpu_num)	\
 	(VGPU_MAX_WEIGHT / (vgpu_num))
 
-static struct {
-	unsigned int low_mm;
-	unsigned int high_mm;
-	unsigned int fence;
-
-	/* A vGPU with a weight of 8 will get twice as much GPU as a vGPU
-	 * with a weight of 4 on a contended host, different vGPU type has
-	 * different weight set. Legal weights range from 1 to 16.
-	 */
-	unsigned int weight;
-	enum intel_vgpu_edid edid;
-	char *name;
-} vgpu_types[] = {
-/* Fixed vGPU type table */
-	{ MB_TO_BYTES(64), MB_TO_BYTES(384), 4, VGPU_WEIGHT(8), GVT_EDID_1024_768, "8" },
-	{ MB_TO_BYTES(128), MB_TO_BYTES(512), 4, VGPU_WEIGHT(4), GVT_EDID_1920_1200, "4" },
-	{ MB_TO_BYTES(256), MB_TO_BYTES(1024), 4, VGPU_WEIGHT(2), GVT_EDID_1920_1200, "2" },
-	{ MB_TO_BYTES(512), MB_TO_BYTES(2048), 4, VGPU_WEIGHT(1), GVT_EDID_1920_1200, "1" },
-};
-
 /**
  * intel_gvt_init_vgpu_types - initialize vGPU type list
  * @gvt : GVT device
@@ -106,9 +86,32 @@ static struct {
  */
 int intel_gvt_init_vgpu_types(struct intel_gvt *gvt)
 {
-	unsigned int num_types;
 	unsigned int i, low_avail, high_avail;
 	unsigned int min_low;
+	static const struct vgpu_types {
+		unsigned int low_mm;
+		unsigned int high_mm;
+		unsigned int fence;
+
+		/* A vGPU with a weight of 8 will get twice as much GPU
+		 * as a vGPU with a weight of 4 on a contended host,
+		 * different vGPU type has different weight set.
+		 * Legal weights range from 1 to 16.
+		 */
+		unsigned int weight;
+		enum intel_vgpu_edid edid;
+		char *name;
+	} vgpu_types[] = {
+		/* Fixed vGPU type table */
+		{ MB_TO_BYTES(64), MB_TO_BYTES(384), 4,
+		  VGPU_WEIGHT(8), GVT_EDID_1024_768, "8" },
+		{ MB_TO_BYTES(128), MB_TO_BYTES(512), 4,
+		  VGPU_WEIGHT(4), GVT_EDID_1920_1200, "4" },
+		{ MB_TO_BYTES(256), MB_TO_BYTES(1024), 4,
+		  VGPU_WEIGHT(2), GVT_EDID_1920_1200, "2" },
+		{ MB_TO_BYTES(512), MB_TO_BYTES(2048), 4,
+		  VGPU_WEIGHT(1), GVT_EDID_1920_1200, "1" },
+	};
 
 	/* vGPU type name is defined as GVTg_Vx_y which contains
 	 * physical GPU generation type (e.g V4 as BDW server, V5 as
@@ -124,45 +127,44 @@ int intel_gvt_init_vgpu_types(struct intel_gvt *gvt)
 	 */
 	low_avail = gvt_aperture_sz(gvt) - HOST_LOW_GM_SIZE;
 	high_avail = gvt_hidden_sz(gvt) - HOST_HIGH_GM_SIZE;
-	num_types = sizeof(vgpu_types) / sizeof(vgpu_types[0]);
 
-	gvt->types = kcalloc(num_types, sizeof(struct intel_vgpu_type),
-			     GFP_KERNEL);
+	gvt->types = kcalloc(ARRAY_SIZE(vgpu_types),
+			     sizeof(struct intel_vgpu_type), GFP_KERNEL);
 	if (!gvt->types)
 		return -ENOMEM;
 
 	min_low = MB_TO_BYTES(32);
-	for (i = 0; i < num_types; ++i) {
-		if (low_avail / vgpu_types[i].low_mm == 0)
+	for (i = 0; i < ARRAY_SIZE(vgpu_types); i++) {
+		struct intel_vgpu_type *type = &gvt->types[i];
+		const struct vgpu_types *vgpu = &vgpu_types[i];
+
+		if (low_avail / vgpu->low_mm == 0)
 			break;
 
-		gvt->types[i].low_gm_size = vgpu_types[i].low_mm;
-		gvt->types[i].high_gm_size = vgpu_types[i].high_mm;
-		gvt->types[i].fence = vgpu_types[i].fence;
+		type->low_gm_size = vgpu->low_mm;
+		type->high_gm_size = vgpu->high_mm;
+		type->fence = vgpu->fence;
 
-		if (vgpu_types[i].weight < 1 ||
-					vgpu_types[i].weight > VGPU_MAX_WEIGHT)
+		if (vgpu->weight < 1 || vgpu->weight > VGPU_MAX_WEIGHT)
 			return -EINVAL;
 
-		gvt->types[i].weight = vgpu_types[i].weight;
-		gvt->types[i].resolution = vgpu_types[i].edid;
-		gvt->types[i].avail_instance = min(low_avail / vgpu_types[i].low_mm,
-						   high_avail / vgpu_types[i].high_mm);
+		type->weight = vgpu->weight;
+		type->resolution = vgpu->edid;
+		type->avail_instance = min(low_avail / vgpu->low_mm,
+					   high_avail / vgpu->high_mm);
 
 		if (IS_GEN(gvt->gt->i915, 8))
-			sprintf(gvt->types[i].name, "GVTg_V4_%s",
-				vgpu_types[i].name);
+			sprintf(type->name, "GVTg_V4_%s", vgpu->name);
 		else if (IS_GEN(gvt->gt->i915, 9))
-			sprintf(gvt->types[i].name, "GVTg_V5_%s",
-				vgpu_types[i].name);
+			sprintf(type->name, "GVTg_V5_%s", vgpu->name);
 
 		gvt_dbg_core("type[%d]: %s avail %u low %u high %u fence %u weight %u res %s\n",
-			     i, gvt->types[i].name,
-			     gvt->types[i].avail_instance,
-			     gvt->types[i].low_gm_size,
-			     gvt->types[i].high_gm_size, gvt->types[i].fence,
-			     gvt->types[i].weight,
-			     vgpu_edid_str(gvt->types[i].resolution));
+			     i, type->name,
+			     type->avail_instance,
+			     type->low_gm_size,
+			     type->high_gm_size, type->fence,
+			     type->weight,
+			     vgpu_edid_str(type->resolution));
 	}
 
 	gvt->num_types = i;






More information about the intel-gvt-dev mailing list