All of lore.kernel.org
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: rajan.vaja@gmail.com, zhenyuw@linux.intel.com,
	zhi.a.wang@intel.com, jani.nikula@linux.intel.com,
	joonas.lahtinen@linux.intel.com, rodrigo.vivi@intel.com,
	airlied@linux.ie
Cc: intel-gvt-dev@lists.freedesktop.org,
	dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] drm/i915/gvt: Use ARRAY_SIZE macro
Date: Mon, 18 Jun 2018 09:20:32 -0700	[thread overview]
Message-ID: <1d81ec0834a57d4a864ed05611614192ba5eb3d6.camel@perches.com> (raw)
In-Reply-To: <1529308106-30716-1-git-send-email-rajan.vaja@softnautics.com>

On Mon, 2018-06-18 at 13:18 +0530, rajan.vaja@gmail.com wrote:
> Use ARRAY_SIZE instead of dividing sizeof array with sizeof
> an element. This fixes below warning reported by Coccinelle:
> drivers/gpu/drm//i915/gvt/vgpu.c:122:30-31: WARNING: Use ARRAY_SIZE
[]
> diff --git a/drivers/gpu/drm/i915/gvt/vgpu.c b/drivers/gpu/drm/i915/gvt/vgpu.c
[]
> @@ -119,7 +119,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 = kzalloc(num_types * sizeof(struct intel_vgpu_type),
>  			     GFP_KERNEL);

It seems you are not using -next as this alloc has
already been changed to kcalloc.

It'd be better to get rid of num_types altogether and
use ARRAY_SIZE everywhere instead.

There are also string overflow possibilities in the
function where sprintf should probably use snprintf.

Perhaps:
'
int intel_gvt_init_vgpu_types(struct intel_gvt *gvt)
{
	unsigned int i, low_avail, high_avail;
	unsigned int min_low;

	/* vGPU type name is defined as GVTg_Vx_y which contains
	 * physical GPU generation type (e.g V4 as BDW server, V5 as
	 * SKL server).
	 *
	 * Depend on physical SKU resource, might see vGPU types like
	 * GVTg_V4_8, GVTg_V4_4, GVTg_V4_2, etc. We can create
	 * different types of vGPU on same physical GPU depending on
	 * available resource. Each vGPU type will have "avail_instance"
	 * to indicate how many vGPU instance can be created for this
	 * type.
	 *
	 */
	low_avail = gvt_aperture_sz(gvt) - HOST_LOW_GM_SIZE;
	high_avail = gvt_hidden_sz(gvt) - HOST_HIGH_GM_SIZE;

	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 < ARRAY_SIZE(vgpu_types); i++) {
		if (low_avail / vgpu_types[i].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;

		if (vgpu_types[i].weight < 1 ||
		    vgpu_types[i].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);

		if (IS_GEN8(gvt->dev_priv))
			snprintf(gvt->types[i].name, sizeof(gvt->types[i].name),
				 "GVTg_V4_%s", vgpu_types[i].name);
		else if (IS_GEN9(gvt->dev_priv))
			snprintf(gvt->types[i].name, sizeof(gvt->types[i].name),
				 "GVTg_V5_%s", vgpu_types[i].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));
	}

	gvt->num_types = i;

	return 0;
}

WARNING: multiple messages have this Message-ID (diff)
From: Joe Perches <joe@perches.com>
To: rajan.vaja@gmail.com, zhenyuw@linux.intel.com,
	zhi.a.wang@intel.com, jani.nikula@linux.intel.com,
	joonas.lahtinen@linux.intel.com, rodrigo.vivi@intel.com,
	airlied@linux.ie
Cc: intel-gvt-dev@lists.freedesktop.org,
	linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org
Subject: Re: [PATCH] drm/i915/gvt: Use ARRAY_SIZE macro
Date: Mon, 18 Jun 2018 09:20:32 -0700	[thread overview]
Message-ID: <1d81ec0834a57d4a864ed05611614192ba5eb3d6.camel@perches.com> (raw)
In-Reply-To: <1529308106-30716-1-git-send-email-rajan.vaja@softnautics.com>

On Mon, 2018-06-18 at 13:18 +0530, rajan.vaja@gmail.com wrote:
> Use ARRAY_SIZE instead of dividing sizeof array with sizeof
> an element. This fixes below warning reported by Coccinelle:
> drivers/gpu/drm//i915/gvt/vgpu.c:122:30-31: WARNING: Use ARRAY_SIZE
[]
> diff --git a/drivers/gpu/drm/i915/gvt/vgpu.c b/drivers/gpu/drm/i915/gvt/vgpu.c
[]
> @@ -119,7 +119,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 = kzalloc(num_types * sizeof(struct intel_vgpu_type),
>  			     GFP_KERNEL);

It seems you are not using -next as this alloc has
already been changed to kcalloc.

It'd be better to get rid of num_types altogether and
use ARRAY_SIZE everywhere instead.

There are also string overflow possibilities in the
function where sprintf should probably use snprintf.

Perhaps:
'
int intel_gvt_init_vgpu_types(struct intel_gvt *gvt)
{
	unsigned int i, low_avail, high_avail;
	unsigned int min_low;

	/* vGPU type name is defined as GVTg_Vx_y which contains
	 * physical GPU generation type (e.g V4 as BDW server, V5 as
	 * SKL server).
	 *
	 * Depend on physical SKU resource, might see vGPU types like
	 * GVTg_V4_8, GVTg_V4_4, GVTg_V4_2, etc. We can create
	 * different types of vGPU on same physical GPU depending on
	 * available resource. Each vGPU type will have "avail_instance"
	 * to indicate how many vGPU instance can be created for this
	 * type.
	 *
	 */
	low_avail = gvt_aperture_sz(gvt) - HOST_LOW_GM_SIZE;
	high_avail = gvt_hidden_sz(gvt) - HOST_HIGH_GM_SIZE;

	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 < ARRAY_SIZE(vgpu_types); i++) {
		if (low_avail / vgpu_types[i].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;

		if (vgpu_types[i].weight < 1 ||
		    vgpu_types[i].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);

		if (IS_GEN8(gvt->dev_priv))
			snprintf(gvt->types[i].name, sizeof(gvt->types[i].name),
				 "GVTg_V4_%s", vgpu_types[i].name);
		else if (IS_GEN9(gvt->dev_priv))
			snprintf(gvt->types[i].name, sizeof(gvt->types[i].name),
				 "GVTg_V5_%s", vgpu_types[i].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));
	}

	gvt->num_types = i;

	return 0;
}
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

  reply	other threads:[~2018-06-18 16:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-18  7:48 [PATCH] drm/i915/gvt: Use ARRAY_SIZE macro rajan.vaja
2018-06-18 16:20 ` Joe Perches [this message]
2018-06-18 16:20   ` Joe Perches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1d81ec0834a57d4a864ed05611614192ba5eb3d6.camel@perches.com \
    --to=joe@perches.com \
    --cc=airlied@linux.ie \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-gvt-dev@lists.freedesktop.org \
    --cc=jani.nikula@linux.intel.com \
    --cc=joonas.lahtinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rajan.vaja@gmail.com \
    --cc=rodrigo.vivi@intel.com \
    --cc=zhenyuw@linux.intel.com \
    --cc=zhi.a.wang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.