Re: [igt-dev] [PATCH i-g-t v2] tests/gem_exec_balancer: Manually calculate VLA struct sizes

Chris Wilson
2019-06-04T13:24:13Z

Quoting Arkadiusz Hiler (2019-06-04 14:15:55)
> VLA in structs (struct { int array[count] }) is a GCC extension, so
> let's avoid using it.
>
> v2: don't be overzealous in converting static-size structs
>
> Cc: Chris Wilson <chris@chris-wilson.co.uk>
> Cc: Simon Ser <simon.ser@intel.com>
> Signed-off-by: Arkadiusz Hiler <arkadiusz.hiler@intel.com>
> ---
> tests/i915/gem_exec_balancer.c | 64 ++++++++++++++++++++++------------
> 1 file changed, 42 insertions(+), 22 deletions(-)
>
> diff --git a/tests/i915/gem_exec_balancer.c b/tests/i915/gem_exec_balancer.c
> index 33b17fc5..29438381 100644
> --- a/tests/i915/gem_exec_balancer.c
> +++ b/tests/i915/gem_exec_balancer.c
> @@ -32,6 +32,26 @@ IGT_TEST_DESCRIPTION("Exercise in-kernel load-balancing");
>
> #define INSTANCE_COUNT (1 << I915_PMU_SAMPLE_INSTANCE_BITS)
>
> +static size_t sizeof_load_balance(int count)
> +{
> + return offsetof(struct i915_context_engines_load_balance,
> + engines[count]);
> +}
> +
> +static size_t sizeof_param_engines(int count)
> +{
> + return offsetof(struct i915_context_param_engines,
> + engines[count]);
> +}
> +
> +static size_t sizeof_engines_bond(int count)
> +{
> + return offsetof(struct i915_context_engines_bond,
> + engines[count]);
> +}
> +
> +#define alloca0(sz) ({ size_t sz__ = (sz); memset(alloca(sz__), 0, sz__); })
> +
> static bool has_class_instance(int i915, uint16_t class, uint16_t instance)
> {
> int fd;
> @@ -93,16 +113,17 @@ static int __set_engines(int i915, uint32_t ctx,
> const struct i915_engine_class_instance *ci,
> unsigned int count)
> {
> - I915_DEFINE_CONTEXT_PARAM_ENGINES(engines, count);
> + struct i915_context_param_engines *engines =
> + alloca0(sizeof_param_engines(count));
> struct drm_i915_gem_context_param p = {
> .ctx_id = ctx,
> .param = I915_CONTEXT_PARAM_ENGINES,
> - .size = sizeof(engines),
> - .value = to_user_pointer(&engines)
> + .size = sizeof_param_engines(count),
> + .value = to_user_pointer(engines)
> };
>
> - engines.extensions = 0;
> - memcpy(engines.engines, ci, sizeof(engines.engines));
> + engines->extensions = 0;
> + memcpy(engines->engines, ci, sizeof(engines->engines));

This now needs to be count * sizeof(*engines->engines)
(Or sizeof(*ci) which is the pattern used later.)

> @@ -709,15 +730,14 @@ static void indices(int i915)
> continue;
>
> for (int n = 0; n < count; n++) {
> - I915_DEFINE_CONTEXT_ENGINES_LOAD_BALANCE(*balancer,
> - count);
> + struct i915_context_engines_load_balance *balancer;
>
> engines.engines[nengines].engine_class =
> I915_ENGINE_CLASS_INVALID;
> engines.engines[nengines].engine_instance =
> I915_ENGINE_CLASS_INVALID_NONE;
>
> - balancer = calloc(sizeof(*balancer), 1);
> + balancer = calloc(sizeof_load_balance(1), 1);
> igt_assert(balancer);

Should be sizeof_load_balance(count)
One hopes this fails with -EFAULT, but it's unlikely to actually cross a
page boundary and be detectable. So maybe -EINVAL.

With the fixes,
Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk>
-Chris