Skip to contentSkip to conversation

dma-buf: add reservation_context for deadlock handling

PATCH In review Christian König 3 participants 6 messages Jun 25, 2019

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

PATCH 1/2 dma-buf: add reservation_context for deadlock handling

The ww_mutex framework allows for detecting deadlocks when multiple threads try to acquire the same set of locks in different order.

The problem is that handling those deadlocks was the burden of the user of the ww_mutex implementation and at least some users didn't got that right on the first try.

So introduce a new reservation_context object which can be used to simplify the deadlock handling. This is done by tracking all locked reservation objects in the context as well as the last contended reservation object.

When a deadlock occurse we now unlock all previously locked object and acquire the contended lock in the slow path. After this is done -EDEADLK is still returned to signal that all other locks now need to be re-acquired again.

---
 drivers/dma-buf/reservation.c | 82 +++++++++++++++++++++++++++++++++++
 include/linux/reservation.h   | 38 ++++++++++++++++
 2 files changed, 120 insertions(+)
drivers/dma-buf/reservation.c +82 -0
index 4d32e2c67862..9e53e42b053a 100644
@@ -55,6 +55,88 @@ EXPORT_SYMBOL(reservation_seqcount_class);
 const char reservation_seqcount_string[] = "reservation_seqcount";
 EXPORT_SYMBOL(reservation_seqcount_string);
 
+/**
+ * reservation_context_init - initialize a reservation context
+ * @ctx: the context to initialize
+ *
+ * Start using this reservation context to lock reservation objects for update.
+ */
+void reservation_context_init(struct reservation_context *ctx)
+{
+	ww_acquire_init(&ctx->ctx, &reservation_ww_class);
+	init_llist_head(&ctx->locked);
+	ctx->contended = NULL;
+}
+EXPORT_SYMBOL(reservation_context_init);
+
+/**
+ * reservation_context_unlock_all - unlock all reservation objects
+ * @ctx: the context which holds the reservation objects
+ *
+ * Unlocks all reservation objects locked with this context.
+ */
+void reservation_context_unlock_all(struct reservation_context *ctx)
+{
+	struct reservation_object *obj, *next;
+
+	if (ctx->contended)
+		ww_mutex_unlock(&ctx->contended->lock);
+	ctx->contended = NULL;
+
+	llist_for_each_entry_safe(obj, next, ctx->locked.first, locked)
+		ww_mutex_unlock(&obj->lock);
+	init_llist_head(&ctx->locked);
+}
+EXPORT_SYMBOL(reservation_context_unlock_all);
+
+/**
+ * reservation_context_lock - lock a reservation object with deadlock handling
+ * @ctx: the context which should be used to lock the object
+ * @obj: the object which needs to be locked
+ * @interruptible: if we should wait interruptible or not
+ *
+ * Use @ctx to lock the reservation object. If a deadlock is detected we backoff
+ * by releasing all locked objects and use the slow path to lock the reservation
+ * object. After successfully locking in the slow path -EDEADLK is returned to
+ * signal that all other locks must be re-taken as well.
+ */
+int reservation_context_lock(struct reservation_context *ctx,
+			     struct reservation_object *obj,
+			     bool interruptible)
+{
+	int ret = 0;
+
+	if (unlikely(ctx->contended == obj))
+		ctx->contended = NULL;
+	else if (interruptible)
+		ret = ww_mutex_lock_interruptible(&obj->lock, &ctx->ctx);
+	else
+		ret = ww_mutex_lock(&obj->lock, &ctx->ctx);
+
+	if (likely(!ret)) {
+		/* don't use llist_add here, we have separate locking */
+		obj->locked.next = ctx->locked.first;
+		ctx->locked.first = &obj->locked;
+		return 0;
+	}
+	if (unlikely(ret != -EDEADLK))
+		return ret;
+
+	reservation_context_unlock_all(ctx);
+
+	if (interruptible) {
+		ret = ww_mutex_lock_slow_interruptible(&obj->lock, &ctx->ctx);
+		if (unlikely(ret))
+			return ret;
+	} else {
+		ww_mutex_lock_slow(&obj->lock, &ctx->ctx);
+	}
+
+	ctx->contended = obj;
+	return -EDEADLK;
+}
+EXPORT_SYMBOL(reservation_context_lock);
+
 /**
  * reservation_object_reserve_shared - Reserve space to add shared fences to
  * a reservation_object.
include/linux/reservation.h +38 -0
index ee750765cc94..a8a52e5d3e80 100644
@@ -44,11 +44,48 @@
 #include <linux/slab.h>
 #include <linux/seqlock.h>
 #include <linux/rcupdate.h>
+#include <linux/llist.h>
 
 extern struct ww_class reservation_ww_class;
 extern struct lock_class_key reservation_seqcount_class;
 extern const char reservation_seqcount_string[];
 
+/**
+ * struct reservation_context - context to lock reservation objects
+ * @ctx: ww_acquire_ctx used for deadlock detection
+ * @locked: list of reservation objects locked in this context
+ * @contended: contended reservation object
+ */
+struct reservation_context {
+	struct ww_acquire_ctx ctx;
+	struct llist_head locked;
+	struct reservation_object *contended;
+};
+
+/**
+ * reservation_context_done - wrapper for ww_acquire_done
+ * @ctx: the reservation context which is done with locking
+ */
+static inline void reservation_context_done(struct reservation_context *ctx)
+{
+	ww_acquire_done(&ctx->ctx);
+}
+
+/**
+ * reservation_context_fini - wrapper for ww_acquire_fini
+ * @ctx: the reservation context which is finished
+ */
+static inline void reservation_context_fini(struct reservation_context *ctx)
+{
+	ww_acquire_fini(&ctx->ctx);
+}
+
+void reservation_context_init(struct reservation_context *ctx);
+void reservation_context_unlock_all(struct reservation_context *ctx);
+int reservation_context_lock(struct reservation_context *ctx,
+			     struct reservation_object *obj,
+			     bool interruptible);
+
 /**
  * struct reservation_object_list - a list of shared fences
  * @rcu: for internal use
@@ -71,6 +108,7 @@ struct reservation_object_list {
  */
 struct reservation_object {
 	struct ww_mutex lock;
+	struct llist_node locked;
 	seqcount_t seq;
 
 	struct dma_fence __rcu *fence_excl;
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 drm/ttm: switch ttm_execbuf_util to new reservation_context

Change ttm_execbuf_util to use the new reservation context object for deadlock handling.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h           |   2 +-
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |   4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c        |   4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c       |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       |   4 +-
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c |   2 +-
 drivers/gpu/drm/qxl/qxl_drv.h                 |   2 +-
 drivers/gpu/drm/qxl/qxl_release.c             |   2 +-
 drivers/gpu/drm/radeon/radeon.h               |   3 +-
 drivers/gpu/drm/radeon/radeon_gem.c           |   2 +-
 drivers/gpu/drm/radeon/radeon_object.c        |   2 +-
 drivers/gpu/drm/radeon/radeon_object.h        |   2 +-
 drivers/gpu/drm/ttm/ttm_execbuf_util.c        | 117 ++++++++----------
 drivers/gpu/drm/virtio/virtgpu_drv.h          |   2 +-
 drivers/gpu/drm/virtio/virtgpu_ioctl.c        |   4 +-
 drivers/gpu/drm/virtio/virtgpu_object.c       |   2 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_resource.c      |   8 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_validation.h    |   2 +-
 include/drm/ttm/ttm_execbuf_util.h            |  12 +-
 19 files changed, 83 insertions(+), 95 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +1 -1
index 19a00282e34c..f7f061f71b26 100644
@@ -457,7 +457,7 @@ struct amdgpu_cs_parser {
 	struct drm_sched_entity	*entity;
 
 	/* buffer objects */
-	struct ww_acquire_ctx		ticket;
+	struct reservation_context	ticket;
 	struct amdgpu_bo_list		*bo_list;
 	struct amdgpu_mn		*mn;
 	struct amdgpu_bo_list_entry	vm_pd;
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -2
index 81e0e758cc54..a9be6d1d4f76 100644
@@ -539,7 +539,7 @@ struct bo_vm_reservation_context {
 	struct amdgpu_bo_list_entry kfd_bo; /* BO list entry for the KFD BO */
 	unsigned int n_vms;		    /* Number of VMs reserved	    */
 	struct amdgpu_bo_list_entry *vm_pd; /* Array of VM BO list entries  */
-	struct ww_acquire_ctx ticket;	    /* Reservation ticket	    */
+	struct reservation_context ticket;  /* Reservation ticket	    */
 	struct list_head list, duplicates;  /* BO lists			    */
 	struct amdgpu_sync *sync;	    /* Pointer to sync object	    */
 	bool reserved;			    /* Whether BOs are reserved	    */
@@ -1772,7 +1772,7 @@ static int validate_invalid_user_pages(struct amdkfd_process_info *process_info)
 {
 	struct amdgpu_bo_list_entry *pd_bo_list_entries;
 	struct list_head resv_list, duplicates;
-	struct ww_acquire_ctx ticket;
+	struct reservation_context ticket;
 	struct amdgpu_sync sync;
 
 	struct amdgpu_vm *peer_vm;
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2 -2
index 3e2da24cd17a..0fc4c0c10ef8 100644
@@ -1322,7 +1322,7 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
 	amdgpu_job_free_resources(job);
 
 	trace_amdgpu_cs_ioctl(job);
-	amdgpu_vm_bo_trace_cs(&fpriv->vm, &p->ticket);
+	amdgpu_vm_bo_trace_cs(&fpriv->vm, &p->ticket.ctx);
 	priority = job->base.s_priority;
 	drm_sched_entity_push_job(&job->base, entity);
 
@@ -1726,7 +1726,7 @@ int amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
 	*map = mapping;
 
 	/* Double check that the BO is reserved by this CS */
-	if (READ_ONCE((*bo)->tbo.resv->lock.ctx) != &parser->ticket)
+	if (READ_ONCE((*bo)->tbo.resv->lock.ctx) != &parser->ticket.ctx)
 		return -EINVAL;
 
 	if (!((*bo)->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)) {
drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c +1 -1
index f660628e6af9..3e13850b2435 100644
@@ -65,7 +65,7 @@ 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 reservation_context ticket;
 	struct list_head list;
 	struct amdgpu_bo_list_entry pd;
 	struct ttm_validate_buffer csa_tv;
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -2
index ed25a4e14404..6e819d6a80e3 100644
@@ -158,7 +158,7 @@ void amdgpu_gem_object_close(struct drm_gem_object *obj,
 	struct amdgpu_bo_list_entry vm_pd;
 	struct list_head list, duplicates;
 	struct ttm_validate_buffer tv;
-	struct ww_acquire_ctx ticket;
+	struct reservation_context ticket;
 	struct amdgpu_bo_va *bo_va;
 	int r;
 
@@ -546,7 +546,7 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 	struct amdgpu_bo_va *bo_va;
 	struct amdgpu_bo_list_entry vm_pd;
 	struct ttm_validate_buffer tv;
-	struct ww_acquire_ctx ticket;
+	struct reservation_context ticket;
 	struct list_head list, duplicates;
 	uint64_t va_flags;
 	int r = 0;
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +1 -1
index 36ee8fe52f38..241bb4562240 100644
@@ -4193,7 +4193,7 @@ static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
 	struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
 	struct list_head list;
 	struct ttm_validate_buffer tv;
-	struct ww_acquire_ctx ticket;
+	struct reservation_context ticket;
 	uint64_t tiling_flags;
 	uint32_t domain;
 	int r;
drivers/gpu/drm/qxl/qxl_drv.h +1 -1
index 2896bb6fdbf4..3066fd9776bd 100644
@@ -154,7 +154,7 @@ struct qxl_release {
 	struct qxl_bo *release_bo;
 	uint32_t release_offset;
 	uint32_t surface_release_id;
-	struct ww_acquire_ctx ticket;
+	struct reservation_context ticket;
 	struct list_head bos;
 };
 
drivers/gpu/drm/qxl/qxl_release.c +1 -1
index 49f9a9385393..e392bdd82609 100644
@@ -459,6 +459,6 @@ void qxl_release_fence_buffer_objects(struct qxl_release *release)
 		reservation_object_unlock(bo->resv);
 	}
 	spin_unlock(&glob->lru_lock);
-	ww_acquire_fini(&release->ticket);
+	reservation_context_fini(&release->ticket);
 }
 
drivers/gpu/drm/radeon/radeon.h +2 -1
index 32808e50be12..04f94792be55 100644
@@ -1084,7 +1084,8 @@ struct radeon_cs_parser {
 	u32			cs_flags;
 	u32			ring;
 	s32			priority;
-	struct ww_acquire_ctx	ticket;
+
+	struct reservation_context	ticket;
 };
 
 static inline u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
drivers/gpu/drm/radeon/radeon_gem.c +1 -1
index 7411e69e2712..823ccb8dae20 100644
@@ -544,7 +544,7 @@ static void radeon_gem_va_update_vm(struct radeon_device *rdev,
 {
 	struct ttm_validate_buffer tv, *entry;
 	struct radeon_bo_list *vm_bos;
-	struct ww_acquire_ctx ticket;
+	struct reservation_context ticket;
 	struct list_head list;
 	unsigned domain;
 	int r;
drivers/gpu/drm/radeon/radeon_object.c +1 -1
index 36683de0300b..56b90903482a 100644
@@ -528,7 +528,7 @@ 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 reservation_context *ticket,
 			    struct list_head *head, int ring)
 {
 	struct ttm_operation_ctx ctx = { true, false };
drivers/gpu/drm/radeon/radeon_object.h +1 -1
index 9ffd8215d38a..9dd3b2fa3f9a 100644
@@ -141,7 +141,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 reservation_context *ticket,
 				   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/ttm/ttm_execbuf_util.c +52 -65

189 lines not on this page · raw message

drivers/gpu/drm/virtio/virtgpu_drv.h +1 -1
index b69ae10ca238..b69ac78079fd 100644
@@ -221,7 +221,7 @@ struct virtio_gpu_fpriv {
 /* virtio_ioctl.c */
 #define DRM_VIRTIO_NUM_IOCTLS 10
 extern struct drm_ioctl_desc virtio_gpu_ioctls[DRM_VIRTIO_NUM_IOCTLS];
-int virtio_gpu_object_list_validate(struct ww_acquire_ctx *ticket,
+int virtio_gpu_object_list_validate(struct reservation_context *ticket,
 				    struct list_head *head);
 void virtio_gpu_unref_list(struct list_head *head);
 
drivers/gpu/drm/virtio/virtgpu_ioctl.c +2 -2
index 24ebe049baa8..7b7fafe4b1f7 100644
@@ -54,7 +54,7 @@ static int virtio_gpu_map_ioctl(struct drm_device *dev, void *data,
 					 &virtio_gpu_map->offset);
 }
 
-int virtio_gpu_object_list_validate(struct ww_acquire_ctx *ticket,
+int virtio_gpu_object_list_validate(struct reservation_context *ticket,
 				    struct list_head *head)
 {
 	struct ttm_operation_ctx ctx = { false, false };
@@ -114,7 +114,7 @@ static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
 	struct list_head validate_list;
 	struct ttm_validate_buffer *buflist = NULL;
 	int i;
-	struct ww_acquire_ctx ticket;
+	struct reservation_context ticket;
 	struct sync_file *sync_file;
 	int in_fence_fd = exbuf->fence_fd;
 	int out_fence_fd = -1;
drivers/gpu/drm/virtio/virtgpu_object.c +1 -1
index b2da31310d24..0d18e5c39357 100644
@@ -142,7 +142,7 @@ int virtio_gpu_object_create(struct virtio_gpu_device *vgdev,
 		struct virtio_gpu_fence_driver *drv = &vgdev->fence_drv;
 		struct list_head validate_list;
 		struct ttm_validate_buffer mainbuf;
-		struct ww_acquire_ctx ticket;
+		struct reservation_context ticket;
 		unsigned long irq_flags;
 		bool signaled;
 
drivers/gpu/drm/vmwgfx/vmwgfx_resource.c +4 -4
index 1d38a8b2f2ec..94bd8033df4d 100644
@@ -443,7 +443,7 @@ void vmw_resource_unreserve(struct vmw_resource *res,
  *                  reserved and validated backup buffer.
  */
 static int
-vmw_resource_check_buffer(struct ww_acquire_ctx *ticket,
+vmw_resource_check_buffer(struct reservation_context *ticket,
 			  struct vmw_resource *res,
 			  bool interruptible,
 			  struct ttm_validate_buffer *val_buf)
@@ -535,7 +535,7 @@ int vmw_resource_reserve(struct vmw_resource *res, bool interruptible,
  * @val_buf:        Backup buffer information.
  */
 static void
-vmw_resource_backoff_reservation(struct ww_acquire_ctx *ticket,
+vmw_resource_backoff_reservation(struct reservation_context *ticket,
 				 struct ttm_validate_buffer *val_buf)
 {
 	struct list_head val_list;
@@ -558,7 +558,7 @@ vmw_resource_backoff_reservation(struct ww_acquire_ctx *ticket,
  * @res:            The resource to evict.
  * @interruptible:  Whether to wait interruptible.
  */
-static int vmw_resource_do_evict(struct ww_acquire_ctx *ticket,
+static int vmw_resource_do_evict(struct reservation_context *ticket,
 				 struct vmw_resource *res, bool interruptible)
 {
 	struct ttm_validate_buffer val_buf;
@@ -822,7 +822,7 @@ static void vmw_resource_evict_type(struct vmw_private *dev_priv,
 	struct vmw_resource *evict_res;
 	unsigned err_count = 0;
 	int ret;
-	struct ww_acquire_ctx ticket;
+	struct reservation_context ticket;
 
 	do {
 		spin_lock(&dev_priv->resource_lock);
drivers/gpu/drm/vmwgfx/vmwgfx_validation.h +1 -1
index 1d2322ad6fd5..ded66b3e0c4c 100644
@@ -77,7 +77,7 @@ struct vmw_validation_context {
 	struct list_head resource_ctx_list;
 	struct list_head bo_list;
 	struct list_head page_list;
-	struct ww_acquire_ctx ticket;
+	struct reservation_context ticket;
 	struct mutex *res_mutex;
 	unsigned int merge_dups;
 	unsigned int mem_size_left;
include/drm/ttm/ttm_execbuf_util.h +6 -6
index 7e46cc678e7e..82c7a25a70be 100644
@@ -52,20 +52,20 @@ struct ttm_validate_buffer {
 /**
  * function ttm_eu_backoff_reservation
  *
- * @ticket:   ww_acquire_ctx from reserve call
+ * @ticket:   reservation_context from reserve call
  * @list:     thread private list of ttm_validate_buffer structs.
  *
  * Undoes all buffer validation reservations for bos pointed to by
  * the list entries.
  */
 
-extern void ttm_eu_backoff_reservation(struct ww_acquire_ctx *ticket,
+extern void ttm_eu_backoff_reservation(struct reservation_context *ticket,
 				       struct list_head *list);
 
 /**
  * function ttm_eu_reserve_buffers
  *
- * @ticket:  [out] ww_acquire_ctx filled in by call, or NULL if only
+ * @ticket:  [out] reservation_context filled in by caller, or NULL if only
  *           non-blocking reserves should be tried.
  * @list:    thread private list of ttm_validate_buffer structs.
  * @intr:    should the wait be interruptible
@@ -97,14 +97,14 @@ extern void ttm_eu_backoff_reservation(struct ww_acquire_ctx *ticket,
  * has failed.
  */
 
-extern int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
+extern int ttm_eu_reserve_buffers(struct reservation_context *ticket,
 				  struct list_head *list, bool intr,
 				  struct list_head *dups, bool del_lru);
 
 /**
  * function ttm_eu_fence_buffer_objects.
  *
- * @ticket:      ww_acquire_ctx from reserve call
+ * @ticket:      reservation_context from reserve call
  * @list:        thread private list of ttm_validate_buffer structs.
  * @fence:       The new exclusive fence for the buffers.
  *
@@ -114,7 +114,7 @@ extern int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
  *
  */
 
-extern void ttm_eu_fence_buffer_objects(struct ww_acquire_ctx *ticket,
+extern void ttm_eu_fence_buffer_objects(struct reservation_context *ticket,
 					struct list_head *list,
 					struct dma_fence *fence);
 
Signature · 6 lines
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw
In reply to Christian König
Quoted 28 lines · Christian König

The ww_mutex framework allows for detecting deadlocks when multiple threads try to acquire the same set of locks in different order.

The problem is that handling those deadlocks was the burden of the user of the ww_mutex implementation and at least some users didn't got that right on the first try.

So introduce a new reservation_context object which can be used to simplify the deadlock handling. This is done by tracking all locked reservation objects in the context as well as the last contended reservation object.

When a deadlock occurse we now unlock all previously locked object and acquire the contended lock in the slow path. After this is done -EDEADLK is still returned to signal that all other locks now need to be re-acquired again.

---
 drivers/dma-buf/reservation.c | 82 +++++++++++++++++++++++++++++++++++
 include/linux/reservation.h   | 38 ++++++++++++++++
 2 files changed, 120 insertions(+)
drivers/dma-buf/reservation.c +0 -0
index 4d32e2c67862..9e53e42b053a 100644

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 62

Quoted 8 lines
drivers/dma-buf/reservation.c +5 -0
@@ -55,6 +55,88 @@ EXPORT_SYMBOL(reservation_seqcount_class);
 const char reservation_seqcount_string[] = "reservation_seqcount";
 EXPORT_SYMBOL(reservation_seqcount_string);
 
+/**
+ * reservation_context_init - initialize a reservation context
+ * @ctx: the context to initialize
+ *
+ * Start using this reservation context to lock reservation objects for update.

Bunch of hyperlinks here for more consistent story would be really nice in the kerneldoc.

Quoted 6 lines
drivers/dma-buf/reservation.c +6 -0
+ */
+void reservation_context_init(struct reservation_context *ctx)
+{
+	ww_acquire_init(&ctx->ctx, &reservation_ww_class);
+	init_llist_head(&ctx->locked);
+	ctx->contended = NULL;

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 78

Quoted 10 lines
drivers/dma-buf/reservation.c +10 -0
+}
+EXPORT_SYMBOL(reservation_context_init);
+
+/**
+ * reservation_context_unlock_all - unlock all reservation objects
+ * @ctx: the context which holds the reservation objects
+ *
+ * Unlocks all reservation objects locked with this context.
+ */
+void reservation_context_unlock_all(struct reservation_context *ctx)

I'd just call this reservation_unlock_all or so. Feel free to ignore the bikeshed.

Quoted 17 lines
drivers/dma-buf/reservation.c +17 -0
+{
+	struct reservation_object *obj, *next;
+
+	if (ctx->contended)
+		ww_mutex_unlock(&ctx->contended->lock);
+	ctx->contended = NULL;
+
+	llist_for_each_entry_safe(obj, next, ctx->locked.first, locked)
+		ww_mutex_unlock(&obj->lock);
+	init_llist_head(&ctx->locked);
+}
+EXPORT_SYMBOL(reservation_context_unlock_all);
+
+/**
+ * reservation_context_lock - lock a reservation object with deadlock handling
+ * @ctx: the context which should be used to lock the object
+ * @obj: the object which needs to be locked

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 105

Quoted 10 lines
drivers/dma-buf/reservation.c +10 -0
+ * @interruptible: if we should wait interruptible or not
+ *
+ * Use @ctx to lock the reservation object. If a deadlock is detected we backoff
+ * by releasing all locked objects and use the slow path to lock the reservation
+ * object. After successfully locking in the slow path -EDEADLK is returned to
+ * signal that all other locks must be re-taken as well.
+ */
+int reservation_context_lock(struct reservation_context *ctx,
+			     struct reservation_object *obj,
+			     bool interruptible)

reservation_lock_ctx is what we generally used in drm_modeset_lock, I like that bikeshed a bit better.

Also to stay in style I think the explicit set of functions is much better, i.e. reservation_lock_ctx, reservation_lock_interruptible_ctx and reservation_trylock_ctx (later useful for lru applications where you still want to drop the entire pile with resrvation_unlock_ctx).

That's what all the other locking things do. ttm_bo_reserve has a long list of parameters, and I can never remember which is which. I don't think that's a great style.

Another option for interruptible vs. not is to store that in the reservation_context and dtrt. Since generally interruptible or not is a propery of the top-level handler - you need be able to pass EDEADLCK all the way up anyway.

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 110

Quoted 5 lines
drivers/dma-buf/reservation.c +5 -0
+{
+	int ret = 0;
+
+	if (unlikely(ctx->contended == obj))
+		ctx->contended = NULL;

Imo cleaner to handle that with EALREADY filtering from the ww_mutex_lock.

Quoted 71 lines
drivers/dma-buf/reservation.c +29 -0
+	else if (interruptible)
+		ret = ww_mutex_lock_interruptible(&obj->lock, &ctx->ctx);
+	else
+		ret = ww_mutex_lock(&obj->lock, &ctx->ctx);
+
+	if (likely(!ret)) {
+		/* don't use llist_add here, we have separate locking */
+		obj->locked.next = ctx->locked.first;
+		ctx->locked.first = &obj->locked;
+		return 0;
+	}
+	if (unlikely(ret != -EDEADLK))
+		return ret;
+
+	reservation_context_unlock_all(ctx);
+
+	if (interruptible) {
+		ret = ww_mutex_lock_slow_interruptible(&obj->lock, &ctx->ctx);
+		if (unlikely(ret))
+			return ret;
+	} else {
+		ww_mutex_lock_slow(&obj->lock, &ctx->ctx);
+	}
+
+	ctx->contended = obj;
+	return -EDEADLK;
+}
+EXPORT_SYMBOL(reservation_context_lock);
+
 /**
  * reservation_object_reserve_shared - Reserve space to add shared fences to
  * a reservation_object.
include/linux/reservation.h +26 -0
index ee750765cc94..a8a52e5d3e80 100644
@@ -44,11 +44,48 @@
 #include <linux/slab.h>
 #include <linux/seqlock.h>
 #include <linux/rcupdate.h>
+#include <linux/llist.h>
 
 extern struct ww_class reservation_ww_class;
 extern struct lock_class_key reservation_seqcount_class;
 extern const char reservation_seqcount_string[];
 
+/**
+ * struct reservation_context - context to lock reservation objects
+ * @ctx: ww_acquire_ctx used for deadlock detection
+ * @locked: list of reservation objects locked in this context
+ * @contended: contended reservation object
+ */
+struct reservation_context {
+	struct ww_acquire_ctx ctx;
+	struct llist_head locked;
+	struct reservation_object *contended;
+};
+
+/**
+ * reservation_context_done - wrapper for ww_acquire_done
+ * @ctx: the reservation context which is done with locking
+ */
+static inline void reservation_context_done(struct reservation_context *ctx)
+{
+	ww_acquire_done(&ctx->ctx);
+}
+
+/**
+ * reservation_context_fini - wrapper for ww_acquire_fini
+ * @ctx: the reservation context which is finished
+ */

include/linux/reservation.h line 87

Quoted 10 lines
include/linux/reservation.h +10 -0
+static inline void reservation_context_fini(struct reservation_context *ctx)
+{
+	ww_acquire_fini(&ctx->ctx);
+}
+
+void reservation_context_init(struct reservation_context *ctx);
+void reservation_context_unlock_all(struct reservation_context *ctx);
+int reservation_context_lock(struct reservation_context *ctx,
+			     struct reservation_object *obj,
+			     bool interruptible);

Needs a __must_check.

include/linux/reservation.h struct reservation_object_list { · line 114

Quoted 12 lines
include/linux/reservation.h +2 -0
+
 /**
  * struct reservation_object_list - a list of shared fences
  * @rcu: for internal use
@@ -71,6 +108,7 @@ struct reservation_object_list {
  */
 struct reservation_object {
 	struct ww_mutex lock;
+	struct llist_node locked;
 	seqcount_t seq;
 
 	struct dma_fence __rcu *fence_excl;

Aside from the nits&bikesheds, I like.
-Daniel

Quoted 3 lines
Signature · 1 line
2.17.1
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#3 lore raw

Am 25.06.19 um 16:16 schrieb Daniel Vetter:

Quoted 80 lines · Christian König
Quoted 28 lines

The ww_mutex framework allows for detecting deadlocks when multiple threads try to acquire the same set of locks in different order.

The problem is that handling those deadlocks was the burden of the user of the ww_mutex implementation and at least some users didn't got that right on the first try.

So introduce a new reservation_context object which can be used to simplify the deadlock handling. This is done by tracking all locked reservation objects in the context as well as the last contended reservation object.

When a deadlock occurse we now unlock all previously locked object and acquire the contended lock in the slow path. After this is done -EDEADLK is still returned to signal that all other locks now need to be re-acquired again.

---
  drivers/dma-buf/reservation.c | 82 +++++++++++++++++++++++++++++++++++
  include/linux/reservation.h   | 38 ++++++++++++++++
  2 files changed, 120 insertions(+)
drivers/dma-buf/reservation.c +0 -0
index 4d32e2c67862..9e53e42b053a 100644

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 62

Quoted 8 lines
drivers/dma-buf/reservation.c +5 -0
@@ -55,6 +55,88 @@ EXPORT_SYMBOL(reservation_seqcount_class);
  const char reservation_seqcount_string[] = "reservation_seqcount";
  EXPORT_SYMBOL(reservation_seqcount_string);
 
+/**
+ * reservation_context_init - initialize a reservation context
+ * @ctx: the context to initialize
+ *
+ * Start using this reservation context to lock reservation objects for update.

Bunch of hyperlinks here for more consistent story would be really nice in the kerneldoc.

Quoted 6 lines
drivers/dma-buf/reservation.c +6 -0
+ */
+void reservation_context_init(struct reservation_context *ctx)
+{
+	ww_acquire_init(&ctx->ctx, &reservation_ww_class);
+	init_llist_head(&ctx->locked);
+	ctx->contended = NULL;

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 78

Quoted 10 lines
drivers/dma-buf/reservation.c +10 -0
+}
+EXPORT_SYMBOL(reservation_context_init);
+
+/**
+ * reservation_context_unlock_all - unlock all reservation objects
+ * @ctx: the context which holds the reservation objects
+ *
+ * Unlocks all reservation objects locked with this context.
+ */
+void reservation_context_unlock_all(struct reservation_context *ctx)

I'd just call this reservation_unlock_all or so. Feel free to ignore the bikeshed.

Quoted 21 lines
drivers/dma-buf/reservation.c +21 -0
+{
+	struct reservation_object *obj, *next;
+
+	if (ctx->contended)
+		ww_mutex_unlock(&ctx->contended->lock);
+	ctx->contended = NULL;
+
+	llist_for_each_entry_safe(obj, next, ctx->locked.first, locked)
+		ww_mutex_unlock(&obj->lock);
+	init_llist_head(&ctx->locked);
+}
+EXPORT_SYMBOL(reservation_context_unlock_all);
+
+/**
+ * reservation_context_lock - lock a reservation object with deadlock handling
+ * @ctx: the context which should be used to lock the object
+ * @obj: the object which needs to be locked
+ * @interruptible: if we should wait interruptible or not
+ *
+ * Use @ctx to lock the reservation object. If a deadlock is detected we backoff
+ * by releasing all locked objects and use the slow path to lock the reservation
Quoted 8 lines

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 105

Quoted 6 lines
drivers/dma-buf/reservation.c +6 -0
+ * object. After successfully locking in the slow path -EDEADLK is returned to
+ * signal that all other locks must be re-taken as well.
+ */
+int reservation_context_lock(struct reservation_context *ctx,
+			     struct reservation_object *obj,
+			     bool interruptible)

reservation_lock_ctx is what we generally used in drm_modeset_lock, I like that bikeshed a bit better.

Actually doesn't sound that good if you ask me.

Is reservation_lock_ctx the name of the function or the name of the structure?

Quoted 4 lines

Also to stay in style I think the explicit set of functions is much better, i.e. reservation_lock_ctx, reservation_lock_interruptible_ctx and reservation_trylock_ctx (later useful for lru applications where you still want to drop the entire pile with resrvation_unlock_ctx).

The problem is that I then will duplicate a lot of logic between reservation_lock_ctx and reservation_lock_interruptible_ctx.

Quoted 3 lines

That's what all the other locking things do. ttm_bo_reserve has a long list of parameters, and I can never remember which is which. I don't think that's a great style.

Yeah, I don't really like that either. It is one of the reasons why I want to get rid of it.

But duplicating implementations is not a good idea either. We could go down the wait_event_* wait of doing thins and implement everything in macros, but I don't really like that either.

Quoted 11 lines

Another option for interruptible vs. not is to store that in the reservation_context and dtrt. Since generally interruptible or not is a propery of the top-level handler - you need be able to pass EDEADLCK all the way up anyway.

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 110

Quoted 5 lines
drivers/dma-buf/reservation.c +5 -0
+{
+	int ret = 0;
+
+	if (unlikely(ctx->contended == obj))
+		ctx->contended = NULL;

Imo cleaner to handle that with EALREADY filtering from the ww_mutex_lock.

How do you want to do this? EALREADY handling is different for different users of this API.

Christian.

Quoted 95 lines
Quoted 72 lines
drivers/dma-buf/reservation.c +29 -0
+	else if (interruptible)
+		ret = ww_mutex_lock_interruptible(&obj->lock, &ctx->ctx);
+	else
+		ret = ww_mutex_lock(&obj->lock, &ctx->ctx);
+
+	if (likely(!ret)) {
+		/* don't use llist_add here, we have separate locking */
+		obj->locked.next = ctx->locked.first;
+		ctx->locked.first = &obj->locked;
+		return 0;
+	}
+	if (unlikely(ret != -EDEADLK))
+		return ret;
+
+	reservation_context_unlock_all(ctx);
+
+	if (interruptible) {
+		ret = ww_mutex_lock_slow_interruptible(&obj->lock, &ctx->ctx);
+		if (unlikely(ret))
+			return ret;
+	} else {
+		ww_mutex_lock_slow(&obj->lock, &ctx->ctx);
+	}
+
+	ctx->contended = obj;
+	return -EDEADLK;
+}
+EXPORT_SYMBOL(reservation_context_lock);
+
  /**
   * reservation_object_reserve_shared - Reserve space to add shared fences to
   * a reservation_object.
include/linux/reservation.h +26 -0
index ee750765cc94..a8a52e5d3e80 100644
@@ -44,11 +44,48 @@
  #include <linux/slab.h>
  #include <linux/seqlock.h>
  #include <linux/rcupdate.h>
+#include <linux/llist.h>
 
  extern struct ww_class reservation_ww_class;
  extern struct lock_class_key reservation_seqcount_class;
  extern const char reservation_seqcount_string[];
 
+/**
+ * struct reservation_context - context to lock reservation objects
+ * @ctx: ww_acquire_ctx used for deadlock detection
+ * @locked: list of reservation objects locked in this context
+ * @contended: contended reservation object
+ */
+struct reservation_context {
+	struct ww_acquire_ctx ctx;
+	struct llist_head locked;
+	struct reservation_object *contended;
+};
+
+/**
+ * reservation_context_done - wrapper for ww_acquire_done
+ * @ctx: the reservation context which is done with locking
+ */
+static inline void reservation_context_done(struct reservation_context *ctx)
+{
+	ww_acquire_done(&ctx->ctx);
+}
+
+/**
+ * reservation_context_fini - wrapper for ww_acquire_fini
+ * @ctx: the reservation context which is finished
+ */

include/linux/reservation.h line 87

Quoted 10 lines
include/linux/reservation.h +10 -0
+static inline void reservation_context_fini(struct reservation_context *ctx)
+{
+	ww_acquire_fini(&ctx->ctx);
+}
+
+void reservation_context_init(struct reservation_context *ctx);
+void reservation_context_unlock_all(struct reservation_context *ctx);
+int reservation_context_lock(struct reservation_context *ctx,
+			     struct reservation_object *obj,
+			     bool interruptible);

Needs a __must_check.

Quoted 11 lines
include/linux/reservation.h +2 -0
+
  /**
   * struct reservation_object_list - a list of shared fences
   * @rcu: for internal use
@@ -71,6 +108,7 @@ struct reservation_object_list {
   */
  struct reservation_object {
  	struct ww_mutex lock;
+	struct llist_node locked;
  	seqcount_t seq;
 
Quoted 7 lines

include/linux/reservation.h struct reservation_object_list { · line 114

Quoted 1 line
include/linux/reservation.h +0 -0
  	struct dma_fence __rcu *fence_excl;

Aside from the nits&bikesheds, I like.
-Daniel

Quoted 3 lines
Signature · 1 line
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw
Quoted 89 lines · Christian König

Am 25.06.19 um 16:16 schrieb Daniel Vetter:

Quoted 79 lines · Christian König
Quoted 28 lines

The ww_mutex framework allows for detecting deadlocks when multiple threads try to acquire the same set of locks in different order.

The problem is that handling those deadlocks was the burden of the user of the ww_mutex implementation and at least some users didn't got that right on the first try.

So introduce a new reservation_context object which can be used to simplify the deadlock handling. This is done by tracking all locked reservation objects in the context as well as the last contended reservation object.

When a deadlock occurse we now unlock all previously locked object and acquire the contended lock in the slow path. After this is done -EDEADLK is still returned to signal that all other locks now need to be re-acquired again.

---
  drivers/dma-buf/reservation.c | 82 +++++++++++++++++++++++++++++++++++
  include/linux/reservation.h   | 38 ++++++++++++++++
  2 files changed, 120 insertions(+)
drivers/dma-buf/reservation.c +0 -0
index 4d32e2c67862..9e53e42b053a 100644

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 61

Quoted 7 lines
drivers/dma-buf/reservation.c +5 -0
@@ -55,6 +55,88 @@ EXPORT_SYMBOL(reservation_seqcount_class);
  const char reservation_seqcount_string[] = "reservation_seqcount";
  EXPORT_SYMBOL(reservation_seqcount_string);
+/**
+ * reservation_context_init - initialize a reservation context
+ * @ctx: the context to initialize
+ *
+ * Start using this reservation context to lock reservation objects for update.

Bunch of hyperlinks here for more consistent story would be really nice in the kerneldoc.

Quoted 6 lines
drivers/dma-buf/reservation.c +6 -0
+ */
+void reservation_context_init(struct reservation_context *ctx)
+{
+	ww_acquire_init(&ctx->ctx, &reservation_ww_class);
+	init_llist_head(&ctx->locked);
+	ctx->contended = NULL;

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 78

Quoted 10 lines
drivers/dma-buf/reservation.c +10 -0
+}
+EXPORT_SYMBOL(reservation_context_init);
+
+/**
+ * reservation_context_unlock_all - unlock all reservation objects
+ * @ctx: the context which holds the reservation objects
+ *
+ * Unlocks all reservation objects locked with this context.
+ */
+void reservation_context_unlock_all(struct reservation_context *ctx)

I'd just call this reservation_unlock_all or so. Feel free to ignore the bikeshed.

Quoted 21 lines
drivers/dma-buf/reservation.c +21 -0
+{
+	struct reservation_object *obj, *next;
+
+	if (ctx->contended)
+		ww_mutex_unlock(&ctx->contended->lock);
+	ctx->contended = NULL;
+
+	llist_for_each_entry_safe(obj, next, ctx->locked.first, locked)
+		ww_mutex_unlock(&obj->lock);
+	init_llist_head(&ctx->locked);
+}
+EXPORT_SYMBOL(reservation_context_unlock_all);
+
+/**
+ * reservation_context_lock - lock a reservation object with deadlock handling
+ * @ctx: the context which should be used to lock the object
+ * @obj: the object which needs to be locked
+ * @interruptible: if we should wait interruptible or not
+ *
+ * Use @ctx to lock the reservation object. If a deadlock is detected we backoff
+ * by releasing all locked objects and use the slow path to lock the reservation
Quoted 8 lines

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 105

Quoted 6 lines
drivers/dma-buf/reservation.c +6 -0
+ * object. After successfully locking in the slow path -EDEADLK is returned to
+ * signal that all other locks must be re-taken as well.
+ */
+int reservation_context_lock(struct reservation_context *ctx,
+			     struct reservation_object *obj,
+			     bool interruptible)

reservation_lock_ctx is what we generally used in drm_modeset_lock, I like that bikeshed a bit better.

Quoted 4 lines

Actually doesn't sound that good if you ask me.

Is reservation_lock_ctx the name of the function or the name of the structure?

Ah we put the ctx argument last for everything, i.e. operates on the reservation_object as the main thing, but with the context-aware variant.

Quoted 8 lines
Quoted 5 lines

Also to stay in style I think the explicit set of functions is much better, i.e. reservation_lock_ctx, reservation_lock_interruptible_ctx and reservation_trylock_ctx (later useful for lru applications where you still want to drop the entire pile with resrvation_unlock_ctx).

The problem is that I then will duplicate a lot of logic between reservation_lock_ctx and reservation_lock_interruptible_ctx.

Yeah I know, but that seems to be the style for locking functions. It's mildly tedious in the shared code, but I do think cleaner and easier to read in the actual users.

Quoted 4 lines
Quoted 3 lines

That's what all the other locking things do. ttm_bo_reserve has a long list of parameters, and I can never remember which is which. I don't think that's a great style.

Quoted 11 lines

Yeah, I don't really like that either. It is one of the reasons why I want to get rid of it.

But duplicating implementations is not a good idea either. We could go down the wait_event_* wait of doing thins and implement everything in macros, but I don't really like that either.

Quoted 4 lines

Another option for interruptible vs. not is to store that in the reservation_context and dtrt. Since generally interruptible or not is a propery of the top-level handler - you need be able to pass EDEADLCK all the way up anyway.

^^ this is what I recommend if you like neither.

Quoted 10 lines
Quoted 7 lines

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 110

Quoted 6 lines
drivers/dma-buf/reservation.c +5 -0
+{
+	int ret = 0;
+
+	if (unlikely(ctx->contended == obj))
+		ctx->contended = NULL;

Imo cleaner to handle that with EALREADY filtering from the ww_mutex_lock.

How do you want to do this? EALREADY handling is different for different users of this API.

Oh right the amdgpu CS semantics of throwing an errno if a bo is listed twice. In drm_modeset_lock we could rely on this fully. Maybe add a comment that except for the contended case we have to pass EALREADY back to callers for reasons? Your kerneldoc is lacking a "Returns:" paragraph anyway, good to list the options there.

Cheers, Daniel

Quoted 107 lines

Christian.

Quoted 82 lines
Quoted 70 lines
drivers/dma-buf/reservation.c +29 -0
+	else if (interruptible)
+		ret = ww_mutex_lock_interruptible(&obj->lock, &ctx->ctx);
+	else
+		ret = ww_mutex_lock(&obj->lock, &ctx->ctx);
+
+	if (likely(!ret)) {
+		/* don't use llist_add here, we have separate locking */
+		obj->locked.next = ctx->locked.first;
+		ctx->locked.first = &obj->locked;
+		return 0;
+	}
+	if (unlikely(ret != -EDEADLK))
+		return ret;
+
+	reservation_context_unlock_all(ctx);
+
+	if (interruptible) {
+		ret = ww_mutex_lock_slow_interruptible(&obj->lock, &ctx->ctx);
+		if (unlikely(ret))
+			return ret;
+	} else {
+		ww_mutex_lock_slow(&obj->lock, &ctx->ctx);
+	}
+
+	ctx->contended = obj;
+	return -EDEADLK;
+}
+EXPORT_SYMBOL(reservation_context_lock);
+
  /**
   * reservation_object_reserve_shared - Reserve space to add shared fences to
   * a reservation_object.
include/linux/reservation.h +26 -0
index ee750765cc94..a8a52e5d3e80 100644
@@ -44,11 +44,48 @@
  #include <linux/slab.h>
  #include <linux/seqlock.h>
  #include <linux/rcupdate.h>
+#include <linux/llist.h>
  extern struct ww_class reservation_ww_class;
  extern struct lock_class_key reservation_seqcount_class;
  extern const char reservation_seqcount_string[];
+/**
+ * struct reservation_context - context to lock reservation objects
+ * @ctx: ww_acquire_ctx used for deadlock detection
+ * @locked: list of reservation objects locked in this context
+ * @contended: contended reservation object
+ */
+struct reservation_context {
+	struct ww_acquire_ctx ctx;
+	struct llist_head locked;
+	struct reservation_object *contended;
+};
+
+/**
+ * reservation_context_done - wrapper for ww_acquire_done
+ * @ctx: the reservation context which is done with locking
+ */
+static inline void reservation_context_done(struct reservation_context *ctx)
+{
+	ww_acquire_done(&ctx->ctx);
+}
+
+/**
+ * reservation_context_fini - wrapper for ww_acquire_fini
+ * @ctx: the reservation context which is finished
+ */

include/linux/reservation.h line 85

Quoted 10 lines
include/linux/reservation.h +10 -0
+static inline void reservation_context_fini(struct reservation_context *ctx)
+{
+	ww_acquire_fini(&ctx->ctx);
+}
+
+void reservation_context_init(struct reservation_context *ctx);
+void reservation_context_unlock_all(struct reservation_context *ctx);
+int reservation_context_lock(struct reservation_context *ctx,
+			     struct reservation_object *obj,
+			     bool interruptible);

Needs a __must_check.

Quoted 17 lines

include/linux/reservation.h struct reservation_object_list { · line 113

Quoted 11 lines
include/linux/reservation.h +2 -0
+
  /**
   * struct reservation_object_list - a list of shared fences
   * @rcu: for internal use
@@ -71,6 +108,7 @@ struct reservation_object_list {
   */
  struct reservation_object {
  	struct ww_mutex lock;
+	struct llist_node locked;
  	seqcount_t seq;
  	struct dma_fence __rcu *fence_excl;

Aside from the nits&bikesheds, I like.
-Daniel

Quoted 3 lines
Signature · 1 line
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#5 lore raw
In reply to Christian König
Quoted 8 lines · Christian König
drivers/dma-buf/reservation.c +0 -0
index 4d32e2c67862..9e53e42b053a 100644
@@ -55,6 +55,88 @@ EXPORT_SYMBOL(reservation_seqcount_class);
 const char reservation_seqcount_string[] = "reservation_seqcount";
 EXPORT_SYMBOL(reservation_seqcount_string);
 

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 67

Quoted 10 lines
drivers/dma-buf/reservation.c +10 -0
+/**
+ * reservation_context_init - initialize a reservation context
+ * @ctx: the context to initialize
+ *
+ * Start using this reservation context to lock reservation objects for update.
+ */
+void reservation_context_init(struct reservation_context *ctx)
+{
+	ww_acquire_init(&ctx->ctx, &reservation_ww_class);
+	init_llist_head(&ctx->locked);
	ctx->locked = NULL;
Quoted 10 lines
drivers/dma-buf/reservation.c +10 -0
+	ctx->contended = NULL;
+}
+EXPORT_SYMBOL(reservation_context_init);
+
+/**
+ * reservation_context_unlock_all - unlock all reservation objects
+ * @ctx: the context which holds the reservation objects
+ *
+ * Unlocks all reservation objects locked with this context.
+ */

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 87

Quoted 10 lines
drivers/dma-buf/reservation.c +10 -0
+void reservation_context_unlock_all(struct reservation_context *ctx)
+{
+	struct reservation_object *obj, *next;
+
+	if (ctx->contended)
+		ww_mutex_unlock(&ctx->contended->lock);
+	ctx->contended = NULL;
+
+	llist_for_each_entry_safe(obj, next, ctx->locked.first, locked)
+		ww_mutex_unlock(&obj->lock);
	for (obj = ctx->locked; obj && (next = obj->locked, true); obj = next)

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 88

Quoted 1 line
drivers/dma-buf/reservation.c +1 -0
+	init_llist_head(&ctx->locked);
	ctx->locked = NULL;
Quoted 21 lines
drivers/dma-buf/reservation.c +21 -0
+}
+EXPORT_SYMBOL(reservation_context_unlock_all);
+
+/**
+ * reservation_context_lock - lock a reservation object with deadlock handling
+ * @ctx: the context which should be used to lock the object
+ * @obj: the object which needs to be locked
+ * @interruptible: if we should wait interruptible or not
+ *
+ * Use @ctx to lock the reservation object. If a deadlock is detected we backoff
+ * by releasing all locked objects and use the slow path to lock the reservation
+ * object. After successfully locking in the slow path -EDEADLK is returned to
+ * signal that all other locks must be re-taken as well.
+ */
+int reservation_context_lock(struct reservation_context *ctx,
+			     struct reservation_object *obj,
+			     bool interruptible)
+{
+	int ret = 0;
+
+	if (unlikely(ctx->contended == obj))

drivers/dma-buf/reservation.c EXPORT_SYMBOL(reservation_seqcount_class); · line 119

Quoted 10 lines
drivers/dma-buf/reservation.c +10 -0
+		ctx->contended = NULL;
+	else if (interruptible)
+		ret = ww_mutex_lock_interruptible(&obj->lock, &ctx->ctx);
+	else
+		ret = ww_mutex_lock(&obj->lock, &ctx->ctx);
+
+	if (likely(!ret)) {
+		/* don't use llist_add here, we have separate locking */
+		obj->locked.next = ctx->locked.first;
+		ctx->locked.first = &obj->locked;

Since you're not actually using llist, could you then maybe open-code the whole thing and not rely on its implementation?

That is, it hardly seems worth the trouble, as shown the bits you do use are hardly longer than just writing it out. Also, I was confused by the use of llist, as I wondered where the concurrency came from.

Quoted 24 lines
drivers/dma-buf/reservation.c +19 -0
+		return 0;
+	}
+	if (unlikely(ret != -EDEADLK))
+		return ret;
+
+	reservation_context_unlock_all(ctx);
+
+	if (interruptible) {
+		ret = ww_mutex_lock_slow_interruptible(&obj->lock, &ctx->ctx);
+		if (unlikely(ret))
+			return ret;
+	} else {
+		ww_mutex_lock_slow(&obj->lock, &ctx->ctx);
+	}
+
+	ctx->contended = obj;
+	return -EDEADLK;
+}
+EXPORT_SYMBOL(reservation_context_lock);
 
include/linux/reservation.h +0 -0
index ee750765cc94..a8a52e5d3e80 100644

include/linux/reservation.h line 61

Quoted 4 lines
include/linux/reservation.h +3 -0
 
+struct reservation_context {
+	struct ww_acquire_ctx ctx;
+	struct llist_head locked;
	struct reservation_object *locked;

include/linux/reservation.h struct reservation_object_list { · line 111

Quoted 8 lines
include/linux/reservation.h +3 -0
+	struct reservation_object *contended;
+};
@@ -71,6 +108,7 @@ struct reservation_object_list {
  */
 struct reservation_object {
 	struct ww_mutex lock;
+	struct llist_node locked;
	struct reservation_object *locked;
Quoted 6 lines
include/linux/reservation.h +0 -0
 	seqcount_t seq;
 
 	struct dma_fence __rcu *fence_excl;
Signature · 1 line
2.17.1