Re: [Intel-gfx] [PATCH] drm/i915/gvt: Use ARRAY_SIZE instead of hardcoded size

2020-04-13 Thread Joe Perches
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 
> ---
>  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 = >types[i];
+   const struct vgpu_types 

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

2020-04-13 Thread Jason Yan
Fix the following coccicheck warning:

drivers/gpu/drm/i915/gvt/vgpu.c:127:30-31: WARNING: Use ARRAY_SIZE

Signed-off-by: Jason Yan 
---
 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);
-- 
2.21.1

___
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx