Skip to contentSkip to conversation

drm/i915: Use BUILD_BUG_ON to detected missing engine definitions

PATCH In review Michal Wajdeczko 3 participants 5 messages Mar 1, 2017 – Mar 2, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 drm/i915: Use BUILD_BUG_ON to detected missing engine definitions

Engine related definitions are located in different files and it is easy to break their cross dependency.

Additionally use GEM_WARN_ON to catch invalid engine index.

v2: compare against array size

Cc Jani Nikula <[email protected]>
Cc Joonas Lahtinen <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Tvrtko Ursulin <[email protected]>
---
 drivers/gpu/drm/i915/intel_engine_cs.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/intel_engine_cs.c +6 -1
index a238304..c1f58b5 100644
@@ -84,11 +84,16 @@ static const struct engine_info {
 
 static int
 intel_engine_setup(struct drm_i915_private *dev_priv,
-		   enum intel_engine_id id)
+		   unsigned int id)
 {
 	const struct engine_info *info = &intel_engines[id];
 	struct intel_engine_cs *engine;
 
+	BUILD_BUG_ON(ARRAY_SIZE(intel_engines) !=
+		     ARRAY_SIZE(dev_priv->engine));
+	if (GEM_WARN_ON(id >= ARRAY_SIZE(intel_engines)))
+		return -EINVAL;
+
 	GEM_BUG_ON(dev_priv->engine[id]);
 	engine = kzalloc(sizeof(*engine), GFP_KERNEL);
 	if (!engine)
Signature · 6 lines
2.7.4

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw

✓ Fi.CI.BAT: success for drm/i915: Use BUILD_BUG_ON to detected missing engine definitions (rev2)

== Series Details ==

== Series Details ==

Series: drm/i915: Use BUILD_BUG_ON to detected missing engine definitions (rev2)
URL : https://patchwork.freedesktop.org/series/20394/
State : success

== Summary ==

Series 20394v2 drm/i915: Use BUILD_BUG_ON to detected missing engine definitions
https://patchwork.freedesktop.org/api/1.0/series/20394/revisions/2/mbox/

Test gem_exec_reloc:

        Subgroup basic-gtt-cpu-active:
                incomplete -> PASS       (fi-byt-j1900)
fi-bdw-5557u     total:278  pass:267  dwarn:0   dfail:0   fail:0   skip:11 
fi-bsw-n3050     total:278  pass:239  dwarn:0   dfail:0   fail:0   skip:39 
fi-bxt-j4205     total:278  pass:259  dwarn:0   dfail:0   fail:0   skip:19 
fi-bxt-t5700     total:278  pass:258  dwarn:0   dfail:0   fail:0   skip:20 
fi-byt-j1900     total:278  pass:251  dwarn:0   dfail:0   fail:0   skip:27 
fi-byt-n2820     total:278  pass:247  dwarn:0   dfail:0   fail:0   skip:31 
fi-hsw-4770      total:278  pass:262  dwarn:0   dfail:0   fail:0   skip:16 
fi-hsw-4770r     total:278  pass:262  dwarn:0   dfail:0   fail:0   skip:16 
fi-ilk-650       total:278  pass:192  dwarn:36  dfail:0   fail:0   skip:50 
fi-ivb-3520m     total:278  pass:260  dwarn:0   dfail:0   fail:0   skip:18 
fi-ivb-3770      total:278  pass:260  dwarn:0   dfail:0   fail:0   skip:18 
fi-kbl-7500u     total:278  pass:259  dwarn:1   dfail:0   fail:0   skip:18 
fi-skl-6260u     total:278  pass:268  dwarn:0   dfail:0   fail:0   skip:10 
fi-skl-6700hq    total:278  pass:261  dwarn:0   dfail:0   fail:0   skip:17 
fi-skl-6700k     total:278  pass:216  dwarn:44  dfail:0   fail:0   skip:18 
fi-skl-6770hq    total:278  pass:268  dwarn:0   dfail:0   fail:0   skip:10 
fi-snb-2520m     total:278  pass:250  dwarn:0   dfail:0   fail:0   skip:28 
fi-snb-2600      total:278  pass:249  dwarn:0   dfail:0   fail:0   skip:29 

07e9f4ad3a2ca599975099c1fe49189a24be1884 drm-tip: 2017y-03m-01d-18h-00m-03s UTC integration manifest 05b8c8e drm/i915: Use BUILD_BUG_ON to detected missing engine definitions

== Logs ==

For more details see: https://intel-gfx-ci.01.org/CI/Patchwork_4020/
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#2 lore raw
In reply to Michal Wajdeczko
Quoted 29 lines · Michal Wajdeczko

Engine related definitions are located in different files and it is easy to break their cross dependency.

Additionally use GEM_WARN_ON to catch invalid engine index.

v2: compare against array size

Cc Jani Nikula <[email protected]>
Cc Joonas Lahtinen <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Tvrtko Ursulin <[email protected]>
---
 drivers/gpu/drm/i915/intel_engine_cs.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/intel_engine_cs.c +1 -1
index a238304..c1f58b5 100644
@@ -84,11 +84,16 @@ static const struct engine_info {
 
 static int
 intel_engine_setup(struct drm_i915_private *dev_priv,
-		   enum intel_engine_id id)
+		   unsigned int id)
 {
 	const struct engine_info *info = &intel_engines[id];

drivers/gpu/drm/i915/intel_engine_cs.c static const struct engine_info { · line 99

Quoted 10 lines
drivers/gpu/drm/i915/intel_engine_cs.c +5 -0
 	struct intel_engine_cs *engine;
 
+	BUILD_BUG_ON(ARRAY_SIZE(intel_engines) !=
+		     ARRAY_SIZE(dev_priv->engine));
+	if (GEM_WARN_ON(id >= ARRAY_SIZE(intel_engines)))
+		return -EINVAL;
+
 	GEM_BUG_ON(dev_priv->engine[id]);
 	engine = kzalloc(sizeof(*engine), GFP_KERNEL);
 	if (!engine)

I would add asserts for id >= ARRAY_SIZE(intel_engines) and id >= ARRAY_SIZE(dev_priv->engine). That provides guarantees for no out of bounds access within this function and should be enough for this function.

The rest sounds just like trouble for now.

Also I am not sure about negative enum, do we elsewhere check for that class of errors? Is it worth it? Maybe then just cast to unsigned in the above mentioned asserts?

Regards,

Tvrtko
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#3 lore raw
Quoted 32 lines · Tvrtko Ursulin
Quoted 30 lines · Michal Wajdeczko

Engine related definitions are located in different files and it is easy to break their cross dependency.

Additionally use GEM_WARN_ON to catch invalid engine index.

v2: compare against array size

Cc Jani Nikula <[email protected]>
Cc Joonas Lahtinen <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Tvrtko Ursulin <[email protected]>
---
 drivers/gpu/drm/i915/intel_engine_cs.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/intel_engine_cs.c +1 -1
index a238304..c1f58b5 100644
@@ -84,11 +84,16 @@ static const struct engine_info {
 
 static int
 intel_engine_setup(struct drm_i915_private *dev_priv,
-		   enum intel_engine_id id)
+		   unsigned int id)
 {
 	const struct engine_info *info = &intel_engines[id];
 	struct intel_engine_cs *engine;
 
Quoted 13 lines

drivers/gpu/drm/i915/intel_engine_cs.c static const struct engine_info { · line 99

Quoted 9 lines
drivers/gpu/drm/i915/intel_engine_cs.c +5 -0
+	BUILD_BUG_ON(ARRAY_SIZE(intel_engines) !=
+		     ARRAY_SIZE(dev_priv->engine));
+	if (GEM_WARN_ON(id >= ARRAY_SIZE(intel_engines)))
+		return -EINVAL;
+
 	GEM_BUG_ON(dev_priv->engine[id]);
 	engine = kzalloc(sizeof(*engine), GFP_KERNEL);
 	if (!engine)

I would add asserts for id >= ARRAY_SIZE(intel_engines) and id >= ARRAY_SIZE(dev_priv->engine). That provides guarantees for no out of bounds access within this function and should be enough for this function.

True, but then we will loose early (ie. at build time) detection that our engine array definitions are not in sync (which was primary reason for this patch).

Quoted 2 lines

The rest sounds just like trouble for now.

I would not call extra check as a trouble.
But if you prefer freedom over robustness, then I will step back.

Quoted 4 lines

Also I am not sure about negative enum, do we elsewhere check for that class of errors? Is it worth it? Maybe then just cast to unsigned in the above mentioned asserts?

Note that the caller function is not using enum at all, this id/index is defined there as plain "unsigned". Then it is promoted in this function only, where we start to use it as index again (except id assignment).

IMHO enums are not the best choice for indexing arrays, and if you really want to do so, you should add some guards to prevent unexpected use.

Using cast in these two asserts will do the trick.

Let's try this as compromise ;)

-Michal

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#4 lore raw
Quoted 52 lines · Michal Wajdeczko
Quoted 32 lines · Tvrtko Ursulin
Quoted 30 lines · Michal Wajdeczko

Engine related definitions are located in different files and it is easy to break their cross dependency.

Additionally use GEM_WARN_ON to catch invalid engine index.

v2: compare against array size

Cc Jani Nikula <[email protected]>
Cc Joonas Lahtinen <[email protected]>
Cc Chris Wilson <[email protected]>
Cc Tvrtko Ursulin <[email protected]>
---
 drivers/gpu/drm/i915/intel_engine_cs.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/intel_engine_cs.c +1 -1
index a238304..c1f58b5 100644
@@ -84,11 +84,16 @@ static const struct engine_info {
 
 static int
 intel_engine_setup(struct drm_i915_private *dev_priv,
-		   enum intel_engine_id id)
+		   unsigned int id)
 {
 	const struct engine_info *info = &intel_engines[id];
 	struct intel_engine_cs *engine;
 
Quoted 13 lines

drivers/gpu/drm/i915/intel_engine_cs.c static const struct engine_info { · line 99

Quoted 9 lines
drivers/gpu/drm/i915/intel_engine_cs.c +5 -0
+	BUILD_BUG_ON(ARRAY_SIZE(intel_engines) !=
+		     ARRAY_SIZE(dev_priv->engine));
+	if (GEM_WARN_ON(id >= ARRAY_SIZE(intel_engines)))
+		return -EINVAL;
+
 	GEM_BUG_ON(dev_priv->engine[id]);
 	engine = kzalloc(sizeof(*engine), GFP_KERNEL);
 	if (!engine)

I would add asserts for id >= ARRAY_SIZE(intel_engines) and id >= ARRAY_SIZE(dev_priv->engine). That provides guarantees for no out of bounds access within this function and should be enough for this function.

True, but then we will loose early (ie. at build time) detection that our engine array definitions are not in sync (which was primary reason for this patch).

Quoted 1 line
Quoted 4 lines
Quoted 1 line

The rest sounds just like trouble for now.

I would not call extra check as a trouble.
But if you prefer freedom over robustness, then I will step back.

Lets call it flexibility and pragmatism. :)

Quoted 7 lines
Quoted 3 lines

Also I am not sure about negative enum, do we elsewhere check for that class of errors? Is it worth it? Maybe then just cast to unsigned in the above mentioned asserts?

Note that the caller function is not using enum at all, this id/index is defined there as plain "unsigned". Then it is promoted in this function only, where we start to use it as index again (except id assignment).

Maybe we should make the loop in intel_engines_init_early use the id instead of i then...

Quoted 2 lines

IMHO enums are not the best choice for indexing arrays, and if you really want to do so, you should add some guards to prevent unexpected use.

... although it is still a local function with a single call site so doesn't get me too excited. But since I'm usually all for data type tidy so feel free to do it.

Quoted 3 lines

Using cast in these two asserts will do the trick.

Let's try this as compromise ;)

Yes I think this compromise is robust enough! ;)

Regards,

Tvrtko

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx