Skip to contentSkip to conversation

Common DRM execution context v4

PATCH In review Christian König 8 participants 50 messages May 4, 2023 – Jun 21, 2023

Subsystems (11)
lore
Share & tools
lore

Christian König

author reviewer
#0 lore raw

Hi guys,

so well known patch set by now. I've tried to address all review comments and extended the set to also replace drm_gem_lock_reservations() as suggested by Thomas.

I won't have much time to work on this in the next few weeks, so feel free to pick up this work and commit it when you need it.

Regards,
Christian.

author reviewer
#1 lore raw

PATCH 1/13 drm: execution context for GEM buffers v4

This adds the infrastructure for an execution context for GEM buffers which is similar to the existing TTMs execbuf util and intended to replace it in the long term.

The basic functionality is that we abstracts the necessary loop to lock many different GEM buffers with automated deadlock and duplicate handling.

v2: drop xarray and use dynamic resized array instead, the locking
overhead is unecessary and measurable.
v3: drop duplicate tracking, radeon is really the only one needing that.
v4: fixes issues pointed out by Danilo, some typos in comments and a
helper for lock arrays of GEM objects.

---
 Documentation/gpu/drm-mm.rst |  12 ++
 drivers/gpu/drm/Kconfig      |   6 +
 drivers/gpu/drm/Makefile     |   2 +
 drivers/gpu/drm/drm_exec.c   | 278 +++++++++++++++++++++++++++++++++++
 include/drm/drm_exec.h       | 119 +++++++++++++++
 5 files changed, 417 insertions(+)
 create mode 100644 drivers/gpu/drm/drm_exec.c
 create mode 100644 include/drm/drm_exec.h
Documentation/gpu/drm-mm.rst +12 -0
index a79fd3549ff8..a52e6f4117d6 100644
@@ -493,6 +493,18 @@ DRM Sync Objects
 .. kernel-doc:: drivers/gpu/drm/drm_syncobj.c
    :export:
 
+DRM Execution context
+=====================
+
+.. kernel-doc:: drivers/gpu/drm/drm_exec.c
+   :doc: Overview
+
+.. kernel-doc:: include/drm/drm_exec.h
+   :internal:
+
+.. kernel-doc:: drivers/gpu/drm/drm_exec.c
+   :export:
+
 GPU Scheduler
 =============
 
drivers/gpu/drm/Kconfig +6 -0
index ba3fb04bb691..2dc81eb062eb 100644
@@ -201,6 +201,12 @@ config DRM_TTM
 	  GPU memory types. Will be enabled automatically if a device driver
 	  uses it.
 
+config DRM_EXEC
+	tristate
+	depends on DRM
+	help
+	  Execution context for command submissions
+
 config DRM_BUDDY
 	tristate
 	depends on DRM
drivers/gpu/drm/Makefile +2 -0
index a33257d2bc7f..9c6446eb3c83 100644
@@ -78,6 +78,8 @@ obj-$(CONFIG_DRM_PANEL_ORIENTATION_QUIRKS) += drm_panel_orientation_quirks.o
 #
 # Memory-management helpers
 #
+#
+obj-$(CONFIG_DRM_EXEC) += drm_exec.o
 
 obj-$(CONFIG_DRM_BUDDY) += drm_buddy.o
 
drivers/gpu/drm/drm_exec.c new file +278 -0

278 lines not on this page · raw message

include/drm/drm_exec.h new file +119 -0

119 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

Quoted 14 lines · Christian König

This adds the infrastructure for an execution context for GEM buffers which is similar to the existing TTMs execbuf util and intended to replace it in the long term.

The basic functionality is that we abstracts the necessary loop to lock many different GEM buffers with automated deadlock and duplicate handling.

v2: drop xarray and use dynamic resized array instead, the locking
overhead is unecessary and measurable.
v3: drop duplicate tracking, radeon is really the only one needing that.
v4: fixes issues pointed out by Danilo, some typos in comments and a
helper for lock arrays of GEM objects.

...

Quoted 18 lines
include/drm/drm_exec.h new file +18 -0
+/**
+ * struct drm_exec - Execution context
+ */
+struct drm_exec {
+	/**
+	 * @interruptible: If locks should be taken interruptible
+	 */
+	bool			interruptible;
+
+	/**
+	 * @ticket: WW ticket used for acquiring locks
+	 */
+	struct ww_acquire_ctx	ticket;
+
+	/**
+	 * @num_objects: number of objects locked
+	 */
+	unsigned int		num_objects;

include/drm/drm_exec.h line 37

Quoted 10 lines
include/drm/drm_exec.h new file +10 -0
+
+	/**
+	 * @max_objects: maximum objects in array
+	 */
+	unsigned int		max_objects;
+
+	/**
+	 * @objects: array of the locked objects
+	 */
+	struct drm_gem_object	**objects;

Hi, Christian. Did you consider using a list here with links embedded in gem objects, now that only locked objects are to be put on the list / array.

That should work as only the process owning the lock may access the list link. Apart from getting rid of reallocing this is beneficial for the more general types of ww transactions that are used by i915 (and to a minor extent xe as well, I think).

In those cases we would want to unlock a temporary held object within the while_not_all_locked() loop and would then have to search the entire array for the correct pointer. Instead one could just remove it from the list of locked objects.

Thanks,

Thomas

Reviewed-by on v1 1/13
#3 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

In reply to Christian König
Quoted 7 lines · Christian König

This adds the infrastructure for an execution context for GEM buffers which is similar to the existing TTMs execbuf util and intended to replace it in the long term.

The basic functionality is that we abstracts the necessary loop to lock many different GEM buffers with automated deadlock and duplicate handling.

Quoted 7 lines

v2: drop xarray and use dynamic resized array instead, the locking
overhead is unecessary and measurable.
v3: drop duplicate tracking, radeon is really the only one needing that.
v4: fixes issues pointed out by Danilo, some typos in comments and a
helper for lock arrays of GEM objects.

Quoted 472 lines
---
  Documentation/gpu/drm-mm.rst |  12 ++
  drivers/gpu/drm/Kconfig      |   6 +
  drivers/gpu/drm/Makefile     |   2 +
  drivers/gpu/drm/drm_exec.c   | 278 +++++++++++++++++++++++++++++++++++
  include/drm/drm_exec.h       | 119 +++++++++++++++
  5 files changed, 417 insertions(+)

create mode 100644 drivers/gpu/drm/drm_exec.c
create mode 100644 include/drm/drm_exec.h

Documentation/gpu/drm-mm.rst +12 -0
index a79fd3549ff8..a52e6f4117d6 100644
@@ -493,6 +493,18 @@ DRM Sync Objects
  .. kernel-doc:: drivers/gpu/drm/drm_syncobj.c
     :export:
 
+DRM Execution context
+=====================
+
+.. kernel-doc:: drivers/gpu/drm/drm_exec.c
+   :doc: Overview
+
+.. kernel-doc:: include/drm/drm_exec.h
+   :internal:
+
+.. kernel-doc:: drivers/gpu/drm/drm_exec.c
+   :export:
+
  GPU Scheduler
  =============
 
drivers/gpu/drm/Kconfig +6 -0
index ba3fb04bb691..2dc81eb062eb 100644
@@ -201,6 +201,12 @@ config DRM_TTM
  	  GPU memory types. Will be enabled automatically if a device driver
  	  uses it.
 
+config DRM_EXEC
+	tristate
+	depends on DRM
+	help
+	  Execution context for command submissions
+
  config DRM_BUDDY
  	tristate
  	depends on DRM
drivers/gpu/drm/Makefile +2 -0
index a33257d2bc7f..9c6446eb3c83 100644
@@ -78,6 +78,8 @@ obj-$(CONFIG_DRM_PANEL_ORIENTATION_QUIRKS) += drm_panel_orientation_quirks.o
  #
  # Memory-management helpers
  #
+#
+obj-$(CONFIG_DRM_EXEC) += drm_exec.o
 
  obj-$(CONFIG_DRM_BUDDY) += drm_buddy.o
 
drivers/gpu/drm/drm_exec.c new file +278 -0
new file mode 100644
index 000000000000..18071bff20f4
@@ -0,0 +1,278 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+
+#include <drm/drm_exec.h>
+#include <drm/drm_gem.h>
+#include <linux/dma-resv.h>
+
+/**
+ * DOC: Overview
+ *
+ * This component mainly abstracts the retry loop necessary for locking
+ * multiple GEM objects while preparing hardware operations (e.g. command
+ * submissions, page table updates etc..).
+ *
+ * If a contention is detected while locking a GEM object the cleanup procedure
+ * unlocks all previously locked GEM objects and locks the contended one first
+ * before locking any further objects.
+ *
+ * After an object is locked fences slots can optionally be reserved on the
+ * dma_resv object inside the GEM object.
+ *
+ * A typical usage pattern should look like this::
+ *
+ *	struct drm_gem_object *obj;
+ *	struct drm_exec exec;
+ *	unsigned long index;
+ *	int ret;
+ *
+ *	drm_exec_init(&exec, true);
+ *	drm_exec_while_not_all_locked(&exec) {
+ *		ret = drm_exec_prepare_obj(&exec, boA, 1);
+ *		drm_exec_continue_on_contention(&exec);
+ *		if (ret)
+ *			goto error;
+ *
+ *		ret = drm_exec_prepare_obj(&exec, boB, 1);
+ *		drm_exec_continue_on_contention(&exec);
+ *		if (ret)
+ *			goto error;
+ *	}
+ *
+ *	drm_exec_for_each_locked_object(&exec, index, obj) {
+ *		dma_resv_add_fence(obj->resv, fence, DMA_RESV_USAGE_READ);
+ *		...
+ *	}
+ *	drm_exec_fini(&exec);
+ *
+ * See struct dma_exec for more details.
+ */
+
+/* Dummy value used to initially enter the retry loop */
+#define DRM_EXEC_DUMMY (void*)~0
+
+/* Unlock all objects and drop references */
+static void drm_exec_unlock_all(struct drm_exec *exec)
+{
+	struct drm_gem_object *obj;
+	unsigned long index;
+
+	drm_exec_for_each_locked_object(exec, index, obj) {
+		dma_resv_unlock(obj->resv);
+		drm_gem_object_put(obj);
+	}
+
+	drm_gem_object_put(exec->prelocked);
+	exec->prelocked = NULL;
+}
+
+/**
+ * drm_exec_init - initialize a drm_exec object
+ * @exec: the drm_exec object to initialize
+ * @interruptible: if locks should be acquired interruptible
+ *
+ * Initialize the object and make sure that we can track locked objects.
+ */
+void drm_exec_init(struct drm_exec *exec, bool interruptible)
+{
+	exec->interruptible = interruptible;
+	exec->objects = kmalloc(PAGE_SIZE, GFP_KERNEL);
+
+	/* If allocation here fails, just delay that till the first use */
+	exec->max_objects = exec->objects ? PAGE_SIZE / sizeof(void *) : 0;
+	exec->num_objects = 0;
+	exec->contended = DRM_EXEC_DUMMY;
+	exec->prelocked = NULL;
+}
+EXPORT_SYMBOL(drm_exec_init);
+
+/**
+ * drm_exec_fini - finalize a drm_exec object
+ * @exec: the drm_exec object to finalize
+ *
+ * Unlock all locked objects, drop the references to objects and free all memory
+ * used for tracking the state.
+ */
+void drm_exec_fini(struct drm_exec *exec)
+{
+	drm_exec_unlock_all(exec);
+	kvfree(exec->objects);
+	if (exec->contended != DRM_EXEC_DUMMY) {
+		drm_gem_object_put(exec->contended);
+		ww_acquire_fini(&exec->ticket);
+	}
+}
+EXPORT_SYMBOL(drm_exec_fini);
+
+/**
+ * drm_exec_cleanup - cleanup when contention is detected
+ * @exec: the drm_exec object to cleanup
+ *
+ * Cleanup the current state and return true if we should stay inside the retry
+ * loop, false if there wasn't any contention detected and we can keep the
+ * objects locked.
+ */
+bool drm_exec_cleanup(struct drm_exec *exec)
+{
+	if (likely(!exec->contended)) {
+		ww_acquire_done(&exec->ticket);
+		return false;
+	}
+
+	if (likely(exec->contended == DRM_EXEC_DUMMY)) {
+		exec->contended = NULL;
+		ww_acquire_init(&exec->ticket, &reservation_ww_class);
+		return true;
+	}
+
+	drm_exec_unlock_all(exec);
+	exec->num_objects = 0;
+	return true;
+}
+EXPORT_SYMBOL(drm_exec_cleanup);
+
+/* Track the locked object in the array */
+static int drm_exec_obj_locked(struct drm_exec *exec,
+			       struct drm_gem_object *obj)
+{
+	if (unlikely(exec->num_objects == exec->max_objects)) {
+		size_t size = exec->max_objects * sizeof(void *);
+		void *tmp;
+
+		tmp = kvrealloc(exec->objects, size, size + PAGE_SIZE,
+				GFP_KERNEL);
+		if (!tmp)
+			return -ENOMEM;
+
+		exec->objects = tmp;
+		exec->max_objects += PAGE_SIZE / sizeof(void *);
+	}
+	drm_gem_object_get(obj);
+	exec->objects[exec->num_objects++] = obj;
+
+	return 0;
+}
+
+/* Make sure the contended object is locked first */
+static int drm_exec_lock_contended(struct drm_exec *exec)
+{
+	struct drm_gem_object *obj = exec->contended;
+	int ret;
+
+	if (likely(!obj))
+		return 0;
+
+	if (exec->interruptible) {
+		ret = dma_resv_lock_slow_interruptible(obj->resv,
+						       &exec->ticket);
+		if (unlikely(ret))
+			goto error_dropref;
+	} else {
+		dma_resv_lock_slow(obj->resv, &exec->ticket);
+	}
+
+	ret = drm_exec_obj_locked(exec, obj);
+	if (unlikely(ret)) {
+		dma_resv_unlock(obj->resv);
+		goto error_dropref;
+	}
+
+	swap(exec->prelocked, obj);
+
+error_dropref:
+	/* Always cleanup the contention so that error handling can kick in */
+	drm_gem_object_put(obj);
+	exec->contended = NULL;
+	return ret;
+}
+
+/**
+ * drm_exec_prepare_obj - prepare a GEM object for use
+ * @exec: the drm_exec object with the state
+ * @obj: the GEM object to prepare
+ * @num_fences: how many fences to reserve
+ *
+ * Prepare a GEM object for use by locking it and reserving fence slots. All
+ * successfully locked objects are put into the locked container.
+ *
+ * Returns: -EDEADLK if a contention is detected, -EALREADY when object is
+ * already locked, -ENOMEM when memory allocation failed and zero for success.
+ */
+int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
+			 unsigned int num_fences)
+{
+	int ret;
+
+	ret = drm_exec_lock_contended(exec);
+	if (unlikely(ret))
+		return ret;
+
+	if (exec->prelocked == obj) {
+		drm_gem_object_put(exec->prelocked);
+		exec->prelocked = NULL;
+
+		return dma_resv_reserve_fences(obj->resv, num_fences);
+	}
+
+	if (exec->interruptible)
+		ret = dma_resv_lock_interruptible(obj->resv, &exec->ticket);
+	else
+		ret = dma_resv_lock(obj->resv, &exec->ticket);
+
+	if (unlikely(ret == -EDEADLK)) {
+		drm_gem_object_get(obj);
+		exec->contended = obj;
+		return -EDEADLK;
+	}
+
+	if (unlikely(ret))
+		return ret;
+
+	ret = drm_exec_obj_locked(exec, obj);
+	if (ret)
+		goto error_unlock;
+
+	/* Keep locked when reserving fences fails */
+	return dma_resv_reserve_fences(obj->resv, num_fences);
+
+error_unlock:
+	dma_resv_unlock(obj->resv);
+	return ret;
+}
+EXPORT_SYMBOL(drm_exec_prepare_obj);
+
+/**
+ * drm_exec_prepare_array - helper to prepare an array of objects
+ * @exec: the drm_exec object with the state
+ * @objects: array of GEM object to prepare
+ * @num_objects: number of GEM objects in the array
+ * @num_fences: number of fences to reserve on each GEM object
+ *
+ * Prepares all GEM objects in an array, handles contention but aports on first
+ * error otherwise. Reserves @num_fences on each GEM object after locking it.
+ *
+ * Returns: -EALREADY when object is already locked, -ENOMEM when memory
+ * allocation failed and zero for success.
+ */
+int drm_exec_prepare_array(struct drm_exec *exec,
+			   struct drm_gem_object **objects,
+			   unsigned int num_objects,
+			   unsigned int num_fences)
+{
+	int ret;
+
+	drm_exec_while_not_all_locked(exec) {
+		for (unsigned int i = 0; i < num_objects; ++i) {
+			ret = drm_exec_prepare_obj(exec, objects[i],
+						   num_fences);
+			drm_exec_break_on_contention(exec);
+			if (unlikely(ret))
+				return ret;
+		}
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_exec_prepare_array);
+
+MODULE_DESCRIPTION("DRM execution context");
+MODULE_LICENSE("Dual MIT/GPL");
include/drm/drm_exec.h new file +119 -0
new file mode 100644
index 000000000000..7c7481ed088a
@@ -0,0 +1,119 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+
+#ifndef __DRM_EXEC_H__
+#define __DRM_EXEC_H__
+
+#include <linux/ww_mutex.h>
+
+struct drm_gem_object;
+
+/**
+ * struct drm_exec - Execution context
+ */
+struct drm_exec {
+	/**
+	 * @interruptible: If locks should be taken interruptible
+	 */
+	bool			interruptible;
+
+	/**
+	 * @ticket: WW ticket used for acquiring locks
+	 */
+	struct ww_acquire_ctx	ticket;
+
+	/**
+	 * @num_objects: number of objects locked
+	 */
+	unsigned int		num_objects;
+
+	/**
+	 * @max_objects: maximum objects in array
+	 */
+	unsigned int		max_objects;
+
+	/**
+	 * @objects: array of the locked objects
+	 */
+	struct drm_gem_object	**objects;
+
+	/**
+	 * @contended: contended GEM object we backed off for
+	 */
+	struct drm_gem_object	*contended;
+
+	/**
+	 * @prelocked: already locked GEM object due to contention
+	 */
+	struct drm_gem_object *prelocked;
+};
+
+/**
+ * drm_exec_for_each_locked_object - iterate over all the locked objects
+ * @exec: drm_exec object
+ * @index: unsigned long index for the iteration
+ * @obj: the current GEM object
+ *
+ * Iterate over all the locked GEM objects inside the drm_exec object.
+ */
+#define drm_exec_for_each_locked_object(exec, index, obj)	\
+	for (index = 0, obj = (exec)->objects[0];		\
+	     index < (exec)->num_objects;			\
+	     ++index, obj = (exec)->objects[index])
+
+/**
+ * drm_exec_while_not_all_locked - loop until all GEM objects are prepared
+ * @exec: drm_exec object
+ *
+ * Core functionality of the drm_exec object. Loops until all GEM objects are
+ * prepared and no more contention exists.
+ *
+ * At the beginning of the loop it is guaranteed that no GEM object is locked.
+ */
+#define drm_exec_while_not_all_locked(exec)	\
+	while (drm_exec_cleanup(exec))
+
+/**
+ * drm_exec_continue_on_contention - continue the loop when we need to cleanup
+ * @exec: drm_exec object
+ *
+ * Control flow helper to continue when a contention was detected and we need to
+ * clean up and re-start the loop to prepare all GEM objects.
+ */
+#define drm_exec_continue_on_contention(exec)		\
+	if (unlikely(drm_exec_is_contended(exec)))	\
+		continue
+
+/**
+ * drm_exec_break_on_contention - break a subordinal loop on contention
+ * @exec: drm_exec object
+ *
+ * Control flow helper to break a subordinal loop when a contention was detected
+ * and we need to clean up and re-start the loop to prepare all GEM objects.
+ */
+#define drm_exec_break_on_contention(exec)		\
+	if (unlikely(drm_exec_is_contended(exec)))	\
+		break
+
+/**
+ * drm_exec_is_contended - check for contention
+ * @exec: drm_exec object
+ *
+ * Returns true if the drm_exec object has run into some contention while
+ * locking a GEM object and needs to clean up.
+ */
+static inline bool drm_exec_is_contended(struct drm_exec *exec)
+{
+	return !!exec->contended;
+}
+
+void drm_exec_init(struct drm_exec *exec, bool interruptible);
+void drm_exec_fini(struct drm_exec *exec);
+bool drm_exec_cleanup(struct drm_exec *exec);
+int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
+			 unsigned int num_fences);
+int drm_exec_prepare_array(struct drm_exec *exec,
+			   struct drm_gem_object **objects,
+			   unsigned int num_objects,
+			   unsigned int num_fences);
+
+#endif
maintainer
#4 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

In reply to Christian König

Hi Christian,

Quoted 6 lines · On Thu, 4 May 2023 13:51:47 +0200 "Christian König"…

This adds the infrastructure for an execution context for GEM buffers which is similar to the existing TTMs execbuf util and intended to replace it in the long term.

The basic functionality is that we abstracts the necessary loop to lock many different GEM buffers with automated deadlock and duplicate handling.

As many other drivers do already, we are considering using drm_exec() for our resv locking in the PowerVR driver, so we might have more questions/comments in the coming days/weeks, but I already have a couple right now (see below).

Quoted 1 line

v3: drop duplicate tracking, radeon is really the only one needing that

I think we'd actually be interested in duplicate tracking. Is there any way we can make it an optional feature through some extra helpers/flags? Doesn't have to be done in this patch series, I'm just wondering if this is something we can share as well.

[...]

Quoted 20 lines

+/**
+ * DOC: Overview
+ *
+ * This component mainly abstracts the retry loop necessary for locking
+ * multiple GEM objects while preparing hardware operations (e.g. command
+ * submissions, page table updates etc..).
+ *
+ * If a contention is detected while locking a GEM object the cleanup procedure
+ * unlocks all previously locked GEM objects and locks the contended one first
+ * before locking any further objects.
+ *
+ * After an object is locked fences slots can optionally be reserved on the
+ * dma_resv object inside the GEM object.
+ *
+ * A typical usage pattern should look like this::
+ *
+ * struct drm_gem_object *obj;
+ * struct drm_exec exec;
+ * unsigned long index;
+ * int ret;

Quoted 8 lines

+ *
+ * drm_exec_init(&exec, true);
+ * drm_exec_while_not_all_locked(&exec) {
+ * ret = drm_exec_prepare_obj(&exec, boA, 1);
+ * drm_exec_continue_on_contention(&exec);
+ * if (ret)
+ * goto error;
+ *

Have you considered defining a drm_exec_try_prepare_obj_or_retry() combining drm_exec_prepare_obj() and drm_exec_continue_on_contention()?

#define drm_exec_try_prepare_obj_or_retry(exec, obj, num_fences) \
        ({ \
                int __ret = drm_exec_prepare_obj(exec, bo, num_fences); \
                if (unlikely(drm_exec_is_contended(exec))) \
                        continue; \
                __ret; \
        })

This way the following pattern

		ret = drm_exec_prepare_obj(&exec, boA, 1);
		drm_exec_continue_on_contention(&exec);
		if (ret)
			goto error;

can be turned into something more conventional:

		ret = drm_exec_try_prepare_obj_or_retry(&exec, boA, 1);
		if (ret)
			goto error;

I guess we could even add static checks to make sure drm_exec_try_prepare_obj() is called inside a drm_exec_while_not_all_locked() loop.

Quoted 14 lines

+ * ret = drm_exec_prepare_obj(&exec, boB, 1);
+ * drm_exec_continue_on_contention(&exec);
+ * if (ret)
+ * goto error;
+ * }
+ *
+ * drm_exec_for_each_locked_object(&exec, index, obj) {
+ * dma_resv_add_fence(obj->resv, fence, DMA_RESV_USAGE_READ);
+ * ...
+ * }
+ * drm_exec_fini(&exec);
+ *
+ * See struct dma_exec for more details.
+ */

[...]

Quoted 15 lines
drivers/gpu/drm/drm_exec.c new file +15 -0
+/**
+ * drm_exec_prepare_array - helper to prepare an array of objects
+ * @exec: the drm_exec object with the state
+ * @objects: array of GEM object to prepare
+ * @num_objects: number of GEM objects in the array
+ * @num_fences: number of fences to reserve on each GEM object
+ *
+ * Prepares all GEM objects in an array, handles contention but aports on first
+ * error otherwise. Reserves @num_fences on each GEM object after locking it.
+ *
+ * Returns: -EALREADY when object is already locked, -ENOMEM when memory
+ * allocation failed and zero for success.
+ */
+int drm_exec_prepare_array(struct drm_exec *exec,
+			   struct drm_gem_object **objects,

drivers/gpu/drm/drm_exec.c line 267

Quoted 10 lines
drivers/gpu/drm/drm_exec.c new file +10 -0
+			   unsigned int num_objects,
+			   unsigned int num_fences)
+{
+	int ret;
+
+	drm_exec_while_not_all_locked(exec) {
+		for (unsigned int i = 0; i < num_objects; ++i) {
+			ret = drm_exec_prepare_obj(exec, objects[i],
+						   num_fences);
+			drm_exec_break_on_contention(exec);

I had a hard time understanding what the intent was here: we do want the locking to keep going on contention (reset and retry), but we need to break out of the inner loop for this to happen, which is what this drm_exec_break_on_contention() is doing. My misunderstanding was coming from the fact I was expecting drm_exec_break_on_contention() to stop the process of preparing objects. Maybe it's just me, but I think it'd be less confusing if we were getting rid of drm_exec_{break,continue}_on_contention and have the loop slightly adjusted:

	unsigned int obj_ptr = 0;

	drm_exec_while_not_all_locked(exec) {
		int ret;

		/* We acquired/prepared all objects, we can leave the loop now. */
		if (obj_ptr == num_objects)
			break;

		ret = drm_exec_try_prepare_obj_or_retry(exec, objects[obj_ptr++],
							num_fences);
		if (ret)
			return ret;
	}

	return 0;

Of course, this is just my personal view on this, and none of these comments should be considered as blockers, but I thought I'd share my concerns anyway.

Thanks again for your work!

Regards,

Boris

maintainer
#5 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

Am 14.06.23 um 14:23 schrieb Boris Brezillon:

Quoted 16 lines

Hi Christian,

Quoted 6 lines · On Thu, 4 May 2023 13:51:47 +0200 "Christian König"…

This adds the infrastructure for an execution context for GEM buffers which is similar to the existing TTMs execbuf util and intended to replace it in the long term.

The basic functionality is that we abstracts the necessary loop to lock many different GEM buffers with automated deadlock and duplicate handling.

As many other drivers do already, we are considering using drm_exec() for our resv locking in the PowerVR driver, so we might have more questions/comments in the coming days/weeks, but I already have a couple right now (see below).

Quoted 5 lines
Quoted 1 line

v3: drop duplicate tracking, radeon is really the only one needing that

I think we'd actually be interested in duplicate tracking. Is there any way we can make it an optional feature through some extra helpers/flags? Doesn't have to be done in this patch series, I'm just wondering if this is something we can share as well.

You can still capture the -EALREADY error and act appropriately in your driver.

For radeon it just means ignoring the error code and going ahead, but that behavior doesn't seem to be desired in most cases.

Initially I though we need to separately track how many and how often BOs are duplicated, but there is simply no use for this.

Quoted 49 lines

[...]

Quoted 20 lines

+/**
+ * DOC: Overview
+ *
+ * This component mainly abstracts the retry loop necessary for locking
+ * multiple GEM objects while preparing hardware operations (e.g. command
+ * submissions, page table updates etc..).
+ *
+ * If a contention is detected while locking a GEM object the cleanup procedure
+ * unlocks all previously locked GEM objects and locks the contended one first
+ * before locking any further objects.
+ *
+ * After an object is locked fences slots can optionally be reserved on the
+ * dma_resv object inside the GEM object.
+ *
+ * A typical usage pattern should look like this::
+ *
+ * struct drm_gem_object *obj;
+ * struct drm_exec exec;
+ * unsigned long index;
+ * int ret;

Quoted 8 lines

+ *
+ * drm_exec_init(&exec, true);
+ * drm_exec_while_not_all_locked(&exec) {
+ * ret = drm_exec_prepare_obj(&exec, boA, 1);
+ * drm_exec_continue_on_contention(&exec);
+ * if (ret)
+ * goto error;
+ *

Have you considered defining a drm_exec_try_prepare_obj_or_retry() combining drm_exec_prepare_obj() and drm_exec_continue_on_contention()?

#define drm_exec_try_prepare_obj_or_retry(exec, obj, num_fences) \
         ({ \
                 int __ret = drm_exec_prepare_obj(exec, bo, num_fences); \
                 if (unlikely(drm_exec_is_contended(exec))) \
                         continue; \
                 __ret; \
         })

This way the following pattern

		ret = drm_exec_prepare_obj(&exec, boA, 1);
		drm_exec_continue_on_contention(&exec);
		if (ret)
			goto error;
Quoted 5 lines

can be turned into something more conventional:

		ret = drm_exec_try_prepare_obj_or_retry(&exec, boA, 1);
		if (ret)
			goto error;

Yeah, I was considering that as well. But then abandoned it as to complicated.

I really need to find some time to work on that anyway.

Quoted 4 lines

I guess we could even add static checks to make sure drm_exec_try_prepare_obj() is called inside a drm_exec_while_not_all_locked() loop.

Interesting idea, but how would somebody do that?

Regards,
Christian.

Quoted 79 lines
Quoted 15 lines

+ * ret = drm_exec_prepare_obj(&exec, boB, 1);
+ * drm_exec_continue_on_contention(&exec);
+ * if (ret)
+ * goto error;
+ * }
+ *
+ * drm_exec_for_each_locked_object(&exec, index, obj) {
+ * dma_resv_add_fence(obj->resv, fence, DMA_RESV_USAGE_READ);
+ * ...
+ * }
+ * drm_exec_fini(&exec);
+ *
+ * See struct dma_exec for more details.
+ */

[...]

Quoted 15 lines
drivers/gpu/drm/drm_exec.c new file +15 -0
+/**
+ * drm_exec_prepare_array - helper to prepare an array of objects
+ * @exec: the drm_exec object with the state
+ * @objects: array of GEM object to prepare
+ * @num_objects: number of GEM objects in the array
+ * @num_fences: number of fences to reserve on each GEM object
+ *
+ * Prepares all GEM objects in an array, handles contention but aports on first
+ * error otherwise. Reserves @num_fences on each GEM object after locking it.
+ *
+ * Returns: -EALREADY when object is already locked, -ENOMEM when memory
+ * allocation failed and zero for success.
+ */
+int drm_exec_prepare_array(struct drm_exec *exec,
+			   struct drm_gem_object **objects,

drivers/gpu/drm/drm_exec.c line 267

Quoted 10 lines
drivers/gpu/drm/drm_exec.c new file +10 -0
+			   unsigned int num_objects,
+			   unsigned int num_fences)
+{
+	int ret;
+
+	drm_exec_while_not_all_locked(exec) {
+		for (unsigned int i = 0; i < num_objects; ++i) {
+			ret = drm_exec_prepare_obj(exec, objects[i],
+						   num_fences);
+			drm_exec_break_on_contention(exec);

I had a hard time understanding what the intent was here: we do want the locking to keep going on contention (reset and retry), but we need to break out of the inner loop for this to happen, which is what this drm_exec_break_on_contention() is doing. My misunderstanding was coming from the fact I was expecting drm_exec_break_on_contention() to stop the process of preparing objects. Maybe it's just me, but I think it'd be less confusing if we were getting rid of drm_exec_{break,continue}_on_contention and have the loop slightly adjusted:

	unsigned int obj_ptr = 0;

	drm_exec_while_not_all_locked(exec) {
		int ret;

		/* We acquired/prepared all objects, we can leave the loop now. */
		if (obj_ptr == num_objects)
			break;

		ret = drm_exec_try_prepare_obj_or_retry(exec, objects[obj_ptr++],
							num_fences);
		if (ret)
			return ret;
	}

	return 0;

Of course, this is just my personal view on this, and none of these comments should be considered as blockers, but I thought I'd share my concerns anyway.

Thanks again for your work!

Regards,

Boris

maintainer
#6 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

Quoted 90 lines · On Wed, 14 Jun 2023 14:30:53 +0200 Christian König <[email protected]>…

Am 14.06.23 um 14:23 schrieb Boris Brezillon:

Quoted 16 lines

Hi Christian,

Quoted 6 lines · On Thu, 4 May 2023 13:51:47 +0200 "Christian König"…

This adds the infrastructure for an execution context for GEM buffers which is similar to the existing TTMs execbuf util and intended to replace it in the long term.

The basic functionality is that we abstracts the necessary loop to lock many different GEM buffers with automated deadlock and duplicate handling.

As many other drivers do already, we are considering using drm_exec() for our resv locking in the PowerVR driver, so we might have more questions/comments in the coming days/weeks, but I already have a couple right now (see below).

Quoted 5 lines
Quoted 1 line

v3: drop duplicate tracking, radeon is really the only one needing that

I think we'd actually be interested in duplicate tracking. Is there any way we can make it an optional feature through some extra helpers/flags? Doesn't have to be done in this patch series, I'm just wondering if this is something we can share as well.

You can still capture the -EALREADY error and act appropriately in your driver.

For radeon it just means ignoring the error code and going ahead, but that behavior doesn't seem to be desired in most cases.

Initially I though we need to separately track how many and how often BOs are duplicated, but there is simply no use for this.

Quoted 49 lines

[...]

Quoted 20 lines

+/**
+ * DOC: Overview
+ *
+ * This component mainly abstracts the retry loop necessary for locking
+ * multiple GEM objects while preparing hardware operations (e.g. command
+ * submissions, page table updates etc..).
+ *
+ * If a contention is detected while locking a GEM object the cleanup procedure
+ * unlocks all previously locked GEM objects and locks the contended one first
+ * before locking any further objects.
+ *
+ * After an object is locked fences slots can optionally be reserved on the
+ * dma_resv object inside the GEM object.
+ *
+ * A typical usage pattern should look like this::
+ *
+ * struct drm_gem_object *obj;
+ * struct drm_exec exec;
+ * unsigned long index;
+ * int ret;

Quoted 8 lines

+ *
+ * drm_exec_init(&exec, true);
+ * drm_exec_while_not_all_locked(&exec) {
+ * ret = drm_exec_prepare_obj(&exec, boA, 1);
+ * drm_exec_continue_on_contention(&exec);
+ * if (ret)
+ * goto error;
+ *

Have you considered defining a drm_exec_try_prepare_obj_or_retry() combining drm_exec_prepare_obj() and drm_exec_continue_on_contention()?

#define drm_exec_try_prepare_obj_or_retry(exec, obj, num_fences) \
         ({ \
                 int __ret = drm_exec_prepare_obj(exec, bo, num_fences); \
                 if (unlikely(drm_exec_is_contended(exec))) \
                         continue; \
                 __ret; \
         })

This way the following pattern

		ret = drm_exec_prepare_obj(&exec, boA, 1);
		drm_exec_continue_on_contention(&exec);
		if (ret)
			goto error;
Quoted 5 lines

can be turned into something more conventional:

		ret = drm_exec_try_prepare_obj_or_retry(&exec, boA, 1);
		if (ret)
			goto error;  

Yeah, I was considering that as well. But then abandoned it as to complicated.

Quoted 8 lines

I really need to find some time to work on that anyway.

Quoted 4 lines

I guess we could even add static checks to make sure drm_exec_try_prepare_obj() is called inside a drm_exec_while_not_all_locked() loop.

Interesting idea, but how would somebody do that?

There are probably better/cleaner ways, but the below diff seems to catch cases where drm_exec_try_prepare_obj() is called in a context where break/continue are allowed, but that's not inside a drm_exec_while_not_all_locked() section.

What's missing though is a way to detect when it's called from an inner loop.

---

include/drm/drm_exec.h +22 -1
index 7c7481ed088a..1f4e0e1a7783 100644
@@ -69,8 +69,10 @@ struct drm_exec {
  *
  * At the beginning of the loop it is guaranteed that no GEM object is locked.
  */
+#define __in_drm_exec_while_not_all_locked false
 #define drm_exec_while_not_all_locked(exec)    \
-       while (drm_exec_cleanup(exec))
+       for (const bool __in_drm_exec_while_not_all_locked = true; \
+            drm_exec_cleanup(exec); )
 
 /**
  * drm_exec_continue_on_contention - continue the loop when we need to cleanup
@@ -83,6 +85,25 @@ struct drm_exec {
        if (unlikely(drm_exec_is_contended(exec)))      \
                continue
 
+/**
+ * drm_exec_try_prepare_obj - Try prepare an object and retry on contention
+ * @exec: drm_exec object
+ * @obj: GEM object to prepare
+ * @num_fence: number of fence slots to reserve
+ *
+ * Wrapper around drm_exec_prepare_obj() that automatically retries on
+ * contention by going back to the head of the drm_exec_while_not_all_locked()
+ * loop.
+ */
+#define drm_exec_try_prepare_obj(exec, obj, num_fences) \
+       ({ \
+               int __ret = drm_exec_prepare_obj(exec, obj, num_fences); \
+               static_assert(__in_drm_exec_while_not_all_locked == true); \
+               if (unlikely(drm_exec_is_contended(exec))) \
+                       continue; \
+               __ret; \
+       })
+
 /**
  * drm_exec_break_on_contention - break a subordinal loop on contention
  * @exec: drm_exec object
maintainer
#7 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

+Matthew who's been using drm_exec in Xe if I'm correct.

Hello Christian,

Quoted 86 lines · On Wed, 14 Jun 2023 15:02:52 +0200 Boris Brezillon…
Quoted 83 lines · On Wed, 14 Jun 2023 14:30:53 +0200 Christian König <[email protected]>…

Am 14.06.23 um 14:23 schrieb Boris Brezillon:

Quoted 16 lines

Hi Christian,

Quoted 6 lines · On Thu, 4 May 2023 13:51:47 +0200 "Christian König"…

This adds the infrastructure for an execution context for GEM buffers which is similar to the existing TTMs execbuf util and intended to replace it in the long term.

The basic functionality is that we abstracts the necessary loop to lock many different GEM buffers with automated deadlock and duplicate handling.

As many other drivers do already, we are considering using drm_exec() for our resv locking in the PowerVR driver, so we might have more questions/comments in the coming days/weeks, but I already have a couple right now (see below).

Quoted 5 lines
Quoted 1 line

v3: drop duplicate tracking, radeon is really the only one needing that

I think we'd actually be interested in duplicate tracking. Is there any way we can make it an optional feature through some extra helpers/flags? Doesn't have to be done in this patch series, I'm just wondering if this is something we can share as well.

You can still capture the -EALREADY error and act appropriately in your driver.

For radeon it just means ignoring the error code and going ahead, but that behavior doesn't seem to be desired in most cases.

Initially I though we need to separately track how many and how often BOs are duplicated, but there is simply no use for this.

Quoted 51 lines

[...]

Quoted 20 lines

+/**
+ * DOC: Overview
+ *
+ * This component mainly abstracts the retry loop necessary for locking
+ * multiple GEM objects while preparing hardware operations (e.g. command
+ * submissions, page table updates etc..).
+ *
+ * If a contention is detected while locking a GEM object the cleanup procedure
+ * unlocks all previously locked GEM objects and locks the contended one first
+ * before locking any further objects.
+ *
+ * After an object is locked fences slots can optionally be reserved on the
+ * dma_resv object inside the GEM object.
+ *
+ * A typical usage pattern should look like this::
+ *
+ * struct drm_gem_object *obj;
+ * struct drm_exec exec;
+ * unsigned long index;
+ * int ret;

Quoted 8 lines

+ *
+ * drm_exec_init(&exec, true);
+ * drm_exec_while_not_all_locked(&exec) {
+ * ret = drm_exec_prepare_obj(&exec, boA, 1);
+ * drm_exec_continue_on_contention(&exec);
+ * if (ret)
+ * goto error;
+ *

Have you considered defining a drm_exec_try_prepare_obj_or_retry() combining drm_exec_prepare_obj() and drm_exec_continue_on_contention()?

#define drm_exec_try_prepare_obj_or_retry(exec, obj, num_fences) \
         ({ \
                 int __ret = drm_exec_prepare_obj(exec, bo, num_fences); \
                 if (unlikely(drm_exec_is_contended(exec))) \
                         continue; \
                 __ret; \
         })

This way the following pattern

		ret = drm_exec_prepare_obj(&exec, boA, 1);
		drm_exec_continue_on_contention(&exec);
		if (ret)
			goto error;

can be turned into something more conventional:

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
		ret = drm_exec_try_prepare_obj_or_retry(&exec, boA, 1);
		if (ret)
			goto error;    

Yeah, I was considering that as well. But then abandoned it as to complicated.

I really need to find some time to work on that anyway.

I've been playing with drm_exec for a couple weeks now, and I wanted to share something I hacked to try and make the API simpler and more robust against misuse (see the below diff, which is a slightly adjusted version of your work).

In this version, the user is no longer in control of the retry loop. Instead, it provides an expression (a call to a sub-function) to be re-evaluated each time a contention is detected. IMHO, this makes the 'prepare-objs' functions easier to apprehend, and avoids any mistake like calling drm_exec_continue_on_contention() in an inner loop, or breaking out of the drm_exec_while_all_locked() loop unintentionally.

It also makes the internal management a bit simpler, since we no longer call drm_exec_cleanup() on the first attempt, and can thus get rid of the DRM_EXEC_DUMMY trick.

In the below diff, I also re-introduced native support for duplicates as an opt-in, so we don't have to do things like:

	ret = drm_exec_prepare_obj(exec, obj, num_fences);
	if (ret == -EALREADY)
		ret = dma_resv_reserve_fences(obj->resv, num_fences);
	if (ret)
		return ret;

and can just do:

	ret = drm_exec_prepare_obj(exec, obj, num_fences);
	if (ret)
		return;

Of course drivers can open-code a wrapper doing the same thing, but given at least pvr and radeon need this, it'd be nice if the core could support it natively.

That's mostly it. Just wanted to share what I had in case you're interested. If not, that's fine too.

Regards,

Boris

---
 Documentation/gpu/drm-mm.rst |  12 ++
 drivers/gpu/drm/Kconfig      |   6 +
 drivers/gpu/drm/Makefile     |   2 +
 drivers/gpu/drm/drm_exec.c   | 274 +++++++++++++++++++++++++++++++++++
 include/drm/drm_exec.h       | 130 +++++++++++++++++
 5 files changed, 424 insertions(+)
 create mode 100644 drivers/gpu/drm/drm_exec.c
 create mode 100644 include/drm/drm_exec.h
Documentation/gpu/drm-mm.rst +12 -0
index fe40ee686f6e..c9f120cfe730 100644
@@ -524,6 +524,18 @@ DRM Sync Objects
 .. kernel-doc:: drivers/gpu/drm/drm_syncobj.c
    :export:
 
+DRM Execution context
+=====================
+
+.. kernel-doc:: drivers/gpu/drm/drm_exec.c
+   :doc: Overview
+
+.. kernel-doc:: include/drm/drm_exec.h
+   :internal:
+
+.. kernel-doc:: drivers/gpu/drm/drm_exec.c
+   :export:
+
 GPU Scheduler
 =============
 
drivers/gpu/drm/Kconfig +6 -0
index 76991720637c..01a38fcdb1c4 100644
@@ -194,6 +194,12 @@ config DRM_TTM
 	  GPU memory types. Will be enabled automatically if a device driver
 	  uses it.
 
+config DRM_EXEC
+	tristate
+	depends on DRM
+	help
+	  Execution context for command submissions
+
 config DRM_BUDDY
 	tristate
 	depends on DRM
drivers/gpu/drm/Makefile +2 -0
index 1873f64db171..18a02eaf2d49 100644
@@ -79,6 +79,8 @@ obj-$(CONFIG_DRM_PANEL_ORIENTATION_QUIRKS) += drm_panel_orientation_quirks.o
 #
 # Memory-management helpers
 #
+#
+obj-$(CONFIG_DRM_EXEC) += drm_exec.o
 
 obj-$(CONFIG_DRM_BUDDY) += drm_buddy.o
 
drivers/gpu/drm/drm_exec.c new file +274 -0

274 lines not on this page · raw message

include/drm/drm_exec.h new file +130 -0

130 lines not on this page · raw message

#8 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

Quoted 94 lines · Boris Brezillon

+Matthew who's been using drm_exec in Xe if I'm correct.

Hello Christian,

Quoted 87 lines · On Wed, 14 Jun 2023 15:02:52 +0200 Boris Brezillon…
Quoted 84 lines · On Wed, 14 Jun 2023 14:30:53 +0200 Christian König <[email protected]>…

Am 14.06.23 um 14:23 schrieb Boris Brezillon:

Quoted 16 lines

Hi Christian,

Quoted 6 lines · On Thu, 4 May 2023 13:51:47 +0200 "Christian König"…

This adds the infrastructure for an execution context for GEM buffers which is similar to the existing TTMs execbuf util and intended to replace it in the long term.

The basic functionality is that we abstracts the necessary loop to lock many different GEM buffers with automated deadlock and duplicate handling.

As many other drivers do already, we are considering using drm_exec() for our resv locking in the PowerVR driver, so we might have more questions/comments in the coming days/weeks, but I already have a couple right now (see below).

Quoted 5 lines
Quoted 1 line

v3: drop duplicate tracking, radeon is really the only one needing that

I think we'd actually be interested in duplicate tracking. Is there any way we can make it an optional feature through some extra helpers/flags? Doesn't have to be done in this patch series, I'm just wondering if this is something we can share as well.

You can still capture the -EALREADY error and act appropriately in your driver.

For radeon it just means ignoring the error code and going ahead, but that behavior doesn't seem to be desired in most cases.

Initially I though we need to separately track how many and how often BOs are duplicated, but there is simply no use for this.

Quoted 53 lines

[...]

Quoted 20 lines

+/**
+ * DOC: Overview
+ *
+ * This component mainly abstracts the retry loop necessary for locking
+ * multiple GEM objects while preparing hardware operations (e.g. command
+ * submissions, page table updates etc..).
+ *
+ * If a contention is detected while locking a GEM object the cleanup procedure
+ * unlocks all previously locked GEM objects and locks the contended one first
+ * before locking any further objects.
+ *
+ * After an object is locked fences slots can optionally be reserved on the
+ * dma_resv object inside the GEM object.
+ *
+ * A typical usage pattern should look like this::
+ *
+ * struct drm_gem_object *obj;
+ * struct drm_exec exec;
+ * unsigned long index;
+ * int ret;

Quoted 8 lines

+ *
+ * drm_exec_init(&exec, true);
+ * drm_exec_while_not_all_locked(&exec) {
+ * ret = drm_exec_prepare_obj(&exec, boA, 1);
+ * drm_exec_continue_on_contention(&exec);
+ * if (ret)
+ * goto error;
+ *

Have you considered defining a drm_exec_try_prepare_obj_or_retry() combining drm_exec_prepare_obj() and drm_exec_continue_on_contention()?

#define drm_exec_try_prepare_obj_or_retry(exec, obj, num_fences) \
          ({ \
                  int __ret = drm_exec_prepare_obj(exec, bo, num_fences); \
                  if (unlikely(drm_exec_is_contended(exec))) \
                          continue; \
                  __ret; \
          })

This way the following pattern

		ret = drm_exec_prepare_obj(&exec, boA, 1);
		drm_exec_continue_on_contention(&exec);
		if (ret)
			goto error;

can be turned into something more conventional:

		ret = drm_exec_try_prepare_obj_or_retry(&exec, boA, 1);
		if (ret)
			goto error;
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

Yeah, I was considering that as well. But then abandoned it as to complicated.

I really need to find some time to work on that anyway.

I've been playing with drm_exec for a couple weeks now, and I wanted to share something I hacked to try and make the API simpler and more robust against misuse (see the below diff, which is a slightly adjusted version of your work).

It would be good if we could have someone taking charge of this series and address all review comments, I see some of my comments getting lost, we have multiple submitters and I can't find a dri-devel patchwork entry for this. Anyway some comments below.

Quoted 8 lines

In this version, the user is no longer in control of the retry loop. Instead, it provides an expression (a call to a sub-function) to be re-evaluated each time a contention is detected. IMHO, this makes the 'prepare-objs' functions easier to apprehend, and avoids any mistake like calling drm_exec_continue_on_contention() in an inner loop, or breaking out of the drm_exec_while_all_locked() loop unintentionally.

In i915 we've had a very similar helper to this, and while I agree this newer version would probably help make code cleaner, but OTOH there also are some places where the short drm_exec_while_all_locked() -likeblock don't really motivate a separate function. Porting i915 to the current version will take some work, For  the xe driver both versions would work fine.

Some additional review comments not related to the interface change below:

Quoted 228 lines

It also makes the internal management a bit simpler, since we no longer call drm_exec_cleanup() on the first attempt, and can thus get rid of the DRM_EXEC_DUMMY trick.

In the below diff, I also re-introduced native support for duplicates as an opt-in, so we don't have to do things like:

	ret = drm_exec_prepare_obj(exec, obj, num_fences);
	if (ret == -EALREADY)
		ret = dma_resv_reserve_fences(obj->resv, num_fences);
	if (ret)
		return ret;

and can just do:

	ret = drm_exec_prepare_obj(exec, obj, num_fences);
	if (ret)
		return;

Of course drivers can open-code a wrapper doing the same thing, but given at least pvr and radeon need this, it'd be nice if the core could support it natively.

That's mostly it. Just wanted to share what I had in case you're interested. If not, that's fine too.

Regards,

Boris

---
  Documentation/gpu/drm-mm.rst |  12 ++
  drivers/gpu/drm/Kconfig      |   6 +
  drivers/gpu/drm/Makefile     |   2 +
  drivers/gpu/drm/drm_exec.c   | 274 +++++++++++++++++++++++++++++++++++
  include/drm/drm_exec.h       | 130 +++++++++++++++++
  5 files changed, 424 insertions(+)

create mode 100644 drivers/gpu/drm/drm_exec.c
create mode 100644 include/drm/drm_exec.h

Documentation/gpu/drm-mm.rst +12 -0
index fe40ee686f6e..c9f120cfe730 100644
@@ -524,6 +524,18 @@ DRM Sync Objects
  .. kernel-doc:: drivers/gpu/drm/drm_syncobj.c
     :export:
 
+DRM Execution context
+=====================
+
+.. kernel-doc:: drivers/gpu/drm/drm_exec.c
+   :doc: Overview
+
+.. kernel-doc:: include/drm/drm_exec.h
+   :internal:
+
+.. kernel-doc:: drivers/gpu/drm/drm_exec.c
+   :export:
+
  GPU Scheduler
  =============
 
drivers/gpu/drm/Kconfig +6 -0
index 76991720637c..01a38fcdb1c4 100644
@@ -194,6 +194,12 @@ config DRM_TTM
  	  GPU memory types. Will be enabled automatically if a device driver
  	  uses it.
 
+config DRM_EXEC
+	tristate
+	depends on DRM
+	help
+	  Execution context for command submissions
+
  config DRM_BUDDY
  	tristate
  	depends on DRM
drivers/gpu/drm/Makefile +2 -0
index 1873f64db171..18a02eaf2d49 100644
@@ -79,6 +79,8 @@ obj-$(CONFIG_DRM_PANEL_ORIENTATION_QUIRKS) += drm_panel_orientation_quirks.o
  #
  # Memory-management helpers
  #
+#
+obj-$(CONFIG_DRM_EXEC) += drm_exec.o
 
  obj-$(CONFIG_DRM_BUDDY) += drm_buddy.o
 
drivers/gpu/drm/drm_exec.c new file +129 -0
new file mode 100644
index 000000000000..e0ad1a3e1610
@@ -0,0 +1,274 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+
+#include <drm/drm_exec.h>
+#include <drm/drm_gem.h>
+#include <linux/dma-resv.h>
+
+/**
+ * DOC: Overview
+ *
+ * This component mainly abstracts the retry loop necessary for locking
+ * multiple GEM objects while preparing hardware operations (e.g. command
+ * submissions, page table updates etc..).
+ *
+ * If a contention is detected while locking a GEM object the cleanup procedure
+ * unlocks all previously locked GEM objects and locks the contended one first
+ * before locking any further objects.
+ *
+ * After an object is locked fences slots can optionally be reserved on the
+ * dma_resv object inside the GEM object.
+ *
+ * A typical usage pattern should look like this::
+ *
+ * int prepare_objs_func(struct drm_exec *exec, ...)
+ * {
+ *	struct drm_gem_object *boA, *boB;
+ * 	int ret;
+ *
+ *	<retrieve boA and boB here>
+ *
+ *	ret = drm_exec_prepare_obj(&exec, boA, 1);
+ *	if (ret)
+ *		return ret;
+ *
+ *	ret = drm_exec_prepare_obj(&exec, boB, 1);
+ *	if (ret)
+ *		return ret;
+ *
+ * 	return 0;
+ * }
+ *
+ * int some_func()
+ * {
+ *	struct drm_exec exec;
+ *	unsigned long index;
+ *	int ret;
+ *
+ *	drm_exec_init(&exec, true);
+ *	ret = drm_exec_until_all_locked(&exec, prepare_objs_func(&exec, ...));
+ *	if (ret)
+ *		goto error;
+ *
+ *	drm_exec_for_each_locked_object(&exec, index, obj) {
+ *		dma_resv_add_fence(obj->resv, fence, DMA_RESV_USAGE_READ);
+ *		...
+ *	}
+ *	drm_exec_fini(&exec);
+ *
+ * See struct dma_exec for more details.
+ */
+
+/* Unlock all objects and drop references */
+static void drm_exec_unlock_all(struct drm_exec *exec)
+{
+	struct drm_gem_object *obj;
+	unsigned long index;
+
+	drm_exec_for_each_locked_object(exec, index, obj) {
+		dma_resv_unlock(obj->resv);
+		drm_gem_object_put(obj);
+	}
+
+	drm_gem_object_put(exec->prelocked);
+	exec->prelocked = NULL;
+}
+
+/**
+ * drm_exec_init - initialize a drm_exec object
+ * @exec: the drm_exec object to initialize
+ * @interruptible: if locks should be acquired interruptible
+ *
+ * Initialize the object and make sure that we can track locked objects.
+ */
+void drm_exec_init(struct drm_exec *exec, u32 flags)
+{
+	exec->flags = flags;
+	exec->objects = kmalloc(PAGE_SIZE, GFP_KERNEL);
+
+	/* If allocation here fails, just delay that till the first use */
+	exec->max_objects = exec->objects ? PAGE_SIZE / sizeof(void *) : 0;
+	exec->num_objects = 0;
+	exec->contended = NULL;
+	exec->prelocked = NULL;
+	ww_acquire_init(&exec->ticket, &reservation_ww_class);
+}
+EXPORT_SYMBOL(drm_exec_init);
+
+/**
+ * drm_exec_fini - finalize a drm_exec object
+ * @exec: the drm_exec object to finalize
+ *
+ * Unlock all locked objects, drop the references to objects and free all memory
+ * used for tracking the state.
+ */
+void drm_exec_fini(struct drm_exec *exec)
+{
+	drm_exec_unlock_all(exec);
+	kvfree(exec->objects);
+	if (exec->contended)
+		drm_gem_object_put(exec->contended);
+	ww_acquire_fini(&exec->ticket);
+}
+EXPORT_SYMBOL(drm_exec_fini);
+
+/**
+ * drm_exec_reset - reset a drm_exec object after a contention
+ * @exec: the drm_exec object to reset
+ *
+ * Unlock all locked objects and resets the number of objects locked.
+ */
+void drm_exec_reset(struct drm_exec *exec)
+{
+	WARN_ON(!exec->contended);
+	drm_exec_unlock_all(exec);
+	exec->num_objects = 0;
+}
+EXPORT_SYMBOL(drm_exec_reset);
+
+/* Track the locked object in the array */
+static int drm_exec_obj_locked(struct drm_exec *exec,

drivers/gpu/drm/drm_exec.c line 139

Quoted 10 lines
drivers/gpu/drm/drm_exec.c new file +10 -0
+			       struct drm_gem_object *obj)
+{
+	if (unlikely(exec->num_objects == exec->max_objects)) {
+		size_t size = exec->max_objects * sizeof(void *);
+		void *tmp;
+
+		tmp = kvrealloc(exec->objects, size, size + PAGE_SIZE,
+				GFP_KERNEL);
+		if (!tmp)
+			return -ENOMEM;

Sometimes you need to just temporarily lock an object and then unlock it again if it goes out of scope before reaching the end of _until_all_locked(). In that case you might need to remove a lock from the array. I *think* for all use-cases in i915 it would suffice to take a snapshot of num_objects, and unlock everything above that, having exec->objects behave like a stack, but was ever a list considered instead of a realloced array?

Quoted 18 lines
drivers/gpu/drm/drm_exec.c new file +18 -0
+
+		exec->objects = tmp;
+		exec->max_objects += PAGE_SIZE / sizeof(void *);
+	}
+	drm_gem_object_get(obj);
+	exec->objects[exec->num_objects++] = obj;
+
+	return 0;
+}
+
+/* Make sure the contended object is locked first */
+static int drm_exec_lock_contended(struct drm_exec *exec)
+{
+	struct drm_gem_object *obj = exec->contended;
+	int ret;
+
+	if (likely(!obj))
+		return 0;

drivers/gpu/drm/drm_exec.c line 167

Quoted 10 lines
drivers/gpu/drm/drm_exec.c new file +10 -0
+
+	if (exec->flags & DRM_EXEC_FLAG_INTERRUPTIBLE) {
+		ret = dma_resv_lock_slow_interruptible(obj->resv,
+						       &exec->ticket);
+		if (unlikely(ret))
+			goto error_dropref;
+	} else {
+		dma_resv_lock_slow(obj->resv, &exec->ticket);
+	}
+

Sometimes you want to just drop the contended lock after the above relaxation. (Eviction would be one example), and not add as prelocked, if the contended object goes out of scope. Eviction would in some situations be one such example, -EDEADLOCK leading to an error path where the object should otherwise be freed is another. Perhaps we could add an argument to prepare_obj() as to whether the object should be immediately put after relaxation.

Quoted 58 lines
drivers/gpu/drm/drm_exec.c new file +58 -0
+	ret = drm_exec_obj_locked(exec, obj);
+	if (unlikely(ret)) {
+		dma_resv_unlock(obj->resv);
+		goto error_dropref;
+	}
+
+	swap(exec->prelocked, obj);
+
+error_dropref:
+	/* Always cleanup the contention so that error handling can kick in */
+	drm_gem_object_put(obj);
+	exec->contended = NULL;
+	return ret;
+}
+
+/**
+ * drm_exec_prepare_obj - prepare a GEM object for use
+ * @exec: the drm_exec object with the state
+ * @obj: the GEM object to prepare
+ * @num_fences: how many fences to reserve
+ *
+ * Prepare a GEM object for use by locking it and reserving fence slots. All
+ * successfully locked objects are put into the locked container.
+ *
+ * Returns: -EDEADLK if a contention is detected, -EALREADY when object is
+ * already locked, -ENOMEM when memory allocation failed and zero for success.
+ */
+int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
+			 unsigned int num_fences)
+{
+	int ret;
+
+	ret = drm_exec_lock_contended(exec);
+	if (unlikely(ret))
+		return ret;
+
+	if (exec->prelocked == obj) {
+		drm_gem_object_put(exec->prelocked);
+		exec->prelocked = NULL;
+
+		return dma_resv_reserve_fences(obj->resv, num_fences);
+	}
+
+	if (exec->flags & DRM_EXEC_FLAG_INTERRUPTIBLE)
+		ret = dma_resv_lock_interruptible(obj->resv, &exec->ticket);
+	else
+		ret = dma_resv_lock(obj->resv, &exec->ticket);
+
+	if (unlikely(ret == -EDEADLK)) {
+		drm_gem_object_get(obj);
+		exec->contended = obj;
+		return -EDEADLK;
+	}
+
+	if (unlikely(ret == -EALREADY &&
+	    (exec->flags & DRM_EXEC_FLAG_ALLOW_DUPLICATES)))
+		goto reserve_fences;
+

drivers/gpu/drm/drm_exec.c line 235

Quoted 10 lines
drivers/gpu/drm/drm_exec.c new file +10 -0
+	if (unlikely(ret))
+		return ret;
+
+	ret = drm_exec_obj_locked(exec, obj);
+	if (ret)
+		goto error_unlock;
+
+reserve_fences:
+	/* Keep locked when reserving fences fails */
+	return dma_resv_reserve_fences(obj->resv, num_fences);

Ugh, what is the use-case for keeping things locked here? How would a caller tell the difference between an error where everything is locked and nothing is locked? IMO, we should unlock on error here. If there indeed is a use-case we should add a separate function for reserving fences for all locked objects, rather than returning sometimes locked on error sometime not.

Thanks,

Thomas

Quoted 175 lines
drivers/gpu/drm/drm_exec.c new file +39 -0
+
+error_unlock:
+	dma_resv_unlock(obj->resv);
+	return ret;
+}
+EXPORT_SYMBOL(drm_exec_prepare_obj);
+
+/**
+ * drm_exec_prepare_array - helper to prepare an array of objects
+ * @exec: the drm_exec object with the state
+ * @objects: array of GEM object to prepare
+ * @num_objects: number of GEM objects in the array
+ * @num_fences: number of fences to reserve on each GEM object
+ *
+ * Prepares all GEM objects in an array, handles contention but aports on first
+ * error otherwise. Reserves @num_fences on each GEM object after locking it.
+ *
+ * Returns: -EALREADY when object is already locked, -ENOMEM when memory
+ * allocation failed and zero for success.
+ */
+int drm_exec_prepare_array(struct drm_exec *exec,
+			   struct drm_gem_object **objects,
+			   unsigned int num_objects,
+			   unsigned int num_fences)
+{
+	int ret;
+
+	for (unsigned int i = 0; i < num_objects; ++i) {
+		ret = drm_exec_prepare_obj(exec, objects[i], num_fences);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_exec_prepare_array);
+
+MODULE_DESCRIPTION("DRM execution context");
+MODULE_LICENSE("Dual MIT/GPL");
include/drm/drm_exec.h new file +130 -0
new file mode 100644
index 000000000000..b1a5da0509c1
@@ -0,0 +1,130 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+
+#ifndef __DRM_EXEC_H__
+#define __DRM_EXEC_H__
+
+#include <linux/ww_mutex.h>
+
+struct drm_gem_object;
+
+/**
+ * enum drm_exec_flags - Execution context flags
+ */
+enum drm_exec_flags {
+	/**
+	 * DRM_EXEC_FLAG_INTERRUPTIBLE: Set to true to use interruptible locking
+	 * functions.
+	 */
+	DRM_EXEC_FLAG_INTERRUPTIBLE = BIT(0),
+
+	/**
+	 * DRM_EXEC_FLAG_ALLOW_DUPLICATES: Set to true to allow EALREADY errors.
+	 */
+	DRM_EXEC_FLAG_ALLOW_DUPLICATES = BIT(1),
+};
+
+/**
+ * struct drm_exec - Execution context
+ */
+struct drm_exec {
+	/**
+	 * @flags: Combinations of DRM_EXEC_FLAG_* flags.
+	 */
+	u32 flags;
+
+	/**
+	 * @ticket: WW ticket used for acquiring locks
+	 */
+	struct ww_acquire_ctx	ticket;
+
+	/**
+	 * @num_objects: number of objects locked
+	 */
+	unsigned int		num_objects;
+
+	/**
+	 * @max_objects: maximum objects in array
+	 */
+	unsigned int		max_objects;
+
+	/**
+	 * @objects: array of the locked objects
+	 */
+	struct drm_gem_object	**objects;
+
+	/**
+	 * @contended: contended GEM object we backed off for
+	 */
+	struct drm_gem_object	*contended;
+
+	/**
+	 * @prelocked: already locked GEM object due to contention
+	 */
+	struct drm_gem_object *prelocked;
+};
+
+/**
+ * drm_exec_for_each_locked_object - iterate over all the locked objects
+ * @exec: drm_exec object
+ * @index: unsigned long index for the iteration
+ * @obj: the current GEM object
+ *
+ * Iterate over all the locked GEM objects inside the drm_exec object.
+ */
+#define drm_exec_for_each_locked_object(exec, index, obj)	\
+	for (index = 0, obj = (exec)->objects[0];		\
+	     index < (exec)->num_objects;			\
+	     ++index, obj = (exec)->objects[index])
+
+/**
+ * drm_exec_until_all_locked - retry objects preparation until all objects
+ * are locked
+ * @exec: drm_exec object
+ * @expr: expression to be evaluated on each attempt
+ *
+ * This helper tries to prepare objects and if a deadlock is detected,
+ * rollbacks and retries.
+ *
+ * @expr is typically a function that tries to prepare objects using
+ * drm_exec_prepare_obj().
+ *
+ * If we take drm_exec_prepare_array() as an example, you should do:
+ *
+ *	ret = drm_exec_until_all_locked(exec,
+ *					drm_exec_prepare_array(exec,
+ *							       objs,
+ *							       num_objs,
+ *							       num_fences));
+ *	if (ret)
+ *		goto error_path;
+ *
+ *	...
+ *
+ * Returns: 0 on success, a negative error code on failure.
+ */
+#define drm_exec_until_all_locked(exec, expr)		\
+	({						\
+		__label__ retry;			\
+		int __ret;				\
+retry:							\
+		__ret = expr;				\
+		if ((exec)->contended) {		\
+			WARN_ON(__ret != -EDEADLK);	\
+			drm_exec_reset(exec);		\
+			goto retry;			\
+		}					\
+		ww_acquire_done(&(exec)->ticket);	\
+		__ret;					\
+	})
+
+void drm_exec_init(struct drm_exec *exec, u32 flags);
+void drm_exec_fini(struct drm_exec *exec);
+void drm_exec_reset(struct drm_exec *exec);
+int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
+			 unsigned int num_fences);
+int drm_exec_prepare_array(struct drm_exec *exec,
+			   struct drm_gem_object **objects,
+			   unsigned int num_objects,
+			   unsigned int num_fences);
+
+#endif
maintainer
#9 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

Hi guys,

Am 19.06.23 um 10:59 schrieb Thomas Hellström (Intel):

Quoted 9 lines

[SNIP]

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

I really need to find some time to work on that anyway.

I've been playing with drm_exec for a couple weeks now, and I wanted to share something I hacked to try and make the API simpler and more robust against misuse (see the below diff, which is a slightly adjusted version of your work).

It would be good if we could have someone taking charge of this series and address all review comments, I see some of my comments getting lost, we have multiple submitters and I can't find a dri-devel patchwork entry for this. Anyway some comments below.

I can try to find some time for the series this week (As long as nobody comes along and has any burning roof).

Quoted 10 lines
Quoted 9 lines

In this version, the user is no longer in control of the retry loop. Instead, it provides an expression (a call to a sub-function) to be re-evaluated each time a contention is detected. IMHO, this makes the 'prepare-objs' functions easier to apprehend, and avoids any mistake like calling drm_exec_continue_on_contention() in an inner loop, or breaking out of the drm_exec_while_all_locked() loop unintentionally.

Quoted 3 lines

In i915 we've had a very similar helper to this, and while I agree this newer version would probably help make code cleaner, but OTOH there also are some places where the short drm_exec_while_all_locked() -likeblock don't really motivate a separate function. Porting i915 to
the current version will take some work, For  the xe driver both versions would work fine.

Yeah, this is actually what my first version of this looked like. But I abandoned that approach because we have a lot of cases were we just quickly want to lock a few GEM objects and don't want the extra overhead of putting all the state into some bag to forward it to a function.

Quoted 238 lines

Some additional review comments not related to the interface change below:

Quoted 235 lines

It also makes the internal management a bit simpler, since we no longer call drm_exec_cleanup() on the first attempt, and can thus get rid of the DRM_EXEC_DUMMY trick.

In the below diff, I also re-introduced native support for duplicates as an opt-in, so we don't have to do things like:

    ret = drm_exec_prepare_obj(exec, obj, num_fences); if (ret == -EALREADY)
        ret = dma_resv_reserve_fences(obj->resv, num_fences); if (ret)
        return ret;

and can just do:

    ret = drm_exec_prepare_obj(exec, obj, num_fences); if (ret)
        return;

Of course drivers can open-code a wrapper doing the same thing, but given at least pvr and radeon need this, it'd be nice if the core could support it natively.

That's mostly it. Just wanted to share what I had in case you're interested. If not, that's fine too.

Regards,

Boris
---
  Documentation/gpu/drm-mm.rst |  12 ++ drivers/gpu/drm/Kconfig      |   6 + drivers/gpu/drm/Makefile     |   2 + drivers/gpu/drm/drm_exec.c   | 274 +++++++++++++++++++++++++++++++++++ include/drm/drm_exec.h       | 130 +++++++++++++++++

  5 files changed, 424 insertions(+)

  create mode 100644 drivers/gpu/drm/drm_exec.c
  create mode 100644 include/drm/drm_exec.h

Documentation/gpu/drm-mm.rst +0 -0
index fe40ee686f6e..c9f120cfe730 100644
@@ -524,6 +524,18 @@ DRM Sync Objects

  .. kernel-doc:: drivers/gpu/drm/drm_syncobj.c
     :export:
  +DRM Execution context
+=====================

Documentation/gpu/drm-mm.rst +10 -0
+
+.. kernel-doc:: drivers/gpu/drm/drm_exec.c
+   :doc: Overview
+
+.. kernel-doc:: include/drm/drm_exec.h
+   :internal:
+
+.. kernel-doc:: drivers/gpu/drm/drm_exec.c
+   :export:
+

  GPU Scheduler
  =============
  diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig index 76991720637c..01a38fcdb1c4 100644

drivers/gpu/drm/Kconfig +0 -0
@@ -194,6 +194,12 @@ config DRM_TTM

        GPU memory types. Will be enabled automatically if a device driver uses it.
  +config DRM_EXEC
+    tristate
+    depends on DRM
+    help
+      Execution context for command submissions
+
  config DRM_BUDDY
      tristate
      depends on DRM

drivers/gpu/drm/Makefile +0 -0
index 1873f64db171..18a02eaf2d49 100644
@@ -79,6 +79,8 @@ obj-$(CONFIG_DRM_PANEL_ORIENTATION_QUIRKS) += 

drm_panel_orientation_quirks.o
  #
  # Memory-management helpers
  #

drivers/gpu/drm/Makefile +2 -0
+#
+obj-$(CONFIG_DRM_EXEC) += drm_exec.o

    obj-$(CONFIG_DRM_BUDDY) += drm_buddy.o
  diff --git a/drivers/gpu/drm/drm_exec.c b/drivers/gpu/drm/drm_exec.c new file mode 100644
index 000000000000..e0ad1a3e1610

drivers/gpu/drm/drm_exec.c new file +11 -0
@@ -0,0 +1,274 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+
+#include <drm/drm_exec.h>
+#include <drm/drm_gem.h>
+#include <linux/dma-resv.h>
+
+/**
+ * DOC: Overview
+ *
+ * This component mainly abstracts the retry loop necessary for locking
+ * multiple GEM objects while preparing hardware operations (e.g. 

command
+ * submissions, page table updates etc..).
+ *
+ * If a contention is detected while locking a GEM object the cleanup procedure
+ * unlocks all previously locked GEM objects and locks the contended one first
+ * before locking any further objects.
+ *
+ * After an object is locked fences slots can optionally be reserved on the
+ * dma_resv object inside the GEM object.
+ *
+ * A typical usage pattern should look like this::
+ *
+ * int prepare_objs_func(struct drm_exec *exec, ...)
+ * {
+ *    struct drm_gem_object *boA, *boB;
+ *     int ret;
+ *
+ *    <retrieve boA and boB here>
+ *
+ *    ret = drm_exec_prepare_obj(&exec, boA, 1);
+ *    if (ret)
+ *        return ret;
+ *
+ *    ret = drm_exec_prepare_obj(&exec, boB, 1);
+ *    if (ret)
+ *        return ret;
+ *
+ *     return 0;
+ * }
+ *
+ * int some_func()
+ * {
+ *    struct drm_exec exec;
+ *    unsigned long index;
+ *    int ret;
+ *
+ *    drm_exec_init(&exec, true);
+ *    ret = drm_exec_until_all_locked(&exec, prepare_objs_func(&exec, ...));
+ *    if (ret)
+ *        goto error;
+ *
+ *    drm_exec_for_each_locked_object(&exec, index, obj) {
+ *        dma_resv_add_fence(obj->resv, fence, DMA_RESV_USAGE_READ);
+ *        ...
+ *    }
+ *    drm_exec_fini(&exec);
+ *
+ * See struct dma_exec for more details.
+ */

drivers/gpu/drm/drm_exec.c new file +22 -0
+
+/* Unlock all objects and drop references */
+static void drm_exec_unlock_all(struct drm_exec *exec)
+{
+    struct drm_gem_object *obj;
+    unsigned long index;
+
+    drm_exec_for_each_locked_object(exec, index, obj) {
+        dma_resv_unlock(obj->resv);
+        drm_gem_object_put(obj);
+    }
+
+    drm_gem_object_put(exec->prelocked);
+    exec->prelocked = NULL;
+}
+
+/**
+ * drm_exec_init - initialize a drm_exec object
+ * @exec: the drm_exec object to initialize
+ * @interruptible: if locks should be acquired interruptible
+ *
+ * Initialize the object and make sure that we can track locked 

objects.
+ */

drivers/gpu/drm/drm_exec.c new file +19 -0
+void drm_exec_init(struct drm_exec *exec, u32 flags)
+{
+    exec->flags = flags;
+    exec->objects = kmalloc(PAGE_SIZE, GFP_KERNEL);
+
+    /* If allocation here fails, just delay that till the first use */
+    exec->max_objects = exec->objects ? PAGE_SIZE / sizeof(void *) : 0;
+    exec->num_objects = 0;
+    exec->contended = NULL;
+    exec->prelocked = NULL;
+    ww_acquire_init(&exec->ticket, &reservation_ww_class);
+}
+EXPORT_SYMBOL(drm_exec_init);
+
+/**
+ * drm_exec_fini - finalize a drm_exec object
+ * @exec: the drm_exec object to finalize
+ *
+ * Unlock all locked objects, drop the references to objects and 

free all memory
+ * used for tracking the state.
+ */

drivers/gpu/drm/drm_exec.c new file +30 -0
+void drm_exec_fini(struct drm_exec *exec)
+{
+    drm_exec_unlock_all(exec);
+    kvfree(exec->objects);
+    if (exec->contended)
+        drm_gem_object_put(exec->contended);
+    ww_acquire_fini(&exec->ticket);
+}
+EXPORT_SYMBOL(drm_exec_fini);
+
+/**
+ * drm_exec_reset - reset a drm_exec object after a contention
+ * @exec: the drm_exec object to reset
+ *
+ * Unlock all locked objects and resets the number of objects locked.
+ */
+void drm_exec_reset(struct drm_exec *exec)
+{
+    WARN_ON(!exec->contended);
+    drm_exec_unlock_all(exec);
+    exec->num_objects = 0;
+}
+EXPORT_SYMBOL(drm_exec_reset);
+
+/* Track the locked object in the array */
+static int drm_exec_obj_locked(struct drm_exec *exec,
+                   struct drm_gem_object *obj)
+{
+    if (unlikely(exec->num_objects == exec->max_objects)) {
+        size_t size = exec->max_objects * sizeof(void *);
Quoted 8 lines
Quoted 6 lines

+        void *tmp;
+
+        tmp = kvrealloc(exec->objects, size, size + PAGE_SIZE, +                GFP_KERNEL);
+        if (!tmp)
+            return -ENOMEM;

Sometimes you need to just temporarily lock an object and then unlock it again if it goes out of scope before reaching the end of _until_all_locked(). In that case you might need to remove a lock from the array. I *think* for all use-cases in i915 it would suffice to take a snapshot of num_objects, and unlock everything above that, having exec->objects behave like a stack, but was ever a list considered instead of a realloced array?

Yes, the problem is that linked lists really suck regarding their cache line locality. That's why I've came up with this approach here.

What we could maybe do is to allow unlocking objects, but with the cost of linear backward searching for them in the array.

Quoted 23 lines
Quoted 23 lines
drivers/gpu/drm/drm_exec.c new file +22 -0
+
+        exec->objects = tmp;
+        exec->max_objects += PAGE_SIZE / sizeof(void *);
+    }
+    drm_gem_object_get(obj);
+    exec->objects[exec->num_objects++] = obj;
+
+    return 0;
+}
+
+/* Make sure the contended object is locked first */
+static int drm_exec_lock_contended(struct drm_exec *exec)
+{
+    struct drm_gem_object *obj = exec->contended;
+    int ret;
+
+    if (likely(!obj))
+        return 0;
+
+    if (exec->flags & DRM_EXEC_FLAG_INTERRUPTIBLE) {
+        ret = dma_resv_lock_slow_interruptible(obj->resv,
+                               &exec->ticket);
Quoted 8 lines
Quoted 6 lines

+        if (unlikely(ret))
+            goto error_dropref;
+    } else {
+        dma_resv_lock_slow(obj->resv, &exec->ticket); +    }
+

Sometimes you want to just drop the contended lock after the above relaxation. (Eviction would be one example), and not add as prelocked, if the contended object goes out of scope. Eviction would in some situations be one such example, -EDEADLOCK leading to an error path where the object should otherwise be freed is another. Perhaps we could add an argument to prepare_obj() as to whether the object should be immediately put after relaxation.

I was considering a try_prepare version as well, that should cover this use case.

Quoted 68 lines
Quoted 68 lines

+    ret = drm_exec_obj_locked(exec, obj);
+    if (unlikely(ret)) {
+        dma_resv_unlock(obj->resv);
+        goto error_dropref;
+    }

drivers/gpu/drm/drm_exec.c new file +5 -0
+
+    swap(exec->prelocked, obj);
+
+error_dropref:
+    /* Always cleanup the contention so that error handling can kick 

in */
+    drm_gem_object_put(obj);
+    exec->contended = NULL;
+    return ret;

drivers/gpu/drm/drm_exec.c new file +9 -0
+}
+
+/**
+ * drm_exec_prepare_obj - prepare a GEM object for use
+ * @exec: the drm_exec object with the state
+ * @obj: the GEM object to prepare
+ * @num_fences: how many fences to reserve
+ *
+ * Prepare a GEM object for use by locking it and reserving fence 

slots. All
+ * successfully locked objects are put into the locked container.
+ *
+ * Returns: -EDEADLK if a contention is detected, -EALREADY when object is
+ * already locked, -ENOMEM when memory allocation failed and zero for success.
+ */
+int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
+             unsigned int num_fences)

drivers/gpu/drm/drm_exec.c new file +33 -0
+{
+    int ret;
+
+    ret = drm_exec_lock_contended(exec);
+    if (unlikely(ret))
+        return ret;
+
+    if (exec->prelocked == obj) {
+        drm_gem_object_put(exec->prelocked);
+        exec->prelocked = NULL;
+
+        return dma_resv_reserve_fences(obj->resv, num_fences);
+    }
+
+    if (exec->flags & DRM_EXEC_FLAG_INTERRUPTIBLE)
+        ret = dma_resv_lock_interruptible(obj->resv, &exec->ticket);
+    else
+        ret = dma_resv_lock(obj->resv, &exec->ticket);
+
+    if (unlikely(ret == -EDEADLK)) {
+        drm_gem_object_get(obj);
+        exec->contended = obj;
+        return -EDEADLK;
+    }
+
+    if (unlikely(ret == -EALREADY &&
+        (exec->flags & DRM_EXEC_FLAG_ALLOW_DUPLICATES)))
+        goto reserve_fences;
+
+    if (unlikely(ret))
+        return ret;
+
+    ret = drm_exec_obj_locked(exec, obj);
Quoted 8 lines

drivers/gpu/drm/drm_exec.c line 235

Quoted 6 lines

+    if (ret)
+        goto error_unlock;

drivers/gpu/drm/drm_exec.c new file +4 -0
+
+reserve_fences:
+    /* Keep locked when reserving fences fails */
+    return dma_resv_reserve_fences(obj->resv, num_fences);

Ugh, what is the use-case for keeping things locked here? How would a caller tell the difference between an error where everything is locked and nothing is locked? IMO, we should unlock on error here. If there indeed is a use-case we should add a separate function for reserving fences for all locked objects, rather than returning sometimes locked on error sometime not.

We return the object locked here because it was to much churn to remove it again from the array and we are getting fully cleaned up at the end anyway.

Regards,
Christian.

Quoted 189 lines

Thanks,

Thomas

Quoted 183 lines
drivers/gpu/drm/drm_exec.c new file +15 -0
+
+error_unlock:
+    dma_resv_unlock(obj->resv);
+    return ret;
+}
+EXPORT_SYMBOL(drm_exec_prepare_obj);
+
+/**
+ * drm_exec_prepare_array - helper to prepare an array of objects
+ * @exec: the drm_exec object with the state
+ * @objects: array of GEM object to prepare
+ * @num_objects: number of GEM objects in the array
+ * @num_fences: number of fences to reserve on each GEM object
+ *
+ * Prepares all GEM objects in an array, handles contention but 

aports on first
+ * error otherwise. Reserves @num_fences on each GEM object after locking it.
+ *
+ * Returns: -EALREADY when object is already locked, -ENOMEM when memory
+ * allocation failed and zero for success.
+ */

drivers/gpu/drm/drm_exec.c new file +19 -0
+int drm_exec_prepare_array(struct drm_exec *exec,
+               struct drm_gem_object **objects,
+               unsigned int num_objects,
+               unsigned int num_fences)
+{
+    int ret;
+
+    for (unsigned int i = 0; i < num_objects; ++i) {
+        ret = drm_exec_prepare_obj(exec, objects[i], num_fences);
+        if (ret)
+            return ret;
+    }
+
+    return 0;
+}
+EXPORT_SYMBOL(drm_exec_prepare_array);
+
+MODULE_DESCRIPTION("DRM execution context");
+MODULE_LICENSE("Dual MIT/GPL");
include/drm/drm_exec.h new file +15 -0
new file mode 100644
index 000000000000..b1a5da0509c1
@@ -0,0 +1,130 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+
+#ifndef __DRM_EXEC_H__
+#define __DRM_EXEC_H__
+
+#include <linux/ww_mutex.h>
+
+struct drm_gem_object;
+
+/**
+ * enum drm_exec_flags - Execution context flags
+ */
+enum drm_exec_flags {
+    /**
+     * DRM_EXEC_FLAG_INTERRUPTIBLE: Set to true to use interruptible 

locking
+     * functions.
+     */
+    DRM_EXEC_FLAG_INTERRUPTIBLE = BIT(0),
+
+    /**
+     * DRM_EXEC_FLAG_ALLOW_DUPLICATES: Set to true to allow EALREADY errors.
+     */
+    DRM_EXEC_FLAG_ALLOW_DUPLICATES = BIT(1),

include/drm/drm_exec.h new file +44 -0
+};
+
+/**
+ * struct drm_exec - Execution context
+ */
+struct drm_exec {
+    /**
+     * @flags: Combinations of DRM_EXEC_FLAG_* flags.
+     */
+    u32 flags;
+
+    /**
+     * @ticket: WW ticket used for acquiring locks
+     */
+    struct ww_acquire_ctx    ticket;
+
+    /**
+     * @num_objects: number of objects locked
+     */
+    unsigned int        num_objects;
+
+    /**
+     * @max_objects: maximum objects in array
+     */
+    unsigned int        max_objects;
+
+    /**
+     * @objects: array of the locked objects
+     */
+    struct drm_gem_object    **objects;
+
+    /**
+     * @contended: contended GEM object we backed off for
+     */
+    struct drm_gem_object    *contended;
+
+    /**
+     * @prelocked: already locked GEM object due to contention
+     */
+    struct drm_gem_object *prelocked;
+};
+
+/**
+ * drm_exec_for_each_locked_object - iterate over all the locked 

objects
+ * @exec: drm_exec object
+ * @index: unsigned long index for the iteration
+ * @obj: the current GEM object
+ *
+ * Iterate over all the locked GEM objects inside the drm_exec object.
+ */

include/drm/drm_exec.h new file +7 -0
+#define drm_exec_for_each_locked_object(exec, index, obj)    \
+    for (index = 0, obj = (exec)->objects[0];        \
+         index < (exec)->num_objects;            \
+         ++index, obj = (exec)->objects[index])
+
+/**
+ * drm_exec_until_all_locked - retry objects preparation until all 

objects
+ * are locked
+ * @exec: drm_exec object
+ * @expr: expression to be evaluated on each attempt
+ *
+ * This helper tries to prepare objects and if a deadlock is detected,
+ * rollbacks and retries.
+ *
+ * @expr is typically a function that tries to prepare objects using
+ * drm_exec_prepare_obj().
+ *
+ * If we take drm_exec_prepare_array() as an example, you should do:
+ *
+ *    ret = drm_exec_until_all_locked(exec,
+ *                    drm_exec_prepare_array(exec,
+ *                                   objs,
+ *                                   num_objs,
+ *                                   num_fences));
+ *    if (ret)
+ *        goto error_path;
+ *
+ *    ...
+ *
+ * Returns: 0 on success, a negative error code on failure.
+ */

include/drm/drm_exec.h new file +19 -0
+#define drm_exec_until_all_locked(exec, expr)        \
+    ({                        \
+        __label__ retry;            \
+        int __ret;                \
+retry:                            \
+        __ret = expr;                \
+        if ((exec)->contended) {        \
+            WARN_ON(__ret != -EDEADLK);    \
+            drm_exec_reset(exec);        \
+            goto retry;            \
+        }                    \
+        ww_acquire_done(&(exec)->ticket);    \
+        __ret;                    \
+    })
+
+void drm_exec_init(struct drm_exec *exec, u32 flags);
+void drm_exec_fini(struct drm_exec *exec);
+void drm_exec_reset(struct drm_exec *exec);
+int drm_exec_prepare_obj(struct drm_exec *exec, struct 

drm_gem_object *obj,
+             unsigned int num_fences);

include/drm/drm_exec.h new file +6 -0
+int drm_exec_prepare_array(struct drm_exec *exec,
+               struct drm_gem_object **objects,
+               unsigned int num_objects,
+               unsigned int num_fences);
+
+#endif
#10 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

Hi!

Quoted 315 lines · Christian König

Hi guys,

Am 19.06.23 um 10:59 schrieb Thomas Hellström (Intel):

Quoted 9 lines

[SNIP]

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

I really need to find some time to work on that anyway.

I've been playing with drm_exec for a couple weeks now, and I wanted to share something I hacked to try and make the API simpler and more robust against misuse (see the below diff, which is a slightly adjusted version of your work).

It would be good if we could have someone taking charge of this series and address all review comments, I see some of my comments getting lost, we have multiple submitters and I can't find a dri-devel patchwork entry for this. Anyway some comments below.

I can try to find some time for the series this week (As long as nobody comes along and has any burning roof).

Quoted 11 lines
Quoted 9 lines

In this version, the user is no longer in control of the retry loop. Instead, it provides an expression (a call to a sub-function) to be re-evaluated each time a contention is detected. IMHO, this makes the 'prepare-objs' functions easier to apprehend, and avoids any mistake like calling drm_exec_continue_on_contention() in an inner loop, or breaking out of the drm_exec_while_all_locked() loop unintentionally.

In i915 we've had a very similar helper to this, and while I agree this newer version would probably help make code cleaner, but OTOH there also are some places where the short drm_exec_while_all_locked() -likeblock don't really motivate a separate function. Porting i915 to the current version will take some work, For  the xe driver both versions would work fine.

Yeah, this is actually what my first version of this looked like. But I abandoned that approach because we have a lot of cases were we just quickly want to lock a few GEM objects and don't want the extra overhead of putting all the state into some bag to forward it to a function.

Quoted 241 lines

Some additional review comments not related to the interface change below:

Quoted 238 lines

It also makes the internal management a bit simpler, since we no longer call drm_exec_cleanup() on the first attempt, and can thus get rid of the DRM_EXEC_DUMMY trick.

In the below diff, I also re-introduced native support for duplicates as an opt-in, so we don't have to do things like:

    ret = drm_exec_prepare_obj(exec, obj, num_fences); if (ret == -EALREADY)
        ret = dma_resv_reserve_fences(obj->resv, num_fences); if (ret)
        return ret;

and can just do:

    ret = drm_exec_prepare_obj(exec, obj, num_fences); if (ret)
        return;

Of course drivers can open-code a wrapper doing the same thing, but given at least pvr and radeon need this, it'd be nice if the core could support it natively.

That's mostly it. Just wanted to share what I had in case you're interested. If not, that's fine too.

Regards,

Boris
---
  Documentation/gpu/drm-mm.rst |  12 ++ drivers/gpu/drm/Kconfig      |   6 + drivers/gpu/drm/Makefile     |   2 + drivers/gpu/drm/drm_exec.c   | 274
+++++++++++++++++++++++++++++++++++
  include/drm/drm_exec.h       | 130 +++++++++++++++++

  5 files changed, 424 insertions(+)

  create mode 100644 drivers/gpu/drm/drm_exec.c
  create mode 100644 include/drm/drm_exec.h

Documentation/gpu/drm-mm.rst +0 -0
index fe40ee686f6e..c9f120cfe730 100644
@@ -524,6 +524,18 @@ DRM Sync Objects

  .. kernel-doc:: drivers/gpu/drm/drm_syncobj.c
     :export:
  +DRM Execution context
+=====================

Documentation/gpu/drm-mm.rst +10 -0
+
+.. kernel-doc:: drivers/gpu/drm/drm_exec.c
+   :doc: Overview
+
+.. kernel-doc:: include/drm/drm_exec.h
+   :internal:
+
+.. kernel-doc:: drivers/gpu/drm/drm_exec.c
+   :export:
+

  GPU Scheduler
  =============
  diff --git a/drivers/gpu/drm/Kconfig b/drivers/gpu/drm/Kconfig index 76991720637c..01a38fcdb1c4 100644

drivers/gpu/drm/Kconfig +0 -0
@@ -194,6 +194,12 @@ config DRM_TTM

        GPU memory types. Will be enabled automatically if a device driver uses it.
  +config DRM_EXEC
+    tristate
+    depends on DRM
+    help
+      Execution context for command submissions
+
  config DRM_BUDDY
      tristate
      depends on DRM

drivers/gpu/drm/Makefile +0 -0
index 1873f64db171..18a02eaf2d49 100644
@@ -79,6 +79,8 @@ obj-$(CONFIG_DRM_PANEL_ORIENTATION_QUIRKS) += 

drm_panel_orientation_quirks.o
  #
  # Memory-management helpers
  #

drivers/gpu/drm/Makefile +2 -0
+#
+obj-$(CONFIG_DRM_EXEC) += drm_exec.o

    obj-$(CONFIG_DRM_BUDDY) += drm_buddy.o
  diff --git a/drivers/gpu/drm/drm_exec.c b/drivers/gpu/drm/drm_exec.c new file mode 100644
index 000000000000..e0ad1a3e1610

drivers/gpu/drm/drm_exec.c new file +10 -0
@@ -0,0 +1,274 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+
+#include <drm/drm_exec.h>
+#include <drm/drm_gem.h>
+#include <linux/dma-resv.h>
+
+/**
+ * DOC: Overview
+ *
+ * This component mainly abstracts the retry loop necessary for 

locking
+ * multiple GEM objects while preparing hardware operations (e.g. command
+ * submissions, page table updates etc..).
+ *
+ * If a contention is detected while locking a GEM object the cleanup procedure
+ * unlocks all previously locked GEM objects and locks the contended one first
+ * before locking any further objects.
+ *
+ * After an object is locked fences slots can optionally be reserved on the
+ * dma_resv object inside the GEM object.
+ *
+ * A typical usage pattern should look like this::
+ *
+ * int prepare_objs_func(struct drm_exec *exec, ...)
+ * {
+ *    struct drm_gem_object *boA, *boB;
+ *     int ret;
+ *
+ *    <retrieve boA and boB here>
+ *
+ *    ret = drm_exec_prepare_obj(&exec, boA, 1);
+ *    if (ret)
+ *        return ret;
+ *
+ *    ret = drm_exec_prepare_obj(&exec, boB, 1);
+ *    if (ret)
+ *        return ret;
+ *
+ *     return 0;
+ * }
+ *
+ * int some_func()
+ * {
+ *    struct drm_exec exec;
+ *    unsigned long index;
+ *    int ret;
+ *
+ *    drm_exec_init(&exec, true);
+ *    ret = drm_exec_until_all_locked(&exec, prepare_objs_func(&exec, ...));
+ *    if (ret)
+ *        goto error;
+ *
+ *    drm_exec_for_each_locked_object(&exec, index, obj) {
+ *        dma_resv_add_fence(obj->resv, fence, DMA_RESV_USAGE_READ);
+ *        ...
+ *    }
+ *    drm_exec_fini(&exec);
+ *
+ * See struct dma_exec for more details.
+ */

drivers/gpu/drm/drm_exec.c new file +22 -0
+
+/* Unlock all objects and drop references */
+static void drm_exec_unlock_all(struct drm_exec *exec)
+{
+    struct drm_gem_object *obj;
+    unsigned long index;
+
+    drm_exec_for_each_locked_object(exec, index, obj) {
+        dma_resv_unlock(obj->resv);
+        drm_gem_object_put(obj);
+    }
+
+    drm_gem_object_put(exec->prelocked);
+    exec->prelocked = NULL;
+}
+
+/**
+ * drm_exec_init - initialize a drm_exec object
+ * @exec: the drm_exec object to initialize
+ * @interruptible: if locks should be acquired interruptible
+ *
+ * Initialize the object and make sure that we can track locked 

objects.
+ */

drivers/gpu/drm/drm_exec.c new file +7 -0
+void drm_exec_init(struct drm_exec *exec, u32 flags)
+{
+    exec->flags = flags;
+    exec->objects = kmalloc(PAGE_SIZE, GFP_KERNEL);
+
+    /* If allocation here fails, just delay that till the first use */
+    exec->max_objects = exec->objects ? PAGE_SIZE / sizeof(void *) 

: 0;
+    exec->num_objects = 0;
+    exec->contended = NULL;
+    exec->prelocked = NULL;
+    ww_acquire_init(&exec->ticket, &reservation_ww_class);

drivers/gpu/drm/drm_exec.c new file +8 -0
+}
+EXPORT_SYMBOL(drm_exec_init);
+
+/**
+ * drm_exec_fini - finalize a drm_exec object
+ * @exec: the drm_exec object to finalize
+ *
+ * Unlock all locked objects, drop the references to objects and 

free all memory
+ * used for tracking the state.
+ */

drivers/gpu/drm/drm_exec.c new file +30 -0
+void drm_exec_fini(struct drm_exec *exec)
+{
+    drm_exec_unlock_all(exec);
+    kvfree(exec->objects);
+    if (exec->contended)
+        drm_gem_object_put(exec->contended);
+    ww_acquire_fini(&exec->ticket);
+}
+EXPORT_SYMBOL(drm_exec_fini);
+
+/**
+ * drm_exec_reset - reset a drm_exec object after a contention
+ * @exec: the drm_exec object to reset
+ *
+ * Unlock all locked objects and resets the number of objects locked.
+ */
+void drm_exec_reset(struct drm_exec *exec)
+{
+    WARN_ON(!exec->contended);
+    drm_exec_unlock_all(exec);
+    exec->num_objects = 0;
+}
+EXPORT_SYMBOL(drm_exec_reset);
+
+/* Track the locked object in the array */
+static int drm_exec_obj_locked(struct drm_exec *exec,
+                   struct drm_gem_object *obj)
+{
+    if (unlikely(exec->num_objects == exec->max_objects)) {
+        size_t size = exec->max_objects * sizeof(void *);
Quoted 8 lines
Quoted 6 lines

+        void *tmp;
+
+        tmp = kvrealloc(exec->objects, size, size + PAGE_SIZE, +                GFP_KERNEL);
+        if (!tmp)
+            return -ENOMEM;

Sometimes you need to just temporarily lock an object and then unlock it again if it goes out of scope before reaching the end of _until_all_locked(). In that case you might need to remove a lock from the array. I *think* for all use-cases in i915 it would suffice to take a snapshot of num_objects, and unlock everything above that, having exec->objects behave like a stack, but was ever a list considered instead of a realloced array?

Yes, the problem is that linked lists really suck regarding their cache line locality. That's why I've came up with this approach here.

What we could maybe do is to allow unlocking objects, but with the cost of linear backward searching for them in the array.

Quoted 23 lines
Quoted 23 lines
drivers/gpu/drm/drm_exec.c new file +22 -0
+
+        exec->objects = tmp;
+        exec->max_objects += PAGE_SIZE / sizeof(void *);
+    }
+    drm_gem_object_get(obj);
+    exec->objects[exec->num_objects++] = obj;
+
+    return 0;
+}
+
+/* Make sure the contended object is locked first */
+static int drm_exec_lock_contended(struct drm_exec *exec)
+{
+    struct drm_gem_object *obj = exec->contended;
+    int ret;
+
+    if (likely(!obj))
+        return 0;
+
+    if (exec->flags & DRM_EXEC_FLAG_INTERRUPTIBLE) {
+        ret = dma_resv_lock_slow_interruptible(obj->resv,
+                               &exec->ticket);
Quoted 8 lines
Quoted 6 lines

+        if (unlikely(ret))
+            goto error_dropref;
+    } else {
+        dma_resv_lock_slow(obj->resv, &exec->ticket); +    }
+

Sometimes you want to just drop the contended lock after the above relaxation. (Eviction would be one example), and not add as prelocked, if the contended object goes out of scope. Eviction would in some situations be one such example, -EDEADLOCK leading to an error path where the object should otherwise be freed is another. Perhaps we could add an argument to prepare_obj() as to whether the object should be immediately put after relaxation.

Quoted 1 line

I was considering a try_prepare version as well, that should cover this use case.

That sounds a bit different from this use-case. The use-case above would, on -EDEADLOCK actually unlock everything, then lock-slow the contending lock and then immediately unlock it and drop. It sounds like try_prepare would just skip locking and continue with everything locked so far still locked?

Quoted 78 lines
Quoted 69 lines
Quoted 69 lines

+    ret = drm_exec_obj_locked(exec, obj);
+    if (unlikely(ret)) {
+        dma_resv_unlock(obj->resv);
+        goto error_dropref;
+    }

drivers/gpu/drm/drm_exec.c new file +5 -0
+
+    swap(exec->prelocked, obj);
+
+error_dropref:
+    /* Always cleanup the contention so that error handling can 

kick in */
+    drm_gem_object_put(obj);
+    exec->contended = NULL;
+    return ret;

drivers/gpu/drm/drm_exec.c new file +9 -0
+}
+
+/**
+ * drm_exec_prepare_obj - prepare a GEM object for use
+ * @exec: the drm_exec object with the state
+ * @obj: the GEM object to prepare
+ * @num_fences: how many fences to reserve
+ *
+ * Prepare a GEM object for use by locking it and reserving fence 

slots. All
+ * successfully locked objects are put into the locked container.
+ *
+ * Returns: -EDEADLK if a contention is detected, -EALREADY when object is
+ * already locked, -ENOMEM when memory allocation failed and zero for success.
+ */
+int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
+             unsigned int num_fences)

drivers/gpu/drm/drm_exec.c new file +33 -0
+{
+    int ret;
+
+    ret = drm_exec_lock_contended(exec);
+    if (unlikely(ret))
+        return ret;
+
+    if (exec->prelocked == obj) {
+        drm_gem_object_put(exec->prelocked);
+        exec->prelocked = NULL;
+
+        return dma_resv_reserve_fences(obj->resv, num_fences);
+    }
+
+    if (exec->flags & DRM_EXEC_FLAG_INTERRUPTIBLE)
+        ret = dma_resv_lock_interruptible(obj->resv, &exec->ticket);
+    else
+        ret = dma_resv_lock(obj->resv, &exec->ticket);
+
+    if (unlikely(ret == -EDEADLK)) {
+        drm_gem_object_get(obj);
+        exec->contended = obj;
+        return -EDEADLK;
+    }
+
+    if (unlikely(ret == -EALREADY &&
+        (exec->flags & DRM_EXEC_FLAG_ALLOW_DUPLICATES)))
+        goto reserve_fences;
+
+    if (unlikely(ret))
+        return ret;
+
+    ret = drm_exec_obj_locked(exec, obj);
Quoted 8 lines

drivers/gpu/drm/drm_exec.c line 235

Quoted 6 lines

+    if (ret)
+        goto error_unlock;

drivers/gpu/drm/drm_exec.c new file +4 -0
+
+reserve_fences:
+    /* Keep locked when reserving fences fails */
+    return dma_resv_reserve_fences(obj->resv, num_fences);

Ugh, what is the use-case for keeping things locked here? How would a caller tell the difference between an error where everything is locked and nothing is locked? IMO, we should unlock on error here. If there indeed is a use-case we should add a separate function for reserving fences for all locked objects, rather than returning sometimes locked on error sometime not.

Quoted 1 line

We return the object locked here because it was to much churn to remove it again from the array and we are getting fully cleaned up at the end anyway.

OK, so if we add an unlock functionality, we could just have a consistent locking state on error return?

Thanks,
Thomas

Quoted 193 lines

Regards,
Christian.

Quoted 189 lines

Thanks,

Thomas

Quoted 183 lines
drivers/gpu/drm/drm_exec.c new file +15 -0
+
+error_unlock:
+    dma_resv_unlock(obj->resv);
+    return ret;
+}
+EXPORT_SYMBOL(drm_exec_prepare_obj);
+
+/**
+ * drm_exec_prepare_array - helper to prepare an array of objects
+ * @exec: the drm_exec object with the state
+ * @objects: array of GEM object to prepare
+ * @num_objects: number of GEM objects in the array
+ * @num_fences: number of fences to reserve on each GEM object
+ *
+ * Prepares all GEM objects in an array, handles contention but 

aports on first
+ * error otherwise. Reserves @num_fences on each GEM object after locking it.
+ *
+ * Returns: -EALREADY when object is already locked, -ENOMEM when memory
+ * allocation failed and zero for success.
+ */

drivers/gpu/drm/drm_exec.c new file +19 -0
+int drm_exec_prepare_array(struct drm_exec *exec,
+               struct drm_gem_object **objects,
+               unsigned int num_objects,
+               unsigned int num_fences)
+{
+    int ret;
+
+    for (unsigned int i = 0; i < num_objects; ++i) {
+        ret = drm_exec_prepare_obj(exec, objects[i], num_fences);
+        if (ret)
+            return ret;
+    }
+
+    return 0;
+}
+EXPORT_SYMBOL(drm_exec_prepare_array);
+
+MODULE_DESCRIPTION("DRM execution context");
+MODULE_LICENSE("Dual MIT/GPL");
include/drm/drm_exec.h new file +15 -0
new file mode 100644
index 000000000000..b1a5da0509c1
@@ -0,0 +1,130 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+
+#ifndef __DRM_EXEC_H__
+#define __DRM_EXEC_H__
+
+#include <linux/ww_mutex.h>
+
+struct drm_gem_object;
+
+/**
+ * enum drm_exec_flags - Execution context flags
+ */
+enum drm_exec_flags {
+    /**
+     * DRM_EXEC_FLAG_INTERRUPTIBLE: Set to true to use 

interruptible locking
+     * functions.
+     */
+    DRM_EXEC_FLAG_INTERRUPTIBLE = BIT(0),
+
+    /**
+     * DRM_EXEC_FLAG_ALLOW_DUPLICATES: Set to true to allow EALREADY errors.
+     */
+    DRM_EXEC_FLAG_ALLOW_DUPLICATES = BIT(1),

include/drm/drm_exec.h new file +44 -0
+};
+
+/**
+ * struct drm_exec - Execution context
+ */
+struct drm_exec {
+    /**
+     * @flags: Combinations of DRM_EXEC_FLAG_* flags.
+     */
+    u32 flags;
+
+    /**
+     * @ticket: WW ticket used for acquiring locks
+     */
+    struct ww_acquire_ctx    ticket;
+
+    /**
+     * @num_objects: number of objects locked
+     */
+    unsigned int        num_objects;
+
+    /**
+     * @max_objects: maximum objects in array
+     */
+    unsigned int        max_objects;
+
+    /**
+     * @objects: array of the locked objects
+     */
+    struct drm_gem_object    **objects;
+
+    /**
+     * @contended: contended GEM object we backed off for
+     */
+    struct drm_gem_object    *contended;
+
+    /**
+     * @prelocked: already locked GEM object due to contention
+     */
+    struct drm_gem_object *prelocked;
+};
+
+/**
+ * drm_exec_for_each_locked_object - iterate over all the locked 

objects
+ * @exec: drm_exec object
+ * @index: unsigned long index for the iteration
+ * @obj: the current GEM object
+ *
+ * Iterate over all the locked GEM objects inside the drm_exec object.
+ */

include/drm/drm_exec.h new file +7 -0
+#define drm_exec_for_each_locked_object(exec, index, obj) \
+    for (index = 0, obj = (exec)->objects[0];        \
+         index < (exec)->num_objects;            \
+         ++index, obj = (exec)->objects[index])
+
+/**
+ * drm_exec_until_all_locked - retry objects preparation until all 

objects
+ * are locked
+ * @exec: drm_exec object
+ * @expr: expression to be evaluated on each attempt
+ *
+ * This helper tries to prepare objects and if a deadlock is detected,
+ * rollbacks and retries.
+ *
+ * @expr is typically a function that tries to prepare objects using
+ * drm_exec_prepare_obj().
+ *
+ * If we take drm_exec_prepare_array() as an example, you should do:
+ *
+ *    ret = drm_exec_until_all_locked(exec,
+ *                    drm_exec_prepare_array(exec,
+ *                                   objs,
+ *                                   num_objs,
+ *                                   num_fences));
+ *    if (ret)
+ *        goto error_path;
+ *
+ *    ...
+ *
+ * Returns: 0 on success, a negative error code on failure.
+ */

include/drm/drm_exec.h new file +19 -0
+#define drm_exec_until_all_locked(exec, expr)        \
+    ({                        \
+        __label__ retry;            \
+        int __ret;                \
+retry:                            \
+        __ret = expr;                \
+        if ((exec)->contended) {        \
+            WARN_ON(__ret != -EDEADLK);    \
+            drm_exec_reset(exec);        \
+            goto retry;            \
+        }                    \
+        ww_acquire_done(&(exec)->ticket);    \
+        __ret;                    \
+    })
+
+void drm_exec_init(struct drm_exec *exec, u32 flags);
+void drm_exec_fini(struct drm_exec *exec);
+void drm_exec_reset(struct drm_exec *exec);
+int drm_exec_prepare_obj(struct drm_exec *exec, struct 

drm_gem_object *obj,
+             unsigned int num_fences);

include/drm/drm_exec.h new file +6 -0
+int drm_exec_prepare_array(struct drm_exec *exec,
+               struct drm_gem_object **objects,
+               unsigned int num_objects,
+               unsigned int num_fences);
+
+#endif
maintainer
#11 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

Hi,

Am 19.06.23 um 11:33 schrieb Thomas Hellström (Intel):

Quoted 6 lines

[SNIP]

Quoted 3 lines
Quoted 1 line

Sometimes you want to just drop the contended lock after the above relaxation. (Eviction would be one example), and not add as prelocked, if the contended object goes out of scope. Eviction would in some situations be one such example, -EDEADLOCK leading to an error path where the object should otherwise be freed is another. Perhaps we could add an argument to prepare_obj() as to whether the object should be immediately put after relaxation.

I was considering a try_prepare version as well, that should cover this use case.

That sounds a bit different from this use-case. The use-case above would, on -EDEADLOCK actually unlock everything, then lock-slow the contending lock and then immediately unlock it and drop.

Hui? What would that be good for?

Quoted 1 line

It sounds like try_prepare would just skip locking and continue with everything locked so far still locked?

Correct.

Quoted 79 lines
Quoted 79 lines
Quoted 78 lines
Quoted 66 lines

+    ret = drm_exec_obj_locked(exec, obj);
+    if (unlikely(ret)) {
+        dma_resv_unlock(obj->resv);
+        goto error_dropref;
+    }

drivers/gpu/drm/drm_exec.c new file +5 -0
+
+    swap(exec->prelocked, obj);
+
+error_dropref:
+    /* Always cleanup the contention so that error handling can 

kick in */
+    drm_gem_object_put(obj);
+    exec->contended = NULL;
+    return ret;

drivers/gpu/drm/drm_exec.c new file +9 -0
+}
+
+/**
+ * drm_exec_prepare_obj - prepare a GEM object for use
+ * @exec: the drm_exec object with the state
+ * @obj: the GEM object to prepare
+ * @num_fences: how many fences to reserve
+ *
+ * Prepare a GEM object for use by locking it and reserving fence 

slots. All
+ * successfully locked objects are put into the locked container.
+ *
+ * Returns: -EDEADLK if a contention is detected, -EALREADY when object is
+ * already locked, -ENOMEM when memory allocation failed and zero for success.
+ */
+int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
+             unsigned int num_fences)

drivers/gpu/drm/drm_exec.c new file +29 -0
+{
+    int ret;
+
+    ret = drm_exec_lock_contended(exec);
+    if (unlikely(ret))
+        return ret;
+
+    if (exec->prelocked == obj) {
+        drm_gem_object_put(exec->prelocked);
+        exec->prelocked = NULL;
+
+        return dma_resv_reserve_fences(obj->resv, num_fences);
+    }
+
+    if (exec->flags & DRM_EXEC_FLAG_INTERRUPTIBLE)
+        ret = dma_resv_lock_interruptible(obj->resv, &exec->ticket);
+    else
+        ret = dma_resv_lock(obj->resv, &exec->ticket);
+
+    if (unlikely(ret == -EDEADLK)) {
+        drm_gem_object_get(obj);
+        exec->contended = obj;
+        return -EDEADLK;
+    }
+
+    if (unlikely(ret == -EALREADY &&
+        (exec->flags & DRM_EXEC_FLAG_ALLOW_DUPLICATES)))
+        goto reserve_fences;
+

drivers/gpu/drm/drm_exec.c line 235

Quoted 10 lines
drivers/gpu/drm/drm_exec.c new file +10 -0
+    if (unlikely(ret))
+        return ret;
+
+    ret = drm_exec_obj_locked(exec, obj);
+    if (ret)
+        goto error_unlock;
+
+reserve_fences:
+    /* Keep locked when reserving fences fails */
+    return dma_resv_reserve_fences(obj->resv, num_fences);

Ugh, what is the use-case for keeping things locked here? How would a caller tell the difference between an error where everything is locked and nothing is locked? IMO, we should unlock on error here. If there indeed is a use-case we should add a separate function for reserving fences for all locked objects, rather than returning sometimes locked on error sometime not.

Quoted 3 lines
Quoted 1 line

We return the object locked here because it was to much churn to remove it again from the array and we are getting fully cleaned up at the end anyway.

OK, so if we add an unlock functionality, we could just have a consistent locking state on error return?

Yeah, that should work. Going to work on this.

Regards,
Christian.

Quoted 199 lines

Thanks,
Thomas

Quoted 195 lines

Regards,
Christian.

Quoted 191 lines

Thanks,

Thomas

Quoted 185 lines
drivers/gpu/drm/drm_exec.c new file +15 -0
+
+error_unlock:
+    dma_resv_unlock(obj->resv);
+    return ret;
+}
+EXPORT_SYMBOL(drm_exec_prepare_obj);
+
+/**
+ * drm_exec_prepare_array - helper to prepare an array of objects
+ * @exec: the drm_exec object with the state
+ * @objects: array of GEM object to prepare
+ * @num_objects: number of GEM objects in the array
+ * @num_fences: number of fences to reserve on each GEM object
+ *
+ * Prepares all GEM objects in an array, handles contention but 

aports on first
+ * error otherwise. Reserves @num_fences on each GEM object after locking it.
+ *
+ * Returns: -EALREADY when object is already locked, -ENOMEM when memory
+ * allocation failed and zero for success.
+ */

drivers/gpu/drm/drm_exec.c new file +19 -0
+int drm_exec_prepare_array(struct drm_exec *exec,
+               struct drm_gem_object **objects,
+               unsigned int num_objects,
+               unsigned int num_fences)
+{
+    int ret;
+
+    for (unsigned int i = 0; i < num_objects; ++i) {
+        ret = drm_exec_prepare_obj(exec, objects[i], num_fences);
+        if (ret)
+            return ret;
+    }
+
+    return 0;
+}
+EXPORT_SYMBOL(drm_exec_prepare_array);
+
+MODULE_DESCRIPTION("DRM execution context");
+MODULE_LICENSE("Dual MIT/GPL");
include/drm/drm_exec.h new file +15 -0
new file mode 100644
index 000000000000..b1a5da0509c1
@@ -0,0 +1,130 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+
+#ifndef __DRM_EXEC_H__
+#define __DRM_EXEC_H__
+
+#include <linux/ww_mutex.h>
+
+struct drm_gem_object;
+
+/**
+ * enum drm_exec_flags - Execution context flags
+ */
+enum drm_exec_flags {
+    /**
+     * DRM_EXEC_FLAG_INTERRUPTIBLE: Set to true to use 

interruptible locking
+     * functions.
+     */
+    DRM_EXEC_FLAG_INTERRUPTIBLE = BIT(0),
+
+    /**
+     * DRM_EXEC_FLAG_ALLOW_DUPLICATES: Set to true to allow EALREADY errors.
+     */
+    DRM_EXEC_FLAG_ALLOW_DUPLICATES = BIT(1),

include/drm/drm_exec.h new file +44 -0
+};
+
+/**
+ * struct drm_exec - Execution context
+ */
+struct drm_exec {
+    /**
+     * @flags: Combinations of DRM_EXEC_FLAG_* flags.
+     */
+    u32 flags;
+
+    /**
+     * @ticket: WW ticket used for acquiring locks
+     */
+    struct ww_acquire_ctx    ticket;
+
+    /**
+     * @num_objects: number of objects locked
+     */
+    unsigned int        num_objects;
+
+    /**
+     * @max_objects: maximum objects in array
+     */
+    unsigned int        max_objects;
+
+    /**
+     * @objects: array of the locked objects
+     */
+    struct drm_gem_object    **objects;
+
+    /**
+     * @contended: contended GEM object we backed off for
+     */
+    struct drm_gem_object    *contended;
+
+    /**
+     * @prelocked: already locked GEM object due to contention
+     */
+    struct drm_gem_object *prelocked;
+};
+
+/**
+ * drm_exec_for_each_locked_object - iterate over all the locked 

objects
+ * @exec: drm_exec object
+ * @index: unsigned long index for the iteration
+ * @obj: the current GEM object
+ *
+ * Iterate over all the locked GEM objects inside the drm_exec object.
+ */

include/drm/drm_exec.h new file +7 -0
+#define drm_exec_for_each_locked_object(exec, index, obj) \
+    for (index = 0, obj = (exec)->objects[0];        \
+         index < (exec)->num_objects;            \
+         ++index, obj = (exec)->objects[index])
+
+/**
+ * drm_exec_until_all_locked - retry objects preparation until all 

objects
+ * are locked
+ * @exec: drm_exec object
+ * @expr: expression to be evaluated on each attempt
+ *
+ * This helper tries to prepare objects and if a deadlock is detected,
+ * rollbacks and retries.
+ *
+ * @expr is typically a function that tries to prepare objects using
+ * drm_exec_prepare_obj().
+ *
+ * If we take drm_exec_prepare_array() as an example, you should do:
+ *
+ *    ret = drm_exec_until_all_locked(exec,
+ *                    drm_exec_prepare_array(exec,
+ *                                   objs,
+ *                                   num_objs,
+ *                                   num_fences));
+ *    if (ret)
+ *        goto error_path;
+ *
+ *    ...
+ *
+ * Returns: 0 on success, a negative error code on failure.
+ */

include/drm/drm_exec.h new file +19 -0
+#define drm_exec_until_all_locked(exec, expr)        \
+    ({                        \
+        __label__ retry;            \
+        int __ret;                \
+retry:                            \
+        __ret = expr;                \
+        if ((exec)->contended) {        \
+            WARN_ON(__ret != -EDEADLK);    \
+            drm_exec_reset(exec);        \
+            goto retry;            \
+        }                    \
+        ww_acquire_done(&(exec)->ticket);    \
+        __ret;                    \
+    })
+
+void drm_exec_init(struct drm_exec *exec, u32 flags);
+void drm_exec_fini(struct drm_exec *exec);
+void drm_exec_reset(struct drm_exec *exec);
+int drm_exec_prepare_obj(struct drm_exec *exec, struct 

drm_gem_object *obj,
+             unsigned int num_fences);

include/drm/drm_exec.h new file +6 -0
+int drm_exec_prepare_array(struct drm_exec *exec,
+               struct drm_gem_object **objects,
+               unsigned int num_objects,
+               unsigned int num_fences);
+
+#endif
#12 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

Quoted 11 lines · Christian König

Hi,

Am 19.06.23 um 11:33 schrieb Thomas Hellström (Intel):

Quoted 6 lines

[SNIP]

Quoted 3 lines
Quoted 1 line

Sometimes you want to just drop the contended lock after the above relaxation. (Eviction would be one example), and not add as prelocked, if the contended object goes out of scope. Eviction would in some situations be one such example, -EDEADLOCK leading to an error path where the object should otherwise be freed is another. Perhaps we could add an argument to prepare_obj() as to whether the object should be immediately put after relaxation.

I was considering a try_prepare version as well, that should cover this use case.

That sounds a bit different from this use-case. The use-case above would, on -EDEADLOCK actually unlock everything, then lock-slow the contending lock and then immediately unlock it and drop.

Hui? What would that be good for?

It's for the case where you have nested locking, the contended lock has gone out-of-scope and you're probably not going to need it on the next attempt. I think the refcounted "prelocked" object that is lacking in the i915 variant will resolve all correctness / uaf issues, but still the object might be needlessly carried around for yet another locking round.

Quoted 84 lines
Quoted 2 lines

It sounds like try_prepare would just skip locking and continue with everything locked so far still locked?

Correct.

Quoted 79 lines
Quoted 79 lines
Quoted 78 lines
Quoted 66 lines

+    ret = drm_exec_obj_locked(exec, obj);
+    if (unlikely(ret)) {
+        dma_resv_unlock(obj->resv);
+        goto error_dropref;
+    }

drivers/gpu/drm/drm_exec.c new file +5 -0
+
+    swap(exec->prelocked, obj);
+
+error_dropref:
+    /* Always cleanup the contention so that error handling can 

kick in */
+    drm_gem_object_put(obj);
+    exec->contended = NULL;
+    return ret;

drivers/gpu/drm/drm_exec.c new file +9 -0
+}
+
+/**
+ * drm_exec_prepare_obj - prepare a GEM object for use
+ * @exec: the drm_exec object with the state
+ * @obj: the GEM object to prepare
+ * @num_fences: how many fences to reserve
+ *
+ * Prepare a GEM object for use by locking it and reserving fence 

slots. All
+ * successfully locked objects are put into the locked container.
+ *
+ * Returns: -EDEADLK if a contention is detected, -EALREADY when object is
+ * already locked, -ENOMEM when memory allocation failed and zero for success.
+ */
+int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
+             unsigned int num_fences)

drivers/gpu/drm/drm_exec.c new file +29 -0
+{
+    int ret;
+
+    ret = drm_exec_lock_contended(exec);
+    if (unlikely(ret))
+        return ret;
+
+    if (exec->prelocked == obj) {
+        drm_gem_object_put(exec->prelocked);
+        exec->prelocked = NULL;
+
+        return dma_resv_reserve_fences(obj->resv, num_fences);
+    }
+
+    if (exec->flags & DRM_EXEC_FLAG_INTERRUPTIBLE)
+        ret = dma_resv_lock_interruptible(obj->resv, &exec->ticket);
+    else
+        ret = dma_resv_lock(obj->resv, &exec->ticket);
+
+    if (unlikely(ret == -EDEADLK)) {
+        drm_gem_object_get(obj);
+        exec->contended = obj;
+        return -EDEADLK;
+    }
+
+    if (unlikely(ret == -EALREADY &&
+        (exec->flags & DRM_EXEC_FLAG_ALLOW_DUPLICATES)))
+        goto reserve_fences;
+

drivers/gpu/drm/drm_exec.c line 235

Quoted 10 lines
drivers/gpu/drm/drm_exec.c new file +10 -0
+    if (unlikely(ret))
+        return ret;
+
+    ret = drm_exec_obj_locked(exec, obj);
+    if (ret)
+        goto error_unlock;
+
+reserve_fences:
+    /* Keep locked when reserving fences fails */
+    return dma_resv_reserve_fences(obj->resv, num_fences);

Ugh, what is the use-case for keeping things locked here? How would a caller tell the difference between an error where everything is locked and nothing is locked? IMO, we should unlock on error here. If there indeed is a use-case we should add a separate function for reserving fences for all locked objects, rather than returning sometimes locked on error sometime not.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

We return the object locked here because it was to much churn to remove it again from the array and we are getting fully cleaned up at the end anyway.

OK, so if we add an unlock functionality, we could just have a consistent locking state on error return?

Yeah, that should work. Going to work on this.

Great.

Thanks,

Thomas

Quoted 203 lines

Regards,
Christian.

Quoted 199 lines

Thanks,
Thomas

Quoted 195 lines

Regards,
Christian.

Quoted 191 lines

Thanks,

Thomas

Quoted 185 lines
drivers/gpu/drm/drm_exec.c new file +15 -0
+
+error_unlock:
+    dma_resv_unlock(obj->resv);
+    return ret;
+}
+EXPORT_SYMBOL(drm_exec_prepare_obj);
+
+/**
+ * drm_exec_prepare_array - helper to prepare an array of objects
+ * @exec: the drm_exec object with the state
+ * @objects: array of GEM object to prepare
+ * @num_objects: number of GEM objects in the array
+ * @num_fences: number of fences to reserve on each GEM object
+ *
+ * Prepares all GEM objects in an array, handles contention but 

aports on first
+ * error otherwise. Reserves @num_fences on each GEM object after locking it.
+ *
+ * Returns: -EALREADY when object is already locked, -ENOMEM when memory
+ * allocation failed and zero for success.
+ */

drivers/gpu/drm/drm_exec.c new file +19 -0
+int drm_exec_prepare_array(struct drm_exec *exec,
+               struct drm_gem_object **objects,
+               unsigned int num_objects,
+               unsigned int num_fences)
+{
+    int ret;
+
+    for (unsigned int i = 0; i < num_objects; ++i) {
+        ret = drm_exec_prepare_obj(exec, objects[i], num_fences);
+        if (ret)
+            return ret;
+    }
+
+    return 0;
+}
+EXPORT_SYMBOL(drm_exec_prepare_array);
+
+MODULE_DESCRIPTION("DRM execution context");
+MODULE_LICENSE("Dual MIT/GPL");
include/drm/drm_exec.h new file +15 -0
new file mode 100644
index 000000000000..b1a5da0509c1
@@ -0,0 +1,130 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+
+#ifndef __DRM_EXEC_H__
+#define __DRM_EXEC_H__
+
+#include <linux/ww_mutex.h>
+
+struct drm_gem_object;
+
+/**
+ * enum drm_exec_flags - Execution context flags
+ */
+enum drm_exec_flags {
+    /**
+     * DRM_EXEC_FLAG_INTERRUPTIBLE: Set to true to use 

interruptible locking
+     * functions.
+     */
+    DRM_EXEC_FLAG_INTERRUPTIBLE = BIT(0),
+
+    /**
+     * DRM_EXEC_FLAG_ALLOW_DUPLICATES: Set to true to allow EALREADY errors.
+     */
+    DRM_EXEC_FLAG_ALLOW_DUPLICATES = BIT(1),

include/drm/drm_exec.h new file +44 -0
+};
+
+/**
+ * struct drm_exec - Execution context
+ */
+struct drm_exec {
+    /**
+     * @flags: Combinations of DRM_EXEC_FLAG_* flags.
+     */
+    u32 flags;
+
+    /**
+     * @ticket: WW ticket used for acquiring locks
+     */
+    struct ww_acquire_ctx    ticket;
+
+    /**
+     * @num_objects: number of objects locked
+     */
+    unsigned int        num_objects;
+
+    /**
+     * @max_objects: maximum objects in array
+     */
+    unsigned int        max_objects;
+
+    /**
+     * @objects: array of the locked objects
+     */
+    struct drm_gem_object    **objects;
+
+    /**
+     * @contended: contended GEM object we backed off for
+     */
+    struct drm_gem_object    *contended;
+
+    /**
+     * @prelocked: already locked GEM object due to contention
+     */
+    struct drm_gem_object *prelocked;
+};
+
+/**
+ * drm_exec_for_each_locked_object - iterate over all the locked 

objects
+ * @exec: drm_exec object
+ * @index: unsigned long index for the iteration
+ * @obj: the current GEM object
+ *
+ * Iterate over all the locked GEM objects inside the drm_exec object.
+ */

include/drm/drm_exec.h new file +7 -0
+#define drm_exec_for_each_locked_object(exec, index, obj) \
+    for (index = 0, obj = (exec)->objects[0];        \
+         index < (exec)->num_objects;            \
+         ++index, obj = (exec)->objects[index])
+
+/**
+ * drm_exec_until_all_locked - retry objects preparation until 

all objects
+ * are locked
+ * @exec: drm_exec object
+ * @expr: expression to be evaluated on each attempt
+ *
+ * This helper tries to prepare objects and if a deadlock is detected,
+ * rollbacks and retries.
+ *
+ * @expr is typically a function that tries to prepare objects using
+ * drm_exec_prepare_obj().
+ *
+ * If we take drm_exec_prepare_array() as an example, you should do:
+ *
+ *    ret = drm_exec_until_all_locked(exec,
+ *                    drm_exec_prepare_array(exec,
+ *                                   objs,
+ *                                   num_objs,
+ *                                   num_fences));
+ *    if (ret)
+ *        goto error_path;
+ *
+ *    ...
+ *
+ * Returns: 0 on success, a negative error code on failure.
+ */

include/drm/drm_exec.h new file +19 -0
+#define drm_exec_until_all_locked(exec, expr)        \
+    ({                        \
+        __label__ retry;            \
+        int __ret;                \
+retry:                            \
+        __ret = expr;                \
+        if ((exec)->contended) {        \
+            WARN_ON(__ret != -EDEADLK);    \
+            drm_exec_reset(exec);        \
+            goto retry;            \
+        }                    \
+        ww_acquire_done(&(exec)->ticket);    \
+        __ret;                    \
+    })
+
+void drm_exec_init(struct drm_exec *exec, u32 flags);
+void drm_exec_fini(struct drm_exec *exec);
+void drm_exec_reset(struct drm_exec *exec);
+int drm_exec_prepare_obj(struct drm_exec *exec, struct 

drm_gem_object *obj,
+             unsigned int num_fences);

include/drm/drm_exec.h new file +6 -0
+int drm_exec_prepare_array(struct drm_exec *exec,
+               struct drm_gem_object **objects,
+               unsigned int num_objects,
+               unsigned int num_fences);
+
+#endif
maintainer
#13 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

Am 19.06.23 um 13:06 schrieb Thomas Hellström (Intel):

Quoted 8 lines · Christian König
Quoted 6 lines

Hi,

Am 19.06.23 um 11:33 schrieb Thomas Hellström (Intel):

Quoted 3 lines

[SNIP]

Quoted 2 lines
Quoted 1 line

Sometimes you want to just drop the contended lock after the above relaxation. (Eviction would be one example), and not add as prelocked, if the contended object goes out of scope. Eviction would in some situations be one such example, -EDEADLOCK leading to an error path where the object should otherwise be freed is another. Perhaps we could add an argument to prepare_obj() as to whether the object should be immediately put after relaxation.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

I was considering a try_prepare version as well, that should cover this use case.

That sounds a bit different from this use-case. The use-case above would, on -EDEADLOCK actually unlock everything, then lock-slow the contending lock and then immediately unlock it and drop.

Hui? What would that be good for?

It's for the case where you have nested locking, the contended lock has gone out-of-scope and you're probably not going to need it on the next attempt. I think the refcounted "prelocked" object that is lacking in the i915 variant will resolve all correctness / uaf issues, but still the object might be needlessly carried around for yet another locking round.

Yeah, but that case is so rare that we probably don't need to care about it.

I've changed the implementation so that it should now match the other requirements.

Going to send that out now, please double check.

Thanks,
Christian.

Quoted 306 lines
Quoted 88 lines
Quoted 5 lines

It sounds like try_prepare would just skip locking and continue with everything locked so far still locked?

Correct.

Quoted 80 lines
Quoted 80 lines
Quoted 79 lines
Quoted 67 lines

+    ret = drm_exec_obj_locked(exec, obj);
+    if (unlikely(ret)) {
+        dma_resv_unlock(obj->resv);
+        goto error_dropref;
+    }

drivers/gpu/drm/drm_exec.c new file +5 -0
+
+    swap(exec->prelocked, obj);
+
+error_dropref:
+    /* Always cleanup the contention so that error handling can 

kick in */
+    drm_gem_object_put(obj);
+    exec->contended = NULL;
+    return ret;

drivers/gpu/drm/drm_exec.c new file +9 -0
+}
+
+/**
+ * drm_exec_prepare_obj - prepare a GEM object for use
+ * @exec: the drm_exec object with the state
+ * @obj: the GEM object to prepare
+ * @num_fences: how many fences to reserve
+ *
+ * Prepare a GEM object for use by locking it and reserving 

fence slots. All
+ * successfully locked objects are put into the locked container.
+ *
+ * Returns: -EDEADLK if a contention is detected, -EALREADY when object is
+ * already locked, -ENOMEM when memory allocation failed and zero for success.
+ */
+int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
+             unsigned int num_fences)

drivers/gpu/drm/drm_exec.c new file +16 -0
+{
+    int ret;
+
+    ret = drm_exec_lock_contended(exec);
+    if (unlikely(ret))
+        return ret;
+
+    if (exec->prelocked == obj) {
+        drm_gem_object_put(exec->prelocked);
+        exec->prelocked = NULL;
+
+        return dma_resv_reserve_fences(obj->resv, num_fences);
+    }
+
+    if (exec->flags & DRM_EXEC_FLAG_INTERRUPTIBLE)
+        ret = dma_resv_lock_interruptible(obj->resv, 

&exec->ticket);
+    else
+        ret = dma_resv_lock(obj->resv, &exec->ticket);

drivers/gpu/drm/drm_exec.c new file +11 -0
+
+    if (unlikely(ret == -EDEADLK)) {
+        drm_gem_object_get(obj);
+        exec->contended = obj;
+        return -EDEADLK;
+    }
+
+    if (unlikely(ret == -EALREADY &&
+        (exec->flags & DRM_EXEC_FLAG_ALLOW_DUPLICATES)))
+        goto reserve_fences;
+

drivers/gpu/drm/drm_exec.c line 235

Quoted 10 lines
drivers/gpu/drm/drm_exec.c new file +10 -0
+    if (unlikely(ret))
+        return ret;
+
+    ret = drm_exec_obj_locked(exec, obj);
+    if (ret)
+        goto error_unlock;
+
+reserve_fences:
+    /* Keep locked when reserving fences fails */
+    return dma_resv_reserve_fences(obj->resv, num_fences);

Ugh, what is the use-case for keeping things locked here? How would a caller tell the difference between an error where everything is locked and nothing is locked? IMO, we should unlock on error here. If there indeed is a use-case we should add a separate function for reserving fences for all locked objects, rather than returning sometimes locked on error sometime not.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

We return the object locked here because it was to much churn to remove it again from the array and we are getting fully cleaned up at the end anyway.

OK, so if we add an unlock functionality, we could just have a consistent locking state on error return?

Yeah, that should work. Going to work on this.

Great.

Thanks,

Thomas

Quoted 205 lines

Regards,
Christian.

Quoted 201 lines

Thanks,
Thomas

Quoted 197 lines

Regards,
Christian.

Quoted 193 lines

Thanks,

Thomas

Quoted 187 lines
drivers/gpu/drm/drm_exec.c new file +15 -0
+
+error_unlock:
+    dma_resv_unlock(obj->resv);
+    return ret;
+}
+EXPORT_SYMBOL(drm_exec_prepare_obj);
+
+/**
+ * drm_exec_prepare_array - helper to prepare an array of objects
+ * @exec: the drm_exec object with the state
+ * @objects: array of GEM object to prepare
+ * @num_objects: number of GEM objects in the array
+ * @num_fences: number of fences to reserve on each GEM object
+ *
+ * Prepares all GEM objects in an array, handles contention but 

aports on first
+ * error otherwise. Reserves @num_fences on each GEM object after locking it.
+ *
+ * Returns: -EALREADY when object is already locked, -ENOMEM when memory
+ * allocation failed and zero for success.
+ */

drivers/gpu/drm/drm_exec.c new file +19 -0
+int drm_exec_prepare_array(struct drm_exec *exec,
+               struct drm_gem_object **objects,
+               unsigned int num_objects,
+               unsigned int num_fences)
+{
+    int ret;
+
+    for (unsigned int i = 0; i < num_objects; ++i) {
+        ret = drm_exec_prepare_obj(exec, objects[i], num_fences);
+        if (ret)
+            return ret;
+    }
+
+    return 0;
+}
+EXPORT_SYMBOL(drm_exec_prepare_array);
+
+MODULE_DESCRIPTION("DRM execution context");
+MODULE_LICENSE("Dual MIT/GPL");
include/drm/drm_exec.h new file +15 -0
new file mode 100644
index 000000000000..b1a5da0509c1
@@ -0,0 +1,130 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+
+#ifndef __DRM_EXEC_H__
+#define __DRM_EXEC_H__
+
+#include <linux/ww_mutex.h>
+
+struct drm_gem_object;
+
+/**
+ * enum drm_exec_flags - Execution context flags
+ */
+enum drm_exec_flags {
+    /**
+     * DRM_EXEC_FLAG_INTERRUPTIBLE: Set to true to use 

interruptible locking
+     * functions.
+     */
+    DRM_EXEC_FLAG_INTERRUPTIBLE = BIT(0),
+
+    /**
+     * DRM_EXEC_FLAG_ALLOW_DUPLICATES: Set to true to allow EALREADY errors.
+     */
+    DRM_EXEC_FLAG_ALLOW_DUPLICATES = BIT(1),

include/drm/drm_exec.h new file +44 -0
+};
+
+/**
+ * struct drm_exec - Execution context
+ */
+struct drm_exec {
+    /**
+     * @flags: Combinations of DRM_EXEC_FLAG_* flags.
+     */
+    u32 flags;
+
+    /**
+     * @ticket: WW ticket used for acquiring locks
+     */
+    struct ww_acquire_ctx    ticket;
+
+    /**
+     * @num_objects: number of objects locked
+     */
+    unsigned int        num_objects;
+
+    /**
+     * @max_objects: maximum objects in array
+     */
+    unsigned int        max_objects;
+
+    /**
+     * @objects: array of the locked objects
+     */
+    struct drm_gem_object    **objects;
+
+    /**
+     * @contended: contended GEM object we backed off for
+     */
+    struct drm_gem_object    *contended;
+
+    /**
+     * @prelocked: already locked GEM object due to contention
+     */
+    struct drm_gem_object *prelocked;
+};
+
+/**
+ * drm_exec_for_each_locked_object - iterate over all the locked 

objects
+ * @exec: drm_exec object
+ * @index: unsigned long index for the iteration
+ * @obj: the current GEM object
+ *
+ * Iterate over all the locked GEM objects inside the drm_exec object.
+ */

include/drm/drm_exec.h new file +7 -0
+#define drm_exec_for_each_locked_object(exec, index, obj) \
+    for (index = 0, obj = (exec)->objects[0]; \
+         index < (exec)->num_objects; \
+         ++index, obj = (exec)->objects[index])
+
+/**
+ * drm_exec_until_all_locked - retry objects preparation until 

all objects
+ * are locked
+ * @exec: drm_exec object
+ * @expr: expression to be evaluated on each attempt
+ *
+ * This helper tries to prepare objects and if a deadlock is detected,
+ * rollbacks and retries.
+ *
+ * @expr is typically a function that tries to prepare objects using
+ * drm_exec_prepare_obj().
+ *
+ * If we take drm_exec_prepare_array() as an example, you should do:
+ *
+ *    ret = drm_exec_until_all_locked(exec,
+ *                    drm_exec_prepare_array(exec,
+ *                                   objs,
+ *                                   num_objs,
+ *                                   num_fences));
+ *    if (ret)
+ *        goto error_path;
+ *
+ *    ...
+ *
+ * Returns: 0 on success, a negative error code on failure.
+ */

include/drm/drm_exec.h new file +19 -0
+#define drm_exec_until_all_locked(exec, expr)        \
+    ({                        \
+        __label__ retry;            \
+        int __ret;                \
+retry:                            \
+        __ret = expr;                \
+        if ((exec)->contended) {        \
+            WARN_ON(__ret != -EDEADLK);    \
+            drm_exec_reset(exec);        \
+            goto retry;            \
+        }                    \
+        ww_acquire_done(&(exec)->ticket);    \
+        __ret;                    \
+    })
+
+void drm_exec_init(struct drm_exec *exec, u32 flags);
+void drm_exec_fini(struct drm_exec *exec);
+void drm_exec_reset(struct drm_exec *exec);
+int drm_exec_prepare_obj(struct drm_exec *exec, struct 

drm_gem_object *obj,
+             unsigned int num_fences);

include/drm/drm_exec.h new file +6 -0
+int drm_exec_prepare_array(struct drm_exec *exec,
+               struct drm_gem_object **objects,
+               unsigned int num_objects,
+               unsigned int num_fences);
+
+#endif
maintainer
#14 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

In reply to Christian König
Quoted 11 lines · On Mon, 19 Jun 2023 11:20:06 +0200 Christian König <[email protected]>…

Hi guys,

Am 19.06.23 um 10:59 schrieb Thomas Hellström (Intel):

Quoted 8 lines

[SNIP]

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

I really need to find some time to work on that anyway.

I've been playing with drm_exec for a couple weeks now, and I wanted to share something I hacked to try and make the API simpler and more robust against misuse (see the below diff, which is a slightly adjusted version of your work).

Quoted 7 lines
Quoted 4 lines

It would be good if we could have someone taking charge of this series and address all review comments, I see some of my comments getting lost, we have multiple submitters and I can't find a dri-devel patchwork entry for this. Anyway some comments below.

I can try to find some time for the series this week (As long as nobody comes along and has any burning roof).

That's great news!

Quoted 18 lines
Quoted 11 lines
Quoted 10 lines

In this version, the user is no longer in control of the retry loop. Instead, it provides an expression (a call to a sub-function) to be re-evaluated each time a contention is detected. IMHO, this makes the 'prepare-objs' functions easier to apprehend, and avoids any mistake like calling drm_exec_continue_on_contention() in an inner loop, or breaking out of the drm_exec_while_all_locked() loop unintentionally.

Quoted 6 lines

In i915 we've had a very similar helper to this, and while I agree this newer version would probably help make code cleaner, but OTOH there also are some places where the short drm_exec_while_all_locked() -likeblock don't really motivate a separate function. Porting i915 to the current version will take some work, For  the xe driver both versions would work fine.

Quoted 4 lines

Yeah, this is actually what my first version of this looked like. But I abandoned that approach because we have a lot of cases were we just quickly want to lock a few GEM objects and don't want the extra overhead of putting all the state into some bag to forward it to a function.

If you're talking about verbosity, it might be the case, though I guess it mostly a matter of taste (I do like when things are well isolated). As for runtime overhead, I'd expect the compiler to inline the function anyway, so it's unlikely to change anything.

Quoted 21 lines
Quoted 13 lines

drivers/gpu/drm/drm_exec.c line 139

Quoted 12 lines
drivers/gpu/drm/drm_exec.c new file +12 -0
+/* Track the locked object in the array */
+static int drm_exec_obj_locked(struct drm_exec *exec,
+                   struct drm_gem_object *obj)
+{
+    if (unlikely(exec->num_objects == exec->max_objects)) {
+        size_t size = exec->max_objects * sizeof(void *);
+        void *tmp;
+
+        tmp = kvrealloc(exec->objects, size, size + PAGE_SIZE,
+                GFP_KERNEL);
+        if (!tmp)
+            return -ENOMEM;  
Quoted 7 lines

Sometimes you need to just temporarily lock an object and then unlock it again if it goes out of scope before reaching the end of _until_all_locked(). In that case you might need to remove a lock from the array. I *think* for all use-cases in i915 it would suffice to take a snapshot of num_objects, and unlock everything above that, having exec->objects behave like a stack, but was ever a list considered instead of a realloced array?

Quoted 2 lines

Yes, the problem is that linked lists really suck regarding their cache line locality. That's why I've came up with this approach here.

Hm, maybe I'm missing something, but if you place the list_head obj you use to stack the locked objects close enough to the resv pointer, and aligned on cache line, it shouldn't really be a problem, given you have to dereference the GEM object to retrieve its resv anyway.

maintainer
#15 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

In reply to Thomas Hellström (Intel)

Hello Thomas,

Quoted 61 lines · On Mon, 19 Jun 2023 10:59:16 +0200 Thomas Hellström (Intel)…
Quoted 52 lines
Quoted 52 lines
Quoted 52 lines
Quoted 52 lines
Quoted 21 lines

+/**
+ * DOC: Overview
+ *
+ * This component mainly abstracts the retry loop necessary for locking
+ * multiple GEM objects while preparing hardware operations (e.g. command
+ * submissions, page table updates etc..).
+ *
+ * If a contention is detected while locking a GEM object the cleanup procedure
+ * unlocks all previously locked GEM objects and locks the contended one first
+ * before locking any further objects.
+ *
+ * After an object is locked fences slots can optionally be reserved on the
+ * dma_resv object inside the GEM object.
+ *
+ * A typical usage pattern should look like this::
+ *
+ * struct drm_gem_object *obj;
+ * struct drm_exec exec;
+ * unsigned long index;
+ * int ret;

Quoted 8 lines

+ *
+ * drm_exec_init(&exec, true);
+ * drm_exec_while_not_all_locked(&exec) {
+ * ret = drm_exec_prepare_obj(&exec, boA, 1);
+ * drm_exec_continue_on_contention(&exec);
+ * if (ret)
+ * goto error;
+ *

Have you considered defining a drm_exec_try_prepare_obj_or_retry() combining drm_exec_prepare_obj() and drm_exec_continue_on_contention()?

#define drm_exec_try_prepare_obj_or_retry(exec, obj, num_fences) \
          ({ \
                  int __ret = drm_exec_prepare_obj(exec, bo, num_fences); \
                  if (unlikely(drm_exec_is_contended(exec))) \
                          continue; \
                  __ret; \
          })

This way the following pattern

		ret = drm_exec_prepare_obj(&exec, boA, 1);
		drm_exec_continue_on_contention(&exec);
		if (ret)
			goto error;

can be turned into something more conventional:

		ret = drm_exec_try_prepare_obj_or_retry(&exec, boA, 1);
		if (ret)
			goto error;  
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

Yeah, I was considering that as well. But then abandoned it as to complicated.

I really need to find some time to work on that anyway.

I've been playing with drm_exec for a couple weeks now, and I wanted to share something I hacked to try and make the API simpler and more robust against misuse (see the below diff, which is a slightly adjusted version of your work).

Quoted 4 lines

It would be good if we could have someone taking charge of this series and address all review comments, I see some of my comments getting lost, we have multiple submitters and I can't find a dri-devel patchwork entry for this.

My bad, I wasn't intending to submit a new version. I just added a diff to show what I had in mind. This being said, it'd be great if we could make some progress on this series, because we have quite a few drivers depending on it now.

Quoted 10 lines
Quoted 9 lines

In this version, the user is no longer in control of the retry loop. Instead, it provides an expression (a call to a sub-function) to be re-evaluated each time a contention is detected. IMHO, this makes the 'prepare-objs' functions easier to apprehend, and avoids any mistake like calling drm_exec_continue_on_contention() in an inner loop, or breaking out of the drm_exec_while_all_locked() loop unintentionally.

Quoted 6 lines

In i915 we've had a very similar helper to this, and while I agree this newer version would probably help make code cleaner, but OTOH there also are some places where the short drm_exec_while_all_locked() -likeblock don't really motivate a separate function. Porting i915 to the current version will take some work, For  the xe driver both versions would work fine.

Note that the drm_exec_until_all_locked() helper I introduced is taking an expression, so in theory, you don't have to define a separate function.

	drm_exec_until_all_locked(&exec, {
		/* inlined-code */
		int ret;

		ret = blabla()
		if (ret)
			goto error;

		...

error:

		/* return value. */
		ret;
	});

This being said, as soon as you have several failure paths, it makes things a lot easier/controllable if you make it a function, and I honestly don't think the readability would suffer from having a function defined just above the user. My main concern with the original approach was the risk of calling continue/break_if_contended() in the wrong place, and also the fact you can't really externalize things to a function if you're looking for a cleaner split. At least with drm_exec_until_all_locked() you can do both.

Regards,

Boris

maintainer
#16 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

Am 19.06.23 um 12:12 schrieb Boris Brezillon:

Quoted 20 lines

[SNIP]
Note that the drm_exec_until_all_locked() helper I introduced is taking an expression, so in theory, you don't have to define a separate function.

	drm_exec_until_all_locked(&exec, {
		/* inlined-code */
		int ret;

		ret = blabla()
		if (ret)
			goto error;

		...

error:

		/* return value. */
		ret;
	});
Quoted 8 lines

This being said, as soon as you have several failure paths, it makes things a lot easier/controllable if you make it a function, and I honestly don't think the readability would suffer from having a function defined just above the user. My main concern with the original approach was the risk of calling continue/break_if_contended() in the wrong place, and also the fact you can't really externalize things to a function if you're looking for a cleaner split. At least with drm_exec_until_all_locked() you can do both.

Yeah, but that means that you can't use return inside your code block and instead has to define an error label for handling "normal" contention which is what I'm trying to avoid here.

How about:

#define drm_exec_until_all_locked(exec)    \
        __drm_exec_retry: if (drm_exec_cleanup(exec))

#define drm_exec_retry_on_contention(exec)              \
        if (unlikely(drm_exec_is_contended(exec)))      \
                goto __drm_exec_retry

And then use it like:

drm_exec_until_all_locked(exec)
{
    ret = drm_exec_prepare_obj(exec, obj);
    drm_exec_retry_on_contention(exec);
}

The only problem I can see with this is that the __drm_exec_retry label would be function local.

Regards,
Christian.

Quoted 4 lines

Regards,

Boris

maintainer
#17 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

Quoted 45 lines · On Mon, 19 Jun 2023 12:44:06 +0200 Christian König <[email protected]>…

Am 19.06.23 um 12:12 schrieb Boris Brezillon:

Quoted 20 lines

[SNIP]
Note that the drm_exec_until_all_locked() helper I introduced is taking an expression, so in theory, you don't have to define a separate function.

	drm_exec_until_all_locked(&exec, {
		/* inlined-code */
		int ret;

		ret = blabla()
		if (ret)
			goto error;

		...

error:

		/* return value. */
		ret;
	});
Quoted 8 lines

This being said, as soon as you have several failure paths, it makes things a lot easier/controllable if you make it a function, and I honestly don't think the readability would suffer from having a function defined just above the user. My main concern with the original approach was the risk of calling continue/break_if_contended() in the wrong place, and also the fact you can't really externalize things to a function if you're looking for a cleaner split. At least with drm_exec_until_all_locked() you can do both.

Yeah, but that means that you can't use return inside your code block and instead has to define an error label for handling "normal" contention which is what I'm trying to avoid here.

How about:

#define drm_exec_until_all_locked(exec)    \
        __drm_exec_retry: if (drm_exec_cleanup(exec))

#define drm_exec_retry_on_contention(exec)              \
        if (unlikely(drm_exec_is_contended(exec)))      \
                goto __drm_exec_retry

Quoted 7 lines

And then use it like:

drm_exec_until_all_locked(exec)
{
    ret = drm_exec_prepare_obj(exec, obj);
    drm_exec_retry_on_contention(exec);
}

That would work, and I was about to suggest extending my proposal with a drm_exec_retry_on_contention() to support both use cases. The only downside is the fact you might be able to break out of a loop that has local variables, which will leak stack space.

Quoted 3 lines

The only problem I can see with this is that the __drm_exec_retry label would be function local.

You can use local labels [1] to make it local to a block (see my version, just need to rename the retry label into __drm_exec_retry). I checked, and this is used elsewhere in the kernel (like in linux/wait.h, which is a core feature), so it should be safe to use.

[1]https://gcc.gnu.org/onlinedocs/gcc/Local-Labels.html

maintainer
#18 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

Quoted 56 lines · On Mon, 19 Jun 2023 13:05:02 +0200 Boris Brezillon…
Quoted 45 lines · On Mon, 19 Jun 2023 12:44:06 +0200 Christian König <[email protected]>…

Am 19.06.23 um 12:12 schrieb Boris Brezillon:

Quoted 20 lines

[SNIP]
Note that the drm_exec_until_all_locked() helper I introduced is taking an expression, so in theory, you don't have to define a separate function.

	drm_exec_until_all_locked(&exec, {
		/* inlined-code */
		int ret;

		ret = blabla()
		if (ret)
			goto error;

		...

error:

		/* return value. */
		ret;
	});
Quoted 8 lines

This being said, as soon as you have several failure paths, it makes things a lot easier/controllable if you make it a function, and I honestly don't think the readability would suffer from having a function defined just above the user. My main concern with the original approach was the risk of calling continue/break_if_contended() in the wrong place, and also the fact you can't really externalize things to a function if you're looking for a cleaner split. At least with drm_exec_until_all_locked() you can do both.

Yeah, but that means that you can't use return inside your code block and instead has to define an error label for handling "normal" contention which is what I'm trying to avoid here.

How about:

#define drm_exec_until_all_locked(exec)    \
        __drm_exec_retry: if (drm_exec_cleanup(exec))

#define drm_exec_retry_on_contention(exec)              \
        if (unlikely(drm_exec_is_contended(exec)))      \
                goto __drm_exec_retry

Quoted 7 lines

And then use it like:

drm_exec_until_all_locked(exec)
{
    ret = drm_exec_prepare_obj(exec, obj);
    drm_exec_retry_on_contention(exec);
}

Quoted 4 lines

That would work, and I was about to suggest extending my proposal with a drm_exec_retry_on_contention() to support both use cases. The only downside is the fact you might be able to break out of a loop that has local variables, which will leak stack space.

Nevermind, brain fart on my end. It shouldn't leak any stack space, so yeah, I think that's a good compromise.

maintainer
#19 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

In reply to Christian König
Quoted 30 lines · On Mon, 19 Jun 2023 12:44:06 +0200 Christian König <[email protected]>…

Am 19.06.23 um 12:12 schrieb Boris Brezillon:

Quoted 20 lines

[SNIP]
Note that the drm_exec_until_all_locked() helper I introduced is taking an expression, so in theory, you don't have to define a separate function.

	drm_exec_until_all_locked(&exec, {
		/* inlined-code */
		int ret;

		ret = blabla()
		if (ret)
			goto error;

		...

error:

		/* return value. */
		ret;
	});
Quoted 8 lines

This being said, as soon as you have several failure paths, it makes things a lot easier/controllable if you make it a function, and I honestly don't think the readability would suffer from having a function defined just above the user. My main concern with the original approach was the risk of calling continue/break_if_contended() in the wrong place, and also the fact you can't really externalize things to a function if you're looking for a cleaner split. At least with drm_exec_until_all_locked() you can do both.

Quoted 3 lines

Yeah, but that means that you can't use return inside your code block and instead has to define an error label for handling "normal" contention which is what I'm trying to avoid here.

Sorry, didn't pay attention to this particular concern. Indeed, if you want to return inside the expression, that's a problem.

Quoted 14 lines

How about:

#define drm_exec_until_all_locked(exec)    \
        __drm_exec_retry: if (drm_exec_cleanup(exec))

#define drm_exec_retry_on_contention(exec)              \
        if (unlikely(drm_exec_is_contended(exec)))      \
                goto __drm_exec_retry

And then use it like:

Quoted 8 lines

drm_exec_until_all_locked(exec)
{
    ret = drm_exec_prepare_obj(exec, obj);
    drm_exec_retry_on_contention(exec);
}

The only problem I can see with this is that the __drm_exec_retry label would be function local.

Yeah, I'm not sure it's safe to use non-local labels for that, because, as soon as you have more than one drm_exec_until_all_locked() call in a given function it won't work, which is why I placed things in a block with local labels, which in turn means you can't return directly, unfortunately.

maintainer
#20 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

Quoted 33 lines · On Mon, 19 Jun 2023 14:29:23 +0200 Boris Brezillon…
Quoted 30 lines · On Mon, 19 Jun 2023 12:44:06 +0200 Christian König <[email protected]>…

Am 19.06.23 um 12:12 schrieb Boris Brezillon:

Quoted 28 lines

[SNIP]
Note that the drm_exec_until_all_locked() helper I introduced is taking an expression, so in theory, you don't have to define a separate function.

	drm_exec_until_all_locked(&exec, {
		/* inlined-code */
		int ret;

		ret = blabla()
		if (ret)
			goto error;

		...

error:

		/* return value. */
		ret;
	});

This being said, as soon as you have several failure paths, it makes things a lot easier/controllable if you make it a function, and I honestly don't think the readability would suffer from having a function defined just above the user. My main concern with the original approach was the risk of calling continue/break_if_contended() in the wrong place, and also the fact you can't really externalize things to a function if you're looking for a cleaner split. At least with drm_exec_until_all_locked() you can do both.

Quoted 6 lines
Quoted 3 lines

Yeah, but that means that you can't use return inside your code block and instead has to define an error label for handling "normal" contention which is what I'm trying to avoid here.

Sorry, didn't pay attention to this particular concern. Indeed, if you want to return inside the expression, that's a problem.

Sorry, that's wrong again. Had trouble focusing yesterday...

So, returning directly from the expression block should be perfectly fine. The only problem is breaking out of the retry loop early and propagating the error, but that's no more or less problematic than it was before. We just need the drm_exec_retry_on_contention() helper you suggested, and a drm_exec_stop() that would go to some local __drm_exec_stop label.

	int ret = 0;

	ret = drm_exec_until_all_locked(exec, ({
		...
		ret = drm_exec_prepare_obj(exec, objA, 1);
		drm_exec_retry_on_contention(exec);
		if (ret)
			drm_exec_stop(exec, ret);
		...

		ret = drm_exec_prepare_obj(exec, objB, 1);
		drm_exec_retry_on_contention(exec);
		if (ret)
			drm_exec_stop(exec, ret);

		0;
	}));

Which is pretty close to the syntax you defined initially, except for the '0;' oddity at the end, which is ugly, I admit.

maintainer
#21 lore raw

Re: [PATCH 01/13] drm: execution context for GEM buffers v4

Am 20.06.23 um 08:47 schrieb Boris Brezillon:

Quoted 67 lines
Quoted 32 lines · On Mon, 19 Jun 2023 14:29:23 +0200 Boris Brezillon…
Quoted 29 lines · On Mon, 19 Jun 2023 12:44:06 +0200 Christian König <[email protected]>…

Am 19.06.23 um 12:12 schrieb Boris Brezillon:

Quoted 28 lines

[SNIP]
Note that the drm_exec_until_all_locked() helper I introduced is taking an expression, so in theory, you don't have to define a separate function.

	drm_exec_until_all_locked(&exec, {
		/* inlined-code */
		int ret;

		ret = blabla()
		if (ret)
			goto error;

		...

error:

		/* return value. */
		ret;
	});

This being said, as soon as you have several failure paths, it makes things a lot easier/controllable if you make it a function, and I honestly don't think the readability would suffer from having a function defined just above the user. My main concern with the original approach was the risk of calling continue/break_if_contended() in the wrong place, and also the fact you can't really externalize things to a function if you're looking for a cleaner split. At least with drm_exec_until_all_locked() you can do both.

Quoted 5 lines
Quoted 3 lines

Yeah, but that means that you can't use return inside your code block and instead has to define an error label for handling "normal" contention which is what I'm trying to avoid here.

Sorry, didn't pay attention to this particular concern. Indeed, if you want to return inside the expression, that's a problem.

Sorry, that's wrong again. Had trouble focusing yesterday...

So, returning directly from the expression block should be perfectly fine. The only problem is breaking out of the retry loop early and propagating the error, but that's no more or less problematic than it was before. We just need the drm_exec_retry_on_contention() helper you suggested, and a drm_exec_stop() that would go to some local __drm_exec_stop label.

	int ret = 0;

	ret = drm_exec_until_all_locked(exec, ({
		...
		ret = drm_exec_prepare_obj(exec, objA, 1);
		drm_exec_retry_on_contention(exec);
		if (ret)
			drm_exec_stop(exec, ret);
		...

		ret = drm_exec_prepare_obj(exec, objB, 1);
		drm_exec_retry_on_contention(exec);
		if (ret)
			drm_exec_stop(exec, ret);

		0;
	}));
Quoted 2 lines

Which is pretty close to the syntax you defined initially, except for the '0;' oddity at the end, which is ugly, I admit.

Yeah and it looks like giving code blocks as macro argument is usually also not a good idea.

How about this:

#define drm_exec_until_all_locked(exec)                         \
        for (void *__drm_exec_retry_ptr; ({                     \
                __label__ __drm_exec_retry;                     \
__drm_exec_retry:                                               \
                __drm_exec_retry_ptr = &&__drm_exec_retry;      \
                drm_exec_cleanup(exec);                         \
        });)

#define drm_exec_retry_on_contention(exec)              \
        if (unlikely(drm_exec_is_contended(exec)))      \
                goto *__drm_exec_retry_ptr

The problem is that you can't declare a label so that it dominates the body of the loop.

But what you can do is to declare a void* which dominates the loop, then assign the address of a label to it and when you need it use goto*.

The goto* syntax is a gcc extension, but BPF is already using that in the upstream kernel.

It's quite a hack and I need to extend my testing a bit, but as far as I can see this actually works.

Regards,
Christian.

author reviewer
#22 lore raw

PATCH 2/13 drm: add drm_exec selftests v2

In reply to Christian König

Largely just the initial skeleton.

v2: add array test as well

---
 drivers/gpu/drm/Kconfig               |  1 +
 drivers/gpu/drm/tests/Makefile        |  3 +-
 drivers/gpu/drm/tests/drm_exec_test.c | 96 +++++++++++++++++++++++++++
 3 files changed, 99 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/tests/drm_exec_test.c
drivers/gpu/drm/Kconfig +1 -0
index 2dc81eb062eb..068e574e234e 100644
@@ -80,6 +80,7 @@ config DRM_KUNIT_TEST
 	select DRM_BUDDY
 	select DRM_EXPORT_FOR_TESTS if m
 	select DRM_KUNIT_TEST_HELPERS
+	select DRM_EXEC
 	default KUNIT_ALL_TESTS
 	help
 	  This builds unit tests for DRM. This option is not useful for
drivers/gpu/drm/tests/Makefile +2 -1
index bca726a8f483..ba7baa622675 100644
@@ -17,6 +17,7 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \
 	drm_modes_test.o \
 	drm_plane_helper_test.o \
 	drm_probe_helper_test.o \
-	drm_rect_test.o
+	drm_rect_test.o	\
+	drm_exec_test.o
 
 CFLAGS_drm_mm_test.o := $(DISABLE_STRUCTLEAK_PLUGIN)
drivers/gpu/drm/tests/drm_exec_test.c new file +96 -0

96 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#23 lore raw

Re: [PATCH 02/13] drm: add drm_exec selftests v2

Hi Christian,

It would be nice if you use the KUnit macros, instead of pr_info.

Quoted 60 lines · Christian König

Largely just the initial skeleton.

v2: add array test as well

---
  drivers/gpu/drm/Kconfig               |  1 +
  drivers/gpu/drm/tests/Makefile        |  3 +-
  drivers/gpu/drm/tests/drm_exec_test.c | 96 +++++++++++++++++++++++++++
  3 files changed, 99 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/tests/drm_exec_test.c

drivers/gpu/drm/Kconfig +1 -0
index 2dc81eb062eb..068e574e234e 100644
@@ -80,6 +80,7 @@ config DRM_KUNIT_TEST
  	select DRM_BUDDY
  	select DRM_EXPORT_FOR_TESTS if m
  	select DRM_KUNIT_TEST_HELPERS
+	select DRM_EXEC
  	default KUNIT_ALL_TESTS
  	help
  	  This builds unit tests for DRM. This option is not useful for
drivers/gpu/drm/tests/Makefile +2 -1
index bca726a8f483..ba7baa622675 100644
@@ -17,6 +17,7 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \
  	drm_modes_test.o \
  	drm_plane_helper_test.o \
  	drm_probe_helper_test.o \
-	drm_rect_test.o
+	drm_rect_test.o	\
+	drm_exec_test.o
 
  CFLAGS_drm_mm_test.o := $(DISABLE_STRUCTLEAK_PLUGIN)
drivers/gpu/drm/tests/drm_exec_test.c new file +17 -0
new file mode 100644
index 000000000000..26aa13e62d22
@@ -0,0 +1,96 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2019 Intel Corporation
+ */
+
+#define pr_fmt(fmt) "drm_exec: " fmt
+
+#include <kunit/test.h>
+
+#include <linux/module.h>
+#include <linux/prime_numbers.h>
+
+#include <drm/drm_exec.h>
+#include <drm/drm_device.h>
+#include <drm/drm_gem.h>
+
+#include "../lib/drm_random.h"

drivers/gpu/drm/tests/drm_exec_test.c line 27

Quoted 10 lines
drivers/gpu/drm/tests/drm_exec_test.c new file +10 -0
+
+static struct drm_device dev;
+
+static void drm_exec_sanitycheck(struct kunit *test)
+{
+	struct drm_exec exec;
+
+	drm_exec_init(&exec, true);
+	drm_exec_fini(&exec);
+	pr_info("%s - ok!\n", __func__);

Here you could use KUNIT_SUCCEED(test).

drivers/gpu/drm/tests/drm_exec_test.c line 30

Quoted 3 lines
drivers/gpu/drm/tests/drm_exec_test.c new file +3 -0
+}
+
+static void drm_exec_lock1(struct kunit *test)

Is there a reason to call the function drm_exec_lock1 instead of just drm_exec_lock?

Quoted 4 lines
drivers/gpu/drm/tests/drm_exec_test.c new file +4 -0
+{
+	struct drm_gem_object gobj = { };
+	struct drm_exec exec;
+	int ret;

drivers/gpu/drm/tests/drm_exec_test.c line 44

Quoted 10 lines
drivers/gpu/drm/tests/drm_exec_test.c new file +10 -0
+
+	drm_gem_private_object_init(&dev, &gobj, PAGE_SIZE);
+
+	drm_exec_init(&exec, true);
+	drm_exec_while_not_all_locked(&exec) {
+		ret = drm_exec_prepare_obj(&exec, &gobj, 1);
+		drm_exec_continue_on_contention(&exec);
+		if (ret) {
+			drm_exec_fini(&exec);
+			pr_err("%s - err %d!\n", __func__, ret);

Here you could use KUNIT_FAIL. Same for the other function.

Actually, it would be better if you created a function exit associated with the test suite, where you would call drm_exec_fini, and checked the ret variable with KUNIT_EXPECT_EQ(test, ret, 0) in the test.

Quoted 21 lines
drivers/gpu/drm/tests/drm_exec_test.c new file +21 -0
+			return;
+		}
+	}
+	drm_exec_fini(&exec);
+	pr_info("%s - ok!\n", __func__);
+}
+
+static void drm_exec_lock_array(struct kunit *test)
+{
+	struct drm_gem_object gobj1 = { };
+	struct drm_gem_object gobj2 = { };
+	struct drm_gem_object *array[] = { &gobj1, &gobj2 };
+	struct drm_exec exec;
+	int ret;
+
+	drm_gem_private_object_init(&dev, &gobj1, PAGE_SIZE);
+	drm_gem_private_object_init(&dev, &gobj2, PAGE_SIZE);
+
+	drm_exec_init(&exec, true);
+	ret = drm_exec_prepare_array(&exec, array, ARRAY_SIZE(array), 0);
+	if (ret) {

drivers/gpu/drm/tests/drm_exec_test.c line 76

Quoted 10 lines
drivers/gpu/drm/tests/drm_exec_test.c new file +10 -0
+		drm_exec_fini(&exec);
+		pr_err("%s - err %d!\n", __func__, ret);
+		return;
+	}
+	drm_exec_fini(&exec)> +	pr_info("%s - ok!\n", __func__);
+}
+
+static int drm_exec_suite_init(struct kunit_suite *suite)
+{
+	kunit_info(suite, "Testing DRM exec manager\n");

Isn't this already clear by the name of the test?

Best Regards,
- Maíra Canal

Quoted 20 lines
drivers/gpu/drm/tests/drm_exec_test.c new file +20 -0
+	return 0;
+}
+
+static struct kunit_case drm_exec_tests[] = {
+	KUNIT_CASE(drm_exec_sanitycheck),
+	KUNIT_CASE(drm_exec_lock1),
+	KUNIT_CASE(drm_exec_lock_array),
+	{}
+};
+
+static struct kunit_suite drm_exec_test_suite = {
+	.name = "drm_exec",
+	.suite_init = drm_exec_suite_init,
+	.test_cases = drm_exec_tests,
+};
+
+kunit_test_suite(drm_exec_test_suite);
+
+MODULE_AUTHOR("AMD");
+MODULE_LICENSE("GPL and additional rights");
author reviewer
#24 lore raw

Re: [PATCH 02/13] drm: add drm_exec selftests v2

Hi Maira,

Am 04.05.23 um 14:07 schrieb Maíra Canal:

Quoted 3 lines

Hi Christian,

It would be nice if you use the KUnit macros, instead of pr_info.

yeah this was initially written before the DRM tests moved to KUnit and I only quickly converted it over. Going to give this a cleanup.

Thanks,
Christian.

Quoted 160 lines · Christian König
Quoted 59 lines

Largely just the initial skeleton.

v2: add array test as well

---
  drivers/gpu/drm/Kconfig               |  1 + drivers/gpu/drm/tests/Makefile        |  3 +- drivers/gpu/drm/tests/drm_exec_test.c | 96 +++++++++++++++++++++++++++

  3 files changed, 99 insertions(+), 1 deletion(-)

  create mode 100644 drivers/gpu/drm/tests/drm_exec_test.c

drivers/gpu/drm/Kconfig +0 -0
index 2dc81eb062eb..068e574e234e 100644
@@ -80,6 +80,7 @@ config DRM_KUNIT_TEST

      select DRM_BUDDY
      select DRM_EXPORT_FOR_TESTS if m
      select DRM_KUNIT_TEST_HELPERS
+    select DRM_EXEC
      default KUNIT_ALL_TESTS
      help
        This builds unit tests for DRM. This option is not useful for

drivers/gpu/drm/tests/Makefile +2 -1
index bca726a8f483..ba7baa622675 100644
@@ -17,6 +17,7 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \
       drm_modes_test.o \
       drm_plane_helper_test.o \
       drm_probe_helper_test.o \
-    drm_rect_test.o
+    drm_rect_test.o    \
+    drm_exec_test.o
     CFLAGS_drm_mm_test.o := $(DISABLE_STRUCTLEAK_PLUGIN)
drivers/gpu/drm/tests/drm_exec_test.c new file +17 -0
new file mode 100644
index 000000000000..26aa13e62d22
@@ -0,0 +1,96 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2019 Intel Corporation
+ */
+
+#define pr_fmt(fmt) "drm_exec: " fmt
+
+#include <kunit/test.h>
+
+#include <linux/module.h>
+#include <linux/prime_numbers.h>
+
+#include <drm/drm_exec.h>
+#include <drm/drm_device.h>
+#include <drm/drm_gem.h>
+
+#include "../lib/drm_random.h"

drivers/gpu/drm/tests/drm_exec_test.c line 27

Quoted 10 lines
drivers/gpu/drm/tests/drm_exec_test.c new file +10 -0
+
+static struct drm_device dev;
+
+static void drm_exec_sanitycheck(struct kunit *test)
+{
+    struct drm_exec exec;
+
+    drm_exec_init(&exec, true);
+    drm_exec_fini(&exec);
+    pr_info("%s - ok!\n", __func__);

Here you could use KUNIT_SUCCEED(test).

drivers/gpu/drm/tests/drm_exec_test.c line 30

Quoted 3 lines
drivers/gpu/drm/tests/drm_exec_test.c new file +3 -0
+}
+
+static void drm_exec_lock1(struct kunit *test)

Is there a reason to call the function drm_exec_lock1 instead of just drm_exec_lock?

Quoted 4 lines
drivers/gpu/drm/tests/drm_exec_test.c new file +4 -0
+{
+    struct drm_gem_object gobj = { };
+    struct drm_exec exec;
+    int ret;

drivers/gpu/drm/tests/drm_exec_test.c line 44

Quoted 10 lines
drivers/gpu/drm/tests/drm_exec_test.c new file +10 -0
+
+    drm_gem_private_object_init(&dev, &gobj, PAGE_SIZE);
+
+    drm_exec_init(&exec, true);
+    drm_exec_while_not_all_locked(&exec) {
+        ret = drm_exec_prepare_obj(&exec, &gobj, 1);
+        drm_exec_continue_on_contention(&exec);
+        if (ret) {
+            drm_exec_fini(&exec);
+            pr_err("%s - err %d!\n", __func__, ret);

Here you could use KUNIT_FAIL. Same for the other function.

Actually, it would be better if you created a function exit associated with the test suite, where you would call drm_exec_fini, and checked the ret variable with KUNIT_EXPECT_EQ(test, ret, 0) in the test.

Quoted 21 lines
drivers/gpu/drm/tests/drm_exec_test.c new file +21 -0
+            return;
+        }
+    }
+    drm_exec_fini(&exec);
+    pr_info("%s - ok!\n", __func__);
+}
+
+static void drm_exec_lock_array(struct kunit *test)
+{
+    struct drm_gem_object gobj1 = { };
+    struct drm_gem_object gobj2 = { };
+    struct drm_gem_object *array[] = { &gobj1, &gobj2 };
+    struct drm_exec exec;
+    int ret;
+
+    drm_gem_private_object_init(&dev, &gobj1, PAGE_SIZE);
+    drm_gem_private_object_init(&dev, &gobj2, PAGE_SIZE);
+
+    drm_exec_init(&exec, true);
+    ret = drm_exec_prepare_array(&exec, array, ARRAY_SIZE(array), 0);
+    if (ret) {

drivers/gpu/drm/tests/drm_exec_test.c line 76

Quoted 10 lines
drivers/gpu/drm/tests/drm_exec_test.c new file +10 -0
+        drm_exec_fini(&exec);
+        pr_err("%s - err %d!\n", __func__, ret);
+        return;
+    }
+    drm_exec_fini(&exec)> +    pr_info("%s - ok!\n", __func__);
+}
+
+static int drm_exec_suite_init(struct kunit_suite *suite)
+{
+    kunit_info(suite, "Testing DRM exec manager\n");

Isn't this already clear by the name of the test?

Best Regards,
- Maíra Canal

Quoted 20 lines
drivers/gpu/drm/tests/drm_exec_test.c new file +20 -0
+    return 0;
+}
+
+static struct kunit_case drm_exec_tests[] = {
+    KUNIT_CASE(drm_exec_sanitycheck),
+    KUNIT_CASE(drm_exec_lock1),
+    KUNIT_CASE(drm_exec_lock_array),
+    {}
+};
+
+static struct kunit_suite drm_exec_test_suite = {
+    .name = "drm_exec",
+    .suite_init = drm_exec_suite_init,
+    .test_cases = drm_exec_tests,
+};
+
+kunit_test_suite(drm_exec_test_suite);
+
+MODULE_AUTHOR("AMD");
+MODULE_LICENSE("GPL and additional rights");
author reviewer
#25 lore raw

PATCH 3/13 drm/amdkfd: switch over to using drm_exec v2

In reply to Christian König

Avoids quite a bit of logic and kmalloc overhead.

v2: fix multiple problems pointed out by Felix

---
 drivers/gpu/drm/amd/amdgpu/Kconfig            |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |   5 +-
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 302 +++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        |  14 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |   3 +
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c          |  46 +--
 6 files changed, 161 insertions(+), 210 deletions(-)
drivers/gpu/drm/amd/amdgpu/Kconfig +1 -0
index 12adca8c7819..fcad4ea30a0c 100644
@@ -21,6 +21,7 @@ config DRM_AMDGPU
 	select INTERVAL_TREE
 	select DRM_BUDDY
 	select DRM_SUBALLOC_HELPER
+	select DRM_EXEC
 	# amdgpu depends on ACPI_VIDEO when ACPI is enabled, for select to work
 	# ACPI_VIDEO's dependencies must also be selected.
 	select INPUT if ACPI
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2 -3
index 01ba3589b60a..dfb41d56d236 100644
@@ -25,13 +25,13 @@
 #ifndef AMDGPU_AMDKFD_H_INCLUDED
 #define AMDGPU_AMDKFD_H_INCLUDED
 
+#include <linux/list.h>
 #include <linux/types.h>
 #include <linux/mm.h>
 #include <linux/kthread.h>
 #include <linux/workqueue.h>
 #include <linux/mmu_notifier.h>
 #include <kgd_kfd_interface.h>
-#include <drm/ttm/ttm_execbuf_util.h>
 #include "amdgpu_sync.h"
 #include "amdgpu_vm.h"
 
@@ -69,8 +69,7 @@ struct kgd_mem {
 	struct hmm_range *range;
 	struct list_head attachments;
 	/* protected by amdkfd_process_info.lock */
-	struct ttm_validate_buffer validate_list;
-	struct ttm_validate_buffer resv_list;
+	struct list_head validate_list;
 	uint32_t domain;
 	unsigned int mapped_to_gpu_memory;
 	uint64_t va;
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +118 -184

467 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +14 -0
index 3c0310576b3b..594442d23242 100644
@@ -34,6 +34,7 @@
 #include <drm/amdgpu_drm.h>
 #include <drm/drm_drv.h>
 #include <drm/ttm/ttm_tt.h>
+#include <drm/drm_exec.h>
 #include "amdgpu.h"
 #include "amdgpu_trace.h"
 #include "amdgpu_amdkfd.h"
@@ -334,6 +335,19 @@ void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
 	list_add(&entry->tv.head, validated);
 }
 
+/**
+ * amdgpu_vm_lock_pd - lock PD in drm_exec
+ *
+ * @vm: vm providing the BOs
+ * @exec: drm execution context
+ *
+ * Lock the VM root PD in the DRM execution context.
+ */
+int amdgpu_vm_lock_pd(struct amdgpu_vm *vm, struct drm_exec *exec)
+{
+	return drm_exec_prepare_obj(exec, &vm->root.bo->tbo.base, 4);
+}
+
 /**
  * amdgpu_vm_move_to_lru_tail - move all BOs to the end of LRU
  *
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -0
index 6f085f0b4ef3..ed987f73e3a6 100644
@@ -36,6 +36,8 @@
 #include "amdgpu_ring.h"
 #include "amdgpu_ids.h"
 
+struct drm_exec;
+
 struct amdgpu_bo_va;
 struct amdgpu_job;
 struct amdgpu_bo_list_entry;
@@ -390,6 +392,7 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
 void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
 			 struct list_head *validated,
 			 struct amdgpu_bo_list_entry *entry);
+int amdgpu_vm_lock_pd(struct amdgpu_vm *vm, struct drm_exec *exec);
 bool amdgpu_vm_ready(struct amdgpu_vm *vm);
 int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			      int (*callback)(void *p, struct amdgpu_bo *bo),
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +23 -23

76 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#26 lore raw

PATCH 4/13 drm/amdgpu: use drm_exec for GEM and CSA handling

In reply to Christian König

Start using the new component here as well.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c | 42 ++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 77 +++++++++++--------------
 2 files changed, 53 insertions(+), 66 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c +19 -23
index c6d4d41c4393..ea434c8de047 100644
@@ -22,6 +22,8 @@
  * * Author: [email protected]
  */
 
+#include <drm/drm_exec.h>
+
 #include "amdgpu.h"
 
 uint64_t amdgpu_csa_vaddr(struct amdgpu_device *adev)
@@ -65,31 +67,25 @@ int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			  struct amdgpu_bo *bo, struct amdgpu_bo_va **bo_va,
 			  uint64_t csa_addr, uint32_t size)
 {
-	struct ww_acquire_ctx ticket;
-	struct list_head list;
-	struct amdgpu_bo_list_entry pd;
-	struct ttm_validate_buffer csa_tv;
+	struct drm_exec exec;
 	int r;
 
-	INIT_LIST_HEAD(&list);
-	INIT_LIST_HEAD(&csa_tv.head);
-	csa_tv.bo = &bo->tbo;
-	csa_tv.num_shared = 1;
-
-	list_add(&csa_tv.head, &list);
-	amdgpu_vm_get_pd_bo(vm, &list, &pd);
-
-	r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
-	if (r) {
-		DRM_ERROR("failed to reserve CSA,PD BOs: err=%d\n", r);
-		return r;
+	drm_exec_init(&exec, true);
+	drm_exec_while_not_all_locked(&exec) {
+		r = amdgpu_vm_lock_pd(vm, &exec);
+		if (likely(!r))
+			r = drm_exec_prepare_obj(&exec, &bo->tbo.base, 0);
+		drm_exec_continue_on_contention(&exec);
+		if (unlikely(r)) {
+			DRM_ERROR("failed to reserve CSA,PD BOs: err=%d\n", r);
+			goto error;
+		}
 	}
 
 	*bo_va = amdgpu_vm_bo_add(adev, vm, bo);
 	if (!*bo_va) {
-		ttm_eu_backoff_reservation(&ticket, &list);
-		DRM_ERROR("failed to create bo_va for static CSA\n");
-		return -ENOMEM;
+		r = -ENOMEM;
+		goto error;
 	}
 
 	r = amdgpu_vm_bo_map(adev, *bo_va, csa_addr, 0, size,
@@ -99,10 +95,10 @@ int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (r) {
 		DRM_ERROR("failed to do bo_map on static CSA, err=%d\n", r);
 		amdgpu_vm_bo_del(adev, *bo_va);
-		ttm_eu_backoff_reservation(&ticket, &list);
-		return r;
+		goto error;
 	}
 
-	ttm_eu_backoff_reservation(&ticket, &list);
-	return 0;
+error:
+	drm_exec_fini(&exec);
+	return r;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +34 -43

141 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#27 lore raw

PATCH 5/13 drm/amdgpu: use drm_exec for MES testing

In reply to Christian König

Start using the new component here as well.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c | 86 +++++++++++--------------
 1 file changed, 39 insertions(+), 47 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +39 -47
index f0f00466b59f..bfa9006600dd 100644
@@ -22,6 +22,7 @@
  */
 
 #include <linux/firmware.h>
+#include <drm/drm_exec.h>
 
 #include "amdgpu_mes.h"
 #include "amdgpu.h"
@@ -1131,34 +1132,29 @@ int amdgpu_mes_ctx_map_meta_data(struct amdgpu_device *adev,
 				 struct amdgpu_mes_ctx_data *ctx_data)
 {
 	struct amdgpu_bo_va *bo_va;
-	struct ww_acquire_ctx ticket;
-	struct list_head list;
-	struct amdgpu_bo_list_entry pd;
-	struct ttm_validate_buffer csa_tv;
 	struct amdgpu_sync sync;
+	struct drm_exec exec;
 	int r;
 
 	amdgpu_sync_create(&sync);
-	INIT_LIST_HEAD(&list);
-	INIT_LIST_HEAD(&csa_tv.head);
 
-	csa_tv.bo = &ctx_data->meta_data_obj->tbo;
-	csa_tv.num_shared = 1;
-
-	list_add(&csa_tv.head, &list);
-	amdgpu_vm_get_pd_bo(vm, &list, &pd);
-
-	r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
-	if (r) {
-		DRM_ERROR("failed to reserve meta data BO: err=%d\n", r);
-		return r;
+	drm_exec_init(&exec, false);
+	drm_exec_while_not_all_locked(&exec) {
+		r = drm_exec_prepare_obj(&exec,
+					 &ctx_data->meta_data_obj->tbo.base,
+					 0);
+		if (likely(!r))
+			r = amdgpu_vm_lock_pd(vm, &exec);
+		drm_exec_continue_on_contention(&exec);
+                if (unlikely(r))
+			goto error_fini_exec;
 	}
 
 	bo_va = amdgpu_vm_bo_add(adev, vm, ctx_data->meta_data_obj);
 	if (!bo_va) {
-		ttm_eu_backoff_reservation(&ticket, &list);
 		DRM_ERROR("failed to create bo_va for meta data BO\n");
-		return -ENOMEM;
+		r = -ENOMEM;
+		goto error_fini_exec;
 	}
 
 	r = amdgpu_vm_bo_map(adev, bo_va, ctx_data->meta_data_gpu_addr, 0,
@@ -1168,33 +1164,35 @@ int amdgpu_mes_ctx_map_meta_data(struct amdgpu_device *adev,
 
 	if (r) {
 		DRM_ERROR("failed to do bo_map on meta data, err=%d\n", r);
-		goto error;
+		goto error_del_bo_va;
 	}
 
 	r = amdgpu_vm_bo_update(adev, bo_va, false);
 	if (r) {
 		DRM_ERROR("failed to do vm_bo_update on meta data\n");
-		goto error;
+		goto error_del_bo_va;
 	}
 	amdgpu_sync_fence(&sync, bo_va->last_pt_update);
 
 	r = amdgpu_vm_update_pdes(adev, vm, false);
 	if (r) {
 		DRM_ERROR("failed to update pdes on meta data\n");
-		goto error;
+		goto error_del_bo_va;
 	}
 	amdgpu_sync_fence(&sync, vm->last_update);
 
 	amdgpu_sync_wait(&sync, false);
-	ttm_eu_backoff_reservation(&ticket, &list);
+	drm_exec_fini(&exec);
 
 	amdgpu_sync_free(&sync);
 	ctx_data->meta_data_va = bo_va;
 	return 0;
 
-error:
+error_del_bo_va:
 	amdgpu_vm_bo_del(adev, bo_va);
-	ttm_eu_backoff_reservation(&ticket, &list);
+
+error_fini_exec:
+	drm_exec_fini(&exec);
 	amdgpu_sync_free(&sync);
 	return r;
 }
@@ -1205,34 +1203,28 @@ int amdgpu_mes_ctx_unmap_meta_data(struct amdgpu_device *adev,
 	struct amdgpu_bo_va *bo_va = ctx_data->meta_data_va;
 	struct amdgpu_bo *bo = ctx_data->meta_data_obj;
 	struct amdgpu_vm *vm = bo_va->base.vm;
-	struct amdgpu_bo_list_entry vm_pd;
-	struct list_head list, duplicates;
-	struct dma_fence *fence = NULL;
-	struct ttm_validate_buffer tv;
-	struct ww_acquire_ctx ticket;
-	long r = 0;
-
-	INIT_LIST_HEAD(&list);
-	INIT_LIST_HEAD(&duplicates);
-
-	tv.bo = &bo->tbo;
-	tv.num_shared = 2;
-	list_add(&tv.head, &list);
-
-	amdgpu_vm_get_pd_bo(vm, &list, &vm_pd);
-
-	r = ttm_eu_reserve_buffers(&ticket, &list, false, &duplicates);
-	if (r) {
-		dev_err(adev->dev, "leaking bo va because "
-			"we fail to reserve bo (%ld)\n", r);
-		return r;
+	struct dma_fence *fence;
+	struct drm_exec exec;
+	long r;
+
+	drm_exec_init(&exec, false);
+	drm_exec_while_not_all_locked(&exec) {
+		r = drm_exec_prepare_obj(&exec,
+					 &ctx_data->meta_data_obj->tbo.base,
+					 0);
+		if (likely(!r))
+			r = amdgpu_vm_lock_pd(vm, &exec);
+		drm_exec_continue_on_contention(&exec);
+                if (unlikely(r))
+			goto out_unlock;
 	}
 
 	amdgpu_vm_bo_del(adev, bo_va);
 	if (!amdgpu_vm_ready(vm))
 		goto out_unlock;
 
-	r = dma_resv_get_singleton(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP, &fence);
+	r = dma_resv_get_singleton(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP,
+				   &fence);
 	if (r)
 		goto out_unlock;
 	if (fence) {
@@ -1251,7 +1243,7 @@ int amdgpu_mes_ctx_unmap_meta_data(struct amdgpu_device *adev,
 out_unlock:
 	if (unlikely(r < 0))
 		dev_err(adev->dev, "failed to clear page tables (%ld)\n", r);
-	ttm_eu_backoff_reservation(&ticket, &list);
+	drm_exec_fini(&exec);
 
 	return r;
 }
Signature · 1 line
2.34.1
author reviewer
#28 lore raw

PATCH 6/13 drm/amdgpu: use the new drm_exec object for CS v2

In reply to Christian König

Use the new component here as well and remove the old handling.

v2: drop dupplicate handling

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h         |   1 -
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c |  71 ++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h |   5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c      | 210 +++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h      |   7 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c      |  22 --
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h      |   3 -
 7 files changed, 115 insertions(+), 204 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +0 -1
index 02b827785e39..eba3e4f01ea6 100644
@@ -53,7 +53,6 @@
 
 #include <drm/ttm/ttm_bo.h>
 #include <drm/ttm/ttm_placement.h>
-#include <drm/ttm/ttm_execbuf_util.h>
 
 #include <drm/amdgpu_drm.h>
 #include <drm/drm_gem.h>
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +20 -51

109 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +1 -4
index ededdc01ca28..26c01cb131f2 100644
@@ -23,7 +23,6 @@
 #ifndef __AMDGPU_BO_LIST_H__
 #define __AMDGPU_BO_LIST_H__
 
-#include <drm/ttm/ttm_execbuf_util.h>
 #include <drm/amdgpu_drm.h>
 
 struct hmm_range;
@@ -36,7 +35,7 @@ struct amdgpu_bo_va;
 struct amdgpu_fpriv;
 
 struct amdgpu_bo_list_entry {
-	struct ttm_validate_buffer	tv;
+	struct amdgpu_bo		*bo;
 	struct amdgpu_bo_va		*bo_va;
 	uint32_t			priority;
 	struct page			**user_pages;
@@ -60,8 +59,6 @@ struct amdgpu_bo_list {
 
 int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
 		       struct amdgpu_bo_list **result);
-void amdgpu_bo_list_get_list(struct amdgpu_bo_list *list,
-			     struct list_head *validated);
 void amdgpu_bo_list_put(struct amdgpu_bo_list *list);
 int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
 				      struct drm_amdgpu_bo_list_entry **info_param);
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +91 -119

402 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h +3 -4
index fb3e3d56d427..39c33ad100cb 100644
@@ -24,6 +24,7 @@
 #define __AMDGPU_CS_H__
 
 #include <linux/ww_mutex.h>
+#include <drm/drm_exec.h>
 
 #include "amdgpu_job.h"
 #include "amdgpu_bo_list.h"
@@ -62,11 +63,9 @@ struct amdgpu_cs_parser {
 	struct amdgpu_job	*gang_leader;
 
 	/* buffer objects */
-	struct ww_acquire_ctx		ticket;
+	struct drm_exec			exec;
 	struct amdgpu_bo_list		*bo_list;
 	struct amdgpu_mn		*mn;
-	struct amdgpu_bo_list_entry	vm_pd;
-	struct list_head		validated;
 	struct dma_fence		*fence;
 	uint64_t			bytes_moved_threshold;
 	uint64_t			bytes_moved_vis_threshold;
@@ -74,7 +73,7 @@ struct amdgpu_cs_parser {
 	uint64_t			bytes_moved_vis;
 
 	/* user fence */
-	struct amdgpu_bo_list_entry	uf_entry;
+	struct amdgpu_bo		*uf_bo;
 
 	unsigned			num_post_deps;
 	struct amdgpu_cs_post_dep	*post_deps;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -22
index 594442d23242..1dbe75b008a3 100644
@@ -313,28 +313,6 @@ void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
 	amdgpu_vm_bo_evicted(base);
 }
 
-/**
- * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
- *
- * @vm: vm providing the BOs
- * @validated: head of validation list
- * @entry: entry to add
- *
- * Add the page directory to the list of BOs to
- * validate for command submission.
- */
-void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
-			 struct list_head *validated,
-			 struct amdgpu_bo_list_entry *entry)
-{
-	entry->priority = 0;
-	entry->tv.bo = &vm->root.bo->tbo;
-	/* Two for VM updates, one for TTM and one for the CS job */
-	entry->tv.num_shared = 4;
-	entry->user_pages = NULL;
-	list_add(&entry->tv.head, validated);
-}
-
 /**
  * amdgpu_vm_lock_pd - lock PD in drm_exec
  *
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -3
index ed987f73e3a6..38902fbce8f8 100644
@@ -389,9 +389,6 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm);
 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm);
 void amdgpu_vm_release_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm);
 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
-void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
-			 struct list_head *validated,
-			 struct amdgpu_bo_list_entry *entry);
 int amdgpu_vm_lock_pd(struct amdgpu_vm *vm, struct drm_exec *exec);
 bool amdgpu_vm_ready(struct amdgpu_vm *vm);
 int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
Signature · 1 line
2.34.1
#29 lore raw

Re: [PATCH 06/13] drm/amdgpu: use the new drm_exec object for CS v2

Hi Chrisitan,

Quoted 4 lines · On May 4, 2023, at 20:51, Christian König <[email protected]…

Use the new component here as well and remove the old handling.

v2: drop dupplicate handling

It seems that after dropping the duplicate handling, locking of VM PDs on global BO list is basically broken everywhere, as bo->tbo.base.resv == vm->root.bo->tbo.base.resv for AMDGPU_GEM_CREATE_VM_ALWAYS_VALID

Perhaps we need to bring dup handling back?

Tatsuyuki

#30 lore raw

Re: [PATCH 06/13] drm/amdgpu: use the new drm_exec object for CS v2

In reply to Christian König

+Boris and +Matthew in case you want to take over this patch set.

Here are some review / testing comments, including those I posted before to ease tracking.

Quoted 20 lines · Christian König

Use the new component here as well and remove the old handling.

v2: drop dupplicate handling

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h         |   1 -
  drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c |  71 ++-----
  drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h |   5 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c      | 210 +++++++++-----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h      |   7 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c      |  22 --
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h      |   3 -
  7 files changed, 115 insertions(+), 204 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +0 -0
index 02b827785e39..eba3e4f01ea6 100644

drivers/gpu/drm/amd/amdgpu/amdgpu.h int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp, · line 145

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
@@ -133,6 +141,8 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
 
  	list->first_userptr = first_userptr;
  	list->num_entries = num_entries;
+	sort(array, last_entry, sizeof(struct amdgpu_bo_list_entry),
+	     amdgpu_bo_list_entry_cmp, NULL);

Previously amdgpu_bo_list_get_list sorted all entries, but this one only sorts userptr entries. I think this changes behavior?

drivers/gpu/drm/amd/amdgpu/amdgpu.h static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, · line 879

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +3 -6
@@ -928,18 +874,56 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  		e->user_invalidated = userpage_invalidated;
  	}
 
-	r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
-				   &duplicates);
-	if (unlikely(r != 0)) {
-		if (r != -ERESTARTSYS)
-			DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
-		goto out_free_user_pages;
+	drm_exec_while_not_all_locked(&p->exec) {
+		r = amdgpu_vm_lock_pd(&fpriv->vm, &p->exec);
+		drm_exec_continue_on_contention(&p->exec);

Duplicate handling is needed for pretty much every call of amdgpu_vm_lock_pd, as bo->tbo.base.resv == vm->root.bo->tbo.base.resv for AMDGPU_GEM_CREATE_VM_ALWAYS_VALID.

I think Boris's suggestion of having this through a common DRM_EXEC_FLAG_ALLOW_DUPLICATES flag fits well.

drivers/gpu/drm/amd/amdgpu/amdgpu.h static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, · line 884

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
+		if (unlikely(r))
+			goto out_free_user_pages;
+
+		amdgpu_bo_list_for_each_entry(e, p->bo_list) {
+			r = drm_exec_prepare_obj(&p->exec, &e->bo->tbo.base, 2);

Previously there were comments for how the fence count is calculated, now they seem to be removed. I'd prefer if they were properly retained as finding out who calls drm_resv_add_fence is not trivial, and wrong reservation count can also be really hard to debug.

Likewise for amdgpu_vm_lock_pd (which was added in another commit).

Quoted 49 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +39 -3
+			drm_exec_break_on_contention(&p->exec);
+			if (unlikely(r))
+				goto out_free_user_pages;
+
+			e->bo_va = amdgpu_vm_bo_find(vm, e->bo);
+			e->range = NULL;
+		}
+		drm_exec_continue_on_contention(&p->exec);
+
+		if (p->uf_bo) {
+			r = drm_exec_prepare_obj(&p->exec, &p->uf_bo->tbo.base,
+						 2);
+			drm_exec_continue_on_contention(&p->exec);
+			if (unlikely(r))
+				goto out_free_user_pages;
+		}
  	}
 
-	amdgpu_bo_list_for_each_entry(e, p->bo_list) {
-		struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
+	amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
+		struct mm_struct *usermm;
 
-		e->bo_va = amdgpu_vm_bo_find(vm, bo);
+		usermm = amdgpu_ttm_tt_get_usermm(e->bo->tbo.ttm);
+		if (usermm && usermm != current->mm) {
+			r = -EPERM;
+			goto out_free_user_pages;
+		}
+
+		if (amdgpu_ttm_tt_is_userptr(e->bo->tbo.ttm) &&
+		    e->user_invalidated && e->user_pages) {
+			amdgpu_bo_placement_from_domain(e->bo,
+							AMDGPU_GEM_DOMAIN_CPU);
+			r = ttm_bo_validate(&e->bo->tbo, &e->bo->placement,
+					    &ctx);
+			if (r)
+				goto out_free_user_pages;
+
+			amdgpu_ttm_tt_set_user_pages(e->bo->tbo.ttm,
+						     e->user_pages);
+		}
+
+		kvfree(e->user_pages);
+		e->user_pages = NULL;
  	}
 
  	amdgpu_cs_get_threshold_for_moves(p->adev, &p->bytes_moved_threshold,

drivers/gpu/drm/amd/amdgpu/amdgpu.h static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, · line 1276

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -3
@@ -1296,9 +1271,8 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  	 */
  	r = 0;
  	amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
-		struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
-
-		r |= !amdgpu_ttm_tt_get_user_pages_done(bo->tbo.ttm, e->range);
+		r |= !amdgpu_ttm_tt_get_user_pages_done(e->bo->tbo.ttm,
+							e->range);
  		e->range = NULL;

e->range = NULL; needs to be removed, or it's causing *massive* memory leaks.

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +3 -2
  	}
  	if (r) {
@@ -1307,20 +1281,22 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
  	}
 
  	p->fence = dma_fence_get(&leader->base.s_fence->finished);
-	list_for_each_entry(e, &p->validated, tv.head) {
+	drm_exec_for_each_locked_object(&p->exec, index, gobj) {
+
+		ttm_bo_move_to_lru_tail_unlocked(&gem_to_amdgpu_bo(gobj)->tbo);
 
  		/* Everybody except for the gang leader uses READ */
  		for (i = 0; i < p->gang_size; ++i) {
  			if (p->jobs[i] == leader)
  				continue;
 
-			dma_resv_add_fence(e->tv.bo->base.resv,

drivers/gpu/drm/amd/amdgpu/amdgpu.h static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, · line 1300

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +3 -2
+			dma_resv_add_fence(gobj->resv,
  					   &p->jobs[i]->base.s_fence->finished,
  					   DMA_RESV_USAGE_READ);
  		}
 
-		/* The gang leader is remembered as writer */
-		e->tv.num_shared = 0;
+		/* The gang leader as remembered as writer */
+		dma_resv_add_fence(gobj->resv, p->fence, DMA_RESV_USAGE_WRITE);
  	}

Previously PD used READ accesses, now everything is WRITE. This probably isn't right.

Regards,
Tatsuyuki

#31 lore raw

Re: [PATCH 06/13] drm/amdgpu: use the new drm_exec object for CS v2

Quoted 5 lines · Tatsuyuki Ishi
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -0
@@ -1296,9 +1271,8 @@ static int amdgpu_cs_submit(struct 
 amdgpu_cs_parser *p,
        */
       r = 0;
       amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, · line 1276

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2 -3
-        struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
-
-        r |= !amdgpu_ttm_tt_get_user_pages_done(bo->tbo.ttm, e->range);
+        r |= !amdgpu_ttm_tt_get_user_pages_done(e->bo->tbo.ttm,
+                            e->range);
           e->range = NULL;

e->range = NULL; needs to be removed, or it's causing *massive* memory leaks.

Actually, I quoted the wrong hunk, the correct one is below.

Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +4 -6
@@ -928,18 +874,56 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 		e->user_invalidated = userpage_invalidated;
 	}
 
-	r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
-				   &duplicates);
-	if (unlikely(r != 0)) {
-		if (r != -ERESTARTSYS)
-			DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
-		goto out_free_user_pages;
+	drm_exec_while_not_all_locked(&p->exec) {
+		r = amdgpu_vm_lock_pd(&fpriv->vm, &p->exec);
+		drm_exec_continue_on_contention(&p->exec);
+		if (unlikely(r))

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, · line 890

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +10 -0
+			goto out_free_user_pages;
+
+		amdgpu_bo_list_for_each_entry(e, p->bo_list) {
+			r = drm_exec_prepare_obj(&p->exec, &e->bo->tbo.base, 2);
+			drm_exec_break_on_contention(&p->exec);
+			if (unlikely(r))
+				goto out_free_user_pages;
+
+			e->bo_va = amdgpu_vm_bo_find(vm, e->bo);
+			e->range = NULL;

This causes the leak.

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, · line 901

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +10 -0
+		}
+		drm_exec_continue_on_contention(&p->exec);
+
+		if (p->uf_bo) {
+			r = drm_exec_prepare_obj(&p->exec, &p->uf_bo->tbo.base,
+						 2);
+			drm_exec_continue_on_contention(&p->exec);
+			if (unlikely(r))
+				goto out_free_user_pages;
+		}
 	}

Tatsuyuki

author reviewer
#32 lore raw

Re: [PATCH 06/13] drm/amdgpu: use the new drm_exec object for CS v2

Am 20.06.23 um 06:14 schrieb Tatsuyuki Ishi:

Quoted 16 lines · Tatsuyuki Ishi
Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0 -1
@@ -1296,9 +1271,8 @@ static int amdgpu_cs_submit(struct 
 amdgpu_cs_parser *p,
        */
       r = 0;
       amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
-        struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, · line 1276

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2 -2
-
-        r |= !amdgpu_ttm_tt_get_user_pages_done(bo->tbo.ttm, 
 e->range);
+        r |= !amdgpu_ttm_tt_get_user_pages_done(e->bo->tbo.ttm,
+                            e->range);
           e->range = NULL;

e->range = NULL; needs to be removed, or it's causing *massive* memory leaks.

Quoted 1 line

Actually, I quoted the wrong hunk, the correct one is below.

Ah, yes that makes more sense. Going to take a look.

Thanks,
Christian.

Quoted 42 lines
Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +4 -6
@@ -928,18 +874,56 @@ static int amdgpu_cs_parser_bos(struct 
 amdgpu_cs_parser *p,
          e->user_invalidated = userpage_invalidated;
      }
 
-    r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
-                   &duplicates);
-    if (unlikely(r != 0)) {
-        if (r != -ERESTARTSYS)
-            DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
-        goto out_free_user_pages;
+    drm_exec_while_not_all_locked(&p->exec) {
+        r = amdgpu_vm_lock_pd(&fpriv->vm, &p->exec);
+        drm_exec_continue_on_contention(&p->exec);
+        if (unlikely(r))

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_parser_bos(struct · line 891

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +10 -0
+            goto out_free_user_pages;
+
+        amdgpu_bo_list_for_each_entry(e, p->bo_list) {
+            r = drm_exec_prepare_obj(&p->exec, &e->bo->tbo.base, 2);
+            drm_exec_break_on_contention(&p->exec);
+            if (unlikely(r))
+                goto out_free_user_pages;
+
+            e->bo_va = amdgpu_vm_bo_find(vm, e->bo);
+            e->range = NULL;

This causes the leak.

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, · line 901

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +10 -0
+        }
+        drm_exec_continue_on_contention(&p->exec);
+
+        if (p->uf_bo) {
+            r = drm_exec_prepare_obj(&p->exec, &p->uf_bo->tbo.base,
+                         2);
+            drm_exec_continue_on_contention(&p->exec);
+            if (unlikely(r))
+                goto out_free_user_pages;
+        }
      }

Tatsuyuki

author reviewer
#33 lore raw

Re: [PATCH 06/13] drm/amdgpu: use the new drm_exec object for CS v2

In reply to Tatsuyuki Ishi

Am 20.06.23 um 06:07 schrieb Tatsuyuki Ishi:

Quoted 20 lines

+Boris and +Matthew in case you want to take over this patch set.

Here are some review / testing comments, including those I posted before to ease tracking.

Quoted 15 lines · Christian König

Use the new component here as well and remove the old handling.

v2: drop dupplicate handling

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h         |   1 - drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c |  71 ++----- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h |   5 +-

  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c      | 210 +++++++++-----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h      |   7 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c      |  22 --
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h      |   3 -
  7 files changed, 115 insertions(+), 204 deletions(-)
Quoted 12 lines

drivers/gpu/drm/amd/amdgpu/amdgpu.h int amdgpu_bo_list_create(struct amdgpu_device · line 145

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 02b827785e39..eba3e4f01ea6 100644
@@ -133,6 +141,8 @@ int amdgpu_bo_list_create(struct amdgpu_device 
 *adev, struct drm_file *filp,
         list->first_userptr = first_userptr;
       list->num_entries = num_entries;
+    sort(array, last_entry, sizeof(struct amdgpu_bo_list_entry),
+         amdgpu_bo_list_entry_cmp, NULL);

Previously amdgpu_bo_list_get_list sorted all entries, but this one only sorts userptr entries. I think this changes behavior?

The intention here is to sort all entries except the userptrs. Need to double check.

Quoted 16 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +0 -0
@@ -928,18 +874,56 @@ static int amdgpu_cs_parser_bos(struct 
 amdgpu_cs_parser *p,
           e->user_invalidated = userpage_invalidated;

drivers/gpu/drm/amd/amdgpu/amdgpu.h static int amdgpu_cs_parser_bos(struct · line 880

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +3 -5
       }
   -    r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
-                   &duplicates);
-    if (unlikely(r != 0)) {
-        if (r != -ERESTARTSYS)
-            DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
-        goto out_free_user_pages;
+    drm_exec_while_not_all_locked(&p->exec) {
+        r = amdgpu_vm_lock_pd(&fpriv->vm, &p->exec);
+        drm_exec_continue_on_contention(&p->exec);

Duplicate handling is needed for pretty much every call of amdgpu_vm_lock_pd, as bo->tbo.base.resv == vm->root.bo->tbo.base.resv for AMDGPU_GEM_CREATE_VM_ALWAYS_VALID.

Well no. AMDGPU_GEM_CREATE_VM_ALWAYS_VALID means that BOs should *not* be part of the relocation list. So when those cause an EALREADY here then userspace has a bug.

Quoted 1 line

I think Boris's suggestion of having this through a common DRM_EXEC_FLAG_ALLOW_DUPLICATES flag fits well.

No, again. The only driver which should accept duplicates is radeon, for all other drivers especially new ones duplicates should probably be rejected.

We only allow this for radeon because it is already UAPI, could be that we need to do this for amdgpu as well but I really hope we don't need this.

Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu.h static int amdgpu_cs_parser_bos(struct · line 885

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
+        if (unlikely(r))
+            goto out_free_user_pages;
+
+        amdgpu_bo_list_for_each_entry(e, p->bo_list) {
+            r = drm_exec_prepare_obj(&p->exec, &e->bo->tbo.base, 2);

Previously there were comments for how the fence count is calculated, now they seem to be removed. I'd prefer if they were properly retained as finding out who calls drm_resv_add_fence is not trivial, and wrong reservation count can also be really hard to debug.

I need to double check this, the reservation count looks incorrect in the first place.

Quoted 53 lines

Likewise for amdgpu_vm_lock_pd (which was added in another commit).

Quoted 50 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +39 -1
+ drm_exec_break_on_contention(&p->exec);
+            if (unlikely(r))
+                goto out_free_user_pages;
+
+            e->bo_va = amdgpu_vm_bo_find(vm, e->bo);
+            e->range = NULL;
+        }
+        drm_exec_continue_on_contention(&p->exec);
+
+        if (p->uf_bo) {
+            r = drm_exec_prepare_obj(&p->exec, &p->uf_bo->tbo.base,
+                         2);
+            drm_exec_continue_on_contention(&p->exec);
+            if (unlikely(r))
+                goto out_free_user_pages;
+        }
       }
   -    amdgpu_bo_list_for_each_entry(e, p->bo_list) {
-        struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
+    amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
+        struct mm_struct *usermm;
   -        e->bo_va = amdgpu_vm_bo_find(vm, bo);
+        usermm = amdgpu_ttm_tt_get_usermm(e->bo->tbo.ttm);
+        if (usermm && usermm != current->mm) {
+            r = -EPERM;
+            goto out_free_user_pages;
+        }
+
+        if (amdgpu_ttm_tt_is_userptr(e->bo->tbo.ttm) &&
+            e->user_invalidated && e->user_pages) {
+            amdgpu_bo_placement_from_domain(e->bo,
+                            AMDGPU_GEM_DOMAIN_CPU);
+            r = ttm_bo_validate(&e->bo->tbo, &e->bo->placement,
+                        &ctx);
+            if (r)
+                goto out_free_user_pages;
+
+            amdgpu_ttm_tt_set_user_pages(e->bo->tbo.ttm,
+                             e->user_pages);
+        }
+
+        kvfree(e->user_pages);
+        e->user_pages = NULL;
       }
         amdgpu_cs_get_threshold_for_moves(p->adev, &p->bytes_moved_threshold,
@@ -1296,9 +1271,8 @@ static int amdgpu_cs_submit(struct 
 amdgpu_cs_parser *p,
        */
       r = 0;
       amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu.h static int amdgpu_cs_submit(struct · line 1277

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -3
-        struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
-
-        r |= !amdgpu_ttm_tt_get_user_pages_done(bo->tbo.ttm, e->range);
+        r |= !amdgpu_ttm_tt_get_user_pages_done(e->bo->tbo.ttm,
+                            e->range);
           e->range = NULL;

e->range = NULL; needs to be removed, or it's causing *massive* memory leaks.

What? That sounds like nonsense to me amdgpu_ttm_tt_get_user_pages_done() frees the range it gets.

Quoted 19 lines
Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +0 -0
       }

      if (r) {

drivers/gpu/drm/amd/amdgpu/amdgpu.h +4 -1
@@ -1307,20 +1281,22 @@ static int amdgpu_cs_submit(struct 
 amdgpu_cs_parser *p,
       }
         p->fence = dma_fence_get(&leader->base.s_fence->finished);
-    list_for_each_entry(e, &p->validated, tv.head) {
+    drm_exec_for_each_locked_object(&p->exec, index, gobj) {
+
+ ttm_bo_move_to_lru_tail_unlocked(&gem_to_amdgpu_bo(gobj)->tbo);
             /* Everybody except for the gang leader uses READ */
           for (i = 0; i < p->gang_size; ++i) {
               if (p->jobs[i] == leader)
                   continue;
   -            dma_resv_add_fence(e->tv.bo->base.resv,
+            dma_resv_add_fence(gobj->resv,
 &p->jobs[i]->base.s_fence->finished,
                          DMA_RESV_USAGE_READ);
Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +0 -0
           }

  -        /* The gang leader is remembered as writer */
-        e->tv.num_shared = 0;
+        /* The gang leader as remembered as writer */
+        dma_resv_add_fence(gobj->resv, p->fence, DMA_RESV_USAGE_WRITE); }

Previously PD used READ accesses, now everything is WRITE. This probably isn't right.

That shouldn't matter. We should switch to using BOOKKEEP at some point, but for now that's irrelevant.

Thanks,
Christian.

Quoted 3 lines

Regards,
Tatsuyuki

#34 lore raw

Re: [PATCH 06/13] drm/amdgpu: use the new drm_exec object for CS v2

Quoted 33 lines · Christian König

Am 20.06.23 um 06:07 schrieb Tatsuyuki Ishi:

Quoted 20 lines

+Boris and +Matthew in case you want to take over this patch set.

Here are some review / testing comments, including those I posted before to ease tracking.

Quoted 15 lines · Christian König

Use the new component here as well and remove the old handling.

v2: drop dupplicate handling

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h         |   1 - drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c |  71 ++----- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h |   5 +-

  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c      | 210 +++++++++-----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h      |   7 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c      |  22 --
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h      |   3 -
  7 files changed, 115 insertions(+), 204 deletions(-)
Quoted 11 lines

drivers/gpu/drm/amd/amdgpu/amdgpu.h int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp, · line 144

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 02b827785e39..eba3e4f01ea6 100644
@@ -133,6 +141,8 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
         list->first_userptr = first_userptr;
       list->num_entries = num_entries;
+    sort(array, last_entry, sizeof(struct amdgpu_bo_list_entry),
+         amdgpu_bo_list_entry_cmp, NULL);

Previously amdgpu_bo_list_get_list sorted all entries, but this one only sorts userptr entries. I think this changes behavior?

Quoted 1 line

The intention here is to sort all entries except the userptrs. Need to double check.

Sorry, I mistyped. You're right that it sorts all entries except the userptrs. The previous code seems to sort all entries including userptrs.

Quoted 16 lines
Quoted 15 lines

drivers/gpu/drm/amd/amdgpu/amdgpu.h static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, · line 879

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu.h +3 -5
@@ -928,18 +874,56 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
           e->user_invalidated = userpage_invalidated;
       }
   -    r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
-                   &duplicates);
-    if (unlikely(r != 0)) {
-        if (r != -ERESTARTSYS)
-            DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
-        goto out_free_user_pages;
+    drm_exec_while_not_all_locked(&p->exec) {
+        r = amdgpu_vm_lock_pd(&fpriv->vm, &p->exec);
+        drm_exec_continue_on_contention(&p->exec);

Duplicate handling is needed for pretty much every call of amdgpu_vm_lock_pd, as bo->tbo.base.resv == vm->root.bo->tbo.base.resv for AMDGPU_GEM_CREATE_VM_ALWAYS_VALID.

Quoted 1 line

Well no. AMDGPU_GEM_CREATE_VM_ALWAYS_VALID means that BOs should *not* be part of the relocation list. So when those cause an EALREADY here then userspace has a bug.

Sounds fair, lemme check how RADV is handling this again.

Tatsuyuki

#35 lore raw

Re: [PATCH 06/13] drm/amdgpu: use the new drm_exec object for CS v2

Quoted 17 lines · Tatsuyuki Ishi
Quoted 16 lines · Christian König

Am 20.06.23 um 06:07 schrieb Tatsuyuki Ishi:

Quoted 14 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, · line 879

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -5
@@ -928,18 +874,56 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
           e->user_invalidated = userpage_invalidated;
       }
   -    r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
-                   &duplicates);
-    if (unlikely(r != 0)) {
-        if (r != -ERESTARTSYS)
-            DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
-        goto out_free_user_pages;
+    drm_exec_while_not_all_locked(&p->exec) {
+        r = amdgpu_vm_lock_pd(&fpriv->vm, &p->exec);
+        drm_exec_continue_on_contention(&p->exec);

Duplicate handling is needed for pretty much every call of amdgpu_vm_lock_pd, as bo->tbo.base.resv == vm->root.bo->tbo.base.resv for AMDGPU_GEM_CREATE_VM_ALWAYS_VALID.

Quoted 3 lines
Quoted 1 line

Well no. AMDGPU_GEM_CREATE_VM_ALWAYS_VALID means that BOs should *not* be part of the relocation list. So when those cause an EALREADY here then userspace has a bug.

Sounds fair, lemme check how RADV is handling this again.

I checked again and relocation list was actually fine, but other places were not. For example amdgpu_gem_object_close locks both bo->tbo.base.resv and vm->root.bo->tbo.base.resv (PD) on its own.

This was the easily debuggable case since it caused an error log but some other BO operations on ALWAYS_VALID is also presumably broken due to the same reason.

Tatsuyuki

maintainer
#36 lore raw

Re: [PATCH 06/13] drm/amdgpu: use the new drm_exec object for CS v2

In reply to Christian König
Quoted 9 lines · On Tue, 20 Jun 2023 10:12:13 +0200 Christian König…
Quoted 2 lines

I think Boris's suggestion of having this through a common DRM_EXEC_FLAG_ALLOW_DUPLICATES flag fits well.

No, again. The only driver which should accept duplicates is radeon, for all other drivers especially new ones duplicates should probably be rejected.

We only allow this for radeon because it is already UAPI, could be that we need to do this for amdgpu as well but I really hope we don't need this.

Just want to describe the use case we have: we support submission in batch (several jobs passed to the submit ioctl) with a submit-all-or-nothing model: if any of the job description is passed wrong args or causes an allocation error, we fail the whole group. In the submission path, we want to prepare GEMs for all jobs. That means adding enough fence slots for the number job finished fences. Given not all jobs will access the same set of BOs, I thought I could use duplicates support to make my life easier, because otherwise I have to collect all BOs upfront, store them in a temporary array, and keep track of the number of fence slots needed for each of them. I guess the other option would be to over-estimate the number of slots and make it equal to num_jobs for all BOs.

author reviewer
#37 lore raw

Re: [PATCH 06/13] drm/amdgpu: use the new drm_exec object for CS v2

Am 20.06.23 um 10:28 schrieb Boris Brezillon:

Quoted 11 lines
Quoted 8 lines · On Tue, 20 Jun 2023 10:12:13 +0200 Christian König…
Quoted 2 lines

I think Boris's suggestion of having this through a common DRM_EXEC_FLAG_ALLOW_DUPLICATES flag fits well.

No, again. The only driver which should accept duplicates is radeon, for all other drivers especially new ones duplicates should probably be rejected.

We only allow this for radeon because it is already UAPI, could be that we need to do this for amdgpu as well but I really hope we don't need this.

Quoted 12 lines

Just want to describe the use case we have: we support submission in batch (several jobs passed to the submit ioctl) with a submit-all-or-nothing model: if any of the job description is passed wrong args or causes an allocation error, we fail the whole group. In the submission path, we want to prepare GEMs for all jobs. That means adding enough fence slots for the number job finished fences. Given not all jobs will access the same set of BOs, I thought I could use duplicates support to make my life easier, because otherwise I have to collect all BOs upfront, store them in a temporary array, and keep track of the number of fence slots needed for each of them. I guess the other option would be to over-estimate the number of slots and make it equal to num_jobs for all BOs.

Sounds pretty much what amdgpu is doing as well, but question is why don't you give just one list of BOs? Do you really want to add the fences that fine grained?

For radeon it turned out that we just had stupid userspace which sometimes mentioned a BO in the list twice.

On the other hand over estimating the number of fences needed is perfectly fine as well, that is rounded up to the next kvmalloc size or even next page size anyway.

So IIRC and you have <510 fences you either get 14, 30, 62, 126, 254 and above 510 you should get it rounded to the next 512.

Regards,
Christian.

maintainer
#38 lore raw

Re: [PATCH 06/13] drm/amdgpu: use the new drm_exec object for CS v2

Quoted 25 lines · On Tue, 20 Jun 2023 10:44:26 +0200 Christian König…

Am 20.06.23 um 10:28 schrieb Boris Brezillon:

Quoted 11 lines
Quoted 8 lines · On Tue, 20 Jun 2023 10:12:13 +0200 Christian König…
Quoted 2 lines

I think Boris's suggestion of having this through a common DRM_EXEC_FLAG_ALLOW_DUPLICATES flag fits well.

No, again. The only driver which should accept duplicates is radeon, for all other drivers especially new ones duplicates should probably be rejected.

We only allow this for radeon because it is already UAPI, could be that we need to do this for amdgpu as well but I really hope we don't need this.

Quoted 12 lines

Just want to describe the use case we have: we support submission in batch (several jobs passed to the submit ioctl) with a submit-all-or-nothing model: if any of the job description is passed wrong args or causes an allocation error, we fail the whole group. In the submission path, we want to prepare GEMs for all jobs. That means adding enough fence slots for the number job finished fences. Given not all jobs will access the same set of BOs, I thought I could use duplicates support to make my life easier, because otherwise I have to collect all BOs upfront, store them in a temporary array, and keep track of the number of fence slots needed for each of them. I guess the other option would be to over-estimate the number of slots and make it equal to num_jobs for all BOs.

Quoted 3 lines

Sounds pretty much what amdgpu is doing as well, but question is why don't you give just one list of BOs? Do you really want to add the fences that fine grained?

Actually, we don't give a list of BOs at all, we pass a VM, and lock all BOs attached to the VM (similar to what Xe does). And, as all other drivers being submitted recently, we use explicit sync, so most of those VM BOs, except for the imported/exported ones, will be given a BOOKKEEP fence.

The reason we need support for duplicates is because we also have implicit BOs (like the HWRT object that's shared by the geometry/fragment queues to pass data around), and those can be passed to multiple jobs in a given batch and require special synchronization (geometry job writes to them, fragment job reads from them, so we have a reader/writer sync to express). I can of course de-duplicate upfront, by parsing jobs and creating an array of BOs that need to be acquired over the whole submission, but that's still one extra-step I'd prefer to avoid, given the dma_resv framework allows us to figure it out at lock time. I can also just deal with the EALREADY case in the driver directly, it's not like it's super complicated anyway, just thought other drivers would fall in the same situation, that's all.

Quoted 3 lines

For radeon it turned out that we just had stupid userspace which sometimes mentioned a BO in the list twice.

Okay, that's not the same thing, indeed.

Quoted 4 lines

On the other hand over estimating the number of fences needed is perfectly fine as well, that is rounded up to the next kvmalloc size or even next page size anyway.

Yeah, actually over-provisioning is not the most annoying part. Iterating over jobs to collect 'meta'-BOs is, so if I can just rely on EALREADY to detect that case and fallback to reserving an extra slot in that situation, I'd prefer that.

author reviewer
#39 lore raw

Re: [PATCH 06/13] drm/amdgpu: use the new drm_exec object for CS v2

Am 20.06.23 um 11:09 schrieb Boris Brezillon:

Quoted 36 lines
Quoted 24 lines · On Tue, 20 Jun 2023 10:44:26 +0200 Christian König…

Am 20.06.23 um 10:28 schrieb Boris Brezillon:

Quoted 11 lines
Quoted 8 lines · On Tue, 20 Jun 2023 10:12:13 +0200 Christian König…
Quoted 2 lines

I think Boris's suggestion of having this through a common DRM_EXEC_FLAG_ALLOW_DUPLICATES flag fits well.

No, again. The only driver which should accept duplicates is radeon, for all other drivers especially new ones duplicates should probably be rejected.

We only allow this for radeon because it is already UAPI, could be that we need to do this for amdgpu as well but I really hope we don't need this.

Quoted 12 lines

Just want to describe the use case we have: we support submission in batch (several jobs passed to the submit ioctl) with a submit-all-or-nothing model: if any of the job description is passed wrong args or causes an allocation error, we fail the whole group. In the submission path, we want to prepare GEMs for all jobs. That means adding enough fence slots for the number job finished fences. Given not all jobs will access the same set of BOs, I thought I could use duplicates support to make my life easier, because otherwise I have to collect all BOs upfront, store them in a temporary array, and keep track of the number of fence slots needed for each of them. I guess the other option would be to over-estimate the number of slots and make it equal to num_jobs for all BOs.

Quoted 3 lines

Sounds pretty much what amdgpu is doing as well, but question is why don't you give just one list of BOs? Do you really want to add the fences that fine grained?

Actually, we don't give a list of BOs at all, we pass a VM, and lock all BOs attached to the VM (similar to what Xe does). And, as all other drivers being submitted recently, we use explicit sync, so most of those VM BOs, except for the imported/exported ones, will be given a BOOKKEEP fence.

Quoted 12 lines

The reason we need support for duplicates is because we also have implicit BOs (like the HWRT object that's shared by the geometry/fragment queues to pass data around), and those can be passed to multiple jobs in a given batch and require special synchronization (geometry job writes to them, fragment job reads from them, so we have a reader/writer sync to express). I can of course de-duplicate upfront, by parsing jobs and creating an array of BOs that need to be acquired over the whole submission, but that's still one extra-step I'd prefer to avoid, given the dma_resv framework allows us to figure it out at lock time. I can also just deal with the EALREADY case in the driver directly, it's not like it's super complicated anyway, just thought other drivers would fall in the same situation, that's all.

Well as long as you just need to ignore EALREADY, that should be trivial and doable.

What radeon needs is to keep EALREADY BOs in a separate container because we need to double check their properties to not break the UAPI.

I strongly think that this shouldn't be needed by any other driver.

Going to add a flag to ignore EALREADY which can be set during exec init.

Regards,
Christian.

Quoted 12 lines
Quoted 3 lines

For radeon it turned out that we just had stupid userspace which sometimes mentioned a BO in the list twice.

Okay, that's not the same thing, indeed.

Quoted 3 lines

On the other hand over estimating the number of fences needed is perfectly fine as well, that is rounded up to the next kvmalloc size or even next page size anyway.

Yeah, actually over-provisioning is not the most annoying part. Iterating over jobs to collect 'meta'-BOs is, so if I can just rely on EALREADY to detect that case and fallback to reserving an extra slot in that situation, I'd prefer that.

maintainer
#40 lore raw

Re: [PATCH 06/13] drm/amdgpu: use the new drm_exec object for CS v2

Quoted 50 lines · On Tue, 20 Jun 2023 11:14:51 +0200 Christian König…

Am 20.06.23 um 11:09 schrieb Boris Brezillon:

Quoted 36 lines
Quoted 24 lines · On Tue, 20 Jun 2023 10:44:26 +0200 Christian König…

Am 20.06.23 um 10:28 schrieb Boris Brezillon:

Quoted 11 lines
Quoted 8 lines · On Tue, 20 Jun 2023 10:12:13 +0200 Christian König…
Quoted 2 lines

I think Boris's suggestion of having this through a common DRM_EXEC_FLAG_ALLOW_DUPLICATES flag fits well.

No, again. The only driver which should accept duplicates is radeon, for all other drivers especially new ones duplicates should probably be rejected.

We only allow this for radeon because it is already UAPI, could be that we need to do this for amdgpu as well but I really hope we don't need this.

Quoted 12 lines

Just want to describe the use case we have: we support submission in batch (several jobs passed to the submit ioctl) with a submit-all-or-nothing model: if any of the job description is passed wrong args or causes an allocation error, we fail the whole group. In the submission path, we want to prepare GEMs for all jobs. That means adding enough fence slots for the number job finished fences. Given not all jobs will access the same set of BOs, I thought I could use duplicates support to make my life easier, because otherwise I have to collect all BOs upfront, store them in a temporary array, and keep track of the number of fence slots needed for each of them. I guess the other option would be to over-estimate the number of slots and make it equal to num_jobs for all BOs.

Quoted 3 lines

Sounds pretty much what amdgpu is doing as well, but question is why don't you give just one list of BOs? Do you really want to add the fences that fine grained?

Actually, we don't give a list of BOs at all, we pass a VM, and lock all BOs attached to the VM (similar to what Xe does). And, as all other drivers being submitted recently, we use explicit sync, so most of those VM BOs, except for the imported/exported ones, will be given a BOOKKEEP fence.

Quoted 12 lines

The reason we need support for duplicates is because we also have implicit BOs (like the HWRT object that's shared by the geometry/fragment queues to pass data around), and those can be passed to multiple jobs in a given batch and require special synchronization (geometry job writes to them, fragment job reads from them, so we have a reader/writer sync to express). I can of course de-duplicate upfront, by parsing jobs and creating an array of BOs that need to be acquired over the whole submission, but that's still one extra-step I'd prefer to avoid, given the dma_resv framework allows us to figure it out at lock time. I can also just deal with the EALREADY case in the driver directly, it's not like it's super complicated anyway, just thought other drivers would fall in the same situation, that's all.

Quoted 2 lines

Well as long as you just need to ignore EALREADY, that should be trivial and doable.

Oh, yeah, that's all I need really. We probably don't want to add the GEM object a second time in the array though, hence the goto reserve_fences in my proposal when EALREADY is returned.

Quoted 7 lines

What radeon needs is to keep EALREADY BOs in a separate container because we need to double check their properties to not break the UAPI.

I strongly think that this shouldn't be needed by any other driver.

Going to add a flag to ignore EALREADY which can be set during exec init.

Thanks!

author reviewer
#41 lore raw

PATCH 7/13 drm/radeon: switch over to drm_exec

In reply to Christian König

Just a straightforward conversion without any optimization.

---
 drivers/gpu/drm/radeon/Kconfig         |  1 +
 drivers/gpu/drm/radeon/radeon.h        |  7 ++--
 drivers/gpu/drm/radeon/radeon_cs.c     | 45 +++++++++++++-------------
 drivers/gpu/drm/radeon/radeon_gem.c    | 40 +++++++++++++----------
 drivers/gpu/drm/radeon/radeon_object.c | 25 +++++++-------
 drivers/gpu/drm/radeon/radeon_object.h |  2 +-
 drivers/gpu/drm/radeon/radeon_vm.c     | 10 +++---
 7 files changed, 67 insertions(+), 63 deletions(-)
drivers/gpu/drm/radeon/Kconfig +1 -0
index e19d77d58810..2d5fb6240cec 100644
@@ -11,6 +11,7 @@ config DRM_RADEON
 	select DRM_SUBALLOC_HELPER
         select DRM_TTM
 	select DRM_TTM_HELPER
+	select DRM_EXEC
 	select SND_HDA_COMPONENT if SND_HDA_CORE
 	select POWER_SUPPLY
 	select HWMON
drivers/gpu/drm/radeon/radeon.h +4 -3
index 8afb03bbce29..37a932a5195f 100644
@@ -75,8 +75,8 @@
 
 #include <drm/ttm/ttm_bo.h>
 #include <drm/ttm/ttm_placement.h>
-#include <drm/ttm/ttm_execbuf_util.h>
 
+#include <drm/drm_exec.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_audio_component.h>
 #include <drm/drm_suballoc.h>
@@ -458,7 +458,8 @@ struct radeon_mman {
 
 struct radeon_bo_list {
 	struct radeon_bo		*robj;
-	struct ttm_validate_buffer	tv;
+	struct list_head		list;
+	bool				shared;
 	uint64_t			gpu_offset;
 	unsigned			preferred_domains;
 	unsigned			allowed_domains;
@@ -1031,6 +1032,7 @@ struct radeon_cs_parser {
 	struct radeon_bo_list	*vm_bos;
 	struct list_head	validated;
 	unsigned		dma_reloc_idx;
+	struct drm_exec		exec;
 	/* indices of various chunks */
 	struct radeon_cs_chunk  *chunk_ib;
 	struct radeon_cs_chunk  *chunk_relocs;
@@ -1044,7 +1046,6 @@ struct radeon_cs_parser {
 	u32			cs_flags;
 	u32			ring;
 	s32			priority;
-	struct ww_acquire_ctx	ticket;
 };
 
 static inline u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
drivers/gpu/drm/radeon/radeon_cs.c +23 -22

114 lines not on this page · raw message

drivers/gpu/drm/radeon/radeon_gem.c +23 -17
index bdc5af23f005..332c5439c10f 100644
@@ -625,33 +625,41 @@ int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
 static void radeon_gem_va_update_vm(struct radeon_device *rdev,
 				    struct radeon_bo_va *bo_va)
 {
-	struct ttm_validate_buffer tv, *entry;
-	struct radeon_bo_list *vm_bos;
-	struct ww_acquire_ctx ticket;
+	struct radeon_bo_list *vm_bos, *entry;
 	struct list_head list;
+	struct drm_exec exec;
 	unsigned domain;
 	int r;
 
 	INIT_LIST_HEAD(&list);
 
-	tv.bo = &bo_va->bo->tbo;
-	tv.num_shared = 1;
-	list_add(&tv.head, &list);
-
 	vm_bos = radeon_vm_get_bos(rdev, bo_va->vm, &list);
 	if (!vm_bos)
 		return;
 
-	r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL);
-	if (r)
-		goto error_free;
+	drm_exec_init(&exec, true);
+	drm_exec_while_not_all_locked(&exec) {
+		list_for_each_entry(entry, &list, list) {
+			r = drm_exec_prepare_obj(&exec, &entry->robj->tbo.base,
+						 1);
+			drm_exec_break_on_contention(&exec);
+			if (unlikely(r))
+				goto error_cleanup;
+		}
+		drm_exec_continue_on_contention(&exec);
 
-	list_for_each_entry(entry, &list, head) {
-		domain = radeon_mem_type_to_domain(entry->bo->resource->mem_type);
+		r = drm_exec_prepare_obj(&exec, &bo_va->bo->tbo.base, 1);
+		drm_exec_continue_on_contention(&exec);
+		if (unlikely(r))
+			goto error_cleanup;
+	}
+
+	list_for_each_entry(entry, &list, list) {
+		domain = radeon_mem_type_to_domain(entry->robj->tbo.resource->mem_type);
 		/* if anything is swapped out don't swap it in here,
 		   just abort and wait for the next CS */
 		if (domain == RADEON_GEM_DOMAIN_CPU)
-			goto error_unreserve;
+			goto error_cleanup;
 	}
 
 	mutex_lock(&bo_va->vm->mutex);
@@ -665,10 +673,8 @@ static void radeon_gem_va_update_vm(struct radeon_device *rdev,
 error_unlock:
 	mutex_unlock(&bo_va->vm->mutex);
 
-error_unreserve:
-	ttm_eu_backoff_reservation(&ticket, &list);
-
-error_free:
+error_cleanup:
+	drm_exec_fini(&exec);
 	kvfree(vm_bos);
 
 	if (r && r != -ERESTARTSYS)
drivers/gpu/drm/radeon/radeon_object.c +11 -14
index 10c0fbd9d2b4..508a6e9a2dca 100644
@@ -468,23 +468,26 @@ static u64 radeon_bo_get_threshold_for_moves(struct radeon_device *rdev)
 }
 
 int radeon_bo_list_validate(struct radeon_device *rdev,
-			    struct ww_acquire_ctx *ticket,
+			    struct drm_exec *exec,
 			    struct list_head *head, int ring)
 {
 	struct ttm_operation_ctx ctx = { true, false };
 	struct radeon_bo_list *lobj;
-	struct list_head duplicates;
-	int r;
 	u64 bytes_moved = 0, initial_bytes_moved;
 	u64 bytes_moved_threshold = radeon_bo_get_threshold_for_moves(rdev);
+	int r;
 
-	INIT_LIST_HEAD(&duplicates);
-	r = ttm_eu_reserve_buffers(ticket, head, true, &duplicates);
-	if (unlikely(r != 0)) {
-		return r;
+	drm_exec_while_not_all_locked(exec) {
+		list_for_each_entry(lobj, head, list) {
+			r = drm_exec_prepare_obj(exec, &lobj->robj->tbo.base,
+						 1);
+			drm_exec_break_on_contention(exec);
+			if (unlikely(r && r != -EALREADY))
+				return r;
+		}
 	}
 
-	list_for_each_entry(lobj, head, tv.head) {
+	list_for_each_entry(lobj, head, list) {
 		struct radeon_bo *bo = lobj->robj;
 		if (!bo->tbo.pin_count) {
 			u32 domain = lobj->preferred_domains;
@@ -523,7 +526,6 @@ int radeon_bo_list_validate(struct radeon_device *rdev,
 					domain = lobj->allowed_domains;
 					goto retry;
 				}
-				ttm_eu_backoff_reservation(ticket, head);
 				return r;
 			}
 		}
@@ -531,11 +533,6 @@ int radeon_bo_list_validate(struct radeon_device *rdev,
 		lobj->tiling_flags = bo->tiling_flags;
 	}
 
-	list_for_each_entry(lobj, &duplicates, tv.head) {
-		lobj->gpu_offset = radeon_bo_gpu_offset(lobj->robj);
-		lobj->tiling_flags = lobj->robj->tiling_flags;
-	}
-
 	return 0;
 }
 
drivers/gpu/drm/radeon/radeon_object.h +1 -1
index 39cc87a59a9a..d7bbb52db546 100644
@@ -152,7 +152,7 @@ extern void radeon_bo_force_delete(struct radeon_device *rdev);
 extern int radeon_bo_init(struct radeon_device *rdev);
 extern void radeon_bo_fini(struct radeon_device *rdev);
 extern int radeon_bo_list_validate(struct radeon_device *rdev,
-				   struct ww_acquire_ctx *ticket,
+				   struct drm_exec *exec,
 				   struct list_head *head, int ring);
 extern int radeon_bo_set_tiling_flags(struct radeon_bo *bo,
 				u32 tiling_flags, u32 pitch);
drivers/gpu/drm/radeon/radeon_vm.c +4 -6
index 987cabbf1318..647c4a07d92a 100644
@@ -142,10 +142,9 @@ struct radeon_bo_list *radeon_vm_get_bos(struct radeon_device *rdev,
 	list[0].robj = vm->page_directory;
 	list[0].preferred_domains = RADEON_GEM_DOMAIN_VRAM;
 	list[0].allowed_domains = RADEON_GEM_DOMAIN_VRAM;
-	list[0].tv.bo = &vm->page_directory->tbo;
-	list[0].tv.num_shared = 1;
+	list[0].shared = true;
 	list[0].tiling_flags = 0;
-	list_add(&list[0].tv.head, head);
+	list_add(&list[0].list, head);
 
 	for (i = 0, idx = 1; i <= vm->max_pde_used; i++) {
 		if (!vm->page_tables[i].bo)
@@ -154,10 +153,9 @@ struct radeon_bo_list *radeon_vm_get_bos(struct radeon_device *rdev,
 		list[idx].robj = vm->page_tables[i].bo;
 		list[idx].preferred_domains = RADEON_GEM_DOMAIN_VRAM;
 		list[idx].allowed_domains = RADEON_GEM_DOMAIN_VRAM;
-		list[idx].tv.bo = &list[idx].robj->tbo;
-		list[idx].tv.num_shared = 1;
+		list[idx].shared = true;
 		list[idx].tiling_flags = 0;
-		list_add(&list[idx++].tv.head, head);
+		list_add(&list[idx++].list, head);
 	}
 
 	return list;
Signature · 1 line
2.34.1
author reviewer
#42 lore raw

PATCH 8/13 drm/qxl: switch to using drm_exec

In reply to Christian König

Just a straightforward conversion without any optimization.

Only compile tested for now.

---
 drivers/gpu/drm/qxl/Kconfig       |  1 +
 drivers/gpu/drm/qxl/qxl_drv.h     |  7 ++--
 drivers/gpu/drm/qxl/qxl_release.c | 67 ++++++++++++++++---------------
 3 files changed, 39 insertions(+), 36 deletions(-)
drivers/gpu/drm/qxl/Kconfig +1 -0
index ca3f51c2a8fe..9c8e433be33e 100644
@@ -5,6 +5,7 @@ config DRM_QXL
 	select DRM_KMS_HELPER
 	select DRM_TTM
 	select DRM_TTM_HELPER
+	select DRM_EXEC
 	select CRC32
 	help
 	  QXL virtual GPU for Spice virtualization desktop integration.
drivers/gpu/drm/qxl/qxl_drv.h +4 -3
index ea993d7162e8..3e732648b332 100644
@@ -38,12 +38,12 @@
 
 #include <drm/drm_crtc.h>
 #include <drm/drm_encoder.h>
+#include <drm/drm_exec.h>
 #include <drm/drm_gem_ttm_helper.h>
 #include <drm/drm_ioctl.h>
 #include <drm/drm_gem.h>
 #include <drm/qxl_drm.h>
 #include <drm/ttm/ttm_bo.h>
-#include <drm/ttm/ttm_execbuf_util.h>
 #include <drm/ttm/ttm_placement.h>
 
 #include "qxl_dev.h"
@@ -101,7 +101,8 @@ struct qxl_gem {
 };
 
 struct qxl_bo_list {
-	struct ttm_validate_buffer tv;
+	struct qxl_bo		*bo;
+	struct list_head	list;
 };
 
 struct qxl_crtc {
@@ -151,7 +152,7 @@ struct qxl_release {
 	struct qxl_bo *release_bo;
 	uint32_t release_offset;
 	uint32_t surface_release_id;
-	struct ww_acquire_ctx ticket;
+	struct drm_exec	exec;
 	struct list_head bos;
 };
 
drivers/gpu/drm/qxl/qxl_release.c +34 -33

124 lines not on this page · raw message

Signature · 1 line
2.34.1
#43 lore raw

Re: [PATCH 08/13] drm/qxl: switch to using drm_exec

Hi

Am 04.05.23 um 13:51 schrieb Christian König:

Quoted 16 lines

Just a straightforward conversion without any optimization.

Only compile tested for now.

---
  drivers/gpu/drm/qxl/Kconfig       |  1 +
  drivers/gpu/drm/qxl/qxl_drv.h     |  7 ++--
  drivers/gpu/drm/qxl/qxl_release.c | 67 ++++++++++++++++---------------
  3 files changed, 39 insertions(+), 36 deletions(-)
drivers/gpu/drm/qxl/Kconfig +0 -0
index ca3f51c2a8fe..9c8e433be33e 100644

drivers/gpu/drm/qxl/Kconfig config DRM_QXL · line 8

Quoted 4 lines
drivers/gpu/drm/qxl/Kconfig +1 -0
@@ -5,6 +5,7 @@ config DRM_QXL
  	select DRM_KMS_HELPER
  	select DRM_TTM
  	select DRM_TTM_HELPER
+	select DRM_EXEC

Just some nitpicking, but can we try to keep these select statements sorted alphabetically?

Quoted 175 lines
drivers/gpu/drm/qxl/Kconfig +0 -0
  	select CRC32
  	help
  	  QXL virtual GPU for Spice virtualization desktop integration.
drivers/gpu/drm/qxl/qxl_drv.h +4 -3
index ea993d7162e8..3e732648b332 100644
@@ -38,12 +38,12 @@
 
  #include <drm/drm_crtc.h>
  #include <drm/drm_encoder.h>
+#include <drm/drm_exec.h>
  #include <drm/drm_gem_ttm_helper.h>
  #include <drm/drm_ioctl.h>
  #include <drm/drm_gem.h>
  #include <drm/qxl_drm.h>
  #include <drm/ttm/ttm_bo.h>
-#include <drm/ttm/ttm_execbuf_util.h>
  #include <drm/ttm/ttm_placement.h>
 
  #include "qxl_dev.h"
@@ -101,7 +101,8 @@ struct qxl_gem {
  };
 
  struct qxl_bo_list {
-	struct ttm_validate_buffer tv;
+	struct qxl_bo		*bo;
+	struct list_head	list;
  };
 
  struct qxl_crtc {
@@ -151,7 +152,7 @@ struct qxl_release {
  	struct qxl_bo *release_bo;
  	uint32_t release_offset;
  	uint32_t surface_release_id;
-	struct ww_acquire_ctx ticket;
+	struct drm_exec	exec;
  	struct list_head bos;
  };
 
drivers/gpu/drm/qxl/qxl_release.c +34 -33
index 368d26da0d6a..da7cd9cd58f9 100644
@@ -121,13 +121,11 @@ qxl_release_free_list(struct qxl_release *release)
  {
  	while (!list_empty(&release->bos)) {
  		struct qxl_bo_list *entry;
-		struct qxl_bo *bo;
 
  		entry = container_of(release->bos.next,
-				     struct qxl_bo_list, tv.head);
-		bo = to_qxl_bo(entry->tv.bo);
-		qxl_bo_unref(&bo);
-		list_del(&entry->tv.head);
+				     struct qxl_bo_list, list);
+		qxl_bo_unref(&entry->bo);
+		list_del(&entry->list);
  		kfree(entry);
  	}
  	release->release_bo = NULL;
@@ -172,8 +170,8 @@ int qxl_release_list_add(struct qxl_release *release, struct qxl_bo *bo)
  {
  	struct qxl_bo_list *entry;
 
-	list_for_each_entry(entry, &release->bos, tv.head) {
-		if (entry->tv.bo == &bo->tbo)
+	list_for_each_entry(entry, &release->bos, list) {
+		if (entry->bo == bo)
  			return 0;
  	}
 
@@ -182,9 +180,8 @@ int qxl_release_list_add(struct qxl_release *release, struct qxl_bo *bo)
  		return -ENOMEM;
 
  	qxl_bo_ref(bo);
-	entry->tv.bo = &bo->tbo;
-	entry->tv.num_shared = 0;
-	list_add_tail(&entry->tv.head, &release->bos);
+	entry->bo = bo;
+	list_add_tail(&entry->list, &release->bos);
  	return 0;
  }
 
@@ -221,21 +218,27 @@ int qxl_release_reserve_list(struct qxl_release *release, bool no_intr)
  	if (list_is_singular(&release->bos))
  		return 0;
 
-	ret = ttm_eu_reserve_buffers(&release->ticket, &release->bos,
-				     !no_intr, NULL);
-	if (ret)
-		return ret;
-
-	list_for_each_entry(entry, &release->bos, tv.head) {
-		struct qxl_bo *bo = to_qxl_bo(entry->tv.bo);
-
-		ret = qxl_release_validate_bo(bo);
-		if (ret) {
-			ttm_eu_backoff_reservation(&release->ticket, &release->bos);
-			return ret;
+	drm_exec_init(&release->exec, !no_intr);
+	drm_exec_while_not_all_locked(&release->exec) {
+		list_for_each_entry(entry, &release->bos, list) {
+			ret = drm_exec_prepare_obj(&release->exec,
+						   &entry->bo->tbo.base,
+						   1);
+			drm_exec_break_on_contention(&release->exec);
+			if (ret)
+				goto error;
  		}
  	}
+
+	list_for_each_entry(entry, &release->bos, list) {
+		ret = qxl_release_validate_bo(entry->bo);
+		if (ret)
+			goto error;
+	}
  	return 0;
+error:
+	drm_exec_fini(&release->exec);
+	return ret;
  }
 
  void qxl_release_backoff_reserve_list(struct qxl_release *release)
@@ -245,7 +248,7 @@ void qxl_release_backoff_reserve_list(struct qxl_release *release)
  	if (list_is_singular(&release->bos))
  		return;
 
-	ttm_eu_backoff_reservation(&release->ticket, &release->bos);
+	drm_exec_fini(&release->exec);
  }
 
  int qxl_alloc_surface_release_reserved(struct qxl_device *qdev,
@@ -404,18 +407,18 @@ void qxl_release_unmap(struct qxl_device *qdev,
 
  void qxl_release_fence_buffer_objects(struct qxl_release *release)
  {
-	struct ttm_buffer_object *bo;
  	struct ttm_device *bdev;
-	struct ttm_validate_buffer *entry;
+	struct qxl_bo_list *entry;
  	struct qxl_device *qdev;
+	struct qxl_bo *bo;
 
  	/* if only one object on the release its the release itself
  	   since these objects are pinned no need to reserve */
  	if (list_is_singular(&release->bos) || list_empty(&release->bos))
  		return;
 
-	bo = list_first_entry(&release->bos, struct ttm_validate_buffer, head)->bo;
-	bdev = bo->bdev;
+	bo = list_first_entry(&release->bos, struct qxl_bo_list, list)->bo;
+	bdev = bo->tbo.bdev;
  	qdev = container_of(bdev, struct qxl_device, mman.bdev);
 
  	/*
@@ -426,14 +429,12 @@ void qxl_release_fence_buffer_objects(struct qxl_release *release)
  		       release->id | 0xf0000000, release->base.seqno);
  	trace_dma_fence_emit(&release->base);
 
-	list_for_each_entry(entry, &release->bos, head) {
+	list_for_each_entry(entry, &release->bos, list) {
  		bo = entry->bo;
 
-		dma_resv_add_fence(bo->base.resv, &release->base,
+		dma_resv_add_fence(bo->tbo.base.resv, &release->base,
  				   DMA_RESV_USAGE_READ);
-		ttm_bo_move_to_lru_tail_unlocked(bo);
-		dma_resv_unlock(bo->base.resv);
+		ttm_bo_move_to_lru_tail_unlocked(&bo->tbo);
  	}
-	ww_acquire_fini(&release->ticket);
+	drm_exec_fini(&release->exec);
  }
-
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
author reviewer
#44 lore raw

Re: [PATCH 08/13] drm/qxl: switch to using drm_exec

Am 20.06.23 um 11:13 schrieb Thomas Zimmermann:

Quoted 14 lines

Hi

Am 04.05.23 um 13:51 schrieb Christian König:

Quoted 11 lines

Just a straightforward conversion without any optimization.

Only compile tested for now.

---
  drivers/gpu/drm/qxl/Kconfig       |  1 + drivers/gpu/drm/qxl/qxl_drv.h     |  7 ++-- drivers/gpu/drm/qxl/qxl_release.c | 67 ++++++++++++++++---------------

  3 files changed, 39 insertions(+), 36 deletions(-)
Quoted 11 lines
Quoted 9 lines
drivers/gpu/drm/qxl/Kconfig +0 -0
index ca3f51c2a8fe..9c8e433be33e 100644
@@ -5,6 +5,7 @@ config DRM_QXL

      select DRM_KMS_HELPER
      select DRM_TTM
      select DRM_TTM_HELPER
+    select DRM_EXEC

Just some nitpicking, but can we try to keep these select statements sorted alphabetically?

Sure and good point, going to apply that to other drivers as well.

Christian.

Quoted 161 lines
Quoted 160 lines

      select CRC32
      help
        QXL virtual GPU for Spice virtualization desktop integration.

drivers/gpu/drm/qxl/qxl_drv.h +4 -3
index ea993d7162e8..3e732648b332 100644
@@ -38,12 +38,12 @@
     #include <drm/drm_crtc.h>
   #include <drm/drm_encoder.h>
+#include <drm/drm_exec.h>
   #include <drm/drm_gem_ttm_helper.h>
   #include <drm/drm_ioctl.h>
   #include <drm/drm_gem.h>
   #include <drm/qxl_drm.h>
   #include <drm/ttm/ttm_bo.h>
-#include <drm/ttm/ttm_execbuf_util.h>
   #include <drm/ttm/ttm_placement.h>
     #include "qxl_dev.h"
@@ -101,7 +101,8 @@ struct qxl_gem {
   };
     struct qxl_bo_list {
-    struct ttm_validate_buffer tv;
+    struct qxl_bo        *bo;
+    struct list_head    list;
   };
     struct qxl_crtc {
@@ -151,7 +152,7 @@ struct qxl_release {
       struct qxl_bo *release_bo;
       uint32_t release_offset;
       uint32_t surface_release_id;
-    struct ww_acquire_ctx ticket;
+    struct drm_exec    exec;
       struct list_head bos;
   };

  diff --git a/drivers/gpu/drm/qxl/qxl_release.c b/drivers/gpu/drm/qxl/qxl_release.c index 368d26da0d6a..da7cd9cd58f9 100644

drivers/gpu/drm/qxl/qxl_release.c +5 -6
@@ -121,13 +121,11 @@ qxl_release_free_list(struct qxl_release *release)
   {
       while (!list_empty(&release->bos)) {
           struct qxl_bo_list *entry;
-        struct qxl_bo *bo;
             entry = container_of(release->bos.next,
-                     struct qxl_bo_list, tv.head);
-        bo = to_qxl_bo(entry->tv.bo);
-        qxl_bo_unref(&bo);
-        list_del(&entry->tv.head);
+                     struct qxl_bo_list, list);
+        qxl_bo_unref(&entry->bo);
+        list_del(&entry->list);
           kfree(entry);
       }
       release->release_bo = NULL;
@@ -172,8 +170,8 @@ int qxl_release_list_add(struct qxl_release 
 *release, struct qxl_bo *bo)
   {
       struct qxl_bo_list *entry;
   -    list_for_each_entry(entry, &release->bos, tv.head) {
-        if (entry->tv.bo == &bo->tbo)
+    list_for_each_entry(entry, &release->bos, list) {
+        if (entry->bo == bo)
               return 0;
       }

  @@ -182,9 +180,8 @@ int qxl_release_list_add(struct qxl_release *release, struct qxl_bo *bo) return -ENOMEM;
        qxl_bo_ref(bo);
-    entry->tv.bo = &bo->tbo;
-    entry->tv.num_shared = 0;
-    list_add_tail(&entry->tv.head, &release->bos);
+    entry->bo = bo;
+    list_add_tail(&entry->list, &release->bos);
      return 0;
  }
  @@ -221,21 +218,27 @@ int qxl_release_reserve_list(struct qxl_release *release, bool no_intr) if (list_is_singular(&release->bos))
          return 0;
  -    ret = ttm_eu_reserve_buffers(&release->ticket, &release->bos,
-                     !no_intr, NULL);
-    if (ret)
-        return ret;

drivers/gpu/drm/qxl/qxl_release.c +0 -7
-
-    list_for_each_entry(entry, &release->bos, tv.head) {
-        struct qxl_bo *bo = to_qxl_bo(entry->tv.bo);
-
-        ret = qxl_release_validate_bo(bo);
-        if (ret) {
-            ttm_eu_backoff_reservation(&release->ticket, 

&release->bos);
-            return ret;
+    drm_exec_init(&release->exec, !no_intr);
+    drm_exec_while_not_all_locked(&release->exec) {
+        list_for_each_entry(entry, &release->bos, list) { +            ret = drm_exec_prepare_obj(&release->exec,
+                           &entry->bo->tbo.base,
+                           1);
+ drm_exec_break_on_contention(&release->exec);
+            if (ret)
+                goto error;
          }
      }
+
+    list_for_each_entry(entry, &release->bos, list) {
+        ret = qxl_release_validate_bo(entry->bo);
+        if (ret)
+            goto error;
+    }
      return 0;
+error:
+    drm_exec_fini(&release->exec);
+    return ret;
  }
    void qxl_release_backoff_reserve_list(struct qxl_release *release)

drivers/gpu/drm/qxl/qxl_release.c +3 -2
@@ -245,7 +248,7 @@ void qxl_release_backoff_reserve_list(struct 
 qxl_release *release)
       if (list_is_singular(&release->bos))
           return;
   -    ttm_eu_backoff_reservation(&release->ticket, &release->bos);
+    drm_exec_fini(&release->exec);
   }
     int qxl_alloc_surface_release_reserved(struct qxl_device *qdev,
@@ -404,18 +407,18 @@ void qxl_release_unmap(struct qxl_device *qdev,
     void qxl_release_fence_buffer_objects(struct qxl_release *release)
   {
-    struct ttm_buffer_object *bo;
       struct ttm_device *bdev;
-    struct ttm_validate_buffer *entry;
+    struct qxl_bo_list *entry;
       struct qxl_device *qdev;
+    struct qxl_bo *bo;
         /* if only one object on the release its the release itself

         since these objects are pinned no need to reserve */
      if (list_is_singular(&release->bos) || list_empty(&release->bos)) return;
  -    bo = list_first_entry(&release->bos, struct ttm_validate_buffer, head)->bo; -    bdev = bo->bdev;
+    bo = list_first_entry(&release->bos, struct qxl_bo_list, list)->bo; +    bdev = bo->tbo.bdev;
      qdev = container_of(bdev, struct qxl_device, mman.bdev);
        /*

drivers/gpu/drm/qxl/qxl_release.c +4 -3
@@ -426,14 +429,12 @@ void qxl_release_fence_buffer_objects(struct 
 qxl_release *release)
                  release->id | 0xf0000000, release->base.seqno);
       trace_dma_fence_emit(&release->base);
   -    list_for_each_entry(entry, &release->bos, head) {
+    list_for_each_entry(entry, &release->bos, list) {
           bo = entry->bo;
   -        dma_resv_add_fence(bo->base.resv, &release->base,
+        dma_resv_add_fence(bo->tbo.base.resv, &release->base,
                      DMA_RESV_USAGE_READ);
-        ttm_bo_move_to_lru_tail_unlocked(bo);
-        dma_resv_unlock(bo->base.resv);
+        ttm_bo_move_to_lru_tail_unlocked(&bo->tbo);
       }
-    ww_acquire_fini(&release->ticket);
+    drm_exec_fini(&release->exec);

  }
-

author reviewer
#45 lore raw

PATCH 9/13 drm/lima: switch to using drm_exec

In reply to Christian König

Just a straightforward conversion without any optimization.

Only compile tested for now.

---
 drivers/gpu/drm/lima/Kconfig    |  1 +
 drivers/gpu/drm/lima/lima_gem.c | 15 +++++++--------
 2 files changed, 8 insertions(+), 8 deletions(-)
drivers/gpu/drm/lima/Kconfig +1 -0
index fa1d4f5df31e..1d2871d9ddd2 100644
@@ -9,6 +9,7 @@ config DRM_LIMA
        depends on COMMON_CLK
        depends on OF
        select DRM_SCHED
+       select DRM_EXEC
        select DRM_GEM_SHMEM_HELPER
        select PM_DEVFREQ
        select DEVFREQ_GOV_SIMPLE_ONDEMAND
drivers/gpu/drm/lima/lima_gem.c +7 -8
index 10252dc11a22..f48c1edff07d 100644
@@ -8,6 +8,7 @@
 #include <linux/shmem_fs.h>
 #include <linux/dma-mapping.h>
 
+#include <drm/drm_exec.h>
 #include <drm/drm_file.h>
 #include <drm/drm_syncobj.h>
 #include <drm/drm_utils.h>
@@ -292,7 +293,7 @@ static int lima_gem_add_deps(struct drm_file *file, struct lima_submit *submit)
 int lima_gem_submit(struct drm_file *file, struct lima_submit *submit)
 {
 	int i, err = 0;
-	struct ww_acquire_ctx ctx;
+	struct drm_exec exec;
 	struct lima_drm_priv *priv = to_lima_drm_priv(file);
 	struct lima_vm *vm = priv->vm;
 	struct drm_syncobj *out_sync = NULL;
@@ -329,8 +330,9 @@ int lima_gem_submit(struct drm_file *file, struct lima_submit *submit)
 		bos[i] = bo;
 	}
 
-	err = drm_gem_lock_reservations((struct drm_gem_object **)bos,
-					submit->nr_bos, &ctx);
+	drm_exec_init(&exec, true);
+	err = drm_exec_prepare_array(&exec, (struct drm_gem_object **)bos,
+				     submit->nr_bos, 0);
 	if (err)
 		goto err_out0;
 
@@ -360,9 +362,7 @@ int lima_gem_submit(struct drm_file *file, struct lima_submit *submit)
 				   submit->bos[i].flags & LIMA_SUBMIT_BO_WRITE ?
 				   DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_READ);
 	}
-
-	drm_gem_unlock_reservations((struct drm_gem_object **)bos,
-				    submit->nr_bos, &ctx);
+	drm_exec_fini(&exec);
 
 	for (i = 0; i < submit->nr_bos; i++)
 		drm_gem_object_put(&bos[i]->base.base);
@@ -379,8 +379,7 @@ int lima_gem_submit(struct drm_file *file, struct lima_submit *submit)
 err_out2:
 	lima_sched_task_fini(submit->task);
 err_out1:
-	drm_gem_unlock_reservations((struct drm_gem_object **)bos,
-				    submit->nr_bos, &ctx);
+	drm_exec_fini(&exec);
 err_out0:
 	for (i = 0; i < submit->nr_bos; i++) {
 		if (!bos[i])
Signature · 1 line
2.34.1
author reviewer
#46 lore raw

PATCH 10/13 drm/virtgpu: switch to using drm_exec

In reply to Christian König

Just a straightforward conversion without any optimization.

Only compile tested for now.

---
 drivers/gpu/drm/virtio/Kconfig       |  1 +
 drivers/gpu/drm/virtio/virtgpu_drv.h |  3 ++-
 drivers/gpu/drm/virtio/virtgpu_gem.c | 29 +++-------------------------
 3 files changed, 6 insertions(+), 27 deletions(-)
drivers/gpu/drm/virtio/Kconfig +1 -0
index ea06ff2aa4b4..a24a1ce5e666 100644
@@ -5,6 +5,7 @@ config DRM_VIRTIO_GPU
 	select VIRTIO
 	select DRM_KMS_HELPER
 	select DRM_GEM_SHMEM_HELPER
+	select DRM_EXEC
 	select VIRTIO_DMA_SHARED_BUFFER
 	help
 	   This is the virtual GPU driver for virtio.  It can be used with
drivers/gpu/drm/virtio/virtgpu_drv.h +2 -1
index af6ffb696086..c12434222e51 100644
@@ -35,6 +35,7 @@
 #include <drm/drm_atomic.h>
 #include <drm/drm_drv.h>
 #include <drm/drm_encoder.h>
+#include <drm/drm_exec.h>
 #include <drm/drm_fourcc.h>
 #include <drm/drm_framebuffer.h>
 #include <drm/drm_gem.h>
@@ -116,7 +117,7 @@ struct virtio_gpu_object_vram {
 	container_of((virtio_gpu_object), struct virtio_gpu_object_vram, base)
 
 struct virtio_gpu_object_array {
-	struct ww_acquire_ctx ticket;
+	struct drm_exec exec;
 	struct list_head next;
 	u32 nents, total;
 	struct drm_gem_object *objs[];
drivers/gpu/drm/virtio/virtgpu_gem.c +3 -26
index 7db48d17ee3a..bcab407074f4 100644
@@ -171,6 +171,7 @@ struct virtio_gpu_object_array *virtio_gpu_array_alloc(u32 nents)
 
 	objs->nents = 0;
 	objs->total = nents;
+	drm_exec_init(&objs->exec, true);
 	return objs;
 }
 
@@ -214,36 +215,12 @@ void virtio_gpu_array_add_obj(struct virtio_gpu_object_array *objs,
 
 int virtio_gpu_array_lock_resv(struct virtio_gpu_object_array *objs)
 {
-	unsigned int i;
-	int ret;
-
-	if (objs->nents == 1) {
-		ret = dma_resv_lock_interruptible(objs->objs[0]->resv, NULL);
-	} else {
-		ret = drm_gem_lock_reservations(objs->objs, objs->nents,
-						&objs->ticket);
-	}
-	if (ret)
-		return ret;
-
-	for (i = 0; i < objs->nents; ++i) {
-		ret = dma_resv_reserve_fences(objs->objs[i]->resv, 1);
-		if (ret) {
-			virtio_gpu_array_unlock_resv(objs);
-			return ret;
-		}
-	}
-	return ret;
+	return drm_exec_prepare_array(&objs->exec, objs->objs, objs->nents, 1);
 }
 
 void virtio_gpu_array_unlock_resv(struct virtio_gpu_object_array *objs)
 {
-	if (objs->nents == 1) {
-		dma_resv_unlock(objs->objs[0]->resv);
-	} else {
-		drm_gem_unlock_reservations(objs->objs, objs->nents,
-					    &objs->ticket);
-	}
+	drm_exec_fini(&objs->exec);
 }
 
 void virtio_gpu_array_add_fence(struct virtio_gpu_object_array *objs,
Signature · 1 line
2.34.1
author reviewer
#47 lore raw

PATCH 11/13 drm/panfrost: switch to using drm_exec

In reply to Christian König

Just a straightforward conversion without any optimization.

Only compile tested for now.

---
 drivers/gpu/drm/panfrost/Kconfig        |  1 +
 drivers/gpu/drm/panfrost/panfrost_job.c | 11 ++++++-----
 2 files changed, 7 insertions(+), 5 deletions(-)
drivers/gpu/drm/panfrost/Kconfig +1 -0
index e6403a9d66ad..e86a1a2fd8e1 100644
@@ -7,6 +7,7 @@ config DRM_PANFROST
 	depends on !GENERIC_ATOMIC64    # for IOMMU_IO_PGTABLE_LPAE
 	depends on MMU
 	select DRM_SCHED
+	select DRM_EXEC
 	select IOMMU_SUPPORT
 	select IOMMU_IO_PGTABLE_LPAE
 	select DRM_GEM_SHMEM_HELPER
drivers/gpu/drm/panfrost/panfrost_job.c +6 -5
index dbc597ab46fb..7086a6044355 100644
@@ -8,6 +8,7 @@
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/dma-resv.h>
+#include <drm/drm_exec.h>
 #include <drm/gpu_scheduler.h>
 #include <drm/panfrost_drm.h>
 
@@ -275,13 +276,13 @@ static void panfrost_attach_object_fences(struct drm_gem_object **bos,
 int panfrost_job_push(struct panfrost_job *job)
 {
 	struct panfrost_device *pfdev = job->pfdev;
-	struct ww_acquire_ctx acquire_ctx;
+	struct drm_exec exec;
 	int ret = 0;
 
-	ret = drm_gem_lock_reservations(job->bos, job->bo_count,
-					    &acquire_ctx);
+	drm_exec_init(&exec, true);
+	ret = drm_exec_prepare_array(&exec, job->bos, job->bo_count, 1);
 	if (ret)
-		return ret;
+		goto unlock;
 
 	mutex_lock(&pfdev->sched_lock);
 	drm_sched_job_arm(&job->base);
@@ -305,7 +306,7 @@ int panfrost_job_push(struct panfrost_job *job)
 				      job->render_done_fence);
 
 unlock:
-	drm_gem_unlock_reservations(job->bos, job->bo_count, &acquire_ctx);
+	drm_exec_fini(&exec);
 
 	return ret;
 }
Signature · 1 line
2.34.1
author reviewer
#48 lore raw

PATCH 12/13 drm/v3d: switch to using drm_exec

In reply to Christian König

Just a straightforward conversion without any optimization.

Only compile tested for now.

---
 drivers/gpu/drm/v3d/v3d_gem.c | 43 ++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 24 deletions(-)
drivers/gpu/drm/v3d/v3d_gem.c +19 -24
index 2e94ce788c71..75880ffc0cf1 100644
@@ -10,6 +10,7 @@
 #include <linux/sched/signal.h>
 #include <linux/uaccess.h>
 
+#include <drm/drm_exec.h>
 #include <drm/drm_managed.h>
 #include <drm/drm_syncobj.h>
 #include <uapi/drm/v3d_drm.h>
@@ -249,20 +250,16 @@ v3d_invalidate_caches(struct v3d_dev *v3d)
  * to v3d, so we don't attach dma-buf fences to them.
  */
 static int
-v3d_lock_bo_reservations(struct v3d_job *job,
-			 struct ww_acquire_ctx *acquire_ctx)
+v3d_lock_bo_reservations(struct v3d_job *job, struct drm_exec *exec)
 {
 	int i, ret;
 
-	ret = drm_gem_lock_reservations(job->bo, job->bo_count, acquire_ctx);
+	drm_exec_init(exec, true);
+	ret = drm_exec_prepare_array(exec, job->bo, job->bo_count, 1);
 	if (ret)
-		return ret;
+		goto fail;
 
 	for (i = 0; i < job->bo_count; i++) {
-		ret = dma_resv_reserve_fences(job->bo[i]->resv, 1);
-		if (ret)
-			goto fail;
-
 		ret = drm_sched_job_add_implicit_dependencies(&job->base,
 							      job->bo[i], true);
 		if (ret)
@@ -272,7 +269,7 @@ v3d_lock_bo_reservations(struct v3d_job *job,
 	return 0;
 
 fail:
-	drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
+	drm_exec_fini(exec);
 	return ret;
 }
 
@@ -477,7 +474,7 @@ v3d_push_job(struct v3d_job *job)
 static void
 v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv,
 					 struct v3d_job *job,
-					 struct ww_acquire_ctx *acquire_ctx,
+					 struct drm_exec *exec,
 					 u32 out_sync,
 					 struct v3d_submit_ext *se,
 					 struct dma_fence *done_fence)
@@ -492,7 +489,7 @@ v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv,
 				   DMA_RESV_USAGE_WRITE);
 	}
 
-	drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx);
+	drm_exec_fini(exec);
 
 	/* Update the return sync object for the job */
 	/* If it only supports a single signal semaphore*/
@@ -669,7 +666,7 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
 	struct v3d_render_job *render = NULL;
 	struct v3d_job *clean_job = NULL;
 	struct v3d_job *last_job;
-	struct ww_acquire_ctx acquire_ctx;
+	struct drm_exec exec;
 	int ret = 0;
 
 	trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end);
@@ -731,7 +728,7 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
 	if (ret)
 		goto fail;
 
-	ret = v3d_lock_bo_reservations(last_job, &acquire_ctx);
+	ret = v3d_lock_bo_reservations(last_job, &exec);
 	if (ret)
 		goto fail;
 
@@ -775,7 +772,7 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
 
 	v3d_attach_fences_and_unlock_reservation(file_priv,
 						 last_job,
-						 &acquire_ctx,
+						 &exec,
 						 args->out_sync,
 						 &se,
 						 last_job->done_fence);
@@ -791,8 +788,7 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
 fail_unreserve:
 	mutex_unlock(&v3d->sched_lock);
 fail_perfmon:
-	drm_gem_unlock_reservations(last_job->bo,
-				    last_job->bo_count, &acquire_ctx);
+	drm_exec_fini(&exec);
 fail:
 	v3d_job_cleanup((void *)bin);
 	v3d_job_cleanup((void *)render);
@@ -819,7 +815,7 @@ v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
 	struct drm_v3d_submit_tfu *args = data;
 	struct v3d_submit_ext se = {0};
 	struct v3d_tfu_job *job = NULL;
-	struct ww_acquire_ctx acquire_ctx;
+	struct drm_exec exec;
 	int ret = 0;
 
 	trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia);
@@ -870,7 +866,7 @@ v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
 		job->base.bo[job->base.bo_count] = bo;
 	}
 
-	ret = v3d_lock_bo_reservations(&job->base, &acquire_ctx);
+	ret = v3d_lock_bo_reservations(&job->base, &exec);
 	if (ret)
 		goto fail;
 
@@ -879,7 +875,7 @@ v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
 	mutex_unlock(&v3d->sched_lock);
 
 	v3d_attach_fences_and_unlock_reservation(file_priv,
-						 &job->base, &acquire_ctx,
+						 &job->base, &exec,
 						 args->out_sync,
 						 &se,
 						 job->base.done_fence);
@@ -914,7 +910,7 @@ v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
 	struct v3d_submit_ext se = {0};
 	struct v3d_csd_job *job = NULL;
 	struct v3d_job *clean_job = NULL;
-	struct ww_acquire_ctx acquire_ctx;
+	struct drm_exec exec;
 	int ret;
 
 	trace_v3d_submit_csd_ioctl(&v3d->drm, args->cfg[5], args->cfg[6]);
@@ -957,7 +953,7 @@ v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
 	if (ret)
 		goto fail;
 
-	ret = v3d_lock_bo_reservations(clean_job, &acquire_ctx);
+	ret = v3d_lock_bo_reservations(clean_job, &exec);
 	if (ret)
 		goto fail;
 
@@ -983,7 +979,7 @@ v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
 
 	v3d_attach_fences_and_unlock_reservation(file_priv,
 						 clean_job,
-						 &acquire_ctx,
+						 &exec,
 						 args->out_sync,
 						 &se,
 						 clean_job->done_fence);
@@ -996,8 +992,7 @@ v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
 fail_unreserve:
 	mutex_unlock(&v3d->sched_lock);
 fail_perfmon:
-	drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count,
-				    &acquire_ctx);
+	drm_exec_fini(&exec);
 fail:
 	v3d_job_cleanup((void *)job);
 	v3d_job_cleanup(clean_job);
Signature · 1 line
2.34.1
author reviewer
#49 lore raw

PATCH 13/13 drm: remove drm_gem_(un)lock_reservations

In reply to Christian König

Not used any more.

---
 drivers/gpu/drm/drm_gem.c              | 78 --------------------------
 drivers/gpu/drm/scheduler/sched_main.c |  5 +-
 include/drm/drm_gem.h                  |  4 --
 3 files changed, 2 insertions(+), 85 deletions(-)
drivers/gpu/drm/drm_gem.c +0 -78
index 1a5a2cd0d4ec..6666cd411002 100644
@@ -1214,84 +1214,6 @@ void drm_gem_vunmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map)
 }
 EXPORT_SYMBOL(drm_gem_vunmap_unlocked);
 
-/**
- * drm_gem_lock_reservations - Sets up the ww context and acquires
- * the lock on an array of GEM objects.
- *
- * Once you've locked your reservations, you'll want to set up space
- * for your shared fences (if applicable), submit your job, then
- * drm_gem_unlock_reservations().
- *
- * @objs: drm_gem_objects to lock
- * @count: Number of objects in @objs
- * @acquire_ctx: struct ww_acquire_ctx that will be initialized as
- * part of tracking this set of locked reservations.
- */
-int
-drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
-			  struct ww_acquire_ctx *acquire_ctx)
-{
-	int contended = -1;
-	int i, ret;
-
-	ww_acquire_init(acquire_ctx, &reservation_ww_class);
-
-retry:
-	if (contended != -1) {
-		struct drm_gem_object *obj = objs[contended];
-
-		ret = dma_resv_lock_slow_interruptible(obj->resv,
-								 acquire_ctx);
-		if (ret) {
-			ww_acquire_fini(acquire_ctx);
-			return ret;
-		}
-	}
-
-	for (i = 0; i < count; i++) {
-		if (i == contended)
-			continue;
-
-		ret = dma_resv_lock_interruptible(objs[i]->resv,
-							    acquire_ctx);
-		if (ret) {
-			int j;
-
-			for (j = 0; j < i; j++)
-				dma_resv_unlock(objs[j]->resv);
-
-			if (contended != -1 && contended >= i)
-				dma_resv_unlock(objs[contended]->resv);
-
-			if (ret == -EDEADLK) {
-				contended = i;
-				goto retry;
-			}
-
-			ww_acquire_fini(acquire_ctx);
-			return ret;
-		}
-	}
-
-	ww_acquire_done(acquire_ctx);
-
-	return 0;
-}
-EXPORT_SYMBOL(drm_gem_lock_reservations);
-
-void
-drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
-			    struct ww_acquire_ctx *acquire_ctx)
-{
-	int i;
-
-	for (i = 0; i < count; i++)
-		dma_resv_unlock(objs[i]->resv);
-
-	ww_acquire_fini(acquire_ctx);
-}
-EXPORT_SYMBOL(drm_gem_unlock_reservations);
-
 /**
  * drm_gem_lru_init - initialize a LRU
  *
drivers/gpu/drm/scheduler/sched_main.c +2 -3
index b09cdacfd062..2d8249148926 100644
@@ -794,9 +794,8 @@ EXPORT_SYMBOL(drm_sched_job_add_resv_dependencies);
  * @write: whether the job might write the object (so we need to depend on
  * shared fences in the reservation object).
  *
- * This should be called after drm_gem_lock_reservations() on your array of
- * GEM objects used in the job but before updating the reservations with your
- * own fences.
+ * This should be called after locking your GEM objects used in the job but
+ * before updating the reservations with your own fences.
  *
  * Returns:
  * 0 on success, or an error on failing to expand the array.
include/drm/drm_gem.h +0 -4
index b8efd836edef..7e027688a83d 100644
@@ -476,10 +476,6 @@ int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
 struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
 long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
 				    bool wait_all, unsigned long timeout);
-int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
-			      struct ww_acquire_ctx *acquire_ctx);
-void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
-				 struct ww_acquire_ctx *acquire_ctx);
 int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
 			    u32 handle, u64 *offset);
 
Signature · 1 line
2.34.1