Skip to content

Commit 63f23aa

Browse files
k-niemiecgregkh
authored andcommitted
drm/i915/gem: Zero-initialize the eb.vma array in i915_gem_do_execbuffer
commit 4fe2bd1 upstream. Initialize the eb.vma array with values of 0 when the eb structure is first set up. In particular, this sets the eb->vma[i].vma pointers to NULL, simplifying cleanup and getting rid of the bug described below. During the execution of eb_lookup_vmas(), the eb->vma array is successively filled up with struct eb_vma objects. This process includes calling eb_add_vma(), which might fail; however, even in the event of failure, eb->vma[i].vma is set for the currently processed buffer. If eb_add_vma() fails, eb_lookup_vmas() returns with an error, which prompts a call to eb_release_vmas() to clean up the mess. Since eb_lookup_vmas() might fail during processing any (possibly not first) buffer, eb_release_vmas() checks whether a buffer's vma is NULL to know at what point did the lookup function fail. In eb_lookup_vmas(), eb->vma[i].vma is set to NULL if either the helper function eb_lookup_vma() or eb_validate_vma() fails. eb->vma[i+1].vma is set to NULL in case i915_gem_object_userptr_submit_init() fails; the current one needs to be cleaned up by eb_release_vmas() at this point, so the next one is set. If eb_add_vma() fails, neither the current nor the next vma is set to NULL, which is a source of a NULL deref bug described in the issue linked in the Closes tag. When entering eb_lookup_vmas(), the vma pointers are set to the slab poison value, instead of NULL. This doesn't matter for the actual lookup, since it gets overwritten anyway, however the eb_release_vmas() function only recognizes NULL as the stopping value, hence the pointers are being set to NULL as they go in case of intermediate failure. This patch changes the approach to filling them all with NULL at the start instead, rather than handling that manually during failure. Reported-by: Gangmin Kim <km.kim1503@gmail.com> Closes: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/15062 Fixes: 544460c ("drm/i915: Multi-BB execbuf") Cc: stable@vger.kernel.org # 5.16.x Signed-off-by: Krzysztof Niemiec <krzysztof.niemiec@intel.com> Reviewed-by: Janusz Krzysztofik <janusz.krzysztofik@linux.intel.com> Reviewed-by: Krzysztof Karas <krzysztof.karas@intel.com> Reviewed-by: Andi Shyti <andi.shyti@linux.intel.com> Signed-off-by: Andi Shyti <andi.shyti@kernel.org> Link: https://lore.kernel.org/r/20251216180900.54294-2-krzysztof.niemiec@intel.com (cherry picked from commit 08889b7) Signed-off-by: Jani Nikula <jani.nikula@intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 3165fcd commit 63f23aa

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -950,13 +950,13 @@ static int eb_lookup_vmas(struct i915_execbuffer *eb)
950950
vma = eb_lookup_vma(eb, eb->exec[i].handle);
951951
if (IS_ERR(vma)) {
952952
err = PTR_ERR(vma);
953-
goto err;
953+
return err;
954954
}
955955

956956
err = eb_validate_vma(eb, &eb->exec[i], vma);
957957
if (unlikely(err)) {
958958
i915_vma_put(vma);
959-
goto err;
959+
return err;
960960
}
961961

962962
err = eb_add_vma(eb, &current_batch, i, vma);
@@ -965,30 +965,15 @@ static int eb_lookup_vmas(struct i915_execbuffer *eb)
965965

966966
if (i915_gem_object_is_userptr(vma->obj)) {
967967
err = i915_gem_object_userptr_submit_init(vma->obj);
968-
if (err) {
969-
if (i + 1 < eb->buffer_count) {
970-
/*
971-
* Execbuffer code expects last vma entry to be NULL,
972-
* since we already initialized this entry,
973-
* set the next value to NULL or we mess up
974-
* cleanup handling.
975-
*/
976-
eb->vma[i + 1].vma = NULL;
977-
}
978-
968+
if (err)
979969
return err;
980-
}
981970

982971
eb->vma[i].flags |= __EXEC_OBJECT_USERPTR_INIT;
983972
eb->args->flags |= __EXEC_USERPTR_USED;
984973
}
985974
}
986975

987976
return 0;
988-
989-
err:
990-
eb->vma[i].vma = NULL;
991-
return err;
992977
}
993978

994979
static int eb_lock_vmas(struct i915_execbuffer *eb)
@@ -3374,7 +3359,8 @@ i915_gem_do_execbuffer(struct drm_device *dev,
33743359

33753360
eb.exec = exec;
33763361
eb.vma = (struct eb_vma *)(exec + args->buffer_count + 1);
3377-
eb.vma[0].vma = NULL;
3362+
memset(eb.vma, 0, (args->buffer_count + 1) * sizeof(struct eb_vma));
3363+
33783364
eb.batch_pool = NULL;
33793365

33803366
eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
@@ -3583,7 +3569,18 @@ i915_gem_execbuffer2_ioctl(struct drm_device *dev, void *data,
35833569
if (err)
35843570
return err;
35853571

3586-
/* Allocate extra slots for use by the command parser */
3572+
/*
3573+
* Allocate extra slots for use by the command parser.
3574+
*
3575+
* Note that this allocation handles two different arrays (the
3576+
* exec2_list array, and the eventual eb.vma array introduced in
3577+
* i915_gem_do_execbuffer()), that reside in virtually contiguous
3578+
* memory. Also note that the allocation intentionally doesn't fill the
3579+
* area with zeros, because the exec2_list part doesn't need to be, as
3580+
* it's immediately overwritten by user data a few lines below.
3581+
* However, the eb.vma part is explicitly zeroed later in
3582+
* i915_gem_do_execbuffer().
3583+
*/
35873584
exec2_list = kvmalloc_array(count + 2, eb_element_size(),
35883585
__GFP_NOWARN | GFP_KERNEL);
35893586
if (exec2_list == NULL) {

0 commit comments

Comments
 (0)