diff mbox

drm/i915: Make for_each_intel_* macros typesafe

Message ID 1426490483-28312-1-git-send-email-ander.conselvan.de.oliveira@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Ander Conselvan de Oliveira March 16, 2015, 7:21 a.m. UTC
So that the compiler warns if we get something wrong.

Signed-off-by: Ander Conselvan de Oliveira <ander.conselvan.de.oliveira@intel.com>
---
 drivers/gpu/drm/i915/i915_drv.h | 39 ++++++++++++++++++++++++++-------------
 1 file changed, 26 insertions(+), 13 deletions(-)

Comments

Shuang He March 16, 2015, 9:38 a.m. UTC | #1
Tested-By: PRC QA PRTS (Patch Regression Test System Contact: shuang.he@intel.com)
Task id: 5954
-------------------------------------Summary-------------------------------------
Platform          Delta          drm-intel-nightly          Series Applied
PNV                                  268/268              268/268
ILK                 -1              303/303              302/303
SNB                                  283/283              283/283
IVB                                  342/342              342/342
BYT                                  287/287              287/287
HSW                 -1              363/363              362/363
BDW                                  308/308              308/308
-------------------------------------Detailed-------------------------------------
Platform  Test                                drm-intel-nightly          Series Applied
*ILK  igt_gem_fenced_exec_thrash_no-spare-fences-busy      PASS(2)      DMESG_WARN(1)PASS(1)
*HSW  igt_gem_storedw_loop_vebox      PASS(2)      DMESG_WARN(1)PASS(1)
Note: You need to pay more attention to line start with '*'
Jani Nikula March 16, 2015, 10 a.m. UTC | #2
On Mon, 16 Mar 2015, Ander Conselvan de Oliveira <ander.conselvan.de.oliveira@intel.com> wrote:
> So that the compiler warns if we get something wrong.
>
> Signed-off-by: Ander Conselvan de Oliveira <ander.conselvan.de.oliveira@intel.com>
> ---
>  drivers/gpu/drm/i915/i915_drv.h | 39 ++++++++++++++++++++++++++-------------
>  1 file changed, 26 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
> index ae4ba6a..871d77a 100644
> --- a/drivers/gpu/drm/i915/i915_drv.h
> +++ b/drivers/gpu/drm/i915/i915_drv.h
> @@ -232,30 +232,43 @@ enum hpd_pin {
>  	     (__s) < INTEL_INFO(__dev_priv)->num_sprites[(__p)];	\
>  	     (__s)++)
>  
> -#define for_each_crtc(dev, crtc) \
> +#define type_check(variable, type)				\
> +	{							\
> +		typeof(variable) __dummy_ptr = NULL;		\
> +		const type *__type_check = __dummy_ptr;		\
> +		(void) __type_check;				\
> +	}
> +
> +#define for_each_crtc(dev, crtc)				\
> +	type_check(crtc, struct drm_crtc)			\
>  	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)

if (foo)
	for_each_crtc(dev, crtc);

will do type_check if foo, but list_for_each_entry unconditionally.

I think this is too fragile.

I'm already pretty anxious about our loop macros with an if in them
blowing up due to the dangling else:

	if (foo)
        	list_for_each_encoder_on_crtc(dev, crtc, intel_encoder)
                	bar();
	else
        	baz();

where baz() gets called for each encoder *not* on crtc, instead of once
for !foo.


BR,
Jani.



>  
> -#define for_each_intel_crtc(dev, intel_crtc) \
> -	list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list, base.head)
> +#define for_each_intel_crtc(dev, crtc)				\
> +	type_check(crtc, struct intel_crtc)			\
> +	list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
>  
> -#define for_each_intel_encoder(dev, intel_encoder)		\
> -	list_for_each_entry(intel_encoder,			\
> +#define for_each_intel_encoder(dev, encoder)			\
> +	type_check(encoder, struct intel_encoder)		\
> +	list_for_each_entry(encoder,				\
>  			    &(dev)->mode_config.encoder_list,	\
>  			    base.head)
>  
> -#define for_each_intel_connector(dev, intel_connector)		\
> -	list_for_each_entry(intel_connector,			\
> +#define for_each_intel_connector(dev, connector)		\
> +	type_check(connector, struct intel_connector);		\
> +	list_for_each_entry(connector,				\
>  			    &dev->mode_config.connector_list,	\
>  			    base.head)
>  
>  
> -#define for_each_encoder_on_crtc(dev, __crtc, intel_encoder) \
> -	list_for_each_entry((intel_encoder), &(dev)->mode_config.encoder_list, base.head) \
> -		if ((intel_encoder)->base.crtc == (__crtc))
> +#define for_each_encoder_on_crtc(dev, __crtc, encoder)		\
> +	type_check(encoder, struct intel_encoder)		\
> +	list_for_each_entry((encoder), &(dev)->mode_config.encoder_list, base.head) \
> +		if ((encoder)->base.crtc == (__crtc))
>  
> -#define for_each_connector_on_encoder(dev, __encoder, intel_connector) \
> -	list_for_each_entry((intel_connector), &(dev)->mode_config.connector_list, base.head) \
> -		if ((intel_connector)->base.encoder == (__encoder))
> +#define for_each_connector_on_encoder(dev, __encoder, connector) \
> +	type_check(connector, struct intel_connector)		\
> +	list_for_each_entry((connector), &(dev)->mode_config.connector_list, base.head) \
> +		if ((connector)->base.encoder == (__encoder))
>  
>  #define for_each_power_domain(domain, mask)				\
>  	for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++)	\
> -- 
> 2.1.0
>
> _______________________________________________
> Intel-gfx mailing list
> Intel-gfx@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Chris Wilson March 16, 2015, 10:42 a.m. UTC | #3
On Mon, Mar 16, 2015 at 12:00:35PM +0200, Jani Nikula wrote:
> On Mon, 16 Mar 2015, Ander Conselvan de Oliveira <ander.conselvan.de.oliveira@intel.com> wrote:
> > So that the compiler warns if we get something wrong.
> >
> > Signed-off-by: Ander Conselvan de Oliveira <ander.conselvan.de.oliveira@intel.com>
> > ---
> >  drivers/gpu/drm/i915/i915_drv.h | 39 ++++++++++++++++++++++++++-------------
> >  1 file changed, 26 insertions(+), 13 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
> > index ae4ba6a..871d77a 100644
> > --- a/drivers/gpu/drm/i915/i915_drv.h
> > +++ b/drivers/gpu/drm/i915/i915_drv.h
> > @@ -232,30 +232,43 @@ enum hpd_pin {
> >  	     (__s) < INTEL_INFO(__dev_priv)->num_sprites[(__p)];	\
> >  	     (__s)++)
> >  
> > -#define for_each_crtc(dev, crtc) \
> > +#define type_check(variable, type)				\
> > +	{							\
> > +		typeof(variable) __dummy_ptr = NULL;		\
> > +		const type *__type_check = __dummy_ptr;		\
> > +		(void) __type_check;				\
> > +	}
> > +
> > +#define for_each_crtc(dev, crtc)				\
> > +	type_check(crtc, struct drm_crtc)			\
> >  	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)

#define __list_for_each_entry_type(pos, head, type, member) \
	for (pos = list_first_entry(head, type, member); \
	     &pos->member != (head); \
	     pos = list_next_entry(pos, member))

#define for_each_crtc(dev, crtc) \
	__list_for_each_entry_type(crtc, \
				   &dev->mode_config.crtc_list, \
				   struct drm_crtc, \
				   head)
-Chris
diff mbox

Patch

diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
index ae4ba6a..871d77a 100644
--- a/drivers/gpu/drm/i915/i915_drv.h
+++ b/drivers/gpu/drm/i915/i915_drv.h
@@ -232,30 +232,43 @@  enum hpd_pin {
 	     (__s) < INTEL_INFO(__dev_priv)->num_sprites[(__p)];	\
 	     (__s)++)
 
-#define for_each_crtc(dev, crtc) \
+#define type_check(variable, type)				\
+	{							\
+		typeof(variable) __dummy_ptr = NULL;		\
+		const type *__type_check = __dummy_ptr;		\
+		(void) __type_check;				\
+	}
+
+#define for_each_crtc(dev, crtc)				\
+	type_check(crtc, struct drm_crtc)			\
 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
 
-#define for_each_intel_crtc(dev, intel_crtc) \
-	list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list, base.head)
+#define for_each_intel_crtc(dev, crtc)				\
+	type_check(crtc, struct intel_crtc)			\
+	list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
 
-#define for_each_intel_encoder(dev, intel_encoder)		\
-	list_for_each_entry(intel_encoder,			\
+#define for_each_intel_encoder(dev, encoder)			\
+	type_check(encoder, struct intel_encoder)		\
+	list_for_each_entry(encoder,				\
 			    &(dev)->mode_config.encoder_list,	\
 			    base.head)
 
-#define for_each_intel_connector(dev, intel_connector)		\
-	list_for_each_entry(intel_connector,			\
+#define for_each_intel_connector(dev, connector)		\
+	type_check(connector, struct intel_connector);		\
+	list_for_each_entry(connector,				\
 			    &dev->mode_config.connector_list,	\
 			    base.head)
 
 
-#define for_each_encoder_on_crtc(dev, __crtc, intel_encoder) \
-	list_for_each_entry((intel_encoder), &(dev)->mode_config.encoder_list, base.head) \
-		if ((intel_encoder)->base.crtc == (__crtc))
+#define for_each_encoder_on_crtc(dev, __crtc, encoder)		\
+	type_check(encoder, struct intel_encoder)		\
+	list_for_each_entry((encoder), &(dev)->mode_config.encoder_list, base.head) \
+		if ((encoder)->base.crtc == (__crtc))
 
-#define for_each_connector_on_encoder(dev, __encoder, intel_connector) \
-	list_for_each_entry((intel_connector), &(dev)->mode_config.connector_list, base.head) \
-		if ((intel_connector)->base.encoder == (__encoder))
+#define for_each_connector_on_encoder(dev, __encoder, connector) \
+	type_check(connector, struct intel_connector)		\
+	list_for_each_entry((connector), &(dev)->mode_config.connector_list, base.head) \
+		if ((connector)->base.encoder == (__encoder))
 
 #define for_each_power_domain(domain, mask)				\
 	for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++)	\